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

main
ts 123 lines 4.25 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Stack as CfnStack } from "@alchemy.run/aws/CloudFormation";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as CloudFormation from "@distilled.cloud/aws/cloudformation";
6 import * as Provider from "alchemy/Provider";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 class StackStillExists extends Data.TaggedError("StackStillExists") {}
17
18 /** Assert the stack is gone (reads as StackNotFound by name). */
19 const assertDeleted = Effect.fn(function* (stackName: string) {
20 yield* CloudFormation.describeStacks({ StackName: stackName }).pipe(
21 Effect.flatMap(() => Effect.fail(new StackStillExists())),
22 Effect.retry({
23 while: (e) => e._tag === "StackStillExists",
24 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(20)]),
25 }),
26 Effect.catchTag("StackNotFound", () => Effect.void),
27 );
28 });
29
30 // A tiny, free template: a single SSM String parameter whose value is a
31 // template parameter, plus an output echoing the generated parameter name.
32 const template = JSON.stringify({
33 Parameters: { Value: { Type: "String" } },
34 Resources: {
35 Param: {
36 Type: "AWS::SSM::Parameter",
37 Properties: { Type: "String", Value: { Ref: "Value" } },
38 },
39 },
40 Outputs: { ParamName: { Value: { Ref: "Param" } } },
41 });
42
43 const stackDef = (value: string, tags?: Record<string, string>) =>
44 Effect.gen(function* () {
45 const stack = yield* CfnStack("CfnTestStack", {
46 templateBody: template,
47 parameters: { Value: value },
48 tags,
49 });
50 return { stack };
51 });
52
53 test.provider(
54 "create, no-op, update, delete a CloudFormation stack",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 // Create.
60 const { stack: created } = yield* stack.deploy(stackDef("v1"));
61 expect(created.stackStatus).toBe("CREATE_COMPLETE");
62 expect(created.stackId).toMatch(/^arn:aws:cloudformation:/);
63 expect(created.outputs.ParamName).toBeDefined();
64
65 // Verify out-of-band.
66 const described = yield* CloudFormation.describeStacks({
67 StackName: created.stackId,
68 });
69 const live = described.Stacks?.[0];
70 expect(live?.StackStatus).toBe("CREATE_COMPLETE");
71 expect(live?.Tags?.some((t) => t.Key === "alchemy::id" && t.Value === "CfnTestStack")).toBe(
72 true,
73 );
74
75 // No-op update (same template + params) — must not error.
76 const { stack: noop } = yield* stack.deploy(stackDef("v1"));
77 expect(noop.stackStatus).toBe("CREATE_COMPLETE");
78 expect(noop.stackId).toBe(created.stackId);
79
80 // Update the template parameter — stack id is stable, status becomes
81 // UPDATE_COMPLETE.
82 const { stack: updated } = yield* stack.deploy(stackDef("v2", { env: "prod" }));
83 expect(updated.stackId).toBe(created.stackId);
84 expect(updated.stackStatus).toBe("UPDATE_COMPLETE");
85
86 const reDescribed = yield* CloudFormation.describeStacks({
87 StackName: created.stackId,
88 });
89 expect(reDescribed.Stacks?.[0]?.StackStatus).toBe("UPDATE_COMPLETE");
90 expect(
91 reDescribed.Stacks?.[0]?.Tags?.some((t) => t.Key === "env" && t.Value === "prod"),
92 ).toBe(true);
93
94 // Delete + wait gone.
95 yield* stack.destroy();
96 yield* assertDeleted(created.stackName);
97 }).pipe(logLevel),
98 {
99 tags: ["provider:aws", "provider:aws:cloudformation", "live"],
100 timeout: 300_000,
101 },
102 );
103
104 test.provider(
105 "list enumerates the deployed stack",
106 (stack) =>
107 Effect.gen(function* () {
108 yield* stack.destroy();
109
110 const { stack: created } = yield* stack.deploy(stackDef("v1"));
111
112 const provider = yield* Provider.findProvider(CfnStack);
113 const all = yield* provider.list();
114 expect(all.some((x) => x.stackId === created.stackId)).toBe(true);
115
116 yield* stack.destroy();
117 yield* assertDeleted(created.stackName);
118 }).pipe(logLevel),
119 {
120 tags: ["provider:aws", "provider:aws:cloudformation", "live"],
121 timeout: 300_000,
122 },
123 );