103 lines
4.4 KiB
JavaScript
103 lines
4.4 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { applyQueuedActionToCache, createMailOutboxService } = require('../desktop/mail/outbox-service.cjs');
|
|
|
|
function setup({ failSend = null, failAction = null, connectivity = () => false } = {}) {
|
|
const calls = { sends: [], actions: [], errors: [], updates: 0 };
|
|
const store = { accounts: [{
|
|
id: 'account-1',
|
|
mailOutbox: [],
|
|
mailCache: { Inbox: [{ uid: 7, subject: 'One', star: false }] }
|
|
}] };
|
|
const service = createMailOutboxService({
|
|
async sendMailNow(account, data) {
|
|
calls.sends.push([account.id, data]);
|
|
if (failSend) throw failSend;
|
|
},
|
|
async applyMessageAction(account, data) {
|
|
calls.actions.push([account.id, data]);
|
|
if (failAction) throw failAction;
|
|
},
|
|
async updateAccountStore(update) { calls.updates += 1; return update(store); },
|
|
isConnectivityFailure: connectivity,
|
|
createId: () => 'queued-action',
|
|
now: () => 12345,
|
|
logError(...args) { calls.errors.push(args); }
|
|
});
|
|
return { service, calls, store };
|
|
}
|
|
|
|
test('flushes queued sends and actions in order and removes completed operations', async () => {
|
|
const { service, calls, store } = setup();
|
|
const account = {
|
|
id: 'account-1',
|
|
mailOutbox: [
|
|
{ id: 'send-1', type: 'send', data: { to: 'a@example.com' } },
|
|
{ id: 'action-1', type: 'action', data: { uid: 7, action: 'star' } }
|
|
]
|
|
};
|
|
store.accounts[0].mailOutbox = structuredClone(account.mailOutbox);
|
|
|
|
await service.flushMailOutbox(account);
|
|
assert.deepEqual(calls.sends, [['account-1', { to: 'a@example.com' }]]);
|
|
assert.deepEqual(calls.actions, [['account-1', { uid: 7, action: 'star' }]]);
|
|
assert.deepEqual(store.accounts[0].mailOutbox, []);
|
|
assert.equal(calls.updates, 2);
|
|
});
|
|
|
|
test('stops at a connectivity failure and leaves the failed and later operations queued', async () => {
|
|
const networkError = Object.assign(new Error('network down'), { code: 'ECONNRESET' });
|
|
const { service, calls, store } = setup({ failSend: networkError, connectivity: error => error.code === 'ECONNRESET' });
|
|
const operations = [
|
|
{ id: 'send-1', type: 'send', data: {} },
|
|
{ id: 'action-1', type: 'action', data: { uid: 7, action: 'star' } }
|
|
];
|
|
store.accounts[0].mailOutbox = structuredClone(operations);
|
|
await service.flushMailOutbox({ id: 'account-1', mailOutbox: operations });
|
|
|
|
assert.deepEqual(store.accounts[0].mailOutbox, operations);
|
|
assert.equal(calls.actions.length, 0);
|
|
assert.equal(calls.errors.length, 0);
|
|
});
|
|
|
|
test('logs non-connectivity failures and stops without deleting the failed item', async () => {
|
|
const failure = new Error('invalid queued operation');
|
|
const { service, calls, store } = setup({ failAction: failure });
|
|
const operations = [{ id: 'action-1', type: 'action', data: { uid: 7, action: 'star' } }];
|
|
store.accounts[0].mailOutbox = structuredClone(operations);
|
|
await service.flushMailOutbox({ id: 'account-1', mailOutbox: operations });
|
|
|
|
assert.deepEqual(store.accounts[0].mailOutbox, operations);
|
|
assert.equal(calls.errors.length, 1);
|
|
assert.match(calls.errors[0][0], /action-1/);
|
|
});
|
|
|
|
test('queues an action once, updates cached message state, and moves it to its optimistic folder', async () => {
|
|
const { service, store } = setup();
|
|
const result = await service.queueMessageAction({ id: 'account-1' }, {
|
|
uid: 7, folder: 'Inbox', action: 'archive'
|
|
});
|
|
|
|
assert.deepEqual(result, { ok: true, queued: true });
|
|
assert.equal(store.accounts[0].mailOutbox.length, 1);
|
|
assert.deepEqual(store.accounts[0].mailOutbox[0], {
|
|
id: 'queued-action',
|
|
type: 'action',
|
|
data: { uid: 7, folder: 'Inbox', action: 'archive' },
|
|
createdAt: 12345
|
|
});
|
|
assert.equal(store.accounts[0].mailCache.Inbox.length, 0);
|
|
assert.deepEqual(store.accounts[0].mailCache.Archive[0], {
|
|
uid: 7, subject: 'One', star: false, sourceFolder: 'Archive', archived: true, deleted: false
|
|
});
|
|
|
|
await service.queueMessageAction({ id: 'account-1' }, { uid: 7, folder: 'Inbox', action: 'archive' });
|
|
assert.equal(store.accounts[0].mailOutbox.length, 1);
|
|
});
|
|
|
|
test('optimistic flag changes update the cached record without moving it', () => {
|
|
const account = { mailCache: { Inbox: [{ uid: 2, star: false }] } };
|
|
applyQueuedActionToCache(account, { uid: 2, folder: 'Inbox', action: 'star', enabled: true });
|
|
assert.deepEqual(account.mailCache.Inbox, [{ uid: 2, star: true }]);
|
|
});
|