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

main
ts 170 lines 6.56 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 S3TablesBindingsFunctionLive, { S3TablesBindingsFunction } from "./bindings-handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "S3TablesBindings");
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 "S3Tables Bindings",
50 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:s3tables", "live"] },
51 () => {
52 beforeAll(
53 Effect.gen(function* () {
54 yield* Effect.logInfo("S3Tables test setup: destroying previous resources");
55 yield* sharedStack.destroy();
56
57 yield* Effect.logInfo("S3Tables test setup: deploying fixture");
58 const { functionUrl } = yield* sharedStack.deploy(
59 Effect.gen(function* () {
60 return yield* S3TablesBindingsFunction;
61 }).pipe(Effect.provide(S3TablesBindingsFunctionLive)),
62 );
63
64 expect(functionUrl).toBeTruthy();
65 baseUrl = functionUrl!.replace(/\/+$/, "");
66
67 const readinessUrl = `${baseUrl}/bindings`;
68 yield* Effect.logInfo(`S3Tables 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(`S3Tables 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 6 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(6);
93 }),
94 );
95 });
96
97 describe("ListNamespaces", () => {
98 test.provider("lists the bucket's namespaces", (_stack) =>
99 Effect.gen(function* () {
100 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/namespaces`)).pipe(
101 Effect.flatMap((r) => r.json),
102 )) as any;
103 expect(response.names).toContain("bindings");
104 }),
105 );
106 });
107
108 describe("ListTables", () => {
109 test.provider("lists the namespace's tables", (_stack) =>
110 Effect.gen(function* () {
111 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/tables`)).pipe(
112 Effect.flatMap((r) => r.json),
113 )) as any;
114 expect(response.names).toContain("events");
115 }),
116 );
117 });
118
119 describe("GetTable", () => {
120 test.provider("reads the bound table's details", (_stack) =>
121 Effect.gen(function* () {
122 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/table`)).pipe(
123 Effect.flatMap((r) => r.json),
124 )) as any;
125 expect(response.name).toBe("events");
126 expect(response.format).toBe("ICEBERG");
127 expect(response.versionToken).toBeTruthy();
128 expect(response.warehouseLocation).toMatch(/^s3:\/\//);
129 }),
130 );
131 });
132
133 describe("GetTableMetadataLocation + UpdateTableMetadataLocation", () => {
134 test.provider("round-trips the Iceberg commit protocol", (_stack) =>
135 Effect.gen(function* () {
136 const current = (yield* send(HttpClientRequest.get(`${baseUrl}/metadata-location`)).pipe(
137 Effect.flatMap((r) => r.json),
138 )) as any;
139 expect(current.versionToken).toBeTruthy();
140 expect(current.warehouseLocation).toMatch(/^s3:\/\//);
141
142 // The commit either succeeds (the service records the pointer) or is
143 // rejected with a typed 4xx tag because the fixture didn't write a
144 // real Iceberg metadata file first — both prove the IAM grant and
145 // identifier injection (a grant gap would be a 500 AccessDenied).
146 const commit = (yield* send(
147 HttpClientRequest.post(`${baseUrl}/metadata-location/commit`),
148 ).pipe(Effect.flatMap((r) => r.json))) as any;
149 if (commit.committed) {
150 expect(commit.versionTokenChanged).toBe(true);
151 } else {
152 expect(["BadRequestException", "ConflictException"]).toContain(commit.errorTag);
153 }
154 }),
155 );
156 });
157
158 describe("GetTableMaintenanceJobStatus", () => {
159 test.provider("reads the table's maintenance job statuses", (_stack) =>
160 Effect.gen(function* () {
161 const response = (yield* send(HttpClientRequest.get(`${baseUrl}/maintenance-jobs`)).pipe(
162 Effect.flatMap((r) => r.json),
163 )) as any;
164 expect(response.tableArn).toContain("/table/");
165 expect(Array.isArray(response.jobs)).toBe(true);
166 }),
167 );
168 });
169 },
170 );