const test = require('node:test'); const assert = require('node:assert/strict'); const { normalizeOutgoingAttachments, serializeOutgoingAttachments } = require('../desktop/mail-attachments.cjs'); test('normalizes attachment metadata and round-trips bytes through serialized drafts and sends', () => { const bytes = Buffer.from([0, 1, 2, 127, 128, 255]); const [file] = normalizeOutgoingAttachments([{ filename: 'folder\\report.bin', contentType: 'APPLICATION/OCTET-STREAM', size: 999, contentBase64: bytes.toString('base64') }]); assert.equal(file.filename, 'report.bin'); assert.equal(file.contentType, 'application/octet-stream'); assert.equal(file.size, bytes.length); assert.deepEqual(file.content, bytes); const [serialized] = serializeOutgoingAttachments([file]); assert.equal(serialized.contentBase64, bytes.toString('base64')); assert.equal('content' in serialized, false); }); test('rejects malformed attachment data and unsafe content types', () => { assert.throws(() => normalizeOutgoingAttachments({}), /attachment list is invalid/); assert.throws(() => normalizeOutgoingAttachments([{ filename: 'x', contentBase64: '***=' }]), /contents.*invalid/); assert.throws(() => normalizeOutgoingAttachments([{ filename: 'x', contentType: 'text/plain\r\nBcc: bad@example.com', contentBase64: '' }]), /content type.*invalid/); }); test('uses fallback names and MIME types for empty metadata', () => { const [file] = normalizeOutgoingAttachments([{ contentBase64: '' }]); assert.equal(file.filename, 'attachment'); assert.equal(file.contentType, 'application/octet-stream'); assert.equal(file.size, 0); });