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

main
ts 122 lines 5.22 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Rule } from "@alchemy.run/aws/Rbin";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as rbin from "@distilled.cloud/aws/rbin";
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 /** Typed wait-until-gone: getRule must settle on RULE_NOT_FOUND. */
12 const assertRuleGone = (identifier: string) =>
13 rbin.getRule({ Identifier: identifier }).pipe(
14 Effect.flatMap(() => Effect.fail(new Error(`retention rule ${identifier} still exists`))),
15 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
16 Effect.retry({
17 while: (e) => e instanceof Error,
18 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
19 }),
20 );
21
22 describe("AWS.Rbin.Rule", { tags: ["provider:aws", "provider:aws:rbin", "live"] }, () => {
23 test.provider(
24 "lifecycle: create, update in place, replace on resourceType, destroy",
25 (stack) =>
26 Effect.gen(function* () {
27 yield* stack.destroy();
28
29 // Create a tag-level rule (scoped by resourceTags so it never
30 // retains other test suites' deleted snapshots).
31 const created = yield* stack.deploy(
32 Effect.gen(function* () {
33 const rule = yield* Rule("SnapshotRetention", {
34 resourceType: "EBS_SNAPSHOT",
35 retentionPeriod: "7 days",
36 description: "alchemy rbin lifecycle test",
37 resourceTags: [{ key: "alchemy-rbin-test", value: "snapshots" }],
38 tags: { purpose: "alchemy-test" },
39 });
40 return { identifier: rule.identifier, ruleArn: rule.ruleArn };
41 }),
42 );
43
44 // Out-of-band verification via distilled.
45 const observed = yield* rbin.getRule({ Identifier: created.identifier });
46 expect(observed.RuleArn).toEqual(created.ruleArn);
47 expect(observed.ResourceType).toEqual("EBS_SNAPSHOT");
48 expect(observed.RetentionPeriod).toEqual({
49 RetentionPeriodValue: 7,
50 RetentionPeriodUnit: "DAYS",
51 });
52 expect(observed.Description).toEqual("alchemy rbin lifecycle test");
53 expect(observed.ResourceTags).toEqual([
54 { ResourceTagKey: "alchemy-rbin-test", ResourceTagValue: "snapshots" },
55 ]);
56 expect(observed.Status).toEqual("available");
57
58 const tags = yield* rbin.listTagsForResource({ ResourceArn: created.ruleArn });
59 expect(tags.Tags).toEqual(
60 expect.arrayContaining([
61 { Key: "purpose", Value: "alchemy-test" },
62 { Key: "alchemy::id", Value: "SnapshotRetention" },
63 ]),
64 );
65
66 // Update in place: retention period, description, resource tags,
67 // and rule tags (remove `purpose`, add `owner`).
68 const updated = yield* stack.deploy(
69 Effect.gen(function* () {
70 const rule = yield* Rule("SnapshotRetention", {
71 resourceType: "EBS_SNAPSHOT",
72 retentionPeriod: "14 days",
73 description: "alchemy rbin lifecycle test (updated)",
74 resourceTags: [{ key: "alchemy-rbin-test", value: "updated" }],
75 tags: { owner: "alchemy" },
76 });
77 return { identifier: rule.identifier, ruleArn: rule.ruleArn };
78 }),
79 );
80 expect(updated.identifier).toEqual(created.identifier);
81
82 const observedUpdated = yield* rbin.getRule({ Identifier: updated.identifier });
83 expect(observedUpdated.RetentionPeriod?.RetentionPeriodValue).toEqual(14);
84 expect(observedUpdated.Description).toEqual("alchemy rbin lifecycle test (updated)");
85 expect(observedUpdated.ResourceTags).toEqual([
86 { ResourceTagKey: "alchemy-rbin-test", ResourceTagValue: "updated" },
87 ]);
88
89 const updatedTags = yield* rbin.listTagsForResource({ ResourceArn: updated.ruleArn });
90 expect(updatedTags.Tags).toEqual(
91 expect.arrayContaining([{ Key: "owner", Value: "alchemy" }]),
92 );
93 expect(updatedTags.Tags).not.toEqual(
94 expect.arrayContaining([{ Key: "purpose", Value: "alchemy-test" }]),
95 );
96
97 // Replacement: changing the resource type replaces the rule.
98 const replaced = yield* stack.deploy(
99 Effect.gen(function* () {
100 const rule = yield* Rule("SnapshotRetention", {
101 resourceType: "EC2_IMAGE",
102 retentionPeriod: "14 days",
103 description: "alchemy rbin lifecycle test (updated)",
104 resourceTags: [{ key: "alchemy-rbin-test", value: "updated" }],
105 tags: { owner: "alchemy" },
106 });
107 return { identifier: rule.identifier, ruleArn: rule.ruleArn };
108 }),
109 );
110 expect(replaced.identifier).not.toEqual(created.identifier);
111
112 const observedReplaced = yield* rbin.getRule({ Identifier: replaced.identifier });
113 expect(observedReplaced.ResourceType).toEqual("EC2_IMAGE");
114 // The replaced (old) rule is deleted after the new one lands.
115 yield* assertRuleGone(created.identifier);
116
117 yield* stack.destroy();
118 yield* assertRuleGone(replaced.identifier);
119 }),
120 { timeout: 180_000 },
121 );
122 });