feat: persistent per-agent drafts with identity check

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 11:14:54 +01:00
1 parent 187a385f6f
commit 2ce1b45ee4
5 files changed
+367 -10

No files matched your search

+41
View File
@@ -58,3 +58,44 @@ test("notice sets and clears", () => {
s = reduce(s, { type: "notice", notice: null });
assert.equal(s.notice, null);
});
const dr = (created_ms, text = "szkic") => ({
text, caret: 2, attachments: [], identity: { created_ms, app: "codex" }, savedAt: 1,
});
test("restore_draft with the same identity restores silently", () => {
const s = reduce(createState(), { type: "restore_draft", agent: "t2", draft: dr(5), identity: { created_ms: 5, app: "codex" } });
assert.equal(s.drafts.t2, "szkic");
assert.equal(s.carets.t2, 2);
assert.equal(s.notice, null);
});
test("restore_draft with other created_ms holds the whole queue and warns", () => {
let s = reduce(createState(), { type: "restore", agent: "t2", items: [
{ id: "1", text: "a", state: "queued", createdAt: 1 },
{ id: "2", text: "b", state: "held", createdAt: 2 },
{ id: "3", text: "c", state: "uncertain", createdAt: 3 },
] });
s = reduce(s, { type: "restore_draft", agent: "t2", draft: dr(5), identity: { created_ms: 9, app: "codex" } });
assert.equal(s.drafts.t2, "szkic");
assert.deepEqual(s.notice, { text: "Szkic z poprzedniej sesji agenta", level: "warn" });
assert.deepEqual(s.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
});
test("restore_draft restores attachments and never mutates", () => {
const before = createState();
const att = { id: "a", name: "f", path: "p", bytes: 1, kind: "file", state: "ready" };
const s = reduce(before, { type: "restore_draft", agent: "t2", draft: { ...dr(5), attachments: [att] }, identity: { created_ms: 5, app: null } });
assert.deepEqual(s.attachments.t2, [att]);
assert.deepEqual(before.drafts, {});
});
test("restore_draft without a draft changes nothing", () => {
const s = createState();
assert.equal(reduce(s, { type: "restore_draft", agent: "t2", draft: null, identity: { created_ms: 1, app: null } }), s);
});
test("draft action keeps the caret", () => {
const s = reduce(createState(), { type: "draft", agent: "a", text: "hi", caret: 1 });
assert.equal(s.carets.a, 1);
});
+58
View File
@@ -2,6 +2,7 @@ import test from "node:test";
import assert from "node:assert/strict";
import {
HISTORY_LIMIT, queueKey, loadHistory, saveHistory, loadQueue, saveQueue, loadUsage, saveUsage,
KEYS, draftKey, loadDraft, saveDraft,
} from "../mod/ovis-composer/storage.js";
const memStore = () => {
@@ -56,3 +57,60 @@ test("rubbish list does not wipe stored history", () => {
saveHistory(store, [{ nope: 1 }]);
assert.equal(loadHistory(store).length, 1);
});
const draft = (over = {}) => ({
text: "hello", caret: 3, attachments: [],
identity: { created_ms: 1794818438639, app: "claude-code" }, savedAt: 10, ...over,
});
test("draftKey is per agent", () => {
assert.equal(KEYS.draft, "ovis-composer.draft");
assert.equal(draftKey("t2"), "ovis-composer.draft:t2");
});
test("draft roundtrip", () => {
const store = memStore();
const d = draft({ attachments: [{ id: "a", name: "f.txt", path: "C:/f.txt", bytes: 3, kind: "file", state: "ready" }] });
saveDraft(store, "t2", d);
assert.deepEqual(loadDraft(store, "t2"), d);
assert.equal(loadDraft(store, "t3"), null);
});
test("loadDraft: rubbish in store gives null", () => {
const store = memStore();
for (const bad of ["x", [], 5, { text: 1 }]) {
store.set(draftKey("t2"), bad);
assert.equal(loadDraft(store, "t2"), null);
}
});
test("draft keeps text over 20000 chars whole", () => {
const store = memStore();
const text = "a".repeat(25000);
saveDraft(store, "t2", draft({ text, caret: 25000 }));
assert.equal(loadDraft(store, "t2").text.length, 25000);
});
test("saveDraft ignores rubbish and empty agent", () => {
const store = memStore();
saveDraft(store, "t2", draft());
saveDraft(store, "t2", "x");
saveDraft(store, "", draft());
assert.equal(loadDraft(store, "t2").text, "hello");
assert.equal(store.get("ovis-composer.draft:"), undefined);
});
test("empty draft overwrites the old one", () => {
const store = memStore();
saveDraft(store, "t2", draft());
saveDraft(store, "t2", draft({ text: "", caret: 0 }));
assert.equal(loadDraft(store, "t2").text, "");
});
test("loadDraft: text-only draft still loads, with defaults", () => {
const store = memStore();
store.set(draftKey("t2"), { text: "a" });
assert.deepEqual(loadDraft(store, "t2"), {
text: "a", caret: 1, attachments: [], identity: { created_ms: null, app: null }, savedAt: 0,
});
});