| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- import { describe, expect, it } from "vitest";
- import {
- createServiceDescriptor,
- isServiceManagerAvailable,
- registerService,
- unregisterService,
- } from "../src/platform/serviceManager.js";
- describe("service manager adapters", () => {
- it("probes the Linux user service manager without registering a unit", async () => {
- const calls: Array<{
- command: string;
- args: readonly string[];
- timeoutMs: number | undefined;
- }> = [];
- await expect(
- isServiceManagerAvailable("linux", {
- run: async () => undefined,
- runResult: async (command, args, options) => {
- calls.push({ command, args, timeoutMs: options?.timeoutMs });
- return { exitCode: 0, stderr: "" };
- },
- }),
- ).resolves.toBe(true);
- expect(calls).toEqual([
- {
- command: "systemctl",
- args: ["--user", "show-environment"],
- timeoutMs: 2_000,
- },
- ]);
- });
- it("treats an inaccessible user service manager as a fallback capability", async () => {
- await expect(
- isServiceManagerAvailable("linux", {
- run: async () => undefined,
- runResult: async () => ({
- exitCode: 1,
- stderr: "Failed to connect to bus",
- }),
- }),
- ).resolves.toBe(false);
- });
- it("maps all supported platforms to their native registration commands", async () => {
- const calls: Array<{ command: string; args: readonly string[] }> = [];
- const executor = {
- run: async (command: string, args: readonly string[]) => {
- calls.push({ command, args });
- },
- };
- for (const platform of ["darwin", "linux", "win32"] as const) {
- const descriptor = createServiceDescriptor({
- platform,
- command: "/opt/zcode/bin/zcode",
- args: ["serve", "--daemon"],
- });
- await registerService(descriptor, `/tmp/${descriptor.kind}.service`, executor);
- await unregisterService(descriptor, `/tmp/${descriptor.kind}.service`, executor);
- }
- expect(calls.map((call) => call.command)).toEqual([
- "launchctl",
- "launchctl",
- "launchctl",
- "launchctl",
- "systemctl",
- "systemctl",
- "systemctl",
- "schtasks",
- "schtasks",
- "schtasks",
- ]);
- expect(calls.at(-2)?.args).toContain("/Run");
- });
- it("does not restart a launchd service after a normal stop", () => {
- const descriptor = createServiceDescriptor({
- platform: "darwin",
- command: "/opt/zcode/bin/zcode",
- });
- expect(descriptor.content).toContain(
- "<key>KeepAlive</key><dict><key>SuccessfulExit</key><false/></dict>",
- );
- });
- it("tolerates launchd unregister when a deferred registration was never loaded", async () => {
- const descriptor = createServiceDescriptor({
- platform: "darwin",
- command: "/opt/zcode/bin/zcode",
- });
- await expect(
- unregisterService(descriptor, "/tmp/zcode.plist", {
- run: async () => {
- throw new Error("launchctl unload failed (3): Could not find specified service");
- },
- }),
- ).resolves.toBeUndefined();
- });
- it("tolerates a missing Windows task when schtasks reports a missing file", async () => {
- const descriptor = createServiceDescriptor({
- platform: "win32",
- command: "C:/zcode/bin/zcode.cmd",
- });
- await expect(
- unregisterService(descriptor, "C:/zcode/service/task.json", {
- run: async () => {
- throw new Error(
- "schtasks /Delete failed (1): ERROR: The system cannot find the file specified.",
- );
- },
- }),
- ).resolves.toBeUndefined();
- });
- it("does not swallow Windows task permission failures", async () => {
- const descriptor = createServiceDescriptor({
- platform: "win32",
- command: "C:/zcode/bin/zcode.cmd",
- });
- await expect(
- unregisterService(descriptor, "C:/zcode/service/task.json", {
- run: async () => {
- throw new Error("schtasks /Delete failed (5): Access is denied.");
- },
- }),
- ).rejects.toThrow(/Access is denied/);
- });
- it("uses the structured schtasks HRESULT probe before deleting a Windows task", async () => {
- const descriptor = createServiceDescriptor({
- platform: "win32",
- command: "C:/zcode/bin/zcode.cmd",
- });
- const calls: string[][] = [];
- await expect(
- unregisterService(descriptor, "C:/zcode/service/task.json", {
- run: async (_command, args) => {
- calls.push([...args]);
- },
- runResult: async (_command, args) => {
- calls.push([...args]);
- return { exitCode: 0x80070002, stderr: "" };
- },
- }),
- ).resolves.toBeUndefined();
- expect(calls).toEqual([["/Query", "/TN", descriptor.name, "/HResult"]]);
- });
- it("does not treat a structured Windows task query permission error as absence", async () => {
- const descriptor = createServiceDescriptor({
- platform: "win32",
- command: "C:/zcode/bin/zcode.cmd",
- });
- await expect(
- unregisterService(descriptor, "C:/zcode/service/task.json", {
- run: async () => undefined,
- runResult: async () => ({
- exitCode: 0x80070005,
- stderr: "Access is denied.",
- }),
- }),
- ).rejects.toThrow(/failed \(2147942405\)/);
- });
- it("reloads an existing launchd label before starting the new descriptor", async () => {
- const descriptor = createServiceDescriptor({
- platform: "darwin",
- command: "/opt/zcode/bin/zcode",
- args: ["serve", "--new"],
- });
- const calls: string[][] = [];
- await registerService(descriptor, "/tmp/new.plist", {
- run: async (_command, args) => {
- calls.push([...args]);
- if (args[0] === "unload") throw new Error("Could not find specified service");
- },
- });
- expect(calls).toEqual([
- ["unload", "-w", "/tmp/new.plist"],
- ["load", "-w", "/tmp/new.plist"],
- ["start", descriptor.name],
- ]);
- });
- it("propagates launchd load failures with the descriptor path", async () => {
- const descriptor = createServiceDescriptor({
- platform: "darwin",
- command: "/opt/zcode/bin/zcode",
- });
- await expect(
- registerService(descriptor, "/tmp/broken.plist", {
- run: async (_command, args) => {
- if (args[0] === "load") throw new Error("permission denied");
- },
- }),
- ).rejects.toThrow(/broken\.plist/);
- });
- });
|