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
+30
-34
@@ -1,52 +1,48 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { acceptRead, endRound, newReadState } from './readstate.ts';
|
||||
import { acceptRead, markEmitted, newReadState } from './readstate.ts';
|
||||
|
||||
const ISBN = '9780306406157';
|
||||
|
||||
test('single read emits nothing, second consecutive read emits', () => {
|
||||
let r = acceptRead(newReadState(), ISBN, 0);
|
||||
assert.equal(r.emit, null);
|
||||
r = acceptRead(endRound(r.state, [ISBN]), ISBN, 150);
|
||||
test('a valid ISBN-13 is accepted on the first read', () => {
|
||||
const r = acceptRead(newReadState(), ISBN, 0);
|
||||
assert.equal(r.emit, ISBN);
|
||||
assert.equal(r.reject, false);
|
||||
});
|
||||
|
||||
test('a gap between reads resets the streak', () => {
|
||||
test('the same ISBN is not repeated within 5 s, then is again', () => {
|
||||
let r = acceptRead(newReadState(), ISBN, 0);
|
||||
const s = endRound(r.state, []);
|
||||
r = acceptRead(s, ISBN, 150);
|
||||
r = acceptRead(r.state, ISBN, 150);
|
||||
assert.equal(r.emit, null);
|
||||
assert.equal(r.reject, false);
|
||||
r = acceptRead(r.state, ISBN, 4900);
|
||||
assert.equal(r.emit, null);
|
||||
r = acceptRead(r.state, ISBN, 5100);
|
||||
assert.equal(r.emit, ISBN);
|
||||
});
|
||||
|
||||
test('third read does not emit again; emits again after 5 s', () => {
|
||||
let s = newReadState();
|
||||
let t = 0;
|
||||
const read = () => { const r = acceptRead(s, ISBN, t); s = endRound(r.state, [ISBN]); t += 150; return r; };
|
||||
read();
|
||||
assert.equal(read().emit, ISBN);
|
||||
assert.equal(read().emit, null);
|
||||
t = 5200;
|
||||
assert.equal(read().emit, ISBN);
|
||||
test('a 979 ISBN is accepted', () => {
|
||||
assert.equal(acceptRead(newReadState(), '9791034305261', 0).emit, '9791034305261');
|
||||
});
|
||||
|
||||
test('a UPC rejects once per 5 s', () => {
|
||||
test('a UPC-A rejects once per 5 s', () => {
|
||||
const upc = '036000291452';
|
||||
let s = newReadState();
|
||||
let t = 0;
|
||||
const read = () => { const r = acceptRead(s, upc, t); s = endRound(r.state, [upc]); t += 150; return r; };
|
||||
assert.equal(read().reject, false);
|
||||
const second = read();
|
||||
assert.equal(second.reject, true);
|
||||
assert.equal(second.emit, null);
|
||||
assert.equal(read().reject, false);
|
||||
t = 6000;
|
||||
assert.equal(read().reject, true);
|
||||
});
|
||||
|
||||
test('bad check digit rejects', () => {
|
||||
let s = newReadState();
|
||||
let r = acceptRead(s, '9780306406158', 0); s = endRound(r.state, ['9780306406158']);
|
||||
r = acceptRead(s, '9780306406158', 150);
|
||||
let r = acceptRead(newReadState(), upc, 0);
|
||||
assert.equal(r.reject, true);
|
||||
assert.equal(r.emit, null);
|
||||
r = acceptRead(r.state, upc, 150);
|
||||
assert.equal(r.reject, false);
|
||||
r = acceptRead(r.state, upc, 6000);
|
||||
assert.equal(r.reject, true);
|
||||
});
|
||||
|
||||
test('a 13-digit code without a 978/979 prefix, or with a bad check digit, rejects', () => {
|
||||
assert.equal(acceptRead(newReadState(), '5012345678900', 0).reject, true);
|
||||
assert.equal(acceptRead(newReadState(), '9780306406158', 0).reject, true);
|
||||
});
|
||||
|
||||
test('markEmitted silences the loop for 5 s', () => {
|
||||
const s = markEmitted(newReadState(), ISBN, 1000);
|
||||
assert.equal(acceptRead(s, ISBN, 2000).emit, null);
|
||||
assert.equal(acceptRead(s, ISBN, 6100).emit, ISBN);
|
||||
});
|
||||
Reference in new issue
Block a user