import { test } from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import { classified, finishSimilar, keywordsOf, mergeCandidates, queryPlan, searchWords, stem, voteSimilar } from './similar.ts'; import type { SimilarCandidate } from './similar.ts'; import { parseMarcXml } from '../sources/marc.ts'; import { parseSimilar } from '../sources/openlibrary.ts'; const FIX = join(import.meta.dirname, '..', 'sources', 'fixtures'); const c = (title: string, lcc: string[], subjects: string[] = []): SimilarCandidate => ({ title, subjects, lcc }); test('stem folds plurals and -ing', () => { assert.equal(stem('hikes'), stem('hiking')); assert.equal(stem('mountains'), stem('mountain')); assert.equal(stem('birds'), stem('bird')); }); test('keywords drop stop and generic words, numbers and short words', () => { const k = keywordsOf('Beyond Trails in the Wind River Mountains of Wyoming'); assert.deepEqual([...k.title].sort(), ['mountains', 'river', 'trails', 'wind', 'wyoming']); assert.equal(k.title[0], 'mountains'); // longest first assert.deepEqual(keywordsOf('100 Classic Hikes Utah').title.sort(), ['hikes', 'utah']); assert.deepEqual(keywordsOf('The Complete Guide to the New Edition').title, []); }); test('subjects add extra keywords, without noise or repeats of title words', () => { const k = keywordsOf('Guns of August', undefined, ['World War, 1914-1918', 'Accessible book', 'Campaigns', 'Guns']); assert.ok(k.extra.includes('campaigns')); assert.ok(!k.extra.includes('accessible')); assert.ok(!k.extra.includes('guns')); }); test('query plan: widest first, fewer keywords after, at most 3, never one word for a multi-word title', () => { const plan = queryPlan(['a1', 'b2', 'c3', 'd4', 'e5']); assert.deepEqual(plan.map((p) => p.length), [5, 4, 3]); assert.deepEqual(plan[2], ['a1', 'b2', 'c3']); assert.deepEqual(queryPlan(['aa', 'bb']), [['aa', 'bb']]); assert.deepEqual(queryPlan([]), []); }); test('the same book from two searches counts once', () => { const m = mergeCandidates([[c('Hiking Utah', ['GV199.42.U8 A1'])], [c('hiking utah', ['GV199.42.U8 B2'], ['Utah'])]]); assert.equal(m.length, 1); assert.equal(m[0]!.lcc.length, 2); assert.equal(classified(m), 1); }); test('votes by class key; two agreeing books win', () => { const kw = keywordsOf('Hiking Utah Canyons'); const v = voteSimilar(kw, [ c('Hiking Utah canyons', ['GV199.42.U8 B1 2001']), c('Utah canyons hiking guide', ['GV199.42.U8 C2 2005']), c('Utah hiking', ['GV199.42.U8 D3 2010']), c('Canyons of the Southwest', ['F788 .E5']), ]); assert.ok(v); assert.equal(v.key, 'GV199.42.U8'); assert.equal(v.support, 3); }); test('one unrelated popular book does not capture a generic title', () => { const kw = keywordsOf('Summer Guns'); const v = voteSimilar(kw, [ c('Guns of August', ['D530 .T8']), c('Gun control', ['HV7436 .G86']), c('Summer nights', ['PS3566 .A1']), c('Summer rain', ['QC925 .R3']), ]); assert.equal(v, null); }); test('a single candidate needs to match most of the keywords', () => { const kw = keywordsOf('Alpine Wildflowers Colorado'); assert.equal(voteSimilar(kw, [c('Alpine wildflowers of Colorado', ['QK144 .A4'])])?.key, 'QK144'); assert.equal(voteSimilar(kw, [c('Alpine skiing', ['GV854 .A4']), c('Colorado history', ['F776 .H5'])]), null); }); test('candidates matching too few keywords are ignored', () => { const kw = keywordsOf('Alpine Wildflowers Colorado'); assert.equal(voteSimilar(kw, [c('Alpine skiing', ['GV854 .A4']), c('Alpine lakes', ['GB1 .A1']), c('Alpine huts', ['GV854 .B1'])]), null); }); test('scattered fine keys fall back to the class, and scattered classes return nothing', () => { const kw = keywordsOf('Bread Baking Pizza'); const v = voteSimilar(kw, [ c('Bread baking', ['TX769 .A1 2000']), c('Pizza and bread', ['TX770.P58 B1 2001']), c('Baking bread, pizza', ['TX770.P58 C1 2002']), c('Baking pizza', ['TX770.P6 D1 2003']), ]); assert.ok(v); assert.match(v.key, /^TX7/); assert.equal(voteSimilar(kw, [c('Bread baking', ['TX769 .A1']), c('Pizza bread', ['QA76 .B1']), c('Baking pizza', ['HD9 .C1'])]), null); }); test('neighbouring classes do not cancel each other out', () => { const kw = keywordsOf('Western Birds Field'); const v = voteSimilar(kw, [ c('Western birds', ['QL683.W4 A1']), c('Field birds western', ['QL683.W4 B1']), c('Birds field', ['QL681 .C1']), c('Birds of the field', ['QL681 .D1']), ]); assert.ok(v); assert.match(v.key, /^QL68[13]/); }); test('author-numbered literature classes are never borrowed', () => { const kw = keywordsOf('Dark Summer Novel'); assert.equal(voteSimilar(kw, [c('Dark summer', ['PS3562 .A1']), c('Summer dark novel', ['PS3562 .A2'])]), null); }); test('candidates without a parseable call number do not vote', () => { const kw = keywordsOf('Hiking Utah'); assert.equal(voteSimilar(kw, [c('Hiking Utah', ['CPB Box no. 1']), c('Utah hiking', [])]), null); }); test('finish: own author cutter and year after the borrowed key', () => { assert.deepEqual(finishSimilar('GV199.42.W8', { authors: ['Nancy Pallister'], title: 'Beyond Trails', year: 2025 })!.callNumber, 'GV199.42.W8 P35 2025'); assert.equal(finishSimilar('TX769', { authors: ['Ken Forkish'], title: 'Flour', year: 2012 })!.callNumber, 'TX769.F67 2012'); assert.equal(finishSimilar('QL683', { authors: [], title: 'A Field Guide' })!.callNumber, 'QL683.F54'); assert.equal(finishSimilar('???', { authors: [], title: 'x' }), null); }); test('real responses: Wind River Mountains votes for hiking in Wyoming, nonsense for nothing', () => { const ol = parseSimilar(readFileSync(join(FIX, 'ol-similar-mountains-wyoming-trails.json'), 'utf8')); const loc = parseMarcXml(readFileSync(join(FIX, 'loc-similar-mountains-wyoming.xml'), 'utf8')); const cands = mergeCandidates([ ol.map((d) => ({ title: d.title, ...(d.subtitle ? { subtitle: d.subtitle } : {}), subjects: d.subjects, lcc: d.lcc })), loc.map((r) => ({ title: r.meta.title ?? '', subjects: [...(r.meta.subjects ?? []), ...r.topics], lcc: r.lcc.map((x) => x.raw) })), ]); const v = voteSimilar(keywordsOf('Beyond Trails in the Wind River Mountains of Wyoming'), cands); assert.ok(v); assert.match(v.key, /^GV199\.42/); assert.ok(v.support >= 2); assert.equal(voteSimilar(keywordsOf('Xyzzy Plugh Quux Handbook'), cands), null); }); test('real responses: bread books vote for TX769', () => { const loc = parseMarcXml(readFileSync(join(FIX, 'loc-similar-artisan-flour.xml'), 'utf8')); const cands = loc.map((r) => ({ title: r.meta.title ?? '', subjects: [...(r.meta.subjects ?? []), ...r.topics], lcc: r.lcc.map((x) => x.raw) })); const v = voteSimilar(keywordsOf('Artisan Flour Water Bread'), cands); assert.equal(v?.key, 'TX769'); }); test('hinted vote: only candidates in the hinted class count, and one good match is enough', () => { const kw = keywordsOf('Sierra Nevada Backpacking Guide'); const cands = [ c('Sierra Nevada backpacking', ['GV199.42.S55 S54 2020']), c('Sierra backpacking trips', ['GV199.62.C57 2011']), c('Sierra Nevada backpacking', ['GV199.62.C57 2012']), c('Backpacking the Sierra', ['GV199.62.C57 2013']), ]; // Unhinted, the three GV199.62 books win; hinted to GV199.42, only the one book in that class is heard. assert.equal(voteSimilar(kw, cands)?.key, 'GV199.62'); const v = voteSimilar(kw, cands, { hint: 'GV199.42' }); assert.equal(v?.key, 'GV199.42.S55'); assert.equal(v?.support, 1); }); test('hinted vote: a weak lone match, a bare-class candidate, or scattered places give nothing', () => { const kw = keywordsOf('Sierra Nevada Backpacking Guide'); assert.equal(voteSimilar(kw, [c('Sierra history', ['GV199.42.S55 A1'])], { hint: 'GV199.42' }), null); assert.equal(voteSimilar(kw, [c('Sierra Nevada backpacking', ['GV199.42'])], { hint: 'GV199.42' }), null); const scattered = [ c('Sierra Nevada backpacking', ['GV199.42.S55 A1']), c('Sierra Nevada backpacking', ['GV199.42.C2 B1']), c('Sierra Nevada backpacking', ['GV199.42.W3 C1']), c('Sierra Nevada backpacking', ['GV199.42.N4 D1']), ]; assert.equal(voteSimilar(kw, scattered, { hint: 'GV199.42' }), null); }); test('hinted vote: a winning key may equal the hint when real records have a single cutter', () => { const kw = keywordsOf('Trail Runners Handbook Colorado'); const v = voteSimilar(kw, [ c('Colorado trail runners', ['GV199.62.C57 2011']), c('Trail running Colorado', ['GV199.62.C57 2012']), ], { hint: 'GV199.62' }); assert.equal(v?.key, 'GV199.62'); }); test('queries come from the title: capitalised mid-title words first, then longer, and subtitle words stay out', () => { const k = keywordsOf('Day hikes & overnights on the Pacific Crest Trail', 'Southern California : from the Mexican Border to Los Angeles County', ['Guidebooks', 'Hiking']); assert.deepEqual(k.own, ['pacific', 'crest', 'trail', 'overnights', 'hikes']); assert.deepEqual(k.sub.slice(0, 3), ['california', 'mexican', 'angeles']); assert.deepEqual(k.title.slice(0, 5), k.own); // scoring still sees the subtitle after the title's words assert.deepEqual(searchWords(k), [ ['pacific', 'crest', 'trail', 'overnights', 'hikes'], ['pacific', 'crest', 'trail', 'overnights'], ['pacific', 'crest', 'trail'], ]); }); test('a Title Case title carries no signal from capitals: longest words first, as before', () => { const k = keywordsOf('Beyond Trails in the Wind River Mountains of Wyoming'); assert.deepEqual(k.own, ['mountains', 'wyoming', 'trails', 'river', 'wind']); assert.deepEqual(searchWords(k)[2], ['mountains', 'wyoming', 'trails']); }); test('a title with fewer than two usable words borrows the subtitle, then the subjects, in one query of three', () => { const k = keywordsOf('Dune', 'Messiah of the desert planet', ['Science fiction', 'Arrakis (Imaginary place)']); assert.deepEqual(k.own, ['dune']); assert.deepEqual(searchWords(k), [['dune', 'messiah', 'desert']]); const bare = keywordsOf('Dune', undefined, ['Science fiction', 'Arrakis (Imaginary place)']); assert.deepEqual(searchWords(bare), [['dune', 'imaginary', 'science']]); assert.deepEqual(searchWords(keywordsOf('The Complete Guide')), []); });