83 lines
2.8 KiB
JavaScript
83 lines
2.8 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_ACTIONS,
|
|
SCAN_BROADCAST_EXTRA,
|
|
isDuplicateScan,
|
|
normalizeScanValue,
|
|
resetScanDeduplication,
|
|
startScanBroadcast,
|
|
stopScanBroadcast
|
|
} 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.deepEqual(SCAN_BROADCAST_ACTIONS, [
|
|
'android.intent.action.SCANRESULT',
|
|
'com.android.server.scannerservice.broadcast',
|
|
'com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED'
|
|
])
|
|
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, /registeredActivity\.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)
|
|
})
|
|
|
|
test('an old page disposer cannot unregister the receiver owned by the newly shown page', () => {
|
|
const registered = []
|
|
const unregistered = []
|
|
class IntentFilter { addAction(action) { this.action = action } }
|
|
const activity = {
|
|
registerReceiver(value) { registered.push(value) },
|
|
unregisterReceiver(value) { unregistered.push(value) },
|
|
getPackageName() { return 'com.baiyuelogistics.pda.overseas' }
|
|
}
|
|
globalThis.plus = { android: {
|
|
runtimeMainActivity: () => activity,
|
|
importClass(name) {
|
|
if (name === 'android.content.IntentFilter') return IntentFilter
|
|
if (name === 'android.os.Build') return { VERSION: { SDK_INT: 32 } }
|
|
return name
|
|
},
|
|
implements: (_name, implementation) => implementation
|
|
} }
|
|
try {
|
|
const stopFirst = startScanBroadcast({ onScan() {} })
|
|
const first = registered[0]
|
|
const stopSecond = startScanBroadcast({ onScan() {} })
|
|
const second = registered[1]
|
|
assert.deepEqual(unregistered, [first])
|
|
|
|
stopFirst()
|
|
assert.deepEqual(unregistered, [first, first])
|
|
assert.ok(!unregistered.includes(second))
|
|
|
|
stopSecond()
|
|
assert.equal(unregistered.at(-1), second)
|
|
} finally {
|
|
stopScanBroadcast()
|
|
delete globalThis.plus
|
|
}
|
|
})
|