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

main
ts 332 lines 13.2 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 logs from "@distilled.cloud/aws/cloudwatch-logs";
6 import * as dataexchange from "@distilled.cloud/aws/dataexchange";
7 import * as eventbridge from "@distilled.cloud/aws/eventbridge";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as HttpClient from "effect/http/HttpClient";
11 import * as HttpClientRequest from "effect/http/HttpClientRequest";
12 import * as Schedule from "effect/Schedule";
13 import DataExchangeTestFunctionLive, { DataExchangeTestFunction } from "./handler";
14
15 const testOptions = { providers: AWS.providers() };
16 const { test, beforeAll, afterAll } = Test.make(testOptions);
17 const sharedStack = Core.scratchStack(testOptions, "DataExchangeBindings");
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 let functionArn: string;
25 let functionName: string | undefined;
26 let fixtureDataSetId: string | undefined;
27
28 // beforeAll/afterAll hooks run outside `test.provider`'s layer, so raw
29 // distilled calls need the provider layer (credentials, region) supplied
30 // explicitly.
31 const aws = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
32 Core.withProviders(effect, testOptions, sharedStack.name);
33
34 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
35 readonly status: number;
36 readonly body: string;
37 }> {}
38
39 // The shared Lambda fixture occasionally answers a transient 5xx under load
40 // (cold re-init, IAM propagation on the freshly attached policy that the
41 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
42 // 4xx/assertion failure surfaces immediately.
43 const send = (request: HttpClientRequest.HttpClientRequest) =>
44 HttpClient.execute(request).pipe(
45 Effect.flatMap((response) =>
46 response.status >= 500
47 ? response.text.pipe(
48 Effect.flatMap((body) =>
49 Effect.fail(new TransientUpstream({ status: response.status, body })),
50 ),
51 )
52 : Effect.succeed(response),
53 ),
54 Effect.retry({
55 while: (e) => e._tag === "TransientUpstream",
56 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
57 }),
58 );
59
60 const getJson = (path: string) =>
61 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
62
63 const postJson = (path: string) =>
64 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
65
66 class IamPropagationLag extends Data.TaggedError("IamPropagationLag")<{ readonly path: string }> {}
67
68 // A freshly attached IAM policy can lag behind the Lambda's first calls —
69 // DataExchange then rejects with AccessDeniedException, which the handler
70 // surfaces as `{ error: "AccessDeniedException" }`. Retry the route through
71 // the propagation window (bounded ~5.5 minutes — under full-suite load IAM
72 // propagation for fresh roles routinely exceeds 4 minutes); any other
73 // outcome surfaces immediately.
74 const postJsonThroughIamPropagation = (path: string) =>
75 postJson(path).pipe(
76 Effect.flatMap((body) =>
77 (body as { error?: string }).error === "AccessDeniedException"
78 ? Effect.fail(new IamPropagationLag({ path }))
79 : Effect.succeed(body),
80 ),
81 Effect.retry({
82 while: (e) => e._tag === "IamPropagationLag",
83 schedule: Schedule.max([Schedule.spaced("10 seconds"), Schedule.recurs(33)]),
84 }),
85 );
86
87 describe.sequential(
88 "DataExchange Bindings",
89 {
90 tags: [
91 "provider:aws",
92 "provider:aws:dataexchange",
93 "provider:aws:lambda",
94 "provider:aws:s3",
95 "live",
96 ],
97 },
98 () => {
99 beforeAll(
100 Effect.gen(function* () {
101 yield* Effect.logInfo("DataExchange test setup: destroying previous resources");
102 yield* sharedStack.destroy();
103
104 yield* Effect.logInfo("DataExchange test setup: deploying fixture");
105 const attrs = yield* sharedStack.deploy(
106 Effect.gen(function* () {
107 return yield* DataExchangeTestFunction;
108 }).pipe(Effect.provide(DataExchangeTestFunctionLive)),
109 );
110
111 expect(attrs.functionUrl).toBeTruthy();
112 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
113 functionArn = attrs.functionArn;
114 functionName = attrs.functionName;
115
116 const readinessUrl = `${baseUrl}/bindings`;
117 yield* Effect.logInfo(`DataExchange test setup: probing readiness at ${readinessUrl}`);
118 yield* HttpClient.get(readinessUrl).pipe(
119 Effect.flatMap((response) =>
120 response.status === 200
121 ? Effect.succeed(response)
122 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
123 ),
124 Effect.tapError((error) =>
125 Effect.logWarning(`DataExchange test setup: fixture not ready yet (${String(error)})`),
126 ),
127 Effect.retry({ schedule: readinessPolicy }),
128 );
129
130 // Capture the fixture data set's id so afterAll can assert it is gone
131 // after the final destroy.
132 const dataSet = (yield* getJson("/data-set")) as { id: string };
133 fixtureDataSetId = dataSet.id;
134 }),
135 { timeout: 240_000 },
136 );
137
138 afterAll(
139 Effect.gen(function* () {
140 yield* sharedStack.destroy();
141 // Assert the fixture's data set really is gone (zero orphans).
142 if (fixtureDataSetId !== undefined) {
143 const gone = yield* Effect.flip(
144 aws(dataexchange.getDataSet({ DataSetId: fixtureDataSetId })),
145 );
146 expect(gone._tag).toBe("ResourceNotFoundException");
147 }
148
149 // The Lambda provider reaps /aws/lambda/{name} on delete and watches
150 // ~90s for a flush-driven recreation, but this suite invokes the
151 // function right up until destroy, and the Lambda service occasionally
152 // flushes a final log batch AFTER that watch ends — silently
153 // re-creating the just-deleted group. Watch a bit longer here with a
154 // bounded observe→delete loop (every delete is idempotent); a flush
155 // landing after the final observation is unobservable and the nuke
156 // census is the backstop.
157 if (functionName !== undefined) {
158 const logGroupName = `/aws/lambda/${functionName}`;
159 const reapIfObserved = Effect.gen(function* () {
160 const present = yield* aws(
161 logs.describeLogGroups({ logGroupNamePrefix: logGroupName, limit: 1 }),
162 ).pipe(
163 Effect.map((response) =>
164 (response.logGroups ?? []).some((group) => group.logGroupName === logGroupName),
165 ),
166 );
167 if (present) {
168 yield* aws(logs.deleteLogGroup({ logGroupName })).pipe(
169 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
170 );
171 }
172 return present;
173 });
174 // Fixed 6 passes over ~100s — do NOT stop on first absence, since an
175 // absent group can still be recreated by a late flush.
176 yield* reapIfObserved.pipe(
177 Effect.repeat({ schedule: Schedule.spaced("20 seconds"), times: 5 }),
178 );
179 const stillPresent = yield* reapIfObserved;
180 expect(stillPresent).toBe(false);
181 }
182 }),
183 { timeout: 420_000 },
184 );
185
186 describe("binding registration", () => {
187 test.provider("all capabilities initialize in the runtime", (_stack) =>
188 Effect.gen(function* () {
189 const response = (yield* getJson("/bindings")) as { bound: string[] };
190 expect(response.bound).toContain("getDataSet");
191 expect(response.bound).toContain("createJob");
192 expect(response.bound).toContain("listReceivedDataGrants");
193 expect(response.bound).toHaveLength(16);
194 }),
195 );
196 });
197
198 describe("GetDataSet", () => {
199 test.provider("reads the bound data set's detail (injected data set id)", (_stack) =>
200 Effect.gen(function* () {
201 const response = (yield* getJson("/data-set")) as {
202 id: string;
203 name: string;
204 assetType: string;
205 origin: string;
206 };
207 expect(response.id).toBeTruthy();
208 expect(response.assetType).toBe("S3_SNAPSHOT");
209 expect(response.origin).toBe("OWNED");
210 }),
211 );
212 });
213
214 describe("ListDataSetRevisions", () => {
215 test.provider("enumerates the bound data set's revisions", (_stack) =>
216 Effect.gen(function* () {
217 const revision = (yield* getJson("/revision")) as { id: string };
218 const response = (yield* getJson("/revisions")) as { ids: string[] };
219 expect(response.ids).toContain(revision.id);
220 }),
221 );
222 });
223
224 describe("GetRevision", () => {
225 test.provider("reads the bound revision (injected data set + revision ids)", (_stack) =>
226 Effect.gen(function* () {
227 const response = (yield* getJson("/revision")) as { id: string; finalized: boolean };
228 expect(response.id).toBeTruthy();
229 expect(response.finalized).toBe(false);
230 }),
231 );
232 });
233
234 describe("ListDataSets", () => {
235 test.provider("enumerates the account's owned data sets including the fixture's", (_stack) =>
236 Effect.gen(function* () {
237 const dataSet = (yield* getJson("/data-set")) as { id: string };
238 const response = (yield* getJson("/data-sets")) as { ids: string[] };
239 expect(response.ids).toContain(dataSet.id);
240 }),
241 );
242 });
243
244 describe("CreateJob / StartJob / GetJob / ListRevisionAssets / GetAsset", () => {
245 test.provider(
246 "imports an S3 object into the revision via a job and reads it back",
247 (_stack) =>
248 Effect.gen(function* () {
249 const response = (yield* postJsonThroughIamPropagation("/import")) as {
250 jobState?: string;
251 jobErrors?: unknown[];
252 assetCount?: number;
253 assetName?: string;
254 error?: string;
255 message?: string;
256 };
257 expect(response.error, response.message).toBeUndefined();
258 expect(JSON.stringify(response.jobErrors ?? [])).toBe("[]");
259 expect(response.jobState).toBe("COMPLETED");
260 expect(response.assetCount).toBeGreaterThanOrEqual(1);
261 expect(response.assetName).toBe("prices.csv");
262 }),
263 // Covers the full IAM propagation retry budget (~5.5 min) plus the
264 // import job's own create → start → poll-to-COMPLETED runtime.
265 { timeout: 480_000 },
266 );
267
268 test.provider("ListJobs sees the import job", (_stack) =>
269 Effect.gen(function* () {
270 const response = (yield* getJson("/jobs")) as { states: string[] };
271 expect(response.states).toContain("COMPLETED");
272 }),
273 );
274 });
275
276 describe("SendDataSetNotification", () => {
277 // Provider-generated notifications only work for data sets attached to
278 // an AWS Marketplace data product, which cannot be provisioned
279 // self-contained. The typed rejection proves the binding's IAM grant,
280 // call path, and error decoding end-to-end.
281 test.provider(
282 "rejects a data set outside a Marketplace product with a typed error",
283 (_stack) =>
284 Effect.gen(function* () {
285 const response = (yield* postJsonThroughIamPropagation("/notify")) as {
286 ok: boolean;
287 error: string | undefined;
288 message: string | undefined;
289 };
290 expect(response.ok).toBe(false);
291 expect(response.error).toBe("ValidationException");
292 expect(response.message).toContain("not configured for AWS Marketplace");
293 }),
294 // Shares the IAM propagation retry budget with the /import test.
295 { timeout: 420_000 },
296 );
297 });
298
299 describe("ListDataGrants / ListReceivedDataGrants", () => {
300 test.provider("enumerates sent and received data grants", (_stack) =>
301 Effect.gen(function* () {
302 const response = (yield* getJson("/grants")) as { sent: number; received: number };
303 expect(response.sent).toBeGreaterThanOrEqual(0);
304 expect(response.received).toBeGreaterThanOrEqual(0);
305 }),
306 );
307 });
308
309 describe("ListEventActions", () => {
310 test.provider("enumerates the account's event actions", (_stack) =>
311 Effect.gen(function* () {
312 const response = (yield* getJson("/event-actions")) as { count: number };
313 expect(response.count).toBeGreaterThanOrEqual(0);
314 }),
315 );
316 });
317
318 describe("consumeDataSetEvents", { tags: ["provider:aws:eventbridge"] }, () => {
319 test.provider("the deploy created an EventBridge rule targeting the function", (_stack) =>
320 Effect.gen(function* () {
321 // Out-of-band via distilled: the fixture's consumeDataSetEvents
322 // must have materialized as a rule on the default bus with the
323 // Lambda as target.
324 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
325 TargetArn: functionArn,
326 });
327 expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1);
328 }),
329 );
330 });
331 },
332 );