The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Docker from "@alchemy.run/docker"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as Path from "effect/Path"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import TestServiceV2Scaled from "./fixtures/service-v2-scaled.ts"; |
| 11 | import TestServiceV2 from "./fixtures/service-v2.ts"; |
| 12 | import TestService, { SERVICE_EXTERNAL_PORT } from "./fixtures/service.ts"; |
| 13 | import { ensureDockerSwarm } from "./Runtime.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: Docker.providers() }); |
| 16 | |
| 17 | // Full end-to-end for the effectful `Docker.Service` platform: bundle the |
| 18 | // fixture's Effect program, build the content-addressed image on the local |
| 19 | // engine, deploy it as a swarm service, and prove over HTTP that (a) the |
| 20 | // `{ fetch }` handler is served and (b) the `ServerHost.run` background loop |
| 21 | // is actually executing inside the swarm task (`/ticks` keeps climbing). |
| 22 | test.provider( |
| 23 | "effectful service serves fetch and runs background loops", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | yield* ensureDockerSwarm; |
| 27 | yield* stack.destroy(); |
| 28 | |
| 29 | const service = yield* stack.deploy( |
| 30 | Effect.gen(function* () { |
| 31 | return yield* TestService; |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | expect(service.id.length).toBeGreaterThan(0); |
| 36 | expect(service.replicas).toBe(1); |
| 37 | // The bundled image is content-addressed: <physical-name>:<hash>. |
| 38 | expect(service.code?.hash).toBeTruthy(); |
| 39 | expect(service.image).toContain(service.code!.hash); |
| 40 | |
| 41 | // The engine hands the resource's identity to the bundler, which writes |
| 42 | // the bundle to the resource's own directory under `.alchemy` (and not |
| 43 | // to a stray `dist/` in the package that contains `main`). |
| 44 | const fs = yield* FileSystem.FileSystem; |
| 45 | const path = yield* Path.Path; |
| 46 | const { dotAlchemy } = yield* AlchemyContext; |
| 47 | const bundled = yield* fs.readFileString( |
| 48 | path.join( |
| 49 | dotAlchemy, |
| 50 | "bundles", |
| 51 | `${stack.name}-${stack.stage}-DockerPlatformService`, |
| 52 | "index.mjs", |
| 53 | ), |
| 54 | ); |
| 55 | expect(bundled).toContain("/ticks"); |
| 56 | expect(service.ports).toEqual([ |
| 57 | { |
| 58 | external: SERVICE_EXTERNAL_PORT, |
| 59 | internal: 3000, |
| 60 | protocol: "tcp", |
| 61 | mode: "ingress", |
| 62 | }, |
| 63 | ]); |
| 64 | |
| 65 | // The swarm ingress publishes the fixed external port on the local |
| 66 | // node. Retry through task startup (image already local, so this is |
| 67 | // just container boot + routing-mesh programming). |
| 68 | const base = `http://localhost:${SERVICE_EXTERNAL_PORT}`; |
| 69 | const health = yield* HttpClient.get(`${base}/health`).pipe( |
| 70 | Effect.flatMap((res) => |
| 71 | res.status === 200 |
| 72 | ? Effect.succeed(res) |
| 73 | : Effect.fail(new Error(`/health returned ${res.status}`)), |
| 74 | ), |
| 75 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 30 }), |
| 76 | ); |
| 77 | expect(yield* health.json).toEqual({ ok: true }); |
| 78 | |
| 79 | // Prove the ServerHost.run background loop is executing in-container: |
| 80 | // the tick counter climbs between two reads. |
| 81 | const readTicks = HttpClient.get(`${base}/ticks`).pipe( |
| 82 | Effect.flatMap((res) => res.json), |
| 83 | Effect.map((body) => (body as { ticks: number }).ticks), |
| 84 | ); |
| 85 | const first = yield* readTicks; |
| 86 | const second = yield* readTicks.pipe( |
| 87 | Effect.repeat({ |
| 88 | schedule: Schedule.spaced("1 second"), |
| 89 | until: (ticks) => ticks > first, |
| 90 | times: 10, |
| 91 | }), |
| 92 | ); |
| 93 | expect(second).toBeGreaterThan(first); |
| 94 | |
| 95 | // Redeploy with unchanged code: the diff is a noop (no replacement, |
| 96 | // same service id, same content hash). |
| 97 | const redeployed = yield* stack.deploy( |
| 98 | Effect.gen(function* () { |
| 99 | return yield* TestService; |
| 100 | }), |
| 101 | ); |
| 102 | expect(redeployed.id).toBe(service.id); |
| 103 | expect(redeployed.code?.hash).toBe(service.code?.hash); |
| 104 | |
| 105 | // A code change rolls the same service onto the new content-addressed |
| 106 | // image and drops the image built for the previous bundle. |
| 107 | const updatePlan = yield* stack.plan( |
| 108 | Effect.gen(function* () { |
| 109 | return yield* TestServiceV2; |
| 110 | }), |
| 111 | ); |
| 112 | expect(updatePlan.resources.DockerPlatformService).toMatchObject({ action: "update" }); |
| 113 | const updated = yield* stack.deploy( |
| 114 | Effect.gen(function* () { |
| 115 | return yield* TestServiceV2; |
| 116 | }), |
| 117 | ); |
| 118 | expect(updated.id).toBe(service.id); |
| 119 | expect(updated.code?.hash).not.toBe(service.code?.hash); |
| 120 | expect(updated.image).toContain(updated.code!.hash); |
| 121 | |
| 122 | const v2 = yield* HttpClient.get(`${base}/health`).pipe( |
| 123 | Effect.flatMap((res) => res.json), |
| 124 | Effect.repeat({ |
| 125 | schedule: Schedule.spaced("2 seconds"), |
| 126 | until: (body) => (body as { version?: number }).version === 2, |
| 127 | times: 30, |
| 128 | }), |
| 129 | ); |
| 130 | expect(v2).toEqual({ ok: true, version: 2 }); |
| 131 | |
| 132 | const docker = yield* Docker.Docker; |
| 133 | const previousImageGone = yield* docker.image.inspect(service.image).pipe( |
| 134 | Effect.map(() => false), |
| 135 | Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(true)), |
| 136 | ); |
| 137 | expect(previousImageGone).toBe(true); |
| 138 | |
| 139 | // A non-code update keeps the content hash and must not remove the |
| 140 | // image the service is running. |
| 141 | const scaled = yield* stack.deploy( |
| 142 | Effect.gen(function* () { |
| 143 | return yield* TestServiceV2Scaled; |
| 144 | }), |
| 145 | ); |
| 146 | expect(scaled.id).toBe(service.id); |
| 147 | expect(scaled.replicas).toBe(2); |
| 148 | expect(scaled.code?.hash).toBe(updated.code?.hash); |
| 149 | const runningImagePresent = yield* docker.image.inspect(scaled.image).pipe( |
| 150 | Effect.map(() => true), |
| 151 | Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(false)), |
| 152 | ); |
| 153 | expect(runningImagePresent).toBe(true); |
| 154 | |
| 155 | const imageRef = scaled.image; |
| 156 | yield* stack.destroy(); |
| 157 | |
| 158 | // The swarm service is gone after destroy. |
| 159 | const gone = yield* docker.service.inspect(service.id).pipe( |
| 160 | Effect.map(() => false), |
| 161 | Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(true)), |
| 162 | ); |
| 163 | expect(gone).toBe(true); |
| 164 | |
| 165 | // The content-addressed image built for the bundled program was |
| 166 | // cleaned up with the service. |
| 167 | const imageGone = yield* docker.image.inspect(imageRef).pipe( |
| 168 | Effect.map(() => false), |
| 169 | Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(true)), |
| 170 | ); |
| 171 | expect(imageGone).toBe(true); |
| 172 | }), |
| 173 | { |
| 174 | tags: ["provider:docker", "provider:docker:service", "local"], |
| 175 | timeout: 420_000, |
| 176 | }, |
| 177 | ); |