@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 565 lines 22.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 SQS from "@distilled.cloud/aws/sqs";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Schedule from "effect/Schedule";
11 import SQSTestFunctionLive, { SQSTestFunction } from "./handler";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "SQSBindings");
16
17 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
18 // over 60s on a fresh deploy under parallel-suite load. Budget ~150s of
19 // readiness polling so we don't fail the whole suite on a slow init.
20 const readinessPolicy = Schedule.max([Schedule.fixed("4 seconds"), Schedule.recurs(10)]);
21
22 let baseUrl: string;
23 let queueUrl: string;
24 let dlqUrl: string;
25 let moveSourceUrl: string;
26
27 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
28 readonly status: number;
29 readonly body: string;
30 readonly message: string;
31 }> {}
32
33 // The shared Lambda fixture occasionally answers a transient 5xx under
34 // full-suite parallel load — a cold re-init or an eventually-consistent SQS
35 // call that the handler's `Effect.orDie` surfaces as a 500. Retry only 5xx;
36 // a genuine 4xx/assertion failure is returned immediately.
37 const send = (request: HttpClientRequest.HttpClientRequest) =>
38 HttpClient.execute(request).pipe(
39 Effect.timeout("5 seconds"),
40 Effect.mapError(
41 (error) =>
42 new TransientUpstream({
43 status: 0,
44 body: String(error),
45 message: `Function URL request failed: ${String(error)}`,
46 }),
47 ),
48 Effect.flatMap((response) =>
49 response.status >= 500
50 ? response.text.pipe(
51 Effect.flatMap((body) =>
52 Effect.fail(
53 new TransientUpstream({
54 status: response.status,
55 body,
56 message: `Function returned ${response.status}: ${body}`,
57 }),
58 ),
59 ),
60 )
61 : Effect.succeed(response),
62 ),
63 Effect.retry({
64 while: (e) => e._tag === "TransientUpstream",
65 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
66 }),
67 );
68
69 const post = (route: string, body: unknown) =>
70 send(HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}${route}`), body)).pipe(
71 Effect.flatMap((r) => r.json),
72 );
73
74 interface ReceivedMessage {
75 messageId: string;
76 body: string;
77 receiptHandle: string;
78 }
79
80 class MessageNotReceived extends Data.TaggedError("MessageNotReceived") {}
81
82 class MessageNotVisible extends Data.TaggedError("MessageNotVisible") {}
83
84 class QueueStillExists extends Data.TaggedError("QueueStillExists") {}
85
86 /** Poll (bounded) until GetQueueAttributes reports the queue gone. */
87 const assertQueueDeleted = (url: string) =>
88 SQS.getQueueAttributes({ QueueUrl: url, AttributeNames: ["All"] }).pipe(
89 Effect.flatMap(() => Effect.fail(new QueueStillExists())),
90 Effect.retry({
91 // SQS DeleteQueue propagation is documented at ~60s; poll on a fixed
92 // cadence with a bounded budget.
93 while: (e) => e._tag === "QueueStillExists",
94 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(45)]),
95 }),
96 Effect.catchTag("QueueDoesNotExist", () => Effect.void),
97 );
98
99 // Poll the fixture's /receive route (the ReceiveMessage binding) until every
100 // body in `bodies` has been observed; returns a map body -> received message.
101 // Bounded: ~20 polls, each an SQS long-poll of 2s.
102 const receiveViaBindingUntil = (bodies: string[], options?: { visibilityTimeout?: number }) =>
103 Effect.gen(function* () {
104 const found = new Map<string, ReceivedMessage>();
105 yield* Effect.gen(function* () {
106 const response = (yield* post("/receive", {
107 maxNumberOfMessages: 10,
108 waitTimeSeconds: 2,
109 ...(options?.visibilityTimeout !== undefined
110 ? { visibilityTimeout: options.visibilityTimeout }
111 : {}),
112 })) as unknown as { messages: ReceivedMessage[] };
113 for (const message of response.messages) {
114 if (bodies.includes(message.body)) {
115 found.set(message.body, message);
116 }
117 }
118 if (found.size < bodies.length) {
119 return yield* Effect.fail(new MessageNotReceived());
120 }
121 }).pipe(
122 Effect.retry({
123 while: (e) => e._tag === "MessageNotReceived",
124 schedule: Schedule.max([Schedule.fixed("1 second"), Schedule.recurs(20)]),
125 }),
126 );
127 return found;
128 });
129
130 // Out-of-band verification: poll the queue via distilled until every body in
131 // `bodies` has been received, deleting each matching message as it arrives.
132 const receiveAndDeleteViaDistilled = (bodies: string[], sourceUrl = queueUrl) =>
133 Effect.gen(function* () {
134 const found = new Set<string>();
135 yield* Effect.gen(function* () {
136 const result = yield* SQS.receiveMessage({
137 QueueUrl: sourceUrl,
138 MaxNumberOfMessages: 10,
139 WaitTimeSeconds: 2,
140 });
141 for (const message of result.Messages ?? []) {
142 if (message.Body && message.ReceiptHandle && bodies.includes(message.Body)) {
143 found.add(message.Body);
144 yield* SQS.deleteMessage({ QueueUrl: sourceUrl, ReceiptHandle: message.ReceiptHandle });
145 }
146 }
147 if (found.size < bodies.length) {
148 return yield* Effect.fail(new MessageNotReceived());
149 }
150 }).pipe(
151 Effect.retry({
152 while: (e) => e._tag === "MessageNotReceived",
153 schedule: Schedule.max([Schedule.fixed("1 second"), Schedule.recurs(20)]),
154 }),
155 );
156 });
157
158 // Message-move tasks only operate on messages that actually entered a DLQ
159 // through its source queue's redrive policy. Drive the dedicated source's
160 // receive count, then use the clean DLQ's non-consuming approximate count as
161 // readiness. Receiving from the DLQ, even with VisibilityTimeout=0, races the
162 // asynchronous redrive scanner. The retry and final settle are bounded.
163 const seedDlqViaRedrive = (body: string) =>
164 Effect.gen(function* () {
165 yield* SQS.sendMessage({ QueueUrl: moveSourceUrl, MessageBody: body });
166
167 // Actively drive the receive count until SQS reports a visible DLQ
168 // message. VisibilityTimeout=0 increments the source receive count
169 // without hiding it between attempts.
170 yield* Effect.gen(function* () {
171 yield* SQS.receiveMessage({
172 QueueUrl: moveSourceUrl,
173 MaxNumberOfMessages: 10,
174 WaitTimeSeconds: 1,
175 VisibilityTimeout: 0,
176 });
177
178 const dlqAttributes = yield* SQS.getQueueAttributes({
179 QueueUrl: dlqUrl,
180 AttributeNames: ["ApproximateNumberOfMessages"],
181 });
182 if (Number(dlqAttributes.Attributes?.ApproximateNumberOfMessages ?? "0") < 1) {
183 return yield* Effect.fail(new MessageNotVisible());
184 }
185 }).pipe(
186 Effect.retry({
187 while: (error) => error._tag === "MessageNotVisible",
188 schedule: Schedule.fixed("1 second"),
189 times: 15,
190 }),
191 );
192
193 // Let the redrive metadata observed through the approximate count settle
194 // before starting the asynchronous message-move scanner.
195 yield* Effect.sleep("2 seconds");
196 });
197
198 // All five tests share ONE queue; a concurrent receive in test A steals (and
199 // hides, for the visibility timeout) messages test B is polling for. The
200 // global vitest `sequence: { concurrent: true }` must not apply here.
201 describe.sequential(
202 "SQS Bindings",
203 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:sqs", "live"] },
204 () => {
205 beforeAll(
206 Effect.gen(function* () {
207 yield* Effect.logInfo("SQS test setup: destroying previous resources");
208 yield* sharedStack.destroy();
209
210 yield* Effect.logInfo("SQS test setup: deploying fixture");
211 const { functionUrl } = yield* sharedStack.deploy(
212 Effect.gen(function* () {
213 return yield* SQSTestFunction;
214 }).pipe(Effect.provide(SQSTestFunctionLive)),
215 );
216
217 expect(functionUrl).toBeTruthy();
218 baseUrl = functionUrl!.replace(/\/+$/, "");
219
220 yield* Effect.logInfo(`SQS test setup: probing readiness at ${baseUrl}/ready`);
221
222 // A freshly-deployed function can briefly serve a 200 before its
223 // captured env vars (the queue URLs) finish propagating; keep polling
224 // until both queue URLs are populated.
225 const ready = yield* HttpClient.get(`${baseUrl}/ready`).pipe(
226 Effect.timeout("4 seconds"),
227 Effect.mapError(() => new Error("Function URL readiness request timed out")),
228 Effect.flatMap((response) =>
229 response.status === 200
230 ? (response.json as Effect.Effect<{
231 queueUrl?: string;
232 dlqUrl?: string;
233 moveSourceUrl?: string;
234 }>)
235 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
236 ),
237 Effect.flatMap((body) =>
238 typeof body?.queueUrl === "string" &&
239 body.queueUrl.length > 0 &&
240 typeof body?.dlqUrl === "string" &&
241 body.dlqUrl.length > 0 &&
242 typeof body?.moveSourceUrl === "string" &&
243 body.moveSourceUrl.length > 0
244 ? Effect.succeed(body as { queueUrl: string; dlqUrl: string; moveSourceUrl: string })
245 : Effect.fail(new Error("Function returned empty queue URLs")),
246 ),
247 Effect.tapError((error) =>
248 Effect.logWarning(`SQS test setup: fixture not ready yet (${String(error)})`),
249 ),
250 Effect.retry({ schedule: readinessPolicy }),
251 );
252 queueUrl = ready.queueUrl;
253 dlqUrl = ready.dlqUrl;
254 moveSourceUrl = ready.moveSourceUrl;
255
256 yield* Effect.logInfo(`SQS test setup: fixture ready (queueUrl: ${queueUrl})`);
257 }),
258 { timeout: 240_000 },
259 );
260
261 afterAll(
262 Effect.gen(function* () {
263 yield* sharedStack.destroy();
264 // Out-of-band: the shared queue and DLQ are actually gone. The hook
265 // context has no provider environment (unlike `test.provider` bodies),
266 // so provide it explicitly for the distilled SQS calls.
267 yield* Core.withProviders(
268 Effect.gen(function* () {
269 if (queueUrl) yield* assertQueueDeleted(queueUrl);
270 if (dlqUrl) yield* assertQueueDeleted(dlqUrl);
271 if (moveSourceUrl) yield* assertQueueDeleted(moveSourceUrl);
272 }),
273 testOptions,
274 sharedStack.name,
275 );
276 }),
277 { timeout: 240_000 },
278 );
279
280 describe("SendMessage", () => {
281 test.provider("sends a message through the bound queue", (_stack) =>
282 Effect.gen(function* () {
283 const messageBody = `send-${crypto.randomUUID()}`;
284
285 const response = (yield* post("/send", { messageBody })) as { messageId?: string };
286 expect(response.messageId).toBeTruthy();
287
288 // Out-of-band: the message is actually in the queue.
289 yield* receiveAndDeleteViaDistilled([messageBody]);
290 }),
291 );
292 });
293
294 describe("SendMessageBatch", () => {
295 test.provider("sends a batch of messages through the bound queue", (_stack) =>
296 Effect.gen(function* () {
297 const bodies = [
298 `batch-${crypto.randomUUID()}`,
299 `batch-${crypto.randomUUID()}`,
300 `batch-${crypto.randomUUID()}`,
301 ];
302
303 const response = (yield* post("/send-batch", {
304 entries: bodies.map((messageBody, index) => ({ id: `entry-${index}`, messageBody })),
305 })) as {
306 successful: { id: string; messageId: string }[];
307 failed: { id: string; code: string }[];
308 };
309
310 expect(response.failed).toHaveLength(0);
311 expect(response.successful).toHaveLength(3);
312 for (const entry of response.successful) {
313 expect(entry.messageId).toBeTruthy();
314 }
315
316 // Out-of-band: all three messages landed in the queue.
317 yield* receiveAndDeleteViaDistilled(bodies);
318 }),
319 );
320 });
321
322 describe("ReceiveMessage", () => {
323 test.provider("receives a message through the bound queue", (_stack) =>
324 Effect.gen(function* () {
325 const messageBody = `receive-${crypto.randomUUID()}`;
326
327 // Out-of-band send via distilled; receive via the binding.
328 yield* SQS.sendMessage({ QueueUrl: queueUrl, MessageBody: messageBody });
329
330 const found = yield* receiveViaBindingUntil([messageBody]);
331 const message = found.get(messageBody)!;
332 expect(message.messageId).toBeTruthy();
333 expect(message.receiptHandle).toBeTruthy();
334
335 // Cleanup: delete with the receipt handle the binding returned.
336 yield* SQS.deleteMessage({ QueueUrl: queueUrl, ReceiptHandle: message.receiptHandle });
337 }),
338 );
339 });
340
341 describe("DeleteMessage", () => {
342 test.provider("deletes a received message through the bound queue", (_stack) =>
343 Effect.gen(function* () {
344 const messageBody = `delete-${crypto.randomUUID()}`;
345
346 yield* SQS.sendMessage({ QueueUrl: queueUrl, MessageBody: messageBody });
347
348 // Receive with a short (5s) visibility timeout: long enough that the
349 // receipt handle is still the freshest when /delete runs, short
350 // enough that an undeleted message reappears within the absence poll.
351 const found = yield* receiveViaBindingUntil([messageBody], { visibilityTimeout: 5 });
352 const message = found.get(messageBody)!;
353
354 const response = (yield* post("/delete", { receiptHandle: message.receiptHandle })) as {
355 success: boolean;
356 };
357 expect(response.success).toBe(true);
358
359 // The deleted message must not reappear after its 1s visibility
360 // timeout lapses. Bounded absence poll (~10s of 1s long-polls).
361 yield* Effect.gen(function* () {
362 const result = yield* SQS.receiveMessage({
363 QueueUrl: queueUrl,
364 MaxNumberOfMessages: 10,
365 WaitTimeSeconds: 1,
366 VisibilityTimeout: 1,
367 });
368 const bodies = (result.Messages ?? []).map((m) => m.Body);
369 expect(bodies).not.toContain(messageBody);
370 }).pipe(
371 Effect.repeat({
372 schedule: Schedule.max([Schedule.fixed("1 second"), Schedule.recurs(5)]),
373 }),
374 );
375 }),
376 );
377 });
378
379 describe("DeleteMessageBatch", () => {
380 test.provider("deletes a batch of received messages through the bound queue", (_stack) =>
381 Effect.gen(function* () {
382 const bodies = [
383 `delete-batch-${crypto.randomUUID()}`,
384 `delete-batch-${crypto.randomUUID()}`,
385 ];
386
387 yield* SQS.sendMessageBatch({
388 QueueUrl: queueUrl,
389 Entries: bodies.map((body, index) => ({ Id: `entry-${index}`, MessageBody: body })),
390 });
391
392 // 15s visibility: long enough that both receipt handles stay valid
393 // while we collect the pair across polls, short enough that a message
394 // consumed by a failed invocation resurfaces within the poll budget.
395 const found = yield* receiveViaBindingUntil(bodies, { visibilityTimeout: 15 });
396
397 const response = (yield* post("/delete-batch", {
398 entries: bodies.map((body, index) => ({
399 id: `entry-${index}`,
400 receiptHandle: found.get(body)!.receiptHandle,
401 })),
402 })) as { successful: string[]; failed: { id: string; code: string }[] };
403
404 expect(response.failed).toHaveLength(0);
405 expect(response.successful.sort()).toEqual(["entry-0", "entry-1"]);
406 }),
407 );
408 });
409
410 describe("ChangeMessageVisibility", () => {
411 test.provider("releases an in-flight message back to the queue", (_stack) =>
412 Effect.gen(function* () {
413 const messageBody = `visibility-${crypto.randomUUID()}`;
414
415 yield* SQS.sendMessage({ QueueUrl: queueUrl, MessageBody: messageBody });
416
417 // Receive with a long visibility timeout so the message stays
418 // hidden unless the binding releases it.
419 const found = yield* receiveViaBindingUntil([messageBody], { visibilityTimeout: 120 });
420 const message = found.get(messageBody)!;
421
422 const response = (yield* post("/change-visibility", {
423 receiptHandle: message.receiptHandle,
424 visibilityTimeout: 0,
425 })) as { success: boolean };
426 expect(response.success).toBe(true);
427
428 // The message must reappear well before the 120s timeout —
429 // observable only if the binding actually reset visibility.
430 yield* receiveAndDeleteViaDistilled([messageBody]);
431 }),
432 );
433 });
434
435 describe("ChangeMessageVisibilityBatch", () => {
436 test.provider("releases a batch of in-flight messages back to the queue", (_stack) =>
437 Effect.gen(function* () {
438 const bodies = [
439 `visibility-batch-${crypto.randomUUID()}`,
440 `visibility-batch-${crypto.randomUUID()}`,
441 ];
442
443 yield* SQS.sendMessageBatch({
444 QueueUrl: queueUrl,
445 Entries: bodies.map((body, index) => ({ Id: `entry-${index}`, MessageBody: body })),
446 });
447
448 const found = yield* receiveViaBindingUntil(bodies, { visibilityTimeout: 120 });
449
450 const response = (yield* post("/change-visibility-batch", {
451 entries: bodies.map((body, index) => ({
452 id: `entry-${index}`,
453 receiptHandle: found.get(body)!.receiptHandle,
454 visibilityTimeout: 0,
455 })),
456 })) as { successful: string[]; failed: { id: string; code: string }[] };
457 expect(response.failed).toHaveLength(0);
458 expect(response.successful.sort()).toEqual(["entry-0", "entry-1"]);
459
460 // Both messages reappear well before the 120s timeout.
461 yield* receiveAndDeleteViaDistilled(bodies);
462 }),
463 );
464 });
465
466 describe("GetQueueAttributes", () => {
467 test.provider("reads live queue attributes", (_stack) =>
468 Effect.gen(function* () {
469 const response = (yield* post("/attributes", {
470 attributeNames: ["QueueArn", "ApproximateNumberOfMessages"],
471 })) as { attributes: { QueueArn?: string; ApproximateNumberOfMessages?: string } };
472
473 const queueName = queueUrl.split("/").pop()!;
474 expect(response.attributes.QueueArn).toMatch(/^arn:aws:sqs:/);
475 expect(response.attributes.QueueArn!.endsWith(queueName)).toBe(true);
476 expect(Number(response.attributes.ApproximateNumberOfMessages)).toBeGreaterThanOrEqual(0);
477 }),
478 );
479 });
480
481 describe("MessageMoveTasks", () => {
482 test.provider("lists DLQ sources and redrives a message via a move task", (_stack) =>
483 Effect.gen(function* () {
484 // ListDeadLetterSourceQueues on the DLQ sees the main queue, whose
485 // redrivePolicy targets it.
486 const sources = (yield* post("/dlq-sources", {})) as { queueUrls: string[] };
487 expect(sources.queueUrls).toContain(queueUrl);
488 expect(sources.queueUrls).toContain(moveSourceUrl);
489
490 // Redrive through the dedicated source so SQS records the metadata
491 // required for the message-move task.
492 const messageBody = `redrive-${crypto.randomUUID()}`;
493 yield* seedDlqViaRedrive(messageBody);
494
495 const started = (yield* post("/move/start", {})) as { taskHandle?: string };
496 expect(started.taskHandle).toBeTruthy();
497
498 // Out-of-band data-plane proof: the message arrives back in its
499 // source queue. This is authoritative even while List's status and
500 // approximate counters lag.
501 yield* receiveAndDeleteViaDistilled([messageBody], moveSourceUrl);
502
503 // The exact task is visible via the ListMessageMoveTasks binding.
504 const listed = (yield* post("/move/list", {})) as {
505 tasks: { taskHandle?: string; status?: string }[];
506 };
507 expect(Array.isArray(listed.tasks)).toBe(true);
508 const task = listed.tasks.find(
509 (candidate) => candidate.taskHandle === started.taskHandle,
510 );
511 // SQS may omit a one-message task as soon as it completes. When it
512 // is retained, assert the decoded status; the destination receive
513 // above is the authoritative completion proof.
514 if (task !== undefined) {
515 expect(["RUNNING", "COMPLETING", "COMPLETED"]).toContain(task.status);
516 }
517
518 // CancelMessageMoveTask rejects an already-completed task. The
519 // handler converts that exact typed terminal race to canceled:false.
520 const canceled = (yield* post("/move/cancel", { taskHandle: started.taskHandle })) as {
521 canceled: boolean;
522 };
523 expect(typeof canceled.canceled).toBe("boolean");
524 }),
525 );
526 });
527
528 // PurgeQueue runs LAST: it wipes the shared queue, and SQS allows only one
529 // purge per queue per 60 seconds.
530 describe("PurgeQueue", () => {
531 test.provider("purges every message in the queue", (_stack) =>
532 Effect.gen(function* () {
533 const bodies = [`purge-${crypto.randomUUID()}`, `purge-${crypto.randomUUID()}`];
534 yield* SQS.sendMessageBatch({
535 QueueUrl: queueUrl,
536 Entries: bodies.map((body, index) => ({ Id: `entry-${index}`, MessageBody: body })),
537 });
538
539 const response = (yield* post("/purge", {})) as { success: boolean };
540 expect(response.success).toBe(true);
541
542 // The purge deletes messages within up to 60s; poll until neither
543 // body is receivable anymore (bounded).
544 yield* Effect.gen(function* () {
545 const result = yield* SQS.receiveMessage({
546 QueueUrl: queueUrl,
547 MaxNumberOfMessages: 10,
548 WaitTimeSeconds: 1,
549 VisibilityTimeout: 1,
550 });
551 const received = (result.Messages ?? []).map((m) => m.Body);
552 if (bodies.some((body) => received.includes(body))) {
553 return yield* Effect.fail(new MessageNotReceived());
554 }
555 }).pipe(
556 Effect.retry({
557 while: (e) => e._tag === "MessageNotReceived",
558 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(15)]),
559 }),
560 );
561 }),
562 );
563 });
564 },
565 );