platform.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, expect, it } from "vitest";
  2. import { join } from "node:path";
  3. import { createDaemonServiceDescriptor, createServiceDescriptor, serviceDescriptorPath } from "../src/platform/serviceManager.js";
  4. import { resolveServerLayout, stablePathId } from "../src/runtime/paths.js";
  5. describe("user service descriptors", () => {
  6. it.each([
  7. ["darwin", "launchd"],
  8. ["linux", "systemd"],
  9. ["win32", "task-scheduler"],
  10. ] as const)("creates a stable %s descriptor", (platform, kind) => {
  11. const descriptor = createServiceDescriptor({ platform, command: "/opt/zcode/bin/zcode" });
  12. expect(descriptor.kind).toBe(kind);
  13. expect(descriptor.content).toContain("zcode");
  14. });
  15. it.each(["darwin", "linux", "win32"] as const)(
  16. "pins the %s daemon descriptor to the data-root stable launcher and explicit server root",
  17. (platform) => {
  18. const layout = resolveServerLayout("/var/lib/zcode-custom/.zcode/server");
  19. const descriptor = createDaemonServiceDescriptor({ platform, layout });
  20. const launcher = join(layout.stableBinDir, platform === "win32" ? "zcode.cmd" : "zcode");
  21. if (platform === "win32") {
  22. const task = JSON.parse(descriptor.content) as { command: string; args: string[] };
  23. expect(task.command).toBe(launcher);
  24. expect(task.args).toContain(layout.serverRoot);
  25. } else {
  26. expect(descriptor.content).toContain(launcher);
  27. }
  28. expect(descriptor.content).toContain("--server-root");
  29. expect(descriptor.content).toContain("--service-entry");
  30. if (platform !== "win32") {
  31. expect(descriptor.content).toContain(layout.serverRoot);
  32. }
  33. expect(descriptor.content).not.toContain("server-cli.js");
  34. expect(descriptor.name).toBe(`com.zhipu.zcode.server.${stablePathId(layout.serverRoot)}`);
  35. },
  36. );
  37. it("isolates service identity and descriptor filenames by server root", () => {
  38. const firstLayout = resolveServerLayout("/var/lib/zcode-first/.zcode/server");
  39. const secondLayout = resolveServerLayout("/var/lib/zcode-second/.zcode/server");
  40. const first = createDaemonServiceDescriptor({ platform: "linux", layout: firstLayout });
  41. const second = createDaemonServiceDescriptor({ platform: "linux", layout: secondLayout });
  42. expect(first.name).not.toBe(second.name);
  43. expect(serviceDescriptorPath(firstLayout, first)).toBe(join(firstLayout.serviceDir, `${first.name}.service`));
  44. expect(serviceDescriptorPath(secondLayout, second)).toBe(join(secondLayout.serviceDir, `${second.name}.service`));
  45. });
  46. });