Files
wekan/server/lib/tests/scrumHistoryRecovery.tests.js
T
Lauri Ojansivu 9ad9813c09 Let a board admin resolve a Scrum undo or redo that stopped on a conflict.
A Scrum History undo, redo or restore keeps a checkpoint that blocks every
Scrum edit on its board until it finishes, and only its author could retry
it, with the same plan. When somebody else had changed one of its records,
its History row changed or its author lost write access, every retry failed
the same way, the checkpoint could not be discarded online or offline, and
the board's Scrum edits stayed blocked for good.

A retry that fails for a reason the next one would meet again now marks its
checkpoint. A board administrator sees such a checkpoint in the History
recovery notice and can roll it back, which restores every record it wrote
to the saved "before" value and revision and is offered only when nobody
changed those records since and none of its History was written, or keep the
board as it is, which removes the checkpoint and writes nothing while
reporting how much of the operation had been written. A checkpoint that has
not stopped on a conflict stays its author's to retry. The resolution names
the checkpoint it resolves, so a repeat is a no-op; it claims the checkpoint
by compare-and-set, fencing the author's workers, waits for an in-flight
write, and checks every record before and after its own writes. Each one is
recorded in Admin Panel / Problems / Recovery. The offline command
releases/recover-scrum-history.cjs does the same with every writer stopped.

Unit, integration (against MongoDB 7), Meteor and Playwright tests cover
rollback, discard, refusals, concurrency and the offline command. New text
is in English, pending Transifex.

Thanks to xet7 !
2026-10-08 12:06:49 +03:00

81 lines
5.4 KiB
JavaScript

import assert from 'node:assert/strict';
import { Meteor } from 'meteor/meteor';
import { DDP } from 'meteor/ddp';
import { Random } from 'meteor/random';
import Boards from '/models/boards';
import ChangeHistory from '/models/changeHistory';
import ScrumSprints from '/models/scrumSprints';
import RecoveryEvents from '/models/recoveryEvents';
import ScrumHistoryPending, { ScrumHistoryCompletions } from '/server/lib/scrumHistoryPending';
import { applyScrumHistory } from '/server/lib/scrumHistory';
// A Scrum History undo whose retries keep meeting the same conflict used to
// block the board's Scrum edits for good (server/lib/scrumHistoryRecovery.js).
// The retry now marks its checkpoint, and a board administrator - not an
// ordinary member - resolves it through the server methods.
describe('Scrum History checkpoint recovery', function () {
this.timeout(30000);
it('records the lasting failure and lets only a board administrator discard it, once', async function () {
if (!Meteor.isAppTest) this.skip();
const adminId = Random.id(), memberId = Random.id(), boardId = Random.id();
const as = userId => {
const context = { userId, isSimulation: false, connection: null, setUserId() {}, unblock() {} };
return {
actor: fn => DDP._CurrentMethodInvocation.withValue(context, fn),
invoke: (method, ...args) => DDP._CurrentMethodInvocation.withValue(context,
() => Meteor.server.method_handlers[method].apply(context, args)),
};
};
const admin = as(adminId), member = as(memberId);
try {
for (const id of [adminId, memberId]) await Meteor.users.rawCollection().insertOne({ _id: id, username: `recovery-${id}`, profile: {} });
await Boards.rawCollection().insertOne({ _id: boardId, title: 'Stopped undo', permission: 'private', archived: false,
members: [{ userId: adminId, isAdmin: true, isActive: true }, { userId: memberId, isAdmin: false, isActive: true }] });
const created = await admin.invoke('scrum.saveSprint', boardId, null, { name: 'Sprint 1' });
const row = await ChangeHistory.findOneAsync({ boardId, entityType: 'scrum' }, { sort: { createdAt: -1 } });
// The undo deletes the sprint - but somebody replaces it with another
// lifetime of the same record first, so every retry meets a conflict.
const originalRemove = ScrumSprints.removeAsync;
ScrumSprints.removeAsync = async function (selector, ...args) {
await ScrumSprints.rawCollection().updateOne({ _id: created._id }, { $set: { incarnation: 'someone-else' } });
return originalRemove.call(this, selector, ...args);
};
try { await assert.rejects(admin.actor(() => applyScrumHistory(row, row.previousContent, 'undo')), /scrum-conflict/); }
finally { ScrumSprints.removeAsync = originalRemove; }
await assert.rejects(admin.actor(() => applyScrumHistory(row, row.previousContent, 'undo')), /scrum-conflict/);
const checkpoint = await ScrumHistoryPending.findOneAsync(boardId);
assert.equal(checkpoint.lastFailure.error, 'scrum-conflict', 'the retry marked its checkpoint');
await assert.rejects(admin.invoke('scrum.saveSprint', boardId, null, { name: 'Blocked' }), /scrum-history-pending/);
// An ordinary member sees it is stuck, not what it holds, and cannot resolve it.
const seen = await member.invoke('scrum.inspectHistoryCheckpoint', boardId);
assert.equal(seen.stuck, true); assert.equal(seen.canResolve, false); assert.equal(seen.targets, undefined);
for (const action of ['rollback', 'discard']) {
await assert.rejects(member.invoke('scrum.resolveHistoryCheckpoint', boardId, seen.key, action), /not-authorized/);
}
assert.ok(await ScrumHistoryPending.findOneAsync(boardId));
const report = await admin.invoke('scrum.inspectHistoryCheckpoint', boardId);
assert.equal(report.canResolve, true); assert.equal(report.canRollback, false, 'the other lifetime is never overwritten');
await assert.rejects(admin.invoke('scrum.resolveHistoryCheckpoint', boardId, report.key, 'rollback'), /scrum-history-recovery/);
const result = await admin.invoke('scrum.resolveHistoryCheckpoint', boardId, report.key, 'discard');
assert.equal(result.state, 'discarded');
assert.equal(await ScrumHistoryPending.findOneAsync(boardId), undefined);
assert.equal((await ScrumSprints.findOneAsync(created._id)).incarnation, 'someone-else');
const events = await RecoveryEvents.find({ type: 'scrum-history-checkpoint-resolved', boardIds: boardId }).fetchAsync();
assert.deepEqual(events.map(event => event.done), [false, true], 'the refused rollback and the discard');
// Scrum edits work again; a repeated discard is a no-op.
await admin.invoke('scrum.saveSprint', boardId, null, { name: 'Unblocked' });
assert.equal((await admin.invoke('scrum.resolveHistoryCheckpoint', boardId, report.key, 'discard')).changed, false);
} finally {
await ScrumSprints.rawCollection().deleteMany({ boardId });
await ScrumHistoryPending.rawCollection().deleteMany({ _id: boardId });
await ScrumHistoryCompletions.rawCollection().deleteMany({ boardId });
await ChangeHistory.rawCollection().deleteMany({ boardId });
await RecoveryEvents.rawCollection().deleteMany({ boardIds: boardId });
await Boards.rawCollection().deleteOne({ _id: boardId });
await Meteor.users.rawCollection().deleteMany({ _id: { $in: [adminId, memberId] } });
}
});
});