Files
booksorter/src/web/library.test.ts
T
swaitsandClaude Fable 5.2 bb46d343a8 Show the edition statement on cards and in the library
The edition comes only from a record for this exact edition: the Library
of Congress record's edition statement first, then Open Library's edition
name when it is a well-formed statement. Existing books are filled in at
startup without changing anything else about them.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 17:49:59 -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, 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([]), []);
});