serviceManager.test.ts 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { describe, expect, it } from "vitest";
  2. import { createServiceDescriptor, registerService, unregisterService } from "../src/platform/serviceManager.js";
  3. describe("service manager adapters", () => {
  4. it("maps all supported platforms to their native registration commands", async () => {
  5. const calls: Array<{ command: string; args: readonly string[] }> = [];
  6. const executor = { run: async (command: string, args: readonly string[]) => { calls.push({ command, args }); } };
  7. for (const platform of ["darwin", "linux", "win32"] as const) {
  8. const descriptor = createServiceDescriptor({ platform, command: "/opt/zcode/bin/zcode", args: ["serve", "--daemon"] });
  9. await registerService(descriptor, `/tmp/${descriptor.kind}.service`, executor);
  10. await unregisterService(descriptor, `/tmp/${descriptor.kind}.service`, executor);
  11. }
  12. expect(calls.map((call) => call.command)).toEqual([
  13. "launchctl", "launchctl", "launchctl", "launchctl",
  14. "systemctl", "systemctl", "systemctl",
  15. "schtasks", "schtasks", "schtasks",
  16. ]);
  17. expect(calls.at(-2)?.args).toContain("/Run");
  18. });
  19. it("does not restart a launchd service after a normal stop", () => {
  20. const descriptor = createServiceDescriptor({ platform: "darwin", command: "/opt/zcode/bin/zcode" });
  21. expect(descriptor.content).toContain("<key>KeepAlive</key><dict><key>SuccessfulExit</key><false/></dict>");
  22. });
  23. it("tolerates launchd unregister when a deferred registration was never loaded", async () => {
  24. const descriptor = createServiceDescriptor({ platform: "darwin", command: "/opt/zcode/bin/zcode" });
  25. await expect(unregisterService(descriptor, "/tmp/zcode.plist", {
  26. run: async () => {
  27. throw new Error("launchctl unload failed (3): Could not find specified service");
  28. },
  29. })).resolves.toBeUndefined();
  30. });
  31. it("tolerates a missing Windows task when schtasks reports a missing file", async () => {
  32. const descriptor = createServiceDescriptor({ platform: "win32", command: "C:/zcode/bin/zcode.cmd" });
  33. await expect(unregisterService(descriptor, "C:/zcode/service/task.json", {
  34. run: async () => {
  35. throw new Error("schtasks /Delete failed (1): ERROR: The system cannot find the file specified.");
  36. },
  37. })).resolves.toBeUndefined();
  38. });
  39. it("does not swallow Windows task permission failures", async () => {
  40. const descriptor = createServiceDescriptor({ platform: "win32", command: "C:/zcode/bin/zcode.cmd" });
  41. await expect(unregisterService(descriptor, "C:/zcode/service/task.json", {
  42. run: async () => {
  43. throw new Error("schtasks /Delete failed (5): Access is denied.");
  44. },
  45. })).rejects.toThrow(/Access is denied/);
  46. });
  47. it("uses the structured schtasks HRESULT probe before deleting a Windows task", async () => {
  48. const descriptor = createServiceDescriptor({ platform: "win32", command: "C:/zcode/bin/zcode.cmd" });
  49. const calls: string[][] = [];
  50. await expect(unregisterService(descriptor, "C:/zcode/service/task.json", {
  51. run: async (_command, args) => { calls.push([...args]); },
  52. runResult: async (_command, args) => {
  53. calls.push([...args]);
  54. return { exitCode: 0x80070002, stderr: "" };
  55. },
  56. })).resolves.toBeUndefined();
  57. expect(calls).toEqual([["/Query", "/TN", descriptor.name, "/HResult"]]);
  58. });
  59. it("does not treat a structured Windows task query permission error as absence", async () => {
  60. const descriptor = createServiceDescriptor({ platform: "win32", command: "C:/zcode/bin/zcode.cmd" });
  61. await expect(unregisterService(descriptor, "C:/zcode/service/task.json", {
  62. run: async () => undefined,
  63. runResult: async () => ({ exitCode: 0x80070005, stderr: "Access is denied." }),
  64. })).rejects.toThrow(/failed \(2147942405\)/);
  65. });
  66. it("reloads an existing launchd label before starting the new descriptor", async () => {
  67. const descriptor = createServiceDescriptor({ platform: "darwin", command: "/opt/zcode/bin/zcode", args: ["serve", "--new"] });
  68. const calls: string[][] = [];
  69. await registerService(descriptor, "/tmp/new.plist", {
  70. run: async (_command, args) => {
  71. calls.push([...args]);
  72. if (args[0] === "unload") throw new Error("Could not find specified service");
  73. },
  74. });
  75. expect(calls).toEqual([
  76. ["unload", "-w", "/tmp/new.plist"],
  77. ["load", "-w", "/tmp/new.plist"],
  78. ["start", descriptor.name],
  79. ]);
  80. });
  81. it("propagates launchd load failures with the descriptor path", async () => {
  82. const descriptor = createServiceDescriptor({ platform: "darwin", command: "/opt/zcode/bin/zcode" });
  83. await expect(registerService(descriptor, "/tmp/broken.plist", {
  84. run: async (_command, args) => {
  85. if (args[0] === "load") throw new Error("permission denied");
  86. },
  87. })).rejects.toThrow(/broken\.plist/);
  88. });
  89. });