Files
wekan/tests/undoRedoSelection.test.cjs
T
Lauri Ojansivu 4ca33d29f7 #6478: undo/redo for card/list/swimlane moves + confirm high-impact list move on touch.
A user accidentally dragged a list into another swimlane on mobile (merging it
with an existing list) and spent ~15 min recovering - no confirmation, no undo.

Undo/Redo: the userPositionHistory collection modelled this but was NOT actually
recording (trackChange guarded on an un-imported global) and had no redo or key
bindings. Now:
- list moves are recorded in updateListSort (import UserPositionHistory so the
  guard passes); card moves already had the call.
- add a symmetric redo() model method + an undone/undoneAt redo stack; add
  userPositionHistory.undoLast/redoLast methods (a new action clears the redo
  stack via trackChange).
- wire Ctrl+Z / Ctrl+Y (and Cmd variants) in client/lib/keyboard.js; disabled
  inside inputs/textareas so native text undo is unaffected.
- pure selection logic models/lib/undoRedoSelection.js + tests.

Confirmation: on touch / small screens, moving a list to a DIFFERENT swimlane
(which re-homes every card) now asks for confirmation; a decline reverts the
drag. Same-swimlane reordering is unaffected.

Thanks to mueschel (report) and xet7.
2026-07-17 18:44:14 +03:00

78 lines
2.8 KiB
JavaScript

'use strict';
// Unit test for #6478: the position-history undo/redo stack selection rules.
// pickUndo/pickRedo decide which change Ctrl+Z / Ctrl+Y act on; the server
// (server/models/userPositionHistory.js) applies the same rule via Mongo sort.
//
// Run: node tests/undoRedoSelection.test.cjs
const assert = require('assert');
const { pickUndo, pickRedo } = require('../models/lib/undoRedoSelection');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
const t = n => new Date(2026, 0, 1, 0, 0, n); // increasing timestamps by second
test('pickUndo returns the most recent non-undone change', () => {
const entries = [
{ _id: 'a', createdAt: t(1) },
{ _id: 'b', createdAt: t(3) },
{ _id: 'c', createdAt: t(2) },
];
assert.strictEqual(pickUndo(entries)._id, 'b');
});
test('pickUndo skips already-undone changes and checkpoints', () => {
const entries = [
{ _id: 'a', createdAt: t(1) },
{ _id: 'b', createdAt: t(4), undone: true }, // already undone
{ _id: 'chk', createdAt: t(5), isCheckpoint: true }, // checkpoint
{ _id: 'c', createdAt: t(2) },
];
assert.strictEqual(pickUndo(entries)._id, 'c');
});
test('pickUndo returns null when everything is undone/empty', () => {
assert.strictEqual(pickUndo([]), null);
assert.strictEqual(pickUndo([{ _id: 'a', createdAt: t(1), undone: true }]), null);
});
test('pickRedo returns the MOST-RECENTLY-undone change (by undoneAt, not createdAt)', () => {
// a was created first but undone LAST, so redo must re-apply a before b.
const entries = [
{ _id: 'a', createdAt: t(1), undone: true, undoneAt: t(9) },
{ _id: 'b', createdAt: t(2), undone: true, undoneAt: t(8) },
];
assert.strictEqual(pickRedo(entries)._id, 'a');
});
test('pickRedo ignores non-undone changes and returns null when none undone', () => {
const entries = [
{ _id: 'a', createdAt: t(1) },
{ _id: 'b', createdAt: t(2) },
];
assert.strictEqual(pickRedo(entries), null);
});
test('undo then redo of the same change is symmetric (LIFO)', () => {
// Two moves recorded; undo picks the newer (b); after marking b undone, undo
// picks a; redo then re-applies the most-recently-undone (a), then b.
let entries = [
{ _id: 'a', createdAt: t(1) },
{ _id: 'b', createdAt: t(2) },
];
assert.strictEqual(pickUndo(entries)._id, 'b');
entries = entries.map(e => (e._id === 'b' ? { ...e, undone: true, undoneAt: t(5) } : e));
assert.strictEqual(pickUndo(entries)._id, 'a');
entries = entries.map(e => (e._id === 'a' ? { ...e, undone: true, undoneAt: t(6) } : e));
assert.strictEqual(pickUndo(entries), null, 'nothing left to undo');
assert.strictEqual(pickRedo(entries)._id, 'a', 'redo re-applies the most-recently-undone first');
});
console.log(`\nAll ${passed} undo/redo selection tests passed`);