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

+83 -1
View File
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
import { openDb } from './db.ts';
import { createJobs, DEFAULT_RETRY_DELAYS_MS } from './jobs.ts';
import type { ResolverLike } from './jobs.ts';
import type { NewBook } from './db.ts';
import type { Book, Resolution } from '../shared/types.ts';
class TemporaryError extends Error {}
@@ -17,7 +18,7 @@ function setup(resolve: ResolverLike['resolve'], delays = [0, 0, 0]) {
const db = openDb(':memory:');
const changes: Book[] = [];
const logs: string[] = [];
const resolver: ResolverLike = { resolve, search: async () => [] };
const resolver: ResolverLike = { resolve, tail: async () => [], search: async () => [] };
const jobs = createJobs({ db, resolver, onChange: (b) => changes.push(b), retryDelaysMs: delays, log: (l) => logs.push(l) });
return { db, jobs, changes, logs };
}
@@ -258,3 +259,84 @@ test('an LCCN placeholder title counts as empty', async () => {
await jobs.idle();
assert.equal(db.getBook(book.id)!.title, 'The Rust Book');
});
function tailBook(db: ReturnType<typeof openDb>, over: Partial<NewBook>) {
return db.insertPending({
status: 'resolved', callNumber: 'GV199.42.U82 U564 2024', sortKey: 'GV 0199.42 U82', confidence: 'exact',
source: 'loc-isbn', inQueue: false, title: 'T', ...over,
}).book;
}
test('lookup stores the tail; the tail stays unknown (NULL) when the catalogue could not be asked', async () => {
const a = setup(async () => ({ ...found, tail: ['Utah'] }));
const x = a.db.insertPending({ isbn13: '9781593278281', title: '9781593278281' }).book;
a.jobs.enqueue(x.id);
await a.jobs.idle();
assert.deepEqual(a.db.getBook(x.id)!.hierarchyTail, ['Utah']);
const b = setup(async () => ({ ...found, tail: null }));
const y = b.db.insertPending({ isbn13: '9781593278281', title: '9781593278281' }).book;
b.jobs.enqueue(y.id);
await b.jobs.idle();
assert.equal(b.db.getBook(y.id)!.status, 'resolved');
assert.equal(b.db.getBook(y.id)!.hierarchyTail, null);
});
test('tail backfill changes only the tail, emits the update, and never touches status or the number', async () => {
const calls: unknown[] = [];
const { db, changes } = setup(async () => found);
const resolver: ResolverLike = {
resolve: async () => { throw new Error('no lookup'); }, search: async () => [],
tail: async (i) => { calls.push(i); return ['Utah', 'Uinta Mountains']; },
};
const j = createJobs({ db, resolver, onChange: (b) => changes.push(b), retryDelaysMs: [0], log: () => {} });
const printed = '2026-02-03T00:00:00.000Z';
const b = tailBook(db, { isbn13: '9781593278281' });
db.updateBook(b.id, { printedAt: printed, inQueue: true, confidence: 'approximate' });
const before = db.getBook(b.id)!;
j.resumeTails();
await j.idle();
const after = db.getBook(b.id)!;
assert.deepEqual(after.hierarchyTail, ['Utah', 'Uinta Mountains']);
for (const k of ['callNumber', 'sortKey', 'confidence', 'source', 'status', 'attentionReason', 'inQueue', 'printedAt', 'manualOverride'] as const) {
assert.deepEqual(after[k], before[k], k);
}
assert.ok(changes.some((c) => c.id === b.id && c.hierarchyTail !== null), 'book-updated event');
assert.equal(calls.length, 1);
});
test('tail backfill: no identifiers, hand entries and similar numbers get [] without asking', async () => {
const { db } = setup(async () => found);
const asked: unknown[] = [];
const resolver: ResolverLike = { resolve: async () => found, search: async () => [], tail: async (i) => { asked.push(i); return ['X']; } };
const jobs = createJobs({ db, resolver, onChange: () => {}, retryDelaysMs: [0], log: () => {} });
const none = tailBook(db, { title: 'no ids' });
const manual = tailBook(db, { isbn13: '9780000000002', manualOverride: true, source: 'manual' });
const similar = tailBook(db, { isbn13: '9780000000019', source: 'similar' });
const byHand = tailBook(db, { lccn: '2012047313', source: 'manual' });
const pending = db.insertPending({ isbn13: '9780000000026', title: 'p' }).book; // status pending: the lookup's job
jobs.resumeTails();
await jobs.idle();
for (const b of [none, manual, similar, byHand]) assert.deepEqual(db.getBook(b.id)!.hierarchyTail, [], b.title);
assert.equal(db.getBook(pending.id)!.hierarchyTail, null);
assert.equal(asked.length, 0);
});
test('tail backfill: a temporary failure is retried, then left NULL for the next start; a missing record stores []', async () => {
const { db } = setup(async () => found);
let n = 0;
const logs: string[] = [];
const resolver: ResolverLike = {
resolve: async () => found, search: async () => [],
tail: async (i) => { n++; if (i.isbn13 === '9780000000002') throw new TemporaryError('loc.gov busy'); return []; },
};
const jobs = createJobs({ db, resolver, onChange: () => {}, retryDelaysMs: [0, 0], isTemporary: (e) => e instanceof TemporaryError, log: (l) => logs.push(l) });
const busy = tailBook(db, { isbn13: '9780000000002' });
const empty = tailBook(db, { isbn13: '9780000000019' });
jobs.resumeTails();
await jobs.idle();
assert.equal(db.getBook(busy.id)!.hierarchyTail, null);
assert.equal(db.getBook(busy.id)!.status, 'resolved');
assert.deepEqual(db.getBook(empty.id)!.hierarchyTail, []);
assert.equal(n, 4); // busy: first try and two retries; empty: once
assert.ok(logs.some((l) => l.includes('left for the next start')));
});