Files
pda-overseas-uniapp/tests/relabel-active-task.test.mjs
T
2026-10-12 13:43:37 +08:00

60 lines
3.1 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
const source = readFileSync(new URL('../src/pages/relabel/relabel.vue', import.meta.url), 'utf8')
const body = source.split('<script>')[1].split('</script>')[0].replace(/^\s*import .*$/gm, '').replace('export default', 'return')
function setup(confirm) {
const calls = []
const uni = { showModal(options) { calls.push(options); options.success({ confirm }) }, redirectTo(options) { calls.push(options) } }
const component = new Function('PortalHeader', 'RelabelPalletWork', 'ScanTargetInput', 'ReturnRelabelPackWork', 'uni', body)({}, {}, {}, {}, uni)
const context = { busy: false, stage: 'receive', taskCode: 'PREVIOUS', error: null, candidates: [{ relabelNo: 'RL-ACTIVE', taskStatus: 'PROCESSING', claimedByCurrentOperator: true }] }
context.promptActiveTask = component.methods.promptActiveTask.bind(context)
return { calls, context, open: () => component.methods.openExecution.call(context, { relabelNo: 'RL-OTHER', taskStatus: 'AVAILABLE' }) }
}
test('conflicting task opens modal and confirm resumes owned task without claiming another', async () => {
const { calls, context, open } = setup(true)
await open()
assert.equal(calls[0].confirmText, '前往任务')
assert.equal(calls[0].cancelText, '取消')
assert.match(calls[0].content, /RL-ACTIVE/)
assert.equal(context.taskCode, 'RL-ACTIVE')
assert.equal(calls[1].url, '/pages/relabel/relabel?op=relabelExecute&task=RL-ACTIVE')
assert.equal(context.error, null)
assert.equal(context.busy, false)
})
test('cancel stays on list and preserves existing task selection', async () => {
const { calls, context, open } = setup(false)
await open()
assert.equal(calls.length, 1)
assert.equal(context.taskCode, 'PREVIOUS')
assert.equal(context.busy, false)
})
test('new relabel task asks whether to start before it can be claimed', async () => {
const calls = []
const uni = { showModal(options) { calls.push(options); options.success({ confirm: false }) } }
const component = new Function('PortalHeader', 'RelabelPalletWork', 'ScanTargetInput', 'ReturnRelabelPackWork', 'uni', body)({}, {}, {}, {}, uni)
const context = { busy: false }
const confirmed = await component.methods.confirmStartTask.call(context, { relabelNo: 'RL-NEW' })
assert.equal(confirmed, false)
assert.equal(calls[0].title, '开始贴标作业')
assert.match(calls[0].content, /是否开始贴标作业/)
assert.match(calls[0].content, /RL-NEW/)
assert.equal(calls[0].confirmText, '是')
assert.equal(calls[0].cancelText, '否')
assert.equal(context.busy, false)
})
test('start confirmation gates task selection and claim', () => {
const start = source.indexOf('const confirmed = await this.confirmStartTask(item)')
const selection = source.indexOf('this.taskCode = item.relabelNo', start)
const claim = source.indexOf("warehouseIdempotencyKey('relabel', 'claim', payload)", start)
assert.ok(start >= 0)
assert.ok(selection > start)
assert.ok(claim > selection)
assert.match(source.slice(start, selection), /if \(!confirmed\) return/)
})