mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-01-31 06:42:03 +00:00
chore: update CI configuration and enhance test stability
- Added deterministic API key and environment variables in e2e-tests.yml to ensure consistent test behavior. - Refactored CodexProvider tests to improve type safety and mock handling, ensuring reliable test execution. - Updated provider-factory tests to mock installation detection for CodexProvider, enhancing test isolation. - Adjusted Playwright configuration to conditionally use external backend, improving flexibility in test environments. - Enhanced kill-test-servers script to handle external server scenarios, ensuring proper cleanup of test processes. These changes improve the reliability and maintainability of the testing framework, leading to a more stable development experience.
This commit is contained in:
@@ -282,28 +282,40 @@ export async function apiListBranches(
|
||||
*/
|
||||
export async function authenticateWithApiKey(page: Page, apiKey: string): Promise<boolean> {
|
||||
try {
|
||||
// Ensure the backend is up before attempting login (especially in local runs where
|
||||
// the backend may be started separately from Playwright).
|
||||
const start = Date.now();
|
||||
while (Date.now() - start < 15000) {
|
||||
try {
|
||||
const health = await page.request.get(`${API_BASE_URL}/api/health`, {
|
||||
timeout: 3000,
|
||||
});
|
||||
if (health.ok()) break;
|
||||
} catch {
|
||||
// Retry
|
||||
}
|
||||
await page.waitForTimeout(250);
|
||||
}
|
||||
|
||||
// Ensure we're on a page (needed for cookies to work)
|
||||
const currentUrl = page.url();
|
||||
if (!currentUrl || currentUrl === 'about:blank') {
|
||||
await page.goto('http://localhost:3007', { waitUntil: 'domcontentloaded' });
|
||||
}
|
||||
|
||||
// Use browser context fetch to ensure cookies are set in the browser
|
||||
const response = await page.evaluate(
|
||||
async ({ url, apiKey }) => {
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({ apiKey }),
|
||||
});
|
||||
const data = await res.json();
|
||||
return { success: data.success, token: data.token };
|
||||
},
|
||||
{ url: `${API_BASE_URL}/api/auth/login`, apiKey }
|
||||
);
|
||||
// Use Playwright request API (tied to this browser context) to avoid flakiness
|
||||
// with cross-origin fetch inside page.evaluate.
|
||||
const loginResponse = await page.request.post(`${API_BASE_URL}/api/auth/login`, {
|
||||
data: { apiKey },
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
timeout: 15000,
|
||||
});
|
||||
const response = (await loginResponse.json().catch(() => null)) as {
|
||||
success?: boolean;
|
||||
token?: string;
|
||||
} | null;
|
||||
|
||||
if (response.success && response.token) {
|
||||
if (response?.success && response.token) {
|
||||
// Manually set the cookie in the browser context
|
||||
// The server sets a cookie named 'automaker_session' (see SESSION_COOKIE_NAME in auth.ts)
|
||||
await page.context().addCookies([
|
||||
@@ -322,22 +334,19 @@ export async function authenticateWithApiKey(page: Page, apiKey: string): Promis
|
||||
let attempts = 0;
|
||||
const maxAttempts = 10;
|
||||
while (attempts < maxAttempts) {
|
||||
const statusResponse = await page.evaluate(
|
||||
async ({ url }) => {
|
||||
const res = await fetch(url, {
|
||||
credentials: 'include',
|
||||
});
|
||||
return res.json();
|
||||
},
|
||||
{ url: `${API_BASE_URL}/api/auth/status` }
|
||||
);
|
||||
const statusRes = await page.request.get(`${API_BASE_URL}/api/auth/status`, {
|
||||
timeout: 5000,
|
||||
});
|
||||
const statusResponse = (await statusRes.json().catch(() => null)) as {
|
||||
authenticated?: boolean;
|
||||
} | null;
|
||||
|
||||
if (statusResponse.authenticated === true) {
|
||||
if (statusResponse?.authenticated === true) {
|
||||
return true;
|
||||
}
|
||||
attempts++;
|
||||
// Use a very short wait between polling attempts (this is acceptable for polling)
|
||||
await page.waitForFunction(() => true, { timeout: 50 });
|
||||
await page.waitForTimeout(50);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user