Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import * as Effect from "effect/Effect";
4import * as Fiber from "effect/Fiber";
5
6import {
7 SERVICE_LAUNCHER_CONTEXT_ENV,
8 SERVICE_LAUNCHER_PROTOCOL,
9 type ServiceLauncherChildMessage,
10 type ServiceLauncherParentMessage,
11} from "./serviceProtocol.ts";
12import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
13
14class FakeLauncherProcess {
15 readonly connected: boolean;
16 readonly env: Record<string, string | undefined>;
17 readonly sent: ServiceLauncherChildMessage[] = [];
18 readonly #listeners = new Map<string, Set<(...args: ReadonlyArray<unknown>) => void>>();
19
20 constructor(context: unknown, connected = true) {
21 this.connected = connected;
22 this.env = { [SERVICE_LAUNCHER_CONTEXT_ENV]: JSON.stringify(context) };
23 }
24
25 send = (message: ServiceLauncherChildMessage, callback?: (error: Error | null) => void) => {
26 this.sent.push(message);
27 callback?.(null);
28 return true;
29 };
30
31 on = (event: "message" | "disconnect", listener: (...args: ReadonlyArray<unknown>) => void) => {
32 const listeners = this.#listeners.get(event) ?? new Set();
33 listeners.add(listener);
34 this.#listeners.set(event, listeners);
35 };
36
37 off = (event: "message" | "disconnect", listener: (...args: ReadonlyArray<unknown>) => void) => {
38 this.#listeners.get(event)?.delete(listener);
39 };
40
41 emit(message: ServiceLauncherParentMessage) {
42 for (const listener of this.#listeners.get("message") ?? []) listener(message);
43 }
44}
45
46const makeClient = (host: FakeLauncherProcess, currentVersion: string) =>
47 ServiceLauncherClient.make({ currentVersion }).pipe(
48 Effect.provideService(ServiceLauncherClient.ServiceLauncherHostProcess, host),
49 Effect.provideService(HostProcess.Environment, host.env),
50 );
51
52it.effect("waits for the launcher to durably commit the trial update ID", () =>
53 Effect.gen(function* () {
54 const pending = {
55 id: "update-1",
56 fromVersion: "1.0.0",
57 targetVersion: "1.1.0",
58 dbPath: "/tmp/state.sqlite",
59 status: "pending" as const,
60 };
61 const host = new FakeLauncherProcess({
62 protocol: SERVICE_LAUNCHER_PROTOCOL,
63 childVersion: "1.1.0",
64 update: pending,
65 });
66 const client = yield* makeClient(host, "1.1.0");
67 const prepared = yield* Effect.forkChild(client.prepareTrial, { startImmediately: true });
68 yield* Effect.yieldNow;
69 expect(host.sent).toEqual([{ type: "prepared", updateId: "update-1" }]);
70
71 const committed = {
72 id: pending.id,
73 fromVersion: pending.fromVersion,
74 targetVersion: pending.targetVersion,
75 status: "committed" as const,
76 };
77 host.emit({ type: "committed", updateId: committed.id });
78 expect(yield* Fiber.join(prepared)).toEqual(committed);
79 }),
80);
81
82it.effect("returns the launcher-generated ID only after update acceptance", () =>
83 Effect.gen(function* () {
84 const host = new FakeLauncherProcess({
85 protocol: SERVICE_LAUNCHER_PROTOCOL,
86 childVersion: "1.0.0",
87 });
88 const client = yield* makeClient(host, "1.0.0");
89 const requested = yield* Effect.forkChild(
90 client.requestUpdate({ targetVersion: "1.1.0", dbPath: "/tmp/state.sqlite" }),
91 { startImmediately: true },
92 );
93 yield* Effect.yieldNow;
94 host.emit({
95 type: "update-accepted",
96 updateId: "launcher-id",
97 });
98 expect(yield* Fiber.join(requested)).toBe("launcher-id");
99 }),
100);
101
102it.effect("preserves a launcher rejection as a distinct error", () =>
103 Effect.gen(function* () {
104 const host = new FakeLauncherProcess({
105 protocol: SERVICE_LAUNCHER_PROTOCOL,
106 childVersion: "1.0.0",
107 });
108 const client = yield* makeClient(host, "1.0.0");
109 const requested = yield* Effect.forkChild(
110 client.requestUpdate({ targetVersion: "1.1.0", dbPath: "/tmp/state.sqlite" }),
111 { startImmediately: true },
112 );
113 yield* Effect.yieldNow;
114 host.emit({ type: "update-rejected", reason: "requires local update" });
115 expect(yield* Fiber.join(requested).pipe(Effect.flip)).toMatchObject({
116 _tag: "ServiceLauncherRejectedError",
117 targetVersion: "1.1.0",
118 reason: "requires local update",
119 });
120 }),
121);
122
123it.effect("rejects contradictory trial context instead of leaving activation closed", () =>
124 Effect.gen(function* () {
125 const host = new FakeLauncherProcess({
126 protocol: SERVICE_LAUNCHER_PROTOCOL,
127 childVersion: "1.1.0",
128 update: {
129 id: "update-1",
130 fromVersion: "1.0.0",
131 targetVersion: "1.2.0",
132 dbPath: "/tmp/state.sqlite",
133 status: "pending",
134 },
135 });
136 const error = yield* makeClient(host, "1.1.0").pipe(Effect.flip);
137 expect(error.message).toBe("The service launcher supplied invalid startup context.");
138 }),
139);
140
141it.effect("ignores launcher context inherited without an IPC channel", () =>
142 Effect.gen(function* () {
143 const host = new FakeLauncherProcess(
144 { protocol: SERVICE_LAUNCHER_PROTOCOL, childVersion: "1.0.0" },
145 false,
146 );
147 const client = yield* makeClient(host, "1.1.0");
148 const error = yield* client
149 .requestUpdate({ targetVersion: "1.2.0", dbPath: "/tmp/state.sqlite" })
150 .pipe(Effect.flip);
151 expect(error).toMatchObject({ _tag: "ServiceLauncherClientError", operation: "unmanaged" });
152 expect(host.sent).toEqual([]);
153 }),
154);