import assert from 'node:assert/strict' import { readFile } from 'node:fs/promises' import vm from 'node:vm' import { test } from 'node:test' import { nextTick, ref, watch } from 'vue' import { isDuplicateScan, normalizeScanValue } from '../src/utils/palletScan.js' test('共享扫码输入允许输入短托盘号且仍拒绝空值', async () => { const source = await readFile(new URL('../src/components/PalletScanInput/index.vue', import.meta.url), 'utf8') const script = source.match(/]*>([\s\S]*?)<\/script>/)[1] .replace(/^import[\s\S]*?from\s+['"][^'"]+['"];?\s*$/gm, '') const events = [] const context = vm.createContext({ nextTick, ref, watch, onMounted() {}, normalizeScanValue, isDuplicateScan, defineProps: definitions => Object.fromEntries(Object.entries(definitions).map(([key, value]) => [key, value.default])), defineEmits: () => (...event) => events.push(event), }) vm.runInContext(`${script}\nglobalThis.page = {scanValue, handleSubmit}`, context) context.page.scanValue.value = ' 123 ' context.page.handleSubmit() assert.deepEqual(events[0], ['scan', '123']) context.page.scanValue.value = ' ' context.page.handleSubmit() assert.equal(events[1][0], 'invalid') })