flotilla/e2e/harness/app/session.ts

43 lines
1.6 KiB
TypeScript
Raw Normal View History

2026-08-06 15:43:57 +00:00
import type {BrowserContext} from "@playwright/test"
import type {TrustedEvent} from "@welshman/util"
import type {TestUser} from "../keys"
// Must match TEST_SESSION_KEY and TEST_EVENTS_KEY in src/lib/test/session.ts.
const TEST_SESSION_KEY = "__TEST_SESSION__"
const TEST_EVENTS_KEY = "__TEST_EVENTS__"
// The {method, data} shape @welshman/app's session handlers deserialize, installed before navigating.
2026-09-30 17:21:48 +00:00
// A nip07 session carries no key and signs through whatever injectNip07 put on window.
export const injectSession = (context: BrowserContext, user: TestUser, nip07 = false) =>
2026-08-06 15:43:57 +00:00
context.addInitScript(
([key, session]) => {
Object.assign(window, {[key]: session})
},
2026-09-30 17:21:48 +00:00
[
TEST_SESSION_KEY,
nip07 ? {method: "nip07", data: {}} : {method: "nip01", data: {secret: user.secret}},
] as const,
2026-08-06 15:43:57 +00:00
)
// The local cache a returning user would boot with, loaded once the injected session is restored.
2026-08-06 15:43:57 +00:00
export const injectEvents = (context: BrowserContext, events: TrustedEvent[]) =>
context.addInitScript(
([key, value]) => {
Object.assign(window, {[key]: value})
},
[TEST_EVENTS_KEY, events] as const,
)
// Read state as an earlier visit left it, so a room can open with unread messages already in it.
export const injectChecked = (context: BrowserContext, checked: Record<string, number>) =>
context.addInitScript(
([key, value]) => {
// An init script also runs on about:blank, where reading localStorage is a SecurityError.
if (location.protocol.startsWith("http")) {
localStorage.setItem(key, value)
}
},
["CapacitorStorage.checked", JSON.stringify(checked)] as const,
)