Files
booksorter/src/server/db.test.ts
T
swaitsandClaude Fable 5.2 bb46d343a8 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>
2026-11-19 17:49:59 -07:00

153 lines
7.1 KiB
TypeScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { openDb } from './db.ts';
test('insertPending is idempotent on isbn13', () => {
const db = openDb(':memory:');
const a = db.insertPending({ isbn13: '9781593278281', title: 'x' });
const b = db.insertPending({ isbn13: '9781593278281', title: 'y' });
assert.equal(a.created, true);
assert.equal(b.created, false);
assert.equal(b.book.id, a.book.id);
assert.equal(db.listBooks().length, 1);
});
test('books without isbn do not collide', () => {
const db = openDb(':memory:');
db.insertPending({ title: 'a' });
db.insertPending({ title: 'b' });
assert.equal(db.listBooks().length, 2);
});
test('listBooks orders by sort key, nulls last, then title', () => {
const db = openDb(':memory:');
db.insertPending({ title: 'Zed' });
db.insertPending({ title: 'Abe' });
db.insertPending({ title: 'B', sortKey: 'QA 76' });
db.insertPending({ title: 'A', sortKey: 'PS 12' });
assert.deepEqual(db.listBooks().map((b) => b.title), ['A', 'B', 'Abe', 'Zed']);
});
test('filters and search', () => {
const db = openDb(':memory:');
const a = db.insertPending({ title: 'Rust Book', authors: ['Klabnik, Steve'], inQueue: true }).book;
db.insertPending({ title: 'Other', status: 'needs_attention', inQueue: false });
assert.deepEqual(db.listBooks({ filter: 'queue' }).map((b) => b.id), [a.id]);
assert.equal(db.listBooks({ filter: 'attention' }).length, 1);
assert.equal(db.listBooks({ q: 'klabnik' }).length, 1);
});
test('updateBook round-trips fields', () => {
const db = openDb(':memory:');
const { book } = db.insertPending({ title: 't' });
const u = db.updateBook(book.id, { authors: ['A'], manualOverride: true, status: 'resolved', year: 2001 })!;
assert.deepEqual(u.authors, ['A']);
assert.equal(u.manualOverride, true);
assert.equal(u.year, 2001);
assert.equal(db.updateBook(999, { title: 'z' }), null);
assert.deepEqual(db.listPendingIds(), []);
assert.equal(db.deleteBook(book.id), true);
});
test('settings and cache', () => {
const db = openDb(':memory:');
assert.deepEqual(db.getSettings(), { offsetXmm: 0, offsetYmm: 0, rotationDeg: 0 });
db.putSettings({ offsetXmm: 1.5, offsetYmm: -2.05, rotationDeg: 0.37 });
assert.deepEqual(db.getSettings(), { offsetXmm: 1.5, offsetYmm: -2.05, rotationDeg: 0.37 });
assert.equal(db.cache.get('k'), null);
db.cache.set('k', 'v');
db.cache.set('k', 'w');
assert.equal(db.cache.get('k'), 'w');
});
test('settings saved before rotation existed read rotation as 0', async () => {
const { DatabaseSync } = await import('node:sqlite');
const { mkdtempSync, rmSync } = await import('node:fs');
const { tmpdir } = await import('node:os');
const { join } = await import('node:path');
const dir = mkdtempSync(join(tmpdir(), 'booksorter-settings-'));
try {
const path = join(dir, 'old.db');
const first = openDb(path);
first.close();
const raw = new DatabaseSync(path);
raw.prepare("INSERT INTO settings (key, value) VALUES ('settings', ?)").run(JSON.stringify({ offsetXmm: -2.7, offsetYmm: 0.5 }));
raw.close();
const db = openDb(path);
assert.deepEqual(db.getSettings(), { offsetXmm: -2.7, offsetYmm: 0.5, rotationDeg: 0 });
db.close();
} finally { rmSync(dir, { recursive: true, force: true }); }
});
test('a database made before the hierarchy tail gets the column; old books read as not looked for', async () => {
const { DatabaseSync } = await import('node:sqlite');
const { mkdtempSync, rmSync } = await import('node:fs');
const { tmpdir } = await import('node:os');
const { join } = await import('node:path');
const dir = mkdtempSync(join(tmpdir(), 'booksorter-migrate-'));
try {
const path = join(dir, 'old.db');
const old = new DatabaseSync(path);
old.exec(`
CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT, isbn13 TEXT UNIQUE, lccn TEXT, title TEXT NOT NULL, subtitle TEXT,
authors TEXT NOT NULL DEFAULT '[]', publisher TEXT, year INTEGER, subjects TEXT NOT NULL DEFAULT '[]',
call_number TEXT, sort_key TEXT, confidence TEXT, source TEXT, dewey TEXT,
status TEXT NOT NULL DEFAULT 'pending', attention_reason TEXT, manual_override INTEGER NOT NULL DEFAULT 0,
in_queue INTEGER NOT NULL DEFAULT 0, printed_at TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL
);
CREATE TABLE source_cache (key TEXT PRIMARY KEY, body TEXT NOT NULL);
CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT NOT NULL);
INSERT INTO books (isbn13, title, call_number, sort_key, confidence, source, status, in_queue, printed_at, created_at, updated_at)
VALUES ('9780874805451', 'Canyoneering 3', 'GV199.42.U82 G733 1997', 'GV 0199.42 U82 G733 1997', 'exact', 'loc-isbn',
'resolved', 1, '2026-01-02T00:00:00.000Z', '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z');
`);
old.close();
const db = openDb(path);
const before = db.listBooks()[0]!;
assert.equal(before.hierarchyTail, null);
assert.equal(before.callNumber, 'GV199.42.U82 G733 1997');
assert.deepEqual(db.listTailMissingIds(), [before.id]);
assert.equal(before.edition, null); // the edition column was added too; old books read as not looked up
assert.deepEqual(db.listEditionMissingIds(), [before.id]);
assert.equal(db.updateBook(before.id, { edition: 'Third edition' })!.edition, 'Third edition');
assert.equal(db.updateBook(before.id, { edition: '' })!.edition, '');
assert.deepEqual(db.listEditionMissingIds(), []);
assert.equal(db.updateBook(before.id, { edition: null })!.edition, null);
const after = db.updateBook(before.id, { hierarchyTail: ['Utah'] })!;
assert.deepEqual(after.hierarchyTail, ['Utah']);
assert.equal(after.printedAt, before.printedAt);
assert.equal(db.updateBook(before.id, { hierarchyTail: [] })!.hierarchyTail?.length, 0);
assert.deepEqual(db.listTailMissingIds(), []);
assert.equal(db.updateBook(before.id, { hierarchyTail: null })!.hierarchyTail, null);
db.close();
const again = openDb(path); // opening a migrated database again is harmless
assert.equal(again.listBooks().length, 1);
again.close();
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test('search finds an inverted stored name by its natural form', () => {
const db = openDb(':memory:');
db.insertPending({ title: 'Canyoneering 2', authors: ['Allen, Steve'] });
assert.equal(db.listBooks({ q: 'steve allen' }).length, 1);
assert.equal(db.listBooks({ q: 'allen, steve' }).length, 1);
assert.equal(db.listBooks({ q: 'nobody' }).length, 0);
});
test('edition: NULL means not looked up, empty means none, text is kept as given', () => {
const db = openDb(':memory:');
const a = db.insertPending({ isbn13: '9781593278281', title: 'A', status: 'resolved' }).book;
const b = db.insertPending({ isbn13: '9780201835953', title: 'B', status: 'resolved', edition: '' }).book;
const c = db.insertPending({ title: 'C', status: 'resolved', edition: '2nd ed.' }).book;
assert.equal(a.edition, null);
assert.equal(b.edition, '');
assert.equal(c.edition, '2nd ed.');
assert.deepEqual(db.listEditionMissingIds(), [a.id]);
db.close();
});