Compare commits

..

1 Commits

Author SHA1 Message Date
zhi
766f5ef33e fix: wait for backend ready in global-setup, add login retry in milestone test
Root cause: milestone test was the first to run after global-setup completed
the wizard configuration. The backend needed time to detect the config file
and run database migrations, but tests started immediately. This caused
/auth/token requests to fail with net::ERR_FAILED.

Changes:
- global-setup.ts: after wizard setup, poll backend /docs until it returns 200
- milestone.spec.ts: add retry loop (max 3 attempts) for login as extra safety
  and assert token presence before proceeding
2026-03-16 06:17:29 +00:00
2 changed files with 58 additions and 24 deletions

View File

@@ -74,6 +74,28 @@ async function setupWizard() {
} else {
console.log('⚠️ Wizard already configured or on main page');
}
// Wait for backend to be ready (it starts after config file is written)
const backendURL = process.env.BACKEND_URL || 'http://backend:8000';
console.log('⏳ Waiting for backend to be ready...');
const maxRetries = 30;
for (let i = 0; i < maxRetries; i++) {
try {
const resp = await page.request.get(`${backendURL}/docs`, { timeout: 3000 });
if (resp.ok()) {
console.log('✅ Backend is ready!');
break;
}
} catch {
// backend not ready yet
}
if (i === maxRetries - 1) {
console.warn('⚠️ Backend did not respond after retries, proceeding anyway...');
} else {
console.log(` Waiting for backend... (${i + 1}/${maxRetries})`);
await new Promise(r => setTimeout(r, 2000));
}
}
await browser.close();
return;

View File

@@ -7,42 +7,54 @@ const ADMIN_PASSWORD = 'admin123';
test.describe('Milestone Editor', () => {
test('login -> create project -> create milestone through frontend form', async ({ page }) => {
// Step 1: Login
// Step 1: Login (with retry — backend may still be warming up)
console.log('🔐 Logging in...');
page.on('requestfailed', request => {
if (request.url().includes('/auth/token')) {
console.log('Login request failed:', request.failure()?.errorText);
const MAX_LOGIN_RETRIES = 3;
for (let attempt = 1; attempt <= MAX_LOGIN_RETRIES; attempt++) {
console.log(`Login attempt ${attempt}/${MAX_LOGIN_RETRIES}`);
await page.goto(`${BASE_URL}/login`);
await page.waitForLoadState('networkidle');
if (attempt === 1) {
const backendBase = await page.evaluate(() => localStorage.getItem('HF_BACKEND_BASE_URL'));
console.log('HF_BACKEND_BASE_URL:', backendBase);
}
});
await page.goto(`${BASE_URL}/login`);
await page.waitForLoadState('networkidle');
await page.fill('input[type="text"], input[name="username"]', ADMIN_USERNAME);
await page.fill('input[type="password"], input[name="password"]', ADMIN_PASSWORD);
await page.fill('input[type="text"], input[name="username"]', ADMIN_USERNAME);
await page.fill('input[type="password"], input[name="password"]', ADMIN_PASSWORD);
const loginPromise = page.waitForResponse(response =>
response.url().includes('/auth/token') && response.request().method() === 'POST'
, { timeout: 10000 }).catch(() => null);
const loginPromise = page.waitForResponse(response =>
response.url().includes('/auth/token') && response.request().method() === 'POST'
, { timeout: 15000 }).catch(() => null);
await page.click('button[type="submit"], button:has-text("Sign in")');
await page.click('button[type="submit"], button:has-text("Sign in")');
const loginResponse = await loginPromise;
if (loginResponse) {
console.log('Login response status:', loginResponse.status());
console.log('Login response:', (await loginResponse.text()).substring(0, 200));
} else {
console.log('No /auth/token response');
const loginResponse = await loginPromise;
if (loginResponse) {
console.log('Login response status:', loginResponse.status());
if (loginResponse.status() === 200) {
// Wait for navigation after successful login
await page.waitForURL(`${BASE_URL}/**`, { timeout: 10000 }).catch(() => {});
await page.waitForLoadState('networkidle');
break;
}
console.log('Login response:', (await loginResponse.text()).substring(0, 200));
} else {
console.log('No /auth/token response (backend may not be ready)');
}
if (attempt < MAX_LOGIN_RETRIES) {
console.log(`Retrying login in 3s...`);
await page.waitForTimeout(3000);
}
}
// Wait for navigation
await page.waitForURL(`${BASE_URL}/**`, { timeout: 10000 });
await page.waitForLoadState('networkidle');
const token = await page.evaluate(() => localStorage.getItem('token'));
console.log('Token after login:', token ? 'present' : 'missing');
console.log('Current URL:', page.url());
expect(token, 'Login failed — no token obtained after retries').toBeTruthy();
await page.waitForSelector('a:has-text("📁 Projects")', { timeout: 10000 });
console.log('✅ Logged in');