Files

65 lines
3.0 KiB
JavaScript

import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import vm from 'node:vm'
import { test } from 'node:test'
import { computed, reactive, ref, toRefs, watch, nextTick } from 'vue'
// 执行真实页面的 setup 与提交方法;只替代网络和运行平台边界。
async function fillingPage(platform) {
const source = await readFile(new URL(platform === 'pc'
? '../src/views/business/filling/index.vue'
: '../../shierfeng-fastapi-app/src/pages/business/filling/index.vue', import.meta.url), 'utf8')
const script = source.match(/<script\b[^>]*>([\s\S]*?)<\/script>/)[1]
.replace(/^import[\s\S]*?from\s+['"][^'"]+['"];?\s*$/gm, '')
const submissions = []
let key = 0
const proxy = {
$auth: { hasPermi: () => true, hasPermiAnd: () => true },
$refs: { fillingRef: { validate: callback => callback(true), clearValidate() {} } },
$modal: { msgSuccess() {} },
resetForm() {},
}
const context = vm.createContext({
computed, reactive, ref, toRefs, watch,
getCurrentInstance: () => ({ proxy }),
useUserStore: () => ({ admin: true }), useTestBatchStore: () => ({ isTestMode: false }),
onShow() {}, createIdempotencyKey: () => `date-command-${++key}`,
listFilling: async () => ({ rows: [], total: 0 }),
addFilling: async payload => { submissions.push(payload); throw new Error('网络暂不可用') },
})
vm.runInContext(`${script}\nglobalThis.page = {form, scannedPallet, submitFilling, formError};`, context)
const page = context.page
const form = platform === 'pc' ? page.form.value : page.form
Object.assign(form, { qrToken: 'qr-1', warehouseId: 1, strainId: 1, batchNo: 'B-1', packageSpec: '标准包', initialPackages: 100 })
page.scannedPallet.value = { palletId: 1, capacityPackages: 100 }
await nextTick()
return { page, form, submissions }
}
for (const platform of ['pc', 'app']) {
test(`${platform} 装填提交保留来源日期,失败后重试复用幂等键`, async () => {
const { page, form, submissions } = await fillingPage(platform)
Object.assign(form, { productionDate: '2026-10-01', expiryDate: '2027-03-31' })
await nextTick()
page.submitFilling()
await new Promise(resolve => setImmediate(resolve))
assert.equal(submissions.length, 1)
assert.equal(submissions[0].productionDate, '2026-10-01')
assert.equal(submissions[0].expiryDate, '2027-03-31')
page.submitFilling()
await new Promise(resolve => setImmediate(resolve))
assert.equal(submissions[1].idempotencyKey, submissions[0].idempotencyKey)
})
test(`${platform} 装填日期缺失或倒置时不发送请求`, async () => {
const { page, form, submissions } = await fillingPage(platform)
for (const dates of [{ productionDate: '', expiryDate: '' }, { productionDate: '2026-10-02', expiryDate: '2026-10-01' }]) {
Object.assign(form, dates)
page.submitFilling()
await new Promise(resolve => setImmediate(resolve))
assert.equal(submissions.length, 0)
assert.ok(page.formError.value)
}
})
}