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

main
ts 184 lines 6.5 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 BCMDataExportsTestFunctionLive, {
11 BCMDataExportsTestFunction,
12 fixtureExportName,
13 } from "./handler";
14
15 const testOptions = { providers: AWS.providers() };
16 const { test, beforeAll, afterAll } = Test.make(testOptions);
17 const sharedStack = Core.scratchStack(testOptions, "BCMDataExportsBindings");
18
19 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
20 // over 60s on a fresh deploy.
21 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
22
23 let baseUrl: string;
24
25 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
26 readonly status: number;
27 readonly body: string;
28 }> {}
29
30 // The shared Lambda fixture occasionally answers a transient 5xx under load
31 // (cold re-init, IAM propagation on the freshly attached policy that the
32 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
33 // 4xx/assertion failure surfaces immediately.
34 const send = (request: HttpClientRequest.HttpClientRequest) =>
35 HttpClient.execute(request).pipe(
36 Effect.flatMap((response) =>
37 response.status >= 500
38 ? response.text.pipe(
39 Effect.flatMap((body) =>
40 Effect.fail(new TransientUpstream({ status: response.status, body })),
41 ),
42 )
43 : Effect.succeed(response),
44 ),
45 Effect.retry({
46 while: (e) => e._tag === "TransientUpstream",
47 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
48 }),
49 );
50
51 describe.sequential(
52 "BCMDataExports Bindings",
53 {
54 tags: [
55 "provider:aws",
56 "provider:aws:bcmdataexports",
57 "provider:aws:lambda",
58 "provider:aws:s3",
59 "live",
60 ],
61 },
62 () => {
63 beforeAll(
64 Effect.gen(function* () {
65 yield* Effect.logInfo("BCMDataExports test setup: destroying previous resources");
66 yield* sharedStack.destroy();
67
68 yield* Effect.logInfo("BCMDataExports test setup: deploying fixture");
69 const { functionUrl } = yield* sharedStack.deploy(
70 Effect.gen(function* () {
71 return yield* BCMDataExportsTestFunction;
72 }).pipe(Effect.provide(BCMDataExportsTestFunctionLive)),
73 );
74
75 expect(functionUrl).toBeTruthy();
76 baseUrl = functionUrl!.replace(/\/+$/, "");
77
78 const readinessUrl = `${baseUrl}/bindings`;
79 yield* Effect.logInfo(`BCMDataExports test setup: probing readiness at ${readinessUrl}`);
80 yield* HttpClient.get(readinessUrl).pipe(
81 Effect.flatMap((response) =>
82 response.status === 200
83 ? Effect.succeed(response)
84 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
85 ),
86 Effect.tapError((error) =>
87 Effect.logWarning(
88 `BCMDataExports test setup: fixture not ready yet (${String(error)})`,
89 ),
90 ),
91 Effect.retry({ schedule: readinessPolicy }),
92 );
93 }),
94 { timeout: 240_000 },
95 );
96
97 afterAll(sharedStack.destroy(), { timeout: 120_000 });
98
99 describe("binding registration", () => {
100 test.provider("all 6 capabilities initialize in the runtime", (_stack) =>
101 Effect.gen(function* () {
102 const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe(
103 Effect.flatMap((r) => r.json),
104 );
105 expect((response as any).bound).toHaveLength(6);
106 }),
107 );
108 });
109
110 describe("GetExport", () => {
111 test.provider("reads the fixture export's definition", (_stack) =>
112 Effect.gen(function* () {
113 const response = yield* send(HttpClientRequest.get(`${baseUrl}/export`)).pipe(
114 Effect.flatMap((r) => r.json),
115 );
116 expect((response as any).name).toBe(fixtureExportName);
117 expect((response as any).prefix).toBe("bindings");
118 expect((response as any).frequency).toBe("SYNCHRONOUS");
119 }),
120 );
121 });
122
123 describe("ListExports", () => {
124 test.provider("lists the fixture export among the account's exports", (_stack) =>
125 Effect.gen(function* () {
126 const response = yield* send(HttpClientRequest.get(`${baseUrl}/exports`)).pipe(
127 Effect.flatMap((r) => r.json),
128 );
129 expect((response as any).names).toContain(fixtureExportName);
130 }),
131 );
132 });
133
134 describe("ListExecutions", () => {
135 test.provider("lists the export's executions (fresh export: >= 0)", (_stack) =>
136 Effect.gen(function* () {
137 const response = yield* send(HttpClientRequest.get(`${baseUrl}/executions`)).pipe(
138 Effect.flatMap((r) => r.json),
139 );
140 expect(typeof (response as any).count).toBe("number");
141 expect((response as any).count).toBeGreaterThanOrEqual(0);
142 }),
143 );
144 });
145
146 describe("GetExecution", () => {
147 test.provider(
148 "surfaces a typed error for a nonexistent execution (proving the grant)",
149 (_stack) =>
150 Effect.gen(function* () {
151 const response = yield* send(
152 HttpClientRequest.get(`${baseUrl}/execution-not-found`),
153 ).pipe(Effect.flatMap((r) => r.json));
154 expect(["ResourceNotFoundException", "ValidationException"]).toContain(
155 (response as any).tag,
156 );
157 }),
158 );
159 });
160
161 describe("GetTable", () => {
162 test.provider("reads the COST_AND_USAGE_REPORT table schema", (_stack) =>
163 Effect.gen(function* () {
164 const response = yield* send(HttpClientRequest.get(`${baseUrl}/table`)).pipe(
165 Effect.flatMap((r) => r.json),
166 );
167 expect((response as any).tableName).toBe("COST_AND_USAGE_REPORT");
168 expect((response as any).columnCount).toBeGreaterThan(0);
169 }),
170 );
171 });
172
173 describe("ListTables", () => {
174 test.provider("lists the table dictionary", (_stack) =>
175 Effect.gen(function* () {
176 const response = yield* send(HttpClientRequest.get(`${baseUrl}/tables`)).pipe(
177 Effect.flatMap((r) => r.json),
178 );
179 expect((response as any).names).toContain("COST_AND_USAGE_REPORT");
180 }),
181 );
182 });
183 },
184 );