Files
wekan/tests/integration/syncRuleArchiveEffects.test.cjs
T
Lauri Ojansivu 76f6eba249 Link Sync History rows to the chain when they are written, not planned.
Sync and the stored rule archive runner fixed each History row's
previousHash and integrityHash when they PLANNED a batch, so the board's
History chain had to stay unchanged from planning until the last row was
written - which needed a multi-row History reservation that would block
or queue ordinary edits, and made a coordinated board refuse Sync.
Maintainer decision of 2026-09-30: plans are content only, and each row
is linked when it is appended, like an ordinary edit.

On a board still on the legacy path, the batch holds one writer token
(so a chain migration drains it) and each row is linked after the
chain's tip - the newest hashed row, then its successors, since the rows
of one batch share a planned createdAt and "newest" alone is a tie that
forks the chain. On a coordinated board each row goes through the chain
head. Appends are idempotent by row _id, and a replay accepts only the
planned content. The archive runner needs no reservation any more; it
captures the actor's redo candidates when it plans. Plans in the old
chained format are refused. An index on (boardId, previousHash) serves
the tip walk.

Unit and MongoDB tests: plans carry no link; rows chain correctly in
both modes and replay once; ordinary History written between planning
and writing is neither blocked nor forked; a coordinated board appends
through the head; old-format plans and adapters without admission or
linking are refused. The full-app archive case now runs without a
reservation and verifies the resulting chain. 561 server tests pass;
the one other failure in that run, the activity notification recovery
scan, is intermittent and passes on its own.

Thanks to xet7 !
2026-09-30 10:35:57 +03:00

66 lines
5.2 KiB
JavaScript

'use strict';
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { linkingHistory } = require('./syncHistoryLinking.cjs');
const { MongoClient, ObjectId } = require('mongodb');
const { prepareRulePlan } = require('../../server/lib/syncRulePlan');
const { ensureRuleArchiveCommand } = require('../../server/lib/syncRuleArchiveCommand');
const { applyRuleArchiveEffects: apply, prepareRuleArchiveEffects, ensureRuleArchiveEffects } = require('../../server/lib/syncRuleArchiveEffects');
const uri = process.env.WEKAN_SYNC_TEST_MONGO_URL;
test('stored History and activities resume after delivery interruption without duplicate card writes or events', { skip: !uri }, async t => {
const client = await new MongoClient(uri).connect(), db = client.db(`archive_effects_${new ObjectId().toHexString()}`);
t.after(async () => { await db.dropDatabase(); await client.close(); });
const cards = db.collection('cards'), receipts = db.collection('receipts');
await cards.insertMany(['root', 'child'].map(_id => ({ _id, boardId: 'board', listId: 'list',
swimlaneId: 'lane', title: _id, archived: false, ...(_id === 'child' ? { parentId: 'root', archivedAt: null } : {}) })));
const f = { activity: { _id: 'activity', boardId: 'board', cardId: 'root', userId: 'actor' },
effectId: 'a'.repeat(64), index: 0, assertCurrent: async () => {}, assertCard: async () => {} };
f.plan = await prepareRulePlan({ ...f,
selectRules: async () => [{ _id: 'rule', boardId: 'board', triggerId: 'trigger', actionId: 'action' }],
readAction: async () => ({ _id: 'action', actionType: 'archive' }) });
f.command = await ensureRuleArchiveCommand({ ...f, commands: db.collection('commands'),
readCard: id => cards.findOne({ _id: id }), readChildren: id => cards.find({ parentId: id }).toArray(),
now: () => new Date(1000) });
f.policy = { activities: true, notifications: false };
const prepared = prepareRuleArchiveEffects({ ...f, username: 'Actor', lists: [{ _id: 'list', boardId: 'board', title: 'Saved list' }] });
f.effects = await ensureRuleArchiveEffects({ ...f, effects: db.collection('effects'), build: async () => prepared });
const collectionAdapter = collection => ({ admitHistoryWriter:({work})=>work({mode:'legacy',assertCurrent:async()=>{}}),appendSyncHistoryRow:async function({row}){if(await this.findOneAsync(row._id))return row._id;const saved={...row,previousHash:null};saved.integrityHash=require('../../models/lib/changeHistoryIntegrity').hashHistoryRow(saved);await this.insertAsync(saved);return row._id;},
findOneAsync: selector => collection.findOne(typeof selector === 'string' ? { _id: selector } : selector),
insertAsync: async row => { await collection.insertOne(row); return row._id; },
updateAsync: async (selector, modifier) => (await collection.updateOne(selector, modifier)).modifiedCount });
f.history = linkingHistory(db.collection('history'));
f.activities = collectionAdapter(db.collection('activities')); f.readPolicy = async () => f.policy;
const writes = [], delivered = [];
const wrap = collection => ({ findOne: (...args) => collection.findOne(...args),
updateOne: async (selector, modifier) => {
const result = await collection.updateOne(selector, modifier);
if (result.modifiedCount) writes.push(selector._id);
throw Error('lost write reply');
} });
f.cards = wrap(cards); f.receipts = receipts;
f.completeDelivery = async () => { throw Error('interrupted effects'); };
await assert.rejects(apply(f), /interrupted effects/);
assert.deepEqual(writes, ['child']); assert.equal(await receipts.countDocuments({}), 0);
assert.equal((await cards.findOne({ _id: 'root' })).archived, false);
assert.equal(await db.collection('history').countDocuments({}), 1);
assert.equal(await db.collection('activities').countDocuments({}), 1);
const restarted = await new MongoClient(uri).connect();
try {
const next = restarted.db(db.databaseName);
f.cards = wrap(next.collection('cards')); f.receipts = next.collection('receipts');
f.history = linkingHistory(next.collection('history')); f.activities = collectionAdapter(next.collection('activities'));
f.effects = await ensureRuleArchiveEffects({ ...f, effects: next.collection('effects'), build: () => assert.fail('must not rebuild') });
f.completeDelivery = async ({ activity, effectId }) => { delivered.push(activity.cardId); return effectId; };
assert.equal(await apply(f), f.command.invocationId);
assert.deepEqual(writes, ['child', 'root']); assert.deepEqual(delivered, ['child', 'root']);
await apply(f); assert.equal(writes.length, 2); assert.equal(delivered.length, 2);
assert.equal(await receipts.countDocuments({}), 3);
assert.equal(await db.collection('history').countDocuments({}), 2);
assert.equal(await db.collection('activities').countDocuments({}), 2);
assert.equal(await db.collection('activities').countDocuments({ listName: 'Saved list' }), 2);
const rows = await db.collection('history').find({}).sort({ cardId: 1 }).toArray();
assert.equal(rows[1].previousHash, rows[0].integrityHash);
assert.equal(await cards.countDocuments({ archived: true, archivedAt: new Date(1000) }), 2);
} finally { await restarted.close(); }
});