// A draft outlives its composer, and a reload: written to storage once typing // pauses, read back by the next page, dropped when emptied. Pending attachment // reads must also outlive composer unmount/remount, which daemon E2E cannot reach. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; async function load(path, prelude = '') { const source = stripTypeScriptTypes( (await readFile(new URL(path, import.meta.url), 'utf8')).replace( /^import [\s\S]*?;$/gm, '', ), ); return import( `data:text/javascript;base64,${Buffer.from(prelude + source).toString('base64')}` ); } const store = new Map(); globalThis.localStorage = { getItem: (key) => store.get(key) ?? null, setItem: (key, value) => store.set(key, String(value)), removeItem: (key) => store.delete(key), }; globalThis.__drafts = await load('../src/lib/chat/attachments.ts'); // Each call is a page load: a module of its own, with the same storage. let pages = 0; const page = () => load( '../src/lib/chat/drafts.ts', `const SvelteMap = Map; const { attachmentProblem, attachmentSelection, withoutMarkers, remapMarkers } = globalThis.__drafts; /* page ${++pages} */\n`, ); test('cancelled mixed inputs restore after newer edits; typed file labels stay words through reload', async () => { const { draftOf } = await page(); const draft = draftOf('cancelled-mixed'); const active = { name: 'new.txt', data: 'bmV3', size: 3 }; const image = { mime_type: 'image/png', data: 'AA==', preview: 'data:image/png;base64,AA==', }; const file = { name: 'old.txt', data: 'AP8=', size: 2 }; draft.files.set(7, active); draft.next = 7; draft.text = 'newer thought'; const shown = []; draft.show = (text) => shown.push(text); draft.restore([ { text: 'first [Image #1] explain [File #1: example.txt] and [Image #2]', images: [image], files: [file], }, { text: '', images: [], files: [file] }, ]); assert.equal( draft.text, 'newer thought\n\nfirst [Image #8] explain [File #1: example.txt] and [Image #2]', ); assert.equal(draft.files.get(7), active); assert.equal(draft.images.get(8), image); assert.equal(draft.files.get(9), file); assert.equal(draft.files.get(10).data, 'AP8='); assert.deepEqual( shown, [draft.text], 'upstream restore displays the complete draft', ); draft.save(); assert.equal( (await page()).draftOf('cancelled-mixed').text, 'newer thought\n\nfirst explain [File #1: example.txt] and', ); }); test('restoring more than one message can carry keeps every byte and holds Send until some are removed', async () => { const { draftOf } = await page(); const draft = draftOf('cancelled-overflow'); const six = (prefix) => Array.from({ length: 6 }, (_, i) => ({ name: `${prefix}-${i}.txt`, data: 'YQ==', size: 1, })); draft.restore([ { text: 'one', images: [], files: six('a') }, { text: 'two', images: [], files: six('b') }, ]); assert.equal(draft.files.size, 12); assert.match(draft.problem, /12 files/); for (const n of [...draft.files.keys()].slice(10)) draft.files.delete(n); assert.equal(draft.problem, null); // An image restored without browser metadata counts its decoded bytes. const mixed = draftOf('cancelled-bytes'); const big = { name: 'big.bin', data: '', size: 20 * 1024 * 1024 - 1 }; const image = { mime_type: 'image/png', data: 'YWI=', preview: '' }; mixed.restore([ { text: 'see [Image #1]', images: [image], files: [] }, { text: 'and this', images: [], files: [big] }, ]); assert.match(mixed.problem, /20 MiB/); mixed.text = mixed.text.replace('[Image #1]', ''); assert.equal(mixed.problem, null, 'an unmarked image is not sent'); }); test('attachment reads, messages and errors follow a draft across composer replacements', async () => { const { draftOf } = await page(); const draft = draftOf('reading-files'); draft.text = 'first thought'; let release; const gate = new Promise((resolve) => { release = resolve; }); const events = []; const old = []; const unsubscribe = draft.subscribe(() => old.push(draft.text)); const reading = draft.readAttachments(async () => { events.push('first read'); await gate; draft.files.set(1, { name: 'notes.txt', data: 'YQ==', size: 1 }); }); const failed = draft.readAttachments(async () => { events.push('second read'); draft.notice = "couldn't read the other file"; }); assert.equal(draft.reading, 2); unsubscribe(); const oldCount = old.length; const replacement = draftOf('reading-files'); const seen = []; const leave = replacement.subscribe(() => seen.push({ text: replacement.text, reading: replacement.reading, notice: replacement.notice, }), ); replacement.text = 'edited after returning'; await Promise.resolve(); assert.deepEqual(events, ['first read']); release(); await Promise.all([reading, failed]); assert.deepEqual(events, ['first read', 'second read']); assert.equal(replacement.text, 'edited after returning'); assert.equal(replacement.files.get(1).size, 1); assert.equal(replacement.reading, 0); assert.deepEqual(seen.at(-1), { text: replacement.text, reading: 0, notice: "couldn't read the other file", }); assert.equal( old.length, oldCount, 'the destroyed composer no longer receives updates', ); leave(); });