@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, 04:45 UTC

main
ts 190 lines 7.55 KB
Raw
1 import { expect } from "@alchemy.run/test";
2 import * as Test from "@alchemy.run/test/Alchemy";
3 import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as pathe from "pathe";
6 import * as Command from "@/Command";
7 import { Stack } from "@/Stack";
8 import { type ResourceState, State } from "@/State";
9
10 const { test } = Test.make({ providers: Command.providers(), dev: true });
11
12 const FIXTURE_DIR = pathe.resolve(import.meta.dirname, "fixture");
13
14 const makeTemporaryFixture = Effect.fn(function* () {
15 const fs = yield* FileSystem.FileSystem;
16 const tempDir = yield* fs.makeTempDirectoryScoped();
17 yield* fs.copy(FIXTURE_DIR, tempDir);
18 return { cwd: tempDir, outdir: pathe.join(tempDir, "dist") };
19 });
20
21 // Seed a resource row directly into the scratch stack's shared in-memory state,
22 // simulating state written by an older provider version. Both the test body and
23 // the deploys driven through `stack` read/write the same store keyed by
24 // (stack, stage, fqn).
25 const seedLegacyState = Effect.fn(function* (fqn: string, value: ResourceState) {
26 const state = yield* yield* State;
27 const stk = yield* Stack;
28 yield* state.set({ stack: stk.name, stage: stk.stage, fqn, value });
29 });
30
31 const getState = Effect.fn(function* (fqn: string) {
32 const state = yield* yield* State;
33 const stk = yield* Stack;
34 return (yield* state.get({ stack: stk.name, stage: stk.stage, fqn })) as
35 | ResourceState
36 | undefined;
37 });
38
39 // ─────────────────────────────────────────────────────────────────────
40 // Build.Command → Command.Build
41 //
42 // `Build.Command` was renamed to `Command.Build` and its `attr.hash` shape
43 // changed from a bare `string` to `{ input, output }`. Deploying the renamed
44 // resource over the old state must not error — it may re-run the build (the
45 // memoization logic changed), but it must converge.
46 // ─────────────────────────────────────────────────────────────────────
47 test.provider(
48 "Command.Build deploys over legacy Build.Command state without error",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const fixture = yield* makeTemporaryFixture();
54
55 // Mirror `examples/.../Website__Build.json`: old resourceType and the
56 // legacy `attr.hash` string (pre-`{ input, output }`).
57 yield* seedLegacyState("test-build", {
58 status: "created",
59 fqn: "test-build",
60 logicalId: "test-build",
61 instanceId: "606bcaf4c0a931156f5a314ba7b57fc3",
62 resourceType: "Build.Command",
63 props: { command: "bash build.sh", outdir: "dist", env: {} },
64 attr: {
65 outdir: "dist",
66 hash: "a41cbcecfdeb355f6f98130ca4ff8269d0ad5cc3a628f1918d956c71f859648c",
67 },
68 bindings: [],
69 providerVersion: 0,
70 downstream: [],
71 removalPolicy: "destroy",
72 namespace: undefined,
73 });
74
75 const build = yield* stack.deploy(
76 Command.Build("test-build", {
77 command: "bash build.sh",
78 shell: true,
79 cwd: fixture.cwd,
80 outdir: "dist",
81 }),
82 );
83
84 // The build re-ran and produced the new attribute shape.
85 expect(pathe.resolve(build.outdir)).toBe(fixture.outdir);
86 expect(build.hash).toMatchObject({
87 input: expect.any(String),
88 output: expect.any(String),
89 });
90
91 // State is migrated to the new resource type.
92 const migrated = yield* getState("test-build");
93 expect(migrated?.resourceType).toBe("Command.Build");
94
95 yield* stack.destroy();
96 }),
97 { tags: ["local"], timeout: 60000 },
98 );
99
100 // ─────────────────────────────────────────────────────────────────────
101 // Build.DevServer → Command.Dev
102 //
103 // `Build.DevServer` was renamed to `Command.Dev`. Deploying the renamed
104 // resource (in dev mode) over the old state must spawn the new dev process and
105 // converge — re-extracting a fresh URL rather than carrying the stale one.
106 // ─────────────────────────────────────────────────────────────────────
107 test.provider(
108 "Command.Dev deploys over legacy Build.DevServer state without error",
109 (stack) =>
110 Effect.gen(function* () {
111 yield* stack.destroy();
112
113 // Mirror `examples/.../Website__Dev.json`, including the stale URL.
114 yield* seedLegacyState("test-dev", {
115 status: "created",
116 fqn: "test-dev",
117 logicalId: "test-dev",
118 instanceId: "b4ca740550ef6e2e409481cedf9314c7",
119 resourceType: "Build.DevServer",
120 props: { command: "zola serve", env: {} },
121 attr: { url: "http://127.0.0.1:1024" },
122 bindings: [],
123 providerVersion: 0,
124 downstream: [],
125 removalPolicy: "destroy",
126 namespace: undefined,
127 });
128
129 // A dev server must stay alive (the provider treats an early exit as an
130 // error), so print a URL and then block. `stack.destroy()` interrupts it.
131 const dev = yield* stack.deploy(
132 Command.Dev("test-dev", {
133 command: "echo http://127.0.0.1:4321 && sleep 600",
134 shell: true,
135 }),
136 );
137
138 // The new provider ran: it re-extracted the URL the spawned process
139 // printed rather than echoing back the stale seeded attribute.
140 expect(dev.url).toBe("http://127.0.0.1:4321");
141
142 const migrated = yield* getState("test-dev");
143 expect(migrated?.resourceType).toBe("Command.Dev");
144
145 yield* stack.destroy();
146 }),
147 { tags: ["local"], timeout: 60000 },
148 );
149
150 // ─────────────────────────────────────────────────────────────────────
151 // Orphaned legacy resource cleanup
152 //
153 // When a `Build.Command` / `Build.DevServer` row lingers in state but is no
154 // longer declared, the engine resolves the old type to the `Renamed` no-op
155 // provider and deletes the row. This must not error.
156 // ─────────────────────────────────────────────────────────────────────
157 test.provider(
158 "destroy removes an orphaned legacy Build.Command via the renamed provider",
159 (stack) =>
160 Effect.gen(function* () {
161 yield* stack.destroy();
162
163 yield* seedLegacyState("orphan-build", {
164 status: "created",
165 fqn: "orphan-build",
166 logicalId: "orphan-build",
167 instanceId: "606bcaf4c0a931156f5a314ba7b57fc3",
168 resourceType: "Build.Command",
169 props: { command: "zola build", outdir: "public", env: {} },
170 attr: {
171 outdir: "public",
172 hash: "a41cbcecfdeb355f6f98130ca4ff8269d0ad5cc3a628f1918d956c71f859648c",
173 },
174 bindings: [],
175 providerVersion: 0,
176 downstream: [],
177 removalPolicy: "destroy",
178 namespace: undefined,
179 });
180
181 expect(yield* getState("orphan-build")).toBeDefined();
182
183 // An empty-plan apply (destroy) reaps the orphan through the renamed
184 // provider's no-op delete.
185 yield* stack.destroy();
186
187 expect(yield* getState("orphan-build")).toBeUndefined();
188 }),
189 { tags: ["local"], timeout: 60000 },
190 );