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

main
ts 295 lines 10.4 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 SimpleDBTestFunctionLive, { SimpleDBTestFunction } from "./handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "SimpleDBBindings");
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. Budget ~150s of
18 // readiness polling so we don't fail the whole suite on a slow init.
19 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
20
21 let baseUrl: string;
22
23 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
24 readonly status: number;
25 readonly body: string;
26 }> {}
27
28 // The shared Lambda fixture occasionally answers a transient 5xx under
29 // full-suite parallel load — a cold re-init or a throttled SimpleDB call the
30 // handler's `Effect.orDie` surfaces as a 500. Retry 5xx a few times; genuine
31 // 4xx/assertion failures are returned immediately.
32 const send = (request: HttpClientRequest.HttpClientRequest) =>
33 HttpClient.execute(request).pipe(
34 Effect.flatMap((response) =>
35 response.status >= 500
36 ? response.text.pipe(
37 Effect.flatMap((body) =>
38 Effect.fail(new TransientUpstream({ status: response.status, body })),
39 ),
40 )
41 : Effect.succeed(response),
42 ),
43 Effect.retry({
44 while: (e) => e._tag === "TransientUpstream" || e._tag === "HttpClientError",
45 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
46 }),
47 );
48
49 // Response bodies cross the HTTP wire, so they are opaque JSON to the test.
50 // Widen to `unknown` so call sites narrow with a single explicit cast.
51 const asUnknown = Effect.map((body: unknown) => body);
52
53 const postJson = (path: string, body: unknown) =>
54 send(HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}${path}`), body)).pipe(
55 Effect.flatMap((r) => r.json),
56 asUnknown,
57 );
58
59 const deleteJson = (path: string, body: unknown) =>
60 send(HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.delete(`${baseUrl}${path}`), body)).pipe(
61 Effect.flatMap((r) => r.json),
62 asUnknown,
63 );
64
65 const getJson = (path: string) =>
66 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(
67 Effect.flatMap((r) => r.json),
68 asUnknown,
69 );
70
71 interface Attribute {
72 Name: string;
73 Value: string;
74 }
75
76 interface Item {
77 Name: string;
78 Attributes?: Attribute[];
79 }
80
81 describe(
82 "SimpleDB Bindings",
83 {
84 tags: [
85 "provider:aws",
86 "provider:aws:batch",
87 "provider:aws:lambda",
88 "provider:aws:simpledb",
89 "live",
90 ],
91 },
92 () => {
93 beforeAll(
94 Effect.gen(function* () {
95 yield* Effect.logInfo("SimpleDB test setup: destroying previous resources");
96 yield* sharedStack.destroy();
97
98 yield* Effect.logInfo("SimpleDB test setup: deploying fixture");
99 const { functionUrl } = yield* sharedStack.deploy(
100 Effect.gen(function* () {
101 return yield* SimpleDBTestFunction;
102 }).pipe(Effect.provide(SimpleDBTestFunctionLive)),
103 );
104
105 expect(functionUrl).toBeTruthy();
106 baseUrl = functionUrl!.replace(/\/+$/, "");
107 const readinessUrl = `${baseUrl}/metadata`;
108
109 yield* Effect.logInfo(`SimpleDB test setup: probing readiness at ${readinessUrl}`);
110 yield* HttpClient.get(readinessUrl).pipe(
111 Effect.flatMap((response) =>
112 response.status === 200
113 ? Effect.succeed(response)
114 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
115 ),
116 Effect.tapError((error) =>
117 Effect.logWarning(`SimpleDB test setup: fixture not ready yet (${String(error)})`),
118 ),
119 Effect.retry({ schedule: readinessPolicy }),
120 );
121 }),
122 { timeout: 240_000 },
123 );
124
125 afterAll(sharedStack.destroy(), { timeout: 120_000 });
126
127 describe("PutAttributes", () => {
128 test.provider("puts attributes on an item", (_stack) =>
129 Effect.gen(function* () {
130 const response = yield* postJson("/put", {
131 item: "put-test#1",
132 attributes: [{ name: "color", value: "blue" }],
133 });
134 expect(response).toHaveProperty("success", true);
135 }),
136 );
137 });
138
139 describe("GetAttributes", () => {
140 test.provider("round-trips put then get", (_stack) =>
141 Effect.gen(function* () {
142 yield* postJson("/put", {
143 item: "get-test#1",
144 attributes: [
145 { name: "color", value: "green" },
146 { name: "size", value: "large" },
147 ],
148 });
149
150 const response = (yield* getJson(`/get?item=${encodeURIComponent("get-test#1")}`)) as {
151 attributes: Attribute[];
152 };
153
154 const byName = Object.fromEntries(response.attributes.map((a) => [a.Name, a.Value]));
155 expect(byName).toMatchObject({ color: "green", size: "large" });
156 }),
157 );
158
159 test.provider("returns no attributes for a missing item", (_stack) =>
160 Effect.gen(function* () {
161 const response = (yield* getJson(
162 `/get?item=${encodeURIComponent("missing-item#404")}`,
163 )) as { attributes: Attribute[] };
164 expect(response.attributes).toEqual([]);
165 }),
166 );
167 });
168
169 describe("Select", () => {
170 test.provider("selects items with a where clause", (_stack) =>
171 Effect.gen(function* () {
172 yield* postJson("/put", {
173 item: "select-test#1",
174 attributes: [{ name: "kind", value: "select-target" }],
175 });
176
177 const response = (yield* getJson(
178 `/select?where=${encodeURIComponent("kind = 'select-target'")}`,
179 )) as { items: Item[] };
180
181 expect(response.items.map((i) => i.Name)).toContain("select-test#1");
182 const item = response.items.find((i) => i.Name === "select-test#1")!;
183 expect(item.Attributes).toContainEqual({ Name: "kind", Value: "select-target" });
184 }),
185 );
186 });
187
188 describe("BatchPutAttributes", () => {
189 test.provider("puts attributes on multiple items at once", (_stack) =>
190 Effect.gen(function* () {
191 const response = yield* postJson("/batch-put", {
192 items: [
193 { item: "batch-test#1", attributes: [{ name: "batch", value: "one" }] },
194 { item: "batch-test#2", attributes: [{ name: "batch", value: "two" }] },
195 ],
196 });
197 expect(response).toHaveProperty("success", true);
198
199 const first = (yield* getJson(`/get?item=${encodeURIComponent("batch-test#1")}`)) as {
200 attributes: Attribute[];
201 };
202 expect(first.attributes).toContainEqual({ Name: "batch", Value: "one" });
203 const second = (yield* getJson(`/get?item=${encodeURIComponent("batch-test#2")}`)) as {
204 attributes: Attribute[];
205 };
206 expect(second.attributes).toContainEqual({ Name: "batch", Value: "two" });
207 }),
208 );
209 });
210
211 describe("DeleteAttributes", () => {
212 test.provider("deletes a whole item", (_stack) =>
213 Effect.gen(function* () {
214 yield* postJson("/put", {
215 item: "delete-test#1",
216 attributes: [{ name: "doomed", value: "yes" }],
217 });
218
219 const response = yield* deleteJson("/delete", { item: "delete-test#1" });
220 expect(response).toHaveProperty("success", true);
221
222 const after = (yield* getJson(`/get?item=${encodeURIComponent("delete-test#1")}`)) as {
223 attributes: Attribute[];
224 };
225 expect(after.attributes).toEqual([]);
226 }),
227 );
228
229 test.provider("deletes a single attribute", (_stack) =>
230 Effect.gen(function* () {
231 yield* postJson("/put", {
232 item: "delete-test#2",
233 attributes: [
234 { name: "keep", value: "me" },
235 { name: "drop", value: "me" },
236 ],
237 });
238
239 yield* deleteJson("/delete", { item: "delete-test#2", attributes: ["drop"] });
240
241 const after = (yield* getJson(`/get?item=${encodeURIComponent("delete-test#2")}`)) as {
242 attributes: Attribute[];
243 };
244 expect(after.attributes).toContainEqual({ Name: "keep", Value: "me" });
245 expect(after.attributes.map((a) => a.Name)).not.toContain("drop");
246 }),
247 );
248 });
249
250 describe("BatchDeleteAttributes", () => {
251 test.provider("deletes multiple items at once", (_stack) =>
252 Effect.gen(function* () {
253 yield* postJson("/batch-put", {
254 items: [
255 { item: "batch-delete#1", attributes: [{ name: "x", value: "1" }] },
256 { item: "batch-delete#2", attributes: [{ name: "x", value: "2" }] },
257 ],
258 });
259
260 const response = yield* postJson("/batch-delete", {
261 items: ["batch-delete#1", "batch-delete#2"],
262 });
263 expect(response).toHaveProperty("success", true);
264
265 const first = (yield* getJson(`/get?item=${encodeURIComponent("batch-delete#1")}`)) as {
266 attributes: Attribute[];
267 };
268 expect(first.attributes).toEqual([]);
269 const second = (yield* getJson(`/get?item=${encodeURIComponent("batch-delete#2")}`)) as {
270 attributes: Attribute[];
271 };
272 expect(second.attributes).toEqual([]);
273 }),
274 );
275 });
276
277 describe("ListDomains", () => {
278 test.provider("lists the fixture's domain", (_stack) =>
279 Effect.gen(function* () {
280 const response = (yield* getJson("/domains")) as { domainNames: string[] };
281 expect(response.domainNames.some((name) => name.includes("BindingsDomain"))).toBe(true);
282 }),
283 );
284 });
285
286 describe("DomainMetadata", () => {
287 test.provider("returns domain metadata", (_stack) =>
288 Effect.gen(function* () {
289 const response = (yield* getJson("/metadata")) as { itemCount?: number };
290 expect(typeof response.itemCount).toBe("number");
291 }),
292 );
293 });
294 },
295 );