const test = require('node:test'); const assert = require('node:assert/strict'); const { createMessageActionService } = require('../desktop/mail/action-service.cjs'); function setup({ sourcePath = 'INBOX', usable = true } = {}) { const calls = { actions: [], locks: [], released: 0, logout: 0, updates: 0 }; const store = { accounts: [{ id: 'account-1', snoozedMessages: [ { uid: 7, folder: 'Inbox' }, { uid: 8, folder: 'Inbox' }, { uid: 7, folder: 'Sent' } ] }] }; const client = { usable, async connect() {}, async getMailboxLock(path) { calls.locks.push(path); return { release() { calls.released += 1; } }; }, async messageFlagsAdd(...args) { calls.actions.push(['flagsAdd', ...args]); }, async messageFlagsRemove(...args) { calls.actions.push(['flagsRemove', ...args]); }, async messageMove(...args) { calls.actions.push(['move', ...args]); }, async messageDelete(...args) { calls.actions.push(['delete', ...args]); }, async logout() { calls.logout += 1; } }; const service = createMessageActionService({ imapClient() { return client; }, async folderPath(_client, folder) { return folder === 'Inbox' ? sourcePath : folder; }, async findSpecialMailbox(_client, specialUse, _fallbackNames, createName) { calls.actions.push(['find', specialUse, createName]); return specialUse === '\\Archive' ? 'Archive' : 'Trash'; }, async updateAccountStore(update) { calls.updates += 1; return update(store); } }); return { service, calls, store }; } test('applies star and unread flags with UID addressing and releases the mailbox lock', async () => { const { service, calls } = setup(); assert.deepEqual(await service.applyMessageAction({ id: 'account-1' }, { uid: 7, action: 'star', enabled: true }), { ok: true }); assert.deepEqual(calls.actions[0], ['flagsAdd', 7, ['\\Flagged'], { uid: true }]); assert.deepEqual(calls.locks, ['INBOX']); assert.equal(calls.released, 1); assert.equal(calls.logout, 1); assert.equal(calls.updates, 0); }); test('moves archived messages and removes their matching snooze entry', async () => { const { service, calls, store } = setup(); await service.applyMessageAction({ id: 'account-1' }, { uid: 7, folder: 'Inbox', action: 'archive' }); assert.deepEqual(calls.actions, [ ['find', '\\Archive', 'Archive'], ['move', 7, 'Archive', { uid: true }] ]); assert.deepEqual(store.accounts[0].snoozedMessages, [{ uid: 8, folder: 'Inbox' }, { uid: 7, folder: 'Sent' }]); assert.equal(calls.updates, 1); assert.equal(calls.released, 1); }); test('deleting in Trash deletes rather than attempting to move a message to itself', async () => { const { service, calls } = setup({ sourcePath: 'Trash' }); await service.applyMessageAction({ id: 'account-1' }, { uid: 9, folder: 'Inbox', action: 'delete' }); assert.deepEqual(calls.actions, [ ['find', '\\Trash', 'Trash'], ['delete', 9, { uid: true }] ]); }); test('rejects invalid message identifiers and actions without opening IMAP', async () => { const { service, calls } = setup(); await assert.rejects(() => service.applyMessageAction({}, { uid: 0, action: 'star' }), /could not be identified/); await assert.rejects(() => service.applyMessageAction({}, { uid: 1, action: 'expunge' }), /not supported/); assert.equal(calls.locks.length, 0); assert.equal(calls.logout, 0); });