Files
booksorter/scripts/live-lookup-check.ts
swaitsandClaude Fable 5.2 3208d4f19c Add property tests, an old-database test and a live lookup check
The queue test runs random sequences of enqueue, look up again, cancel,
failure, time and restart. It found a book left pending for ever when
Look up again landed on a book already queued for its weekly re-check;
that lookup now counts as a real one.

Also: the three-frame camera rule against an independent model, a
pre-looked_up_at database opening intact, a script that checks nine
books against the live catalogues, and the sisi contract naming them.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-22 19:55:31 -07:00

70 lines
4.4 KiB
TypeScript

// Live check of the real resolver against the real catalogues. Not a unit test (the name does not end in .test.ts),
// so `npm test` never runs it. Usage: node scripts/live-lookup-check.ts [--regressions-only]
// Exits 0 only when every book gets the expected call number. A temporary failure (Open Library drops connections
// often) is retried with backoff for up to a few minutes before the book counts as failed.
import { createResolver, TemporaryError } from '../src/server/resolve/resolver.ts';
import { backoffMs } from '../src/server/jobs.ts';
import { jobMemo } from '../src/server/sources/http.ts';
interface Case { isbn: string; label: string; check: (callNumber: string) => boolean; expect: string }
const norm = (s: string) => s.replace(/\s+/g, ' ').trim();
const exact = (want: string) => (got: string) => norm(got) === norm(want);
// A target is a class prefix: "GV199.42.U8" accepts GV199.42.U82 P76 2019 (the number is chosen from similar books, so only the class is pinned).
const prefix = (...wants: string[]) => (got: string) => wants.some((w) => norm(got).startsWith(w));
const REGRESSIONS: Case[] = [
{ isbn: '9781640976108', label: 'F721.F57 2023', expect: 'F721.F57 2023', check: exact('F721.F57 2023') },
{ isbn: '9781493029150', label: 'GB1198.3.C6 W25 2018', expect: 'GB1198.3.C6 W25 2018', check: exact('GB1198.3.C6 W25 2018') },
{ isbn: '9780826363770', label: 'F774.3.M86 2022', expect: 'F774.3.M86 2022', check: exact('F774.3.M86 2022') },
{ isbn: '9781937052898', label: 'GV854.5.C6 F68 2024', expect: 'GV854.5.C6 F68 2024', check: exact('GV854.5.C6 F68 2024') },
{ isbn: '9780874808131', label: 'GV199.42.C63 M58 2005', expect: 'GV199.42.C63 M58 2005', check: exact('GV199.42.C63 M58 2005') },
{ isbn: '9781426220951', label: 'GV199.5.S55 2020', expect: 'GV199.5.S55 2020', check: exact('GV199.5.S55 2020') },
];
const TARGETS: Case[] = [
{ isbn: '9780984221356', label: 'Durango Hikes', expect: 'GV199.42.C6...', check: prefix('GV199.42.C6') },
{ isbn: '9780692215005', label: 'San Rafael Swell', expect: 'GV199.42.U8... or F832...', check: prefix('GV199.42.U8', 'F832') },
{ isbn: '9798886472189', label: 'Adventures of a Lifetime', expect: 'E160... or GV191...', check: prefix('E160', 'GV191') },
];
const MAX_WAIT_MS = 5 * 60_000;
const cases = process.argv.includes('--regressions-only') ? REGRESSIONS : [...REGRESSIONS, ...TARGETS];
const store = new Map<string, string>();
const resolver = createResolver({ fetch: globalThis.fetch, cache: { get: (k) => store.get(k) ?? null, set: (k, v) => { store.set(k, v); } } });
const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
async function lookup(isbn: string): Promise<{ callNumber: string | null; derivation: string; note: string }> {
const memo = new Map<string, string>(); // what the sources already answered for real: a retry asks only for the rest
const started = Date.now();
for (let attempt = 1; ; attempt++) {
let similar = '';
try {
const res = await jobMemo.run(memo, () => resolver.resolve({ isbn13: isbn }, { onSimilar: (d) => { similar = d; } }));
const derivation = res.callNumber ? `${res.source}${res.source === 'similar' && similar ? ` (${similar})` : ''}${res.confidence ? `, ${res.confidence}` : ''}` : `none (${res.reason ?? 'no reason'})`;
return { callNumber: res.callNumber, derivation, note: attempt > 1 ? `after ${attempt} attempts` : '' };
} catch (e) {
if (!(e instanceof TemporaryError)) throw e;
const wait = backoffMs(attempt, e.retryAfterMs);
if (Date.now() - started + wait > MAX_WAIT_MS) throw new Error(`still failing after ${attempt} attempts: ${e.message}`);
await sleep(wait);
}
}
}
let failed = 0;
for (const c of cases) {
const t0 = Date.now();
let got: string | null = null, derivation = '', ok = false, err = '';
try {
const r = await lookup(c.isbn);
got = r.callNumber; derivation = r.derivation + (r.note ? `, ${r.note}` : '');
ok = got !== null && c.check(got);
} catch (e) { err = e instanceof Error ? e.message : String(e); }
const secs = ((Date.now() - t0) / 1000).toFixed(1);
if (!ok) failed++;
console.log(`${ok ? 'ok ' : 'FAIL'} ${c.isbn} ${got ?? '(none)'} [${err || derivation}] ${secs}s${ok ? '' : ` expected ${c.expect}`}`);
}
console.log(failed === 0 ? `PASS (${cases.length} books)` : `FAIL (${failed} of ${cases.length} books)`);
process.exit(failed === 0 ? 0 : 1);