import { describe, expect, it } from "vitest"; import type { WorkbenchEvent } from "../api/workbench-sync-client"; import { shouldApplyWorkbenchSnapshot, shouldRefreshWorkbenchRuntime } from "./use-workbench-sync"; describe("workbench snapshot ordering", () => { it("rejects an older concurrent response even when its cursor is valid", () => { expect(shouldApplyWorkbenchSnapshot({ requestGeneration: 1, latestRequestGeneration: 2, eventCursor: 120, currentCursor: 100 })).toBe(false); }); it("rejects a snapshot whose cursor trails already consumed events", () => { expect(shouldApplyWorkbenchSnapshot({ requestGeneration: 2, latestRequestGeneration: 2, eventCursor: 99, currentCursor: 100 })).toBe(false); }); it("accepts only the latest response without cursor rollback", () => { expect(shouldApplyWorkbenchSnapshot({ requestGeneration: 2, latestRequestGeneration: 2, eventCursor: 100, currentCursor: 100 })).toBe(true); }); }); describe("work-order projection readiness", () => { const event: WorkbenchEvent = { schema: "workbench-event@1", event_seq: 1, event_id: "324ff7ee-c2d4-5184-a84b-48fb14a2933e", project_id: "324ff7ee-c2d4-5184-a84b-48fb14a2933e", topic: "workbench.work_order.changed", aggregate_type: "peripheral_work_order", aggregate_id: "source-order", aggregate_revision: 1, occurred_at: "2026-10-08T00:00:00Z", metadata: { source: "peripheral_sample" } }; it("waits for authoritative projection events instead of fetching a premature snapshot", () => { expect(shouldRefreshWorkbenchRuntime(event)).toBe(false); expect(shouldRefreshWorkbenchRuntime({ ...event, aggregate_type: "operational_action" })).toBe(true); expect(shouldRefreshWorkbenchRuntime({ ...event, topic: "workbench.alert.changed", aggregate_type: "workbench_alert" })).toBe(true); }); });