const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const root = path.resolve(__dirname, '..'); const source = fs.readFileSync(path.join(root, 'mail-ui.js'), 'utf8'); const syncSource = source.slice(source.indexOf('async function syncMailbox('), source.indexOf('\nif (window.daybreakMail) {', source.indexOf('async function syncMailbox('))); function deferred() { let resolve; const promise = new Promise(done => { resolve = done; }); return { promise, resolve }; } function harness({ platform = 'android', cached, sync }) { const messages = []; const state = { messages, activeMessage: null, activeFolder: 'Inbox', selectedIds: new Set(), query: '' }; const feature = { connectedAccount: { id: 'account' }, pageOffset: 0, syncPromise: null, syncQueued: false, displayedMailboxKey: JSON.stringify(['account', 'Inbox']) }; const elements = new Map(); const opened = []; const renders = []; const toasts = []; const classes = new Set(); const bridge = { platform, syncFolder: sync, updateMessage: () => Promise.resolve() }; if (cached) bridge.getCachedFolder = cached; const context = { window: { daybreakMail: bridge }, document: { body: { classList: { remove: value => classes.delete(value) } } }, rendererState: state, mailFeatureState: feature, selectMailUi: selector => { if (!elements.has(selector)) elements.set(selector, { classList: { toggle() {} }, innerHTML: '', textContent: '' }); return elements.get(selector); }, appShell: { renderList: () => renders.push(messages.map(message => message.id)), openMessage: id => opened.push(id), emptyReader: () => 'empty' }, mailSearchFilters: () => ({}), localStorage: { getItem: () => null }, updateAccountCard() {}, toastMailUi: message => toasts.push(message), console }; vm.runInNewContext(`${syncSource}\nthis.syncMailbox = syncMailbox`, context); return { state, feature, context, elements, opened, renders, classes, toasts }; } function result(ids) { return { messages: ids.map(id => ({ id, uid: id, tags: [] })), total: ids.length, start: 1, end: ids.length }; } test('Android shows cached inbox before network sync without opening a message', async () => { const network = deferred(); const ui = harness({ cached: async () => result([1]), sync: () => network.promise }); const pending = ui.context.syncMailbox(false); await new Promise(resolve => setImmediate(resolve)); assert.deepEqual(ui.renders[0], [1]); assert.equal(ui.state.activeMessage, null); assert.equal(ui.elements.get('#mail-status').textContent, 'Showing saved messages — syncing'); network.resolve(result([2, 1])); await pending; assert.equal(ui.state.activeMessage, null); assert.deepEqual(ui.opened, []); assert.deepEqual(ui.state.messages.map(message => message.id), [2, 1]); }); test('Android refresh respects back navigation during an in-flight sync', async () => { const network = deferred(); const ui = harness({ sync: () => network.promise }); ui.state.activeMessage = 1; ui.classes.add('reader-open'); const pending = ui.context.syncMailbox(false); ui.state.activeMessage = null; ui.classes.delete('reader-open'); network.resolve(result([1, 2])); await pending; assert.equal(ui.state.activeMessage, null); assert.deepEqual(ui.opened, []); assert.equal(ui.elements.get('#reader-pane').innerHTML, 'empty'); }); test('refresh keeps an explicitly selected Android message and desktop still selects the first', async () => { const android = harness({ sync: async () => result([1, 2]) }); android.state.activeMessage = 2; await android.context.syncMailbox(false); assert.deepEqual(android.opened, [2]); const desktop = harness({ platform: 'desktop', sync: async () => result([1, 2]) }); await desktop.context.syncMailbox(false); assert.deepEqual(desktop.opened, [1]); }); test('a message selected from the cache stays open when sync finishes', async () => { const network = deferred(); const ui = harness({ cached: async () => result([1]), sync: () => network.promise }); const pending = ui.context.syncMailbox(false); await new Promise(resolve => setImmediate(resolve)); ui.state.activeMessage = 1; network.resolve(result([2, 1])); await pending; assert.deepEqual(ui.opened, [1]); }); test('sync errors leave cached messages visible and report the failure', async () => { const ui = harness({ cached: async () => result([1]), sync: async () => { throw new Error('Network failed'); } }); await ui.context.syncMailbox(false); assert.deepEqual(ui.state.messages.map(message => message.id), [1]); assert.equal(ui.elements.get('#mail-status').textContent, 'Could not sync — showing saved messages'); assert.deepEqual(ui.toasts, ['Network failed']); }); test('a sync result for a previous folder does not replace the current mailbox', async () => { const network = deferred(); const ui = harness({ sync: () => network.promise }); const pending = ui.context.syncMailbox(false); ui.state.activeFolder = 'Sent'; network.resolve(result([1])); await pending; assert.equal(ui.state.messages.length, 0); assert.deepEqual(ui.opened, []); }); test('switching folders shows that folder’s cached messages before the network response', async () => { const network = deferred(); const ui = harness({ cached: async folder => folder === 'Sent' ? result([42]) : null, sync: () => network.promise }); ui.state.messages.push({ id: 1, uid: 1, tags: [] }); ui.state.activeFolder = 'Sent'; const pending = ui.context.syncMailbox(false, { offset: 0 }); await new Promise(resolve => setImmediate(resolve)); assert.deepEqual(ui.state.messages.map(message => message.id), [42]); assert.deepEqual(ui.renders.at(-1), [42]); network.resolve(result([43])); await pending; }); test('a different folder can load from cache while the previous folder is still syncing', async () => { const inbox = deferred(); const sent = deferred(); const ui = harness({ cached: async folder => folder === 'Sent' ? result([42]) : null, sync: folder => folder === 'Inbox' ? inbox.promise : sent.promise }); const first = ui.context.syncMailbox(false); await new Promise(resolve => setImmediate(resolve)); ui.state.activeFolder = 'Sent'; const second = ui.context.syncMailbox(false, { offset: 0 }); await new Promise(resolve => setImmediate(resolve)); assert.deepEqual(ui.state.messages.map(message => message.id), [42]); sent.resolve(result([43])); await second; inbox.resolve(result([1])); await first; assert.deepEqual(ui.state.messages.map(message => message.id), [43]); }); test('returning to a folder ignores an older request for the same folder', async () => { const firstInbox = deferred(); const sent = deferred(); const secondInbox = deferred(); let calls = 0; const ui = harness({ sync: () => [firstInbox, sent, secondInbox][calls++].promise }); const first = ui.context.syncMailbox(false); ui.state.activeFolder = 'Sent'; const middle = ui.context.syncMailbox(false); ui.state.activeFolder = 'Inbox'; const last = ui.context.syncMailbox(false); secondInbox.resolve(result([3])); await last; firstInbox.resolve(result([1])); sent.resolve(result([2])); await Promise.all([first, middle]); assert.deepEqual(ui.state.messages.map(message => message.id), [3]); assert.equal(ui.elements.get('#refresh').disabled, false); }); test('switching accounts never displays the previous account’s messages', async () => { const network = deferred(); const ui = harness({ cached: async () => null, sync: () => network.promise }); ui.state.messages.push({ id: 1, uid: 1, tags: [] }); ui.feature.connectedAccount = { id: 'other' }; const pending = ui.context.syncMailbox(false); assert.equal(ui.state.messages.length, 0); network.resolve(result([2])); await pending; assert.deepEqual(ui.state.messages.map(message => message.id), [2]); }); test('Android startup theme uses the EP vector on supported splash screens', () => { const styles = fs.readFileSync(path.join(root, 'android', 'app', 'src', 'main', 'res', 'values', 'styles.xml'), 'utf8'); const icon = fs.readFileSync(path.join(root, 'android', 'app', 'src', 'main', 'res', 'drawable', 'ep_splash_icon.xml'), 'utf8'); assert.match(styles, /name="windowSplashScreenAnimatedIcon">@drawable\/ep_splash_icon/); assert.match(styles, /name="postSplashScreenTheme">@style\/AppTheme.NoActionBar/); assert.match(icon, /android:strokeColor="#eee9de"/); }); test('Android launcher icons use the EP logo on adaptive and legacy launchers', () => { const res = path.join(root, 'android', 'app', 'src', 'main', 'res'); const read = (...parts) => fs.readFileSync(path.join(res, ...parts), 'utf8'); for (const name of ['ic_launcher.xml', 'ic_launcher_round.xml']) { assert.match(read('mipmap-anydpi-v26', name), /@drawable\/ep_launcher_foreground/); assert.match(read('mipmap-anydpi', name), /@drawable\/ep_splash_icon/); } assert.match(read('drawable', 'ep_launcher_foreground.xml'), /@drawable\/ep_splash_icon/); assert.match(read('drawable', 'ep_launcher_foreground.xml'), /android:inset="22dp"/); assert.match(read('values', 'ic_launcher_background.xml'), /#0a0c0b/); });