const test = require('node:test'); const assert = require('node:assert/strict'); const { createAttachmentService } = require('../desktop/mail/attachment-service.cjs'); function setup({ mailboxPath = 'INBOX', item = { source: Buffer.from('message') }, attachments = [] } = {}) { const calls = { loggedOut: 0, locksReleased: 0, saved: [], dialogOptions: [], parsedSources: [] }; const client = { usable: true, async connect() {}, async getMailboxLock(path) { assert.equal(path, mailboxPath); return { release() { calls.locksReleased += 1; } }; }, async fetchOne(uid, query, options) { assert.equal(uid, 4); assert.deepEqual(query, { uid: true, source: true }); assert.deepEqual(options, { uid: true }); return item; }, async logout() { calls.loggedOut += 1; } }; const service = createAttachmentService({ imapClient() { return client; }, async folderPath(_client, folder) { return folder === 'Missing' ? null : mailboxPath; }, async parseForAttachments(source) { calls.parsedSources.push(source); return { attachments }; }, async writeFile(filePath, content) { calls.saved.push({ filePath, content }); } }); return { service, calls }; } test('saves the requested attachment using a sanitized dialog default path', async () => { const bytes = Buffer.from('contents'); const { service, calls } = setup({ attachments: [{ filename: 'reports/annual:2026.txt', content: bytes }] }); const result = await service.saveAttachment({ id: 'account-1' }, { uid: 4, folder: 'Inbox', filename: 'reports/annual:2026.txt' }, async options => { calls.dialogOptions.push(options); return { canceled: false, filePath: 'C:/exports/report.txt' }; }); assert.deepEqual(result, { saved: true }); assert.deepEqual(calls.dialogOptions, [{ defaultPath: 'reports_annual_2026.txt' }]); assert.equal(calls.saved[0].filePath, 'C:/exports/report.txt'); assert.deepEqual(calls.saved[0].content, bytes); assert.equal(calls.locksReleased, 1); assert.equal(calls.loggedOut, 1); }); test('cancellation does not write attachment bytes and save falls back to the first attachment', async () => { const { service, calls } = setup({ attachments: [{ filename: 'first.txt', content: Buffer.from('first') }] }); const result = await service.saveAttachment({ id: 'account-1' }, { uid: 4 }, async options => { calls.dialogOptions.push(options); return { canceled: true }; }); assert.deepEqual(result, { canceled: true }); assert.deepEqual(calls.dialogOptions, [{ defaultPath: 'first.txt' }]); assert.equal(calls.saved.length, 0); }); test('previews supported image and text attachments with explicit size bounds', async () => { const imageBytes = Buffer.from([1, 2, 3]); const imageService = setup({ attachments: [{ filename: 'image.png', contentType: 'image/png', content: imageBytes }] }).service; assert.deepEqual(await imageService.previewAttachment({}, { uid: 4, filename: 'image.png' }), { kind: 'image', dataUrl: `data:image/png;base64,${imageBytes.toString('base64')}` }); const largeImage = setup({ attachments: [{ filename: 'large.png', contentType: 'image/png', content: Buffer.alloc(5 * 1048576 + 1) }] }).service; assert.deepEqual(await largeImage.previewAttachment({}, { uid: 4, filename: 'large.png' }), { kind: 'none', reason: 'This image is too large to preview. Download it instead.' }); const textBytes = Buffer.from('hello'); const textService = setup({ attachments: [{ filename: 'note.txt', contentType: 'text/plain; charset=utf-8', content: textBytes }] }).service; assert.deepEqual(await textService.previewAttachment({}, { uid: 4, filename: 'note.txt' }), { kind: 'text', text: 'hello', truncated: false }); }); test('rejects invalid identifiers, missing messages, folders, and attachment names', async () => { const { service, calls } = setup({ item: null }); await assert.rejects(() => service.previewAttachment({}, { uid: 0 }), /could not be identified/); await assert.rejects(() => service.previewAttachment({}, { uid: 4 }), /no longer available/); assert.equal(calls.loggedOut, 1); const missingFolder = setup().service; await assert.rejects(() => missingFolder.previewAttachment({}, { uid: 4, folder: 'Missing' }), /folder could not be found/); const noAttachment = setup().service; await assert.rejects(() => noAttachment.previewAttachment({}, { uid: 4, filename: 'unknown.txt' }), /No attachment/); });