2026-03-22 19:28:57 +08:00
|
|
|
import fs from "node:fs";
|
2026-03-25 09:27:12 +08:00
|
|
|
import path from "node:path";
|
|
|
|
|
import { expect, test as setup } from "@playwright/test";
|
2026-03-22 19:28:57 +08:00
|
|
|
|
2026-04-30 16:11:33 +08:00
|
|
|
const authFile = path.join(process.cwd(), ".playwright", ".auth", "user.json");
|
2026-03-22 19:28:57 +08:00
|
|
|
|
|
|
|
|
setup("authenticate", async ({ page }) => {
|
|
|
|
|
// Ensure directory exists
|
|
|
|
|
const dir = path.dirname(authFile);
|
|
|
|
|
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
|
|
|
|
|
|
|
|
await page.goto("/login");
|
|
|
|
|
await page.getByLabel("Username").fill("admin");
|
|
|
|
|
await page.getByLabel("Password").fill("admin");
|
|
|
|
|
await page.getByRole("button", { name: /login/i }).click();
|
|
|
|
|
|
2026-04-24 01:24:15 +08:00
|
|
|
// Wait for login to complete and grab the token in one step
|
2026-04-24 18:02:21 +08:00
|
|
|
const handle = await page.waitForFunction(() => localStorage.getItem("snapotter-token"), null, {
|
2026-04-24 01:24:15 +08:00
|
|
|
timeout: 15_000,
|
|
|
|
|
});
|
|
|
|
|
const token = await handle.jsonValue();
|
|
|
|
|
|
|
|
|
|
// Dismiss analytics consent via API so it won't block any test
|
|
|
|
|
const apiBase = process.env.API_URL || "http://localhost:13490";
|
|
|
|
|
await page.request.put(`${apiBase}/api/v1/user/analytics`, {
|
|
|
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
|
|
|
data: { enabled: false },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Now navigate to "/" - consent guard is satisfied
|
2026-04-30 16:11:33 +08:00
|
|
|
// Use waitUntil: "domcontentloaded" to avoid racing with client-side redirects
|
|
|
|
|
await page.goto("/", { waitUntil: "domcontentloaded" });
|
|
|
|
|
// Wait for the URL to settle (app may redirect through consent/auth guards)
|
|
|
|
|
await page.waitForURL((url) => url.pathname === "/", { timeout: 30_000 }).catch(() => {});
|
|
|
|
|
await page.waitForLoadState("load");
|
2026-03-22 19:28:57 +08:00
|
|
|
|
|
|
|
|
// Save storage state (includes localStorage with the token)
|
|
|
|
|
await page.context().storageState({ path: authFile });
|
|
|
|
|
});
|