The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | ClerkPublishableKeyFrontendApiError, |
| 5 | clerkFrontendApiHostnameFromPublishableKey, |
| 6 | clerkFrontendApiUrlFromPublishableKey, |
| 7 | } from "./relayAuth.ts"; |
| 8 | |
| 9 | const clerkPublishableKey = (hostname: string): string => `pk_test_${btoa(`${hostname}$`)}`; |
| 10 | |
| 11 | const captureError = (run: () => unknown): unknown => { |
| 12 | try { |
| 13 | run(); |
| 14 | } catch (cause) { |
| 15 | return cause; |
| 16 | } |
| 17 | throw new Error("Expected operation to throw"); |
| 18 | }; |
| 19 | |
| 20 | describe("Clerk relay auth", () => { |
| 21 | it("derives a custom Frontend API hostname from a Clerk publishable key", () => { |
| 22 | expect(clerkFrontendApiHostnameFromPublishableKey(clerkPublishableKey("clerk.t3.codes"))).toBe( |
| 23 | "clerk.t3.codes", |
| 24 | ); |
| 25 | expect(clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey("clerk.t3.codes"))).toBe( |
| 26 | "https://clerk.t3.codes", |
| 27 | ); |
| 28 | }); |
| 29 | |
| 30 | it("reports semantic frontend API failures without inventing a cause", () => { |
| 31 | const emptyError = captureError(() => clerkFrontendApiUrlFromPublishableKey("pk_test_")); |
| 32 | const pathFrontendApi = "clerk.t3.codes/path"; |
| 33 | const pathError = captureError(() => |
| 34 | clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey(pathFrontendApi)), |
| 35 | ); |
| 36 | |
| 37 | expect(emptyError).toBeInstanceOf(ClerkPublishableKeyFrontendApiError); |
| 38 | expect(emptyError).toMatchObject({ |
| 39 | keyPrefix: "pk_test", |
| 40 | frontendApi: "", |
| 41 | reason: "empty", |
| 42 | }); |
| 43 | expect((emptyError as Error & { cause?: unknown }).cause).toBeUndefined(); |
| 44 | expect(pathError).toBeInstanceOf(ClerkPublishableKeyFrontendApiError); |
| 45 | expect(pathError).toMatchObject({ |
| 46 | keyPrefix: "pk_test", |
| 47 | frontendApi: pathFrontendApi, |
| 48 | reason: "contains-path", |
| 49 | }); |
| 50 | expect((pathError as Error & { cause?: unknown }).cause).toBeUndefined(); |
| 51 | }); |
| 52 | }); |