Print the subject hierarchy as a tree on cards, remove the QR code

Each card now shows, under the call number, the Library of Congress
outline levels for its class (from the published outline, down to the
narrowest range) followed by the place or topic from the catalogue
record's subject heading, one level per line. Existing books get the
subject part filled in by a background job at startup that changes
nothing else about them.

The QR code and everything that read it are removed; the subjects line
on the card is replaced by the tree.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-19 14:06:50 -07:00
1 parent c9b2c2e8c9
commit 7ae1bcef0a
46 files changed
+8591 -545

No files matched your search

+19 -39
View File
@@ -17,7 +17,7 @@ function setupRecording(fresh: (boolean | undefined)[]) {
function setup(resolve: (i?: any, o?: any) => Promise<Resolution> = async () => found, port?: number) {
const db = openDb(':memory:');
const events = createEventBus();
const resolver = { resolve, search: async () => [{ title: 'Hit', authors: [], lcc: [] }] };
const resolver = { resolve, tail: async () => [], search: async () => [{ title: 'Hit', authors: [], lcc: [] }] };
const jobs = createJobs({ db, resolver, onChange: (b) => events.emitBook(b), retryDelaysMs: [0], log: () => {} });
const pdfCalls: { cards: CardData[]; opts: Settings & { startPosition: number } }[] = [];
const app = createApp({
@@ -134,8 +134,7 @@ test('print passes cards in shelf order and honours start; confirm clears queue'
const call = pdfCalls[0]!;
assert.deepEqual(call.cards.map((c) => c.title), ['Approx', 'First', 'Second']);
assert.equal(call.opts.startPosition, 2);
assert.equal(call.cards[1]!.className, 'Class P');
assert.equal(call.cards[1]!.subclassName, 'Sub PS');
assert.deepEqual(call.cards[1]!.hierarchy, { outline: ['Class P', 'Sub PS'], tail: [] });
assert.equal(call.cards[0]!.approximate, true);
assert.equal(call.cards[1]!.approximate, false);
const ids = r.headers.get('x-book-ids')!.split(',').map(Number);
@@ -234,7 +233,7 @@ test('debug capture: saves a PNG, keeps the newest 30, refuses other bodies', as
const dir = mkdtempSync(join(tmpdir(), 'captures-'));
try {
const db = openDb(':memory:');
const resolver = { resolve: async () => found, search: async () => [] };
const resolver = { resolve: async () => found, tail: async () => [], search: async () => [] };
const jobs = createJobs({ db, resolver, onChange: () => {}, retryDelaysMs: [0], log: () => {} });
const app = createApp({
db, jobs, resolver, capturesDir: join(dir, 'captures'),
@@ -254,41 +253,6 @@ test('debug capture: saves a PNG, keeps the newest 30, refuses other bodies', as
} finally { rmSync(dir, { recursive: true, force: true }); }
});
test('from-card: parses, adds a resolved book that is not queued, then finds it again', async () => {
const { req, db } = setup();
assert.equal((await req('POST', '/api/books/from-card', { text: 'https://example.com' })).status, 400);
assert.equal((await req('POST', '/api/books/from-card', {})).status, 400);
const text = 'PQ2230.A1 H8 1987 ~\nLes Misérables\nVictor Hugo\nbooksorter 1';
const first = await (await req('POST', '/api/books/from-card', { text })).json() as any;
assert.equal(first.existed, false);
assert.equal(first.book.status, 'resolved');
assert.equal(first.book.source, 'card');
assert.equal(first.book.confidence, 'approximate');
assert.equal(first.book.inQueue, false);
assert.equal(first.book.callNumber, 'PQ2230.A1 H8 1987');
assert.deepEqual(first.book.authors, ['Victor Hugo']);
// same title (any case, padded) with the same call number is the same book; a different call number is not
const again = await (await req('POST', '/api/books/from-card', { text: text.replace('Les Misérables', 'LES MISÉRABLES ') })).json() as any;
assert.equal(again.existed, true);
assert.equal(again.book.id, first.book.id);
const other = await (await req('POST', '/api/books/from-card', { text: text.replace('H8', 'H9') })).json() as any;
assert.equal(other.existed, false);
assert.equal(db.listBooks().length, 2);
});
test('from-card: a card with an ISBN matches by ISBN; cross-origin is refused', async () => {
const { req, db } = setup(undefined, 4321);
const text = 'QA76.73.R87 K53 2018\nThe Rust Programming Language\nSteve Klabnik\nISBN 9781593278281\n2018\nbooksorter 1';
const made = await (await req('POST', '/api/books/from-card', { text }, { origin: 'http://localhost:4321' })).json() as any;
assert.equal(made.book.isbn13, '9781593278281');
assert.equal(made.book.year, 2018);
const found = await (await req('POST', '/api/books/from-card', { text: text.replace('The Rust Programming Language', 'Other') }, { origin: 'http://localhost:4321' })).json() as any;
assert.equal(found.existed, true);
assert.equal(found.book.id, made.book.id);
assert.equal((await req('POST', '/api/books/from-card', { text }, { origin: 'http://evil.example' })).status, 403);
assert.equal(db.listBooks().length, 1);
});
test('retry asks the lookup to ignore its caches', async () => {
const fresh: (boolean | undefined)[] = [];
const { req, jobs } = setupRecording(fresh);
@@ -316,3 +280,19 @@ test('a search result\'s LCCN (work-level) is not saved onto the book', async ()
const f = await (await req('POST', '/api/books/from-search', { result: { title: 'Hit', authors: ['X'], lccn: '96067014', lcc: [] } })).json() as any;
assert.equal(f.book.lccn, null);
});
test('GET /api/books/:id/hierarchy answers with the outline and the stored tail', async () => {
const { req, db } = setup();
const b = db.insertPending({ title: 'H', callNumber: 'PS3558.E63 D8 2005', status: 'resolved', hierarchyTail: ['Utah'] }).book;
const r = await req('GET', `/api/books/${b.id}/hierarchy`);
assert.equal(r.status, 200);
assert.deepEqual(await r.json(), { outline: ['Class P', 'Sub PS'], tail: [] }); // the default builds from class names only
assert.equal((await req('GET', '/api/books/999/hierarchy')).status, 404);
});
test('a cross-origin request that changes state is refused', async () => {
const { req, db } = setup(undefined, 4321);
assert.equal((await req('POST', '/api/books/manual', { title: 'X' }, { origin: 'http://evil.example' })).status, 403);
assert.equal((await req('POST', '/api/books/manual', { title: 'X' }, { origin: 'http://localhost:4321' })).status, 200);
assert.equal(db.listBooks().length, 1);
});