Files
wekan/tests/removeAllLabelsRuleAction.test.cjs
T
Lauri Ojansivu a81b4d5d97 Bring stale source guards up to the behaviour their code deliberately changed.
Eighteen suites had been failing because the code they read had changed on
purpose: notification delivery moved to stored plans, exports gained the
admin-only field guard and Scrum transfer, board copy became selectable,
card copy re-keys filtered custom fields, the Scrum settings menu entry was
added, new operator-only scripts, directory-login canaries, a rate-limit rule
that made the first mention of undoLast no longer the method, rule parts and
Sync-scoped recording in hooks evaluated by test harnesses, per-board card
dragging, and a Dependabot file-type bump. Each guard now pins the new
behaviour with a comment naming the change, keeps its original assertions,
and several gained positive and negative cases (export field guard, copy
defaults, Sync-scoped recording, card dragging, method lookup).

Two list-sync browser specs logged a second context in with the same
session token; they now get their own tokens, and the token guard counts
distinct pages instead of logins.

Thanks to xet7 !
2026-09-29 14:49:07 +03:00

109 lines
4.1 KiB
JavaScript

'use strict';
// Regression coverage for #3432: a new "Remove all labels" rule action that
// clears every label from a card in one step, instead of requiring one
// "remove label" action per label (the existing addLabel/removeLabel actions
// this mirrors).
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const cardsSource = fs.readFileSync(
path.join(__dirname, '..', 'models', 'cards.js'),
'utf8',
);
const rulesHelperSource = fs.readFileSync(
path.join(__dirname, '..', 'server', 'rulesHelper.js'),
'utf8',
);
const cardActionsJsSource = fs.readFileSync(
path.join(__dirname, '..', 'client', 'components', 'rules', 'actions', 'cardActions.js'),
'utf8',
);
const cardActionsJadeSource = fs.readFileSync(
path.join(__dirname, '..', 'client', 'components', 'rules', 'actions', 'cardActions.jade'),
'utf8',
);
// 1) models/cards.js defines Cards.helpers().removeAllLabels() and it sets
// labelIds to an empty array via $set (the same shape addLabel/removeLabel
// use $addToSet/$pull for a single label).
{
const match = cardsSource.match(
/removeAllLabels\(\)\s*\{([\s\S]*?)\n\s*\},/,
);
assert.ok(match, 'Cards.helpers defines removeAllLabels()');
const body = match[1];
assert.match(body, /labelIds\s*=\s*\[\]/, 'removeAllLabels empties labelIds on the in-memory card');
assert.match(
body,
/\$set:\s*\{\s*labelIds:\s*\[\]\s*\}/,
'removeAllLabels persists an empty labelIds array with $set',
);
}
// 2) server/rulesHelper.js wires the 'removeAllLabels' actionType to
// card.removeAllLabels(), the same way 'removeLabel' wires to
// card.removeLabel(action.labelId).
// The call is AWAITED: card.removeAllLabels() returns the promise of its
// updateAsync, and the rule runner must not move on to the next action (or
// report success) before the write lands. The guard used to require the bare
// call; it now accepts an optional `await` but still requires the call to be
// the first statement of that branch.
{
assert.match(
rulesHelperSource,
/actionType === 'removeAllLabels'\)\s*\{\s*\n\s*(?:await\s+)?card\.removeAllLabels\(\);/,
'rulesHelper calls card.removeAllLabels() for the removeAllLabels action',
);
}
// 3) The rule-builder UI has a "Remove all labels" button that inserts an
// Action with actionType 'removeAllLabels' and no label sub-field
// (unlike the existing add/remove-one-label action).
{
const handlerMatch = cardActionsJsSource.match(
/'click \.js-add-removealllabels-action'\(event, tpl\) \{([\s\S]*?)\n {2}\},/,
);
assert.ok(handlerMatch, 'cardActions.js has the js-add-removealllabels-action click handler');
const handlerBody = handlerMatch[1];
assert.match(
handlerBody,
/actionType:\s*'removeAllLabels'/,
'the handler inserts an Action with actionType removeAllLabels',
);
assert.doesNotMatch(
handlerBody,
/labelId/,
'unlike add/remove-one-label, the remove-all action reads no label-id field',
);
assert.match(
cardActionsJadeSource,
/js-add-removealllabels-action/,
'the Rules UI has a trigger button wired to the remove-all-labels handler',
);
assert.match(
cardActionsJadeSource,
/r-remove-all-labels/,
'the Rules UI labels the button with the r-remove-all-labels i18n key',
);
}
// 4) Behavioural replica of removeAllLabels' semantics (the same shape
// addLabel/removeLabel/toggleLabel are - a small pure state transition,
// testable without a running Meteor/Mongo stack): removing all labels
// always yields an empty array, regardless of how many labels were
// present, and a card with no labels is a no-op.
function removeAllLabels(labelIds) {
return [];
}
assert.deepEqual(removeAllLabels(['red', 'green', 'blue']), [], 'clears a card with several labels');
assert.deepEqual(removeAllLabels(['only-one']), [], 'clears a card with exactly one label');
assert.deepEqual(removeAllLabels([]), [], 'a card with no labels stays empty (no-op)');
assert.deepEqual(removeAllLabels(undefined), [], 'a card whose labelIds was never set becomes an empty array');
console.log('\nremoveAllLabelsRuleAction: 11 checks passed');