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