Files
ep-mail-client/tests/mail-send-service.test.cjs
T

108 lines
4.1 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { createMailSender } = require('../desktop/mail/send-service.cjs');
function makeSender({ sentMailboxFails = false, smtpFails = false } = {}) {
const calls = { smtp: [], appended: [], closed: 0, loggedOut: 0 };
const sender = createMailSender({
smtpTransport() {
return {
async sendMail(message) {
calls.smtp.push(message);
if (smtpFails) throw new Error('SMTP rejected the message.');
return { accepted: ['recipient@example.com'] };
},
close() { calls.closed += 1; }
};
},
createTransport(options) {
assert.deepEqual(options, { streamTransport: true, buffer: true, newline: 'windows' });
return {
async sendMail(message) { return { message: Buffer.from(JSON.stringify(message)) }; }
};
},
imapClient() {
return {
usable: true,
async connect() {
if (sentMailboxFails) throw new Error('IMAP unavailable.');
},
async getMailboxLock(folder) {
assert.equal(folder, 'Sent');
return { release() {} };
},
async search() { return []; },
async append(folder, content, flags) { calls.appended.push({ folder, content, flags }); },
async logout() { calls.loggedOut += 1; }
};
},
async findSentMailbox(_client, createIfMissing) {
assert.equal(createIfMissing, true);
return 'Sent';
}
});
return { ...sender, calls };
}
test('sends a message with bounded fields and attachments, then appends a Sent copy', async () => {
const { sendMailNow, calls } = makeSender();
const result = await sendMailNow({
email: 'sender@example.com', displayName: 'Sender', signature: 'Regards'
}, {
to: ' recipient@example.com ',
subject: 'A subject',
text: 'Message body',
messageId: '<test@ep-mail.local>',
attachments: [{ filename: 'report.txt', contentBase64: Buffer.from('report').toString('base64') }]
});
assert.deepEqual(result, { ok: true, savedToSent: true });
assert.equal(calls.smtp.length, 1);
assert.equal(calls.smtp[0].to, 'recipient@example.com');
assert.equal(calls.smtp[0].text, 'Message body\n\nRegards');
assert.equal(calls.smtp[0].messageId, '<test@ep-mail.local>');
assert.deepEqual(calls.smtp[0].attachments[0].content, Buffer.from('report'));
assert.equal(calls.appended.length, 1);
assert.equal(calls.appended[0].folder, 'Sent');
assert.deepEqual(calls.appended[0].flags, ['\\Seen']);
assert.equal(calls.closed, 1);
assert.equal(calls.loggedOut, 1);
});
test('reports Sent-copy failures without retrying a message accepted by SMTP', async () => {
const { sendMailNow, calls } = makeSender({ sentMailboxFails: true });
const result = await sendMailNow({ email: 'sender@example.com' }, {
to: 'recipient@example.com',
text: 'Already sent'
});
assert.deepEqual(result, { ok: true, savedToSent: false });
assert.equal(calls.smtp.length, 1);
assert.equal(calls.closed, 1);
assert.equal(calls.appended.length, 0);
});
test('preserves reply headers in the SMTP message and Sent copy', async () => {
const { sendMailNow, calls } = makeSender();
await sendMailNow({ email: 'sender@example.com' }, {
to: 'recipient@example.com',
inReplyTo: '<parent@example.test>',
references: '<root@example.test> <parent@example.test>'
});
assert.equal(calls.smtp[0].inReplyTo, '<parent@example.test>');
assert.deepEqual(calls.smtp[0].references, ['<root@example.test>', '<parent@example.test>']);
const saved = JSON.parse(calls.appended[0].content.toString());
assert.equal(saved.inReplyTo, calls.smtp[0].inReplyTo);
assert.deepEqual(saved.references, calls.smtp[0].references);
});
test('closes SMTP transport and reports send failures', async () => {
const { sendMailNow, calls } = makeSender({ smtpFails: true });
await assert.rejects(
() => sendMailNow({ email: 'sender@example.com' }, { to: 'recipient@example.com' }),
/SMTP rejected/
);
assert.equal(calls.closed, 1);
assert.equal(calls.appended.length, 0);
});