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

@ -48,24 +48,43 @@ async function loginUser(page: Page, email: string, password: string) {
}
// Helper to clear profile data via API
// Verifies the operation succeeds to prevent race conditions
async function clearProfileData(page: Page) {
const cookies = await page.context().cookies();
const authCookie = cookies.find((c) => c.name === "auth_token");
if (authCookie) {
await page.request.put(`${getBackendUrl()}/api/profile`, {
headers: {
Cookie: `auth_token=${authCookie.value}`,
"Content-Type": "application/json",
},
data: {
contact_email: null,
telegram: null,
signal: null,
nostr_npub: null,
},
});
if (!authCookie) {
throw new Error("No auth cookie found when trying to clear profile data");
}
const response = await page.request.put(`${getBackendUrl()}/api/profile`, {
headers: {
Cookie: `auth_token=${authCookie.value}`,
"Content-Type": "application/json",
},
data: {
contact_email: null,
telegram: null,
signal: null,
nostr_npub: null,
},
});
if (!response.ok()) {
const text = await response.text();
throw new Error(`Failed to clear profile data: ${response.status()} - ${text}`);
}
// Verify the response indicates fields were cleared
const body = await response.json();
if (body.telegram !== null && body.telegram !== undefined && body.telegram !== "") {
throw new Error(
`Profile data not cleared properly. Telegram still has value: ${body.telegram}`
);
}
// Small delay to ensure database commit is visible to subsequent operations
await new Promise((resolve) => setTimeout(resolve, 100));
}
test.describe("Profile - Regular User Access", () => {
@ -122,14 +141,23 @@ test.describe("Profile - Form Behavior", () => {
});
await clearAuth(page);
await loginUser(page, REGULAR_USER.email, REGULAR_USER.password);
// Clear any existing profile data
// Clear any existing profile data and verify it's cleared
await clearProfileData(page);
// Navigate to profile page to verify it's actually cleared
await page.goto("/profile");
// Wait for page to load and verify fields are empty
await expect(page.getByLabel("Contact Email")).toBeVisible({ timeout: 10000 });
await expect(page.getByLabel("Telegram")).toHaveValue("");
await expect(page.getByLabel("Signal")).toHaveValue("");
await expect(page.getByLabel("Nostr (npub)")).toHaveValue("");
});
test("form state management, save, persistence, and clearing fields", async ({ page }) => {
// Page is already loaded in beforeEach, but ensure we're on it
await page.goto("/profile");
await expect(page.getByLabel("Contact Email")).toBeVisible({ timeout: 10000 });
// All editable fields should be empty
// All editable fields should be empty (verified in beforeEach, but double-check)
await expect(page.getByLabel("Contact Email")).toHaveValue("");
await expect(page.getByLabel("Telegram")).toHaveValue("");
await expect(page.getByLabel("Signal")).toHaveValue("");
@ -166,13 +194,17 @@ test.describe("Profile - Form Behavior", () => {
await expect(page.getByText(/saved successfully/i)).toBeVisible();
await expect(page.getByText(/saved successfully/i)).not.toBeVisible({ timeout: 5000 });
// Clear the field
await page.fill("#telegram", "");
// Clear the field - use clear() instead of fill("") for reliable clearing
await page.locator("#telegram").clear();
await page.click('button:has-text("Save Changes")');
await expect(page.getByText(/saved successfully/i)).toBeVisible();
await expect(page.getByText(/saved successfully/i)).not.toBeVisible({ timeout: 5000 });
// Reload and verify it's cleared
// Reload and wait for page to fully load before checking
await page.reload();
// Wait for the form to be loaded (check for a form field to ensure page is ready)
await expect(page.getByLabel("Contact Email")).toBeVisible({ timeout: 10000 });
// Verify telegram field is cleared
await expect(page.getByLabel("Telegram")).toHaveValue("");
});
});