// File cards live beside the prose, never in it. Daemon E2E cannot reach the // browser's admission budgets, image tile GC, note pop-out or FileReader // results before submission. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; const source = stripTypeScriptTypes( ( await readFile( new URL('../src/lib/chat/attachments.ts', import.meta.url), 'utf8', ) ).replace(/^import .*;$/gm, ''), ); const { chooseAttachment, collectAttachments, readFile: readAttachedFile, splitForNote, } = await import( `data:text/javascript;base64,${Buffer.from(source).toString('base64')}` ); test('image and file slots have independent caps while both kinds share the message', () => { const images = new Map( Array.from({ length: 10 }, (_, i) => [ i + 1, { mime_type: 'image/png', data: 'YQ==', preview: '' }, ]), ); const files = new Map( Array.from({ length: 10 }, (_, i) => [ i + 1, { name: `note-${i}.txt`, data: 'YQ==', size: 1 }, ]), ); const imageSelection = collectAttachments( [...images.keys()].map((n) => `[Image #${n}]`).join(' '), images, new Map(), ); const fileSelection = collectAttachments('', new Map(), files); const picture = { name: 'new.png', type: 'image/png', size: 1 }; const document = { name: 'new.txt', type: 'text/plain', size: 1 }; assert.ok('problem' in chooseAttachment(picture, imageSelection)); assert.equal(chooseAttachment(document, imageSelection).kind, 'file'); assert.ok('problem' in chooseAttachment(document, fileSelection)); assert.equal(chooseAttachment(picture, fileSelection).kind, 'image'); }); test('the combined byte budget counts padded legacy image bytes exactly', () => { const images = new Map([ [1, { mime_type: 'image/png', data: 'YQ==', preview: '' }], [2, { mime_type: 'image/png', data: 'YWI=', preview: '' }], [3, { mime_type: 'image/png', data: 'YWJj', preview: '' }], ]); const files = new Map([ [4, { name: 'big.bin', data: '', size: 20 * 1024 * 1024 - 7 }], ]); const selected = collectAttachments( '[Image #1] [Image #2] [Image #3]', images, files, ); const next = { name: 'next.bin', type: '', size: 1 }; assert.equal( chooseAttachment(next, selected).kind, 'file', 'one byte still fits alongside padded image payloads', ); assert.ok('problem' in chooseAttachment({ ...next, size: 2 }, selected)); const full = { ...selected, bytes: selected.bytes + 1 }; assert.equal( chooseAttachment({ ...next, size: 0 }, full).kind, 'file', 'a zero-byte file fits at the exact budget', ); assert.ok('problem' in chooseAttachment(next, full)); }); test('removing an image tile frees its bytes while clearing prose keeps file cards', () => { const image = { mime_type: 'image/png', data: 'YQ==', preview: 'kept' }; const file = { name: 'keep.txt', data: 'Yg==', size: 1 }; const images = new Map([ [3, image], [4, { ...image, size: 20 * 1024 * 1024 }], ]); const files = new Map([[8, file]]); const selected = collectAttachments('[Image #3]', images, files); assert.deepEqual([...images], [[3, image]]); assert.deepEqual([...files], [[8, file]]); assert.deepEqual(selected, { imageCount: 1, fileCount: 1, bytes: 2 }); assert.equal( chooseAttachment( { name: 'room.bin', type: '', size: 20 * 1024 * 1024 - 2 }, selected, ).kind, 'file', ); assert.deepEqual(collectAttachments('', images, files), { imageCount: 0, fileCount: 1, bytes: 1, }); files.delete(8); assert.deepEqual(collectAttachments('', images, files), { imageCount: 0, fileCount: 0, bytes: 0, }); }); test('remote chats refuse generic files before reading while still accepting images', () => { const selected = collectAttachments('', new Map(), new Map()); assert.ok( 'problem' in chooseAttachment( { name: 'notes.txt', type: 'text/plain', size: 12 }, selected, true, ), ); assert.equal( chooseAttachment( { name: 'look.png', type: 'image/png', size: 12 }, selected, true, ).kind, 'image', ); assert.equal( chooseAttachment( { name: 'notes.txt', type: 'text/plain', size: 12 }, selected, ).kind, 'file', ); }); test('plain filenames use the daemon UTF-8 byte boundary before any read', () => { const selected = collectAttachments('', new Map(), new Map()); const file = { name: 'é'.repeat(125) + '.txt', type: 'text/plain', size: 1 }; assert.equal(chooseAttachment(file, selected).kind, 'file'); for (const name of [ 'é'.repeat(128), '../notes.txt', 'bad\nname.txt', '', '.', '..', ]) { assert.ok('problem' in chooseAttachment({ ...file, name }, selected), name); } }); test('popping prose into a note keeps marked images in the composer and every literal label in the note', () => { const image = { mime_type: 'image/png', data: 'YQ==', preview: '' }; const images = new Map([[5, image]]); const moved = splitForNote( 'compare [Image #5] with [File #1: example.txt] and [Image #99]', images, ); assert.deepEqual(moved, { note: 'compare with [File #1: example.txt] and [Image #99]', text: '[Image #5] ', }); assert.equal(images.get(5), image); }); test('reader results preserve empty files and reject malformed or incomplete payloads before send', async () => { const original = globalThis.FileReader; let result; globalThis.FileReader = class { readAsDataURL() { this.result = result; queueMicrotask(() => this.onload()); } }; try { const file = { name: 'LICENSE', type: '', size: 0 }; for (result of ['data:application/octet-stream;base64,', 'data:']) { assert.deepEqual(await readAttachedFile(file), { name: 'LICENSE', data: '', size: 0, }); } result = 'data:application/octet-stream;base64,YQ=='; assert.equal((await readAttachedFile({ ...file, size: 1 })).data, 'YQ=='); for (result of [ null, 'data:', 'data:application/octet-stream;base64', 'data:text/plain;base64,not base64', 'data:text/plain;base64,YWI=', ]) { await assert.rejects(readAttachedFile({ ...file, size: 1 })); } } finally { globalThis.FileReader = original; } });