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

main
ts 106 lines 3.73 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { IPSet } from "@alchemy.run/aws/WAFv2";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as wafv2 from "@distilled.cloud/aws/wafv2";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 class IPSetStillExists extends Data.TaggedError("IPSetStillExists")<{
13 readonly name: string;
14 }> {}
15
16 const assertIpSetDeleted = (name: string, id: string) =>
17 wafv2.getIPSet({ Name: name, Scope: "REGIONAL", Id: id }).pipe(
18 Effect.flatMap(() => Effect.fail(new IPSetStillExists({ name }))),
19 Effect.catchTag("WAFNonexistentItemException", () => Effect.void),
20 Effect.retry({
21 while: (e) => e._tag === "IPSetStillExists",
22 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
23 }),
24 );
25
26 test.provider(
27 "create, update addresses and tags, replace on ip version change, delete",
28 (stack) =>
29 Effect.gen(function* () {
30 // reconcile away any prior partial/crashed deployment
31 yield* stack.destroy();
32
33 const ipSet = yield* stack.deploy(
34 Effect.gen(function* () {
35 return yield* IPSet("LifecycleIpSet", {
36 addresses: ["192.0.2.44/32", "203.0.113.0/24"],
37 description: "test block list",
38 tags: { Environment: "test" },
39 });
40 }),
41 );
42
43 expect(ipSet.ipAddressVersion).toBe("IPV4");
44 expect(ipSet.scope).toBe("REGIONAL");
45 expect([...ipSet.addresses].sort()).toEqual(["192.0.2.44/32", "203.0.113.0/24"]);
46
47 // out-of-band verification via distilled (WAF returns addresses in
48 // arbitrary order)
49 const created = yield* wafv2.getIPSet({
50 Name: ipSet.ipSetName,
51 Scope: "REGIONAL",
52 Id: ipSet.ipSetId,
53 });
54 expect([...(created.IPSet?.Addresses ?? [])].sort()).toEqual([
55 "192.0.2.44/32",
56 "203.0.113.0/24",
57 ]);
58 expect(created.IPSet?.Description).toBe("test block list");
59
60 const tags = yield* wafv2.listTagsForResource({
61 ResourceARN: ipSet.ipSetArn,
62 });
63 const tagRecord = Object.fromEntries(
64 (tags.TagInfoForResource?.TagList ?? []).map((t) => [t.Key, t.Value]),
65 );
66 expect(tagRecord.Environment).toBe("test");
67 expect(tagRecord["alchemy::id"]).toBe("LifecycleIpSet");
68
69 // addresses are mutable — updated in place
70 const updated = yield* stack.deploy(
71 Effect.gen(function* () {
72 return yield* IPSet("LifecycleIpSet", {
73 addresses: ["198.51.100.0/24"],
74 description: "test block list",
75 tags: { Environment: "test", Extra: "1" },
76 });
77 }),
78 );
79 expect(updated.ipSetId).toBe(ipSet.ipSetId);
80 expect(updated.addresses).toEqual(["198.51.100.0/24"]);
81
82 const afterUpdate = yield* wafv2.getIPSet({
83 Name: ipSet.ipSetName,
84 Scope: "REGIONAL",
85 Id: ipSet.ipSetId,
86 });
87 expect(afterUpdate.IPSet?.Addresses).toEqual(["198.51.100.0/24"]);
88
89 // changing the IP version is immutable ⇒ replacement
90 const replaced = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* IPSet("LifecycleIpSet", {
93 ipAddressVersion: "IPV6",
94 addresses: ["2620:0:2d0:200::/64"],
95 });
96 }),
97 );
98 expect(replaced.ipSetId).not.toBe(ipSet.ipSetId);
99 expect(replaced.ipAddressVersion).toBe("IPV6");
100 yield* assertIpSetDeleted(ipSet.ipSetName, ipSet.ipSetId);
101
102 yield* stack.destroy();
103 yield* assertIpSetDeleted(replaced.ipSetName, replaced.ipSetId);
104 }),
105 { tags: ["provider:aws", "provider:aws:wafv2", "live"], timeout: 120_000 },
106 );