Files
wekan/tests/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

102 lines
7.0 KiB
JavaScript

'use strict';
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { prepareRulePlan } = require('../server/lib/syncRulePlan');
const { ensureRuleArchiveCommand } = require('../server/lib/syncRuleArchiveCommand');
const { prepareRuleArchiveEffects: prepare, validateRuleArchiveEffects: validate,
ensureRuleArchiveEffects: ensure, applyRuleArchiveEffects: apply } = require('../server/lib/syncRuleArchiveEffects');
const { canonical, sha256, hashHistoryRow } = require('../models/lib/changeHistoryIntegrity');
async function fixture() {
const f = { activity: { _id: 'activity', boardId: 'board', cardId: 'root', userId: 'actor' },
effectId: 'a'.repeat(64), index: 0, assertCurrent: async () => {}, assertCard: async () => {},
username: 'Original name', lists: [{ _id: 'list', boardId: 'board', title: 'Original list' }],
policy: { activities: true, notifications: false } };
f.plan = await prepareRulePlan({ ...f, selectRules: async () => [{ _id: 'rule', boardId: 'board', triggerId: 'trigger', actionId: 'action' }],
readAction: async () => ({ _id: 'action', actionType: 'archive' }) });
f.cards = ['root', 'a', 'b'].map(_id => ({ _id, boardId: 'board', listId: 'list', swimlaneId: 'lane',
title: _id, archived: _id === 'a', ...(_id !== 'root' ? { parentId: 'root' } : {}) }));
let command;
f.command = await ensureRuleArchiveCommand({ ...f, commands: { findOne: async () => command, insertOne: async row => { command = row; } },
readCard: async () => f.cards[0], readChildren: async id => f.cards.filter(card => card.parentId === id), now: () => new Date(1000) });
f.effects = prepare(f);
return f;
}
function rehash(saved) { delete saved.checksum; saved.checksum = sha256(canonical(saved)); }
test('archive effects retain captured metadata; History rows are content, linked only when written', async () => {
const f = await fixture(), rows = f.effects.rows;
assert.equal(rows[0].history.rows.length, 0); assert.equal(rows[0].activities.rows.length, 0);
// Maintainer decision of 2026-09-30: no chain link is planned, so the runner
// needs no History reservation between planning and writing.
assert.equal(f.effects.version, 2); assert.equal(Object.hasOwn(f.effects, 'previousHash'), false);
for (const row of [rows[1].history.rows[0], rows[2].history.rows[0]]) {
assert.equal(Object.hasOwn(row, 'previousHash') || Object.hasOwn(row, 'integrityHash'), false);
}
assert.equal(rows[1].activities.rows[0].activity.listName, 'Original list');
assert.equal(rows[2].activities.rows[0].activity.cardTitle, 'root');
assert.equal(rows[2].activities.context.createdAt.getTime(), 1000);
f.lists[0].title = 'changed'; assert.equal(validate(f.effects, f.command, f).rows[1].activities.rows[0].activity.listName, 'Original list');
});
test('disabled activities preserve History while omitting activity delivery plans', async () => {
const f = await fixture(); f.policy = { activities: false, notifications: false }; f.lists = [];
const result = prepare(f);
assert.ok(result.rows.every(row => row.activities === null));
assert.equal(result.rows.flatMap(row => row.history.rows).length, 2);
});
test('identity, actor, timestamp, policy and an old planned chain are refused', async () => {
const f = await fixture();
for (const change of [s => { s.commandHash = 'b'.repeat(64); }, s => s.rows.reverse(),
s => { s.rows[1].history.userId = 'other'; }, s => { s.rows[1].activities.context.createdAt = new Date(2000); },
s => { s.rows[1].policy.notifications = true; }, s => {
// A row planned with a chain link: the format before 2026-09-30.
const row = s.rows[2].history.rows[0]; row.previousHash = null; row.integrityHash = hashHistoryRow(row);
}, s => { s.version = 1; s.previousHash = null; }]) {
const saved = structuredClone(f.effects); change(saved); rehash(saved);
assert.throws(() => validate(saved, f.command, f));
}
});
test('first persisted effects survive changed metadata and lost insert replies', async () => {
const f = await fixture(); let row;
const effects = { findOne: async () => structuredClone(row), insertOne: async value => { row = structuredClone(value); throw Error('lost reply'); } };
const saved = await ensure({ ...f, effects, build: async () => f.effects });
f.lists[0].title = 'changed';
assert.deepEqual(await ensure({ ...f, effects, build: () => assert.fail('no rebuild') }), saved);
row.rows[1].activities.rows[0].activity.cardTitle = 'corrupt';
await assert.rejects(ensure({ ...f, effects, build: () => assert.fail() }));
});
test('missing lists and false persistence fail before execution', async () => {
const f = await fixture(); assert.throws(() => prepare({ ...f, lists: [] }));
await assert.rejects(ensure({ ...f, effects: { findOne: async () => null, insertOne: async () => {} },
build: async () => f.effects }), /unconfirmed/);
});
test('changed live policy or missing delivery adapters cannot reach card writes', async () => {
const f = await fixture();
const cards = { findOne: async () => assert.fail('no card reads'), updateOne: async () => assert.fail('no writes') };
const history = { findOneAsync() {}, insertAsync() {}, updateAsync() {} };
const activities = { findOneAsync() {}, insertAsync() {} };
const receipts = { findOne: async () => null, insertOne: async () => assert.fail() };
await assert.rejects(apply({ ...f, cards, history, activities, receipts,
readPolicy: async () => ({ activities: false, notifications: false }), completeDelivery: async () => {} }), /policy/);
await assert.rejects(apply({ ...f, cards, history, activities, receipts, readPolicy: async () => f.policy }), /effects-invalid/);
});
test('cascade activity adapter restricts IDs and payloads while preserving scoped delivery deferral', async () => {
const { createRuleArchiveActivities } = require('../server/lib/syncRuleArchiveActivities');
const { deferSyncActivity } = require('../server/lib/syncActivityScope');
const f = await fixture(); let actor, inserts = 0;
const adapter = createRuleArchiveActivities({ ...f, withActor: async (id, work) => {
actor = id; try { return await work(); } finally { actor = undefined; }
}, activities: { findOneAsync: async (id, options) => { assert.equal(options.transform, null); return { _id: id }; },
insertAsync: async event => {
inserts++; assert.equal(actor, 'actor');
for (const kind of ['timestamps', 'notificationIntent', 'rules', 'notifications']) assert.equal(deferSyncActivity(kind, event), true);
return event._id;
} } });
const event = f.effects.rows[1].activities.rows[0].activity;
assert.equal(await adapter.insertAsync(event), event._id);
assert.equal(actor, undefined); assert.equal(deferSyncActivity('rules', event), false);
assert.equal((await adapter.findOneAsync(event._id))._id, event._id);
assert.throws(() => adapter.findOneAsync('unrelated'), /activities-invalid/);
assert.throws(() => adapter.insertAsync({ ...event, _id: 'unrelated' }), /activities-invalid/);
await assert.rejects(adapter.insertAsync({ ...event, cardTitle: 'changed' }), /adapter-invalid/);
assert.equal(inserts, 1);
});