@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 156 lines 6.4 KB
Raw
1 // @effect-diagnostics anyUnknownInErrorContext:off - ScratchStack.deploy is typed with `any` in its error channel by Alchemy.
2 import { inMemoryState } from "alchemy/State";
3 import * as Test from "@alchemy.run/test/Vitest";
4 import * as ConfigProvider from "effect/ConfigProvider";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Layer from "effect/Layer";
8 import * as Path from "effect/Path";
9 import * as Redacted from "effect/Redacted";
10 import { describe, expect, it } from "vite-plus/test";
11 import * as NodeServices from "@effect/platform-node/NodeServices";
12
13 import { PublishClientConfig, reconcileEnvFile, tokenDigest } from "./clientConfig.ts";
14
15 describe("reconcileEnvFile", () => {
16 it("replaces stale values and keeps unrelated lines", () => {
17 const existing = "KEEP=yes\nA=old\n# comment\n";
18 expect(reconcileEnvFile(existing, { A: "new", B: "2" })).toBe(
19 "KEEP=yes\nA=new\n# comment\nB=2\n",
20 );
21 });
22
23 it("appends a newline before adding to a file without one", () => {
24 expect(reconcileEnvFile("A=1", { B: "2" })).toBe("A=1\nB=2\n");
25 });
26
27 it("collapses duplicate assignments to one, since parseEnv lets the last win", () => {
28 expect(reconcileEnvFile("A=old\nKEEP=1\nA=older\n", { A: "new" })).toBe("A=new\nKEEP=1\n");
29 });
30
31 it("recognises every assignment form parseEnv accepts", () => {
32 expect(reconcileEnvFile("A=old\n export A = stale\nexport A=older\n", { A: "new" })).toBe(
33 "A=new\n",
34 );
35 });
36
37 it("replaces a quoted multi-line value whole", () => {
38 expect(reconcileEnvFile('A="one\ntwo"\nKEEP=1\n', { A: "new" })).toBe("A=new\nKEEP=1\n");
39 expect(reconcileEnvFile("A='x'\nKEEP=1\n", { A: "new" })).toBe("A=new\nKEEP=1\n");
40 expect(
41 reconcileEnvFile(
42 '# earlier "quote"\nA="first\nvalue"\nKEEP=1\nA=\'second\nvalue\'\nB=`third\nvalue`',
43 { A: "new", B: "updated" },
44 ),
45 ).toBe('# earlier "quote"\nA=new\nKEEP=1\nB=updated\n');
46 });
47
48 it("keeps later lines when a quoted value never closes", () => {
49 expect(reconcileEnvFile('A="one\nKEEP=1\n', { A: "new" })).toBe("A=new\nKEEP=1\n");
50 expect(
51 reconcileEnvFile("A=\"one\nKEEP='two'\nB=`three\nKEEP=2", { A: "new", B: "updated" }),
52 ).toBe("A=new\nKEEP='two'\nB=updated\nKEEP=2\n");
53 });
54
55 it("leaves a commented-out assignment alone", () => {
56 expect(reconcileEnvFile("#A=old\n", { A: "new" })).toBe("#A=old\nA=new\n");
57 });
58 });
59
60 const { test } = Test.make({
61 providers: Layer.empty,
62 state: inMemoryState(),
63 });
64
65 const clientConfig = (token: string) => ({
66 url: "https://relay.example.com",
67 mobileTracingUrl: "https://axiom.example.com/v1/traces",
68 mobileTracingDataset: "relay-traces",
69 mobileTracingToken: Redacted.make(`mobile-${token}`),
70 clientTracingUrl: "https://axiom.example.com/v1/traces",
71 clientTracingDataset: "relay-traces",
72 clientTracingToken: Redacted.make(`client-${token}`),
73 tokenDigest: tokenDigest([Redacted.make(`mobile-${token}`), Redacted.make(`client-${token}`)]),
74 });
75
76 describe("PublishClientConfig", () => {
77 test.provider("writes the env file on deploy and skips an unchanged redeploy", (stack) =>
78 Effect.gen(function* () {
79 const fs = yield* FileSystem.FileSystem;
80 const path = yield* Path.Path;
81 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-relay-client-config-" });
82 const target = path.join(dir, "client.env");
83 yield* fs.writeFileString(target, "KEEP=yes\n");
84 const configured = Effect.provide(
85 ConfigProvider.layer(
86 ConfigProvider.fromUnknown({ T3CODE_RELAY_CLIENT_CONFIG_ENV: target }),
87 ),
88 );
89
90 yield* stack.deploy(PublishClientConfig(clientConfig("v1"))).pipe(configured);
91 const first = yield* fs.readFileString(target);
92 expect(first).toContain("KEEP=yes\n");
93 expect(first).toContain("T3CODE_RELAY_URL=https://relay.example.com\n");
94 expect(first).toContain("T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN=client-v1\n");
95 expect(first).toContain("T3CODE_MOBILE_OTLP_TRACES_TOKEN=mobile-v1\n");
96
97 yield* fs.writeFileString(target, `${first}MANUAL=1\n`);
98 yield* stack.deploy(PublishClientConfig(clientConfig("v1"))).pipe(configured);
99 expect(yield* fs.readFileString(target)).toContain("MANUAL=1\n");
100
101 yield* stack.deploy(PublishClientConfig(clientConfig("v2"))).pipe(configured);
102 const third = yield* fs.readFileString(target);
103 expect(third).toContain("T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN=client-v2\n");
104 expect(third).not.toContain("client-v1");
105 expect(third).toContain("KEEP=yes\n");
106 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
107 );
108
109 test.provider("fails when the worker has no url rather than publishing an empty relay", (stack) =>
110 Effect.gen(function* () {
111 const fs = yield* FileSystem.FileSystem;
112 const path = yield* Path.Path;
113 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-relay-client-config-" });
114 const target = path.join(dir, "client.env");
115 const exit = yield* stack
116 .deploy(PublishClientConfig({ ...clientConfig("v1"), url: undefined }))
117 .pipe(
118 Effect.provide(
119 ConfigProvider.layer(
120 ConfigProvider.fromUnknown({ T3CODE_RELAY_CLIENT_CONFIG_ENV: target }),
121 ),
122 ),
123 Effect.exit,
124 );
125 expect(exit._tag).toBe("Failure");
126 expect(String(exit)).toContain("RelayUrlUnavailableError");
127 expect(yield* fs.exists(target)).toBe(false);
128 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
129 );
130
131 test.provider("refuses a value with a line break rather than corrupting the file", (stack) =>
132 Effect.gen(function* () {
133 const fs = yield* FileSystem.FileSystem;
134 const path = yield* Path.Path;
135 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-relay-client-config-" });
136 const target = path.join(dir, "client.env");
137 const exit = yield* stack
138 .deploy(
139 PublishClientConfig({
140 ...clientConfig("v1"),
141 clientTracingDataset: "relay-traces\nINJECTED=1",
142 }),
143 )
144 .pipe(
145 Effect.provide(
146 ConfigProvider.layer(
147 ConfigProvider.fromUnknown({ T3CODE_RELAY_CLIENT_CONFIG_ENV: target }),
148 ),
149 ),
150 Effect.exit,
151 );
152 expect(exit._tag).toBe("Failure");
153 expect(yield* fs.exists(target)).toBe(false);
154 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
155 );
156 });