import test from 'brittle'
import { parseEmail } from '../lib/imap-client.js'
test('parseEmail - simple email', (t) => {
const raw = [
'From: Alice <alice@example.com>',
'To: bob@example.com',
'Subject: Test',
'Date: Fri, 14 Feb 2026 10:00:00 +0000',
'Message-ID: <abc123@example.com>',
'Content-Type: text/plain; charset=utf-8',
'',
'Hello, this is a test email.'
].join('\r\n')
const email = parseEmail(raw)
t.is(email.from, 'Alice <alice@example.com>')
t.is(email.to, 'bob@example.com')
t.is(email.subject, 'Test')
t.is(email.body, 'Hello, this is a test email.')
t.is(email.messageId, '<abc123@example.com>')
})
test('parseEmail - multiline body', (t) => {
const raw = [
'From: sender@test.com',
'Subject: Multi',
'',
'Line 1',
'Line 2',
'Line 3'
].join('\r\n')
const email = parseEmail(raw)
t.ok(email.body.includes('Line 1'))
t.ok(email.body.includes('Line 2'))
t.ok(email.body.includes('Line 3'))
})
test('parseEmail - multipart text/plain extraction', (t) => {
const raw = [
'From: sender@test.com',
'Subject: Multipart',
'Content-Type: multipart/alternative; boundary="boundary123"',
'',
'--boundary123',
'Content-Type: text/plain; charset=utf-8',
'',
'Plain text body here',
'--boundary123',
'Content-Type: text/html; charset=utf-8',
'',
'<html><body>HTML body</body></html>',
'--boundary123--'
].join('\r\n')
const email = parseEmail(raw)
t.ok(email.body.includes('Plain text body'))
t.absent(email.body.includes('HTML body'))
})
test('parseEmail - quoted-printable decoding', (t) => {
const raw = [
'From: sender@test.com',
'Subject: QP',
'Content-Transfer-Encoding: quoted-printable',
'',
'Hello =C3=A9l=C3=A8ve'
].join('\r\n')
const email = parseEmail(raw)
t.ok(email.body.includes('Hello'))
// UTF-8 decoded é and è
t.ok(email.body.includes('élève'))
})
test('parseEmail - no headers', (t) => {
const raw = 'Just a body with no headers at all'
const email = parseEmail(raw)
t.is(email.from, '')
t.is(email.subject, '')
t.ok(email.body.includes('Just a body'))
})
test('parseEmail - base64 encoding', (t) => {
const raw = [
'From: sender@test.com',
'Subject: B64',
'Content-Transfer-Encoding: base64',
'',
Buffer.from('Decoded base64 content').toString('base64')
].join('\r\n')
const email = parseEmail(raw)
t.is(email.body, 'Decoded base64 content')
})