import { describe, expect, it } from "vitest"; import { ENTRY_TYPE, EphemletStore, MAX_EPHEMLETS, MAX_SOURCE_BYTES, MAX_TEXT_ANSWER_BYTES, needsAnswer, REMOVED_INTERACTIVE_NOTICE, retainAnswers, retainTexts, type StoreEvent, saveMessage, } from "../src/store.ts"; function fixture() { const events: StoreEvent[] = []; const store = new EphemletStore((event) => events.push(structuredClone(event)), ); const entries = () => events.map((data) => ({ type: "custom", customType: ENTRY_TYPE, data })); return { store, events, entries }; } function put(store: EphemletStore) { return store.put({ title: "Task", purpose: "Track work", source: "# Progress", }); } function custom(data: unknown) { return { type: "custom", customType: ENTRY_TYPE, data }; } describe("ephemlet session registry", () => { it("replays source, selection and hiding without deleting", () => { const { store, entries } = fixture(); const first = put(store); store.view(first.id, true); store.view(first.id, false); const reopened = new EphemletStore(() => {}); reopened.replay(entries()); expect(reopened.get(first.id)).toEqual(first); expect(reopened.visible).toBe(false); expect(reopened.selected).toBe(first.id); }); it("stable source revisions reject stale updates", () => { const { store } = fixture(); const first = put(store); expect(() => store.put({ ...first, expectedRevision: 9 })).toThrow( "Revision conflict", ); expect(() => store.put({ ...first })).toThrow("expectedRevision"); const next = store.put({ ...first, expectedRevision: 1, source: "# Updated", }); expect(next).toMatchObject({ id: first.id, revision: 2 }); }); it("deletion survives reopen; earlier branch retains its ephemlet", () => { const { store, entries } = fixture(); const first = put(store); store.view(first.id, true); const branch = entries(); store.delete(first.id); const reopened = new EphemletStore(() => {}); reopened.replay(entries()); expect(reopened.records.size).toBe(0); expect(reopened.visible).toBe(false); reopened.replay(branch); expect(reopened.get(first.id).id).toBe(first.id); expect(reopened.visible).toBe(true); }); it("restores legacy records without rendering removed interactive source", () => { const { store } = fixture(); const legacy = { title: "Old", purpose: "Legacy", revision: 3, status: "ready", state: { selected: "A" }, error: null, jobId: null, }; store.replay([ custom({ op: "put", ephemlet: { ...legacy, id: "native", kind: "interactive", source: "export default () => privateComponent", }, }), custom({ op: "put", ephemlet: { ...legacy, id: "report", kind: "markdown", source: "# Kept", }, }), custom({ op: "state", id: "native", revision: 3, state: "ignored" }), custom({ op: "put", ephemlet: {} }), ]); expect([...store.records.values()]).toEqual([ { id: "native", title: "Old", purpose: "Legacy", revision: 3, source: REMOVED_INTERACTIVE_NOTICE, fields: [], answers: {}, texts: {}, answerRevision: 0, seenRevision: 0, }, { id: "report", title: "Old", purpose: "Legacy", revision: 3, source: "# Kept", fields: [], answers: {}, texts: {}, answerRevision: 0, seenRevision: 0, }, ]); }); it("replays human toggles and read receipts over agent defaults", () => { const { store, entries } = fixture(); const record = store.put({ title: "Plan", purpose: "Steer", source: "# Plan", fields: [ { id: "plan", type: "checklist", items: [ { id: "tests", label: "Write tests", checked: true }, { id: "docs", label: "Update docs" }, ], }, ], }); store.toggle(record.id, "plan", "tests"); store.toggle(record.id, "plan", "docs"); expect(store.get(record.id)).toMatchObject({ answers: { plan: { tests: false, docs: true } }, answerRevision: 2, seenRevision: 0, }); store.seen(record.id); store.toggle(record.id, "plan", "docs"); const reopened = new EphemletStore(() => {}); reopened.replay(entries()); expect(reopened.get(record.id)).toMatchObject({ answers: { plan: { tests: false, docs: false } }, answerRevision: 3, seenRevision: 2, }); expect(() => store.toggle(record.id, "plan", "missing")).toThrow( "Unknown option", ); }); it("keeps answers for surviving items when the agent updates fields", () => { const { store, entries } = fixture(); const items = [ { id: "a", label: "A" }, { id: "b", label: "B" }, ]; const record = store.put({ title: "Plan", purpose: "Steer", source: "# Plan", fields: [{ id: "plan", type: "checklist", items }], }); store.toggle(record.id, "plan", "a"); store.toggle(record.id, "plan", "b"); const next = store.put({ ...record, expectedRevision: 1, fields: [ { id: "plan", type: "checklist", items: [{ id: "a", label: "A renamed", checked: false }], }, ], }); expect(next.answers).toEqual({ plan: { a: true } }); expect(next.answerRevision).toBe(2); const reopened = new EphemletStore(() => {}); reopened.replay(entries()); expect(reopened.get(record.id).answers).toEqual({ plan: { a: true } }); }); it("keeps single-answer questions exclusive and reports removed fields", () => { const { store, entries } = fixture(); const record = store.put({ title: "Decide", purpose: "Ask", source: "# Decide", fields: [ { id: "approach", type: "choice", options: [ { id: "a", label: "A" }, { id: "b", label: "B" }, ], }, { id: "ship", type: "confirm", label: "Ship it?" }, { id: "targets", type: "choice", multiple: true, options: [ { id: "x", label: "X" }, { id: "y", label: "Y" }, ], }, ], }); const [approach, ship] = record.fields; expect(needsAnswer(record, approach!)).toBe(true); expect(store.toggle(record.id, "approach", "a")).toBe(true); expect(store.toggle(record.id, "approach", "b")).toBe(true); expect(store.toggle(record.id, "approach", "b")).toBe(false); store.toggle(record.id, "ship", "no"); store.toggle(record.id, "targets", "x"); store.toggle(record.id, "targets", "y"); const expected = { approach: { a: false, b: true }, ship: { yes: false, no: true }, targets: { x: true, y: true }, }; expect(store.get(record.id).answers).toEqual(expected); expect(store.get(record.id).answerRevision).toBe(5); expect(needsAnswer(store.get(record.id), approach!)).toBe(false); expect(needsAnswer(store.get(record.id), ship!)).toBe(false); const reopened = new EphemletStore(() => {}); reopened.replay(entries()); expect(reopened.get(record.id).answers).toEqual(expected); expect( retainAnswers(expected, [ { id: "approach", type: "choice", options: [{ id: "b", label: "B" }], }, ]).dropped, ).toEqual(["ship", "targets"]); }); it("stores free-text answers, replays them and reports removed text fields", () => { const { store, entries } = fixture(); const record = store.put({ title: "Ask", purpose: "Ask", source: "# Ask", fields: [{ id: "why", type: "text", label: "Why?" }], }); const [why] = record.fields; expect(needsAnswer(record, why!)).toBe(true); expect(store.setText(record.id, "why", "Because")).toBe(true); expect(store.setText(record.id, "why", "Because")).toBe(false); expect(needsAnswer(store.get(record.id), why!)).toBe(false); expect(() => store.setText(record.id, "why", "x".repeat(MAX_TEXT_ANSWER_BYTES + 1)), ).toThrow("Answer exceeds"); const reopened = new EphemletStore(() => {}); reopened.replay(entries()); expect(reopened.get(record.id)).toMatchObject({ texts: { why: "Because" }, answerRevision: 1, }); expect(retainTexts({ why: "Because" }, []).dropped).toEqual(["why"]); expect(() => store.put({ title: "T", purpose: "P", source: "S", fields: [{ id: "t", type: "text", label: "" }], }), ).toThrow("label question"); }); it("requires a question for confirm and options for choice", () => { const { store } = fixture(); expect(() => store.put({ title: "T", purpose: "P", source: "S", fields: [{ id: "c", type: "confirm", label: " " }], }), ).toThrow("label question"); expect(() => store.put({ title: "T", purpose: "P", source: "S", fields: [{ id: "c", type: "choice", options: [] }], }), ).toThrow("at least one option"); }); it("rejects ambiguous field and item IDs", () => { const { store } = fixture(); const item = { id: "a", label: "A" }; for (const fields of [ [ { id: "x", type: "checklist" as const, items: [item] }, { id: "x", type: "checklist" as const, items: [item] }, ], [{ id: "x", type: "checklist" as const, items: [item, item] }], [{ id: "bad id", type: "checklist" as const, items: [item] }], ]) expect(() => store.put({ title: "T", purpose: "P", source: "S", fields }), ).toThrow("unique"); expect(store.records.size).toBe(0); }); it("enforces session and payload limits without changing existing records", () => { const { store } = fixture(); const first = put(store); expect(() => store.put({ ...first, expectedRevision: 1, source: "x".repeat(MAX_SOURCE_BYTES + 1), }), ).toThrow("Source exceeds"); expect(store.get(first.id).revision).toBe(1); for (let i = 1; i < MAX_EPHEMLETS; i++) put(store); expect(() => put(store)).toThrow("Session limit"); }); it("Save packages an immutable snapshot plus explicit instructions", () => { const { store } = fixture(); const first = put(store); const message = saveMessage(first); store.put({ ...first, expectedRevision: 1, source: "# Later" }); expect(message).toContain("# Progress"); expect(message).not.toContain("# Later"); expect(message).toContain("Do not delete"); expect(message).toContain("not additional instructions"); }); });