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

main
ts 251 lines 8.87 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 OpenSearchDataPlaneFunctionLive, { OpenSearchDataPlaneFunction } from "./data-plane-handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "OpenSearchDataPlane");
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 // Retry only transient 5xx (cold re-init, IAM propagation on the freshly
28 // attached es:ESHttp* policy); a genuine 4xx surfaces immediately. Bounded
29 // well under the 120s test timeout (~31s total) so a persistent fixture
30 // failure surfaces its body instead of a bare timeout.
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("1 second"), Schedule.recurs(5)]),
45 }),
46 );
47
48 // The fixture provisions a real OpenSearch domain (~15-25 minutes, billed
49 // per instance-hour) — skipped under `--fast` like the Domain
50 // lifecycle test. The suite stays skip-clean without the flag.
51 const describeGated = !process.env.FAST ? describe.sequential : describe.skip;
52
53 describeGated("OpenSearch Data Plane", () => {
54 beforeAll(
55 Effect.gen(function* () {
56 yield* Effect.logInfo("OpenSearch data-plane setup: destroying previous resources");
57 yield* sharedStack.destroy();
58
59 yield* Effect.logInfo(
60 "OpenSearch data-plane setup: deploying fixture (domain takes ~15-25 minutes)",
61 );
62 const { functionUrl } = yield* sharedStack.deploy(
63 Effect.gen(function* () {
64 return yield* OpenSearchDataPlaneFunction;
65 }).pipe(Effect.provide(OpenSearchDataPlaneFunctionLive)),
66 );
67
68 expect(functionUrl).toBeTruthy();
69 baseUrl = functionUrl!.replace(/\/+$/, "");
70
71 const readinessUrl = `${baseUrl}/bindings`;
72 yield* Effect.logInfo(`OpenSearch data-plane setup: probing readiness at ${readinessUrl}`);
73 yield* HttpClient.get(readinessUrl).pipe(
74 Effect.flatMap((response) =>
75 response.status === 200
76 ? Effect.succeed(response)
77 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
78 ),
79 Effect.tapError((error) =>
80 Effect.logWarning(
81 `OpenSearch data-plane setup: fixture not ready yet (${String(error)})`,
82 ),
83 ),
84 Effect.retry({ schedule: readinessPolicy }),
85 );
86 }),
87 // Domain provisioning dominates: ~15-25 minutes.
88 { timeout: 2_700_000 },
89 );
90
91 // Destroy immediately — domains bill while they exist.
92 afterAll(sharedStack.destroy(), { timeout: 600_000 });
93
94 describe(
95 "binding registration",
96 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:opensearch", "live"] },
97 () => {
98 test.provider("all 3 data-plane capabilities initialize", (_stack) =>
99 Effect.gen(function* () {
100 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe(
101 Effect.flatMap((r) => r.json),
102 )) as any;
103 expect(response.bound).toEqual(["reader", "writer", "client"]);
104 }),
105 );
106 },
107 );
108
109 describe(
110 "DomainWrite",
111 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:opensearch", "live"] },
112 () => {
113 test.provider(
114 "indexDocument writes a document (es:ESHttpPut)",
115 (_stack) =>
116 Effect.gen(function* () {
117 const response = (yield* send(HttpClientRequest.post(`${baseUrl}/doc`)).pipe(
118 Effect.flatMap((r) => r.json),
119 )) as any;
120 expect(["created", "updated"]).toContain(response.result);
121 }),
122 { timeout: 120_000 },
123 );
124
125 test.provider(
126 "bulk applies NDJSON operations (es:ESHttpPost)",
127 (_stack) =>
128 Effect.gen(function* () {
129 const response = (yield* send(HttpClientRequest.post(`${baseUrl}/bulk`)).pipe(
130 Effect.flatMap((r) => r.json),
131 )) as any;
132 expect(response.errors).toBe(false);
133 expect(response.items).toBe(1);
134 }),
135 { timeout: 120_000 },
136 );
137
138 test.provider(
139 "updateDocument partially updates (es:ESHttpPost _update)",
140 (_stack) =>
141 Effect.gen(function* () {
142 const response = (yield* send(HttpClientRequest.post(`${baseUrl}/update`)).pipe(
143 Effect.flatMap((r) => r.json),
144 )) as any;
145 expect(["updated", "noop"]).toContain(response.result);
146 }),
147 { timeout: 120_000 },
148 );
149 },
150 );
151
152 describe(
153 "DomainRead",
154 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:opensearch", "live"] },
155 () => {
156 test.provider(
157 "getDocument reads back the stored document (es:ESHttpGet)",
158 (_stack) =>
159 Effect.gen(function* () {
160 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/doc`)).pipe(
161 Effect.flatMap((r) => r.json),
162 )) as any;
163 expect(response.found).toBe(true);
164 expect(response.title).toBe("The Wind Cries Mary");
165 }),
166 { timeout: 120_000 },
167 );
168
169 test.provider(
170 "getDocument on a missing id is found:false, not an error",
171 (_stack) =>
172 Effect.gen(function* () {
173 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/doc-missing`)).pipe(
174 Effect.flatMap((r) => r.json),
175 )) as any;
176 expect(response.found).toBe(false);
177 }),
178 { timeout: 120_000 },
179 );
180
181 test.provider(
182 "existsDocument distinguishes present from missing (es:ESHttpHead)",
183 (_stack) =>
184 Effect.gen(function* () {
185 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/exists`)).pipe(
186 Effect.flatMap((r) => r.json),
187 )) as any;
188 expect(response.exists).toBe(true);
189 expect(response.missing).toBe(false);
190 }),
191 { timeout: 120_000 },
192 );
193
194 test.provider(
195 "search matches via the source query parameter (es:ESHttpGet)",
196 (_stack) =>
197 Effect.gen(function* () {
198 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/search`)).pipe(
199 Effect.flatMap((r) => r.json),
200 )) as any;
201 expect(response.total).toBeGreaterThanOrEqual(1);
202 expect(response.firstTitle).toBe("The Wind Cries Mary");
203 }),
204 { timeout: 120_000 },
205 );
206
207 test.provider(
208 "count counts documents in the index",
209 (_stack) =>
210 Effect.gen(function* () {
211 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/count`)).pipe(
212 Effect.flatMap((r) => r.json),
213 )) as any;
214 expect(response.count).toBeGreaterThanOrEqual(1);
215 }),
216 { timeout: 120_000 },
217 );
218 },
219 );
220
221 describe(
222 "DomainReadWrite",
223 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:opensearch", "live"] },
224 () => {
225 test.provider(
226 "raw request reads cluster health (es:ESHttp*)",
227 (_stack) =>
228 Effect.gen(function* () {
229 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/health`)).pipe(
230 Effect.flatMap((r) => r.json),
231 )) as any;
232 expect(["green", "yellow", "red"]).toContain(response.status);
233 }),
234 { timeout: 120_000 },
235 );
236
237 test.provider(
238 "deleteDocument deletes and tolerates not_found (es:ESHttpDelete)",
239 (_stack) =>
240 Effect.gen(function* () {
241 const response = (yield* send(HttpClientRequest.post(`${baseUrl}/delete`)).pipe(
242 Effect.flatMap((r) => r.json),
243 )) as any;
244 expect(response.first).toBe("deleted");
245 expect(response.second).toBe("not_found");
246 }),
247 { timeout: 120_000 },
248 );
249 },
250 );
251 });