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