1import * as Docker from "@/Docker";
2import * as Test from "@/Test/Alchemy";
3import { expect } from "alchemy-test";
4import * as Effect from "effect/Effect";
5import * as Schedule from "effect/Schedule";
6import * as HttpClient from "effect/http/HttpClient";
7import TestService, { SERVICE_EXTERNAL_PORT } from "./fixtures/service.ts";
8import { ensureDockerSwarm } from "./Runtime.ts";
9
10const { test } = Test.make({ providers: Docker.providers() });
11
12// Full end-to-end for the effectful `Docker.Service` platform: bundle the
13// fixture's Effect program, build the content-addressed image on the local
14// engine, deploy it as a swarm service, and prove over HTTP that (a) the
15// `{ fetch }` handler is served and (b) the `ServerHost.run` background loop
16// is actually executing inside the swarm task (`/ticks` keeps climbing).
17test.provider(
18 "effectful service serves fetch and runs background loops",
19 (stack) =>
20 Effect.gen(function* () {
21 yield* ensureDockerSwarm;
22 yield* stack.destroy();
23
24 const service = yield* stack.deploy(
25 Effect.gen(function* () {
26 return yield* TestService;
27 }),
28 );
29
30 expect(service.id.length).toBeGreaterThan(0);
31 expect(service.replicas).toBe(1);
32 // The bundled image is content-addressed: <physical-name>:<hash>.
33 expect(service.code?.hash).toBeTruthy();
34 expect(service.image).toContain(service.code!.hash);
35 expect(service.ports).toEqual([
36 {
37 external: SERVICE_EXTERNAL_PORT,
38 internal: 3000,
39 protocol: "tcp",
40 mode: "ingress",
41 },
42 ]);
43
44 // The swarm ingress publishes the fixed external port on the local
45 // node. Retry through task startup (image already local, so this is
46 // just container boot + routing-mesh programming).
47 const base = `http://localhost:${SERVICE_EXTERNAL_PORT}`;
48 const health = yield* HttpClient.get(`${base}/health`).pipe(
49 Effect.flatMap((res) =>
50 res.status === 200
51 ? Effect.succeed(res)
52 : Effect.fail(new Error(`/health returned ${res.status}`)),
53 ),
54 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 30 }),
55 );
56 expect(yield* health.json).toEqual({ ok: true });
57
58 // Prove the ServerHost.run background loop is executing in-container:
59 // the tick counter climbs between two reads.
60 const readTicks = HttpClient.get(`${base}/ticks`).pipe(
61 Effect.flatMap((res) => res.json),
62 Effect.map((body) => (body as { ticks: number }).ticks),
63 );
64 const first = yield* readTicks;
65 const second = yield* readTicks.pipe(
66 Effect.repeat({
67 schedule: Schedule.spaced("1 second"),
68 until: (ticks) => ticks > first,
69 times: 10,
70 }),
71 );
72 expect(second).toBeGreaterThan(first);
73
74 // Redeploy with unchanged code: the diff is a noop (no replacement,
75 // same service id, same content hash).
76 const redeployed = yield* stack.deploy(
77 Effect.gen(function* () {
78 return yield* TestService;
79 }),
80 );
81 expect(redeployed.id).toBe(service.id);
82 expect(redeployed.code?.hash).toBe(service.code?.hash);
83
84 const imageRef = service.image;
85 yield* stack.destroy();
86
87 // The swarm service is gone after destroy.
88 const docker = yield* Docker.Docker;
89 const gone = yield* docker.service.inspect(service.id).pipe(
90 Effect.map(() => false),
91 Effect.catchReason("PlatformError", "NotFound", () =>
92 Effect.succeed(true),
93 ),
94 );
95 expect(gone).toBe(true);
96
97 // The content-addressed image built for the bundled program was
98 // cleaned up with the service.
99 const imageGone = yield* docker.image.inspect(imageRef).pipe(
100 Effect.map(() => false),
101 Effect.catchReason("PlatformError", "NotFound", () =>
102 Effect.succeed(true),
103 ),
104 );
105 expect(imageGone).toBe(true);
106 }),
107 {
108 tags: ["provider:docker", "provider:docker:service", "local"],
109 timeout: 420_000,
110 },
111);