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

main
ts 201 lines 7.42 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import Route53ResolverBindingsFunctionLive, { Route53ResolverBindingsFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "Route53ResolverBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy.
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 // The shared Lambda fixture occasionally answers a transient 5xx under load
28 // (cold re-init, IAM propagation on the freshly attached policy). Retry only
29 // 5xx; a genuine 4xx/assertion failure surfaces immediately.
30 const send = (request: HttpClientRequest.HttpClientRequest) =>
31 HttpClient.execute(request).pipe(
32 Effect.flatMap((response) =>
33 response.status >= 500
34 ? response.text.pipe(
35 Effect.flatMap((body) =>
36 Effect.fail(new TransientUpstream({ status: response.status, body })),
37 ),
38 )
39 : Effect.succeed(response),
40 ),
41 Effect.retry({
42 while: (e) => e._tag === "TransientUpstream",
43 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
44 }),
45 );
46
47 const getJson = (path: string) =>
48 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
49
50 const postJson = (path: string) =>
51 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
52
53 describe.sequential(
54 "Route53Resolver Bindings",
55 {
56 tags: [
57 "provider:aws",
58 "provider:aws:ec2",
59 "provider:aws:lambda",
60 "provider:aws:route53resolver",
61 "live",
62 ],
63 },
64 () => {
65 beforeAll(
66 Effect.gen(function* () {
67 yield* Effect.logInfo("Route53Resolver test setup: destroying previous resources");
68 yield* sharedStack.destroy();
69
70 yield* Effect.logInfo("Route53Resolver test setup: deploying fixture");
71 const attrs = yield* sharedStack.deploy(
72 Effect.gen(function* () {
73 return yield* Route53ResolverBindingsFunction;
74 }).pipe(Effect.provide(Route53ResolverBindingsFunctionLive)),
75 );
76
77 expect(attrs.functionUrl).toBeTruthy();
78 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
79
80 const readinessUrl = `${baseUrl}/bindings`;
81 yield* Effect.logInfo(`Route53Resolver test setup: probing readiness at ${readinessUrl}`);
82 yield* HttpClient.get(readinessUrl).pipe(
83 Effect.flatMap((response) =>
84 response.status === 200
85 ? Effect.succeed(response)
86 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
87 ),
88 Effect.tapError((error) =>
89 Effect.logWarning(
90 `Route53Resolver test setup: fixture not ready yet (${String(error)})`,
91 ),
92 ),
93 Effect.retry({ schedule: readinessPolicy }),
94 );
95 }),
96 { timeout: 420_000 },
97 );
98
99 afterAll(process.env.NO_DESTROY ? Effect.void : sharedStack.destroy(), { timeout: 420_000 });
100
101 describe("binding registration", () => {
102 test.provider("all 5 capabilities initialize in the runtime", (_stack) =>
103 Effect.gen(function* () {
104 const response = (yield* getJson("/bindings")) as { bound: string[] };
105 expect(response.bound).toHaveLength(5);
106 expect(response.bound).toContain("getResolverEndpoint");
107 expect(response.bound).toContain("listResolverEndpointIpAddresses");
108 expect(response.bound).toContain("getResolverRule");
109 expect(response.bound).toContain("updateResolverRule");
110 expect(response.bound).toContain("listResolverRuleAssociations");
111 }),
112 );
113 });
114
115 describe("GetResolverEndpoint", () => {
116 test.provider("reads the bound endpoint's live state (ID injected)", (_stack) =>
117 Effect.gen(function* () {
118 const response = (yield* getJson("/endpoint")) as {
119 status: string;
120 direction: string;
121 ipAddressCount: number;
122 };
123 expect(response.direction).toBe("OUTBOUND");
124 expect(response.status).toBe("OPERATIONAL");
125 expect(response.ipAddressCount).toBe(2);
126 }),
127 );
128 });
129
130 describe("ListResolverEndpointIpAddresses", () => {
131 test.provider("discovers the endpoint's attached IPs", (_stack) =>
132 Effect.gen(function* () {
133 const response = (yield* getJson("/endpoint/ips")) as {
134 ips: { ip: string; status: string }[];
135 };
136 expect(response.ips).toHaveLength(2);
137 for (const entry of response.ips) {
138 expect(entry.ip).toMatch(/^\d+\.\d+\.\d+\.\d+$/);
139 expect(entry.status).toBe("ATTACHED");
140 }
141 }),
142 );
143 });
144
145 describe("GetResolverRule", () => {
146 test.provider("reads the bound rule's targets (ID injected)", (_stack) =>
147 Effect.gen(function* () {
148 const response = (yield* getJson("/rule")) as {
149 domainName: string;
150 ruleType: string;
151 targetIps: string[];
152 };
153 expect(response.domainName.replace(/\.$/, "")).toBe(
154 "bindings.alchemy-r53r-test.internal",
155 );
156 expect(response.ruleType).toBe("FORWARD");
157 expect(response.targetIps).toEqual(["10.100.0.10"]);
158 }),
159 );
160 });
161
162 describe("UpdateResolverRule", () => {
163 test.provider(
164 "fails the rule over to a new target IP at runtime",
165 (_stack) =>
166 Effect.gen(function* () {
167 const response = (yield* postJson("/rule/target?ip=10.100.0.11")) as {
168 status: string;
169 targetIps: string[];
170 };
171 expect(response.targetIps).toEqual(["10.100.0.11"]);
172
173 // The update settles asynchronously — poll the rule-scoped read
174 // (bounded) until the new target is observed.
175 const updated = yield* getJson("/rule").pipe(
176 Effect.map((r) => r as { targetIps: string[] }),
177 Effect.repeat({
178 schedule: Schedule.fixed("3 seconds"),
179 until: (r): boolean => r.targetIps.includes("10.100.0.11"),
180 times: 10,
181 }),
182 );
183 expect(updated.targetIps).toEqual(["10.100.0.11"]);
184 }),
185 { timeout: 120_000 },
186 );
187 });
188
189 describe("ListResolverRuleAssociations", () => {
190 test.provider("lists the bound rule's VPC associations (filter injected)", (_stack) =>
191 Effect.gen(function* () {
192 const response = (yield* getJson("/rule/associations")) as { count: number };
193 // The fixture never associates the rule with a VPC — an empty,
194 // successfully-authorized listing proves the wiring and the
195 // rule-scoped IAM grant.
196 expect(response.count).toBe(0);
197 }),
198 );
199 });
200 },
201 );