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

main
ts 201 lines 7.63 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 EfsBindingsFunctionLive, { EfsBindingsFunction } from "./bindings-handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "EFSBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy under parallel-suite load.
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 that the
29 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
30 // 4xx/assertion 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 describe.sequential(
49 "EFS Bindings",
50 { tags: ["provider:aws", "provider:aws:efs", "provider:aws:lambda", "live"] },
51 () => {
52 beforeAll(
53 Effect.gen(function* () {
54 yield* Effect.logInfo("EFS test setup: destroying previous resources");
55 yield* sharedStack.destroy();
56
57 yield* Effect.logInfo("EFS test setup: deploying fixture");
58 const { functionUrl } = yield* sharedStack.deploy(
59 Effect.gen(function* () {
60 return yield* EfsBindingsFunction;
61 }).pipe(Effect.provide(EfsBindingsFunctionLive)),
62 );
63
64 expect(functionUrl).toBeTruthy();
65 baseUrl = functionUrl!.replace(/\/+$/, "");
66
67 const readinessUrl = `${baseUrl}/bindings`;
68 yield* Effect.logInfo(`EFS test setup: probing readiness at ${readinessUrl}`);
69 yield* HttpClient.get(readinessUrl).pipe(
70 Effect.flatMap((response) =>
71 response.status === 200
72 ? Effect.succeed(response)
73 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
74 ),
75 Effect.tapError((error) =>
76 Effect.logWarning(`EFS test setup: fixture not ready yet (${String(error)})`),
77 ),
78 Effect.retry({ schedule: readinessPolicy }),
79 );
80 }),
81 { timeout: 240_000 },
82 );
83
84 afterAll(sharedStack.destroy(), { timeout: 240_000 });
85
86 describe("binding registration", () => {
87 test.provider("all 10 capabilities initialize in the runtime", (_stack) =>
88 Effect.gen(function* () {
89 const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe(
90 Effect.flatMap((r) => r.json),
91 );
92 expect((response as any).bound).toHaveLength(10);
93 }),
94 );
95 });
96
97 describe("DescribeFileSystem", () => {
98 test.provider("reads the bound file system's description", (_stack) =>
99 Effect.gen(function* () {
100 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/file-system`)).pipe(
101 Effect.flatMap((r) => r.json),
102 )) as any;
103 expect(response.fileSystemId).toMatch(/^fs-/);
104 expect(response.state).toBe("available");
105 expect(response.encrypted).toBe(true);
106 }),
107 );
108 });
109
110 describe("DescribeMountTargets", () => {
111 test.provider("lists the file system's mount targets", (_stack) =>
112 Effect.gen(function* () {
113 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/mount-targets`)).pipe(
114 Effect.flatMap((r) => r.json),
115 )) as any;
116 // The fixture file system has no mount targets — assert the call
117 // round-trips (FileSystemId injection + fs-scoped IAM).
118 expect(response.count).toBe(0);
119 }),
120 );
121 });
122
123 describe("DescribeAccessPoints", () => {
124 test.provider("lists the file system's access points", (_stack) =>
125 Effect.gen(function* () {
126 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/access-points`)).pipe(
127 Effect.flatMap((r) => r.json),
128 )) as any;
129 expect(typeof response.count).toBe("number");
130 }),
131 );
132 });
133
134 describe("PutBackupPolicy + DescribeBackupPolicy", () => {
135 test.provider("enables then disables automatic backups", (_stack) =>
136 Effect.gen(function* () {
137 const initial = (yield* send(HttpClientRequest.get(`${baseUrl}/backup-policy`)).pipe(
138 Effect.flatMap((r) => r.json),
139 )) as any;
140 expect(["NONE", "DISABLED", "DISABLING"]).toContain(initial.status);
141
142 const enabled = (yield* send(
143 HttpClientRequest.post(`${baseUrl}/backup-policy/enable`),
144 ).pipe(Effect.flatMap((r) => r.json))) as any;
145 expect(["ENABLED", "ENABLING"]).toContain(enabled.status);
146
147 const disabled = (yield* send(
148 HttpClientRequest.post(`${baseUrl}/backup-policy/disable`),
149 ).pipe(Effect.flatMap((r) => r.json))) as any;
150 expect(["DISABLED", "DISABLING"]).toContain(disabled.status);
151 }),
152 );
153 });
154
155 describe("PutLifecycleConfiguration + DescribeLifecycleConfiguration", () => {
156 test.provider("sets, reads, and clears lifecycle policies", (_stack) =>
157 Effect.gen(function* () {
158 const set = (yield* send(HttpClientRequest.post(`${baseUrl}/lifecycle/set`)).pipe(
159 Effect.flatMap((r) => r.json),
160 )) as any;
161 expect(set.count).toBe(1);
162
163 const read = (yield* send(HttpClientRequest.get(`${baseUrl}/lifecycle`)).pipe(
164 Effect.flatMap((r) => r.json),
165 )) as any;
166 expect(read.count).toBe(1);
167
168 const cleared = (yield* send(HttpClientRequest.post(`${baseUrl}/lifecycle/clear`)).pipe(
169 Effect.flatMap((r) => r.json),
170 )) as any;
171 expect(cleared.count).toBe(0);
172 }),
173 );
174 });
175
176 describe("CreateAccessPoint + DeleteAccessPoint", () => {
177 test.provider("creates and deletes an access point at runtime", (_stack) =>
178 Effect.gen(function* () {
179 const response = (yield* send(HttpClientRequest.post(`${baseUrl}/access-point`)).pipe(
180 Effect.flatMap((r) => r.json),
181 )) as any;
182 expect(response.accessPointId).toMatch(/^fsap-/);
183 expect(response.deleted).toBe(true);
184 }),
185 );
186 });
187
188 describe("DescribeReplicationConfigurations", () => {
189 test.provider(
190 "returns the typed ReplicationNotFound for an unreplicated file system",
191 (_stack) =>
192 Effect.gen(function* () {
193 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/replication`)).pipe(
194 Effect.flatMap((r) => r.json),
195 )) as any;
196 expect(response.hasReplication).toBe(false);
197 }),
198 );
199 });
200 },
201 );