const assert = require('node:assert/strict') const { chromium, frontend } = require('./vue3-test-runtime.cjs') const pause = ms => new Promise(resolve => setTimeout(resolve, ms)) ;(async () => { const browser = await chromium.launch() const errors = [], writes = [], blocked = [], arrivals = new Map(), withdrawals = new Map() let loseArrivalResponse = true, loseWithdrawalResponse = true const purchase = { id: 81001, materialName: '移动到货重试物品', projectName: '隔离项目', quantity: 10, remainingQuantity: 10, purchaseStatus: 1, priceLocked: true, finalUnitPrice: 5, unit: '件' } try { const context = await browser.newContext({ viewport: { width: 412, height: 900 } }) await context.route('**/dev-api/**', async route => { const request = route.request(), path = new URL(request.url()).pathname.replace('/dev-api', '') const reply = data => route.fulfill({ json: { code: 200, data } }) if (request.method() === 'GET') { if (path === '/captchaImage') return route.fulfill({ json: { code: 200, captchaEnabled: false } }) if (path === '/warehouse/purchase/list') return route.fulfill({ json: { code: 200, rows: [purchase], total: 1 } }) if (path === '/warehouse/purchase/81001') return reply(purchase) if (path === '/warehouse/purchase/arrival/81001') return reply([...arrivals.values()].map((data, index) => ({ ...data, id: 82001 + index, arrivalNo: 'DH-MOCK-' + index, acceptanceStatus: 0 }))) if (path === '/warehouse/purchase/81001/operations' || path === '/warehouse/suppliers' || path === '/project/info/category/options') return reply([]) blocked.push('GET ' + path); return route.abort('blockedbyclient') } const data = request.postDataJSON() writes.push({ path, data }) if (path === '/warehouse/purchase/arrival' && request.method() === 'POST') { await pause(120) assert.ok(data.requestKey, 'Mobile single arrival must submit a stable requestKey') const previous = arrivals.get(data.requestKey) if (previous && JSON.stringify(previous) !== JSON.stringify(data)) return route.fulfill({ json: { code: 500, msg: '重复到货内容不一致,请核对日期和备注' } }) if (!previous) arrivals.set(data.requestKey, data) if (loseArrivalResponse) { loseArrivalResponse = false; return route.abort('failed') } return reply(1) } if (path === '/warehouse/in/revoke/83001' && request.method() === 'PUT') { await pause(120) const previous = withdrawals.get(data.key) if (previous && JSON.stringify(previous) !== JSON.stringify(data)) return route.fulfill({ json: { code: 500, msg: '重复撤销内容不一致' } }) if (!previous) withdrawals.set(data.key, data) if (loseWithdrawalResponse) { loseWithdrawalResponse = false; return route.abort('failed') } return reply(1) } blocked.push(request.method() + ' ' + path); return route.abort('blockedbyclient') }) const page = await context.newPage(); page.setDefaultTimeout(12000) page.on('pageerror', error => errors.push(error.message)) await page.goto(frontend + '/login') await page.evaluate(async () => { const { createApp, h, ElementPlus, nextTick } = await import('/tests/browser-runtime.js') const { default: store } = await import('/src/store/index.js'), { default: useUser } = await import('/src/store/modules/user.js') const { default: business } = await import('/src/plugins/business.js') useUser(store).permissions = ['*:*:*']; document.querySelector('#app')?.remove(); window.messages = [] window.mountRetry = async (path, props = {}) => { window.retryApp?.unmount(); document.getElementById('retry-test')?.remove() const el = document.createElement('div'); el.id = 'retry-test'; document.body.append(el) const Component = (await import(path)).default const app = createApp({ render: () => h(Component, { ...props, ref: 'child' }) }) app.use(store); app.use(ElementPlus); business(app) Object.assign(app.config.globalProperties, { parseTime: () => '2026-10-14', $modal: { msgSuccess: text => window.messages.push(text), msgWarning: text => window.messages.push(text) } }) window.retryApp = app; const root = app.mount(el); await nextTick(); window.retryVm = root.$refs.child } }) await page.evaluate(() => window.mountRetry('/src/views/mobile/components/MobilePurchase.vue', { permissions: ['*:*:*'] })) await page.waitForFunction(() => window.retryVm.list.length === 1 && !window.retryVm.loading) await page.evaluate(purchase => window.retryVm.openArrival(purchase), purchase) await page.waitForFunction(() => !window.retryVm.arrivalLoading) await page.evaluate(() => { window.retryVm.arrivalForm.arrivalQuantity = 2; window.retryVm.arrivalForm.remark = '原送货备注' }) const arrivalKey = await page.evaluate(() => window.retryVm.arrivalForm.requestKey) await page.evaluate(() => { window.retryVm.submitArrival(); window.retryVm.submitArrival() }) await page.waitForFunction(() => !window.retryVm.arrivalSubmitting) assert.equal(writes.filter(r => r.path === '/warehouse/purchase/arrival').length, 1) assert.equal(await page.evaluate(() => window.retryVm.arrivalForm.requestKey), arrivalKey) assert.equal(await page.evaluate(() => window.retryVm.arrivalForm.arrivalQuantity), 2) assert.equal(arrivals.size, 1) await page.evaluate(() => window.retryVm.refreshArrival(81001)) assert.equal(await page.evaluate(() => window.retryVm.arrivalForm.requestKey), arrivalKey, 'Metadata refresh must not discard the retry key') await page.evaluate(() => { window.retryVm.arrivalForm.remark = '不同备注'; window.retryVm.submitArrival() }) await page.waitForFunction(() => !window.retryVm.arrivalSubmitting) assert.equal(await page.evaluate(() => window.retryVm.arrivalForm.requestKey), arrivalKey) await page.evaluate(() => { window.retryVm.arrivalForm.remark = '原送货备注'; window.retryVm.submitArrival() }) await page.waitForFunction(() => !window.retryVm.arrivalSubmitting && !window.retryVm.arrivalLoading && window.retryVm.arrivalForm.arrivalQuantity == null) assert.equal(arrivals.size, 1, 'Retry after lost response must not register a second arrival') assert.notEqual(await page.evaluate(() => window.retryVm.arrivalForm.requestKey), arrivalKey) await page.evaluate(() => { window.retryVm.arrivalForm.arrivalQuantity = 1; window.retryVm.submitArrival() }) await page.waitForFunction(() => !window.retryVm.arrivalSubmitting && !window.retryVm.arrivalLoading && window.retryVm.arrivalForm.arrivalQuantity == null) assert.equal(arrivals.size, 2) console.log('PASS mobile arrival: double-click guarded; lost response keeps key and fields; refresh preserves draft; changed retry rejected; confirmed success resets key') await page.evaluate(() => window.mountRetry('/src/components/ReceiptDialog/Withdraw.vue')) await page.evaluate(() => { window.retryVm.open({ id: 83001, parentInId: 83000, productName: '重试库存', inNum: 5 }); window.retryVm.quantity = 2; window.retryVm.reason = '误操作' }) const withdrawalKey = await page.evaluate(() => window.retryVm.key) await page.evaluate(() => { window.retryVm.save(); window.retryVm.save() }) await page.waitForFunction(() => !window.retryVm.saving) assert.equal(await page.evaluate(() => window.retryVm.visible), true) assert.equal(await page.evaluate(() => window.retryVm.key), withdrawalKey) await page.evaluate(() => { window.retryVm.reason = '不同原因'; window.retryVm.save() }) await page.waitForFunction(() => !window.retryVm.saving) assert.equal(await page.evaluate(() => window.retryVm.visible), true) await page.evaluate(() => { window.retryVm.reason = '误操作'; window.retryVm.save() }) await page.waitForFunction(() => !window.retryVm.saving && !window.retryVm.visible) assert.equal(withdrawals.size, 1) assert.equal(writes.filter(r => r.path === '/warehouse/in/revoke/83001').length, 3) console.log('PASS receipt withdrawal: lost response retry retains key, rejects changed reason, succeeds once without double inventory mutation') assert.deepEqual(blocked, []); assert.deepEqual(errors, []) console.log(`INBOUND RETRY PASS: ${writes.length} isolated mock writes; zero real writes, blocked requests or uncaught errors`) } finally { await browser.close() } })().catch(error => { console.error(error); process.exitCode = 1 })