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>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-19 17:49:59 -07:00
1 parent e83774adbd
commit bb46d343a8
26 files changed
+455 -34

No files matched your search

+79 -5
View File
@@ -18,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, tail: async () => [], search: async () => [] };
const resolver: ResolverLike = { resolve, tail: async () => [], edition: 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 };
}
@@ -286,7 +286,7 @@ test('tail backfill changes only the tail, emits the update, and never touches s
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']; },
edition: 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';
@@ -307,7 +307,7 @@ test('tail backfill changes only the tail, emits the update, and never touches s
test('tail backfill: hand entries get [] without asking; every other book is asked, whatever its source', 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 resolver: ResolverLike = { resolve: async () => found, search: async () => [], edition: 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' });
@@ -327,7 +327,7 @@ test('tail rules version: books with [] are checked again once, hand entries and
const asked: string[] = [];
const resolver: ResolverLike = {
resolve: async () => found, search: async () => [],
tail: async (i) => { asked.push(i.isbn13 ?? ''); return ['Utah']; },
edition: async () => '', tail: async (i) => { asked.push(i.isbn13 ?? ''); return ['Utah']; },
};
const mk = () => createJobs({ db, resolver, onChange: () => {}, retryDelaysMs: [0], log: () => {} });
const empty = tailBook(db, { isbn13: '9780000000002', hierarchyTail: [] });
@@ -357,7 +357,7 @@ test('tail backfill: a temporary failure is retried, then left NULL for the next
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 []; },
edition: 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' });
@@ -370,3 +370,77 @@ test('tail backfill: a temporary failure is retried, then left NULL for the next
assert.equal(n, 4); // busy: first try and two retries; empty: once
assert.ok(logs.some((l) => l.includes('left for the next start')));
});
// The edition backfill.
function editionSetup(edition: ResolverLike['edition']) {
const { db } = setup(async () => found);
const changes: Book[] = [];
const logs: string[] = [];
const resolver: ResolverLike = { resolve: async () => { throw new Error('no lookup'); }, search: async () => [], tail: async () => [], edition };
const jobs = createJobs({ db, resolver, onChange: (b) => changes.push(b), retryDelaysMs: [0], log: (l) => logs.push(l), isTemporary: (e) => e instanceof TemporaryError });
return { db, jobs, changes, logs };
}
test('a lookup sets the edition of a book not looked up yet; "look up again" replaces it, but never with nothing', async () => {
const a = setup(async () => ({ ...found, edition: 'Third edition' }));
const x = a.db.insertPending({ isbn13: '9781593278281', title: '9781593278281' }).book;
a.jobs.enqueue(x.id);
await a.jobs.idle();
assert.equal(a.db.getBook(x.id)!.edition, 'Third edition');
// A normal lookup leaves a value (for example one typed by hand) alone.
a.db.updateBook(x.id, { edition: 'By hand', status: 'pending' });
a.jobs.enqueue(x.id);
await a.jobs.idle();
assert.equal(a.db.getBook(x.id)!.edition, 'By hand');
a.db.updateBook(x.id, { status: 'pending' });
a.jobs.enqueue(x.id, { fresh: true });
await a.jobs.idle();
assert.equal(a.db.getBook(x.id)!.edition, 'Third edition');
const b = setup(async () => ({ ...found, edition: 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)!.edition, null); // unknown: the backfill asks later
});
test('edition backfill fills only NULL rows and changes nothing else about the book', async () => {
const asked: unknown[] = [];
const { db, jobs, changes } = editionSetup(async (i) => { asked.push(i); return 'Third edition'; });
const printed = '2026-02-03T00:00:00.000Z';
const b = tailBook(db, { isbn13: '9781593278281', hierarchyTail: ['Utah'] });
db.updateBook(b.id, { printedAt: printed, inQueue: true, confidence: 'approximate' });
const typed = tailBook(db, { isbn13: '9780000000002', edition: 'By hand' });
const none = tailBook(db, { isbn13: '9780000000019', edition: '' });
const noIds = tailBook(db, { title: 'no ids' });
const pending = db.insertPending({ isbn13: '9780000000026', title: 'p' }).book;
const before = db.getBook(b.id)!;
jobs.resumeEditions();
await jobs.idle();
const after = db.getBook(b.id)!;
assert.equal(after.edition, 'Third edition');
for (const k of ['callNumber', 'sortKey', 'confidence', 'source', 'status', 'attentionReason', 'inQueue', 'printedAt', 'manualOverride', 'hierarchyTail', 'title', 'publisher', 'year'] as const) {
assert.deepEqual(after[k], before[k], k);
}
assert.equal(db.getBook(typed.id)!.edition, 'By hand');
assert.equal(db.getBook(none.id)!.edition, '');
assert.equal(db.getBook(noIds.id)!.edition, ''); // neither ISBN nor LCCN: nothing to ask
assert.equal(db.getBook(pending.id)!.edition, null); // the lookup will set it
assert.deepEqual(asked, [{ isbn13: '9781593278281', lccn: null }]);
assert.ok(changes.some((c) => c.id === b.id));
});
test('edition backfill: a network failure leaves NULL (retried next start), and a later answer is not overwritten if typed meanwhile', async () => {
let down = true;
const { db, jobs, logs } = editionSetup(async () => { if (down) throw new TemporaryError('Library of Congress timed out'); return 'Second edition'; });
const b = tailBook(db, { isbn13: '9781593278281' });
jobs.resumeEditions();
await jobs.idle();
assert.equal(db.getBook(b.id)!.edition, null);
assert.ok(logs.some((l) => l.includes('left for the next start')));
assert.deepEqual(db.listEditionMissingIds(), [b.id]);
down = false;
jobs.resumeEditions();
await jobs.idle();
assert.equal(db.getBook(b.id)!.edition, 'Second edition');
});