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

main
ts 141 lines 5.24 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 ProfilesTestFunctionLive, { ProfilesTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "Route53ProfilesBindings");
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 Lambda fixture occasionally answers a transient 5xx under load (cold
28 // re-init, IAM propagation on the freshly attached policy that the handler's
29 // `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx/assertion
30 // failure surfaces immediately.
31 const send = (request: HttpClientRequest.HttpClientRequest) =>
32 HttpClient.execute(request).pipe(
33 Effect.flatMap((response) =>
34 response.status >= 500
35 ? response.text.pipe(
36 Effect.flatMap((body) =>
37 Effect.fail(new TransientUpstream({ status: response.status, body })),
38 ),
39 )
40 : Effect.succeed(response),
41 ),
42 Effect.retry({
43 while: (e) => e._tag === "TransientUpstream",
44 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
45 }),
46 );
47
48 const getJson = (path: string) =>
49 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
50
51 describe.sequential(
52 "Route53Profiles Bindings",
53 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:route53profiles", "live"] },
54 () => {
55 beforeAll(
56 Effect.gen(function* () {
57 yield* Effect.logInfo("Route53Profiles bindings setup: destroying previous resources");
58 yield* sharedStack.destroy();
59
60 yield* Effect.logInfo("Route53Profiles bindings setup: deploying fixture");
61 const attrs = yield* sharedStack.deploy(
62 Effect.gen(function* () {
63 return yield* ProfilesTestFunction;
64 }).pipe(Effect.provide(ProfilesTestFunctionLive)),
65 );
66
67 expect(attrs.functionUrl).toBeTruthy();
68 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
69
70 const readinessUrl = `${baseUrl}/bindings`;
71 yield* Effect.logInfo(
72 `Route53Profiles bindings setup: probing readiness at ${readinessUrl}`,
73 );
74 yield* HttpClient.get(readinessUrl).pipe(
75 Effect.flatMap((response) =>
76 response.status === 200
77 ? Effect.succeed(response)
78 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
79 ),
80 Effect.tapError((error) =>
81 Effect.logWarning(
82 `Route53Profiles bindings setup: fixture not ready yet (${String(error)})`,
83 ),
84 ),
85 Effect.retry({ schedule: readinessPolicy }),
86 );
87 }),
88 { timeout: 240_000 },
89 );
90
91 afterAll(sharedStack.destroy(), { timeout: 240_000 });
92
93 describe("binding registration", () => {
94 test.provider("the capabilities initialize in the runtime", (_stack) =>
95 Effect.gen(function* () {
96 const response = (yield* getJson("/bindings")) as { bound: string[] };
97 expect(response.bound).toEqual([
98 "listProfileAssociations",
99 "listProfileResourceAssociations",
100 ]);
101 }),
102 );
103 });
104
105 describe("ListProfileAssociations", () => {
106 test.provider("lists the bound profile's VPC associations (injected profile id)", (_stack) =>
107 Effect.gen(function* () {
108 // The fixture's profile is never associated with a VPC, so an
109 // empty list proves both the
110 // route53profiles:ListProfileAssociations grant and the ProfileId
111 // filter injection.
112 const response = (yield* getJson("/associations")) as {
113 count: number;
114 resourceIds: string[];
115 };
116 expect(response.count).toBe(0);
117 expect(response.resourceIds).toEqual([]);
118 }),
119 );
120 });
121
122 describe("ListProfileResourceAssociations", () => {
123 test.provider(
124 "lists the bound profile's attached DNS resources (injected profile id)",
125 (_stack) =>
126 Effect.gen(function* () {
127 // No DNS resources are ever attached to the fixture's profile, so
128 // an empty list proves both the
129 // route53profiles:ListProfileResourceAssociations grant and the
130 // ProfileId injection.
131 const response = (yield* getJson("/resource-associations")) as {
132 count: number;
133 resourceArns: string[];
134 };
135 expect(response.count).toBe(0);
136 expect(response.resourceArns).toEqual([]);
137 }),
138 );
139 });
140 },
141 );