import type { Page } from "@playwright/test"; const FIXED_TIME_MS = new Date("2024-01-01T00:00:00.000Z").getTime(); export async function applyDeterministicPage(page: Page) { await page.addInitScript( ({ now }: { now: number }) => { Math.random = () => 0.42; const OriginalDate = Date; class MockDate extends OriginalDate { constructor(...args: any[]) { if (args.length === 0) { super(now); return; } switch (args.length) { case 1: super(args[0]); break; case 2: super(args[0], args[1]); break; case 3: super(args[0], args[1], args[2]); break; case 4: super(args[0], args[1], args[2], args[3]); break; case 5: super(args[0], args[1], args[2], args[3], args[4]); break; case 6: super(args[0], args[1], args[2], args[3], args[4], args[5]); break; default: super( args[0], args[1], args[2], args[3], args[4], args[5], args[6], ); } } static override now() { return now; } } // @ts-expect-error overriding Date for deterministic tests. Date = MockDate; const style = document.createElement("style"); style.textContent = ` *, *::before, *::after { transition-duration: 0s !important; animation-duration: 0s !important; animation-delay: 0s !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; } `; const target = document.head || document.documentElement; target.appendChild(style); }, { now: FIXED_TIME_MS }, ); } export async function useE2EUser(page: Page, userKey: string) { const userIdMap: Record = { landlord: "e2e-landlord-1", "landlord-onboarding": "e2e-landlord-setup-1", tenant: "e2e-tenant-1", } as Record; const userId: string = userIdMap[userKey] ?? userKey; await page.addInitScript(({ id }: { id: string }) => { (window as { __E2E_SKIP_ENCRYPTION__?: boolean }).__E2E_SKIP_ENCRYPTION__ = true; const skipKey = `realestate_app_skip_user_${id}`; localStorage.setItem(skipKey, "true"); }, { id: userId }); await page.context().setExtraHTTPHeaders({ "x-e2e-user": userKey, }); } export function e2eHeaders(userKey: string) { return { "x-e2e-user": userKey }; }