@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 86 lines 2.97 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Lambda from "@distilled.cloud/aws/lambda";
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 Schedule from "effect/Schedule";
9 import InitIOProbe from "./init-io-probe.ts";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 interface InitIOBody {
14 nonce: string;
15 traceLength: number;
16 hasUag: boolean;
17 initFetches: number;
18 }
19
20 class FunctionNotReady extends Data.TaggedError("FunctionNotReady")<{
21 status: number;
22 }> {}
23
24 /**
25 * Pins the init-phase I/O contract on Lambda: one-shot async I/O in a
26 * Layer build (a real `fetch` at cold start, run under the instance scope)
27 * completes once per sandbox and the cached plain value serves every
28 * invocation.
29 */
30 test.provider(
31 "async I/O in a layer build runs once per sandbox and serves every invocation",
32 (stack) =>
33 Effect.gen(function* () {
34 yield* stack.destroy();
35
36 const fn = yield* stack.deploy(
37 Effect.gen(function* () {
38 return yield* InitIOProbe;
39 }),
40 );
41
42 const client = yield* HttpClient.HttpClient;
43 const get = Effect.gen(function* () {
44 const res = yield* client.get(fn.functionUrl!).pipe(
45 Effect.flatMap((res) =>
46 res.status === 200
47 ? Effect.succeed(res)
48 : Effect.fail(new FunctionNotReady({ status: res.status })),
49 ),
50 Effect.retry({
51 while: (e): e is FunctionNotReady => e instanceof FunctionNotReady,
52 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
53 }),
54 );
55 return (yield* res.json) as unknown as InitIOBody;
56 }).pipe(Effect.orDie);
57
58 const first = yield* get;
59 const second = yield* get;
60 const concurrent = yield* Effect.all(
61 Array.from({ length: 4 }, () => get),
62 { concurrency: "unbounded" },
63 );
64
65 for (const body of [first, second, ...concurrent]) {
66 expect(body.hasUag).toBe(true);
67 expect(body.traceLength).toBeGreaterThan(0);
68 expect(body.nonce).toMatch(/^[0-9a-f-]{36}$/);
69 // Exactly one init fetch on whichever sandbox served this
70 // invocation, no matter how many invocations it has handled.
71 expect(body.initFetches).toBe(1);
72 }
73
74 yield* stack.destroy();
75
76 // Out-of-band proof the destroy removed the function from the cloud.
77 yield* Lambda.getFunction({ FunctionName: fn.functionName }).pipe(
78 Effect.flatMap(() => Effect.fail(new Error(`Function ${fn.functionName} still exists`))),
79 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
80 Effect.retry({
81 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
82 }),
83 );
84 }),
85 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 600_000 },
86 );