(options: {
readonly respond?: () => Promise<{ ok: boolean; body: string }>;
readonly storage?: "working" | "blocked";
})
| 54 | } |
| 55 | |
| 56 | const environment = (options: { |
| 57 | readonly respond?: () => Promise<{ ok: boolean; body: string }>; |
| 58 | readonly storage?: "working" | "blocked"; |
| 59 | }) => { |
| 60 | const body = new FakeElement(); |
| 61 | const fetches: FetchCall[] = []; |
| 62 | const reloads = { count: 0 }; |
| 63 | const reported: unknown[] = []; |
| 64 | const store = new Map<string, string>(); |
| 65 | |
| 66 | const sessionStorage = { |
| 67 | getItem: (key: string) => store.get(key) ?? null, |
| 68 | setItem: (key: string, value: string) => store.set(key, value), |
| 69 | }; |
| 70 | |
| 71 | const fakeWindow = { |
| 72 | addEventListener: () => {}, |
| 73 | removeEventListener: () => {}, |
| 74 | location: { |
| 75 | reload: () => { |
| 76 | reloads.count += 1; |
| 77 | }, |
| 78 | }, |
| 79 | }; |
| 80 | if (options.storage === "blocked") { |
| 81 | Object.defineProperty(fakeWindow, "sessionStorage", { |
| 82 | get: () => { |
| 83 | // oxlint-disable-next-line executor/no-error-constructor, executor/no-try-catch-or-throw -- boundary: reproduces the SecurityError a browser raises when storage is blocked |
| 84 | throw new Error("SecurityError: storage is disabled"); |
| 85 | }, |
| 86 | }); |
| 87 | } else { |
| 88 | Object.defineProperty(fakeWindow, "sessionStorage", { get: () => sessionStorage }); |
| 89 | } |
| 90 | |
| 91 | const globals = globalThis as Record<string, unknown>; |
| 92 | globals.window = fakeWindow; |
| 93 | globals.document = { |
| 94 | body, |
| 95 | createElement: () => new FakeElement(), |
| 96 | getElementById: (id: string) => body.byId(id), |
| 97 | }; |
| 98 | globals.fetch = async (input: unknown, init?: RequestInit) => { |
| 99 | fetches.push({ input, init }); |
| 100 | const answer = await (options.respond?.() ?? Promise.resolve({ ok: true, body: "ok" })); |
| 101 | return { ok: answer.ok, text: async () => answer.body }; |
| 102 | }; |
| 103 | globals.reportError = (error: unknown) => reported.push(error); |
| 104 | |
| 105 | return { env: browserEnvironment(), body, fetches, reloads, reported, store }; |
| 106 | }; |
| 107 | |
| 108 | describe("browserEnvironment", () => { |
| 109 | it("probes the unauthenticated health endpoint and believes only a healthy answer", async () => { |
no test coverage detected