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:
1 parent
468cafc6db
commit
b5defb4e4f
46 files changed
+1957
-406
No files matched your search
+51
-5
@@ -10,20 +10,20 @@ const found: Resolution = {
|
||||
callNumber: 'QA76.73.R87 K53 2018', sortKey: 'QA 76', confidence: 'exact', source: 'loc-isbn', dewey: null, reason: null,
|
||||
};
|
||||
|
||||
function setup(resolve = async () => found) {
|
||||
function setup(resolve = async () => found, port?: number) {
|
||||
const db = openDb(':memory:');
|
||||
const events = createEventBus();
|
||||
const resolver = { resolve, search: async () => [{ title: 'Hit', authors: [], lcc: [] }] };
|
||||
const jobs = createJobs({ db, resolver, onChange: (b) => events.emitBook(b), retryDelaysMs: [0] });
|
||||
const jobs = createJobs({ db, resolver, onChange: (b) => events.emitBook(b), retryDelaysMs: [0], log: () => {} });
|
||||
const pdfCalls: { cards: CardData[]; opts: Settings & { startPosition: number } }[] = [];
|
||||
const app = createApp({
|
||||
db, jobs, resolver, events,
|
||||
db, jobs, resolver, events, port,
|
||||
renderCardsPdf: async (cards, opts) => { pdfCalls.push({ cards, opts }); return new TextEncoder().encode('%PDF-stub'); },
|
||||
renderCalibrationPdf: async () => new TextEncoder().encode('%PDF-cal'),
|
||||
classNames: (l) => ({ className: 'Class ' + l[0], subclassName: 'Sub ' + l }),
|
||||
});
|
||||
const req = (method: string, path: string, json?: unknown) =>
|
||||
app.request(path, { method, ...(json === undefined ? {} : { body: JSON.stringify(json), headers: { 'content-type': 'application/json' } }) });
|
||||
const req = (method: string, path: string, json?: unknown, extra: Record<string, string> = {}) =>
|
||||
app.request(path, { method, headers: { ...(json === undefined ? {} : { 'content-type': 'application/json' }), ...extra }, ...(json === undefined ? {} : { body: JSON.stringify(json) }) });
|
||||
return { db, jobs, app, req, pdfCalls, events };
|
||||
}
|
||||
|
||||
@@ -176,3 +176,49 @@ test('events stream delivers changes and cleans up on disconnect', async () => {
|
||||
await res2.body!.cancel();
|
||||
assert.equal(events.listenerCount(), 0);
|
||||
});
|
||||
|
||||
test('state-changing requests from another origin are refused', async () => {
|
||||
const { req, db } = setup(undefined, 4321);
|
||||
const ok = ['http://localhost:4321', 'http://127.0.0.1:4321'];
|
||||
const bad = ['http://evil.example', 'http://localhost:9999', 'http://localhost:4321.evil.com', 'null', 'https://localhost:4321'];
|
||||
for (const origin of ok) assert.equal((await req('POST', '/api/books/manual', { title: 'T' }, { origin })).status, 200, origin);
|
||||
for (const method of ['POST', 'PUT', 'PATCH', 'DELETE']) {
|
||||
for (const origin of bad) {
|
||||
const r = await req(method, '/api/books/1', method === 'DELETE' ? undefined : { title: 'x' }, { origin });
|
||||
assert.equal(r.status, 403, `${method} ${origin}`);
|
||||
}
|
||||
}
|
||||
assert.equal(db.listBooks().length, 2);
|
||||
assert.equal((await req('GET', '/api/books', undefined, { origin: 'http://evil.example' })).status, 200);
|
||||
assert.equal((await req('POST', '/api/books/manual', { title: 'No origin header' })).status, 200);
|
||||
});
|
||||
|
||||
test('PATCH year must be an integer or null', async () => {
|
||||
const { req, db, jobs } = setup();
|
||||
const m = await (await req('POST', '/api/books/manual', { title: 'Foo', year: 2000 })).json() as any;
|
||||
await jobs.idle();
|
||||
for (const year of ['abc', 1999.5, true, {}, -4]) {
|
||||
assert.equal((await req('PATCH', `/api/books/${m.book.id}`, { year })).status, 400, JSON.stringify(year));
|
||||
}
|
||||
assert.equal(db.getBook(m.book.id)!.year, 2000);
|
||||
assert.equal((await req('PATCH', `/api/books/${m.book.id}`, { year: 2011 })).status, 200);
|
||||
assert.equal(db.getBook(m.book.id)!.year, 2011);
|
||||
assert.equal((await req('PATCH', `/api/books/${m.book.id}`, { year: null })).status, 200);
|
||||
assert.equal(db.getBook(m.book.id)!.year, null);
|
||||
});
|
||||
|
||||
test('retry forgets the book\'s cached source responses', async () => {
|
||||
const { req, db, jobs } = setup();
|
||||
const s = await (await req('POST', '/api/scan', { isbn: '9781593278281' })).json() as any;
|
||||
await jobs.idle();
|
||||
db.updateBook(s.book.id, { lccn: '2018012345', title: 'Rust Book' });
|
||||
for (const k of ['ol-data:9781593278281', 'ol-search:9781593278281', 'loc-isbn:9781593278281', 'loc-lccn:2018012345',
|
||||
'loc-title:Rust Book|Steve Klabnik', 'ol-data:9780201835953', 'loc-title:Other|X']) db.cache.set(k, 'body');
|
||||
assert.equal((await req('POST', `/api/books/${s.book.id}/retry`)).status, 200);
|
||||
await jobs.idle();
|
||||
for (const k of ['ol-data:9781593278281', 'ol-search:9781593278281', 'loc-isbn:9781593278281', 'loc-lccn:2018012345', 'loc-title:Rust Book|Steve Klabnik']) {
|
||||
assert.equal(db.cache.get(k), null, k);
|
||||
}
|
||||
assert.equal(db.cache.get('ol-data:9780201835953'), 'body');
|
||||
assert.equal(db.cache.get('loc-title:Other|X'), 'body');
|
||||
});
|
||||
Reference in new issue
Block a user