Files
pda-overseas-uniapp/tests/scan-broadcast.test.mjs
T
2026-08-26 17:23:42 +08:00

38 lines
1.4 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import {
SCAN_BROADCAST_ACTION,
SCAN_BROADCAST_EXTRA,
isDuplicateScan,
normalizeScanValue,
resetScanDeduplication
} from '../src/utils/scan-broadcast.js'
test('scanner uses the confirmed PDA broadcast contract', () => {
assert.equal(SCAN_BROADCAST_ACTION, 'android.intent.action.SCANRESULT')
assert.equal(SCAN_BROADCAST_EXTRA, 'value')
assert.equal(normalizeScanValue(' EL-38201\r\n'), 'EL-38201')
assert.equal(normalizeScanValue(null), '')
})
test('scanner source keeps receiver diagnostics before page routing', async () => {
const source = await readFile(new URL('../src/utils/scan-broadcast.js', import.meta.url), 'utf8')
assert.match(source, /\[Overseas PDA broadcast\] received/)
assert.match(source, /rawValue[\s\S]*value/)
assert.match(source, /ignored duplicate/)
assert.match(source, /receive failed/)
assert.match(source, /receiver registered/)
assert.match(source, /registration failed/)
assert.match(source, /activity\.getPackageName\(\)/)
})
test('scanner drops only immediate duplicate broadcasts', () => {
resetScanDeduplication()
assert.equal(isDuplicateScan('EL-38201', 1000), false)
assert.equal(isDuplicateScan('EL-38201', 1050), true)
assert.equal(isDuplicateScan('EL-38201', 1100), false)
assert.equal(isDuplicateScan('EL-38202', 1110), false)
})