| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { describe, expect, it } from "vitest";
- import {
- controlRequestSchema,
- controlResponseSchema,
- releaseCatalogSchema,
- releaseManifestSchema,
- runtimeIdentitySchema,
- serverStatusSchema,
- createStoppedServerStatus,
- } from "../src/contracts.js";
- describe("R2 control contract", () => {
- it("rejects unknown lifecycle commands and accepts JSONL-safe requests", () => {
- expect(controlRequestSchema.safeParse({ id: "1", command: "status" }).success).toBe(true);
- expect(controlRequestSchema.safeParse({ id: "1", command: "runtime-identity" }).success).toBe(
- true,
- );
- expect(controlRequestSchema.safeParse({ id: "1", command: "kill-pid", pid: 4 }).success).toBe(
- false,
- );
- expect(
- controlRequestSchema.safeParse({
- id: "1",
- command: "confirm-uninstall",
- confirmation: "DELETE",
- }).success,
- ).toBe(true);
- });
- it("keeps runtime identity separate from the persisted status contract", () => {
- expect(
- runtimeIdentitySchema.parse({
- supervisorVersion: "3.9.0",
- supervisorReleaseId: "release-b",
- activeReleaseVersion: "3.9.0",
- activeReleaseId: "release-b",
- coreVersion: "3.9.0",
- coreReleaseId: "release-b",
- }),
- ).toMatchObject({ supervisorReleaseId: "release-b", coreReleaseId: "release-b" });
- expect(
- serverStatusSchema.safeParse({
- state: "ready",
- pid: 42,
- port: 38123,
- host: "127.0.0.1",
- version: "3.9.0",
- generation: 1,
- serviceRegistered: false,
- runningTaskCount: 0,
- crashBudget: { crashCount: 0, nextRestartDelayMs: 0, exhausted: false },
- updatedAt: 2,
- supervisorReleaseId: "release-b",
- }).success,
- ).toBe(false);
- });
- it("accepts a ready status inside a control response", () => {
- const parsed = serverStatusSchema.parse({
- state: "ready",
- pid: 42,
- port: 38123,
- host: "127.0.0.1",
- version: "3.8.0",
- generation: 1,
- serviceRegistered: false,
- runningTaskCount: 0,
- crashBudget: { windowStartedAt: 1, crashCount: 0, nextRestartDelayMs: 0, exhausted: false },
- updatedAt: 2,
- });
- expect(controlResponseSchema.parse({ id: "1", ok: true, result: parsed }).ok).toBe(true);
- });
- it("rejects release versions that could escape the releases directory", () => {
- expect(
- releaseManifestSchema.safeParse({ version: "../escape", releaseDir: "/tmp/release" }).success,
- ).toBe(false);
- expect(
- releaseCatalogSchema.safeParse({
- schemaVersion: 1,
- releases: [
- {
- version: "3.8.0/evil",
- target: "linux-x64",
- archiveUrl: "https://example.test/release.tar.gz",
- archiveSha256: "a".repeat(64),
- },
- ],
- }).success,
- ).toBe(false);
- });
- it("builds a complete stopped snapshot for offline CLI fallbacks", () => {
- const status = createStoppedServerStatus("3.8.0", { serviceRegistered: true, now: 42 });
- expect(status).toEqual(
- expect.objectContaining({
- state: "stopped",
- pid: null,
- port: null,
- host: null,
- version: "3.8.0",
- generation: 0,
- startedAt: null,
- lastExitReason: null,
- serviceRegistered: true,
- runningTaskCount: 0,
- updatedAt: 42,
- }),
- );
- expect(serverStatusSchema.parse(status)).toEqual(status);
- });
- });
|