Files
KrisHD_AI/test/draftStore.test.js
2026-10-06 00:32:18 +02:00

34 lines
1.9 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {createDraftStore} from "../public/js/draftStore.js";
test("drafts recover text and uploaded references per account and conversation", () => {
const values = new Map();
const storage = {getItem: key => values.get(key), setItem: (key, value) => values.set(key, value)};
const alice = createDraftStore("alice", {storage});
const metadata = {id: "12345678-1234-1234-1234-123456789abc", originalName: "image.png"};
alice.textDrafts.set(1, "Unsent text"); alice.textDrafts.set("new", "New conversation draft");
alice.setAttachments(1, [metadata]);
const recovered = createDraftStore("alice", {storage});
assert.equal(recovered.textDrafts.get(1), "Unsent text");
assert.equal(recovered.textDrafts.get("new"), "New conversation draft");
assert.deepEqual(recovered.attachments(1), [metadata]);
assert.equal(createDraftStore("bob", {storage}).textDrafts.size, 0);
assert.deepEqual(createDraftStore("bob", {storage}).attachments(1), []);
recovered.textDrafts.delete(1);
assert.deepEqual(createDraftStore("alice", {storage}).attachments(1), [metadata]);
recovered.setAttachments(1, []);
const sent = createDraftStore("alice", {storage});
assert.equal(sent.textDrafts.has(1), false);
assert.deepEqual(sent.attachments(1), []);
assert.equal(sent.textDrafts.get("new"), "New conversation draft");
});
test("unavailable storage reports recovery failure once while keeping drafts usable", () => {
const notices = [];
const drafts = createDraftStore("alice", {storage: {getItem() { throw new Error("Blocked"); }, setItem() { throw new Error("Full"); }}, onError: message => notices.push(message)});
drafts.textDrafts.set(1, "Still here"); drafts.setAttachments(1, []);
assert.equal(drafts.textDrafts.get(1), "Still here");
assert.equal(notices.length, 1);
});