const test = require('node:test'); const assert = require('node:assert/strict'); const { backgroundFolderLabel, createMailboxSyncService } = require('../desktop/mail/sync-service.cjs'); function setup({ mailboxes, mailboxByPath, fetchResults = {} }) { const calls = { fetches: [], flags: [], moves: [], locksReleased: 0, logout: 0, outbox: 0, labels: 0, saved: null, errors: [] }; const generations = new Map([['account-1', 1]]); const snapshots = new Map(); const savedStore = { accounts: [{ id: 'account-1', ruleLabels: {} }] }; const client = { usable: true, async connect() {}, async list() { return mailboxes; }, async mailboxOpen(path, options) { assert.deepEqual(options, { readOnly: true }); return mailboxByPath[path]; }, async fetchAll(range, query, options) { calls.fetches.push({ range, query, options }); const result = fetchResults[range] || []; return result.map(item => ({ ...item, flags: item.flags || new Set() })); }, async getMailboxLock(path) { calls.flags.push(['lock', path]); return { release() { calls.locksReleased += 1; } }; }, async messageFlagsAdd(...args) { calls.flags.push(['add', ...args]); }, async messageMove(...args) { calls.moves.push(args); }, async logout() { calls.logout += 1; } }; const service = createMailboxSyncService({ imapClient() { return client; }, async flushMailOutbox() { calls.outbox += 1; }, async updateAccountStore(update) { calls.labels += 1; return update(savedStore); }, async saveAccountSyncState(_id, generation, folders) { calls.saved = folders; return generations.get('account-1') === generation; }, backgroundSyncGenerations: generations, flagSnapshots: snapshots, async findSpecialMailbox() { return 'Archive'; }, async folderPath(_client, folder) { return folder; }, logError(...args) { calls.errors.push(args); } }); return { service, calls, generations, snapshots, savedStore }; } test('maps server special-use and fallback names to user-facing folders', () => { assert.equal(backgroundFolderLabel({ path: 'INBOX' }), 'Inbox'); assert.equal(backgroundFolderLabel({ path: 'Provider/Sent Items' }), 'Sent'); assert.equal(backgroundFolderLabel({ path: 'Archive', specialUse: '\\Archive' }), 'Archive'); assert.equal(backgroundFolderLabel({ path: 'Projects/2026' }), 'Projects/2026'); }); test('initializes a new mailbox cursor without reporting historical messages as new', async () => { const { service, calls, snapshots } = setup({ mailboxes: [{ path: 'INBOX' }], mailboxByPath: { INBOX: { uidValidity: 9, uidNext: 22, highestModseq: null } }, fetchResults: { '1:*': [{ uid: 1, flags: new Set(['\\Seen']) }, { uid: 21, flags: new Set() }] } }); const result = await service.syncAccountMailboxes({ id: 'account-1', syncState: { folders: {} } }); assert.deepEqual(result, { newCount: 0, changedFolders: [] }); assert.deepEqual(calls.saved.INBOX, { uidValidity: '9', lastUid: 21, highestModseq: null }); assert.equal(snapshots.get('account-1').get('INBOX').flags.size, 2); assert.equal(calls.outbox, 1); assert.equal(calls.logout, 1); }); test('detects new inbox messages, applies matching rules, persists labels, and closes the client', async () => { const oldFlags = new Map([[1, '\\Seen']]); const { service, calls, snapshots, savedStore } = setup({ mailboxes: [{ path: 'INBOX' }], mailboxByPath: { INBOX: { uidValidity: 9, uidNext: 5, highestModseq: null } }, fetchResults: { '3:4': [ { uid: 3, envelope: { from: [{ address: 'flag@example.com' }], subject: 'Mark' } }, { uid: 4, envelope: { from: [{ address: 'label@example.com' }], subject: 'Keep' } } ], '1:*': [{ uid: 1, flags: new Set() }, { uid: 3, flags: new Set() }, { uid: 4, flags: new Set() }] } }); snapshots.set('account-1', new Map([['INBOX', { uidValidity: '9', flags: oldFlags }]])); const account = { id: 'account-1', syncState: { folders: { INBOX: { uidValidity: '9', lastUid: 2, highestModseq: null } } }, rules: [ { id: 'flag-rule', senderContains: 'flag@', subjectContains: '', action: 'flag' }, { id: 'label-rule', senderContains: 'label@', subjectContains: '', action: 'label', value: 'Keep' } ] }; const result = await service.syncAccountMailboxes(account); assert.deepEqual(result, { newCount: 2, changedFolders: ['Inbox'] }); assert.deepEqual(calls.flags[1], ['add', 3, ['\\Flagged'], { uid: true }]); assert.equal(calls.locksReleased, 2); assert.equal(savedStore.accounts[0].ruleLabels['INBOX:4'][0], 'Keep'); assert.equal(calls.labels, 1); assert.equal(calls.saved.INBOX.lastUid, 4); assert.equal(calls.logout, 1); }); test('does not publish a stale sync result after its account generation changes', async () => { const { service, calls, generations } = setup({ mailboxes: [{ path: 'INBOX' }], mailboxByPath: { INBOX: { uidValidity: 9, uidNext: 1, highestModseq: 1n } } }); const pending = service.syncAccountMailboxes({ id: 'account-1', syncState: { folders: {} } }); generations.set('account-1', 2); const result = await pending; assert.deepEqual(result, { newCount: 0, changedFolders: [] }); assert.equal(calls.logout, 1); });