@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, 06:00 UTC

main
ts 252 lines 9.1 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ExperimentTemplate } from "@alchemy.run/aws/FIS";
3 import { Role } from "@alchemy.run/aws/IAM/Role";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as fis from "@distilled.cloud/aws/fis";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 const findTemplate = (id: string) =>
14 fis.getExperimentTemplate({ id }).pipe(
15 Effect.map((r) => r.experimentTemplate),
16 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
17 );
18
19 class TemplateStillExists extends Data.TaggedError("TemplateStillExists")<{
20 readonly id: string;
21 }> {}
22
23 const assertTemplateDeleted = (id: string) =>
24 findTemplate(id).pipe(
25 Effect.flatMap((template) =>
26 template === undefined ? Effect.void : Effect.fail(new TemplateStillExists({ id })),
27 ),
28 Effect.retry({
29 while: (e) => e._tag === "TemplateStillExists",
30 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
31 }),
32 );
33
34 const fisRole = Effect.gen(function* () {
35 return yield* Role("FisExperimentRole", {
36 assumeRolePolicyDocument: {
37 Version: "2012-10-17",
38 Statement: [
39 {
40 Effect: "Allow",
41 Principal: { Service: "fis.amazonaws.com" },
42 Action: ["sts:AssumeRole"],
43 },
44 ],
45 },
46 inlinePolicies: {
47 "fis-ec2": {
48 Version: "2012-10-17",
49 Statement: [
50 {
51 Effect: "Allow",
52 Action: ["ec2:StopInstances", "ec2:StartInstances", "ec2:DescribeInstances"],
53 Resource: "*",
54 },
55 ],
56 },
57 },
58 });
59 });
60
61 test.provider(
62 "create, update definition + tags, delete experiment template",
63 (stack) =>
64 Effect.gen(function* () {
65 yield* stack.destroy();
66
67 // Definition only — no experiment is ever started, so nothing is
68 // disrupted. The stop-instances action targets instances by a tag no
69 // resource in this account carries.
70 const template = yield* stack.deploy(
71 Effect.gen(function* () {
72 const role = yield* fisRole;
73 return yield* ExperimentTemplate("StopInstancesTemplate", {
74 description: "alchemy fis test",
75 roleArn: role.roleArn,
76 targets: {
77 Instances: {
78 resourceType: "aws:ec2:instance",
79 resourceTags: { "alchemy:fis-test": "true" },
80 selectionMode: "COUNT(1)",
81 },
82 },
83 actions: {
84 StopInstances: {
85 actionId: "aws:ec2:stop-instances",
86 parameters: { startInstancesAfterDuration: "PT2M" },
87 targets: { Instances: "Instances" },
88 },
89 },
90 stopConditions: [{ source: "none" }],
91 tags: { Environment: "test" },
92 });
93 }),
94 );
95
96 expect(template.id).toMatch(/^EXT/);
97 expect(template.arn).toContain(":experiment-template/");
98 expect(template.roleArn).toContain(":role/");
99
100 // out-of-band verification via distilled — the template exists with
101 // the exact definition we declared.
102 const created = yield* findTemplate(template.id);
103 expect(created).toBeDefined();
104 expect(created?.description).toBe("alchemy fis test");
105 expect(created?.roleArn).toBe(template.roleArn);
106 expect(created?.stopConditions).toEqual([{ source: "none" }]);
107 expect(created?.targets?.Instances?.resourceType).toBe("aws:ec2:instance");
108 expect(created?.targets?.Instances?.selectionMode).toBe("COUNT(1)");
109 expect(created?.targets?.Instances?.resourceTags).toEqual({
110 "alchemy:fis-test": "true",
111 });
112 expect(created?.actions?.StopInstances?.actionId).toBe("aws:ec2:stop-instances");
113 expect(created?.actions?.StopInstances?.parameters).toEqual({
114 startInstancesAfterDuration: "PT2M",
115 });
116 expect(created?.tags?.Environment).toBe("test");
117 expect(created?.tags?.["alchemy::id"]).toBe("StopInstancesTemplate");
118
119 // update — new description, wider selection, an extra sequenced wait
120 // action, and changed tags. Same physical template (stable id/arn).
121 const updated = yield* stack.deploy(
122 Effect.gen(function* () {
123 const role = yield* fisRole;
124 return yield* ExperimentTemplate("StopInstancesTemplate", {
125 description: "alchemy fis test updated",
126 roleArn: role.roleArn,
127 targets: {
128 Instances: {
129 resourceType: "aws:ec2:instance",
130 resourceTags: { "alchemy:fis-test": "true" },
131 selectionMode: "ALL",
132 },
133 },
134 actions: {
135 StopInstances: {
136 actionId: "aws:ec2:stop-instances",
137 parameters: { startInstancesAfterDuration: "PT2M" },
138 targets: { Instances: "Instances" },
139 },
140 Wait: {
141 actionId: "aws:fis:wait",
142 parameters: { duration: "PT1M" },
143 startAfter: ["StopInstances"],
144 },
145 },
146 stopConditions: [{ source: "none" }],
147 tags: { Environment: "test", Team: "chaos" },
148 });
149 }),
150 );
151 expect(updated.id).toBe(template.id);
152 expect(updated.arn).toBe(template.arn);
153
154 const afterUpdate = yield* findTemplate(template.id);
155 expect(afterUpdate?.description).toBe("alchemy fis test updated");
156 expect(afterUpdate?.targets?.Instances?.selectionMode).toBe("ALL");
157 expect(afterUpdate?.actions?.Wait?.actionId).toBe("aws:fis:wait");
158 expect(afterUpdate?.actions?.Wait?.startAfter).toEqual(["StopInstances"]);
159 expect(afterUpdate?.tags?.Team).toBe("chaos");
160
161 // removing the user tag converges (internal tags survive)
162 yield* stack.deploy(
163 Effect.gen(function* () {
164 const role = yield* fisRole;
165 return yield* ExperimentTemplate("StopInstancesTemplate", {
166 description: "alchemy fis test updated",
167 roleArn: role.roleArn,
168 targets: {
169 Instances: {
170 resourceType: "aws:ec2:instance",
171 resourceTags: { "alchemy:fis-test": "true" },
172 selectionMode: "ALL",
173 },
174 },
175 actions: {
176 StopInstances: {
177 actionId: "aws:ec2:stop-instances",
178 parameters: { startInstancesAfterDuration: "PT2M" },
179 targets: { Instances: "Instances" },
180 },
181 Wait: {
182 actionId: "aws:fis:wait",
183 parameters: { duration: "PT1M" },
184 startAfter: ["StopInstances"],
185 },
186 },
187 stopConditions: [{ source: "none" }],
188 tags: { Environment: "test" },
189 });
190 }),
191 );
192 const afterTagRemoval = yield* findTemplate(template.id);
193 expect(afterTagRemoval?.tags?.Team).toBeUndefined();
194 expect(afterTagRemoval?.tags?.Environment).toBe("test");
195 expect(afterTagRemoval?.tags?.["alchemy::id"]).toBe("StopInstancesTemplate");
196
197 yield* stack.destroy();
198 yield* assertTemplateDeleted(template.id);
199 }),
200 {
201 tags: ["provider:aws", "provider:aws:fis", "provider:aws:iam", "live"],
202 timeout: 120_000,
203 },
204 );
205
206 test.provider(
207 "changing accountTargeting replaces the template",
208 (stack) =>
209 Effect.gen(function* () {
210 yield* stack.destroy();
211
212 // A target-less wait action keeps the template valid under both
213 // single-account and multi-account targeting.
214 const waitTemplate = (accountTargeting?: "single-account" | "multi-account") =>
215 Effect.gen(function* () {
216 const role = yield* fisRole;
217 return yield* ExperimentTemplate("WaitTemplate", {
218 roleArn: role.roleArn,
219 actions: {
220 Wait: {
221 actionId: "aws:fis:wait",
222 parameters: { duration: "PT1M" },
223 },
224 },
225 experimentOptions: accountTargeting ? { accountTargeting } : undefined,
226 });
227 });
228
229 const first = yield* stack.deploy(waitTemplate());
230 expect(first.id).toMatch(/^EXT/);
231 // the description defaults to the logical id
232 const observedFirst = yield* findTemplate(first.id);
233 expect(observedFirst?.description).toBe("WaitTemplate");
234 expect(observedFirst?.experimentOptions?.accountTargeting).toBe("single-account");
235
236 // accountTargeting is create-only → replacement: new physical
237 // template, old one deleted.
238 const second = yield* stack.deploy(waitTemplate("multi-account"));
239 expect(second.id).not.toBe(first.id);
240
241 const observedSecond = yield* findTemplate(second.id);
242 expect(observedSecond?.experimentOptions?.accountTargeting).toBe("multi-account");
243 yield* assertTemplateDeleted(first.id);
244
245 yield* stack.destroy();
246 yield* assertTemplateDeleted(second.id);
247 }),
248 {
249 tags: ["provider:aws", "provider:aws:fis", "provider:aws:iam", "live"],
250 timeout: 120_000,
251 },
252 );