serviceManager.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createServiceDescriptor,
  4. isServiceManagerAvailable,
  5. registerService,
  6. unregisterService,
  7. } from "../src/platform/serviceManager.js";
  8. describe("service manager adapters", () => {
  9. it("probes the Linux user service manager without registering a unit", async () => {
  10. const calls: Array<{
  11. command: string;
  12. args: readonly string[];
  13. timeoutMs: number | undefined;
  14. }> = [];
  15. await expect(
  16. isServiceManagerAvailable("linux", {
  17. run: async () => undefined,
  18. runResult: async (command, args, options) => {
  19. calls.push({ command, args, timeoutMs: options?.timeoutMs });
  20. return { exitCode: 0, stderr: "" };
  21. },
  22. }),
  23. ).resolves.toBe(true);
  24. expect(calls).toEqual([
  25. {
  26. command: "systemctl",
  27. args: ["--user", "show-environment"],
  28. timeoutMs: 2_000,
  29. },
  30. ]);
  31. });
  32. it("treats an inaccessible user service manager as a fallback capability", async () => {
  33. await expect(
  34. isServiceManagerAvailable("linux", {
  35. run: async () => undefined,
  36. runResult: async () => ({
  37. exitCode: 1,
  38. stderr: "Failed to connect to bus",
  39. }),
  40. }),
  41. ).resolves.toBe(false);
  42. });
  43. it("maps all supported platforms to their native registration commands", async () => {
  44. const calls: Array<{ command: string; args: readonly string[] }> = [];
  45. const executor = {
  46. run: async (command: string, args: readonly string[]) => {
  47. calls.push({ command, args });
  48. },
  49. };
  50. for (const platform of ["darwin", "linux", "win32"] as const) {
  51. const descriptor = createServiceDescriptor({
  52. platform,
  53. command: "/opt/zcode/bin/zcode",
  54. args: ["serve", "--daemon"],
  55. });
  56. await registerService(descriptor, `/tmp/${descriptor.kind}.service`, executor);
  57. await unregisterService(descriptor, `/tmp/${descriptor.kind}.service`, executor);
  58. }
  59. expect(calls.map((call) => call.command)).toEqual([
  60. "launchctl",
  61. "launchctl",
  62. "launchctl",
  63. "launchctl",
  64. "systemctl",
  65. "systemctl",
  66. "systemctl",
  67. "schtasks",
  68. "schtasks",
  69. "schtasks",
  70. ]);
  71. expect(calls.at(-2)?.args).toContain("/Run");
  72. });
  73. it("does not restart a launchd service after a normal stop", () => {
  74. const descriptor = createServiceDescriptor({
  75. platform: "darwin",
  76. command: "/opt/zcode/bin/zcode",
  77. });
  78. expect(descriptor.content).toContain(
  79. "<key>KeepAlive</key><dict><key>SuccessfulExit</key><false/></dict>",
  80. );
  81. });
  82. it("tolerates launchd unregister when a deferred registration was never loaded", async () => {
  83. const descriptor = createServiceDescriptor({
  84. platform: "darwin",
  85. command: "/opt/zcode/bin/zcode",
  86. });
  87. await expect(
  88. unregisterService(descriptor, "/tmp/zcode.plist", {
  89. run: async () => {
  90. throw new Error("launchctl unload failed (3): Could not find specified service");
  91. },
  92. }),
  93. ).resolves.toBeUndefined();
  94. });
  95. it("tolerates a missing Windows task when schtasks reports a missing file", async () => {
  96. const descriptor = createServiceDescriptor({
  97. platform: "win32",
  98. command: "C:/zcode/bin/zcode.cmd",
  99. });
  100. await expect(
  101. unregisterService(descriptor, "C:/zcode/service/task.json", {
  102. run: async () => {
  103. throw new Error(
  104. "schtasks /Delete failed (1): ERROR: The system cannot find the file specified.",
  105. );
  106. },
  107. }),
  108. ).resolves.toBeUndefined();
  109. });
  110. it("does not swallow Windows task permission failures", async () => {
  111. const descriptor = createServiceDescriptor({
  112. platform: "win32",
  113. command: "C:/zcode/bin/zcode.cmd",
  114. });
  115. await expect(
  116. unregisterService(descriptor, "C:/zcode/service/task.json", {
  117. run: async () => {
  118. throw new Error("schtasks /Delete failed (5): Access is denied.");
  119. },
  120. }),
  121. ).rejects.toThrow(/Access is denied/);
  122. });
  123. it("uses the structured schtasks HRESULT probe before deleting a Windows task", async () => {
  124. const descriptor = createServiceDescriptor({
  125. platform: "win32",
  126. command: "C:/zcode/bin/zcode.cmd",
  127. });
  128. const calls: string[][] = [];
  129. await expect(
  130. unregisterService(descriptor, "C:/zcode/service/task.json", {
  131. run: async (_command, args) => {
  132. calls.push([...args]);
  133. },
  134. runResult: async (_command, args) => {
  135. calls.push([...args]);
  136. return { exitCode: 0x80070002, stderr: "" };
  137. },
  138. }),
  139. ).resolves.toBeUndefined();
  140. expect(calls).toEqual([["/Query", "/TN", descriptor.name, "/HResult"]]);
  141. });
  142. it("does not treat a structured Windows task query permission error as absence", async () => {
  143. const descriptor = createServiceDescriptor({
  144. platform: "win32",
  145. command: "C:/zcode/bin/zcode.cmd",
  146. });
  147. await expect(
  148. unregisterService(descriptor, "C:/zcode/service/task.json", {
  149. run: async () => undefined,
  150. runResult: async () => ({
  151. exitCode: 0x80070005,
  152. stderr: "Access is denied.",
  153. }),
  154. }),
  155. ).rejects.toThrow(/failed \(2147942405\)/);
  156. });
  157. it("reloads an existing launchd label before starting the new descriptor", async () => {
  158. const descriptor = createServiceDescriptor({
  159. platform: "darwin",
  160. command: "/opt/zcode/bin/zcode",
  161. args: ["serve", "--new"],
  162. });
  163. const calls: string[][] = [];
  164. await registerService(descriptor, "/tmp/new.plist", {
  165. run: async (_command, args) => {
  166. calls.push([...args]);
  167. if (args[0] === "unload") throw new Error("Could not find specified service");
  168. },
  169. });
  170. expect(calls).toEqual([
  171. ["unload", "-w", "/tmp/new.plist"],
  172. ["load", "-w", "/tmp/new.plist"],
  173. ["start", descriptor.name],
  174. ]);
  175. });
  176. it("propagates launchd load failures with the descriptor path", async () => {
  177. const descriptor = createServiceDescriptor({
  178. platform: "darwin",
  179. command: "/opt/zcode/bin/zcode",
  180. });
  181. await expect(
  182. registerService(descriptor, "/tmp/broken.plist", {
  183. run: async (_command, args) => {
  184. if (args[0] === "load") throw new Error("permission denied");
  185. },
  186. }),
  187. ).rejects.toThrow(/broken\.plist/);
  188. });
  189. });