mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 20:45:00 -05:00
Board.searchCards() (used by the sidebar search and the per-list quick
search) only matched a card's title, description and custom fields via
buildCardSearchOr(). Card comments live in a separate CardComments
collection, so they were never searched at board level, even though
global search (server/publications/cards.js) already matches comment
text.
Add matchingCommentCardIds() to models/lib/cardSearch.js: given the
board's comments (as plain { cardId, text } pairs) and the search term,
it returns the de-duplicated card ids whose comment text matches,
reusing the same case-insensitive RegExp matching rule as the rest of
the search. Board.searchCards() now pulls the board's comments via
ReactiveCache.getCardComments (works the same on client Minimongo and
server Mongo) and ORs the matching card ids into the existing query.
Thanks to javiloncho and xet7 !
Closes #3841,
179 lines
7.0 KiB
JavaScript
179 lines
7.0 KiB
JavaScript
'use strict';
|
|
(async () => {
|
|
|
|
// Plain-Node unit test (no Meteor) for the board card-search query builder.
|
|
// Run: node tests/cardSearch.test.cjs
|
|
//
|
|
// Regression guard for #5680 ("How can I search for numbers in user-defined
|
|
// fields?"): number and currency custom fields store their value as a JS Number,
|
|
// but the search only matched custom fields with a regex, and a Mongo/Minimongo
|
|
// regex never matches numeric values — so searching a custom-field number found
|
|
// nothing. buildCardSearchOr() must add an exact numeric-equality clause for
|
|
// plain-number terms while leaving the string regex clauses intact, and must NOT
|
|
// add one for non-numeric terms.
|
|
|
|
const assert = require('assert');
|
|
const {
|
|
parseNumericSearchTerm,
|
|
buildCardSearchOr,
|
|
matchingCommentCardIds,
|
|
} = await import('../models/lib/cardSearch.js');
|
|
|
|
let passed = 0;
|
|
function test(name, fn) {
|
|
fn();
|
|
passed += 1;
|
|
console.log(' ok -', name);
|
|
}
|
|
|
|
// A tiny Minimongo-like matcher so we can assert the built $or actually selects
|
|
// (or rejects) representative card documents — closer to real behavior than just
|
|
// eyeballing the query shape.
|
|
function clauseMatches(clause, card) {
|
|
if (clause.title) return clause.title.test(card.title || '');
|
|
if (clause.description) return clause.description.test(card.description || '');
|
|
if (clause.customFields) {
|
|
const cond = clause.customFields.$elemMatch.value;
|
|
const fields = card.customFields || [];
|
|
return fields.some(f => {
|
|
if (cond instanceof RegExp) {
|
|
return typeof f.value === 'string' && cond.test(f.value);
|
|
}
|
|
return f.value === cond; // exact (numeric) equality
|
|
});
|
|
}
|
|
return false;
|
|
}
|
|
const orMatches = (or, card) => or.some(c => clauseMatches(c, card));
|
|
|
|
// --- parseNumericSearchTerm --------------------------------------------------
|
|
test('parseNumericSearchTerm parses integers and decimals', () => {
|
|
assert.strictEqual(parseNumericSearchTerm('2025001'), 2025001);
|
|
assert.strictEqual(parseNumericSearchTerm('123'), 123);
|
|
assert.strictEqual(parseNumericSearchTerm('123.45'), 123.45);
|
|
});
|
|
|
|
test('parseNumericSearchTerm accepts a comma decimal separator', () => {
|
|
assert.strictEqual(parseNumericSearchTerm('123,00'), 123);
|
|
assert.strictEqual(parseNumericSearchTerm('123,45'), 123.45);
|
|
});
|
|
|
|
test('parseNumericSearchTerm rejects non-plain-numbers', () => {
|
|
// things Number() would otherwise coerce, plus mixed text
|
|
['', ' ', 'abc', '123abc', '1 2', '0x1f', '1e3', 'NaN', '12,00 €'].forEach(t => {
|
|
assert.strictEqual(parseNumericSearchTerm(t), null, `should reject ${JSON.stringify(t)}`);
|
|
});
|
|
});
|
|
|
|
test('parseNumericSearchTerm tolerates non-string input', () => {
|
|
assert.strictEqual(parseNumericSearchTerm(123), null);
|
|
assert.strictEqual(parseNumericSearchTerm(null), null);
|
|
assert.strictEqual(parseNumericSearchTerm(undefined), null);
|
|
});
|
|
|
|
// --- buildCardSearchOr: POSITIVE --------------------------------------------
|
|
test('#5680: a numeric custom field is matched by a number term', () => {
|
|
const or = buildCardSearchOr('2025001');
|
|
const card = { title: 'Invoice', customFields: [{ value: 2025001 }] };
|
|
assert.ok(orMatches(or, card), 'card with numeric custom field should match');
|
|
});
|
|
|
|
test('#5680: a currency custom field (123) matches search "123"', () => {
|
|
const or = buildCardSearchOr('123');
|
|
const card = { title: 'X', customFields: [{ value: 123 }] };
|
|
assert.ok(orMatches(or, card));
|
|
});
|
|
|
|
test('#5680: comma-typed term "123,00" matches numeric value 123', () => {
|
|
const or = buildCardSearchOr('123,00');
|
|
const card = { title: 'X', customFields: [{ value: 123 }] };
|
|
assert.ok(orMatches(or, card));
|
|
});
|
|
|
|
test('string custom fields still match via regex (unchanged behavior)', () => {
|
|
const or = buildCardSearchOr('123');
|
|
const card = { title: 'X', customFields: [{ value: '123,00 €' }] };
|
|
assert.ok(orMatches(or, card));
|
|
});
|
|
|
|
test('title and description still match via regex', () => {
|
|
const or = buildCardSearchOr('hello');
|
|
assert.ok(orMatches(or, { title: 'Say HELLO there' }));
|
|
assert.ok(orMatches(or, { description: 'a hello world card' }));
|
|
});
|
|
|
|
test('the numeric clause is only appended for numeric terms', () => {
|
|
assert.strictEqual(buildCardSearchOr('2025001').length, 4); // + numeric clause
|
|
assert.strictEqual(buildCardSearchOr('hello').length, 3); // no numeric clause
|
|
});
|
|
|
|
// --- buildCardSearchOr: NEGATIVE --------------------------------------------
|
|
test('a different number does NOT match (exact equality, not substring)', () => {
|
|
const or = buildCardSearchOr('202');
|
|
const card = { title: 'X', customFields: [{ value: 2025001 }] };
|
|
assert.ok(!orMatches(or, card), '202 must not match 2025001');
|
|
});
|
|
|
|
test('numeric term does not match a numeric field of a different value', () => {
|
|
const or = buildCardSearchOr('124');
|
|
const card = { title: 'X', customFields: [{ value: 123 }] };
|
|
assert.ok(!orMatches(or, card));
|
|
});
|
|
|
|
test('regex clause never matches a numeric field value', () => {
|
|
// Only the string-regex clauses (no numeric term) — a numeric field must not
|
|
// be matched by a regex, which is the exact #5680 bug.
|
|
const or = buildCardSearchOr('hello');
|
|
const card = { customFields: [{ value: 123 }] };
|
|
assert.ok(!orMatches(or, card));
|
|
});
|
|
|
|
test('non-numeric term against a numeric-only card finds nothing', () => {
|
|
const or = buildCardSearchOr('abc');
|
|
const card = { title: 'Numbers', customFields: [{ value: 2025001 }] };
|
|
assert.ok(!orMatches(or, card));
|
|
});
|
|
|
|
// --- matchingCommentCardIds (#3841: board search must match comment text) ---
|
|
test('#3841: a card with no title/description match but a matching comment is found', () => {
|
|
const comments = [
|
|
{ cardId: 'card1', text: 'nothing relevant here' },
|
|
{ cardId: 'card2', text: 'please check the payment gateway' },
|
|
];
|
|
assert.deepStrictEqual(matchingCommentCardIds(comments, 'payment gateway'), ['card2']);
|
|
});
|
|
|
|
test('#3841: a card with no matching comment (and no title/description match) is not found', () => {
|
|
const comments = [
|
|
{ cardId: 'card1', text: 'unrelated comment' },
|
|
{ cardId: 'card2', text: 'another one' },
|
|
];
|
|
assert.deepStrictEqual(matchingCommentCardIds(comments, 'payment'), []);
|
|
});
|
|
|
|
test('#3841: matching is case-insensitive', () => {
|
|
const comments = [{ cardId: 'card1', text: 'Mentions a Gateway' }];
|
|
assert.deepStrictEqual(matchingCommentCardIds(comments, 'GATEWAY'), ['card1']);
|
|
});
|
|
|
|
test('#3841: a card id is only returned once even with several matching comments', () => {
|
|
const comments = [
|
|
{ cardId: 'card1', text: 'first mentions widget' },
|
|
{ cardId: 'card1', text: 'second also mentions widget' },
|
|
{ cardId: 'card2', text: 'unrelated' },
|
|
];
|
|
assert.deepStrictEqual(matchingCommentCardIds(comments, 'widget'), ['card1']);
|
|
});
|
|
|
|
test('#3841: tolerates missing/empty/non-string text and empty input', () => {
|
|
assert.deepStrictEqual(matchingCommentCardIds([], 'x'), []);
|
|
assert.deepStrictEqual(matchingCommentCardIds(undefined, 'x'), []);
|
|
assert.deepStrictEqual(
|
|
matchingCommentCardIds([{ cardId: 'c1', text: null }, { cardId: 'c2' }], 'x'),
|
|
[],
|
|
);
|
|
});
|
|
|
|
console.log(`\n${passed} tests passed`);
|
|
|
|
})().catch(e => { console.error(e); process.exit(1); }); |