Viewing is open to everyone. Downloading and starring need a GitHub sign-in.
Sign in with GitHub
scanner.test.mjs
v0.1.0-build.85
Latest
Commit ede38f5
Sign in with GitHub to download. Each download is recorded with your GitHub username.
| 1 | // Pure-JS tests: scanner session logic with a fake decoder and recorder. |
| 2 | import test from 'node:test'; |
| 3 | import assert from 'node:assert/strict'; |
| 4 | import { makeScanner } from '../js/scanner.mjs'; |
| 5 | |
| 6 | function setup(decodeAt) { |
| 7 | const log = []; |
| 8 | let clock = 0; |
| 9 | let frames = 0; |
| 10 | const sender = { armed: 0, soon: 0, arm() { this.armed++; }, trySendSoon() { this.soon++; } }; |
| 11 | const deps = { |
| 12 | decode: () => { |
| 13 | clock += 5; |
| 14 | return ++frames === decodeAt ? { bytes: Uint8Array.of(1), text: null, profile: '0', version: 3, layout: 'standard', profileCode: 0 } : null; |
| 15 | }, |
| 16 | telemetry: { |
| 17 | begin: (t) => log.push(['begin', t]), |
| 18 | frame: (t, ms) => log.push(['frame', t, ms]), |
| 19 | success: (t, code) => log.push(['success', t, code]), |
| 20 | end: (t) => log.push(['end', t]), |
| 21 | }, |
| 22 | now: () => (clock += 1), |
| 23 | sender, |
| 24 | }; |
| 25 | return { log, deps, sender }; |
| 26 | } |
| 27 | |
| 28 | test('begin on first frame, success on decode, end on stop', () => { |
| 29 | const { log, deps, sender } = setup(2); |
| 30 | const results = []; |
| 31 | const s = makeScanner(deps, { onResult: (r) => results.push(r) }); |
| 32 | assert.equal(s.processFrame({}), null); |
| 33 | const r = s.processFrame({}); |
| 34 | assert.equal(r.profile, '0'); |
| 35 | assert.equal('profileCode' in r, false); |
| 36 | assert.equal(results.length, 1); |
| 37 | assert.equal(s.processFrame({}), null, 'frames after the decode are skipped'); |
| 38 | s.stop(); |
| 39 | assert.deepEqual(log.map((e) => e[0]), ['begin', 'frame', 'frame', 'success', 'end']); |
| 40 | assert.equal(log[1][2], 6, 'processing time is measured around decode'); |
| 41 | assert.equal(log[3][2], 0, 'profile code passed to the core'); |
| 42 | assert.equal(sender.armed, 1); |
| 43 | assert.equal(sender.soon, 1); |
| 44 | }); |
| 45 | |
| 46 | test('reset begins a new session; stop without decode ends it', () => { |
| 47 | const { log, deps } = setup(1); |
| 48 | const s = makeScanner(deps); |
| 49 | assert.ok(s.processFrame({})); |
| 50 | assert.ok(s.decoded); |
| 51 | s.reset(); |
| 52 | assert.equal(s.decoded, null); |
| 53 | s.processFrame({}); |
| 54 | s.stop(); |
| 55 | s.stop(); |
| 56 | assert.deepEqual(log.map((e) => e[0]), ['begin', 'frame', 'success', 'begin', 'frame', 'end']); |
| 57 | }); |
| 58 | |
| 59 | test('decoder errors are counted as frames and reported', () => { |
| 60 | const { log, deps } = setup(0); |
| 61 | deps.decode = () => { throw new TypeError('bad frame'); }; |
| 62 | const errors = []; |
| 63 | const s = makeScanner(deps, { onError: (e) => errors.push(e) }); |
| 64 | assert.equal(s.processFrame({}), null); |
| 65 | assert.equal(errors.length, 1); |
| 66 | assert.deepEqual(log.map((e) => e[0]), ['begin', 'frame']); |
| 67 | const t = makeScanner(deps); |
| 68 | assert.throws(() => t.processFrame({}), TypeError); |
| 69 | }); |
| 70 | |
| 71 | test('telemetry failures never break scanning', () => { |
| 72 | const { deps } = setup(1); |
| 73 | deps.telemetry = { begin() { throw new Error('x'); }, frame() { throw new Error('x'); }, success() { throw new Error('x'); }, end() { throw new Error('x'); } }; |
| 74 | const s = makeScanner(deps); |
| 75 | assert.ok(s.processFrame({})); |
| 76 | assert.doesNotThrow(() => s.stop()); |
| 77 | }); |