Cards now carry a QR code holding the book's own record (call number, title, authors, ISBN, year), and the scanner reads it back to find or add that book. Call number selection no longer pads catalogue numbers with generated cutters. A number is "exact" only when it comes verbatim from a record for this edition; a bare class is a hint that steers the other-edition and similar-books lookups; a record fetched by LCCN must list the book's ISBN to count as this edition. "Look up again" bypasses every cached result, and shelf order breaks ties by author then title. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
69 lines
3.6 KiB
TypeScript
69 lines
3.6 KiB
TypeScript
import { test, before } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import { createRequire } from 'node:module';
|
|
import { prepareZXingModule, readBarcodes } from 'zxing-wasm/reader';
|
|
import { qrMatrix, qrRects, QR_BOTTOM, QR_SIZE, QR_TOP, QR_X } from './qr.ts';
|
|
import { encodeCardCode, parseCardCode } from '../../shared/cardcode.ts';
|
|
import type { CardCode } from '../../shared/cardcode.ts';
|
|
|
|
const short = 'QA76.73.R87 K53 2018\nThe Rust Programming Language\nSteve Klabnik; Carol Nichols\nISBN 9781718500440\n2018\nbooksorter 1';
|
|
|
|
test('the symbol is 90 pt square, flush left, from y 32 to 122, for any version', () => {
|
|
const longest = encodeCardCode({
|
|
callNumber: 'QA76.73.C153 S77 K47 2013', approximate: true, title: 'x'.repeat(200), authors: ['a'.repeat(60)], isbn13: '9781718500440', year: 2018,
|
|
});
|
|
for (const payload of [short, longest, 'booksorter 1']) {
|
|
const { rects, version } = qrRects(payload);
|
|
assert.ok(version >= 1 && version <= 6, `version ${version}`);
|
|
const x0 = Math.min(...rects.map(r => r.x)), x1 = Math.max(...rects.map(r => r.x + r.width));
|
|
const y0 = Math.min(...rects.map(r => r.y)), y1 = Math.max(...rects.map(r => r.y + r.height));
|
|
// finder patterns sit in every corner except bottom-right, so all four edges are touched
|
|
assert.ok(Math.abs(x0 - QR_X) < 1e-9 && Math.abs(x1 - (QR_X + QR_SIZE)) < 1e-9);
|
|
assert.ok(Math.abs(y0 - QR_BOTTOM) < 1e-9 && Math.abs(y1 - QR_TOP) < 1e-9);
|
|
assert.equal(QR_X, 14);
|
|
}
|
|
});
|
|
|
|
test('horizontal runs are merged: rectangles cover exactly the dark modules', () => {
|
|
const m = qrMatrix(short), n = m.length, mod = QR_SIZE / n;
|
|
const { rects } = qrRects(short);
|
|
let dark = 0;
|
|
for (const row of m) for (const v of row) if (v) dark++;
|
|
const area = rects.reduce((a, r) => a + r.width * r.height, 0);
|
|
assert.ok(Math.abs(area - dark * mod * mod) < 1e-6);
|
|
assert.ok(rects.length < dark);
|
|
for (const r of rects) assert.ok(Math.abs(r.height - mod) < 1e-9);
|
|
});
|
|
|
|
// ---- round trip: draw the symbol into pixels and read it back with zxing-wasm ----------------------
|
|
before(async () => {
|
|
const wasm = readFileSync(createRequire(import.meta.url).resolve('zxing-wasm/reader').replace(/dist[\\/].*$/, 'dist/reader/zxing_reader.wasm'));
|
|
await prepareZXingModule({ overrides: { wasmBinary: wasm.buffer.slice(wasm.byteOffset, wasm.byteOffset + wasm.byteLength) as ArrayBuffer }, fireImmediately: true });
|
|
});
|
|
|
|
function raster(matrix: boolean[][], px = 6, quiet = 4): { data: Uint8ClampedArray; width: number; height: number } {
|
|
const n = matrix.length, size = (n + 2 * quiet) * px, data = new Uint8ClampedArray(size * size * 4).fill(255);
|
|
for (let r = 0; r < n; r++) for (let c = 0; c < n; c++) {
|
|
if (!matrix[r]![c]) continue;
|
|
for (let y = 0; y < px; y++) for (let x = 0; x < px; x++) {
|
|
const i = (((r + quiet) * px + y) * size + (c + quiet) * px + x) * 4;
|
|
data[i] = data[i + 1] = data[i + 2] = 0;
|
|
}
|
|
}
|
|
return { data, width: size, height: size };
|
|
}
|
|
|
|
test('a printed symbol with a non-ASCII title reads back to the same payload', async () => {
|
|
const card: CardCode = {
|
|
callNumber: 'PQ2230.A1 H8 1987', approximate: false, title: 'Les Misérables',
|
|
authors: ['Victor Hugo'], isbn13: '9780140444308', year: 1987,
|
|
};
|
|
const payload = encodeCardCode(card);
|
|
const [hit] = await readBarcodes(raster(qrMatrix(payload)) as ImageData, { formats: ['QRCode'], tryHarder: true });
|
|
assert.ok(hit, 'zxing found no QR code');
|
|
assert.equal(hit.format, 'QRCode');
|
|
assert.equal(hit.text, payload);
|
|
assert.deepEqual(parseCardCode(hit.text), card);
|
|
});
|