1import { assert, it } from "@effect/vitest";
2import * as ConfigProvider from "effect/ConfigProvider";
3import * as Effect from "effect/Effect";
4import * as Result from "effect/Result";
5
6import { hostedAppUrlConfig, makeCloudCliOAuthConfig, makeRelayUrlConfig } from "./publicConfig.ts";
7
8const provideEnv = (env: Readonly<Record<string, string>>) =>
9 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env })));
10
11it.effect("uses the statically injected relay URL when no runtime override exists", () =>
12 Effect.gen(function* () {
13 const relayUrl = yield* makeRelayUrlConfig("https://embedded.example.test///").pipe(
14 provideEnv({}),
15 );
16
17 assert.equal(relayUrl, "https://embedded.example.test");
18 }),
19);
20
21it.effect("prefers a runtime relay URL override over the statically injected value", () =>
22 Effect.gen(function* () {
23 const relayUrl = yield* makeRelayUrlConfig("https://embedded.example.test").pipe(
24 provideEnv({ T3CODE_RELAY_URL: "https://runtime.example.test///" }),
25 );
26
27 assert.equal(relayUrl, "https://runtime.example.test");
28 }),
29);
30
31it.effect("requires a relay URL when the server bundle has no injected value", () =>
32 makeRelayUrlConfig("").pipe(provideEnv({}), Effect.flip),
33);
34
35it.effect("rejects an insecure runtime relay URL override", () =>
36 makeRelayUrlConfig("https://embedded.example.test").pipe(
37 provideEnv({ T3CODE_RELAY_URL: "http://runtime.example.test" }),
38 Effect.flip,
39 ),
40);
41
42it.effect("rejects an injected relay URL with a non-origin path", () =>
43 makeRelayUrlConfig("https://embedded.example.test/path").pipe(provideEnv({}), Effect.flip),
44);
45
46it.effect("normalizes the hosted app URL to an absolute origin", () =>
47 Effect.gen(function* () {
48 assert.equal(
49 yield* hostedAppUrlConfig.pipe(
50 provideEnv({ T3CODE_HOSTED_APP_URL: "https://nightly.app.t3.codes" }),
51 ),
52 "https://nightly.app.t3.codes",
53 );
54 assert.equal(
55 yield* hostedAppUrlConfig.pipe(
56 provideEnv({ T3CODE_HOSTED_APP_URL: "http://localhost:5733" }),
57 ),
58 "http://localhost:5733",
59 );
60 }),
61);
62
63it.effect("rejects malformed or insecure hosted app URLs", () =>
64 Effect.gen(function* () {
65 for (const value of [
66 "app.t3.codes",
67 "http://app.t3.codes",
68 "https://app.t3.codes/nested",
69 "https://app.t3.codes?alias=true",
70 ]) {
71 const result = yield* hostedAppUrlConfig.pipe(
72 provideEnv({ T3CODE_HOSTED_APP_URL: value }),
73 Effect.result,
74 );
75 assert.isTrue(Result.isFailure(result), value);
76 }
77 }),
78);
79
80it.effect("derives direct Clerk OAuth endpoints from statically injected public config", () =>
81 Effect.gen(function* () {
82 const config = yield* makeCloudCliOAuthConfig({
83 clerkPublishableKeyFallback: "pk_test_Y2xlcmsuZXhhbXBsZS50ZXN0JA==",
84 clerkCliOAuthClientIdFallback: "oauth_client_embedded",
85 }).pipe(provideEnv({}));
86
87 assert.deepEqual(config, {
88 tokenEndpoint: "https://clerk.example.test/oauth/token",
89 deviceAuthorizationEndpoint: "https://clerk.example.test/oauth/device_authorization",
90 clientId: "oauth_client_embedded",
91 loopbackPort: 34338,
92 redirectUri: "http://127.0.0.1:34338/callback",
93 scopes: ["openid", "profile", "email", "offline_access"],
94 });
95 }),
96);
97
98it.effect("prefers runtime Clerk OAuth config overrides over statically injected values", () =>
99 Effect.gen(function* () {
100 const config = yield* makeCloudCliOAuthConfig({
101 clerkPublishableKeyFallback: "pk_test_ZW1iZWRkZWQuZXhhbXBsZS50ZXN0JA==",
102 clerkCliOAuthClientIdFallback: "oauth_client_embedded",
103 }).pipe(
104 provideEnv({
105 T3CODE_CLERK_PUBLISHABLE_KEY: "pk_test_cnVudGltZS5leGFtcGxlLnRlc3Qk",
106 T3CODE_CLERK_CLI_OAUTH_CLIENT_ID: "oauth_client_runtime",
107 }),
108 );
109
110 assert.equal(config.tokenEndpoint, "https://runtime.example.test/oauth/token");
111 assert.equal(config.clientId, "oauth_client_runtime");
112 }),
113);
114
115it.effect("requires Clerk OAuth config when the server bundle has no injected values", () =>
116 makeCloudCliOAuthConfig({
117 clerkPublishableKeyFallback: "",
118 clerkCliOAuthClientIdFallback: "",
119 }).pipe(provideEnv({}), Effect.flip),
120);
121
122it.effect("reports malformed Clerk publishable keys as typed configuration failures", () =>
123 Effect.gen(function* () {
124 const result = yield* makeCloudCliOAuthConfig({
125 clerkPublishableKeyFallback: "pk_test_not-base64!!",
126 clerkCliOAuthClientIdFallback: "oauth_client_embedded",
127 }).pipe(provideEnv({}), Effect.result);
128
129 assert.isTrue(Result.isFailure(result));
130 if (Result.isFailure(result)) {
131 assert.equal(result.failure.cause._tag, "SourceError");
132 if (result.failure.cause._tag === "SourceError") {
133 assert.equal(
134 result.failure.cause.message,
135 "Failed to derive Clerk Frontend API URL from the publishable key.",
136 );
137 assert.instanceOf(result.failure.cause.cause, Error);
138 }
139 }
140 }),
141);