@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 122 lines 4.85 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Profile, ProfileResourceAssociation } from "@alchemy.run/aws/Route53Profiles";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as profiles from "@distilled.cloud/aws/route53profiles";
6 import * as resolver from "@distilled.cloud/aws/route53resolver";
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 const FRG_NAME = "alchemy-test-r53p-frg";
13
14 // The DNS Firewall rule group under test is created out-of-band via
15 // distilled (free, instant, and not the resource under test). Creation is
16 // idempotent via a deterministic CreatorRequestId; a leaked group from a
17 // prior crashed run is reused.
18 const ensureFirewallRuleGroup = Effect.gen(function* () {
19 const existing = yield* resolver.listFirewallRuleGroups({});
20 const found = existing.FirewallRuleGroups?.find((g) => g.Name === FRG_NAME);
21 if (found?.Arn !== undefined && found.Id !== undefined) {
22 return { arn: found.Arn, id: found.Id };
23 }
24 const created = yield* resolver.createFirewallRuleGroup({
25 CreatorRequestId: FRG_NAME,
26 Name: FRG_NAME,
27 });
28 return {
29 arn: created.FirewallRuleGroup!.Arn!,
30 id: created.FirewallRuleGroup!.Id!,
31 };
32 });
33
34 // Deleting the rule group races the association's asynchronous drain.
35 const deleteFirewallRuleGroup = (id: string) =>
36 resolver.deleteFirewallRuleGroup({ FirewallRuleGroupId: id }).pipe(
37 Effect.retry({
38 while: (e) => e._tag === "ConflictException",
39 schedule: Schedule.max([Schedule.fixed("4 seconds"), Schedule.recurs(15)]),
40 }),
41 Effect.asVoid,
42 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
43 );
44
45 const assertAssociationGone = (profileResourceAssociationId: string) =>
46 profiles
47 .getProfileResourceAssociation({
48 ProfileResourceAssociationId: profileResourceAssociationId,
49 })
50 .pipe(
51 Effect.flatMap((r) =>
52 r.ProfileResourceAssociation?.Status === "DELETING" ||
53 r.ProfileResourceAssociation?.Status === "DELETED"
54 ? Effect.void
55 : Effect.fail(new Error(`association still ${r.ProfileResourceAssociation?.Status}`)),
56 ),
57 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
58 Effect.retry({
59 while: (e) => e instanceof Error,
60 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]),
61 }),
62 );
63
64 test.provider(
65 "attach a DNS Firewall rule group, update its priority, destroy",
66 (stack) =>
67 Effect.gen(function* () {
68 yield* stack.destroy();
69 const ruleGroup = yield* ensureFirewallRuleGroup;
70
71 const deploy = (priority: number) =>
72 stack.deploy(
73 Effect.gen(function* () {
74 const profile = yield* Profile("FirewallProfile", {});
75 const attachment = yield* ProfileResourceAssociation("FirewallRules", {
76 profileId: profile.profileId,
77 resourceArn: ruleGroup.arn,
78 resourceProperties: JSON.stringify({ priority }),
79 });
80 return { profile, attachment };
81 }),
82 );
83
84 yield* Effect.gen(function* () {
85 const first = yield* deploy(102);
86 expect(first.attachment.profileResourceAssociationId).toMatch(/^rpr-/);
87 expect(first.attachment.resourceArn).toBe(ruleGroup.arn);
88 expect(first.attachment.resourceType).toBe("FIREWALL_RULE_GROUP");
89 expect(JSON.parse(first.attachment.resourceProperties ?? "{}")).toEqual({ priority: 102 });
90
91 // Out-of-band: association exists with the requested priority.
92 const live = yield* profiles.getProfileResourceAssociation({
93 ProfileResourceAssociationId: first.attachment.profileResourceAssociationId,
94 });
95 expect(JSON.parse(live.ProfileResourceAssociation?.ResourceProperties ?? "{}")).toEqual({
96 priority: 102,
97 });
98
99 // Priority updates in place — same association id.
100 const second = yield* deploy(103);
101 expect(second.attachment.profileResourceAssociationId).toBe(
102 first.attachment.profileResourceAssociationId,
103 );
104 const updated = yield* profiles.getProfileResourceAssociation({
105 ProfileResourceAssociationId: second.attachment.profileResourceAssociationId,
106 });
107 expect(JSON.parse(updated.ProfileResourceAssociation?.ResourceProperties ?? "{}")).toEqual({
108 priority: 103,
109 });
110
111 yield* stack.destroy();
112 yield* assertAssociationGone(first.attachment.profileResourceAssociationId);
113 }).pipe(
114 // Always clean up the out-of-band rule group, even on failure.
115 Effect.ensuring(deleteFirewallRuleGroup(ruleGroup.id).pipe(Effect.ignore)),
116 );
117 }),
118 {
119 tags: ["provider:aws", "provider:aws:route53profiles", "provider:aws:route53resolver", "live"],
120 timeout: 360_000,
121 },
122 );