Files
wekan/tests/syncRuleAddSwimlaneCommand.test.cjs
T
Lauri Ojansivu 4764b8aa42 Run the addSwimlane rule action through durable Sync.
An addSwimlane rule now has a saved command: the title is resolved once
by RulesHelper.ruleSwimlaneTitle, the swimlane is inserted once under an
id derived from the rule invocation, and the insert hook's
createSwimlane activity is written once, by a derived id, by the command
itself. A swimlane activity has no card, and WeKan's durable activity
delivery is keyed by a card, so that activity is delivered the way every
board-level activity is; what the command guarantees is that the
swimlane and its activity each exist exactly once across replays.

The stored-rule tests now use moveAllCardsInList as their example of an
action with no durable adapter.

Thanks to xet7 !
2026-10-02 17:14:15 +03:00

63 lines
3.8 KiB
JavaScript

'use strict';
// Durable rule addSwimlane (server/lib/syncRuleAddSwimlaneCommand.js).
// Run: node tests/syncRuleAddSwimlaneCommand.test.cjs
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { prepareRulePlan } = require('../server/lib/syncRulePlan');
const A = require('../server/lib/syncRuleAddSwimlaneCommand');
const { canonical, sha256 } = require('../models/lib/changeHistoryIntegrity');
const ROOT = path.join(__dirname, '..');
async function fixture(actionType = 'addSwimlane') {
const f = { activity: { _id: 'activity', boardId: 'board', cardId: 'card', userId: 'actor' },
effectId: 'a'.repeat(64), index: 0, assertCurrent: async () => {} };
f.plan = await prepareRulePlan({ ...f,
selectRules: async () => [{ _id: 'rule', boardId: 'board', triggerId: 'trigger', actionId: 'action' }],
readAction: async () => ({ _id: 'action', boardId: 'board', actionType, swimlaneName: 'Lane for {cardTitle}' }) });
f.context = { plan: f.plan, activity: f.activity, effectId: f.effectId, index: 0 };
f.prepare = over => A.prepareRuleAddSwimlaneCommand({ ...f.context, title: 'Lane for T', createdAt: new Date(1000), ...over });
return f;
}
test('one derived swimlane and the hook\'s activity, each by its own id', async () => {
const f = await fixture();
const command = f.prepare();
assert.deepEqual(command.swimlane, { _id: A.swimlaneIdFor(command._id), title: 'Lane for T', boardId: 'board', sort: 0 });
const { _id, createdAt, modifiedAt, ...activity } = command.activity;
assert.deepEqual(activity, { userId: 'actor', type: 'swimlane', activityType: 'createSwimlane', boardId: 'board',
swimlaneId: command.swimlane._id }, 'the insert hook\'s own activity');
assert.equal(f.prepare({ createdAt: new Date(5) }).swimlane._id, command.swimlane._id, 'a replay names the same swimlane');
assert.deepEqual(A.validateRuleAddSwimlaneCommand(command, f.context), command);
});
test('negative: another action and tampering are refused', async () => {
const other = await fixture('addChecklist');
assert.throws(() => other.prepare(), /invalid/);
const f = await fixture();
const command = f.prepare();
const resum = row => { const { checksum, ...content } = row; return { ...content, checksum: sha256(canonical(content)) }; };
assert.throws(() => A.validateRuleAddSwimlaneCommand(resum({ ...command, swimlane: { ...command.swimlane, boardId: 'x' } }),
f.context), /command-invalid/);
assert.throws(() => A.validateRuleAddSwimlaneCommand(resum({ ...command, activity: { ...command.activity, userId: 'x' } }),
f.context), /command-invalid/);
});
test('wiring: durable, registered, the hook deferred only for that swimlane', async () => {
const { DURABLE_RULE_ACTIONS } = require('../server/lib/listSyncSteps');
assert.ok(DURABLE_RULE_ACTIONS.has('addSwimlane'));
const plans = fs.readFileSync(path.join(ROOT, 'server/notifications/storedRulePlans.js'), 'utf8');
assert.match(plans, /addSwimlane: \(\{ invocation \}\) => runStoredSyncRuleAddSwimlane\(/);
assert.match(plans, /RulesHelper\.ruleSwimlaneTitle\(/);
assert.match(fs.readFileSync(path.join(ROOT, 'server/models/swimlanes.js'), 'utf8'),
/if \(!deferSyncSwimlaneActivity\(doc\)\) await Activities\.insertAsync\(\{/);
const { withSyncSwimlaneActivityDeferred, deferSyncSwimlaneActivity } = require('../server/lib/syncRecordingScope');
assert.equal(deferSyncSwimlaneActivity({ _id: 's1' }), false, 'no scope, no deferral');
await withSyncSwimlaneActivityDeferred('s1', async () => {
assert.equal(deferSyncSwimlaneActivity({ _id: 's2' }), false, 'another swimlane');
assert.equal(deferSyncSwimlaneActivity({ _id: 's1' }), true);
assert.equal(deferSyncSwimlaneActivity({ _id: 's1' }), false, 'one shot');
});
});