@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 84 lines 3.94 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 Alchemy from "alchemy";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { materializeIsolatedProject, removeIsolatedProject } from "../../IsolatedProject.ts";
10 import { project } from "./fixtures/microvm/isolated/sandbox.ts";
11 import IsolatedStack from "./fixtures/microvm/isolated/stack.ts";
12
13 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
14 providers: AWS.providers(),
15 state: Alchemy.localState(),
16 });
17
18 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
19
20 // MicroVM image builds run server-side (Firecracker snapshot) and can take
21 // several minutes, so give deploy/destroy plenty of room.
22 const HOOK_TIMEOUT = 1_500_000;
23 const TEST_TIMEOUT = 300_000;
24
25 // Same gate as the AWS-only blocks in MicrovmImage.test.ts: builds are
26 // asynchronous (minutes). No Cloudflare leg, so no
27 // LAMBDA_TEST_MICROVM_CLOUDFLARE gate.
28 const skip = !process.env.LAMBDA_TEST_MICROVM;
29
30 // Live proof that the MicroVM bootstrap boots when the image's `main` lives
31 // in an isolated project (see test/IsolatedProject.ts) — the bundle `cwd`
32 // resolves none of alchemy's dependencies, so the bootstrap's `alchemy/*` /
33 // `effect/*` imports must be bundled by the virtual-entry plugin rather than
34 // found from the project root. With them left external the in-VM server
35 // dies at module load and the orchestrator's RPC/fetch calls never get a
36 // reply (the `/rpc` route answers 500).
37 describe.skipIf(skip)(
38 "isolated-project microvm (main)",
39 { tags: ["provider:aws", "provider:aws:iam", "provider:aws:lambda", "live"] },
40 () => {
41 beforeAll(materializeIsolatedProject(project));
42 const stack = beforeAll(deploy(IsolatedStack), { timeout: HOOK_TIMEOUT });
43 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(IsolatedStack), { timeout: HOOK_TIMEOUT });
44 afterAll(removeIsolatedProject(project));
45
46 test(
47 "in-VM program bundled from an isolated project answers RPC and fetch",
48 Effect.gen(function* () {
49 const { url } = yield* stack;
50 const baseUrl = url.replace(/\/+$/, "");
51 const client = yield* HttpClient.HttpClient;
52
53 // One request per attempt: the handler runs ONE MicroVM, waits for
54 // RUNNING, calls `hello` over RPC and GETs `/echo`, then terminates it.
55 //
56 // Non-200s are retried on a small budget because a fresh deploy leaves
57 // the function's config update `InProgress` for a few seconds, during
58 // which AWS still runs the PREVIOUS configuration — whose environment
59 // lacks the image ARN this binding injects as `imageIdentifier`, so
60 // `runMicrovm` fails to serialize and the route answers 500. (The
61 // provider does not block on that window by design; see the precreate
62 // stub comment in AWS/Lambda/Function.ts.) Retrying is safe here: the
63 // handler terminates the MicroVM it launched via `Effect.ensuring`, on
64 // success or failure, so an attempt cannot leak a running VM.
65 const res = yield* client.post(`${baseUrl}/rpc?message=world`).pipe(
66 Effect.timeout("150 seconds"),
67 Effect.flatMap((r) =>
68 r.status === 200
69 ? Effect.succeed(r)
70 : r.text.pipe(
71 Effect.flatMap((text) => Effect.fail(new Error(`/rpc ${r.status}: ${text}`))),
72 ),
73 ),
74 Effect.retry({ schedule: Schedule.spaced("5 seconds"), times: 6 }),
75 Effect.orDie,
76 );
77 const body = (yield* res.json.pipe(Effect.orDie)) as { reply: string; echo: string };
78 expect(body.reply).toBe("hello, world!");
79 expect(body.echo).toBe("world");
80 }).pipe(logLevel),
81 { timeout: TEST_TIMEOUT },
82 );
83 },
84 );