Files
wekan/server/lib/tests/subtaskCreation.tests.js
T
Lauri Ojansivu 23c81e0b6c Tests drive the code the app runs, not copies of it nothing loads.
Ten modules were reached only from tests. Some were copies of production
logic kept beside it, so a test passed whatever production did; the rest
were re-export files that only tests imported.

Copies removed, the test now reading what runs:
- models/lib/oauth2ClientSecretJwt.js: wekan-oidc ran its own vendored copy.
  The package file is now the only one and tests/oauth2ClientSecretJwt
  drives it; tests/packageAppImportBoundary fails if a second copy returns.
- models/lib/scrollbarCss.js (#5439): tests/scrollbarCss reads the rules in
  client/components/lists/list.css.
- models/lib/createListParams.js: tests/createListParams reads the check()
  pattern of createListAfter in server/models/lists.js and runs it by the
  rules of Meteor's check, so changing the server pattern fails the test.
- server/lib/cardMatch.js (#5910): the search is a Mongo selector in
  server/publications/cards.js, which tests/boardSearchComments now reads.
- server/lib/dueDateEdits.js (#6081): Cards.getDueDateChangeCount() counts
  the a-dueAt activities itself.

Helpers that production now calls:
- models/lib/checklistHide.js (was server/lib): the Checklists helpers
  hideCheckedState() and isItemHidden() call it, with the same strict
  "checked is true" rule as before.
- models/lib/subtaskSettings.js (was server/lib): the Board Settings
  subtask pickers mark the selected board and list with it.
  resolveSubtaskLanding(), which nothing called, is removed with its tests.

Re-export files removed, the tests importing the real module:
server/lib/listColors.js, cardScope.js, announcementVisibility.js,
customNumberFormat.js and subtaskHelpers.js.

docs/DeveloperDocs/Directory-Structure.md pointed at the migrations/
directory, which no longer exists; it now names server/migrations.

Thanks to xet7 !
2026-11-21 18:17:53 +02:00

117 lines
4.6 KiB
JavaScript

/* eslint-env mocha */
import { expect } from 'chai';
import {
wouldCreateCycle,
subtaskCustomFields,
} from '/imports/lib/subtaskHelpers';
/**
* Unit tests for the pure subtask-creation helpers (imports/lib/subtaskHelpers.js).
*
* These cover three distinct subtask CREATION bugs whose pure logic was
* extracted out of models/cards.js so it can be tested Meteor-free:
*
* - #3328 circular subtask guard: the old guard tested `crtParentId in array`,
* which compares ARRAY INDICES ("0","1",...) rather than the stored ids, so
* it never matched and a card could become its own ancestor (hanging the
* board). wouldCreateCycle compares by VALUE.
* - #4037 / #3562 custom fields: a new subtask card must receive the board's
* automaticallyOnCard / alwaysOnCard custom fields, exactly like a normal
* new card does. subtaskCustomFields builds that array.
*/
describe('subtask creation helpers', function() {
describe('wouldCreateCycle (#3328)', function() {
it('returns true when the new parent is the card itself', function() {
expect(wouldCreateCycle('cardA', 'cardA', [])).to.equal(true);
});
it('returns true when the card is among the new parent ancestors (by VALUE)', function() {
// cardA would become a subtask of cardC, but cardA is already an ancestor
// of cardC -> cycle. The buggy `in` check compared indices and missed this.
expect(wouldCreateCycle('cardA', 'cardC', ['cardC', 'cardB', 'cardA'])).to.equal(true);
});
it('returns false for a normal, acyclic assignment', function() {
expect(wouldCreateCycle('cardX', 'cardP', ['cardP', 'cardG'])).to.equal(false);
});
it('returns false when clearing the parent (top-level card)', function() {
expect(wouldCreateCycle('cardA', '', ['anything'])).to.equal(false);
expect(wouldCreateCycle('cardA', null, ['cardA'])).to.equal(false);
expect(wouldCreateCycle('cardA', undefined, ['cardA'])).to.equal(false);
});
it('is NOT fooled by an id that merely equals a numeric array index', function() {
// Regression for the exact #3328 bug: with the old `cardId in array`
// logic, an id of "1" would falsely match index 1. Here "1" is the card,
// it is NOT in the ancestor chain, so there must be no cycle.
expect(wouldCreateCycle('1', 'cardP', ['cardP', 'cardG'])).to.equal(false);
// And when "1" really IS an ancestor, it is detected by value.
expect(wouldCreateCycle('1', 'cardP', ['cardP', '1'])).to.equal(true);
});
it('tolerates a non-array ancestor argument', function() {
expect(wouldCreateCycle('cardA', 'cardP', undefined)).to.equal(false);
expect(wouldCreateCycle('cardA', 'cardP', null)).to.equal(false);
});
});
describe('subtaskCustomFields (#4037/#3562)', function() {
it('includes fields flagged automaticallyOnCard', function() {
const boardFields = [
{ _id: 'f1', automaticallyOnCard: true },
{ _id: 'f2', automaticallyOnCard: false },
];
expect(subtaskCustomFields(boardFields)).to.deep.equal([
{ _id: 'f1', value: null },
]);
});
it('includes fields flagged alwaysOnCard', function() {
const boardFields = [
{ _id: 'f1', alwaysOnCard: true },
{ _id: 'f2' },
];
expect(subtaskCustomFields(boardFields)).to.deep.equal([
{ _id: 'f1', value: null },
]);
});
it('includes a field flagged by either flag, only once', function() {
const boardFields = [
{ _id: 'f1', automaticallyOnCard: true, alwaysOnCard: true },
];
expect(subtaskCustomFields(boardFields)).to.deep.equal([
{ _id: 'f1', value: null },
]);
});
it('returns an empty array when no field is automatic', function() {
const boardFields = [
{ _id: 'f1', automaticallyOnCard: false, alwaysOnCard: false },
{ _id: 'f2' },
];
expect(subtaskCustomFields(boardFields)).to.deep.equal([]);
});
it('returns an empty array for missing / non-array input', function() {
expect(subtaskCustomFields(undefined)).to.deep.equal([]);
expect(subtaskCustomFields(null)).to.deep.equal([]);
expect(subtaskCustomFields({})).to.deep.equal([]);
});
it('every produced entry has value null (mirrors a normal new card)', function() {
const boardFields = [
{ _id: 'a', automaticallyOnCard: true },
{ _id: 'b', alwaysOnCard: true },
];
const result = subtaskCustomFields(boardFields);
expect(result).to.have.length(2);
result.forEach(entry => {
expect(entry).to.have.property('value', null);
expect(entry).to.have.property('_id');
});
});
});
});