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

@ -10,6 +10,7 @@ import { getBackendUrl } from "./helpers/backend-url";
*/
// Set up availability for a date using the API
// Includes retry logic to handle race conditions with database reset
async function setAvailability(page: Page, dateStr: string) {
const cookies = await page.context().cookies();
const authCookie = cookies.find((c) => c.name === "auth_token");
@ -18,21 +19,63 @@ async function setAvailability(page: Page, dateStr: string) {
throw new Error("No auth cookie found when trying to set availability");
}
const response = await page.request.put(`${getBackendUrl()}/api/admin/availability`, {
headers: {
Cookie: `auth_token=${authCookie.value}`,
"Content-Type": "application/json",
},
data: {
date: dateStr,
slots: [{ start_time: "09:00:00", end_time: "12:00:00" }],
},
});
const maxRetries = 3;
let lastError: Error | null = null;
if (!response.ok()) {
const body = await response.text();
throw new Error(`Failed to set availability: ${response.status()} - ${body}`);
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
const response = await page.request.put(`${getBackendUrl()}/api/admin/availability`, {
headers: {
Cookie: `auth_token=${authCookie.value}`,
"Content-Type": "application/json",
},
data: {
date: dateStr,
slots: [{ start_time: "09:00:00", end_time: "12:00:00" }],
},
});
if (response.ok()) {
// Verify the response indicates success
const body = await response.json();
if (body.date === dateStr && body.slots?.length > 0) {
return; // Success
}
throw new Error(`Unexpected availability response: ${JSON.stringify(body)}`);
}
const body = await response.text();
const error = new Error(`Failed to set availability: ${response.status()} - ${body}`);
// Don't retry on 4xx errors (client errors), only on 5xx (server errors)
if (response.status() >= 400 && response.status() < 500) {
throw error;
}
lastError = error;
// Don't retry on the last attempt
if (attempt < maxRetries - 1) {
// Exponential backoff: 200ms, 400ms, 800ms
const delay = 200 * Math.pow(2, attempt);
await new Promise((resolve) => setTimeout(resolve, delay));
continue;
}
} catch (error) {
lastError = error instanceof Error ? error : new Error(String(error));
// Don't retry on the last attempt
if (attempt < maxRetries - 1) {
// Exponential backoff: 200ms, 400ms, 800ms
const delay = 200 * Math.pow(2, attempt);
await new Promise((resolve) => setTimeout(resolve, delay));
continue;
}
}
}
// If we get here, all retries failed
throw new Error(`Failed to set availability after ${maxRetries} attempts: ${lastError?.message}`);
}
test.describe("Exchange Page - Regular User Access", () => {