contracts.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. controlRequestSchema,
  4. controlResponseSchema,
  5. releaseCatalogSchema,
  6. releaseManifestSchema,
  7. serverStatusSchema,
  8. createStoppedServerStatus,
  9. } from "../src/contracts.js";
  10. describe("R2 control contract", () => {
  11. it("rejects unknown lifecycle commands and accepts JSONL-safe requests", () => {
  12. expect(controlRequestSchema.safeParse({ id: "1", command: "status" }).success).toBe(true);
  13. expect(controlRequestSchema.safeParse({ id: "1", command: "kill-pid", pid: 4 }).success).toBe(
  14. false,
  15. );
  16. expect(
  17. controlRequestSchema.safeParse({ id: "1", command: "confirm-uninstall", confirmation: "DELETE" })
  18. .success,
  19. ).toBe(true);
  20. });
  21. it("keeps status output free of business payloads", () => {
  22. const parsed = serverStatusSchema.parse({
  23. state: "ready",
  24. pid: 42,
  25. port: 38123,
  26. host: "127.0.0.1",
  27. version: "3.8.0",
  28. generation: 1,
  29. serviceRegistered: false,
  30. runningTaskCount: 0,
  31. crashBudget: { windowStartedAt: 1, crashCount: 0, nextRestartDelayMs: 0, exhausted: false },
  32. updatedAt: 2,
  33. });
  34. expect(parsed).not.toHaveProperty("token");
  35. expect(controlResponseSchema.parse({ id: "1", ok: true, result: parsed }).ok).toBe(true);
  36. });
  37. it("rejects release versions that could escape the releases directory", () => {
  38. expect(releaseManifestSchema.safeParse({ version: "../escape", releaseDir: "/tmp/release" }).success).toBe(false);
  39. expect(releaseCatalogSchema.safeParse({
  40. schemaVersion: 1,
  41. releases: [{ version: "3.8.0/evil", target: "linux-x64", archiveUrl: "https://example.test/release.tar.gz", archiveSha256: "a".repeat(64) }],
  42. }).success).toBe(false);
  43. });
  44. it("builds a complete stopped snapshot for offline CLI fallbacks", () => {
  45. const status = createStoppedServerStatus("3.8.0", { serviceRegistered: true, now: 42 });
  46. expect(status).toEqual(expect.objectContaining({
  47. state: "stopped",
  48. pid: null,
  49. port: null,
  50. host: null,
  51. version: "3.8.0",
  52. generation: 0,
  53. startedAt: null,
  54. lastExitReason: null,
  55. serviceRegistered: true,
  56. runningTaskCount: 0,
  57. updatedAt: 42,
  58. }));
  59. expect(serverStatusSchema.parse(status)).toEqual(status);
  60. });
  61. });