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

main
ts 75 lines 3.05 KB
Raw
1 import { describe, expect } from "@alchemy.run/test";
2 import * as Test from "@alchemy.run/test/Alchemy";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Alchemy from "@/index.ts";
6 import { Stack } from "@/Stack";
7 import { Stage } from "@/Stage";
8 import { InMemoryService, State, type ResourceState } from "@/State";
9 import { TestLayers, TestResource } from "./test.resources.ts";
10
11 // Regression coverage for https://github.com/alchemy-run/alchemy/issues/961:
12 // destroying a stack used to re-persist an empty `{}` output record for the
13 // stage. `state.getOutput` then returned a non-null, field-less husk while
14 // `state stages` no longer reported the stage, and cross-stack
15 // `Output.stackRef` consumers crashed reading fields off the husk at plan
16 // time. After destroy, the persisted output record must be gone entirely.
17
18 const { test } = Test.make({ providers: TestLayers() });
19
20 describe("destroy clears the persisted stack output", { tags: ["unit", "local"] }, () => {
21 test.provider("scratch stack destroy removes the output record", (stack) =>
22 Effect.gen(function* () {
23 const state = yield* yield* State;
24 const stk = yield* Stack;
25
26 const deployed = yield* Effect.gen(function* () {
27 const A = yield* TestResource("A", { string: "test-string" });
28 return { url: A.string };
29 }).pipe(stack.deploy);
30 expect(deployed).toEqual({ url: "test-string" });
31
32 expect(yield* state.getOutput({ stack: stk.name, stage: stk.stage })).toEqual({
33 url: "test-string",
34 });
35
36 yield* stack.destroy();
37
38 // The output record must be removed, not overwritten with `{}`.
39 expect(yield* state.getOutput({ stack: stk.name, stage: stk.stage })).toBeUndefined();
40 // ... and `listStages` must agree the stage is gone.
41 expect(yield* state.listStages(stk.name)).not.toContain(stk.stage);
42 }),
43 );
44 });
45
46 // The same regression through the real `deploy`/`destroy` entry points
47 // (`Destroy.ts`), which is what `alchemy destroy` and the test harness's
48 // `destroy(Stack)` run.
49 const store: Record<string, Record<string, Record<string, ResourceState>>> = {};
50 const outputs: Record<string, Record<string, unknown>> = {};
51 const stateLayer = Layer.succeed(State, InMemoryService(store, outputs));
52
53 const harness = Test.make({ providers: TestLayers(), state: stateLayer });
54
55 const DestroyOutputStack = Alchemy.Stack(
56 "DestroyOutputStack",
57 { providers: TestLayers(), state: stateLayer },
58 Effect.gen(function* () {
59 const A = yield* TestResource("A", { string: "test-string" });
60 return { url: A.string, stage: yield* Stage };
61 }),
62 );
63
64 harness.test(
65 "stack destroy removes the persisted stack output",
66 Effect.gen(function* () {
67 const { stage } = yield* harness.deploy(DestroyOutputStack);
68 expect(outputs.DestroyOutputStack?.[stage]).toEqual({ url: "test-string", stage });
69
70 yield* harness.destroy(DestroyOutputStack);
71 expect(outputs.DestroyOutputStack?.[stage]).toBeUndefined();
72 expect(store.DestroyOutputStack?.[stage]).toBeUndefined();
73 }),
74 { tags: ["unit", "local"] },
75 );