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

main
ts 215 lines 8.13 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Group } from "@alchemy.run/aws/ResourceGroups";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as resourcegroups from "@distilled.cloud/aws/resource-groups";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import * as Stream from "effect/Stream";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const assertGroupGone = (groupName: string) =>
13 resourcegroups.getGroup({ Group: groupName }).pipe(
14 Effect.flatMap(() => Effect.fail(new Error(`group ${groupName} still exists`))),
15 Effect.catchTag("NotFoundException", () => 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 const tagQuery = (tagKey: string, tagValue: string) =>
23 JSON.stringify({
24 ResourceTypeFilters: ["AWS::AllSupported"],
25 TagFilters: [{ Key: tagKey, Values: [tagValue] }],
26 });
27
28 describe(
29 "AWS.ResourceGroups.Group",
30 { tags: ["provider:aws", "provider:aws:resourcegroups", "live"] },
31 () => {
32 test.provider(
33 "creates, updates, and deletes a tag-based group",
34 (stack) =>
35 Effect.gen(function* () {
36 yield* stack.destroy();
37
38 const created = yield* stack.deploy(
39 Effect.gen(function* () {
40 const group = yield* Group("TagGroup", {
41 description: "Alchemy resource-groups test",
42 resourceQuery: {
43 type: "TAG_FILTERS_1_0",
44 query: tagQuery("alchemy-rg-test", "v1"),
45 },
46 tags: { purpose: "alchemy-test" },
47 });
48 return { groupName: group.groupName, groupArn: group.groupArn };
49 }),
50 );
51
52 // Verify out-of-band via distilled.
53 const observed = yield* resourcegroups.getGroup({ Group: created.groupName });
54 expect(observed.Group.GroupArn).toEqual(created.groupArn);
55 expect(observed.Group.Description).toEqual("Alchemy resource-groups test");
56
57 const query = yield* resourcegroups.getGroupQuery({ Group: created.groupName });
58 expect(query.GroupQuery?.ResourceQuery).toEqual({
59 Type: "TAG_FILTERS_1_0",
60 Query: tagQuery("alchemy-rg-test", "v1"),
61 });
62
63 const tags = yield* resourcegroups.getTags({ Arn: created.groupArn });
64 expect(tags.Tags?.purpose).toEqual("alchemy-test");
65 expect(tags.Tags?.["alchemy::id"]).toEqual("TagGroup");
66
67 // The query group is functional: enumerating members succeeds
68 // (nothing carries the probe tag, so it's simply empty).
69 const members = yield* resourcegroups.listGroupResources
70 .pages({ Group: created.groupName })
71 .pipe(
72 Stream.runCollect,
73 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Resources ?? [])),
74 );
75 expect(members).toEqual([]);
76
77 // Update description, query, and tags in place — the ARN must
78 // survive (no replacement).
79 const updated = yield* stack.deploy(
80 Effect.gen(function* () {
81 const group = yield* Group("TagGroup", {
82 description: "Alchemy resource-groups test updated",
83 resourceQuery: {
84 type: "TAG_FILTERS_1_0",
85 query: tagQuery("alchemy-rg-test", "v2"),
86 },
87 tags: { purpose: "alchemy-test-updated" },
88 });
89 return { groupName: group.groupName, groupArn: group.groupArn };
90 }),
91 );
92 expect(updated.groupArn).toEqual(created.groupArn);
93
94 const afterUpdate = yield* resourcegroups.getGroup({ Group: created.groupName });
95 expect(afterUpdate.Group.Description).toEqual("Alchemy resource-groups test updated");
96 const queryAfterUpdate = yield* resourcegroups.getGroupQuery({
97 Group: created.groupName,
98 });
99 expect(queryAfterUpdate.GroupQuery?.ResourceQuery.Query).toEqual(
100 tagQuery("alchemy-rg-test", "v2"),
101 );
102 const tagsAfterUpdate = yield* resourcegroups.getTags({ Arn: created.groupArn });
103 expect(tagsAfterUpdate.Tags?.purpose).toEqual("alchemy-test-updated");
104
105 yield* stack.destroy();
106 yield* assertGroupGone(created.groupName);
107 }),
108 { timeout: 120_000 },
109 );
110
111 test.provider(
112 "replaces the group when the name changes",
113 (stack) =>
114 Effect.gen(function* () {
115 yield* stack.destroy();
116
117 const groupProps = {
118 resourceQuery: {
119 type: "TAG_FILTERS_1_0",
120 query: tagQuery("alchemy-rg-replace", "true"),
121 },
122 } as const;
123
124 const created = yield* stack.deploy(
125 Effect.gen(function* () {
126 const group = yield* Group("RenamedGroup", {
127 ...groupProps,
128 groupName: "alchemy-test-rg-original",
129 });
130 return { groupName: group.groupName };
131 }),
132 );
133 expect(created.groupName).toEqual("alchemy-test-rg-original");
134
135 const renamed = yield* stack.deploy(
136 Effect.gen(function* () {
137 const group = yield* Group("RenamedGroup", {
138 ...groupProps,
139 groupName: "alchemy-test-rg-renamed",
140 });
141 return { groupName: group.groupName };
142 }),
143 );
144 expect(renamed.groupName).toEqual("alchemy-test-rg-renamed");
145
146 // The old group is deleted by the replacement.
147 yield* assertGroupGone("alchemy-test-rg-original");
148
149 const observed = yield* resourcegroups.getGroup({ Group: "alchemy-test-rg-renamed" });
150 expect(observed.Group.Name).toEqual("alchemy-test-rg-renamed");
151
152 yield* stack.destroy();
153 yield* assertGroupGone("alchemy-test-rg-renamed");
154 }),
155 { timeout: 120_000 },
156 );
157
158 test.provider(
159 "creates a configuration-based group",
160 (stack) =>
161 Effect.gen(function* () {
162 yield* stack.destroy();
163
164 const created = yield* stack.deploy(
165 Effect.gen(function* () {
166 const group = yield* Group("ConfigGroup", {
167 configuration: [
168 {
169 type: "AWS::ResourceGroups::Generic",
170 parameters: [
171 { name: "allowed-resource-types", values: ["AWS::EC2::CapacityReservation"] },
172 ],
173 },
174 { type: "AWS::EC2::CapacityReservationPool" },
175 ],
176 });
177 return { groupName: group.groupName, groupArn: group.groupArn };
178 }),
179 );
180
181 const observed = yield* resourcegroups.getGroupConfiguration({
182 Group: created.groupName,
183 });
184 const types = (observed.GroupConfiguration?.Configuration ?? []).map((item) => item.Type);
185 expect(types).toContain("AWS::EC2::CapacityReservationPool");
186 expect(types).toContain("AWS::ResourceGroups::Generic");
187
188 // Re-deploying the identical configuration is a no-op (the
189 // order-insensitive fingerprint must not trigger a rejected
190 // putGroupConfiguration).
191 const second = yield* stack.deploy(
192 Effect.gen(function* () {
193 const group = yield* Group("ConfigGroup", {
194 configuration: [
195 {
196 type: "AWS::ResourceGroups::Generic",
197 parameters: [
198 { name: "allowed-resource-types", values: ["AWS::EC2::CapacityReservation"] },
199 ],
200 },
201 { type: "AWS::EC2::CapacityReservationPool" },
202 ],
203 });
204 return { groupArn: group.groupArn };
205 }),
206 );
207 expect(second.groupArn).toEqual(created.groupArn);
208
209 yield* stack.destroy();
210 yield* assertGroupGone(created.groupName);
211 }),
212 { timeout: 120_000 },
213 );
214 },
215 );