// What a local sweep reads (demo epic D2, item 1), on the D0 baseline's own // cases: the Acolyte question's terms led with "jeremy" and "show"/"say", and // its 40 read held nothing from 2024. import { test } from "node:test"; import assert from "node:assert/strict"; import { cleanTerms, normalizeTerm, quarterOf, rareTermsOf, readingSet, readingSize, scoreOf, subjectNames, termStats, termsNear, usableTerms, } from "./selection"; const QUARTERING = [{ slug: "the-quartering", name: "TheQuartering" }]; test("subjectNames: the person the question asks about and the channel's own words", () => { const n = subjectNames({ question: "What has Jeremy said about the Star Wars show The Acolyte?", channels: QUARTERING }); for (const w of ["jeremy", "quartering", "thequartering"]) assert.ok(n.has(w), w); for (const w of ["acolyte", "star", "wars", "the", "show"]) assert.ok(!n.has(w), w); assert.ok(subjectNames({ question: "What does Jeremy Hambly think of Disney?", channels: [] }).has("hambly")); assert.ok(subjectNames({ question: "Jeremy's views on Disney", channels: [] }).has("jeremy")); assert.ok(subjectNames({ question: "anything", channels: [], hosts: ["Chrissie Mayr"] }).has("mayr")); }); test("cleanTerms: D0's Acolyte terms lose the subject's name and the generic words; phrases stay phrases", () => { const names = subjectNames({ question: "What has Jeremy said about the Star Wars show The Acolyte?", channels: QUARTERING }); const c = cleanTerms(["jeremy", "star wars", "acolyte", "show", "comment", "say"], { names }); assert.deepEqual(c.kept, ["star wars", "acolyte"]); assert.deepEqual( c.dropped.map((d) => [d.term, d.why]), [ ["jeremy", "the subject's own name"], ["show", "a generic word"], ["comment", "a generic word"], ["say", "a generic word"], ], ); // The coffee question's phrases: the name and the possessive come off. const a = cleanTerms(["jeremy's coffee company", "company's performance", "the quartering"], { names }); assert.deepEqual(a.kept, ["coffee company", "company performance"]); assert.equal(a.dropped[0]!.why, "the subject's own name"); assert.equal(normalizeTerm('"Wizards of the Coast"'), "wizards of the coast"); assert.equal(normalizeTerm("the acolyte"), "acolyte"); assert.deepEqual(cleanTerms(["contest", "contests"], { names }).kept, ["contest"]); // qwen3:8b's Acolyte terms: the phrase that holds another term gives its remainder. assert.deepEqual(cleanTerms(["star wars the acolyte", "star wars", "comments"], { names }).kept, ["star wars the acolyte", "star wars", "acolyte"]); }); test("termStats: rarity over the scope; a term in > 25% is common; a capped list scores but never nominates", () => { const s = termStats( [ { term: "acolyte", videos: 48, partial: false }, { term: "star wars", videos: 755, partial: false }, { term: "twitter", videos: 2000, partial: true }, { term: "people", videos: 4000, partial: false }, { term: "nothing", videos: 0, partial: false }, ], 11712, ); const by = Object.fromEntries(s.map((x) => [x.term, x])); assert.ok(by.acolyte!.idf > by["star wars"]!.idf); assert.equal(by.people!.common, true); assert.deepEqual([by.twitter!.common, by.twitter!.nominates], [false, false]); assert.deepEqual(usableTerms(s).map((x) => x.term), ["acolyte", "star wars", "twitter"]); assert.deepEqual(rareTermsOf(s), ["acolyte"]); // A scope too small to say anything: nothing is common. assert.equal(termStats([{ term: "contest", videos: 1, partial: false }], 3)[0]!.common, false); // No term under 5%: the rarest nominating one is still the gate's. assert.deepEqual(rareTermsOf(termStats([{ term: "elon musk", videos: 966, partial: false }, { term: "buy", videos: 1500, partial: false }], 11712)), ["elon musk"]); }); test("scoreOf: rarity × saturated hits — 30 'star wars' no longer outrank 5 'acolyte'; two terms near = a bonus", () => { const stats = new Map(termStats([{ term: "acolyte", videos: 48, partial: false }, { term: "star wars", videos: 755, partial: false }], 11712).map((x) => [x.term, x])); assert.ok(scoreOf({ acolyte: 5, "star wars": 1 }, stats) > scoreOf({ "star wars": 30 }, stats)); assert.ok(scoreOf({ acolyte: 2 }, stats, true) > scoreOf({ acolyte: 2 }, stats)); assert.equal(scoreOf({ unknown: 9 }, stats), 0); }); test("termsNear: two different terms within 90 s", () => { const lines: ([number, string] | null)[] = [ [10, "the new star wars show"], [70, "the acolyte is out"], [400, "acolyte again"], ]; assert.equal(termsNear(lines, ["acolyte", "star wars"]), true); assert.equal(termsNear(lines.slice(1), ["acolyte", "star wars"]), false); assert.equal(termsNear([[10, "star wars"], [200, "acolyte"]], ["acolyte", "star wars"]), false); }); test("readingSet: the top n by score, plus the best of each quarter a rare term hits", () => { const c = (id: string, date: string, score: number, rareHit = true) => ({ id, date, score, rareHit }); const ranked = [ c("sw2019a", "2019-12-01", 30, false), c("sw2019b", "2019-12-02", 29, false), c("aco2024a", "2024-06-05", 20), c("aco2024b", "2024-06-06", 19), c("aco2020", "2020-12-11", 3), c("sw2018", "2018-01-01", 2, false), ]; const s = readingSet(ranked, { n: 3, spreadable: (x) => x.rareHit }); assert.deepEqual( s.read.map((x) => x.id), ["sw2019a", "sw2019b", "aco2024a", "aco2020"], ); assert.deepEqual(s.spread, ["aco2020"]); assert.equal(quarterOf("2024-06-11"), "2024Q2"); assert.equal(quarterOf("20240611"), "2024Q2"); assert.equal(quarterOf(""), null); }); test("readingSize: a time budget over the measured seconds a call; 40 by default", () => { assert.equal(readingSize(), 40); assert.equal(readingSize({ budgetMinutes: 20, secondsPerCall: 4.3 }), 121); assert.equal(readingSize({ budgetMinutes: 20, secondsPerCall: 10.4 }), 50); assert.equal(readingSize({ budgetMinutes: 1, secondsPerCall: 60 }), 5); });