Rework scan screen, make lookups non-blocking, fix review findings

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-18 10:19:32 -07:00
1 parent 468cafc6db
commit b5defb4e4f
46 files changed
+1957 -406

No files matched your search

+123 -5
View File
@@ -1,7 +1,7 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { openDb } from './db.ts';
import { createJobs } from './jobs.ts';
import { createJobs, DEFAULT_RETRY_DELAYS_MS } from './jobs.ts';
import type { ResolverLike } from './jobs.ts';
import type { Book, Resolution } from '../shared/types.ts';
@@ -13,12 +13,13 @@ const found: Resolution = {
reason: null,
};
function setup(resolve: ResolverLike['resolve']) {
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 jobs = createJobs({ db, resolver, onChange: (b) => changes.push(b), retryDelaysMs: [0, 0, 0] });
return { db, jobs, changes };
const jobs = createJobs({ db, resolver, onChange: (b) => changes.push(b), retryDelaysMs: delays, log: (l) => logs.push(l) });
return { db, jobs, changes, logs };
}
test('success resolves the book and emits changes', async () => {
@@ -37,7 +38,7 @@ test('success resolves the book and emits changes', async () => {
test('no call number -> needs_attention with reason, metadata kept', async () => {
const { db, jobs } = setup(async () => ({ ...found, callNumber: null, sortKey: null, confidence: null, source: null, reason: 'No Library of Congress number found' }));
const { book } = db.insertPending({ isbn13: '9781593278281', title: 'x' });
const { book } = db.insertPending({ isbn13: '9781593278281', title: '9781593278281' });
jobs.enqueue(book.id);
await jobs.idle();
const b = db.getBook(book.id)!;
@@ -140,3 +141,120 @@ test('deleting a book mid-queue is harmless', async () => {
await jobs.idle();
assert.equal(db.getBook(book.id), null);
});
test('default retry delays are 5 s, 30 s, 120 s', () => {
assert.deepEqual(DEFAULT_RETRY_DELAYS_MS, [5000, 30000, 120000]);
});
test('a waiting retry does not block the books behind it', async () => {
const order: string[] = [];
let aCalls = 0;
const { db, jobs } = setup(async (i) => {
if (i.isbn13 === '9781593278281' && ++aCalls === 1) throw new TemporaryError('LoC did not answer');
order.push(i.isbn13!);
return found;
}, [60, 60, 60]);
const a = db.insertPending({ isbn13: '9781593278281', title: 'a' }).book;
const b = db.insertPending({ isbn13: '9780201835953', title: 'b' }).book;
jobs.enqueue(a.id); jobs.enqueue(b.id);
await jobs.idle();
assert.deepEqual(order, ['9780201835953', '9781593278281']);
assert.equal(db.getBook(a.id)!.status, 'resolved');
});
test('a book waiting for a retry stays pending with a progress note, cleared when it resolves', async () => {
let calls = 0;
const { db, jobs, changes } = setup(async () => {
if (++calls === 1) throw new TemporaryError('Network error for https://lx2.loc.gov/sru/lcdb: fetch failed');
return found;
}, [40, 40, 40]);
const { book } = db.insertPending({ isbn13: '9781593278281', title: '9781593278281' });
jobs.enqueue(book.id);
await new Promise((r) => setTimeout(r, 15));
const mid = db.getBook(book.id)!;
assert.equal(mid.status, 'pending');
assert.equal(mid.attentionReason, 'Library of Congress did not answer. Trying again shortly.');
assert.ok(changes.some((c) => c.attentionReason === mid.attentionReason), 'a change was emitted');
await jobs.idle();
const done = db.getBook(book.id)!;
assert.equal(done.status, 'resolved');
assert.equal(done.attentionReason, null);
});
test('retry timers are unref\'d', async () => {
const real = globalThis.setTimeout;
const made: ReturnType<typeof setTimeout>[] = [];
globalThis.setTimeout = ((fn: () => void, ms?: number) => { const t = real(fn, ms); made.push(t); return t; }) as typeof setTimeout;
try {
const { db, jobs } = setup(async () => { throw new TemporaryError('x'); }, [60_000]);
const { book } = db.insertPending({ isbn13: '9781593278281', title: 'x' });
jobs.enqueue(book.id);
await new Promise((r) => real(r, 20));
assert.equal(made.length, 1);
assert.equal(made[0]!.hasRef(), false);
jobs.enqueue(book.id); // cancels the wait; ends the test cleanly
} finally { globalThis.setTimeout = real; }
});
test('only the final attempt asks the resolver to tolerate LoC failure', async () => {
const flags: (boolean | undefined)[] = [];
const { db, jobs } = setup(async (_i, o) => {
flags.push(o?.tolerateLocFailure);
if (flags.length < 4) throw new TemporaryError('blocked');
return found;
});
const { book } = db.insertPending({ isbn13: '9781593278281', title: 'x' });
jobs.enqueue(book.id);
await jobs.idle();
assert.deepEqual(flags, [false, false, false, true]);
assert.equal(db.getBook(book.id)!.status, 'resolved');
});
test('"Look up again" while waiting restarts the retries at once', async () => {
let calls = 0;
const { db, jobs } = setup(async () => { if (++calls === 1) throw new TemporaryError('x'); return found; }, [60_000]);
const { book } = db.insertPending({ isbn13: '9781593278281', title: 'x' });
jobs.enqueue(book.id);
await new Promise((r) => setTimeout(r, 15));
assert.equal(db.getBook(book.id)!.status, 'pending');
jobs.enqueue(book.id);
await jobs.idle();
assert.equal(db.getBook(book.id)!.status, 'resolved');
});
test('one log line per attempt: ISBN, outcome and elapsed ms', async () => {
let calls = 0;
const { db, jobs, logs } = setup(async () => { if (++calls === 1) throw new TemporaryError('timed out'); return found; });
const { book } = db.insertPending({ isbn13: '9781593278281', title: 'x' });
jobs.enqueue(book.id);
await jobs.idle();
assert.equal(logs.length, 2);
assert.match(logs[0]!, /9781593278281.*timed out.*\d+ ms/);
assert.match(logs[1]!, /9781593278281.*QA76\.73\.R87 K53 2018 \(loc-isbn\).* in \d+ ms/);
const t = setup(async () => ({ ...found, callNumber: null, sortKey: null, confidence: null, source: null, reason: 'No Library of Congress number found' }));
const nb = t.db.insertPending({ title: 'Some Title', authors: ['A'] }).book;
t.jobs.enqueue(nb.id);
await t.jobs.idle();
assert.match(t.logs[0]!, /Some Title.*No Library of Congress number found.*\d+ ms/);
});
test('lookup metadata fills empty fields but never overwrites what the user typed', async () => {
const { db, jobs } = setup(async (_i, o) => { o?.onMeta?.(found.meta!); return found; });
const typed = db.insertPending({ isbn13: '9781593278281', title: 'My Title', authors: ['Me Myself'], publisher: 'Mine', year: 1999 }).book;
const bare = db.insertPending({ isbn13: '9780201835953', title: '9780201835953' }).book;
jobs.enqueue(typed.id); jobs.enqueue(bare.id);
await jobs.idle();
const t = db.getBook(typed.id)!;
assert.deepEqual([t.title, t.authors, t.publisher, t.year], ['My Title', ['Me Myself'], 'Mine', 1999]);
assert.deepEqual(t.subjects, ['Rust']); // was empty, so filled
const b = db.getBook(bare.id)!;
assert.deepEqual([b.title, b.authors, b.publisher, b.year], ['The Rust Book', ['Klabnik, Steve'], 'No Starch', 2018]);
});
test('an LCCN placeholder title counts as empty', async () => {
const { db, jobs } = setup(async (_i, o) => { o?.onMeta?.(found.meta!); return found; });
const { book } = db.insertPending({ lccn: '88005934', title: '88005934' });
jobs.enqueue(book.id);
await jobs.idle();
assert.equal(db.getBook(book.id)!.title, 'The Rust Book');
});