Skip to content
File

Blob: tests/client/lib/client-config.dom.test.ts

typescript60 lines
1import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2 
3beforeEach(() => {
4 vi.resetModules();
5});
6 
7afterEach(() => {
8 delete window.__BLAND_PUBLIC_CONFIG__;
9 delete window.__BLAND_CSP_NONCE__;
10 vi.restoreAllMocks();
11});
12 
13describe("client config bootstrap", () => {
14 it("reads and memoizes Worker-injected bootstrap config", async () => {
15 window.__BLAND_PUBLIC_CONFIG__ = {
16 sentry_dsn: "https://public@example.ingest.sentry.io/1",
17 };
18 window.__BLAND_CSP_NONCE__ = "nonce-test";
19 
20 const clientConfig = await import("@/client/lib/client-config");
21 
22 expect(clientConfig.getClientConfigSnapshot()).toEqual({
23 sentry_dsn: "https://public@example.ingest.sentry.io/1",
24 });
25 expect(clientConfig.getBootstrapCspNonceSnapshot()).toBe("nonce-test");
26 expect(clientConfig.getClientConfigErrorSnapshot()).toBeNull();
27 
28 window.__BLAND_PUBLIC_CONFIG__ = {
29 sentry_dsn: null,
30 };
31 
32 expect(clientConfig.getClientConfigSnapshot()).toEqual({
33 sentry_dsn: "https://public@example.ingest.sentry.io/1",
34 });
35 });
36 
37 it("returns a stable error when the Worker bootstrap config is missing", async () => {
38 const clientConfig = await import("@/client/lib/client-config");
39 
40 expect(clientConfig.getClientConfigSnapshot()).toBeNull();
41 expect(clientConfig.getBootstrapCspNonceSnapshot()).toBeNull();
42 expect(clientConfig.getClientConfigErrorSnapshot()).toEqual(
43 expect.objectContaining({ message: "Missing Worker bootstrap config" }),
44 );
45 });
46 
47 it("returns a stable error when the Worker bootstrap config is invalid", async () => {
48 window.__BLAND_PUBLIC_CONFIG__ = {
49 sentry_dsn: 123,
50 };
51 
52 const clientConfig = await import("@/client/lib/client-config");
53 
54 expect(clientConfig.getClientConfigSnapshot()).toBeNull();
55 expect(clientConfig.getClientConfigErrorSnapshot()).toEqual(
56 expect.objectContaining({ message: "Invalid Worker bootstrap config" }),
57 );
58 });
59});