import test from "node:test"; import assert from "node:assert/strict"; import { CHANNEL_FIND_MIN_CHANNELS, findChannels, foldForFind, } from "./channelFind"; type G = { id: string; name: string }; const GROUPINGS: { group: G; channels: string[] }[] = [ { group: { id: "g1", name: "Group One" }, channels: ["Alpha", "Beta"] }, { group: { id: "g2", name: "Reviews" }, channels: ["Gamma", "Delta"] }, { group: { id: "g3", name: "" }, channels: ["Café Corner", "Epsilon"] }, ]; const SLUGS: Record = { Alpha: "alpha-channel", Beta: "beta", Gamma: "gamma", Delta: "delta_desk", "Café Corner": "cafe-corner", Epsilon: "epsilon", }; const textsOf = (key: string) => [key, SLUGS[key]]; test("the finder is offered only past a constant number of channels", () => { assert.equal(CHANNEL_FIND_MIN_CHANNELS, 12); }); test("foldForFind ignores case, accents and punctuation", () => { assert.equal(foldForFind(" Café-Corner! "), "cafe corner"); assert.equal(foldForFind("@some_channel"), "some channel"); }); test("an empty or punctuation-only query filters nothing", () => { assert.equal(findChannels(GROUPINGS, "", textsOf), null); assert.equal(findChannels(GROUPINGS, " - ", textsOf), null); }); test("a channel name narrows its group to the matching channels", () => { assert.deepEqual(findChannels(GROUPINGS, "alp", textsOf), [ { group: GROUPINGS[0].group, channels: ["Alpha"] }, ]); }); test("a slug matches, separators read as spaces", () => { const found = findChannels(GROUPINGS, "delta desk", textsOf); assert.deepEqual(found?.map((g) => g.channels), [["Delta"]]); const bySlug = findChannels(GROUPINGS, "alpha-chan", textsOf); assert.deepEqual(bySlug?.map((g) => g.channels), [["Alpha"]]); }); test("a group name lists the whole group", () => { const found = findChannels(GROUPINGS, "review", textsOf); assert.deepEqual(found, [ { group: GROUPINGS[1].group, channels: ["Gamma", "Delta"] }, ]); }); test("accents fold on both sides, groups keep their order, no match is []", () => { assert.deepEqual( findChannels(GROUPINGS, "CAFE", textsOf)?.map((g) => g.group.id), ["g3"], ); assert.deepEqual( findChannels(GROUPINGS, "a", textsOf)?.map((g) => g.group.id), ["g1", "g2", "g3"], ); assert.deepEqual(findChannels(GROUPINGS, "zzz", textsOf), []); });