The best way to control Claude Code, Codex and more from any computer or phone

main
ts 159 lines 6.14 KB
Raw
1 import * as Docker from "@alchemy.run/docker";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import { adopt, OwnedBySomeoneElse } from "alchemy/AdoptPolicy";
5 import { inMemoryState } from "alchemy/State";
6 import * as Cause from "effect/Cause";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: Docker.providers(), state: inMemoryState() });
11
12 // Docker.Swarm mutates engine-level state (swarm membership), so these tests
13 // never touch the host engine. Each test boots a disposable docker:dind
14 // engine, points a Docker.Context at it, and provisions the swarm there —
15 // the exact remote-VPS flow, on a throwaway daemon.
16 const startDind = Effect.fn(function* (name: string, port: number) {
17 const docker = yield* Docker.Docker;
18 const host = `tcp://127.0.0.1:${port}`;
19
20 yield* Effect.addFinalizer(() => docker.run(["rm", "-f", name]).pipe(Effect.ignore));
21 yield* docker.run(["rm", "-f", name]).pipe(Effect.ignore);
22 yield* docker.run([
23 "run",
24 "-d",
25 "--rm",
26 "--privileged",
27 "--name",
28 name,
29 "-e",
30 "DOCKER_TLS_CERTDIR=",
31 "-p",
32 `127.0.0.1:${port}:2375`,
33 "docker:dind",
34 ]);
35
36 // The inner daemon takes a few seconds to boot before it serves the API.
37 yield* docker
38 .run(["-H", host, "info", "--format", "{{.ServerVersion}}"])
39 .pipe(Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 30 }));
40
41 return { host };
42 });
43
44 describe(
45 "Docker.Swarm",
46 { tags: ["provider:docker", "provider:docker:swarm", "local"], concurrent: false },
47 () => {
48 test.provider.skipIf(!!process.env.FAST)(
49 "initializes a swarm on a fresh engine and deploys a service into it",
50 (stack) =>
51 Effect.gen(function* () {
52 const docker = yield* Docker.Docker;
53 const { host } = yield* startDind("alchemy-test-dind-swarm", 23750);
54
55 const program = Effect.gen(function* () {
56 const context = yield* Docker.Context("dind-context", {
57 name: "alchemy-test-dind-swarm-ctx",
58 docker: `host=${host}`,
59 });
60 const swarm = yield* Docker.Swarm("swarm", { context, advertiseAddr: "127.0.0.1" });
61 const service = yield* Docker.Service("web", {
62 context: swarm,
63 image: "nginx:alpine",
64 replicas: 1,
65 });
66 return { swarm, service };
67 });
68
69 const { swarm, service } = yield* stack.deploy(program);
70
71 expect(swarm.id.length).toBeGreaterThan(0);
72 expect(swarm.nodeId.length).toBeGreaterThan(0);
73 expect(swarm.context).toBe("alchemy-test-dind-swarm-ctx");
74 expect(swarm.managers).toBe(1);
75 expect(swarm.nodes).toBe(1);
76 // The service inherited the swarm's context and landed on the dind
77 // engine, ordered after the swarm init.
78 expect(service.context).toBe("alchemy-test-dind-swarm-ctx");
79 expect(service.replicas).toBe(1);
80
81 // Idempotent: a second deploy converges on the same cluster.
82 const again = yield* stack.deploy(program);
83 expect(again.swarm.id).toBe(swarm.id);
84 expect(again.service.id).toBe(service.id);
85
86 yield* stack.destroy();
87
88 // Destroy dissolved the swarm (service first, then leave) and
89 // removed the context.
90 const state = yield* docker.run([
91 "-H",
92 host,
93 "info",
94 "--format",
95 "{{.Swarm.LocalNodeState}}",
96 ]);
97 expect(state.stdout).toBe("inactive");
98 const contextGone = yield* docker.context.inspect("alchemy-test-dind-swarm-ctx").pipe(
99 Effect.map(() => false),
100 Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(true)),
101 );
102 expect(contextGone).toBe(true);
103 }),
104 { tags: ["provider:docker:context", "provider:docker:service"], timeout: 300_000 },
105 );
106
107 test.provider.skipIf(!!process.env.FAST)(
108 "refuses an already-initialized engine unless explicitly adopted",
109 (stack) =>
110 Effect.gen(function* () {
111 const docker = yield* Docker.Docker;
112 const { host } = yield* startDind("alchemy-test-dind-adopt", 23751);
113
114 // The engine is already a swarm manager — initialized out of band,
115 // reachable through an out-of-band context. The Swarm resource's
116 // props must be fully resolved (a plain context name, not a
117 // same-plan Context resource) for the engine's adoption probe to
118 // run at plan time.
119 yield* docker.run(["-H", host, "swarm", "init", "--advertise-addr", "127.0.0.1"]);
120 const contextName = "alchemy-test-dind-adopt-ctx";
121 yield* Effect.addFinalizer(() =>
122 docker.context.remove(contextName, true).pipe(Effect.ignore),
123 );
124 yield* docker.context.remove(contextName, true).pipe(Effect.ignore);
125 yield* docker.context.create({ name: contextName, docker: `host=${host}` });
126
127 const makeSwarm = (adopted: boolean) =>
128 Effect.gen(function* () {
129 const swarm = Docker.Swarm("existing-swarm", { context: contextName });
130 return yield* adopted ? swarm.pipe(adopt(true)) : swarm;
131 });
132
133 const error = yield* stack.deploy(makeSwarm(false)).pipe(
134 Effect.as(undefined),
135 Effect.catchCause((cause) => Effect.succeed(findOwnedError(cause))),
136 );
137 expect(error).toBeInstanceOf(OwnedBySomeoneElse);
138
139 const adopted = yield* stack.deploy(makeSwarm(true));
140 expect(adopted.id.length).toBeGreaterThan(0);
141 expect(adopted.nodeId.length).toBeGreaterThan(0);
142
143 yield* stack.destroy();
144 }),
145 { timeout: 300_000 },
146 );
147 },
148 );
149
150 const findOwnedError = (cause: Cause.Cause<unknown>): OwnedBySomeoneElse | undefined =>
151 cause.reasons
152 .map((reason) =>
153 Cause.isFailReason(reason)
154 ? reason.error
155 : Cause.isDieReason(reason)
156 ? reason.defect
157 : undefined,
158 )
159 .find((value): value is OwnedBySomeoneElse => value instanceof OwnedBySomeoneElse);