import { describe, expect, it } from "vitest"; import { canStartServer, canStopServer, runtimeObservationFreshness } from "./serverManagement"; import type { RunEndpointResponse, ServerInstanceResponse, ServerInstanceState } from "../api/types"; describe("server management lifecycle contracts", () => { it("allows explicit starts from recoverable non-running states", () => { const startable: ServerInstanceState[] = ["ready", "stopped", "failed"]; const blocked: ServerInstanceState[] = ["draft", "installing", "running", "deleted"]; expect(startable.every(canStartServer)).toBe(true); expect(blocked.some(canStartServer)).toBe(false); }); it("keeps stop limited to running servers", () => { expect(canStopServer("running")).toBe(true); expect(canStopServer("failed")).toBe(false); }); it("distinguishes a fresh Run observation from an unverified historical lifecycle state", () => { const instance = { id: "server-1", state: "running", runEndpointId: "run-1" } as ServerInstanceResponse; const endpoint = { id: "run-1", status: "online", lastHeartbeatAt: "2026-08-07T10:00:00Z" } as RunEndpointResponse; expect(runtimeObservationFreshness(instance, endpoint, Date.parse("2026-08-07T10:00:30Z"))).toBe("fresh"); expect(runtimeObservationFreshness(instance, endpoint, Date.parse("2026-08-07T10:01:00Z"))).toBe("unverified"); expect(runtimeObservationFreshness(instance, { ...endpoint, status: "offline" }, Date.parse("2026-08-07T10:00:01Z"))).toBe("unverified"); }); });