@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 97 lines 4 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Effect from "effect/Effect";
4 import * as HttpClient from "effect/http/HttpClient";
5 import * as Redacted from "effect/Redacted";
6 import * as Schedule from "effect/Schedule";
7 import TestInstance, { keyPair } from "./fixtures/instance.ts";
8 import { assertInstanceTerminated } from "./Gone.ts";
9 import * as Test from "./VpcTest.ts";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // Full end-to-end: bundle the hosted program, launch a real EC2 instance into a
14 // public subnet, and prove over HTTP (against `instance.url`) that (a) the
15 // `{ fetch }` handler is served by the instance's Bun HTTP server and (b) the
16 // `ServerHost.run` background loop is executing on the instance (`/ticks`
17 // keeps climbing). `url` is also what `alchemy dev` prints — the emulator's
18 // host-routed DNS, not a fake public IP.
19 //
20 // Heavy (instance boot + bun install + S3 sync + systemd), so skipped under
21 // `FAST=1`.
22 test.provider.skipIf(!!process.env.FAST)(
23 "deploys a real EC2 instance that serves HTTP and runs a background loop",
24 (stack) =>
25 Effect.gen(function* () {
26 yield* stack.destroy();
27
28 const { instanceId, url, privateKey } = yield* stack.deploy(
29 Effect.gen(function* () {
30 const instance = yield* TestInstance;
31 // Resolve the same key-pair resource the instance uses and return its
32 // private key from the stack (resolved to a `Redacted` value).
33 const key = yield* keyPair;
34 return {
35 instanceId: instance.instanceId,
36 url: instance.url,
37 privateKey: key.privateKey,
38 };
39 }),
40 );
41
42 expect(url).toBeTruthy();
43 // Unredact only to prove the returned value is usable key material. Do
44 // not log the ephemeral private key into the test artifact.
45 const pem = privateKey ? Redacted.value(privateKey) : undefined;
46 expect(pem).toContain("PRIVATE KEY");
47 const base = url!;
48
49 // Poll until the instance boots, installs bun, syncs the bundle from S3,
50 // and the systemd unit serves 200 on :3000. Connection errors before the
51 // server binds are normalised to "not ready" so the poll keeps going
52 // (a bare `Effect.retry` does not retry the transport-level failure).
53 const served = yield* HttpClient.get(`${base}/health`).pipe(
54 Effect.map((res) => res.status === 200),
55 Effect.catch(() => Effect.succeed(false)),
56 Effect.repeat({
57 schedule: Schedule.spaced("8 seconds"),
58 until: (ok) => ok,
59 times: 75,
60 }),
61 );
62 expect(served).toBe(true);
63
64 const getJson = (path: string) =>
65 HttpClient.get(`${base}${path}`).pipe(
66 Effect.flatMap((res) =>
67 res.status === 200
68 ? res.json
69 : Effect.fail(new Error(`${path} temporarily returned ${res.status}`)),
70 ),
71 // cloud-init/systemd can briefly restart the hosted process just
72 // after the first successful health probe, especially while a full
73 // network lane is saturating EC2. Bound the post-readiness probes as
74 // well as the initial health poll.
75 Effect.retry({ schedule: Schedule.spaced("1 second"), times: 10 }),
76 );
77
78 const body = yield* getJson("/health");
79 expect(body).toEqual({ ok: true });
80
81 // Prove the ServerHost.run background loop is executing on the instance:
82 // the tick counter climbs between two reads.
83 const readTicks = getJson("/ticks").pipe(
84 Effect.map((value) => (value as { ticks: number }).ticks),
85 );
86 const first = yield* readTicks;
87 yield* Effect.sleep("3 seconds");
88 const second = yield* readTicks;
89 expect(second).toBeGreaterThan(first);
90
91 yield* stack.destroy();
92
93 // Zero-orphan proof: the (billed) instance reached a terminal state.
94 yield* assertInstanceTerminated(instanceId);
95 }),
96 { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 1_200_000 },
97 );