Files
wekan/tests/subtaskAncestors.test.cjs
T
Lauri Ojansivu a904ca8807 Let a card be a subtask of several parent cards.
"A must be done before B and C can start, so A is a subtask of both." A card
now keeps every parent in parentIds, with parentId still the primary one
(parentIds[0]), so everything that reads one parent keeps working and a card
written before this has its one parent. Picking an existing card as a
subtask adds this card as a further parent instead of replacing the one it
had; Card -> More lists the other parents and removes one, and choosing the
primary parent keeps the others. Children are found under any parent - the
subtask list and counter, the parent filter, rule email Details and the
exports - and the loop guard and the board publication walk every parent.
Archiving or deleting a card takes only the subtasks it is the one parent
of; a shared subtask just loses it. Every parent written must be on a board
the writer can see (GHSA-jvv9-498p-hxrg), and board copy and WeKan import
remap all of them. The old single-parent walker is removed.

Thanks to ikomhoog and xet7 !

Fixes #3626,
2026-09-29 17:59:21 +03:00

167 lines
7.1 KiB
JavaScript

'use strict';
// Plain-Node unit test (no Meteor) for the subtask ancestor-chain walk used by
// the `board` publication's "Parent cards (for subtasks)" child.
// Run: ELECTRON_RUN_AS_NODE=1 <node> tests/subtaskAncestors.test.cjs
//
// Regression guard for #3453 ("Full path display of subtasks cards disappears
// after a refresh of the subtask board"). The 'prefix-with-full-path' subtask
// setting renders parentString(' > ') — the subtask's WHOLE ancestor chain —
// client-side from minimongo. A cross-board subtask's ancestors live on the
// parent board, so after F5 they only exist on the client if the subtask
// board's publication ships them. Publishing only the DIRECT parents (the old
// behaviour) truncated the path to one level; collectAncestorIds() must walk
// every level, terminate on cyclic/corrupt data (#3328), and never lose ids.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
// #3626: the walk moved to models/lib/cardParents.js, where it follows EVERY
// parent of a card; server/lib/subtaskAncestors.js, which followed only
// parentId, is gone. The contract below is unchanged.
const { collectAllAncestorIds: collectAncestorIds } = require('../models/lib/cardParents');
let passed = 0;
const tests = [];
function test(name, fn) {
tests.push([name, fn]);
}
// A fetchParents stub over a card "database": id -> parentId (undefined = top
// level, id absent from the map = deleted card). Records each batch queried.
function makeFetch(cardsById, calls) {
return async ids => {
if (calls) calls.push([...ids]);
return ids
.filter(id => Object.prototype.hasOwnProperty.call(cardsById, id))
.map(id => ({ _id: id, parentId: cardsById[id] }));
};
}
// --- POSITIVE: the full path is collected ------------------------------------
test('single-level chain: the direct parent is published', async () => {
// subtask (on Board2) -> parent P1 (on Board1): the original #3453 repro.
const fetch = makeFetch({ P1: undefined });
const ids = await collectAncestorIds(['P1'], fetch);
assert.deepStrictEqual(ids.sort(), ['P1']);
});
test('#3453 full path: EVERY ancestor level is collected, not just the direct parent', async () => {
// sub -> P1 -> P2 -> P3 (P1..P3 on another board). 'prefix-with-full-path'
// renders "P3 > P2 > P1", so all three docs must be published.
const fetch = makeFetch({ P1: 'P2', P2: 'P3', P3: undefined });
const ids = await collectAncestorIds(['P1'], fetch);
assert.deepStrictEqual(ids.sort(), ['P1', 'P2', 'P3']);
});
test('multiple subtasks: chains are merged and shared ancestors appear once', async () => {
const fetch = makeFetch({ P1: 'ROOT', P2: 'ROOT', ROOT: undefined });
const ids = await collectAncestorIds(['P1', 'P2', 'P1'], fetch);
assert.deepStrictEqual(ids.sort(), ['P1', 'P2', 'ROOT']);
});
test('walks one batched query per ancestor level (no per-card queries)', async () => {
const calls = [];
const fetch = makeFetch({ A1: 'B1', A2: 'B2', B1: 'C1', B2: 'C1', C1: undefined }, calls);
const ids = await collectAncestorIds(['A1', 'A2'], fetch);
assert.deepStrictEqual(ids.sort(), ['A1', 'A2', 'B1', 'B2', 'C1']);
// Level 1: [A1, A2]; level 2: [B1, B2]; level 3: [C1]. C1 is top level, so
// the walk stops after 3 queries.
assert.deepStrictEqual(calls, [['A1', 'A2'], ['B1', 'B2'], ['C1']]);
});
// --- NEGATIVE / robustness ----------------------------------------------------
test('no parents at all: returns [] and never queries', async () => {
const calls = [];
const fetch = makeFetch({}, calls);
assert.deepStrictEqual(await collectAncestorIds([], fetch), []);
assert.deepStrictEqual(await collectAncestorIds(undefined, fetch), []);
assert.deepStrictEqual(await collectAncestorIds(null, fetch), []);
assert.strictEqual(calls.length, 0);
});
test('falsy parent ids are ignored', async () => {
const calls = [];
const fetch = makeFetch({ P1: undefined }, calls);
const ids = await collectAncestorIds([null, '', undefined, 'P1', 0], fetch);
assert.deepStrictEqual(ids.sort(), ['P1']);
assert.deepStrictEqual(calls, [['P1']]);
});
test('deleted ancestor: walk stops at the gap and still terminates', async () => {
// P1's parent GONE was deleted: its id is still returned (matches nothing
// when published — harmless) and the walk must not loop or throw.
const fetch = makeFetch({ P1: 'GONE' });
const ids = await collectAncestorIds(['P1'], fetch);
assert.deepStrictEqual(ids.sort(), ['GONE', 'P1']);
});
test('#3328 cyclic chain terminates and yields each ancestor exactly once', async () => {
// Corrupt/legacy data: P1 -> P2 -> P1. Without the seen-set this loops
// forever inside the publication.
const fetch = makeFetch({ P1: 'P2', P2: 'P1' });
const ids = await collectAncestorIds(['P1'], fetch);
assert.deepStrictEqual(ids.sort(), ['P1', 'P2']);
});
test('#3626: a card with several parents contributes every chain', async () => {
const docs = { A: { _id: 'A', parentId: 'B', parentIds: ['B', 'C'] }, B: { _id: 'B' }, C: { _id: 'C', parentId: 'D' }, D: { _id: 'D' } };
const ids = await collectAncestorIds(['A'], batch => batch.map(id => docs[id]).filter(Boolean));
assert.deepStrictEqual(ids.sort(), ['A', 'B', 'C', 'D'], 'D is reached only through the second parent');
});
test('self-parent card terminates', async () => {
const calls = [];
const fetch = makeFetch({ P1: 'P1' }, calls);
const ids = await collectAncestorIds(['P1'], fetch);
assert.deepStrictEqual(ids, ['P1']);
assert.deepStrictEqual(calls, [['P1']]);
});
test('fetch returning null/undefined is treated as "no parents found"', async () => {
const ids = await collectAncestorIds(['P1'], async () => null);
assert.deepStrictEqual(ids, ['P1']);
const ids2 = await collectAncestorIds(['P1'], async () => undefined);
assert.deepStrictEqual(ids2, ['P1']);
});
// --- wiring: the `board` publication actually uses the full-chain walk --------
test('board publication publishes the FULL ancestor chain (uses collectAncestorIds)', () => {
const src = fs.readFileSync(
path.join(__dirname, '..', 'server', 'publications', 'boards.js'),
'utf8',
);
assert.ok(
/const \{ cardParentIds, collectAllAncestorIds \} = require\('\/models\/lib\/cardParents'\);/.test(src),
'boards.js must use the all-parents walk from /models/lib/cardParents',
);
// The "Parent cards (for subtasks)" child must publish the walked ancestor
// set, not the one-level parentIds snapshot (the #3453 truncation).
const parentChild = src.slice(src.indexOf('// Parent cards (for subtasks)'));
assert.ok(
parentChild.includes('collectAllAncestorIds('),
'the parent-cards child must walk the full ancestor chain',
);
assert.ok(
/getCards\(\s*\{\s*_id:\s*\{\s*\$in:\s*ancestorIds\s*\}\s*\}/.test(parentChild),
'the parent-cards child must publish the collected ancestor ids',
);
});
// --- runner --------------------------------------------------------------------
(async () => {
for (const [name, fn] of tests) {
await fn();
passed += 1;
console.log(' ok -', name);
}
console.log(`\nsubtaskAncestors: ${passed} tests passed`);
})().catch(err => {
console.error(err);
process.exit(1);
});