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

service.test.ts

277 lines9.5 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, expect, it } from "@effect/vitest";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import * as NetService from "@t3tools/shared/Net";
5import * as ConfigProvider from "effect/ConfigProvider";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Terminal from "effect/Terminal";
10import { Command } from "effect/cli";
11import { afterEach, vi } from "vite-plus/test";
12​
13import packageJson from "../../package.json" with { type: "json" };
14import * as BootService from "../cloud/bootService.ts";
15import {
16 formatServiceStatus,
17 offerServiceDuringOnboarding,
18 reconcileService,
19 recoverServiceOnboardingOffer,
20 serviceCommand,
21} from "./service.ts";
22​
23afterEach(() => vi.restoreAllMocks());
24​
25const status = {
26 supported: true,
27 installed: true,
28 current: true,
29 unitPath: "/home/me/.config/systemd/user/t3code.service",
30 logPath: "/home/me/.t3/userdata/logs/boot-service.log",
31} as const;
32​
33it("reports the installed service version and host paths", () => {
34 assert.equal(
35 formatServiceStatus(status, "0.0.29"),
36 [
37 "T3 Code service",
38 " Status: installed · t3@0.0.29",
39 " Unit: /home/me/.config/systemd/user/t3code.service",
40 " Logs: /home/me/.t3/userdata/logs/boot-service.log",
41 ].join("\n"),
42 );
43});
44​
45it("gives a direct repair command for a stale service", () => {
46 assert.include(
47 formatServiceStatus({ ...status, current: false }, "0.0.29"),
48 "Next: Run `t3 service install` to repair it.",
49 );
50});
51​
52it("explains an incomplete nightly installation and keeps repair on its installed version", () => {
53 const output = formatServiceStatus(
54 {
55 ...status,
56 current: false,
57 installedVersion: "0.0.32-nightly.1",
58 problems: ["linger-disabled", "service-stopped"],
59 },
60 "0.0.32-nightly.1",
61 );
62​
63 expect(output).toContain("[linger-disabled]");
64 expect(output).toContain("last login session ends");
65 expect(output).toContain('sudo loginctl enable-linger "$(id -un)"');
66 expect(output).toContain("[service-stopped]");
67 expect(output).toContain("Run `t3 service install` to repair it.");
68 expect(output).not.toContain("npx");
69});
70​
71it("points an older service at a repair, never at npx", () => {
72 const output = formatServiceStatus(
73 { ...status, current: false, installedVersion: "0.0.28" },
74 "0.0.29",
75 );
76 expect(output).toContain("Run `t3 service install` to repair it.");
77 expect(output).not.toContain("npx");
78});
79​
80it("explains where the service is supported", () => {
81 assert.include(
82 formatServiceStatus({ ...status, supported: false, installed: false }, "0.0.29"),
83 "Supported on: Linux with systemd, macOS with launchd",
84 );
85});
86​
87it("reports a newer installed service and tells the CLI to catch up to it", () => {
88 const output = formatServiceStatus(
89 { ...status, current: false, installedVersion: "0.0.32-nightly.1" },
90 "0.0.31",
91 );
92​
93 assert.include(output, "t3@0.0.32-nightly.1 (newer than this t3@0.0.31 CLI)");
94 assert.include(output, "Run `t3 update 0.0.32-nightly.1` to match it");
95 assert.notInclude(output, "npx");
96});
97​
98const newerServiceStatus = { ...status, current: false, installedVersion: "999.0.0" };
99​
100function makeTestService(serviceStatus: BootService.BootServiceStatus) {
101 const installOptions: Array<Parameters<BootService.BootService["Service"]["install"]>[0]> = [];
102 const restarts: Array<true> = [];
103 const service = BootService.BootService.of({
104 status: Effect.succeed(serviceStatus),
105 withStopped: (operation) => operation,
106 restart: Effect.sync(() => {
107 restarts.push(true);
108 return serviceStatus.installed;
109 }),
110 install: (options) =>
111 Effect.sync(() => {
112 installOptions.push(options);
113 return {
114 program: ["/test/t3/runtime/versions/1.0.0/t3", "__service-launcher"],
115 baseDir: "/test/t3",
116 unitPath: serviceStatus.unitPath,
117 logPath: serviceStatus.logPath,
118 };
119 }),
120 uninstall: Effect.succeed(false),
121 });
122 return { service, installOptions, restarts };
124​
125it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands", (it) => {
126 it.effect("restart restarts the installed service", () =>
127 Effect.gen(function* () {
128 const fs = yield* FileSystem.FileSystem;
129 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
130 const { service, installOptions, restarts } = makeTestService(status);
131 vi.spyOn(BootService, "layer").mockReturnValue(
132 Layer.succeed(BootService.BootService, service),
133 );
134​
135 yield* Command.runWith(serviceCommand, { version: packageJson.version })([
136 "restart",
137 "--base-dir",
138 baseDir,
139 ]).pipe(
140 Effect.provideService(HostProcess.Environment, {}),
141 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
142 );
143​
144 expect(restarts).toEqual([true]);
145 expect(installOptions).toEqual([]);
146 }),
147 );
148​
149 it.effect.each(["install", "update"] as const)(
150 "%s refuses a downgrade before changing the service",
151 (command) =>
152 Effect.gen(function* () {
153 const fs = yield* FileSystem.FileSystem;
154 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
155 const { service, installOptions } = makeTestService(newerServiceStatus);
156 vi.spyOn(BootService, "layer").mockReturnValue(
157 Layer.succeed(BootService.BootService, service),
158 );
159​
160 const error = yield* Command.runWith(serviceCommand, { version: packageJson.version })([
161 command,
162 "--base-dir",
163 baseDir,
164 ]).pipe(
165 Effect.provideService(HostProcess.Environment, {}),
166 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
167 Effect.flip,
168 );
169​
170 expect(error).toMatchObject({
171 _tag: "BootServiceDowngradeRefusedError",
172 installedVersion: "999.0.0",
173 targetVersion: packageJson.version,
174 });
175 expect(installOptions).toEqual([]);
176 }),
177 );
178​
179 it.effect.each(["install", "update"] as const)("%s allows an explicit downgrade", (command) =>
180 Effect.gen(function* () {
181 const fs = yield* FileSystem.FileSystem;
182 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
183 const { service, installOptions } = makeTestService(newerServiceStatus);
184 vi.spyOn(BootService, "layer").mockReturnValue(
185 Layer.succeed(BootService.BootService, service),
186 );
187​
188 yield* Command.runWith(serviceCommand, { version: packageJson.version })([
189 command,
190 "--base-dir",
191 baseDir,
192 "--allow-downgrade",
193 ]).pipe(
194 Effect.provideService(HostProcess.Environment, {}),
195 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
196 );
197​
198 expect(installOptions).toEqual([{ allowDowngrade: true }]);
199 }),
200 );
201});
202​
203it.effect.each([
204 { name: "a new service", state: { ...status, installed: false, current: false } },
205 { name: "an older service", state: { ...status, current: false, installedVersion: "0.0.0" } },
206 {
207 name: "the same version",
208 state: { ...status, current: false, installedVersion: packageJson.version },
209 },
210 {
211 name: "an incomplete install of the same version",
212 state: {
213 ...status,
214 current: false,
215 installedVersion: packageJson.version,
216 problems: ["linger-disabled"] as const,
217 },
218 },
219 { name: "an unknown version", state: { ...status, current: false } },
220])("installs or repairs $name without an override", ({ state }) =>
221 Effect.gen(function* () {
222 const { service, installOptions } = makeTestService(state);
223​
224 const result = yield* reconcileService().pipe(
225 Effect.provideService(BootService.BootService, service),
226 );
227​
228 expect(result.changed).toBe(true);
229 expect(installOptions).toEqual([undefined]);
230 }),
231);
232​
233it.effect("leaves a newer service unchanged during onboarding without prompting", () =>
234 Effect.gen(function* () {
235 const { service, installOptions } = makeTestService(newerServiceStatus);
236 const terminal = Terminal.make({
237 columns: Effect.succeed(80),
238 rows: Effect.succeed(24),
239 readInput: Effect.die("Onboarding must not prompt to replace a newer service."),
240 readLine: Effect.die("Onboarding must not prompt to replace a newer service."),
241 display: () => Effect.die("Onboarding must not prompt to replace a newer service."),
242 });
243​
244 const ready = yield* offerServiceDuringOnboarding.pipe(
245 Effect.provideService(BootService.BootService, service),
246 Effect.provideService(Terminal.Terminal, terminal),
247 Effect.provide(NodeServices.layer),
248 );
249​
250 expect(ready).toBe(false);
251 expect(installOptions).toEqual([]);
252 }),
253);
254​
255it.effect("keeps onboarding successful when a newer version appears before install", () =>
256 Effect.gen(function* () {
257 const ready = yield* recoverServiceOnboardingOffer(
258 Effect.fail(
259 new BootService.BootServiceDowngradeRefusedError({
260 installedVersion: "999.0.0",
261 targetVersion: packageJson.version,
262 }),
263 ),
264 );
265​
266 expect(ready).toBe(false);
267 }),
268);
269​
270it.effect("keeps the manual-server fallback when background prerequisites fail", () =>
271 Effect.gen(function* () {
272 const ready = yield* recoverServiceOnboardingOffer(
273 Effect.fail(new BootService.BootServicePrerequisiteError({ problem: "linger-disabled" })),
274 );
275 expect(ready).toBe(false);
276 }),
277);