const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const { createMailUiApi } = require('../renderer-mail-api.js'); test('mail feature API exposes explicit state and account/sync UI actions', () => { const calls = []; let connectedAccount = { id: 'first' }; const state = { activeFolder: 'Inbox' }; const methods = { setPageOffset: value => calls.push(['offset', value]), syncMailbox: (...args) => calls.push(['sync', ...args]), updateAccountCard: value => calls.push(['card', value]), getRenderList: () => () => calls.push(['render']), getOpenMessage: () => () => calls.push(['open']), getShowCompose: () => () => calls.push(['compose']), baseRenderList: () => calls.push(['base-render']), emptyReader: () => '', openMailAccount: value => calls.push(['open-account', value]), select: selector => selector, selectAll: selector => [selector], toast: value => calls.push(['toast', value]), setRenderList: renderer => { calls.push(['set-render-list', renderer]); }, setOpenMessage: renderer => { calls.push(['set-open-message', renderer]); }, setShowCompose: renderer => { calls.push(['set-show-compose', renderer]); } }; const api = createMailUiApi({ state, getConnectedAccount: () => connectedAccount, setConnectedAccount: value => { connectedAccount = value; }, ...methods }); assert.equal(api.state, state); assert.equal(api.connectedAccount.id, 'first'); api.connectedAccount = { id: 'second' }; assert.equal(connectedAccount.id, 'second'); api.setPageOffset(0); api.syncMailbox(false, { offset: 0 }); api.updateAccountCard(api.connectedAccount); api.renderList(); api.openMessage(1); api.showCompose(); api.baseRenderList(); assert.equal(api.emptyReader(), ''); assert.equal(api.select('selector'), 'selector'); assert.deepEqual(api.selectAll('selector'), ['selector']); api.wrapRenderList(base => () => { calls.push(['wrapped-render']); return base(); }); api.wrapOpenMessage(base => () => { calls.push(['wrapped-open']); return base(); }); api.wrapShowCompose(base => () => { calls.push(['wrapped-compose']); return base(); }); api.openMailAccount(); assert.deepEqual(calls.map(call => call[0]), [ 'offset', 'sync', 'card', 'render', 'open', 'compose', 'base-render', 'set-render-list', 'set-open-message', 'set-show-compose', 'open-account' ]); assert.equal(Object.isFrozen(api), true); }); test('account and snooze UI consume the explicit mail API instead of selected legacy globals', () => { const root = path.resolve(__dirname, '..'); const accountUi = fs.readFileSync(path.join(root, 'account-ui.js'), 'utf8'); const snoozeUi = fs.readFileSync(path.join(root, 'snooze-ui.js'), 'utf8'); const attachmentUi = fs.readFileSync(path.join(root, 'attachments.js'), 'utf8'); const pgpMailUi = fs.readFileSync(path.join(root, 'pgp-mail-ui.js'), 'utf8'); const pgpUi = fs.readFileSync(path.join(root, 'pgp-ui.js'), 'utf8'); const davUi = fs.readFileSync(path.join(root, 'dav-ui.js'), 'utf8'); const appShell = fs.readFileSync(path.join(root, 'app.js'), 'utf8'); const mailUi = fs.readFileSync(path.join(root, 'mail-ui.js'), 'utf8'); const shortcutsUi = fs.readFileSync(path.join(root, 'shortcuts.js'), 'utf8'); const productivityUi = fs.readFileSync(path.join(root, 'productivity.js'), 'utf8'); assert.match(accountUi, /window\.epMailFeatures\.mail/); assert.match(snoozeUi, /window\.epMailFeatures\.mail/); assert.match(attachmentUi, /window\.epMailFeatures\.mail/); assert.match(pgpMailUi, /window\.epMailFeatures\.mail/); assert.match(pgpMailUi, /^\(\(\) => \{/); assert.match(pgpMailUi, /window\.epMailFeatures\.pgp = Object\.freeze/); assert.doesNotMatch(pgpMailUi, /(? \{/); assert.match(accountUi, /const accountDavUi = window\.epMailFeatures\.dav/); assert.match(accountUi, /accountDavUi\.openManager/); assert.doesNotMatch(accountUi, /(? \{/); assert.match(appShell, /window\.epMailShell = Object\.freeze/); assert.match(mailUi, /appShell\.wrapOpenMessage/); assert.match(mailUi, /appShell\.wrapShowCompose/); assert.match(mailUi, /^\(\(\) => \{/); assert.match(mailUi, /window\.epMailFeatures\.mail = mailUiApi/); assert.doesNotMatch(mailUi, /(? \{/); assert.match(productivityUi, /window\.epMailFeatures\.productivity = Object\.freeze/); assert.match(productivityUi, /addMessageLabel,/); assert.match(snoozeUi, /snoozeProductivityUi\.addMessageLabel/); assert.doesNotMatch(snoozeUi, /(?