Refractoring Stage 3 Completed the mail-service refactoring stage and started IPC hardening
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { createBackgroundSyncCoordinator } = require('../desktop/mail/sync-coordinator.cjs');
|
||||
|
||||
function setup({ accounts = [], sync, clock = 100000, concurrency = 2 } = {}) {
|
||||
let now = clock;
|
||||
const calls = { sync: [], updates: [], notifications: [], errors: [], reconciles: 0, active: 0, maxActive: 0 };
|
||||
const generations = new Map(accounts.map(account => [account.id, 0]));
|
||||
const failures = new Map();
|
||||
const lastSync = new Map();
|
||||
const snapshots = new Map();
|
||||
const coordinator = createBackgroundSyncCoordinator({
|
||||
async readAccountStore() { return { accounts }; },
|
||||
async syncAccountMailboxes(account) {
|
||||
calls.sync.push(account.id);
|
||||
calls.active += 1;
|
||||
calls.maxActive = Math.max(calls.maxActive, calls.active);
|
||||
try {
|
||||
return sync ? await sync(account, calls) : { newCount: 0, changedFolders: [] };
|
||||
} finally {
|
||||
calls.active -= 1;
|
||||
}
|
||||
},
|
||||
backgroundSyncGenerations: generations,
|
||||
backgroundSyncFailures: failures,
|
||||
lastBackgroundSyncAt: lastSync,
|
||||
flagSnapshots: snapshots,
|
||||
async reconcileIdleWatchers() { calls.reconciles += 1; },
|
||||
sendMailSyncUpdate(update) { calls.updates.push(update); },
|
||||
showNewMailNotification(account, count) { calls.notifications.push([account.id, count]); },
|
||||
concurrency,
|
||||
retryIntervalMs: 1000,
|
||||
now: () => now,
|
||||
logError(...args) { calls.errors.push(args); }
|
||||
});
|
||||
return { coordinator, calls, generations, failures, lastSync, snapshots, setNow(value) { now = value; } };
|
||||
}
|
||||
|
||||
test('sends sync updates and notifications only for the current account generation', async () => {
|
||||
const accounts = [{ id: 'account-1', email: 'one@example.com' }];
|
||||
const { coordinator, calls, generations } = setup({
|
||||
accounts,
|
||||
sync: async () => ({ newCount: 2, changedFolders: ['Inbox'] })
|
||||
});
|
||||
await coordinator.syncAccountWithBackoff(accounts[0]);
|
||||
assert.deepEqual(calls.updates, [{ accountId: 'account-1', newCount: 2, foldersChanged: ['Inbox'] }]);
|
||||
assert.deepEqual(calls.notifications, [['account-1', 2]]);
|
||||
|
||||
calls.updates.length = 0;
|
||||
calls.notifications.length = 0;
|
||||
const pending = coordinator.syncAccountWithBackoff(accounts[0]);
|
||||
generations.set('account-1', 1);
|
||||
await pending;
|
||||
assert.deepEqual(calls.updates, []);
|
||||
assert.deepEqual(calls.notifications, []);
|
||||
});
|
||||
|
||||
test('applies periodic account intervals and exponential retry backoff', async () => {
|
||||
let attempts = 0;
|
||||
const account = { id: 'account-1', email: 'one@example.com', syncIntervalMinutes: 2 };
|
||||
const { coordinator, calls, failures, lastSync, setNow } = setup({
|
||||
accounts: [account],
|
||||
sync: async () => {
|
||||
attempts += 1;
|
||||
throw new Error('offline');
|
||||
}
|
||||
});
|
||||
lastSync.set(account.id, 99000);
|
||||
await coordinator.syncAccountWithBackoff(account, true);
|
||||
assert.equal(attempts, 0);
|
||||
|
||||
await coordinator.syncAccountWithBackoff(account);
|
||||
assert.deepEqual(failures.get(account.id), { attempts: 1, retryAt: 101000 });
|
||||
await coordinator.syncAccountWithBackoff(account);
|
||||
assert.equal(attempts, 1);
|
||||
assert.equal(calls.updates.length, 1);
|
||||
assert.equal(calls.errors.length, 1);
|
||||
|
||||
setNow(101000);
|
||||
await coordinator.syncAccountWithBackoff(account);
|
||||
assert.deepEqual(failures.get(account.id), { attempts: 2, retryAt: 103000 });
|
||||
});
|
||||
|
||||
test('runs account syncs in bounded concurrent batches', async () => {
|
||||
const accounts = Array.from({ length: 5 }, (_, index) => ({ id: `account-${index}` }));
|
||||
const { coordinator, calls } = setup({
|
||||
accounts,
|
||||
concurrency: 2,
|
||||
sync: async (_account, state) => {
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
state.sync.push('completed');
|
||||
return { newCount: 0, changedFolders: [] };
|
||||
}
|
||||
});
|
||||
await coordinator.runBackgroundSync();
|
||||
assert.ok(calls.maxActive <= 2);
|
||||
assert.equal(calls.sync.filter(id => id.startsWith('account-')).length, 5);
|
||||
});
|
||||
|
||||
test('account-specific requests invalidate stale state and force requests clear retry delays', () => {
|
||||
const { coordinator, generations, failures, snapshots } = setup({ accounts: [] });
|
||||
generations.set('account-1', 3);
|
||||
failures.set('account-1', { attempts: 3, retryAt: 999999 });
|
||||
failures.set('account-2', { attempts: 1, retryAt: 999999 });
|
||||
snapshots.set('account-1', new Map());
|
||||
coordinator.requestBackgroundSync('account-1', true);
|
||||
assert.equal(generations.get('account-1'), 4);
|
||||
assert.equal(failures.has('account-1'), false);
|
||||
assert.equal(failures.get('account-2').retryAt, 0);
|
||||
assert.equal(snapshots.has('account-1'), false);
|
||||
});
|
||||
Reference in New Issue
Block a user