import { afterEach, describe, expect, it, vi } from "vitest"; import { ComposerDraftStore, clearStoredComposerDrafts, type ComposerDraft } from "@/lib/composer-draft"; const key = "websocket:draft-storage-test"; const storageKey = `nanobot.composer-draft.v1.${key}`; const draft: ComposerDraft = { text: " Keep this draft\n继续编辑", quotedContext: "Quoted answer", sessionMentions: [{ name: "other", title: "Other topic", session_key: "websocket:other" }], attachments: [new File(["attachment"], "draft.txt", { type: "text/plain" })], }; afterEach(() => { vi.restoreAllMocks(); clearStoredComposerDrafts(); }); describe("composer draft storage", () => { it("restores text, mentions and quotes in a fresh store while keeping files in memory", () => { const store = new ComposerDraftStore(); store.set(key, draft, true); expect(store.get(key)).toBe(draft); expect(new ComposerDraftStore().get(key, true)).toEqual({ ...draft, attachments: [] }); expect(new ComposerDraftStore().get("websocket:other", true)).toBeUndefined(); expect(JSON.parse(localStorage.getItem(storageKey)!).attachments).toEqual([]); }); it("keeps the identity of an unchanged restored draft but advances it for edits", () => { const store = new ComposerDraftStore(); store.set(key, draft, true); store.set(key, { ...draft, attachments: [...draft.attachments], sessionMentions: [...draft.sessionMentions] }, true); expect(store.get(key)).toBe(draft); const edited = { ...draft, text: "edited draft" }; store.set(key, edited, true); expect(store.get(key)).toBe(edited); const reverted = { ...draft }; store.set(key, reverted, true); expect(store.get(key)).toBe(reverted); }); it("keeps mixed attachment order in memory and the local subsequence after reload", () => { const first = { path: "/project/first.png", name: "first.png", size: 8, mime: "image/png", kind: "image" as const, url: "" }; const last = { ...first, path: "/project/last.png", name: "last.png" }; const mixed = { ...draft, attachments: [first, draft.attachments[0], last] }; const store = new ComposerDraftStore(); store.set(key, mixed, true); store.set(key, { ...mixed, attachments: [...mixed.attachments] }, true); expect(store.get(key)).toBe(mixed); expect(new ComposerDraftStore().get(key, true)?.attachments).toEqual([first, last]); const reordered = { ...mixed, attachments: [last, draft.attachments[0], first] }; store.set(key, reordered, true); expect(store.get(key)).toBe(reordered); expect(new ComposerDraftStore().get(key, true)?.attachments).toEqual([last, first]); }); it("reads local references already saved in v1 drafts", () => { const local = { path: "/project/previous.png", name: "previous.png", size: 8, mime: "image/png", kind: "image", url: "" }; localStorage.setItem(storageKey, JSON.stringify({ text: "previous draft", quotedContext: null, sessionMentions: [], localAttachments: [local] })); expect(new ComposerDraftStore().get(key, true)?.attachments).toEqual([local]); }); it("requires explicit opt-in for both saving and restoring", () => { const store = new ComposerDraftStore(); store.set(key, draft); expect(store.get(key)).toBe(draft); expect(localStorage.getItem(storageKey)).toBeNull(); store.set(key, draft, true); expect(new ComposerDraftStore().get(key, false)).toBeUndefined(); }); it("removes saved text when discarded, even if attachments remain", () => { const store = new ComposerDraftStore(); store.set(key, draft, true); store.set(key, { ...draft, text: "", quotedContext: null, sessionMentions: [] }, true); expect(store.get(key)?.attachments).toEqual(draft.attachments); expect(new ComposerDraftStore().get(key, true)).toBeUndefined(); }); it("deletes saved drafts without first loading them and clears only drafts on logout", () => { const store = new ComposerDraftStore(); store.set(key, draft, true); new ComposerDraftStore().delete(key); expect(new ComposerDraftStore().get(key, true)).toBeUndefined(); store.set(key, draft, true); store.set("new:chat", draft, true); localStorage.setItem("nanobot.test.other", "keep"); clearStoredComposerDrafts(); expect(new ComposerDraftStore().get(key, true)).toBeUndefined(); expect(new ComposerDraftStore().get("new:chat", true)).toBeUndefined(); expect(localStorage.getItem("nanobot.test.other")).toBe("keep"); localStorage.removeItem("nanobot.test.other"); }); it.each(["{", "null", '{"text":42}', '{"text":"draft","quotedContext":{},"sessionMentions":[]}'])( "ignores malformed saved drafts: %s", (raw) => { localStorage.setItem(storageKey, raw); expect(new ComposerDraftStore().get(key, true)).toBeUndefined(); }, ); it("drops malformed mention records without losing the draft text", () => { localStorage.setItem(storageKey, JSON.stringify({ ...draft, sessionMentions: [null, { name: 42 }, ...draft.sessionMentions], })); expect(new ComposerDraftStore().get(key, true)).toEqual({ ...draft, attachments: [] }); }); it("keeps editing usable when storage is unavailable and removes stale saved text", () => { const store = new ComposerDraftStore(); store.set(key, draft, true); vi.spyOn(localStorage, "setItem").mockImplementation(() => { throw new Error("quota"); }); const updated = { ...draft, text: "latest draft" }; store.set(key, updated, true); expect(store.get(key)).toBe(updated); expect(new ComposerDraftStore().get(key, true)).toBeUndefined(); vi.spyOn(localStorage, "getItem").mockImplementation(() => { throw new Error("disabled"); }); expect(new ComposerDraftStore().get(key, true)).toBeUndefined(); }); });