69 lines
4.0 KiB
JavaScript
69 lines
4.0 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import ts from 'typescript'
|
|
|
|
const barcodeSource = fs.readFileSync(new URL('../src/components/BarcodePrint.vue', import.meta.url), 'utf8')
|
|
const terminalSource = fs.readFileSync(new URL('../src/views/settings/components/PrintTerminalManagement.vue', import.meta.url), 'utf8')
|
|
|
|
const extract = (source, name, nextName, bindings = {}) => {
|
|
const start = source.indexOf(`const ${name} =`)
|
|
const end = source.indexOf(`const ${nextName} =`, start)
|
|
assert.ok(start >= 0 && end > start)
|
|
const executable = ts.transpileModule(source.slice(start, end), { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText
|
|
return new Function(...Object.keys(bindings), `${executable}; return ${name};`)(...Object.values(bindings))
|
|
}
|
|
|
|
test('online legacy and unspecified-protocol terminals remain ready without masking configuration failures', () => {
|
|
const ready = extract(terminalSource, 'isTerminalReady', 'getLabelProfileText')
|
|
for (const protocol_version of [undefined, 1, 2]) {
|
|
const terminal = { status: 'online', protocol_version, selected_printer_name: 'Label printer', readiness: { is_ready: true } }
|
|
assert.equal(ready(terminal), true)
|
|
assert.equal(ready({ ...terminal, status: 'disabled' }), false)
|
|
assert.equal(ready({ ...terminal, readiness: { is_ready: false } }), false)
|
|
assert.equal(ready({ ...terminal, readiness: undefined, label_profile: { configured: true } }), true)
|
|
assert.equal(ready({ ...terminal, readiness: undefined, selected_printer_name: '' }), false)
|
|
}
|
|
})
|
|
|
|
test('legacy remote-print status stays available and upgrade advice does not promise waiting for an update', () => {
|
|
for (const protocol_version of [undefined, 1, 2]) {
|
|
const defaultTerminal = { value: { id: 7, name: 'Office', status: 'online', protocol_version, readiness: { is_ready: true } } }
|
|
const bindings = { computed: (callback) => ({ value: callback() }), defaultTerminal, terminalLoading: { value: false }, terminalError: { value: null }, remotePrinterName: { value: 'Label printer' } }
|
|
const status = extract(barcodeSource, 'remoteStatusText', 'remotePrinterName', bindings)
|
|
const description = extract(barcodeSource, 'remoteStatusDescription', 'remoteActionDisabled', bindings)
|
|
const disabled = extract(barcodeSource, 'remoteActionDisabled', 'getJobStatusText', { ...bindings, loading: { value: false }, remoteSubmitting: { value: false } })
|
|
assert.equal(status.value, '可直接打印')
|
|
assert.equal(disabled.value, false)
|
|
assert.doesNotMatch(description.value, /不能接单|等待兼容|需要升级/)
|
|
if (protocol_version !== 2) assert.match(description.value, /旧版助手可正常打印.*建议升级/)
|
|
}
|
|
})
|
|
|
|
test('legacy and unspecified-protocol terminals can submit a real label job through the existing handler', async () => {
|
|
for (const protocol_version of [undefined, 1]) {
|
|
const submissions = []
|
|
const notices = []
|
|
const latestRemoteJob = { value: null }
|
|
const remoteSubmitting = { value: false }
|
|
const print = extract(barcodeSource, 'handleRemotePrint', 'handleCancelRemoteJob', {
|
|
props: { equipmentIds: [11] }, equipmentList: { value: [{ id: 11 }] },
|
|
terminalLoading: { value: false }, defaultTerminal: { value: { id: 7, name: 'Office', status: 'online', protocol_version } },
|
|
remoteSubmitting, latestRemoteJob, alert: (message) => notices.push(message),
|
|
ensureImagesGenerated: async () => ['data:image/png;base64,YQ=='],
|
|
createAssetLabelPrintJob: async (payload) => {
|
|
submissions.push(payload)
|
|
return { job: { id: 33, status: 'queued', target_terminal_name: 'Office' } }
|
|
},
|
|
fetchDefaultTerminal: async () => {}
|
|
})
|
|
await print()
|
|
assert.equal(submissions.length, 1)
|
|
assert.equal(submissions[0].target_terminal_id, 7)
|
|
assert.deepEqual(submissions[0].equipment_ids, [11])
|
|
assert.equal(latestRemoteJob.value.id, 33)
|
|
assert.equal(remoteSubmitting.value, false)
|
|
assert.match(notices[0], /已发送/)
|
|
}
|
|
})
|