contracts.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. controlRequestSchema,
  4. controlResponseSchema,
  5. releaseCatalogSchema,
  6. releaseManifestSchema,
  7. runtimeIdentitySchema,
  8. serverStatusSchema,
  9. createStoppedServerStatus,
  10. } from "../src/contracts.js";
  11. describe("R2 control contract", () => {
  12. it("rejects unknown lifecycle commands and accepts JSONL-safe requests", () => {
  13. expect(controlRequestSchema.safeParse({ id: "1", command: "status" }).success).toBe(true);
  14. expect(controlRequestSchema.safeParse({ id: "1", command: "runtime-identity" }).success).toBe(
  15. true,
  16. );
  17. expect(controlRequestSchema.safeParse({ id: "1", command: "kill-pid", pid: 4 }).success).toBe(
  18. false,
  19. );
  20. expect(
  21. controlRequestSchema.safeParse({
  22. id: "1",
  23. command: "confirm-uninstall",
  24. confirmation: "DELETE",
  25. }).success,
  26. ).toBe(true);
  27. });
  28. it("keeps runtime identity separate from the persisted status contract", () => {
  29. expect(
  30. runtimeIdentitySchema.parse({
  31. supervisorVersion: "3.9.0",
  32. supervisorReleaseId: "release-b",
  33. activeReleaseVersion: "3.9.0",
  34. activeReleaseId: "release-b",
  35. coreVersion: "3.9.0",
  36. coreReleaseId: "release-b",
  37. }),
  38. ).toMatchObject({ supervisorReleaseId: "release-b", coreReleaseId: "release-b" });
  39. expect(
  40. serverStatusSchema.safeParse({
  41. state: "ready",
  42. pid: 42,
  43. port: 38123,
  44. host: "127.0.0.1",
  45. version: "3.9.0",
  46. generation: 1,
  47. serviceRegistered: false,
  48. runningTaskCount: 0,
  49. crashBudget: { crashCount: 0, nextRestartDelayMs: 0, exhausted: false },
  50. updatedAt: 2,
  51. supervisorReleaseId: "release-b",
  52. }).success,
  53. ).toBe(false);
  54. });
  55. it("accepts a ready status inside a control response", () => {
  56. const parsed = serverStatusSchema.parse({
  57. state: "ready",
  58. pid: 42,
  59. port: 38123,
  60. host: "127.0.0.1",
  61. version: "3.8.0",
  62. generation: 1,
  63. serviceRegistered: false,
  64. runningTaskCount: 0,
  65. crashBudget: { windowStartedAt: 1, crashCount: 0, nextRestartDelayMs: 0, exhausted: false },
  66. updatedAt: 2,
  67. });
  68. expect(controlResponseSchema.parse({ id: "1", ok: true, result: parsed }).ok).toBe(true);
  69. });
  70. it("rejects release versions that could escape the releases directory", () => {
  71. expect(
  72. releaseManifestSchema.safeParse({ version: "../escape", releaseDir: "/tmp/release" }).success,
  73. ).toBe(false);
  74. expect(
  75. releaseCatalogSchema.safeParse({
  76. schemaVersion: 1,
  77. releases: [
  78. {
  79. version: "3.8.0/evil",
  80. target: "linux-x64",
  81. archiveUrl: "https://example.test/release.tar.gz",
  82. archiveSha256: "a".repeat(64),
  83. },
  84. ],
  85. }).success,
  86. ).toBe(false);
  87. });
  88. it("builds a complete stopped snapshot for offline CLI fallbacks", () => {
  89. const status = createStoppedServerStatus("3.8.0", { serviceRegistered: true, now: 42 });
  90. expect(status).toEqual(
  91. expect.objectContaining({
  92. state: "stopped",
  93. pid: null,
  94. port: null,
  95. host: null,
  96. version: "3.8.0",
  97. generation: 0,
  98. startedAt: null,
  99. lastExitReason: null,
  100. serviceRegistered: true,
  101. runningTaskCount: 0,
  102. updatedAt: 42,
  103. }),
  104. );
  105. expect(serverStatusSchema.parse(status)).toEqual(status);
  106. });
  107. });