|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { scoreChannelMatch, scoreChannelName } from "./channelSearchScore.ts"; |
| 5 | + |
| 6 | +test("scoreChannelName: empty query matches everything at top score", () => { |
| 7 | + assert.equal(scoreChannelName("release-notes", ""), 0); |
| 8 | +}); |
| 9 | + |
| 10 | +test("scoreChannelName: exact name beats prefix beats word matches", () => { |
| 11 | + const exact = scoreChannelName("general", "general"); |
| 12 | + const prefix = scoreChannelName("general-chat", "general"); |
| 13 | + const wordExact = scoreChannelName("team-general", "general"); |
| 14 | + assert.ok(exact < prefix, "exact should rank above prefix"); |
| 15 | + assert.ok(prefix < wordExact, "prefix should rank above later-word match"); |
| 16 | +}); |
| 17 | + |
| 18 | +test("scoreChannelName: matches a later whole word", () => { |
| 19 | + // "notes" is the second word of "release-notes" |
| 20 | + assert.notEqual(scoreChannelName("release-notes", "notes"), null); |
| 21 | +}); |
| 22 | + |
| 23 | +test("scoreChannelName: matches a later word prefix", () => { |
| 24 | + assert.notEqual(scoreChannelName("release-notes", "not"), null); |
| 25 | +}); |
| 26 | + |
| 27 | +test("scoreChannelName: plain substring still matches", () => { |
| 28 | + assert.notEqual(scoreChannelName("release-notes", "ease"), null); |
| 29 | +}); |
| 30 | + |
| 31 | +test("scoreChannelName: collapsing separators matches 'releasenotes'", () => { |
| 32 | + // The core pain point: dropping the dash should still find the channel. |
| 33 | + const score = scoreChannelName("release-notes", "releasenotes"); |
| 34 | + assert.notEqual(score, null); |
| 35 | +}); |
| 36 | + |
| 37 | +test("scoreChannelName: partial across the separator ('releasenot')", () => { |
| 38 | + assert.notEqual(scoreChannelName("release-notes", "releasenot"), null); |
| 39 | +}); |
| 40 | + |
| 41 | +test("scoreChannelName: subsequence matches 'reln'", () => { |
| 42 | + // r-e-l...n appears in order inside "release-notes" |
| 43 | + assert.notEqual(scoreChannelName("release-notes", "reln"), null); |
| 44 | +}); |
| 45 | + |
| 46 | +test("scoreChannelName: subsequence works across underscores and dots", () => { |
| 47 | + assert.notEqual(scoreChannelName("build_and_deploy", "bd"), null); |
| 48 | + assert.notEqual(scoreChannelName("v1.2.release", "vrel"), null); |
| 49 | +}); |
| 50 | + |
| 51 | +test("scoreChannelName: single-char subsequence noise is rejected", () => { |
| 52 | + // A lone char that isn't a prefix/substring should NOT fuzzy-match, or every |
| 53 | + // channel containing that letter would show up. |
| 54 | + assert.equal(scoreChannelName("release-notes", "z"), null); |
| 55 | + // "x" doesn't appear at all |
| 56 | + assert.equal(scoreChannelName("release-notes", "x"), null); |
| 57 | +}); |
| 58 | + |
| 59 | +test("scoreChannelName: unrelated query returns null", () => { |
| 60 | + assert.equal(scoreChannelName("release-notes", "budget"), null); |
| 61 | +}); |
| 62 | + |
| 63 | +test("scoreChannelName: subsequence requires correct order", () => { |
| 64 | + // "sn" — 's' then 'n' — is NOT in order in "release-notes" (n comes... yes it |
| 65 | + // is: relea-s-e-n-otes). Use a genuinely out-of-order example instead. |
| 66 | + assert.equal(scoreChannelName("abc", "ca"), null); |
| 67 | +}); |
| 68 | + |
| 69 | +test("scoreChannelName: better matches score lower than fuzzier ones", () => { |
| 70 | + const prefix = scoreChannelName("release-notes", "release"); |
| 71 | + const collapsed = scoreChannelName("release-notes", "releasenotes"); |
| 72 | + const subsequence = scoreChannelName("release-notes", "reln"); |
| 73 | + assert.ok(prefix < collapsed, "prefix beats collapsed-separator match"); |
| 74 | + assert.ok(collapsed < subsequence, "collapsed beats subsequence match"); |
| 75 | +}); |
| 76 | + |
| 77 | +test("scoreChannelMatch: name match outranks description match", () => { |
| 78 | + const nameHit = scoreChannelMatch( |
| 79 | + { name: "release-notes", description: "" }, |
| 80 | + "release", |
| 81 | + ); |
| 82 | + const descHit = scoreChannelMatch( |
| 83 | + { name: "random", description: "release coordination" }, |
| 84 | + "release", |
| 85 | + ); |
| 86 | + assert.ok( |
| 87 | + nameHit !== null && descHit !== null && nameHit < descHit, |
| 88 | + "a name match should rank above a description-only match", |
| 89 | + ); |
| 90 | +}); |
| 91 | + |
| 92 | +test("scoreChannelMatch: description only does plain substring, not fuzzy", () => { |
| 93 | + // "reln" should not fuzzy-match the description. |
| 94 | + assert.equal( |
| 95 | + scoreChannelMatch({ name: "random", description: "release notes" }, "reln"), |
| 96 | + null, |
| 97 | + ); |
| 98 | + // But a real substring in the description matches. |
| 99 | + assert.notEqual( |
| 100 | + scoreChannelMatch( |
| 101 | + { name: "random", description: "release notes" }, |
| 102 | + "notes", |
| 103 | + ), |
| 104 | + null, |
| 105 | + ); |
| 106 | +}); |
| 107 | + |
| 108 | +test("scoreChannelMatch: no match anywhere returns null", () => { |
| 109 | + assert.equal( |
| 110 | + scoreChannelMatch({ name: "general", description: "chat" }, "budget"), |
| 111 | + null, |
| 112 | + ); |
| 113 | +}); |
0 commit comments