2026-05-04 17:50:06 +05:30
|
|
|
import path from "node:path";
|
|
|
|
|
import { expect, test as setup } from "@playwright/test";
|
2026-06-04 17:03:26 +02:00
|
|
|
import { announcements } from "../lib/announcements/announcements-data";
|
2026-05-11 19:48:18 +05:30
|
|
|
import { acquireTestToken } from "./helpers/api/auth";
|
2026-05-04 17:50:06 +05:30
|
|
|
|
|
|
|
|
/**
|
2026-05-11 19:48:18 +05:30
|
|
|
* One-time authentication setup. Acquires a bearer token for the seeded
|
|
|
|
|
* e2e user (rate-limit-free /__e2e__/auth/token first, /auth/jwt/login
|
|
|
|
|
* fallback) and persists it via localStorage so every test in the
|
|
|
|
|
* chromium project starts already authenticated.
|
2026-06-04 17:03:26 +02:00
|
|
|
*
|
|
|
|
|
* Also pre-seeds the localStorage flags that gate the two new-user UI
|
|
|
|
|
* overlays so they never intercept clicks in journeys:
|
|
|
|
|
* - `surfsense_announcements_state` — the blocking AnnouncementSpotlight
|
|
|
|
|
* dialog (e.g. "Introducing AI Automations") plus its toasts.
|
|
|
|
|
* - `surfsense-tour-<userId>` — the OnboardingTour spotlight for new users.
|
2026-05-04 17:50:06 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const authFile = path.join(__dirname, "..", "playwright", ".auth", "user.json");
|
|
|
|
|
|
|
|
|
|
const STORAGE_KEY = "surfsense_bearer_token";
|
2026-06-04 17:03:26 +02:00
|
|
|
const ANNOUNCEMENTS_KEY = "surfsense_announcements_state";
|
|
|
|
|
|
|
|
|
|
/** Decode the user id (`sub`) from a JWT without verifying the signature. */
|
|
|
|
|
function decodeUserId(token: string): string | null {
|
|
|
|
|
try {
|
|
|
|
|
const payload = token.split(".")[1];
|
|
|
|
|
if (!payload) return null;
|
|
|
|
|
const json = Buffer.from(payload, "base64").toString("utf8");
|
|
|
|
|
const obj = JSON.parse(json) as { sub?: string };
|
|
|
|
|
return obj.sub ?? null;
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-04 17:50:06 +05:30
|
|
|
|
|
|
|
|
setup("authenticate", async ({ page, request }) => {
|
2026-05-11 19:48:18 +05:30
|
|
|
const access_token = await acquireTestToken(request);
|
|
|
|
|
expect(access_token, "Failed to acquire e2e bearer token").toBeTruthy();
|
2026-05-04 17:50:06 +05:30
|
|
|
|
2026-06-04 17:03:26 +02:00
|
|
|
const userId = decodeUserId(access_token);
|
|
|
|
|
// Mark every known announcement read + toasted so spotlight/toast
|
|
|
|
|
// announcements never overlay the dashboard during journeys. Sourced
|
|
|
|
|
// from the real data file so future announcements are covered too.
|
|
|
|
|
const announcementIds = announcements.map((a) => a.id);
|
|
|
|
|
const announcementState = { readIds: announcementIds, toastedIds: announcementIds };
|
|
|
|
|
|
2026-05-04 17:50:06 +05:30
|
|
|
await page.addInitScript(
|
2026-06-04 17:03:26 +02:00
|
|
|
({ key, token, announcementsKey, state, uid }) => {
|
2026-05-04 17:50:06 +05:30
|
|
|
localStorage.setItem(key, token);
|
2026-06-04 17:03:26 +02:00
|
|
|
localStorage.setItem(announcementsKey, JSON.stringify(state));
|
|
|
|
|
if (uid) {
|
|
|
|
|
localStorage.setItem(`surfsense-tour-${uid}`, "true");
|
|
|
|
|
}
|
2026-05-04 17:50:06 +05:30
|
|
|
},
|
2026-06-04 17:03:26 +02:00
|
|
|
{
|
|
|
|
|
key: STORAGE_KEY,
|
|
|
|
|
token: access_token,
|
|
|
|
|
announcementsKey: ANNOUNCEMENTS_KEY,
|
|
|
|
|
state: announcementState,
|
|
|
|
|
uid: userId,
|
|
|
|
|
}
|
2026-05-04 17:50:06 +05:30
|
|
|
);
|
|
|
|
|
|
2026-05-04 19:35:27 +05:30
|
|
|
// Use a public page so the init script can write localStorage without
|
|
|
|
|
// racing the dashboard auth redirect.
|
|
|
|
|
await page.goto("/login", { waitUntil: "domcontentloaded" });
|
2026-05-04 17:50:06 +05:30
|
|
|
|
|
|
|
|
await page.context().storageState({ path: authFile });
|
|
|
|
|
});
|