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>
This commit is contained in:
1 parent
a08fbff685
commit
3208d4f19c
6 files changed
+696
-1
No files matched your search
@@ -0,0 +1,114 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { acceptRead, newReadState } from './readstate.ts';
|
||||
|
||||
// Replay a failure with READSTATE_SEED=<n>. SEEDS controls how many seeds run.
|
||||
const ONLY = process.env.READSTATE_SEED ? Number(process.env.READSTATE_SEED) : null;
|
||||
const SEEDS = Number(process.env.READSTATE_SEEDS ?? 500);
|
||||
|
||||
function prng(seed: number): () => number {
|
||||
let a = seed >>> 0;
|
||||
return () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
|
||||
}
|
||||
|
||||
const check13 = (d12: string) => { let s = 0; for (let i = 0; i < 12; i++) s += Number(d12[i]) * (i % 2 ? 3 : 1); return String((10 - (s % 10)) % 10); };
|
||||
const isIsbn = (c: string) => /^\d{13}$/.test(c) && /^97[89]/.test(c) && check13(c.slice(0, 12)) === c[12];
|
||||
|
||||
// Independent reference: the rules as documented (three consecutive valid-ISBN reads of one code, each within 1500 ms
|
||||
// of the previous one; non-ISBN frames never reset the run; a code just announced is quiet for 5000 ms).
|
||||
// A "run" is kept as a list of timestamps instead of a counter.
|
||||
function reference() {
|
||||
const announced = new Map<string, number>();
|
||||
const rejected = new Map<string, number>();
|
||||
let run: { isbn: string; times: number[] } | null = null;
|
||||
return (code: string, t: number): { emit: string | null; reject: boolean } => {
|
||||
if (!isIsbn(code)) {
|
||||
const prev = rejected.get(code);
|
||||
if (prev !== undefined && t - prev < 5000) return { emit: null, reject: false };
|
||||
rejected.set(code, t);
|
||||
return { emit: null, reject: true };
|
||||
}
|
||||
const prev = announced.get(code);
|
||||
if (prev !== undefined && t - prev < 5000) { run = null; return { emit: null, reject: false }; }
|
||||
const last = run && run.isbn === code ? run.times[run.times.length - 1]! : null;
|
||||
if (run && run.isbn === code && last !== null && t - last <= 1500) run.times.push(t);
|
||||
else run = { isbn: code, times: [t] };
|
||||
if (run.times.length >= 3) { run = null; announced.set(code, t); return { emit: code, reject: false }; }
|
||||
return { emit: null, reject: false };
|
||||
};
|
||||
}
|
||||
|
||||
let emits = 0;
|
||||
function scenario(seed: number): void {
|
||||
const rnd = prng(seed);
|
||||
const pick = <T,>(xs: T[]): T => xs[Math.floor(rnd() * xs.length)]!;
|
||||
const digits = (n: number) => Array.from({ length: n }, () => Math.floor(rnd() * 10)).join('');
|
||||
const valid: string[] = [];
|
||||
const nearMiss: string[] = [];
|
||||
const junk = ['', '0', '12345', '012345678905', '0123456789012', 'abcdefghijklm', '9781234', '97812345678901', '5901234123457', ' 9780306406157'];
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const body = (rnd() < 0.5 ? '978' : '979') + digits(9);
|
||||
const c = body + check13(body);
|
||||
valid.push(c);
|
||||
const wrong = (Number(c[12]) + 1 + Math.floor(rnd() * 9)) % 10;
|
||||
nearMiss.push(body + wrong);
|
||||
}
|
||||
// A valid EAN-13 check digit but not a book prefix, and a book prefix with a wrong check digit.
|
||||
const nb = '123' + digits(9); junk.push(nb + check13(nb));
|
||||
const gaps = [0, 1, 50, 120, 400, 1499, 1500, 1501, 2000, 4999, 5000, 5001, 9000];
|
||||
const ref = reference();
|
||||
let state = newReadState();
|
||||
let t = Math.floor(rnd() * 1e6);
|
||||
const n = 30 + Math.floor(rnd() * 120);
|
||||
const log: string[] = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
t += rnd() < 0.7 ? pick(gaps) : Math.floor(rnd() * 3000);
|
||||
const r = rnd();
|
||||
// Mostly one code held in view, so runs of agreement actually happen.
|
||||
const code = r < 0.55 ? pick(valid.slice(0, 2)) : r < 0.7 ? pick(valid) : r < 0.82 ? pick(nearMiss) : pick(junk);
|
||||
const want = ref(code, t);
|
||||
const got = acceptRead(state, code, t);
|
||||
state = got.state;
|
||||
log.push(`${t}:${code || '(empty)'}${got.emit ? ' EMIT' : ''}`);
|
||||
const ctx = `seed ${seed} (replay: READSTATE_SEED=${seed}) step ${i}\n${log.slice(-8).join('\n')}`;
|
||||
assert.equal(got.emit, want.emit, `emit differs from the reference. ${ctx}`);
|
||||
assert.equal(got.reject, want.reject, `reject differs from the reference. ${ctx}`);
|
||||
if (got.emit !== null) {
|
||||
emits++;
|
||||
assert.ok(isIsbn(got.emit), `emitted something that is not a valid ISBN. ${ctx}`);
|
||||
assert.ok(!nearMiss.includes(got.emit) || valid.includes(got.emit), `a wrong check digit was accepted. ${ctx}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test('random frame sequences: the accept logic matches an independent model of the three-frame rule', () => {
|
||||
const seeds = ONLY !== null ? [ONLY] : Array.from({ length: SEEDS }, (_, i) => i + 1);
|
||||
for (const s of seeds) scenario(s);
|
||||
if (ONLY === null) assert.ok(emits > 500, `the generator should reach acceptance often (got ${emits})`);
|
||||
});
|
||||
|
||||
test('a code with a wrong check digit is never accepted, however often or fast it is read', () => {
|
||||
for (let seed = 1; seed <= 200; seed++) {
|
||||
const rnd = prng(seed * 7919);
|
||||
let state = newReadState();
|
||||
let t = 0;
|
||||
const body = '978' + Array.from({ length: 9 }, () => Math.floor(rnd() * 10)).join('');
|
||||
const ok = check13(body);
|
||||
const bad = body + String((Number(ok) + 1 + Math.floor(rnd() * 9)) % 10);
|
||||
for (let i = 0; i < 60; i++) {
|
||||
t += Math.floor(rnd() * 1200);
|
||||
const r = acceptRead(state, bad, t);
|
||||
state = r.state;
|
||||
assert.equal(r.emit, null, `seed ${seed}: ${bad} accepted at step ${i}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('three agreeing reads accept, two do not, a gap over 1.5 s restarts the count (pinned examples)', () => {
|
||||
const code = '9781593278281';
|
||||
let s = newReadState();
|
||||
const feed = (t: number) => { const r = acceptRead(s, code, t); s = r.state; return r.emit; };
|
||||
assert.equal(feed(0), null); assert.equal(feed(300), null); assert.equal(feed(600), code);
|
||||
s = newReadState();
|
||||
assert.equal(feed(0), null); assert.equal(feed(300), null); assert.equal(feed(2000), null); assert.equal(feed(2300), null); assert.equal(feed(2600), code);
|
||||
});
|
||||
Reference in new issue
Block a user