Files
booksorter/src/web/library.test.ts
T
swaitsandClaude Fable 5.2 a08fbff685 Find a number for books the catalogues have no LC number for
Google Books joins Open Library and the Library of Congress as a source
of title, authors, publisher and categories when no number is in hand.
The similar-books tier widens its search step by step (other word forms,
place and topic words from the subtitle and publisher, fewer words) when
the first search finds no class, and needs at least two agreeing books
before it accepts one. The result is marked approximate.

Books with no number or an approximate one are looked up again at startup
once the last lookup is a week old. An ISBN no catalogue knows that is
within three digits of a book already here says it may be a misread of
that book.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-22 12:03:43 -07:00

50 lines
2.6 KiB
TypeScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import type { Book } from '../shared/types.ts';
import { groupLibrary, mainClass, matchesQuery } from './library.ts';
function book(id: number, over: Partial<Book>): Book {
return {
id, isbn13: null, lccn: null, title: 'T' + id, subtitle: null, authors: [], publisher: null, year: null,
subjects: [], callNumber: null, sortKey: null, confidence: null, source: null, dewey: null,
status: 'resolved', attentionReason: null, manualOverride: false, inQueue: false,
printedAt: null, hierarchyTail: null, edition: null, lookedUpAt: null, createdAt: '', updatedAt: '', ...over,
};
}
const qa = (id: number, key: string) => book(id, { callNumber: 'QA76.73 .R87', sortKey: key });
const ps = (id: number, key: string) => book(id, { callNumber: 'PS3558.E63 Z46', sortKey: key });
test('main class comes from the call number letters', () => {
assert.equal(mainClass(qa(1, 'a')), 'Q');
assert.equal(mainClass(ps(1, 'a')), 'P');
assert.equal(mainClass(book(1, {})), null);
assert.equal(mainClass(book(1, { callNumber: 'not a number' })), null);
});
test('books are grouped by main class in shelf order, with names and an Unsorted group last', () => {
const groups = groupLibrary([
book(9, { title: 'Unknown', status: 'pending' }), qa(2, 'QA 0076'), ps(3, 'PS 3558'), qa(1, 'QA 0001'),
book(8, { callNumber: 'garbage', status: 'needs_attention', sortKey: '~GARBAGE' }),
]);
assert.deepEqual(groups.map(g => g.key), ['P', 'Q', 'Unsorted']);
assert.equal(groups[0]!.heading, 'P · Language and literature');
assert.equal(groups[1]!.heading, 'Q · Science');
assert.deepEqual(groups[1]!.books.map(b => b.id), [1, 2]);
assert.deepEqual(groups[2]!.books.map(b => b.id).sort(), [8, 9]);
assert.equal(groups[2]!.heading, 'Unsorted');
});
test('search matches title, author, ISBN and call number', () => {
const b = book(1, { title: 'Dune', authors: ['Frank Herbert'], isbn13: '9780441172719', callNumber: 'PS3558.E63' });
for (const q of ['dune', 'herbert', '97804411', 'ps3558', ' ']) assert.ok(matchesQuery(b, q), q);
assert.ok(!matchesQuery(b, 'emma'));
});
test('filters apply before grouping, and an empty result has no groups', () => {
const books = [qa(1, 'QA 1'), book(2, { status: 'needs_attention', callNumber: null }), ps(3, 'PS 1')];
assert.deepEqual(groupLibrary(books, { attentionOnly: true }).map(g => g.key), ['Unsorted']);
assert.deepEqual(groupLibrary(books, { query: 'T3' }).map(g => g.key), ['P']);
assert.deepEqual(groupLibrary(books, { query: 'zzz' }), []);
assert.deepEqual(groupLibrary([]), []);
});