Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { 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 * as TestConsole from "effect/testing/TestConsole";
11import { Command } from "effect/cli";
12import { afterEach, vi } from "vite-plus/test";
13
14import packageJson from "../../package.json" with { type: "json" };
15import * as BootService from "../cloud/bootService.ts";
16import {
17 offerServiceDuringOnboarding,
18 recoverServiceOnboardingOffer,
19 serviceCommand,
20} from "./service.ts";
21
22afterEach(() => vi.restoreAllMocks());
23
24const status = {
25 supported: true,
26 installed: true,
27 current: true,
28 unitPath: "/home/me/.config/systemd/user/t3code.service",
29 logPath: "/home/me/.t3/userdata/logs/boot-service.log",
30} as const;
31
32const newerServiceStatus = { ...status, current: false, installedVersion: "999.0.0" };
33
34function makeTestService(
35 serviceStatus: BootService.BootServiceStatus,
36 restartOutcome: "restarted" | "queued" = "restarted",
37) {
38 const installOptions: Array<Parameters<BootService.BootService["Service"]["install"]>[0]> = [];
39 const restarts: Array<true> = [];
40 const service = BootService.BootService.of({
41 status: Effect.succeed(serviceStatus),
42 withStopped: (operation) => operation,
43 restart: Effect.sync(() => {
44 restarts.push(true);
45 return serviceStatus.installed ? restartOutcome : "skipped";
46 }),
47 install: (options) =>
48 Effect.sync(() => {
49 installOptions.push(options);
50 return {
51 program: ["/test/t3/runtime/versions/1.0.0/t3", "__service-launcher"],
52 baseDir: "/test/t3",
53 unitPath: serviceStatus.unitPath,
54 logPath: serviceStatus.logPath,
55 };
56 }),
57 uninstall: Effect.succeed(false),
58 });
59 return { service, installOptions, restarts };
60}
61
62it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands", (it) => {
63 it.effect.each(["install", "update"] as const)(
64 "%s refuses a downgrade before changing the service",
65 (command) =>
66 Effect.gen(function* () {
67 const fs = yield* FileSystem.FileSystem;
68 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
69 const { service, installOptions } = makeTestService(newerServiceStatus);
70 vi.spyOn(BootService, "layer").mockReturnValue(
71 Layer.succeed(BootService.BootService, service),
72 );
73
74 const error = yield* Command.runWith(serviceCommand, { version: packageJson.version })([
75 command,
76 "--base-dir",
77 baseDir,
78 ]).pipe(
79 Effect.provideService(HostProcess.Environment, {}),
80 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
81 Effect.flip,
82 );
83
84 expect(error).toMatchObject({
85 _tag: "BootServiceDowngradeRefusedError",
86 installedVersion: "999.0.0",
87 targetVersion: packageJson.version,
88 });
89 expect(installOptions).toEqual([]);
90 }),
91 );
92
93 it.effect("protect refuses root before creating any T3 directories", () =>
94 Effect.gen(function* () {
95 const fs = yield* FileSystem.FileSystem;
96 const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
97 const baseDir = `${parent}/t3`;
98 vi.spyOn(BootService, "layer").mockReturnValue(
99 Layer.succeed(BootService.BootService, makeTestService(status).service),
100 );
101
102 const output = yield* Command.runWith(serviceCommand, { version: packageJson.version })([
103 "protect",
104 "--base-dir",
105 baseDir,
106 ]).pipe(
107 Effect.andThen(TestConsole.logLines),
108 Effect.map((lines) => lines.join("\n")),
109 Effect.provideService(HostProcess.Environment, {}),
110 Effect.provideService(HostProcess.Platform, "linux"),
111 Effect.provideService(HostProcess.UserId, 0),
112 Effect.provide(
113 Layer.mergeAll(
114 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
115 Layer.fresh(TestConsole.layer),
116 ),
117 ),
118 );
119 expect(output).toContain("without sudo");
120 expect(yield* fs.exists(baseDir)).toBe(false);
121 }),
122 );
123
124 // regression: #13239 a stale nvm-installed CLI reported "already using" its own version
125 it.effect.each(["install", "update"] as const)(
126 "%s names the CLI that found nothing to change",
127 (command) =>
128 Effect.gen(function* () {
129 const fs = yield* FileSystem.FileSystem;
130 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
131 const { service, installOptions } = makeTestService(status);
132 vi.spyOn(BootService, "layer").mockReturnValue(
133 Layer.succeed(BootService.BootService, service),
134 );
135 const node = "/home/me/.nvm/versions/node/v26.8.2/bin/node";
136 const script = "/home/me/.nvm/versions/node/v26.8.2/lib/node_modules/t3/dist/bin.mjs";
137
138 const output = yield* Command.runWith(serviceCommand, { version: packageJson.version })([
139 command,
140 "--base-dir",
141 baseDir,
142 ]).pipe(
143 Effect.andThen(TestConsole.logLines),
144 Effect.map((lines) => lines.join("\n")),
145 Effect.provideService(HostProcess.Environment, {}),
146 Effect.provideService(HostProcess.IsExecutable, false),
147 Effect.provideService(HostProcess.ExecutablePath, node),
148 Effect.provideService(HostProcess.Arguments, [node, script, "service", command]),
149 Effect.provide(
150 Layer.mergeAll(
151 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
152 Layer.fresh(TestConsole.layer),
153 ),
154 ),
155 );
156 expect(output).toContain(`t3@${packageJson.version}`);
157 expect(output).toContain(script);
158 expect(installOptions).toEqual([]);
159 }),
160 );
161});
162
163it.effect("leaves a newer service unchanged during onboarding without prompting", () =>
164 Effect.gen(function* () {
165 const { service, installOptions } = makeTestService(newerServiceStatus);
166 const terminal = Terminal.make({
167 columns: Effect.succeed(80),
168 rows: Effect.succeed(24),
169 readInput: Effect.die("Onboarding must not prompt to replace a newer service."),
170 readLine: Effect.die("Onboarding must not prompt to replace a newer service."),
171 display: () => Effect.die("Onboarding must not prompt to replace a newer service."),
172 });
173
174 const ready = yield* offerServiceDuringOnboarding.pipe(
175 Effect.provideService(BootService.BootService, service),
176 Effect.provideService(Terminal.Terminal, terminal),
177 Effect.provide(NodeServices.layer),
178 );
179
180 expect(ready).toBe("skipped");
181 expect(installOptions).toEqual([]);
182 }),
183);
184
185// regression: #9602 diagnose incomplete headless server setup
186it.effect("keeps the manual-server fallback when background prerequisites fail", () =>
187 Effect.gen(function* () {
188 const ready = yield* recoverServiceOnboardingOffer(
189 Effect.fail(new BootService.BootServicePrerequisiteError({ problem: "linger-disabled" })),
190 );
191 expect(ready).toBe("skipped");
192 }),
193);