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