Add resolver, server, PDF rendering and web app
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
6a7fcdc4b8
commit
bdfdf581f3
59 files changed
+4088
No files matched your search
@@ -0,0 +1,142 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { openDb } from './db.ts';
|
||||
import { createJobs } from './jobs.ts';
|
||||
import type { ResolverLike } from './jobs.ts';
|
||||
import type { Book, Resolution } from '../shared/types.ts';
|
||||
|
||||
class TemporaryError extends Error {}
|
||||
|
||||
const found: Resolution = {
|
||||
meta: { title: 'The Rust Book', authors: ['Klabnik, Steve'], subjects: ['Rust'], year: 2018, publisher: 'No Starch' },
|
||||
callNumber: 'QA76.73.R87 K53 2018', sortKey: 'QA 76', confidence: 'exact', source: 'loc-isbn', dewey: '005.1',
|
||||
reason: null,
|
||||
};
|
||||
|
||||
function setup(resolve: ResolverLike['resolve']) {
|
||||
const db = openDb(':memory:');
|
||||
const changes: Book[] = [];
|
||||
const resolver: ResolverLike = { resolve, search: async () => [] };
|
||||
const jobs = createJobs({ db, resolver, onChange: (b) => changes.push(b), retryDelaysMs: [0, 0, 0] });
|
||||
return { db, jobs, changes };
|
||||
}
|
||||
|
||||
test('success resolves the book and emits changes', async () => {
|
||||
const { db, jobs, changes } = setup(async (_i, o) => { o?.onMeta?.(found.meta!); return found; });
|
||||
const { book } = db.insertPending({ isbn13: '9781593278281', title: '9781593278281' });
|
||||
jobs.enqueue(book.id);
|
||||
await jobs.idle();
|
||||
const b = db.getBook(book.id)!;
|
||||
assert.equal(b.status, 'resolved');
|
||||
assert.equal(b.title, 'The Rust Book');
|
||||
assert.equal(b.callNumber, 'QA76.73.R87 K53 2018');
|
||||
assert.equal(b.confidence, 'exact');
|
||||
assert.equal(b.dewey, '005.1');
|
||||
assert.ok(changes.length >= 2);
|
||||
});
|
||||
|
||||
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' });
|
||||
jobs.enqueue(book.id);
|
||||
await jobs.idle();
|
||||
const b = db.getBook(book.id)!;
|
||||
assert.equal(b.status, 'needs_attention');
|
||||
assert.equal(b.attentionReason, 'No Library of Congress number found');
|
||||
assert.equal(b.title, 'The Rust Book');
|
||||
});
|
||||
|
||||
test('no metadata -> needs_attention', async () => {
|
||||
const reason = 'Not found in Open Library or the Library of Congress';
|
||||
const { db, jobs } = setup(async () => ({ meta: null, callNumber: null, sortKey: null, confidence: null, source: null, dewey: null, reason }));
|
||||
const { book } = db.insertPending({ isbn13: '9781593278281', title: '9781593278281' });
|
||||
jobs.enqueue(book.id);
|
||||
await jobs.idle();
|
||||
assert.equal(db.getBook(book.id)!.attentionReason, reason);
|
||||
assert.equal(db.getBook(book.id)!.title, '9781593278281');
|
||||
});
|
||||
|
||||
test('temporary failures retry then succeed', async () => {
|
||||
let calls = 0;
|
||||
const { db, jobs } = setup(async () => {
|
||||
if (++calls < 3) throw new TemporaryError('blocked');
|
||||
return found;
|
||||
});
|
||||
const { book } = db.insertPending({ isbn13: '9781593278281', title: 'x' });
|
||||
jobs.enqueue(book.id);
|
||||
await jobs.idle();
|
||||
assert.equal(calls, 3);
|
||||
assert.equal(db.getBook(book.id)!.status, 'resolved');
|
||||
});
|
||||
|
||||
test('failure after all retries -> needs_attention', async () => {
|
||||
let calls = 0;
|
||||
const { db, jobs } = setup(async () => { calls++; throw new TemporaryError('blocked'); });
|
||||
const { book } = db.insertPending({ isbn13: '9781593278281', title: 'x' });
|
||||
jobs.enqueue(book.id);
|
||||
await jobs.idle();
|
||||
assert.equal(calls, 4);
|
||||
const b = db.getBook(book.id)!;
|
||||
assert.equal(b.status, 'needs_attention');
|
||||
assert.equal(b.attentionReason, 'Lookup failed: blocked');
|
||||
});
|
||||
|
||||
test('a non-temporary error never loses the book or stops the queue', async () => {
|
||||
let n = 0;
|
||||
const { db, jobs } = setup(async () => { if (n++ === 0) throw new Error('boom'); return found; });
|
||||
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.equal(db.getBook(a.id)!.attentionReason, 'Lookup failed: boom');
|
||||
assert.equal(db.getBook(b.id)!.status, 'resolved');
|
||||
});
|
||||
|
||||
test('manual override keeps its call number', async () => {
|
||||
const { db, jobs } = setup(async () => found);
|
||||
const { book } = db.insertPending({ isbn13: '9781593278281', title: 'Mine', callNumber: 'PS3558.E63 D8 2005',
|
||||
sortKey: 'PS', confidence: 'manual', source: 'manual', manualOverride: true });
|
||||
jobs.enqueue(book.id);
|
||||
await jobs.idle();
|
||||
const b = db.getBook(book.id)!;
|
||||
assert.equal(b.callNumber, 'PS3558.E63 D8 2005');
|
||||
assert.equal(b.confidence, 'manual');
|
||||
assert.equal(b.title, 'Mine');
|
||||
});
|
||||
|
||||
test('manual override set while pending is not clobbered', async () => {
|
||||
const { db, jobs } = setup(async () => found);
|
||||
const { book } = db.insertPending({ isbn13: '9781593278281', title: 'x' });
|
||||
jobs.enqueue(book.id);
|
||||
db.updateBook(book.id, { manualOverride: true, callNumber: 'PS3558', status: 'resolved', confidence: 'manual' });
|
||||
await jobs.idle();
|
||||
const b = db.getBook(book.id)!;
|
||||
assert.equal(b.callNumber, 'PS3558');
|
||||
assert.equal(b.status, 'resolved');
|
||||
});
|
||||
|
||||
test('one job at a time, and resume() picks up pending rows', async () => {
|
||||
let active = 0, peak = 0;
|
||||
const { db, jobs } = setup(async () => {
|
||||
peak = Math.max(peak, ++active);
|
||||
await new Promise((r) => setImmediate(r));
|
||||
active--;
|
||||
return found;
|
||||
});
|
||||
const ids = ['9781593278281', '9780201835953', '9780441172719'].map((i) => db.insertPending({ isbn13: i, title: i }).book.id);
|
||||
db.updateBook(ids[2]!, { status: 'resolved' });
|
||||
jobs.resume();
|
||||
await jobs.idle();
|
||||
assert.equal(peak, 1);
|
||||
assert.equal(db.getBook(ids[0]!)!.status, 'resolved');
|
||||
assert.equal(db.getBook(ids[1]!)!.status, 'resolved');
|
||||
});
|
||||
|
||||
test('deleting a book mid-queue is harmless', async () => {
|
||||
const { db, jobs } = setup(async () => found);
|
||||
const { book } = db.insertPending({ isbn13: '9781593278281', title: 'x' });
|
||||
jobs.enqueue(book.id);
|
||||
db.deleteBook(book.id);
|
||||
await jobs.idle();
|
||||
assert.equal(db.getBook(book.id), null);
|
||||
});
|
||||
Reference in new issue
Block a user