refactors

This commit is contained in:
counterweight 2025-12-26 20:04:46 +01:00
parent 4e1a339432
commit 82c4d0168e
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
28 changed files with 1042 additions and 782 deletions

View file

@ -9,13 +9,41 @@ import { getBackendUrl } from "./backend-url";
/**
* Reset the database for the current worker.
* Truncates all tables and re-seeds base data.
* Retries up to 3 times with exponential backoff to handle transient failures.
*/
export async function resetDatabase(request: APIRequestContext): Promise<void> {
const backendUrl = getBackendUrl();
const response = await request.post(`${backendUrl}/api/test/reset`);
const maxRetries = 3;
let lastError: Error | null = null;
if (!response.ok()) {
const text = await response.text();
throw new Error(`Failed to reset database: ${response.status()} - ${text}`);
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
const response = await request.post(`${backendUrl}/api/test/reset`);
if (response.ok()) {
// Verify the response body indicates success
const body = await response.json();
if (body.status === "reset") {
return; // Success
}
throw new Error(`Unexpected reset response: ${JSON.stringify(body)}`);
}
const text = await response.text();
throw new Error(`Failed to reset database: ${response.status()} - ${text}`);
} catch (error) {
lastError = error instanceof Error ? error : new Error(String(error));
// Don't retry on the last attempt
if (attempt < maxRetries - 1) {
// Exponential backoff: 100ms, 200ms, 400ms
const delay = 100 * Math.pow(2, attempt);
await new Promise((resolve) => setTimeout(resolve, delay));
continue;
}
}
}
// If we get here, all retries failed
throw new Error(`Failed to reset database after ${maxRetries} attempts: ${lastError?.message}`);
}

View file

@ -22,13 +22,12 @@ test.beforeEach(async ({ context, request }, testInfo) => {
process.env.NEXT_PUBLIC_API_URL = backendUrl;
// Reset database before each test for isolation
try {
await resetDatabase(request);
} catch (error) {
// If reset fails, log but don't fail the test
// This allows tests to run even if reset endpoint is unavailable
console.warn(`Failed to reset database: ${error}`);
}
// This must complete successfully before tests run to avoid race conditions
await resetDatabase(request);
// Small delay to ensure database transaction commits are visible
// This prevents race conditions where tests start before reset completes
await new Promise((resolve) => setTimeout(resolve, 100));
// Add init script to set English language before any page loads
// This must be called before any page.goto() calls