Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6
7import {
8 hasDeployChanges,
9 missingRelayPublicConfigFields,
10 publicConfigFromOutput,
11 reconcileRootEnvPublicConfig,
12 reconcileRootEnvRelayUrl,
13 RelayDeployError,
14 RelayDeployPublicConfigUnavailableError,
15 serializeGithubOutput,
16 serializeRelayClientTracingEnvironment,
17} from "./deploy.ts";
18
19describe("RelayDeployError", () => {
20 it("reports the incomplete state source, stage, and missing fields", () => {
21 const missingFields = missingRelayPublicConfigFields({
22 url: "https://relay.example.test",
23 mobileTracingUrl: "https://api.axiom.co/v1/traces",
24 });
25 const error = new RelayDeployError({
26 source: "alchemy_state",
27 stage: "production",
28 missingFields,
29 });
30
31 expect(error).toMatchObject({
32 source: "alchemy_state",
33 stage: "production",
34 missingFields: [
35 "mobileTracingDataset",
36 "mobileTracingToken",
37 "clientTracingUrl",
38 "clientTracingDataset",
39 "clientTracingToken",
40 ],
41 });
42 expect(error.message).toBe(
43 "Relay deploy output from 'alchemy_state' for stage 'production' is missing required public config fields: mobileTracingDataset, mobileTracingToken, clientTracingUrl, clientTracingDataset, clientTracingToken",
44 );
45 });
46
47 it("distinguishes deploy results that do not produce public config", () => {
48 const error = new RelayDeployPublicConfigUnavailableError({
49 result: "dry-run",
50 stage: "production",
51 outputPath: "/tmp/relay-client.env",
52 });
53
54 expect(error.message).toBe(
55 "Relay deploy result 'dry-run' for stage 'production' did not produce public config required by GitHub environment output '/tmp/relay-client.env'.",
56 );
57 });
58});
59
60describe("hasDeployChanges", () => {
61 it("detects resource, binding, and deletion changes", () => {
62 expect(hasDeployChanges({ resources: {}, deletions: {} } as never)).toBe(false);
63 expect(
64 hasDeployChanges({
65 resources: {
66 api: { action: "create", bindings: [] },
67 },
68 deletions: {},
69 } as never),
70 ).toBe(true);
71 expect(
72 hasDeployChanges({
73 resources: {
74 api: { action: "noop", bindings: [{ action: "update" }] },
75 },
76 deletions: {},
77 } as never),
78 ).toBe(true);
79 expect(
80 hasDeployChanges({
81 resources: {},
82 deletions: {
83 api: { action: "delete", bindings: [] },
84 },
85 } as never),
86 ).toBe(true);
87 });
88});
89
90describe("reconcileRootEnvRelayUrl", () => {
91 it("adds the relay URL to an empty root env file", () => {
92 expect(reconcileRootEnvRelayUrl("", "https://relay.example.test")).toBe(
93 "T3CODE_RELAY_URL=https://relay.example.test\n",
94 );
95 });
96
97 it("preserves unrelated root env entries while replacing a previous relay URL", () => {
98 expect(
99 reconcileRootEnvRelayUrl(
100 "T3CODE_CLERK_PUBLISHABLE_KEY=pk_test_example\nT3CODE_RELAY_URL=https://old.example.test\n",
101 "https://relay.example.test",
102 ),
103 ).toBe(
104 "T3CODE_CLERK_PUBLISHABLE_KEY=pk_test_example\nT3CODE_RELAY_URL=https://relay.example.test\n",
105 );
106 });
107});
108
109describe("reconcileRootEnvPublicConfig", () => {
110 const config = {
111 relayUrl: "https://relay.example.test",
112 mobileTracingUrl: "https://api.axiom.co/v1/traces",
113 mobileTracingDataset: "t3-code-mobile-traces-dev",
114 mobileTracingToken: "xaat-public-ingest",
115 clientTracingUrl: "https://api.axiom.co/v1/traces",
116 clientTracingDataset: "t3-code-relay-client-traces-dev",
117 clientTracingToken: "xaat-relay-client-ingest",
118 } as const;
119
120 it("adds the complete local client config", () => {
121 expect(reconcileRootEnvPublicConfig("", config)).toBe(
122 [
123 "T3CODE_RELAY_URL=https://relay.example.test",
124 "T3CODE_MOBILE_OTLP_TRACES_URL=https://api.axiom.co/v1/traces",
125 "T3CODE_MOBILE_OTLP_TRACES_DATASET=t3-code-mobile-traces-dev",
126 "T3CODE_MOBILE_OTLP_TRACES_TOKEN=xaat-public-ingest",
127 "T3CODE_RELAY_CLIENT_OTLP_TRACES_URL=https://api.axiom.co/v1/traces",
128 "T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET=t3-code-relay-client-traces-dev",
129 "T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN=xaat-relay-client-ingest",
130 "",
131 ].join("\n"),
132 );
133 });
134
135 it("replaces stale values while preserving unrelated entries", () => {
136 expect(
137 reconcileRootEnvPublicConfig(
138 [
139 "T3CODE_CLERK_PUBLISHABLE_KEY=pk_test_example",
140 "T3CODE_RELAY_URL=https://old.example.test",
141 "T3CODE_MOBILE_OTLP_TRACES_URL=https://old.example.test/v1/traces",
142 "T3CODE_MOBILE_OTLP_TRACES_DATASET=old-dataset",
143 "T3CODE_MOBILE_OTLP_TRACES_TOKEN=old-token",
144 "T3CODE_RELAY_CLIENT_OTLP_TRACES_URL=https://old.example.test/v1/traces",
145 "T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET=old-client-dataset",
146 "T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN=old-client-token",
147 "",
148 ].join("\n"),
149 config,
150 ),
151 ).toBe(
152 [
153 "T3CODE_CLERK_PUBLISHABLE_KEY=pk_test_example",
154 "T3CODE_RELAY_URL=https://relay.example.test",
155 "T3CODE_MOBILE_OTLP_TRACES_URL=https://api.axiom.co/v1/traces",
156 "T3CODE_MOBILE_OTLP_TRACES_DATASET=t3-code-mobile-traces-dev",
157 "T3CODE_MOBILE_OTLP_TRACES_TOKEN=xaat-public-ingest",
158 "T3CODE_RELAY_CLIENT_OTLP_TRACES_URL=https://api.axiom.co/v1/traces",
159 "T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET=t3-code-relay-client-traces-dev",
160 "T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN=xaat-relay-client-ingest",
161 "",
162 ].join("\n"),
163 );
164 });
165});
166
167describe("serializeGithubOutput", () => {
168 it("serializes relay deploy metadata for GitHub Actions outputs", () => {
169 expect(
170 serializeGithubOutput({
171 changed: false,
172 result: "noop",
173 relay_url: "https://relay.example.test",
174 }),
175 ).toBe("changed=false\nresult=noop\nrelay_url=https://relay.example.test\n");
176 });
177});
178
179describe("serializeRelayClientTracingEnvironment", () => {
180 it("serializes tracing config for downstream GITHUB_ENV loading", () => {
181 expect(
182 serializeRelayClientTracingEnvironment({
183 relayUrl: "https://relay.example.test",
184 mobileTracingUrl: "https://api.axiom.co/v1/traces",
185 mobileTracingDataset: "mobile",
186 mobileTracingToken: "mobile-token",
187 clientTracingUrl: "https://api.axiom.co/v1/traces",
188 clientTracingDataset: "relay",
189 clientTracingToken: "client-token",
190 }),
191 ).toBe(
192 [
193 "T3CODE_RELAY_CLIENT_OTLP_TRACES_URL=https://api.axiom.co/v1/traces",
194 "T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET=relay",
195 "T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN=client-token",
196 "",
197 ].join("\n"),
198 );
199 });
200});
201
202describe("release workflow tracing config propagation", () => {
203 it.effect("uses an artifact instead of a masked cross-job token output", () =>
204 Effect.gen(function* () {
205 const fileSystem = yield* FileSystem.FileSystem;
206 const path = yield* Path.Path;
207 const workflowPath = yield* path.fromFileUrl(
208 new URL("../../../.github/workflows/release.yml", import.meta.url),
209 );
210 const workflow = yield* fileSystem.readFileString(workflowPath);
211
212 expect(workflow).not.toContain("client_tracing_token:");
213 expect(workflow).not.toContain("needs.relay_public_config.outputs.client_tracing_token");
214 expect(workflow).toContain('--github-env-file "$RUNNER_TEMP/relay-client-tracing.env"');
215 expect(workflow).toContain("name: relay-client-tracing-config");
216 expect(workflow).toContain('cat "$config_path" >> "$GITHUB_ENV"');
217 }).pipe(Effect.provide(NodeServices.layer)),
218 );
219});
220
221describe("publicConfigFromOutput", () => {
222 it("reads the complete public tracing config from persisted Alchemy output", () => {
223 expect(
224 publicConfigFromOutput({
225 url: "https://relay.example.test",
226 mobileTracingUrl: "https://api.axiom.co/v1/traces",
227 mobileTracingDataset: "mobile",
228 mobileTracingToken: "mobile-token",
229 clientTracingUrl: "https://api.axiom.co/v1/traces",
230 clientTracingDataset: "relay",
231 clientTracingToken: "client-token",
232 }),
233 ).toEqual({
234 relayUrl: "https://relay.example.test",
235 mobileTracingUrl: "https://api.axiom.co/v1/traces",
236 mobileTracingDataset: "mobile",
237 mobileTracingToken: "mobile-token",
238 clientTracingUrl: "https://api.axiom.co/v1/traces",
239 clientTracingDataset: "relay",
240 clientTracingToken: "client-token",
241 });
242 });
243
244 it("rejects incomplete stack output", () => {
245 expect(publicConfigFromOutput({ url: "https://relay.example.test" })).toBeNull();
246 });
247});