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

main
ts 106 lines 3.92 KB
Raw
1 import * as ag from "@distilled.cloud/aws/api-gateway";
2 import * as Data from "effect/Data";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import * as Schedule from "effect/Schedule";
6
7 /**
8 * Post-destroy "assert gone" helpers shared by the ApiGateway suite.
9 *
10 * Each helper reads the resource out-of-band via distilled and succeeds only
11 * once the API answers with the typed `NotFoundException`. A resource that is
12 * still visible after the bounded retry window fails the test with
13 * `ResourceStillExists` — proving the trailing `stack.destroy()` actually
14 * deleted everything (zero orphans).
15 */
16 export class ResourceStillExists extends Data.TaggedError("ResourceStillExists")<{
17 readonly resource: string;
18 }> {}
19
20 // Deletion is effectively read-your-write for API Gateway, but reads are
21 // also subject to the account-wide throttle during a busy suite — retry
22 // both "still visible" and `TooManyRequestsException` briefly.
23 const goneSchedule = Schedule.max([
24 Schedule.exponential(500).pipe(
25 Schedule.modifyDelay(({ duration }) =>
26 Effect.succeed(
27 Duration.isGreaterThan(duration, Duration.seconds(5)) ? Duration.seconds(5) : duration,
28 ),
29 ),
30 ),
31 // 0.5 + 1 + 2 + 4 + five 5s delays = 32.5s, long enough to cross the
32 // account's ~30s read throttle while remaining inside the retry budget.
33 Schedule.recurs(9),
34 ]);
35
36 export const assertRestApiDeleted = Effect.fn(function* (restApiId: string) {
37 yield* ag.getRestApi({ restApiId }).pipe(
38 Effect.flatMap(() =>
39 Effect.fail(new ResourceStillExists({ resource: `RestApi ${restApiId}` })),
40 ),
41 Effect.retry({
42 while: (e): boolean =>
43 e._tag === "ResourceStillExists" || e._tag === "TooManyRequestsException",
44 schedule: goneSchedule,
45 }),
46 Effect.catchTag("NotFoundException", () => Effect.void),
47 );
48 });
49
50 export const assertApiKeyDeleted = Effect.fn(function* (apiKeyId: string) {
51 yield* ag.getApiKey({ apiKey: apiKeyId }).pipe(
52 Effect.flatMap(() => Effect.fail(new ResourceStillExists({ resource: `ApiKey ${apiKeyId}` }))),
53 Effect.retry({
54 while: (e): boolean =>
55 e._tag === "ResourceStillExists" || e._tag === "TooManyRequestsException",
56 schedule: goneSchedule,
57 }),
58 Effect.catchTag("NotFoundException", () => Effect.void),
59 );
60 });
61
62 export const assertUsagePlanDeleted = Effect.fn(function* (usagePlanId: string) {
63 yield* ag.getUsagePlan({ usagePlanId }).pipe(
64 Effect.flatMap(() =>
65 Effect.fail(new ResourceStillExists({ resource: `UsagePlan ${usagePlanId}` })),
66 ),
67 Effect.retry({
68 while: (e): boolean =>
69 e._tag === "ResourceStillExists" || e._tag === "TooManyRequestsException",
70 schedule: goneSchedule,
71 }),
72 Effect.catchTag("NotFoundException", () => Effect.void),
73 );
74 });
75
76 export const assertDomainNameDeleted = Effect.fn(function* (domainName: string) {
77 yield* ag.getDomainName({ domainName }).pipe(
78 Effect.flatMap(() =>
79 Effect.fail(new ResourceStillExists({ resource: `DomainName ${domainName}` })),
80 ),
81 Effect.retry({
82 while: (e): boolean =>
83 e._tag === "ResourceStillExists" || e._tag === "TooManyRequestsException",
84 schedule: goneSchedule,
85 }),
86 Effect.catchTag("NotFoundException", () => Effect.void),
87 );
88 });
89
90 export const assertVpcLinkDeleted = Effect.fn(function* (vpcLinkId: string) {
91 // A VPC link lingers in DELETING for several minutes after deleteVpcLink;
92 // reaching that status is the terminal signal deletion was accepted.
93 yield* ag.getVpcLink({ vpcLinkId }).pipe(
94 Effect.flatMap((link) =>
95 link.status === "DELETING"
96 ? Effect.void
97 : Effect.fail(new ResourceStillExists({ resource: `VpcLink ${vpcLinkId}` })),
98 ),
99 Effect.retry({
100 while: (e): boolean =>
101 e._tag === "ResourceStillExists" || e._tag === "TooManyRequestsException",
102 schedule: goneSchedule,
103 }),
104 Effect.catchTag("NotFoundException", () => Effect.void),
105 );
106 });