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

service.test.ts

248 lines8.6 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, expect, it } from "@effect/vitest";
3import { HostProcessEnvironment } 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/unstable/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 `npx t3@0.0.29 service update`.",
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("npx t3@0.0.32-nightly.1 service update");
68 expect(output).not.toContain("t3@latest");
69});
70​
71it("suggests the newer CLI version when the installed service needs an update", () => {
72 const output = formatServiceStatus(
73 { ...status, current: false, installedVersion: "0.0.28" },
74 "0.0.29",
75 );
76 expect(output).toContain("npx t3@0.0.29 service update");
77 expect(output).not.toContain("npx t3@0.0.28 service update");
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 gives an exact-version repair command", () => {
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, "npx t3@0.0.32-nightly.1 service update");
95 assert.notInclude(output, "npx t3@latest service update");
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 service = BootService.BootService.of({
103 status: Effect.succeed(serviceStatus),
104 install: (options) =>
105 Effect.sync(() => {
106 installOptions.push(options);
107 return {
108 program: ["/test/t3/runtime/versions/1.0.0/t3", "__service-launcher"],
109 baseDir: "/test/t3",
110 unitPath: serviceStatus.unitPath,
111 logPath: serviceStatus.logPath,
112 };
113 }),
114 uninstall: Effect.succeed(false),
115 });
116 return { service, installOptions };
118​
119it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands", (it) => {
120 it.effect.each(["install", "update"] as const)(
121 "%s refuses a downgrade before changing the service",
122 (command) =>
123 Effect.gen(function* () {
124 const fs = yield* FileSystem.FileSystem;
125 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
126 const { service, installOptions } = makeTestService(newerServiceStatus);
127 vi.spyOn(BootService, "layer").mockReturnValue(
128 Layer.succeed(BootService.BootService, service),
129 );
130​
131 const error = yield* Command.runWith(serviceCommand, { version: packageJson.version })([
132 command,
133 "--base-dir",
134 baseDir,
135 ]).pipe(
136 Effect.provideService(HostProcessEnvironment, {}),
137 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
138 Effect.flip,
139 );
140​
141 expect(error).toMatchObject({
142 _tag: "BootServiceDowngradeRefusedError",
143 installedVersion: "999.0.0",
144 targetVersion: packageJson.version,
145 });
146 expect(installOptions).toEqual([]);
147 }),
148 );
149​
150 it.effect.each(["install", "update"] as const)("%s allows an explicit downgrade", (command) =>
151 Effect.gen(function* () {
152 const fs = yield* FileSystem.FileSystem;
153 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
154 const { service, installOptions } = makeTestService(newerServiceStatus);
155 vi.spyOn(BootService, "layer").mockReturnValue(
156 Layer.succeed(BootService.BootService, service),
157 );
158​
159 yield* Command.runWith(serviceCommand, { version: packageJson.version })([
160 command,
161 "--base-dir",
162 baseDir,
163 "--allow-downgrade",
164 ]).pipe(
165 Effect.provideService(HostProcessEnvironment, {}),
166 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
167 );
168​
169 expect(installOptions).toEqual([{ allowDowngrade: true }]);
170 }),
171 );
172});
173​
174it.effect.each([
175 { name: "a new service", state: { ...status, installed: false, current: false } },
176 { name: "an older service", state: { ...status, current: false, installedVersion: "0.0.0" } },
177 {
178 name: "the same version",
179 state: { ...status, current: false, installedVersion: packageJson.version },
180 },
181 {
182 name: "an incomplete install of the same version",
183 state: {
184 ...status,
185 current: false,
186 installedVersion: packageJson.version,
187 problems: ["linger-disabled"] as const,
188 },
189 },
190 { name: "an unknown version", state: { ...status, current: false } },
191])("installs or repairs $name without an override", ({ state }) =>
192 Effect.gen(function* () {
193 const { service, installOptions } = makeTestService(state);
194​
195 const result = yield* reconcileService().pipe(
196 Effect.provideService(BootService.BootService, service),
197 );
198​
199 expect(result.changed).toBe(true);
200 expect(installOptions).toEqual([undefined]);
201 }),
202);
203​
204it.effect("leaves a newer service unchanged during onboarding without prompting", () =>
205 Effect.gen(function* () {
206 const { service, installOptions } = makeTestService(newerServiceStatus);
207 const terminal = Terminal.make({
208 columns: Effect.succeed(80),
209 rows: Effect.succeed(24),
210 readInput: Effect.die("Onboarding must not prompt to replace a newer service."),
211 readLine: Effect.die("Onboarding must not prompt to replace a newer service."),
212 display: () => Effect.die("Onboarding must not prompt to replace a newer service."),
213 });
214​
215 const ready = yield* offerServiceDuringOnboarding.pipe(
216 Effect.provideService(BootService.BootService, service),
217 Effect.provideService(Terminal.Terminal, terminal),
218 Effect.provide(NodeServices.layer),
219 );
220​
221 expect(ready).toBe(false);
222 expect(installOptions).toEqual([]);
223 }),
224);
225​
226it.effect("keeps onboarding successful when a newer version appears before install", () =>
227 Effect.gen(function* () {
228 const ready = yield* recoverServiceOnboardingOffer(
229 Effect.fail(
230 new BootService.BootServiceDowngradeRefusedError({
231 installedVersion: "999.0.0",
232 targetVersion: packageJson.version,
233 }),
234 ),
235 );
236​
237 expect(ready).toBe(false);
238 }),
239);
240​
241it.effect("keeps the manual-server fallback when background prerequisites fail", () =>
242 Effect.gen(function* () {
243 const ready = yield* recoverServiceOnboardingOffer(
244 Effect.fail(new BootService.BootServicePrerequisiteError({ problem: "linger-disabled" })),
245 );
246 expect(ready).toBe(false);
247 }),
248);