@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 140 lines 5.66 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ServiceLinkedRole } from "@alchemy.run/aws/IAM";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as IAM from "@distilled.cloud/aws/iam";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 // The deletion task can report SUCCEEDED slightly before `getRole` stops
12 // returning the role — poll (bounded) until IAM catches up.
13 const waitUntilRoleGone = (roleName: string) =>
14 IAM.getRole({ RoleName: roleName }).pipe(
15 Effect.option,
16 Effect.repeat({
17 schedule: Schedule.spaced("2 seconds"),
18 until: (result) => result._tag === "None",
19 times: 15,
20 }),
21 );
22
23 // Auto Scaling supports custom suffixes and deletes cleanly when no Auto
24 // Scaling groups reference the role — the canonical safely-testable SLR.
25 // Suffixes keep these tests away from the account's shared (unsuffixed)
26 // AWSServiceRoleForAutoScaling, which other infrastructure may depend on.
27 const testServiceName = "autoscaling.amazonaws.com";
28 const testSuffix = "AlchemyTest";
29 const testAdoptSuffix = "AlchemyAdopt";
30 // Deterministic physical name of the out-of-band SLR the adoption test
31 // creates: Auto Scaling names suffixed SLRs `AWSServiceRoleForAutoScaling_
32 // {suffix}`.
33 const adoptRoleName = `AWSServiceRoleForAutoScaling_${testAdoptSuffix}`;
34
35 // Idempotent out-of-band cleanup for the adoption SLR — safe when the stack
36 // already deleted it (or it never fully created). Submits the async deletion
37 // task and tolerates not-found.
38 const deleteAdoptRoleIfExists = IAM.deleteServiceLinkedRole({ RoleName: adoptRoleName }).pipe(
39 Effect.catchTag("NoSuchEntityException", () => Effect.succeed({ DeletionTaskId: "" })),
40 Effect.asVoid,
41 // Any non-not-found failure is a real cleanup bug — surface it loudly.
42 Effect.orDie,
43 );
44
45 describe(
46 "AWS.IAM.ServiceLinkedRole",
47 { tags: ["provider:aws", "provider:aws:iam", "live"] },
48 () => {
49 test.provider(
50 "create, update description, and delete a service-linked role",
51 (stack) =>
52 Effect.gen(function* () {
53 yield* stack.destroy();
54
55 const deployRole = (description: string) =>
56 stack.deploy(
57 Effect.gen(function* () {
58 return yield* ServiceLinkedRole("AutoScalingSLR", {
59 awsServiceName: testServiceName,
60 customSuffix: testSuffix,
61 description,
62 });
63 }),
64 );
65
66 const created = yield* deployRole("alchemy service-linked role test");
67
68 expect(created.roleName.endsWith(`_${testSuffix}`)).toBe(true);
69 expect(created.awsServiceName).toBe(testServiceName);
70 expect(created.customSuffix).toBe(testSuffix);
71
72 // Out-of-band: the role exists under the service's deterministic path.
73 const observed = yield* IAM.getRole({ RoleName: created.roleName });
74 expect(observed.Role?.Path).toBe(`/aws-service-role/${testServiceName}/`);
75 expect(observed.Role?.Arn).toBe(created.roleArn);
76 expect(observed.Role?.Description).toBe("alchemy service-linked role test");
77
78 // Update — the description is the only mutable aspect of an SLR.
79 const updated = yield* deployRole("alchemy SLR updated description");
80 expect(updated.roleArn).toBe(created.roleArn);
81
82 const afterUpdate = yield* IAM.getRole({ RoleName: created.roleName });
83 expect(afterUpdate.Role?.Description).toBe("alchemy SLR updated description");
84
85 // Destroy — the provider waits for the async deletion task; give
86 // getRole a bounded window to observe the removal.
87 yield* stack.destroy();
88
89 const deleted = yield* waitUntilRoleGone(created.roleName);
90 expect(deleted._tag).toBe("None");
91 }),
92 { timeout: 120_000 },
93 );
94
95 test.provider(
96 "adopts a service-linked role that already exists",
97 (stack) =>
98 Effect.gen(function* () {
99 yield* stack.destroy();
100
101 // Pre-create the role out-of-band, simulating a service-created (or
102 // operator-created) SLR that predates the stack.
103 const preexisting = yield* IAM.createServiceLinkedRole({
104 AWSServiceName: testServiceName,
105 CustomSuffix: testAdoptSuffix,
106 }).pipe(
107 // Idempotence across re-runs: an earlier interrupted run may have
108 // left the role behind — the create then reports InvalidInput.
109 Effect.catchTag("InvalidInputException", () => Effect.succeed({ Role: undefined })),
110 );
111
112 const adopted = yield* stack.deploy(
113 Effect.gen(function* () {
114 return yield* ServiceLinkedRole("AdoptedSLR", {
115 awsServiceName: testServiceName,
116 customSuffix: testAdoptSuffix,
117 });
118 }),
119 );
120
121 expect(adopted.roleName.endsWith(`_${testAdoptSuffix}`)).toBe(true);
122 if (preexisting.Role) {
123 expect(adopted.roleArn).toBe(preexisting.Role.Arn);
124 }
125
126 // Destroy deletes the adopted role (deletion awaited by the provider).
127 yield* stack.destroy();
128
129 const deleted = yield* waitUntilRoleGone(adopted.roleName);
130 expect(deleted._tag).toBe("None");
131 }).pipe(
132 // The role is created out-of-band, so the scratch stack only cleans
133 // it once the deploy adopts it. Guarantee deletion on failure or
134 // interruption in the window between create and adoption.
135 Effect.ensuring(deleteAdoptRoleIfExists),
136 ),
137 { timeout: 120_000 },
138 );
139 },
140 );