cmvr-wms/scripts/projectdev/vue3-production-check.cjs

92 lines
7.0 KiB
JavaScript
Raw Normal View History

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'
;(async () => {
const browser = await chromium.launch({ headless: true })
const writes = [], errors = []
try {
const context = await browser.newContext({ viewport: { width: 1440, height: 900 } })
const page = await context.newPage(); page.setDefaultTimeout(15000)
page.on('pageerror', error => errors.push(error.message))
const task = { id: 987654, task_no: 'TEST-BOM-PRESERVE', project_id: 2, project_name: '只读模拟项目', item_id: 3, item_name: '模拟产品', bom_id: 91, source_bom_id: 90, version_tag: 'V1', planned_quantity: 10, good_quantity: 0, scrap_quantity: 0, business_date: '2026-10-13', status: 'OPEN', version: 1, editable_identity: 1, material_recorded: 0, material_cost: 0, unit_cost: 0 }
await context.route('**/dev-api/**', async route => {
const request = route.request(), url = new URL(request.url()), pathname = url.pathname.replace('/dev-api', '')
const json = data => route.fulfill({ json: { code: 200, data } })
if (pathname === '/production/tasks/page') return json({ rows: [task], total: 1 })
if (pathname === '/production/bom/options') return json([{ id: 92, versionTag: 'V2', isDefault: true }])
if (pathname.startsWith('/production/product/project/')) return json([{ itemId: 3, bomId: 92 }])
if (pathname === '/production/tasks' && request.method() === 'POST') { writes.push(request.postDataJSON()); return json(task) }
if (!['GET', 'HEAD'].includes(request.method()) && pathname !== '/login') return route.abort('blockedbyclient')
return route.continue()
})
await page.goto(base + '/login?redirect=%2Fproduction%2Ftasks')
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('.production-workspace').waitFor()
await page.getByRole('button', { name: '修改', exact: true }).click()
const dialog = page.locator('.el-dialog:visible')
await page.waitForFunction(() => !document.querySelector('.production-workspace').__vueParentComponent.proxy.bomLoading)
const formBefore = await page.locator('.production-workspace').evaluate(el => ({ ...el.__vueParentComponent.proxy.form }))
assert.equal(formBefore.bomId, 91, 'Opening an editable task must retain its frozen BOM even when the project default changed')
assert.match(await dialog.innerText(), /V1(当前任务)/)
await dialog.locator('.el-form-item').filter({ hasText: '计划数量' }).locator('input').fill('12')
await dialog.getByRole('button', { name: '保存', exact: true }).click()
await dialog.waitFor({ state: 'hidden' })
assert.equal(writes.length, 1); assert.equal(writes[0].bomId, 91); assert.equal(writes[0].quantity, 12)
await page.getByRole('button', { name: '修改', exact: true }).click()
await page.waitForFunction(() => !document.querySelector('.production-workspace').__vueParentComponent.proxy.bomLoading)
await dialog.locator('.el-form-item').filter({ hasText: 'BOM版本' }).locator('.el-select').click()
await page.locator('.el-select-dropdown:visible').getByText('V2', { exact: true }).click()
await dialog.getByRole('button', { name: '保存', exact: true }).click()
await dialog.waitFor({ state: 'hidden' })
assert.equal(writes.length, 2); assert.equal(writes[1].bomId, 92)
console.log('PASS task edit preserves original BOM; explicit selection replaces it; both writes intercepted')
await page.locator('.production-workspace').evaluate(el => {
const vm = el.__vueParentComponent.proxy
vm.detail = { id: 987654, project_id: 2 }
vm.childTask({ itemId: 3, itemName: '多版本自制件', purchaseSuggestion: 10, childBomId: null,
childBomIds: [131, 132], childBomOptions: [{ id: 131, versionTag: '子件V1' }, { id: 132, versionTag: '子件V2' }] })
})
const childForm = await page.locator('.production-workspace').evaluate(el => ({ ...el.__vueParentComponent.proxy.form }))
assert.equal(childForm.bomId, undefined, 'Multiple task child versions require an explicit choice')
const childPicker = dialog.locator('.el-form-item').filter({ hasText: 'BOM版本' }).locator('.el-select')
assert.equal(await childPicker.locator('input').isDisabled(), false)
await dialog.getByRole('button', { name: '保存', exact: true }).click()
assert.equal(writes.length, 2, 'A child version is required before saving')
await childPicker.click()
await page.locator('.el-select-dropdown:visible').getByText('子件V2', { exact: true }).waitFor()
assert.equal(await page.locator('.el-select-dropdown:visible .el-select-dropdown__item').count(), 2)
await page.locator('.el-select-dropdown:visible').getByText('子件V2', { exact: true }).click()
await dialog.getByRole('button', { name: '保存', exact: true }).click()
await dialog.waitFor({ state: 'hidden' })
assert.equal(writes.length, 3); assert.equal(writes[2].bomId, 132); assert.equal(writes[2].parentTaskId, 987654)
await page.locator('.production-workspace').evaluate(el => {
const vm = el.__vueParentComponent.proxy
vm.childTask({ itemId: 3, itemName: '单版本自制件', purchaseSuggestion: 5, childBomId: 131 })
})
const singleBom = await page.locator('.production-workspace').evaluate(el => el.__vueParentComponent.proxy.form.bomId)
assert.equal(singleBom, 131)
await dialog.getByRole('button', { name: '取消', exact: true }).click()
console.log('PASS merged item keeps self-made entry; child version choices are task-scoped, required and explicit')
const costResults = await page.evaluate(async () => {
const { productionCostText: text } = await import('/src/utils/production-cost.js')
const known = { status: 'SETTLED', material_recorded: 1, material_cost: 0, unit_cost: 0, good_quantity: 1 }
const cases = [
{ ...known, material_recorded: 0 }, known,
{ ...known, material_cost: null }, { ...known, status: 'OPEN' },
{ ...known, material_cost: 20, good_quantity: 0 },
{ ...known, material_cost: null, cost_status: 'PENDING_MATERIAL', unit_cost_status: 'PENDING_MATERIAL' }
]
return cases.map(row => [text(row), text(row, 'unit_cost')])
})
assert.deepEqual(costResults, [['用料待核对', '用料待核对'], ['0.000000', '0.000000'], ['成本未知', '成本未知'], ['未结算', '未结算'], ['20.000000', '无合格产出'], ['用料待核对', '用料待核对']])
console.log('PASS old/new backend cost states: unrecorded, genuine zero, missing price, reopened, no output')
assert.deepEqual(errors, [])
console.log('PRODUCTION VUE3 PASS; no business data written')
} finally { await browser.close() }
})().catch(error => { console.error(error); process.exit(1) })