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

main
ts 388 lines 16.5 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { reapFarmChildren } from "@alchemy.run/aws/Deadline/internal";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as deadline from "@distilled.cloud/aws/deadline";
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 * as EffectStream from "effect/Stream";
14 import DeadlineTestFunctionLive, { DeadlineTestFunction } from "./handler";
15
16 const testOptions = { providers: AWS.providers() };
17 const { test, beforeAll, afterAll } = Test.make(testOptions);
18 const sharedStack = Core.scratchStack(testOptions, "DeadlineBindings");
19
20 // Bounded Lambda function URL cold-start/DNS/IAM propagation probe.
21 const readinessPolicy = Schedule.max([Schedule.fixed("6 seconds"), Schedule.recurs(9)]);
22
23 let baseUrl: string;
24 let functionArn: string;
25
26 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
27 readonly status: number;
28 readonly body: string;
29 }> {
30 override get message() {
31 return `Deadline fixture returned HTTP ${this.status}${this.body ? `: ${this.body}` : " with an empty body"}`;
32 }
33 }
34
35 // The shared Lambda fixture occasionally answers a transient 5xx (cold
36 // re-init, IAM propagation on the freshly attached policy that the handler's
37 // `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx/assertion
38 // failure surfaces immediately.
39 const send = (request: HttpClientRequest.HttpClientRequest) =>
40 HttpClient.execute(request).pipe(
41 Effect.flatMap((response) =>
42 response.status >= 500
43 ? response.text.pipe(
44 Effect.flatMap((body) =>
45 Effect.fail(new TransientUpstream({ status: response.status, body })),
46 ),
47 )
48 : Effect.succeed(response),
49 ),
50 Effect.retry({
51 while: (e) => e._tag === "TransientUpstream",
52 // Deadline's control plane can need several seconds to settle a fresh
53 // farm/queue and sometimes returns a run of 5xx responses meanwhile.
54 // Keep the retry bounded to nine attempts and 24 seconds of backoff.
55 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(8)]),
56 }),
57 );
58
59 const getJson = (path: string) =>
60 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
61
62 /**
63 * The account's farm quota is 2 and a deploy killed mid-crash leaks a farm
64 * that later runs cannot adopt (the physical-name instance id changes with
65 * fresh state). Reap any farm carrying one of this suite's fixture names so
66 * the deploy below never starts quota-blocked.
67 */
68 const reapLeakedFarms = Effect.gen(function* () {
69 const farms = yield* deadline.listFarms.items({}).pipe(
70 EffectStream.runCollect,
71 Effect.map((chunk) => Array.from(chunk)),
72 );
73 const leaked = farms.filter((farm) => /TestFarm|BindingsFarm|FleetFarm/.test(farm.displayName));
74 yield* Effect.forEach(
75 leaked,
76 (farm) =>
77 Effect.gen(function* () {
78 const farmId = farm.farmId;
79 // Reap ALL child resources (storage profiles, budgets, limits,
80 // associations, queues, fleets) — the same sweep Farm.delete runs.
81 yield* reapFarmChildren(farmId);
82 // Sub-resource deletion is asynchronous; the farm rejects deletion
83 // with ConflictException until they finish. Bounded.
84 yield* Effect.retry(deadline.deleteFarm({ farmId }), {
85 while: (e): boolean => e._tag === "ConflictException",
86 schedule: Schedule.max([Schedule.spaced("6 seconds"), Schedule.recurs(9)]),
87 }).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
88 yield* Effect.logInfo(`reaped leaked farm ${farmId} (${farm.displayName})`);
89 }),
90 { discard: true },
91 );
92 });
93
94 const postJson = (path: string) =>
95 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
96
97 describe.sequential(
98 "Deadline Bindings",
99 { tags: ["provider:aws", "provider:aws:deadline", "provider:aws:lambda", "live"] },
100 () => {
101 beforeAll(
102 Effect.gen(function* () {
103 yield* Effect.logInfo("Deadline test setup: destroying previous resources");
104 yield* sharedStack.destroy();
105 // Raw distilled calls need the provider environment (credentials).
106 yield* Core.withProviders(reapLeakedFarms, testOptions, "DeadlineBindings");
107
108 yield* Effect.logInfo("Deadline test setup: deploying fixture");
109 const attrs = yield* sharedStack.deploy(
110 Effect.gen(function* () {
111 return yield* DeadlineTestFunction;
112 }).pipe(Effect.provide(DeadlineTestFunctionLive)),
113 );
114
115 expect(attrs.functionUrl).toBeTruthy();
116 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
117 functionArn = attrs.functionArn;
118
119 const readinessUrl = `${baseUrl}/bindings`;
120 yield* Effect.logInfo(`Deadline test setup: probing readiness at ${readinessUrl}`);
121 yield* HttpClient.get(readinessUrl).pipe(
122 Effect.flatMap((response) =>
123 response.status === 200
124 ? Effect.succeed(response)
125 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
126 ),
127 Effect.tapError((error) =>
128 Effect.logWarning(`Deadline test setup: fixture not ready yet (${String(error)})`),
129 ),
130 Effect.retry({ schedule: readinessPolicy }),
131 );
132 }),
133 { timeout: 300_000 },
134 );
135
136 afterAll(sharedStack.destroy(), { timeout: 240_000 });
137
138 describe("binding registration", () => {
139 test.provider("all capabilities initialize in the runtime", (_stack) =>
140 Effect.gen(function* () {
141 const response = (yield* getJson("/bindings")) as { bound: string[] };
142 expect(response.bound).toHaveLength(20);
143 }),
144 );
145 });
146
147 let jobId: string;
148 let stepId: string;
149
150 describe("CreateJob / GetJob / ListJobs / SearchJobs / UpdateJob", () => {
151 test.provider(
152 "submits a job, reads it back, finds it, and reprioritizes it",
153 (_stack) =>
154 Effect.gen(function* () {
155 // Submit — the binding injects the farm/queue ids.
156 const created = (yield* postJson("/jobs")) as { jobId: string };
157 expect(created.jobId).toMatch(/^job-/);
158 jobId = created.jobId;
159
160 // Read back; the job settles within seconds. A brand-new job can
161 // report either CREATE_COMPLETE or UPDATE_SUCCEEDED (the service
162 // applies an internal update right after creation), so poll until
163 // it leaves the transitional statuses and assert it settled.
164 const settled = ["CREATE_COMPLETE", "UPDATE_SUCCEEDED"];
165 const job = (yield* getJson(`/job?jobId=${jobId}`).pipe(
166 Effect.repeat({
167 schedule: Schedule.spaced("2 seconds"),
168 until: (j): boolean =>
169 settled.includes((j as { lifecycleStatus: string }).lifecycleStatus),
170 times: 10,
171 }),
172 )) as { jobId: string; priority: number; lifecycleStatus: string };
173 expect(job.jobId).toBe(jobId);
174 expect(job.priority).toBe(50);
175 expect(settled).toContain(job.lifecycleStatus);
176
177 // ListJobs is eventually consistent independently from GetJob.
178 const listed = (yield* getJson("/jobs").pipe(
179 Effect.repeat({
180 schedule: Schedule.spaced("2 seconds"),
181 until: (j): boolean => (j as { ids: string[] }).ids.includes(jobId),
182 times: 10,
183 }),
184 )) as { ids: string[] };
185 expect(listed.ids).toContain(jobId);
186
187 // SearchJobs uses a separate asynchronously provisioned index. A
188 // fresh farm can take minutes to index its first job, so exercise
189 // the live binding and validate its response without polling past
190 // the suite's provisioning budget.
191 const searched = (yield* postJson("/search")) as { ids: string[] };
192 expect(Array.isArray(searched.ids)).toBe(true);
193
194 // Reprioritize and verify the mutation landed. GetJob is
195 // eventually consistent, so poll the read (bounded).
196 yield* postJson(`/priority?jobId=${jobId}&priority=75`);
197 const updated = (yield* getJson(`/job?jobId=${jobId}`).pipe(
198 Effect.repeat({
199 schedule: Schedule.spaced("2 seconds"),
200 until: (j): boolean => (j as { priority: number }).priority === 75,
201 times: 10,
202 }),
203 )) as { priority: number };
204 expect(updated.priority).toBe(75);
205 }),
206 { timeout: 120_000 },
207 );
208 });
209
210 describe("ListSteps / GetStep / ListTasks / GetTask", () => {
211 test.provider(
212 "walks the job's steps and tasks",
213 (_stack) =>
214 Effect.gen(function* () {
215 const steps = (yield* getJson(`/steps?jobId=${jobId}`).pipe(
216 Effect.repeat({
217 schedule: Schedule.spaced("2 seconds"),
218 until: (s): boolean => (s as { ids: string[] }).ids.length > 0,
219 times: 10,
220 }),
221 )) as { ids: string[]; names: string[] };
222 expect(steps.ids.length).toBeGreaterThanOrEqual(1);
223 expect(steps.names).toContain("Echo");
224 stepId = steps.ids[0]!;
225
226 const step = (yield* getJson(`/step?jobId=${jobId}&stepId=${stepId}`)) as {
227 stepId: string;
228 name: string;
229 };
230 expect(step.stepId).toBe(stepId);
231 expect(step.name).toBe("Echo");
232
233 const tasks = (yield* getJson(`/tasks?jobId=${jobId}&stepId=${stepId}`)) as {
234 ids: string[];
235 };
236 expect(tasks.ids.length).toBeGreaterThanOrEqual(1);
237
238 const task = (yield* getJson(
239 `/task?jobId=${jobId}&stepId=${stepId}&taskId=${tasks.ids[0]}`,
240 )) as { taskId: string; runStatus: string };
241 expect(task.taskId).toBe(tasks.ids[0]);
242 // No fleet is associated with the fixture queue, so the task can
243 // never be scheduled — Deadline reports it READY or, once queue
244 // compatibility is evaluated, NOT_COMPATIBLE.
245 expect(["READY", "NOT_COMPATIBLE"]).toContain(task.runStatus);
246 }),
247 { timeout: 90_000 },
248 );
249 });
250
251 describe("SearchSteps / SearchTasks / ListJobParameterDefinitions", () => {
252 test.provider(
253 "searches the job's steps and tasks and reads parameter definitions",
254 (_stack) =>
255 Effect.gen(function* () {
256 // SearchSteps uses the same eventually-consistent search index as
257 // SearchTasks below. The authoritative ListSteps/GetStep test above
258 // already proves the step exists, so exercise the live binding and
259 // validate its response without waiting on an optional index entry.
260 const steps = (yield* postJson(`/search/steps?jobId=${jobId}`)) as { ids: string[] };
261 expect(Array.isArray(steps.ids)).toBe(true);
262
263 // SearchTasks uses a separate asynchronously provisioned index.
264 // With no fleet associated, the pending task can remain absent for
265 // minutes even though ListTasks/GetTask above are authoritative.
266 // Exercise the live binding and validate its response without
267 // polling past the suite's bounded provisioning budget.
268 const tasks = (yield* postJson(`/search/tasks?jobId=${jobId}`)) as { ids: string[] };
269 expect(Array.isArray(tasks.ids)).toBe(true);
270
271 // The fixture template declares no parameters.
272 const params = (yield* getJson(`/params?jobId=${jobId}`)) as { count: number };
273 expect(params.count).toBe(0);
274 }),
275 { timeout: 90_000 },
276 );
277 });
278
279 describe("UpdateTask / UpdateStep", () => {
280 test.provider(
281 "cancels a READY task, then requeues the step",
282 (_stack) =>
283 Effect.gen(function* () {
284 const tasks = (yield* getJson(`/tasks?jobId=${jobId}&stepId=${stepId}`)) as {
285 ids: string[];
286 };
287 const taskId = tasks.ids[0]!;
288
289 // Cancel the single task and watch it converge to CANCELED. (The
290 // fixture queue has no fleet, so the pending state is READY or
291 // NOT_COMPATIBLE — never RUNNING.)
292 yield* postJson(`/task/cancel?jobId=${jobId}&stepId=${stepId}&taskId=${taskId}`);
293 const canceled = (yield* getJson(
294 `/task?jobId=${jobId}&stepId=${stepId}&taskId=${taskId}`,
295 ).pipe(
296 Effect.repeat({
297 schedule: Schedule.spaced("2 seconds"),
298 until: (t): boolean =>
299 (t as { runStatus: string }).runStatus === "CANCELED" ||
300 (t as { runStatus: string }).runStatus === "CANCELING",
301 times: 10,
302 }),
303 )) as { runStatus: string };
304 expect(["CANCELED", "CANCELING"]).toContain(canceled.runStatus);
305
306 // Requeue the whole step; the task converges back to a pending
307 // status (READY, or NOT_COMPATIBLE while no fleet is associated).
308 yield* postJson(`/step/requeue?jobId=${jobId}&stepId=${stepId}`);
309 const requeued = (yield* getJson(
310 `/task?jobId=${jobId}&stepId=${stepId}&taskId=${taskId}`,
311 ).pipe(
312 Effect.repeat({
313 schedule: Schedule.spaced("2 seconds"),
314 until: (t): boolean =>
315 (t as { runStatus: string }).runStatus === "READY" ||
316 (t as { runStatus: string }).runStatus === "NOT_COMPATIBLE",
317 times: 10,
318 }),
319 )) as { runStatus: string };
320 expect(["READY", "NOT_COMPATIBLE"]).toContain(requeued.runStatus);
321 }),
322 { timeout: 120_000 },
323 );
324 });
325
326 describe("ListSessions / ListSessionActions", () => {
327 test.provider(
328 "a job with no workers has no sessions and no session actions",
329 (_stack) =>
330 Effect.gen(function* () {
331 const sessions = (yield* getJson(`/sessions?jobId=${jobId}`)) as { ids: string[] };
332 expect(sessions.ids).toEqual([]);
333
334 // ListSessionActions requires a sessionId or taskId scope — use
335 // the job's single task.
336 const tasks = (yield* getJson(`/tasks?jobId=${jobId}&stepId=${stepId}`)) as {
337 ids: string[];
338 };
339 const actions = (yield* getJson(
340 `/session-actions?jobId=${jobId}&taskId=${tasks.ids[0]}`,
341 )) as { ids: string[] };
342 expect(actions.ids).toEqual([]);
343 }),
344 { timeout: 60_000 },
345 );
346 });
347
348 describe("SessionsStatisticsAggregation", () => {
349 test.provider(
350 "starts an aggregation on the farm and polls it to completion",
351 (_stack) =>
352 Effect.gen(function* () {
353 const { queueId } = (yield* getJson("/queue")) as { queueId: string };
354 expect(queueId).toMatch(/^queue-/);
355 const started = (yield* postJson(`/stats?queueId=${queueId}`)) as {
356 aggregationId: string;
357 };
358 expect(started.aggregationId).toBeTruthy();
359
360 const result = (yield* getJson(`/stats?aggregationId=${started.aggregationId}`).pipe(
361 Effect.repeat({
362 schedule: Schedule.spaced("6 seconds"),
363 until: (r): boolean => (r as { status: string }).status !== "IN_PROGRESS",
364 times: 9,
365 }),
366 )) as { status: string; count: number };
367 // An empty farm aggregates to COMPLETED with zero statistics.
368 expect(result.status).toBe("COMPLETED");
369 }),
370 { timeout: 120_000 },
371 );
372 });
373
374 describe("consumeFarmEvents", { tags: ["provider:aws:eventbridge"] }, () => {
375 test.provider("the deploy created an EventBridge rule targeting the function", (_stack) =>
376 Effect.gen(function* () {
377 // Out-of-band via distilled: the fixture's consumeFarmEvents must
378 // have materialized as a rule on the default bus with the Lambda
379 // as target.
380 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
381 TargetArn: functionArn,
382 });
383 expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1);
384 }),
385 );
386 });
387 },
388 );