import assert from 'node:assert/strict'; import fs from 'node:fs'; import test from 'node:test'; import { addresses, decodeWords, parseMessage, unflow } from '../src/wamail/mime.js'; const fixture = (name) => fs.readFileSync(new URL(`./fixtures/${name}`, import.meta.url)); test('neomutt: 8-bit flowed text + QP attachment with an RFC 2231 filename', () => { const m = parseMessage(fixture('neomutt-adjunto.eml')); assert.equal(m.header('Subject'), 'asunto ñ'); assert.deepEqual(addresses(m.header('To')), ['5491122334455@c.us']); assert.match(m.text, /^hola, ¿qué tal\? ñandú 🎉\n\n> línea citada\n-- \nfirma/); assert.equal(m.attachments.length, 1); assert.equal(m.attachments[0].filename, 'informe año.pdf'); assert.equal(m.attachments[0].contentType, 'application/pdf'); assert.equal(m.attachments[0].data.toString(), 'PDF-FAKE-'.repeat(50)); }); test('neomutt: reply with In-Reply-To and several recipients', () => { const m = parseMessage(fixture('neomutt-respuesta.eml')); assert.equal(m.header('In-Reply-To'), '<3EB0AAA1@wa>'); assert.deepEqual(addresses(m.header('To')), ['5491122334455@c.us', '120363000000@g.us']); assert.equal(m.attachments.length, 0); assert.match(m.text, /la otra vez \nOn Wed, Juan wrote:\n> ¿venís mañana\?/); // "vez " + empty line = soft break }); test('encoded-words, flowed and addresses', () => { assert.equal(decodeWords('=?UTF-8?B?SnVhbiBQw6lyZXo=?= '), 'Juan Pérez '); assert.equal(decodeWords('=?utf-8?Q?a=C3=B1o?= =?utf-8?Q?_nuevo?='), 'año nuevo'); assert.equal(unflow('una línea \nlarga\n-- \nfirma'), 'una línea larga\n-- \nfirma'); assert.deepEqual(addresses('"Pérez, Juan" <1@c.us>, 2@g.us'), ['1@c.us', '2@g.us']); }); test('multipart with base64 and a latin1 charset', () => { const raw = [ 'To: 1@c.us', 'Subject: =?iso-8859-1?Q?caf=E9?=', 'MIME-Version: 1.0', 'Content-Type: multipart/mixed; boundary="b1"', '', '--b1', 'Content-Type: multipart/alternative; boundary="b2"', '', '--b2', 'Content-Type: text/plain; charset=iso-8859-1', 'Content-Transfer-Encoding: quoted-printable', '', 'caf=E9 con leche', '--b2--', '--b1', 'Content-Type: image/png; name="foto.png"', 'Content-Transfer-Encoding: base64', '', Buffer.from('PNG!').toString('base64'), '--b1--', '', ].join('\r\n'); const m = parseMessage(raw); assert.equal(m.header('subject'), 'café'); assert.equal(m.text, 'café con leche'); assert.equal(m.attachments[0].filename, 'foto.png'); assert.equal(m.attachments[0].data.toString(), 'PNG!'); });