202 lines
12 KiB
JavaScript
202 lines
12 KiB
JavaScript
const assert = require('node:assert/strict')
|
|
const { chromium } = require('./vue3-test-runtime.cjs')
|
|
const base = process.env.VUE3_URL || 'http://127.0.0.1:8088'
|
|
|
|
// Real Vue router/components and login. All business writes are intercepted;
|
|
// purchase and loan fixtures never reach the database.
|
|
;(async () => {
|
|
const browser = await chromium.launch({ headless: true })
|
|
try {
|
|
const context = await browser.newContext({ viewport: { width: 1440, height: 900 } })
|
|
const page = await context.newPage()
|
|
page.setDefaultTimeout(15000)
|
|
const errors = [], writes = [], sheetReads = [], loanQueries = [], batchQueries = []
|
|
page.on('pageerror', error => errors.push(error.message))
|
|
await page.goto(base + '/login?redirect=/procurement/details')
|
|
await page.getByPlaceholder('账号').fill(process.env.TEST_USERNAME || 'admin')
|
|
await page.getByPlaceholder('密码', { exact: true }).fill(process.env.TEST_PASSWORD || 'admin123')
|
|
await page.waitForFunction(() => !document.querySelector('input[placeholder="验证码"]'))
|
|
await page.getByRole('button', { name: /登\s*录/ }).click()
|
|
await page.locator('.purchase-sheet').waitFor()
|
|
await page.waitForFunction(() => !document.querySelector('.purchase-sheet').__vueParentComponent.proxy.loading)
|
|
|
|
const fixture = id => ({
|
|
batch: { id, batchNo: `AUDIT-${id}`, businessDate: '2026-10-13' }, version: `v-${id}`,
|
|
rows: [{ id: id + 10, projectId: 9901, expenseCategoryId: 9902, productId: 9903,
|
|
materialName: '采购导航回归物品', materialBrand: '测试', materialParams: 'R1', unit: '个', category: '材料',
|
|
quantity: 1, finalUnitPrice: 2, purchaseStatus: 0, priceLocked: false, purchasePurpose: 'SELF' }]
|
|
})
|
|
let releaseSave
|
|
const saveGate = new Promise(resolve => { releaseSave = resolve })
|
|
await context.route('**/dev-api/**', async route => {
|
|
const request = route.request(), url = new URL(request.url())
|
|
if (/^\/dev-api\/warehouse\/purchase\/sheet\/\d+$/.test(url.pathname)) {
|
|
const id = Number(url.pathname.split('/').pop()); sheetReads.push(id)
|
|
return route.fulfill({ json: { code: 200, data: fixture(id) } })
|
|
}
|
|
if (url.pathname === '/dev-api/warehouse/purchase/sheet' && request.method() === 'POST') {
|
|
const data = request.postDataJSON(); writes.push(data)
|
|
await saveGate
|
|
return route.fulfill({ json: { code: 200, data: { ...data, version: 'saved-in-memory' } } })
|
|
}
|
|
if (url.pathname === '/dev-api/project/loans/options') {
|
|
const params = Object.fromEntries(url.searchParams); loanQueries.push(params)
|
|
const data = Array.from({ length: 125 }, (_, index) => ({
|
|
id: index + 1, loanNo: `JK${index + 1}`, borrowerProjectId: Number(params.projectId),
|
|
itemId: Number(params.itemId), active: true, outstandingQuantity: 1
|
|
}))
|
|
data.push({ id: 999, borrowerProjectId: 9898, itemId: 9903, active: true, outstandingQuantity: 1 })
|
|
return route.fulfill({ json: { code: 200, data } })
|
|
}
|
|
if (url.pathname === '/dev-api/warehouse/purchase/batch/list') {
|
|
batchQueries.push(Object.fromEntries(url.searchParams))
|
|
return route.fulfill({ json: { code: 200, rows: [], total: 0 } })
|
|
}
|
|
if (!['GET', 'HEAD'].includes(request.method())) return route.abort('blockedbyclient')
|
|
return route.continue()
|
|
})
|
|
async function navigate(target, method = 'push') {
|
|
await page.evaluate(({ target, method }) => {
|
|
const router = document.querySelector('#app').__vue_app__.config.globalProperties.$router
|
|
window.__purchaseNavigation = router[method](target)
|
|
}, { target, method })
|
|
}
|
|
async function settled(id) {
|
|
await page.waitForFunction(id => {
|
|
const vm = document.querySelector('.purchase-sheet')?.__vueParentComponent.proxy
|
|
return !vm?.loading && vm?.sheet?.batch.id === id
|
|
}, id)
|
|
}
|
|
async function editQuantity(value) {
|
|
await page.getByRole('button', { name: '编辑', exact: true }).click()
|
|
await page.locator('.sheet-cell[data-field="quantity"]').first().dblclick()
|
|
const input = page.locator('.sheet-cell[data-field="quantity"] .el-input-number input').first()
|
|
await input.fill(String(value)); await input.blur()
|
|
}
|
|
const batchA = '/procurement/details?purchaseBatchId=991001'
|
|
const batchB = '/procurement/details?purchaseBatchId=991002'
|
|
await navigate(batchA); await settled(991001)
|
|
await editQuantity(11)
|
|
await navigate(batchB)
|
|
const confirmation = page.locator('.el-message-box:visible')
|
|
await confirmation.waitFor()
|
|
assert.equal(new URL(page.url()).searchParams.get('purchaseBatchId'), '991001')
|
|
assert.equal(sheetReads.filter(id => id === 991002).length, 0)
|
|
await confirmation.getByRole('button', { name: '取消', exact: true }).click()
|
|
await page.evaluate(() => window.__purchaseNavigation)
|
|
await confirmation.waitFor({ state: 'hidden' })
|
|
assert.equal(new URL(page.url()).searchParams.get('purchaseBatchId'), '991001')
|
|
assert.equal(Number(await page.locator('.sheet-cell[data-field="quantity"] .el-input-number input').first().inputValue()), 11)
|
|
console.log('PASS cancelled query navigation keeps URL, sheet and dirty values together')
|
|
|
|
await navigate(batchB)
|
|
await confirmation.getByRole('button', { name: '确定', exact: true }).click()
|
|
await settled(991002)
|
|
await confirmation.waitFor({ state: 'hidden' })
|
|
assert.equal(await confirmation.count(), 0)
|
|
assert.equal(sheetReads.filter(id => id === 991002).length, 1)
|
|
assert.equal(await page.evaluate(async () => {
|
|
const key = Object.keys(localStorage).find(key => key.startsWith('purchase-sheet-draft:') && key.endsWith(':991001'))
|
|
const {readSheetDraft}=await import('/src/utils/purchase-sheet-editor.js')
|
|
return readSheetDraft(key).sheet.rows[0].quantity
|
|
}), 11)
|
|
console.log('PASS accepted query navigation prompts once and preserves the previous draft')
|
|
|
|
await editQuantity(12)
|
|
const readsBeforeHash = sheetReads.length
|
|
await navigate(batchB + '&panel=quote#details', 'replace')
|
|
await page.evaluate(() => window.__purchaseNavigation)
|
|
assert.equal(await confirmation.count(), 0)
|
|
assert.equal(sheetReads.length, readsBeforeHash)
|
|
assert.equal(Number(await page.locator('.sheet-cell[data-field="quantity"] .el-input-number input').first().inputValue()), 12)
|
|
console.log('PASS unrelated query/hash updates preserve editing without reloading')
|
|
|
|
await page.getByRole('button', { name: '保存清单', exact: true }).click()
|
|
await page.waitForFunction(() => document.querySelector('.purchase-sheet').__vueParentComponent.proxy.saving)
|
|
await navigate(batchA)
|
|
await page.evaluate(() => window.__purchaseNavigation)
|
|
assert.equal(new URL(page.url()).searchParams.get('purchaseBatchId'), '991002')
|
|
releaseSave()
|
|
await page.waitForFunction(() => {
|
|
const vm = document.querySelector('.purchase-sheet').__vueParentComponent.proxy
|
|
return !vm.saving && vm.sheet.version === 'saved-in-memory'
|
|
})
|
|
assert.equal(writes.length, 1)
|
|
assert.equal(writes[0].rows[0].quantity, 12)
|
|
await navigate(batchA, 'replace'); await settled(991001)
|
|
assert.equal(await confirmation.count(), 0)
|
|
console.log('PASS pending save blocks batch navigation; saved sheet navigates without a prompt')
|
|
|
|
const loanResult = await page.evaluate(async () => {
|
|
const vm = document.querySelector('.purchase-sheet').__vueParentComponent.proxy
|
|
const row = vm.sheet.rows[0]
|
|
row.purchasePurpose = 'REPAY'; row.loanId = 125
|
|
await vm.loadLoans(row)
|
|
const result = { count: vm.loans[row._key].length, last: vm.loans[row._key].at(-1).id, selected: row.loanId }
|
|
row.productId = null
|
|
await vm.loadLoans(row)
|
|
result.cleared = vm.loans[row._key].length === 0 && row.loanId === null
|
|
// Reset this simulated selection only; no save is performed.
|
|
vm.sheet = JSON.parse(vm.baseline)
|
|
return result
|
|
})
|
|
assert.deepEqual(loanResult, { count: 125, last: 125, selected: 125, cleared: true })
|
|
assert.deepEqual(loanQueries, [{ projectId: '9901', itemId: '9903' }])
|
|
console.log('PASS all 125 matching loans are selectable; correct itemId and no pagination cutoff')
|
|
|
|
const readsBeforeReturn = sheetReads.length
|
|
await page.evaluate(() => { document.querySelector('.purchase-sheet').__vueParentComponent.proxy.rowFilter = '采购导航' })
|
|
await navigate('/index'); await page.locator('.business-home').waitFor()
|
|
await navigate(batchA); await settled(991001)
|
|
await page.waitForFunction(() => !document.querySelector('.purchase-sheet').__vueParentComponent.proxy.loading)
|
|
assert.equal(sheetReads.length, readsBeforeReturn + 1)
|
|
assert.equal(await page.evaluate(() => document.querySelector('.purchase-sheet').__vueParentComponent.proxy.rowFilter), '采购导航')
|
|
await editQuantity(13)
|
|
await navigate('/index')
|
|
await confirmation.getByRole('button', { name: '确定', exact: true }).click()
|
|
await page.locator('.business-home').waitFor()
|
|
await confirmation.waitFor({ state: 'hidden' })
|
|
const readsBeforeDirtyReturn = sheetReads.length
|
|
await navigate(batchA); await settled(991001)
|
|
assert.equal(await confirmation.count(), 0)
|
|
assert.equal(sheetReads.length, readsBeforeDirtyReturn)
|
|
assert.equal(Number(await page.locator('.sheet-cell[data-field="quantity"] .el-input-number input').first().inputValue()), 13)
|
|
console.log('PASS returning to a clean sheet refreshes with its filter; an edited sheet is preserved')
|
|
|
|
await page.evaluate(() => {
|
|
const vm = document.querySelector('.purchase-sheet').__vueParentComponent.proxy
|
|
vm.sheet = JSON.parse(vm.baseline); vm.editing = false
|
|
})
|
|
await navigate('/procurement/details')
|
|
await page.waitForFunction(() => {
|
|
const vm = document.querySelector('.purchase-sheet').__vueParentComponent.proxy
|
|
return !vm.loading && !vm.sheet
|
|
})
|
|
await page.evaluate(() => {
|
|
const vm = document.querySelector('.purchase-sheet').__vueParentComponent.proxy
|
|
Object.assign(vm.query, { keyword: '批次筛选', materialName: '保留物品筛选', pageNum: 3, supplierId: 9904 })
|
|
})
|
|
const batchReadsBeforeReturn = batchQueries.length
|
|
await navigate('/index'); await page.locator('.business-home').waitFor()
|
|
await Promise.all([page.waitForResponse(r=>r.url().includes('/warehouse/purchase/batch/list')&&new URL(r.url()).searchParams.get('keyword')==='批次筛选'),navigate('/procurement/details')]); await page.locator('.purchase-sheet').waitFor()
|
|
await page.waitForFunction(() => !document.querySelector('.purchase-sheet').__vueParentComponent.proxy.loading)
|
|
assert.equal(batchQueries.length, batchReadsBeforeReturn + 1)
|
|
assert.equal(batchQueries.at(-1).keyword, '批次筛选')
|
|
assert.equal(batchQueries.at(-1).materialName, '保留物品筛选')
|
|
assert.equal(batchQueries.at(-1).supplierId, '9904')
|
|
assert.equal(batchQueries.at(-1).pageNum, '3')
|
|
console.log('PASS returning to the batch list refreshes without losing filters or pagination')
|
|
|
|
const exportArgs = await page.evaluate(async () => {
|
|
const { default: ProjectLoans } = await import('/src/components/ProjectLoans/index.vue')
|
|
let args
|
|
ProjectLoans.methods.exportLoans.call({ query: { projectId: 7, itemId: 9, outstandingOnly: true, pageNum: 3, pageSize: 20 }, download: (...value) => { args = value } })
|
|
return args.slice(0, 2)
|
|
})
|
|
assert.deepEqual(exportArgs, ['project/loans/export', { projectId: 7, itemId: 9, outstandingOnly: true }])
|
|
assert.deepEqual(errors, [])
|
|
console.log('PASS loan export preserves the list filters, without the current-page limit')
|
|
console.log('PROCUREMENT GUARDS REGRESSION PASS (no business data written)')
|
|
} finally { await browser.close() }
|
|
})().catch(error => { console.error(error); process.exitCode = 1 })
|