Files
wekan/tests/cardMoveAwaitBoard.test.cjs
T
Lauri Ojansivu fe27dc0b8e Fix #6560: moving a card to another board blanked every custom field id.
The values survived the move; their keys did not. Each entry came out with
`_id: ""`, which no board can match to a field definition, so the card arrived on
the destination board with every custom field showing empty. Drag-and-drop, the
Move card popup and the REST API `newBoardId` move all go through
Cards.helpers.move(), and Cards.helpers.copy() had it too.

`mapCustomFieldsToBoard()` was SYNCHRONOUS and called
`ReactiveCache.getCustomField()`, which is async on the SERVER - it awaits
findOneAsync - and synchronous only on the client. So on the server both lookups
returned Promises. A Promise is truthy, so `if (!oldCf)` never fired and the
`if (newCf)` branch always did, assigning `newCf._id`: `undefined` on a Promise.
The schema declares `customFields.$._id` as `optional: true, defaultValue: ''`, so
collection2 cleaned that undefined to `''` on the way to the database. Nothing
threw and nothing was logged.

Both lookups are awaited now, and so is `oldCf.addBoard(boardId)` - also async,
and until the two above were awaited its branch was unreachable, so its own
missing await had never been exercised.

Two things found while fixing it, in the same function:

  * The entries are rebuilt rather than mutated. copy() deliberately works on a
    shallow copy of the card "to avoid mutating the source card in ReactiveCache",
    and `cf._id = newCf._id` reached straight through that copy into the source
    card's own entry objects - re-keying the card being copied FROM.
  * An entry with no id is passed through instead of looked up.
    getCustomField() defaults its selector to `{}`, so `getCustomField(undefined)`
    returns an ARBITRARY custom field, and a blank entry - including one blanked
    by this very bug - would have been re-keyed to whichever field came first.

This is the same defect as #6504 (unawaited ReactiveCache.getBoard in this same
cross-board branch of move(), which gave "newBoard.getNextCardNumber is not a
function"), one line further down. The guard added then named the two getBoard
calls and stopped there, so the third call shipped broken for twenty more
releases; tests/cardMoveAwaitBoard.test.cjs now checks the branch as a whole and
fails on any unawaited async call in it - verified against the pre-fix file, where
it reports mapCustomFieldsToBoard.

tests/cardCustomFieldsMove.test.cjs lifts the real function out of models/cards.js
and runs it against both worlds - an async ReactiveCache (the server, where this
broke) and a synchronous one (the client, where it always worked) - which must
give the same answer: a value whose field exists on the destination board by name
and type is re-keyed to that board's definition, and one whose field does not is
left pointing at the definition it had, which gains the destination board so the
value still resolves. It also pins that addBoard is finished before the array is
returned, that an unknown field id costs no value, that a blank entry is never
matched against an arbitrary field, that the source card keeps its own ids, and
that both call sites await.

Cards moved or copied between boards on an affected release are NOT repaired by
this: the entry kept its value but lost the only reference to which field it
belonged to, so there is nothing left to match it back with. Those values have to
be set again.

Thanks to ChristianMa97 and xet7 !

Fixes #6560,
2026-07-31 01:39:44 +03:00

62 lines
3.1 KiB
JavaScript

'use strict';
// Regression guard for #6504: "Rule to move Card is failing" with
// TypeError: newBoard.getNextCardNumber is not a function
// On the server ReactiveCache.getBoard() is ASYNC (returns a Promise). In
// Cards.helpers.move(), the cross-board branch called it WITHOUT await, so
// `newBoard` was a Promise: `newBoard.getNextCardNumber()` threw, and `oldBoard`
// being a Promise silently dropped label carry-over. Both getBoard calls in the
// cross-board branch of move() must be awaited.
// Run: node tests/cardMoveAwaitBoard.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
let passed = 0;
function check(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
const src = fs.readFileSync(path.join(__dirname, '..', 'models', 'cards.js'), 'utf8');
check('move(): the cross-board branch awaits both getBoard() calls', () => {
const at = src.indexOf('async move(');
assert.ok(at > -1, 'async move() must exist');
// Look within the move() body (bounded window covering the cross-board branch).
const body = src.slice(at, at + 4000);
assert.ok(/const oldBoard = await ReactiveCache\.getBoard\(this\.boardId\)/.test(body),
'oldBoard must be awaited (else label carry-over is silently lost on move)');
assert.ok(/const newBoard = await ReactiveCache\.getBoard\(boardId\)/.test(body),
'newBoard must be awaited (else newBoard.getNextCardNumber is not a function)');
});
check('the cross-board branch still calls getNextCardNumber on the awaited board', () => {
assert.ok(/await newBoard\.getNextCardNumber\(\)/.test(src),
'the number is taken from the destination board');
});
check('#6560: EVERY await-needing call in the cross-board branch is awaited', () => {
// This guard named the two getBoard() calls of #6504 and stopped there. The very
// next line, mapCustomFieldsToBoard(), had the identical defect - a helper that
// is async on the server, called without await - and shipped for another twenty
// releases, blanking the custom field id of every card moved between boards.
// So check the branch as a whole rather than the two calls that were noticed.
const at = src.indexOf('async move(');
const branchAt = src.indexOf('if (this.boardId !== boardId) {', at);
assert.ok(branchAt > at, 'the cross-board branch must exist');
const branch = src.slice(branchAt, branchAt + 4000);
const unawaited = [];
// Server-async collaborators used inside the branch. ReactiveCache is async on
// the server and sync on the client; the card helpers below are async outright.
const asyncCalls = /(?:^|[^.\w])(?:(await)\s+)?(?:ReactiveCache\.\w+|this\.mapCustomFieldsToBoard|newBoard\.getNextCardNumber|oldBoard\.getNextCardNumber)\s*\(/gm;
for (const m of branch.matchAll(asyncCalls)) {
if (!m[1]) unawaited.push(m[0].trim());
}
assert.deepStrictEqual(unawaited, [],
'an unawaited async call puts a Promise into the document: getBoard gave '
+ '"newBoard.getNextCardNumber is not a function" (#6504), '
+ 'mapCustomFieldsToBoard gave customFields._id: "" (#6560)');
});
console.log(`\ncardMoveAwaitBoard: ${passed} checks passed`);