140 lines
5.7 KiB
JavaScript
140 lines
5.7 KiB
JavaScript
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 };
|
|
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('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"/);
|
|
});
|