import { describe, expect, it } from "vitest"; import { EventStore } from "../events"; describe("ui event store", () => { it("assigns monotonic ids and timestamps", () => { const store = new EventStore({ maxEvents: 10, now: () => 1000 }); const first = store.append({ type: "state", state: "starting" }); const second = store.append({ type: "log", level: "info", message: "ready", }); expect(first).toMatchObject({ id: 1, at: 1000, type: "state" }); expect(second).toMatchObject({ id: 2, at: 1000, type: "log" }); }); it("keeps a bounded replay buffer", () => { const store = new EventStore({ maxEvents: 3, now: () => 1 }); for (let i = 0; i < 5; i++) { store.append({ type: "log", level: "info", message: `event ${i}` }); } expect(store.replayAfter(0).map((event) => event.id)).toEqual([3, 4, 5]); expect(store.replayAfter(3).map((event) => event.id)).toEqual([4, 5]); }); it("builds deterministic snapshots", () => { const store = new EventStore({ maxEvents: 10, now: () => 7 }); store.append({ type: "state", state: "starting" }); store.append({ type: "message", role: "user", text: "hello" }); expect( store.snapshot({ status: "connected", sessionPath: "C:/tmp/ui.jsonl", commands: [{ name: "ui", description: "browser ui" }], }), ).toEqual({ version: 1, lastEventId: 2, status: "connected", sessionPath: "C:/tmp/ui.jsonl", commands: [{ name: "ui", description: "browser ui" }], events: [ { id: 1, at: 7, type: "state", state: "starting" }, { id: 2, at: 7, type: "message", role: "user", text: "hello" }, ], }); }); });