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

100 lines
4.2 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { createMailReader } = require('../desktop/mail/read-service.cjs');
function setup({ exists = 3, fetched = [], mailboxPath = 'INBOX' } = {}) {
const calls = { fetchRanges: [], cached: [], logout: 0, snoozed: [] };
const client = {
usable: true,
async connect() {},
async mailboxOpen(path, options) {
assert.equal(path, mailboxPath);
assert.deepEqual(options, { readOnly: true });
return { exists };
},
async fetchAll(range, query) {
calls.fetchRanges.push({ range, query });
return fetched.map(item => ({ ...item, flags: item.flags || new Set() }));
},
async logout() { calls.logout += 1; }
};
const reader = createMailReader({
imapClient() { return client; },
async folderPath(_client, folder) { return folder === 'Missing' ? null : mailboxPath; },
async toUiMessage(item, folder) {
return {
uid: Number(item.uid),
id: Number(item.uid),
folder,
sender: item.sender || '',
subject: item.subject || '',
snippet: item.snippet || '',
body: [],
timestamp: item.timestamp || 0,
tags: [folder]
};
},
async cacheMailboxMessages(accountId, folder, messages) { calls.cached.push({ accountId, folder, messages }); },
async fetchSnoozedMailbox(...args) { calls.snoozed.push(args); return { folder: 'Snoozed', messages: [] }; },
filterMailboxMessages(messages, filters) {
const query = String(filters.query || '').toLowerCase();
return messages.filter(message => `${message.sender} ${message.subject} ${message.snippet}`.toLowerCase().includes(query));
}
});
return { reader, calls };
}
test('fetches the newest page, returns newest first, caches it, and logs out', async () => {
const { reader, calls } = setup({
fetched: [
{ uid: 2, source: Buffer.from('two'), subject: 'Second' },
{ uid: 3, source: Buffer.from('three'), subject: 'Third' }
]
});
const result = await reader.fetchMailbox({ id: 'account-1' }, 'Inbox', 0, 2);
assert.deepEqual(calls.fetchRanges.map(call => call.range), ['2:3']);
assert.deepEqual(result.messages.map(message => message.uid), [3, 2]);
assert.deepEqual({ total: result.total, start: result.start, end: result.end }, { total: 3, start: 2, end: 3 });
assert.equal(calls.cached.length, 1);
assert.equal(calls.cached[0].accountId, 'account-1');
assert.equal(calls.logout, 1);
});
test('filtered fetch scans the bounded recent range, excludes snoozed messages, and applies labels', async () => {
const { reader, calls } = setup({
fetched: [
{ uid: 1, source: Buffer.from('one'), sender: 'Alice', subject: 'Needle' },
{ uid: 2, source: Buffer.from('two'), sender: 'Bob', subject: 'Other' },
{ uid: 3, source: Buffer.from('three'), sender: 'Alice', subject: 'Needle' }
]
});
const result = await reader.fetchMailbox({
id: 'account-2',
snoozedMessages: [{ uid: 3, folder: 'Inbox', dueAt: Date.now() + 60_000 }],
ruleLabels: { 'Inbox:1': ['Important'] }
}, 'Inbox', 0, 20, { query: 'needle' });
assert.deepEqual(calls.fetchRanges.map(call => call.range), ['1:3']);
assert.deepEqual(result.messages.map(message => message.uid), [1]);
assert.deepEqual(result.messages[0].tags, ['Inbox', 'Important']);
assert.deepEqual(calls.cached[0].messages.map(message => message.uid), [1, 2]);
assert.equal(calls.logout, 1);
});
test('returns an empty result for an unavailable folder and still closes the connection', async () => {
const { reader, calls } = setup({ mailboxPath: 'INBOX' });
const result = await reader.fetchMailbox({ id: 'account-3' }, 'Missing');
assert.deepEqual(result, { messages: [], total: 0 });
assert.equal(calls.fetchRanges.length, 0);
assert.equal(calls.logout, 1);
});
test('delegates snoozed-folder reads without opening a separate IMAP client', async () => {
const { reader, calls } = setup();
const result = await reader.fetchMailbox({ id: 'account-4' }, 'Snoozed', 5, 10);
assert.deepEqual(result, { folder: 'Snoozed', messages: [] });
assert.deepEqual(calls.snoozed, [[{ id: 'account-4' }, 5, 10]]);
assert.equal(calls.logout, 0);
});