Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

bootService.test.ts

151 lines5.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import {
4 HostProcessArguments,
5 HostProcessExecutablePath,
6 HostProcessPlatform,
7} from "@t3tools/shared/hostProcess";
8import * as ConfigProvider from "effect/ConfigProvider";
9import * as Effect from "effect/Effect";
10import * as FileSystem from "effect/FileSystem";
11import * as Layer from "effect/Layer";
12import * as Path from "effect/Path";
13import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
14​
15import * as ProcessRunner from "../processRunner.ts";
16import * as BootService from "./bootService.ts";
17import { pinnedRuntimePaths } from "./pinnedRuntime.ts";
18import { parseServiceState } from "./serviceProtocol.ts";
19​
20it("keeps systemd pinned to the stable launcher rather than a versioned server", () => {
21 const unit = BootService.renderBootServiceUnit({
22 nodePath: "/usr/bin/node",
23 launcherPath: "/home/theo/.t3/runtime/service-launcher.mjs",
24 baseDir: "/home/theo/.t3",
25 logPath: "/home/theo/.t3/userdata/logs/boot-service.log",
26 unitPath: "/home/theo/.config/systemd/user/t3code.service",
27 });
28​
29 expect(unit).toContain("ExecStart=/usr/bin/node /home/theo/.t3/runtime/service-launcher.mjs");
30 expect(unit).toContain("KillMode=control-group");
31 expect(unit).not.toContain("versions/1.2.3");
32});
33​
34const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
35 platform: NodeJS.Platform = "linux",
36 usePinnedLauncher = false,
37) {
38 const fs = yield* FileSystem.FileSystem;
39 const path = yield* Path.Path;
40 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-boot-service-test-" });
41 const baseDir = path.join(home, ".t3");
42 const sourceLauncher = path.join(home, "service-launcher.mjs");
43 const statePath = path.join(baseDir, "runtime", "service-state.json");
44 yield* fs.writeFileString(sourceLauncher, "export {};\n");
45 const runtime = pinnedRuntimePaths(path, baseDir, "1.2.3");
46 yield* fs.makeDirectory(path.dirname(runtime.entryPath), { recursive: true });
47 yield* fs.writeFileString(runtime.entryPath, "export {};\n");
48 yield* fs.writeFileString(
49 path.join(path.dirname(runtime.entryPath), "service-launcher.mjs"),
50 "export const source = 'pinned runtime';\n",
51 );
52 yield* fs.writeFileString(runtime.sentinelPath, "1.2.3\n");
53​
54 const commands: string[] = [];
55 const control: { failCommand: string | undefined } = { failCommand: undefined };
56 const runner = ProcessRunner.ProcessRunner.of({
57 run: (input) =>
58 Effect.sync(() => {
59 const command = `${input.command} ${input.args.join(" ")}`;
60 commands.push(command);
61 return {
62 stdout: input.args[1] === "--version" ? "t3 v1.2.3\n" : "",
63 stderr: "",
64 code: ChildProcessSpawner.ExitCode(command === control.failCommand ? 1 : 0),
65 timedOut: false,
66 stdoutTruncated: false,
67 stderrTruncated: false,
68 };
69 }),
70 });
71 const service = yield* BootService.make({
72 baseDir,
73 logsDir: path.join(baseDir, "userdata", "logs"),
74 cliVersion: "1.2.3",
75 host: {
76 execPath: "/usr/bin/node",
77 ...(usePinnedLauncher ? {} : { launcherSourcePath: sourceLauncher }),
78 },
79 }).pipe(
80 Effect.provideService(ProcessRunner.ProcessRunner, runner),
81 Effect.provide(
82 Layer.mergeAll(
83 Layer.succeed(HostProcessPlatform, platform),
84 Layer.succeed(HostProcessExecutablePath, "/usr/bin/node"),
85 Layer.succeed(HostProcessArguments, ["/usr/bin/node", path.join(home, "bin.mjs")]),
86 ConfigProvider.layer(ConfigProvider.fromEnv({ env: { HOME: home } })),
87 ),
88 ),
89 );
90 return { service, fs, statePath, commands, control };
91});
92​
93it.layer(NodeServices.layer)("boot service install", (it) => {
94 it.effect("installs, reports current state, and uninstalls", () =>
95 Effect.gen(function* () {
96 const { service, fs, statePath, commands } = yield* makeHarness();
97 const plan = yield* service.install;
98​
99 expect(parseServiceState(yield* fs.readFileString(statePath))).toEqual({
100 protocol: 1,
101 activeVersion: "1.2.3",
102 });
103 expect(yield* fs.readFileString(plan.launcherPath)).toBe("export {};\n");
104 expect((yield* service.status).current).toBe(true);
105 yield* fs.writeFileString(
106 statePath,
107 '{"protocol":1,"activeVersion":"1.2.3","update":{"id":"u","fromVersion":"1.2.3","targetVersion":"1.2.4","status":"pending"}}',
108 );
109 expect((yield* service.status).current).toBe(false);
110 expect(yield* service.uninstall).toBe(true);
111 expect((yield* service.status).installed).toBe(false);
112 expect(commands.some((command) => command.startsWith("npm "))).toBe(false);
113 }),
114 );
115​
116 it.effect("copies the launcher from the prepared pinned runtime", () =>
117 Effect.gen(function* () {
118 const { service, fs } = yield* makeHarness("linux", true);
119 const plan = yield* service.install;
120​
121 expect(yield* fs.readFileString(plan.launcherPath)).toBe(
122 "export const source = 'pinned runtime';\n",
123 );
124 }),
125 );
126​
127 it.effect("restarts an installed service when repair fails", () =>
128 Effect.gen(function* () {
129 const { service, commands, control } = yield* makeHarness();
130 yield* service.install;
131 commands.length = 0;
132 control.failCommand = "systemctl --user daemon-reload";
133​
134 const error = yield* service.install.pipe(Effect.flip);
135 expect(error._tag).toBe("BootServiceCommandError");
136 expect(commands.filter((command) => command.startsWith("systemctl "))).toEqual([
137 "systemctl --user stop t3code.service",
138 "systemctl --user daemon-reload",
139 "systemctl --user restart t3code.service",
140 ]);
141 }),
142 );
143​
144 it.effect("fails closed off Linux", () =>
145 Effect.gen(function* () {
146 const { service } = yield* makeHarness("darwin");
147 expect((yield* service.status).supported).toBe(false);
148 expect((yield* service.install.pipe(Effect.flip))._tag).toBe("BootServiceUnsupportedError");
149 }),
150 );
151});