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@latest service update`.",
49 );
50});
51
52it("explains where the service is supported", () => {
53 assert.include(
54 formatServiceStatus({ ...status, supported: false, installed: false }, "0.0.29"),
55 "Supported on: Linux with systemd, macOS with launchd",
56 );
57});
58
59it("reports a newer installed service and gives an exact-version repair command", () => {
60 const output = formatServiceStatus(
61 { ...status, current: false, installedVersion: "0.0.32-nightly.1" },
62 "0.0.31",
63 );
64
65 assert.include(output, "t3@0.0.32-nightly.1 (newer than this t3@0.0.31 CLI)");
66 assert.include(output, "npx t3@0.0.32-nightly.1 service update");
67 assert.notInclude(output, "npx t3@latest service update");
68});
69
70const newerServiceStatus = { ...status, current: false, installedVersion: "999.0.0" };
71
72function makeTestService(serviceStatus: BootService.BootServiceStatus) {
73 const installOptions: Array<Parameters<BootService.BootService["Service"]["install"]>[0]> = [];
74 const service = BootService.BootService.of({
75 status: Effect.succeed(serviceStatus),
76 install: (options) =>
77 Effect.sync(() => {
78 installOptions.push(options);
79 return {
80 nodePath: "/test/node",
81 launcherPath: "/test/service-launcher.mjs",
82 baseDir: "/test/t3",
83 unitPath: serviceStatus.unitPath,
84 logPath: serviceStatus.logPath,
85 };
86 }),
87 uninstall: Effect.succeed(false),
88 });
89 return { service, installOptions };
90}
91
92it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands", (it) => {
93 it.effect.each(["install", "update"] as const)(
94 "%s refuses a downgrade before changing the service",
95 (command) =>
96 Effect.gen(function* () {
97 const fs = yield* FileSystem.FileSystem;
98 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
99 const { service, installOptions } = makeTestService(newerServiceStatus);
100 vi.spyOn(BootService, "layer").mockReturnValue(
101 Layer.succeed(BootService.BootService, service),
102 );
103
104 const error = yield* Command.runWith(serviceCommand, { version: packageJson.version })([
105 command,
106 "--base-dir",
107 baseDir,
108 ]).pipe(
109 Effect.provideService(HostProcessEnvironment, {}),
110 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
111 Effect.flip,
112 );
113
114 expect(error).toMatchObject({
115 _tag: "BootServiceDowngradeRefusedError",
116 installedVersion: "999.0.0",
117 targetVersion: packageJson.version,
118 });
119 expect(installOptions).toEqual([]);
120 }),
121 );
122
123 it.effect.each(["install", "update"] as const)("%s allows an explicit downgrade", (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 yield* Command.runWith(serviceCommand, { version: packageJson.version })([
133 command,
134 "--base-dir",
135 baseDir,
136 "--allow-downgrade",
137 ]).pipe(
138 Effect.provideService(HostProcessEnvironment, {}),
139 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
140 );
141
142 expect(installOptions).toEqual([{ allowDowngrade: true }]);
143 }),
144 );
145});
146
147it.effect.each([
148 { name: "a new service", state: { ...status, installed: false, current: false } },
149 { name: "an older service", state: { ...status, current: false, installedVersion: "0.0.0" } },
150 {
151 name: "the same version",
152 state: { ...status, current: false, installedVersion: packageJson.version },
153 },
154 { name: "an unknown version", state: { ...status, current: false } },
155])("installs or repairs $name without an override", ({ state }) =>
156 Effect.gen(function* () {
157 const { service, installOptions } = makeTestService(state);
158
159 const result = yield* reconcileService().pipe(
160 Effect.provideService(BootService.BootService, service),
161 );
162
163 expect(result.changed).toBe(true);
164 expect(installOptions).toEqual([undefined]);
165 }),
166);
167
168it.effect("leaves a newer service unchanged during onboarding without prompting", () =>
169 Effect.gen(function* () {
170 const { service, installOptions } = makeTestService(newerServiceStatus);
171 const terminal = Terminal.make({
172 columns: Effect.succeed(80),
173 rows: Effect.succeed(24),
174 readInput: Effect.die("Onboarding must not prompt to replace a newer service."),
175 readLine: Effect.die("Onboarding must not prompt to replace a newer service."),
176 display: () => Effect.die("Onboarding must not prompt to replace a newer service."),
177 });
178
179 const ready = yield* offerServiceDuringOnboarding.pipe(
180 Effect.provideService(BootService.BootService, service),
181 Effect.provideService(Terminal.Terminal, terminal),
182 Effect.provide(NodeServices.layer),
183 );
184
185 expect(ready).toBe(false);
186 expect(installOptions).toEqual([]);
187 }),
188);
189
190it.effect("keeps onboarding successful when a newer version appears before install", () =>
191 Effect.gen(function* () {
192 const ready = yield* recoverServiceOnboardingOffer(
193 Effect.fail(
194 new BootService.BootServiceDowngradeRefusedError({
195 installedVersion: "999.0.0",
196 targetVersion: packageJson.version,
197 }),
198 ),
199 );
200
201 expect(ready).toBe(false);
202 }),
203);