import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as NetService from "@t3tools/shared/Net";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as TestConsole from "effect/testing/TestConsole";
import { Command } from "effect/cli";
import { afterEach, vi } from "vite-plus/test";
import packageJson from "../../package.json" with { type: "json" };
import * as BootService from "../cloud/bootService.ts";
import { serviceCommand } from "./service.ts";
afterEach(() => vi.restoreAllMocks());
const status = {
supported: true,
installed: true,
current: true,
unitPath: "/home/me/.config/systemd/user/t3code.service",
logPath: "/home/me/.t3/userdata/logs/boot-service.log",
} as const;
const newerServiceStatus = { ...status, current: false, installedVersion: "999.0.0" };
function makeTestService(
serviceStatus: BootService.BootServiceStatus,
restartOutcome: "restarted" | "queued" = "restarted",
) {
const installOptions: Array<Parameters<BootService.BootService["Service"]["install"]>[0]> = [];
const restarts: Array<true> = [];
const uninstalls: Array<true> = [];
const service = BootService.BootService.of({
status: Effect.succeed(serviceStatus),
withStopped: (operation) => operation,
restart: Effect.sync(() => {
restarts.push(true);
return serviceStatus.installed ? restartOutcome : "skipped";
}),
install: (options) =>
Effect.sync(() => {
installOptions.push(options);
return {
program: ["/test/t3/runtime/versions/1.0.0/t3", "__service-launcher"],
baseDir: "/test/t3",
unitPath: serviceStatus.unitPath,
logPath: serviceStatus.logPath,
};
}),
uninstall: Effect.sync(() => {
uninstalls.push(true);
return serviceStatus.installed;
}),
});
return { service, installOptions, restarts, uninstalls };
}
it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands", (it) => {
it.effect.each(["install", "update"] as const)(
"%s refuses a downgrade before changing the service",
(command) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
const { service, installOptions } = makeTestService(newerServiceStatus);
vi.spyOn(BootService, "layer").mockReturnValue(
Layer.succeed(BootService.BootService, service),
);
const error = yield* Command.runWith(serviceCommand, { version: packageJson.version })([
command,
"--base-dir",
baseDir,
]).pipe(
Effect.provideService(HostProcess.Environment, {}),
Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
Effect.flip,
);
expect(error).toMatchObject({
_tag: "BootServiceDowngradeRefusedError",
installedVersion: "999.0.0",
targetVersion: packageJson.version,
});
expect(installOptions).toEqual([]);
}),
);
it.effect.each([
{ label: "a new service", installed: false, env: "", expected: "T3CODE_LAN=false\n" },
// Services installed before T3CODE_LAN existed keep listening on the local network.
{ label: "an older service", installed: true, env: "", expected: "" },
{
label: "a set host",
installed: false,
env: "T3CODE_HOST=::\n",
expected: "T3CODE_HOST=::\n",
},
])("install keeps $label off the local network only when it is new", (input) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
yield* fs.writeFileString(`${baseDir}/service.env`, input.env);
const { service } = makeTestService({
...status,
installed: input.installed,
current: false,
});
vi.spyOn(BootService, "layer").mockReturnValue(
Layer.succeed(BootService.BootService, service),
);
yield* Command.runWith(serviceCommand, { version: packageJson.version })([
"install",
"--base-dir",
baseDir,
]).pipe(
Effect.provideService(HostProcess.Environment, {}),
Effect.provide(
Layer.mergeAll(
ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
Layer.fresh(TestConsole.layer),
),
),
);
expect(yield* fs.readFileString(`${baseDir}/service.env`)).toBe(input.expected);
}),
);
it.effect("protect refuses root before creating any T3 directories", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
const baseDir = `${parent}/t3`;
vi.spyOn(BootService, "layer").mockReturnValue(
Layer.succeed(BootService.BootService, makeTestService(status).service),
);
const output = yield* Command.runWith(serviceCommand, { version: packageJson.version })([
"protect",
"--base-dir",
baseDir,
]).pipe(
Effect.andThen(TestConsole.logLines),
Effect.map((lines) => lines.join("\n")),
Effect.provideService(HostProcess.Environment, {}),
Effect.provideService(HostProcess.Platform, "linux"),
Effect.provideService(HostProcess.UserId, 0),
Effect.provide(
Layer.mergeAll(
ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
Layer.fresh(TestConsole.layer),
),
),
);
expect(output).toContain("without sudo");
expect(yield* fs.exists(baseDir)).toBe(false);
}),
);
// regression: #13239 a stale nvm-installed CLI reported "already using" its own version
it.effect.each(["install", "update"] as const)(
"%s names the CLI that found nothing to change",
(command) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
const { service, installOptions } = makeTestService(status);
vi.spyOn(BootService, "layer").mockReturnValue(
Layer.succeed(BootService.BootService, service),
);
const node = "/home/me/.nvm/versions/node/v26.8.2/bin/node";
const script = "/home/me/.nvm/versions/node/v26.8.2/lib/node_modules/t3/dist/bin.mjs";
const output = yield* Command.runWith(serviceCommand, { version: packageJson.version })([
command,
"--base-dir",
baseDir,
]).pipe(
Effect.andThen(TestConsole.logLines),
Effect.map((lines) => lines.join("\n")),
Effect.provideService(HostProcess.Environment, {}),
Effect.provideService(HostProcess.IsExecutable, false),
Effect.provideService(HostProcess.ExecutablePath, node),
Effect.provideService(HostProcess.Arguments, [node, script, "service", command]),
Effect.provide(
Layer.mergeAll(
ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
Layer.fresh(TestConsole.layer),
),
),
);
expect(output).toContain(`t3@${packageJson.version}`);
expect(output).toContain(script);
expect(installOptions).toEqual([]);
}),
);
it.effect.each([
{ label: "this home's service", otherHome: false, unreadable: false, removed: [true] },
{ label: "another home's service", otherHome: true, unreadable: false, removed: [] },
{ label: "an unreadable service", otherHome: false, unreadable: true, removed: [true] },
])("uninstall removes only $label", (input) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
const { service, uninstalls } = makeTestService({
...status,
installedBaseDir: input.otherHome ? "/home/me/.t3" : baseDir,
});
vi.spyOn(BootService, "layer").mockReturnValue(
Layer.succeed(
BootService.BootService,
input.unreadable
? {
...service,
status: Effect.fail(new BootService.BootServiceInstallError({ cause: "EACCES" })),
}
: service,
),
);
const output = yield* Command.runWith(serviceCommand, { version: packageJson.version })([
"uninstall",
"--base-dir",
baseDir,
]).pipe(
Effect.andThen(TestConsole.logLines),
Effect.map((lines) => lines.join("\n")),
Effect.provideService(HostProcess.Environment, {}),
Effect.provide(
Layer.mergeAll(
ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
Layer.fresh(TestConsole.layer),
),
),
);
expect(uninstalls).toEqual(input.removed);
expect(output).toContain(input.otherHome ? "another T3 home" : "Removed");
}),
);
});