Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

publicConfig.test.ts

120 lines4.1 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 CloudPublicConfigMissingError,
5 hasCloudPublicConfig,
6 resolveRelayClerkTokenOptions,
7} from "./publicConfig.ts";
8​
9afterEach(() => {
10 vi.unstubAllEnvs();
11 vi.unstubAllGlobals();
12});
13​
14describe("hasCloudPublicConfig", () => {
15 it("requires both public cloud values", () => {
16 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", "");
17 vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "");
18 vi.stubEnv("VITE_T3CODE_RELAY_URL", "");
19 expect(hasCloudPublicConfig()).toBe(false);
20​
21 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", "pk_test_example");
22 expect(hasCloudPublicConfig()).toBe(false);
23​
24 vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "t3-relay");
25 expect(hasCloudPublicConfig()).toBe(false);
26​
27 vi.stubEnv("VITE_T3CODE_RELAY_URL", "https://relay.example.test");
28 expect(hasCloudPublicConfig()).toBe(true);
29 });
30​
31 it("rejects an insecure relay URL", () => {
32 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", "pk_test_example");
33 vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "t3-relay");
34 vi.stubEnv("VITE_T3CODE_RELAY_URL", "http://relay.example.test");
35​
36 expect(hasCloudPublicConfig()).toBe(false);
37 });
38​
39 describe("browser origins", () => {
40 beforeEach(() => {
41 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", `pk_live_${btoa("clerk.example.com$")}`);
42 vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "t3-relay");
43 vi.stubEnv("VITE_T3CODE_RELAY_URL", "https://relay.example.test");
44 });
45​
46 it.each(["https://example.com", "https://nightly.app.example.com:443"])(
47 "enables production auth on the configured root and subdomains: %s",
48 (origin) => {
49 vi.stubGlobal("window", { location: new URL(origin) });
50​
51 expect(hasCloudPublicConfig()).toBe(true);
52 },
53 );
54​
55 it.each(["http://localhost:3773", "https://machine.example.ts.net"])(
56 "disables production auth on private browser origins: %s",
57 (origin) => {
58 vi.stubGlobal("window", { location: new URL(origin) });
59​
60 expect(hasCloudPublicConfig()).toBe(false);
61 },
62 );
63​
64 it.each(["http://app.example.com:443", "https://app.example.com:3773"])(
65 "rejects unsupported schemes and ports even on the configured domain: %s",
66 (origin) => {
67 vi.stubGlobal("window", { location: new URL(origin) });
68​
69 expect(hasCloudPublicConfig()).toBe(false);
70 },
71 );
72​
73 it.each(["https://notexample.com", "https://example.com.evil.test"])(
74 "rejects domain lookalikes: %s",
75 (origin) => {
76 vi.stubGlobal("window", { location: new URL(origin) });
77​
78 expect(hasCloudPublicConfig()).toBe(false);
79 },
80 );
81​
82 it.each([
83 ["https://tenant.clerk.accounts.dev", true],
84 ["https://other.clerk.accounts.dev", false],
85 ])("keeps a provider hostname scoped to its own instance: %s", (origin, expected) => {
86 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", `pk_live_${btoa("tenant.clerk.accounts.dev$")}`);
87 vi.stubGlobal("window", { location: new URL(origin) });
88​
89 expect(hasCloudPublicConfig()).toBe(expected);
90 });
91​
92 it("keeps development keys available on private browser origins", () => {
93 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", `pk_test_${btoa("tenant.clerk.accounts.dev$")}`);
94 vi.stubGlobal("window", { location: new URL("http://localhost:3773") });
95​
96 expect(hasCloudPublicConfig()).toBe(true);
97 });
98​
99 it("keeps production keys available in the Electron auth shell", () => {
100 vi.stubGlobal("window", { location: new URL("file:///app/index.html"), desktopBridge: {} });
101​
102 expect(hasCloudPublicConfig()).toBe(true);
103 });
104​
105 it("disables cloud auth for an invalid production key without throwing", () => {
106 vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", "pk_live_!");
107 vi.stubGlobal("window", { location: new URL("https://app.example.com") });
108​
109 expect(hasCloudPublicConfig()).toBe(false);
110 });
111 });
112​
113 it("reports the missing Clerk JWT template as structured configuration", () => {
114 vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "");
115​
116 expect(() => resolveRelayClerkTokenOptions()).toThrowError(
117 new CloudPublicConfigMissingError({ key: "T3CODE_CLERK_JWT_TEMPLATE" }),
118 );
119 });
120});