The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Hosted `AWS.EC2.Instance` under `alchemy dev`: the dualized EC2 providers |
| 3 | * deploy the whole fleet (VPC network, security group, instance, and the |
| 4 | * hosted program's S3 bundle) into the floci emulator, where the instance |
| 5 | * runs as a REAL docker container that boots the Alchemy userData — syncs |
| 6 | * the bundle from emulated S3, installs the runtime, and serves the hosted |
| 7 | * `{ fetch }` program. |
| 8 | * |
| 9 | * Proof structure: |
| 10 | * - attrs carry the emulator's `.localhost.floci.io` public address — the |
| 11 | * live cloud can never mint it; |
| 12 | * - e2e over HTTP: floci publishes the security-group app port behind a |
| 13 | * host-routing mux, so `http://<host>:3000/marker` serves THIS build's |
| 14 | * marker from inside the emulated instance; |
| 15 | * - bindings through compute: a floci-hosted Lambda bound to the instance |
| 16 | * (`DescribeInstance` / `DescribeInstanceStatus`) answers over its |
| 17 | * function URL — emulated Lambda → EC2 control plane → emulated |
| 18 | * instance, no real cloud anywhere; |
| 19 | * - destroy terminates the instance (verified out-of-band via distilled). |
| 20 | * |
| 21 | * Requires Docker (floci runs as a container); skipped when unavailable. |
| 22 | */ |
| 23 | import * as AWS from "@alchemy.run/aws"; |
| 24 | import * as Endpoint from "@alchemy.run/aws/Endpoint"; |
| 25 | import * as Region from "@alchemy.run/aws/Region"; |
| 26 | import { expect } from "@alchemy.run/test"; |
| 27 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 28 | import { Credentials } from "@distilled.cloud/aws/Credentials"; |
| 29 | import * as ec2 from "@distilled.cloud/aws/ec2"; |
| 30 | import type { RegionName } from "@distilled.cloud/aws/Region"; |
| 31 | import * as Effect from "effect/Effect"; |
| 32 | import * as HttpClient from "effect/http/HttpClient"; |
| 33 | import * as Layer from "effect/Layer"; |
| 34 | import * as Redacted from "effect/Redacted"; |
| 35 | import * as Schedule from "effect/Schedule"; |
| 36 | import DevProbeFunctionLive, { Ec2DevProbeFunction } from "./fixtures/dev-instance-fn.ts"; |
| 37 | import DevInstance, { MARKER } from "./fixtures/dev-instance.ts"; |
| 38 | |
| 39 | const { test } = Test.make({ providers: AWS.providers(), dev: true }); |
| 40 | |
| 41 | /** Floci-scoped context for the raw distilled calls the test makes itself. */ |
| 42 | const flociContext = Layer.mergeAll( |
| 43 | Endpoint.of("http://localhost:4566"), |
| 44 | Region.of("us-east-1"), |
| 45 | Layer.succeed( |
| 46 | Credentials, |
| 47 | Effect.succeed({ |
| 48 | accessKeyId: Redacted.make("test"), |
| 49 | secretAccessKey: Redacted.make("test"), |
| 50 | sessionToken: undefined, |
| 51 | region: "us-east-1" as RegionName, |
| 52 | }), |
| 53 | ), |
| 54 | ); |
| 55 | |
| 56 | // TODO: floci does not settle security group rules yet |
| 57 | // (SecurityGroupRulesNotSettled); needs a fix in the emulator. |
| 58 | test.provider.todo( |
| 59 | "dev runs a hosted EC2 instance as a container serving HTTP, with Lambda EC2 bindings against it", |
| 60 | (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | yield* stack.destroy(); |
| 63 | |
| 64 | const outputs = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | const instance = yield* DevInstance; |
| 67 | const fn = yield* Ec2DevProbeFunction; |
| 68 | return { |
| 69 | instanceId: instance.instanceId, |
| 70 | url: instance.url, |
| 71 | functionUrl: fn.functionUrl, |
| 72 | }; |
| 73 | }).pipe(Effect.provide(DevProbeFunctionLive)), |
| 74 | ); |
| 75 | |
| 76 | expect(outputs.instanceId).toMatch(/^i-/); |
| 77 | expect(outputs.functionUrl).toBeDefined(); |
| 78 | // Emulator identity: `url` carries floci's host-routed localhost |
| 79 | // address and the hosted port. |
| 80 | expect(outputs.url).toContain(".localhost.floci.io:3000"); |
| 81 | const base = outputs.url!; |
| 82 | |
| 83 | // The instance container boots the Alchemy userData: bundle sync from |
| 84 | // emulated S3, runtime install, then the Bun HTTP server on :3000. |
| 85 | const served = yield* HttpClient.get(`${base}/health`).pipe( |
| 86 | Effect.map((res) => res.status === 200), |
| 87 | Effect.catch(() => Effect.succeed(false)), |
| 88 | Effect.repeat({ |
| 89 | schedule: Schedule.spaced("3 seconds"), |
| 90 | until: (ok): boolean => ok, |
| 91 | times: 60, |
| 92 | }), |
| 93 | ); |
| 94 | expect(served).toBe(true); |
| 95 | |
| 96 | // The program served is THIS build. |
| 97 | const marker = yield* HttpClient.get(`${base}/marker`).pipe( |
| 98 | Effect.flatMap((res) => res.text), |
| 99 | Effect.retry({ schedule: Schedule.spaced("1 second"), times: 10 }), |
| 100 | ); |
| 101 | expect(marker).toBe(MARKER); |
| 102 | |
| 103 | // Bindings through compute: the floci-hosted Lambda observes the |
| 104 | // emulated instance via its DescribeInstance binding. |
| 105 | const describeUrl = `${outputs.functionUrl}describe`; |
| 106 | const described = yield* HttpClient.get(describeUrl).pipe( |
| 107 | Effect.flatMap((res) => |
| 108 | res.status === 200 |
| 109 | ? res.json |
| 110 | : Effect.fail(new Error(`/describe returned ${res.status}`)), |
| 111 | ), |
| 112 | Effect.map((json) => json as { ok: boolean; state?: string; instanceId?: string }), |
| 113 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 20 }), |
| 114 | ); |
| 115 | expect(described.ok).toBe(true); |
| 116 | expect(described.instanceId).toBe(outputs.instanceId); |
| 117 | expect(described.state).toBe("running"); |
| 118 | |
| 119 | // Reboot resilience: the provider's in-place update path ends in |
| 120 | // RebootInstances, and floci's guest services must come back after |
| 121 | // the container restart (the systemd shim's processes die with the |
| 122 | // container; the emulator re-runs the boot sequence). Without this |
| 123 | // the box goes dark after every hosted-program update. |
| 124 | yield* ec2 |
| 125 | .rebootInstances({ InstanceIds: [outputs.instanceId] }) |
| 126 | .pipe(Effect.provide(flociContext)); |
| 127 | const backAfterReboot = yield* HttpClient.get(`${base}/marker`).pipe( |
| 128 | Effect.flatMap((res) => res.text), |
| 129 | Effect.map((text) => text.includes(MARKER)), |
| 130 | Effect.catch(() => Effect.succeed(false)), |
| 131 | Effect.repeat({ |
| 132 | schedule: Schedule.spaced("2 seconds"), |
| 133 | until: (ok): boolean => ok, |
| 134 | times: 60, |
| 135 | }), |
| 136 | ); |
| 137 | expect(backAfterReboot).toBe(true); |
| 138 | |
| 139 | const status = yield* HttpClient.get(`${outputs.functionUrl}status`).pipe( |
| 140 | Effect.flatMap((res) => |
| 141 | res.status === 200 ? res.json : Effect.fail(new Error(`/status returned ${res.status}`)), |
| 142 | ), |
| 143 | Effect.map((json) => json as { ok: boolean; count?: number }), |
| 144 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 10 }), |
| 145 | ); |
| 146 | expect(status.ok).toBe(true); |
| 147 | expect(status.count).toBeGreaterThan(0); |
| 148 | |
| 149 | // Destroy: the emulated instance reaches a terminal state (or is |
| 150 | // fully forgotten by the emulator). |
| 151 | yield* stack.destroy(); |
| 152 | const gone = yield* ec2.describeInstances({ InstanceIds: [outputs.instanceId] }).pipe( |
| 153 | Effect.map((res) => { |
| 154 | const state = res.Reservations?.[0]?.Instances?.[0]?.State?.Name; |
| 155 | return state === undefined || state === "terminated"; |
| 156 | }), |
| 157 | Effect.catchTag("InvalidInstanceID.NotFound", () => Effect.succeed(true)), |
| 158 | Effect.provide(flociContext), |
| 159 | Effect.repeat({ |
| 160 | schedule: Schedule.spaced("2 seconds"), |
| 161 | until: (isGone): boolean => isGone, |
| 162 | times: 30, |
| 163 | }), |
| 164 | ); |
| 165 | expect(gone).toBe(true); |
| 166 | }), |
| 167 | { tags: ["provider:aws", "provider:aws:ec2", "provider:aws:lambda", "local"], timeout: 600_000 }, |
| 168 | ); |