2026-10-07 14:00:37 -04:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-11 10:25:02 -04:00
|
|
|
|
* Self-test for keywords.js — the matching rules, pinned.
|
2026-10-07 14:00:37 -04:00
|
|
|
|
*
|
|
|
|
|
|
* node modules/overflow-tap/keywords.selftest.js
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
const assert = require('node:assert/strict');
|
2026-10-11 10:25:02 -04:00
|
|
|
|
const { keywordMatches, matchKeywords, sameSet, cleanNotes } = require('./keywords');
|
2026-10-07 14:00:37 -04:00
|
|
|
|
|
|
|
|
|
|
let n = 0;
|
|
|
|
|
|
function test(name, fn) {
|
|
|
|
|
|
n += 1;
|
|
|
|
|
|
try {
|
|
|
|
|
|
fn();
|
|
|
|
|
|
console.log(`ok ${name}`);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.log(`FAIL ${name}\n ${err.message}`);
|
|
|
|
|
|
process.exitCode = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-11 10:25:02 -04:00
|
|
|
|
test('a keyword fires as a word of the notes, whatever the case', () => {
|
|
|
|
|
|
assert.equal(keywordMatches('give', 'give'), true);
|
|
|
|
|
|
assert.equal(keywordMatches('Give', 'give'), true);
|
|
|
|
|
|
assert.equal(keywordMatches('please GIVE today', 'give'), true);
|
2026-10-07 14:00:37 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-10-11 10:25:02 -04:00
|
|
|
|
test('surrounding punctuation is ignored, as the helper strips it', () => {
|
|
|
|
|
|
assert.equal(keywordMatches('give!', 'give'), true);
|
|
|
|
|
|
assert.equal(keywordMatches('#give now', 'give'), true);
|
|
|
|
|
|
assert.equal(keywordMatches('go to (give) now', 'give'), true);
|
|
|
|
|
|
assert.equal(keywordMatches('"give"', 'give'), true);
|
|
|
|
|
|
assert.equal(keywordMatches('give.', 'give'), true);
|
2026-10-07 14:00:37 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-10-11 10:25:02 -04:00
|
|
|
|
test('a keyword inside another word does not fire', () => {
|
|
|
|
|
|
assert.equal(keywordMatches('a sermon on forgiveness', 'give'), false);
|
|
|
|
|
|
assert.equal(keywordMatches('giveaway', 'give'), false);
|
|
|
|
|
|
assert.equal(keywordMatches('give_now', 'give'), false);
|
2026-10-07 14:00:37 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-10-11 10:25:02 -04:00
|
|
|
|
test('a phrase keyword is bounded the same way', () => {
|
|
|
|
|
|
assert.equal(keywordMatches('Please fill out your Next Steps card', 'next steps'), true);
|
|
|
|
|
|
assert.equal(keywordMatches('nextsteps', 'next steps'), false);
|
2026-10-07 14:00:37 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('regex characters in a keyword are literal', () => {
|
2026-10-11 10:25:02 -04:00
|
|
|
|
assert.equal(keywordMatches('give.now', 'give.now'), true);
|
|
|
|
|
|
assert.equal(keywordMatches('giveXnow', 'give.now'), false);
|
2026-10-07 14:00:37 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('empty notes never match; zero-width padding is ignored', () => {
|
|
|
|
|
|
assert.equal(keywordMatches('', 'give'), false);
|
2026-10-11 10:25:02 -04:00
|
|
|
|
assert.equal(keywordMatches('', 'give'), false);
|
2026-10-07 14:00:37 -04:00
|
|
|
|
assert.equal(cleanNotes('give'), 'give');
|
2026-10-11 10:25:02 -04:00
|
|
|
|
assert.equal(keywordMatches('give', 'give'), true);
|
2026-10-07 14:00:37 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-10-11 10:25:02 -04:00
|
|
|
|
test('matchKeywords returns every hit, in list order, once each', () => {
|
|
|
|
|
|
const list = ['connect', 'give', 'prayer', { keyword: 'Events' }];
|
|
|
|
|
|
assert.deepEqual(matchKeywords('give and connect, then events', list), ['connect', 'give', 'Events']);
|
|
|
|
|
|
assert.deepEqual(matchKeywords('just worship', list), []);
|
|
|
|
|
|
assert.deepEqual(matchKeywords('give', []), []);
|
|
|
|
|
|
assert.deepEqual(matchKeywords('give give', ['give', 'GIVE']), ['give']);
|
2026-10-07 14:00:37 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-10-11 10:25:02 -04:00
|
|
|
|
test('sameSet compares keyword lists regardless of order and case', () => {
|
|
|
|
|
|
assert.equal(sameSet(['give', 'connect'], ['Connect', 'give']), true);
|
|
|
|
|
|
assert.equal(sameSet(['give'], ['give', 'connect']), false);
|
|
|
|
|
|
assert.equal(sameSet([], []), true);
|
|
|
|
|
|
assert.equal(sameSet(['give'], []), false);
|
2026-10-07 14:00:37 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`${n} tests, exit ${process.exitCode || 0}`);
|