Refractoring Stage 3 Completed the mail-service refactoring stage and started IPC hardening
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { EventEmitter } = require('node:events');
|
||||
const { createIdleWatcherManager } = require('../desktop/mail/idle-watcher-manager.cjs');
|
||||
|
||||
function setup(accounts, { concurrency = 2, generations = new Map(accounts.map(account => [account.id, 0])) } = {}) {
|
||||
const calls = { clients: [], requests: 0, loggedOut: 0, closed: 0, mailboxOpens: [], errors: [] };
|
||||
const manager = createIdleWatcherManager({
|
||||
async readAccountStore() { return { accounts }; },
|
||||
async readAccount(accountId) { return accounts.find(account => account.id === accountId) || null; },
|
||||
imapClient(account, options) {
|
||||
const client = new EventEmitter();
|
||||
client.usable = true;
|
||||
client.connect = async () => {};
|
||||
client.mailboxOpen = async (...args) => { calls.mailboxOpens.push(args); };
|
||||
client.logout = async () => { calls.loggedOut += 1; client.usable = false; };
|
||||
client.close = () => { calls.closed += 1; client.emit('close'); };
|
||||
calls.clients.push({ account, options, client });
|
||||
return client;
|
||||
},
|
||||
backgroundSyncGenerations: generations,
|
||||
concurrency,
|
||||
wait: () => new Promise(() => {}),
|
||||
logError(...args) { calls.errors.push(args); }
|
||||
});
|
||||
manager.setRequestBackgroundSync(() => { calls.requests += 1; });
|
||||
return { manager, calls, generations };
|
||||
}
|
||||
|
||||
test('starts only the configured number of IDLE watchers and reconciles updates', async () => {
|
||||
const accounts = [
|
||||
{ id: 'one', email: 'one@example.com' },
|
||||
{ id: 'two', email: 'two@example.com' },
|
||||
{ id: 'three', email: 'three@example.com' }
|
||||
];
|
||||
const { manager, calls } = setup(accounts, { concurrency: 2 });
|
||||
await manager.reconcileIdleWatchers();
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
|
||||
assert.deepEqual(calls.clients.map(item => item.account.id), ['one', 'two']);
|
||||
assert.deepEqual(calls.clients[0].options, {
|
||||
autoIdleDelay: 10_000,
|
||||
maxIdleTime: 25 * 60 * 1000,
|
||||
socketTimeout: 30 * 60 * 1000
|
||||
});
|
||||
assert.deepEqual(calls.mailboxOpens, [['INBOX', { readOnly: true }], ['INBOX', { readOnly: true }]]);
|
||||
|
||||
const firstClient = calls.clients[0].client;
|
||||
firstClient.emit('exists', { path: 'INBOX', count: 2, prevCount: 1 });
|
||||
firstClient.emit('exists', { path: 'INBOX', count: 2, prevCount: 2 });
|
||||
firstClient.emit('exists', { path: 'Sent', count: 3, prevCount: 2 });
|
||||
firstClient.emit('flags', { path: 'INBOX' });
|
||||
firstClient.emit('flags', { path: 'Sent' });
|
||||
assert.equal(calls.requests, 2);
|
||||
|
||||
manager.stopAll();
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
assert.equal(calls.closed, 2);
|
||||
assert.equal(calls.loggedOut, 2);
|
||||
});
|
||||
|
||||
test('stops watchers when accounts are removed or their generation changes', async () => {
|
||||
const accounts = [{ id: 'one', email: 'one@example.com' }, { id: 'two', email: 'two@example.com' }];
|
||||
const generations = new Map([['one', 0], ['two', 0]]);
|
||||
const { manager, calls } = setup(accounts, { generations });
|
||||
await manager.reconcileIdleWatchers();
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
generations.set('one', 1);
|
||||
accounts.splice(1, 1);
|
||||
await manager.reconcileIdleWatchers();
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
assert.equal(calls.closed, 2);
|
||||
assert.equal(calls.loggedOut, 2);
|
||||
manager.stopAll();
|
||||
});
|
||||
Reference in New Issue
Block a user