const assert = require('node:assert/strict') const { chromium, frontend, backend, username, password } = require('./vue3-test-runtime.cjs') ;(async () => { const login = await (await fetch(backend + '/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) })).json() assert.equal(login.code, 200) const browser = await chromium.launch() try { const context = await browser.newContext({ viewport: { width: 1440, height: 900 } }) await context.addCookies([{ name: 'Admin-Token', value: login.token, url: frontend }]) const page = await context.newPage(), errors = [], writes = [] page.on('pageerror', e => errors.push(e.message)) let failSave = true, failList = false await page.route('**/dev-api/**', route => { const req = route.request(), path = new URL(req.url()).pathname if (path === '/dev-api/project/info/budget') { writes.push(req.postDataJSON()) return route.fulfill({ json: failSave ? { code: 500, msg: '模拟保存失败,保留输入' } : { code: 200 } }) } if (!['GET', 'HEAD'].includes(req.method())) return route.abort('blockedbyclient') if (path === '/dev-api/project/info/990900') return route.fulfill({ json: { code: 200, data: { id: 990900, projectName: '预算回归项目', projectType: 'PRODUCT', totalAmount: 10000, projectStatus: 1 } } }) if (path.includes('/overview/990900')) return route.fulfill({ json: { code: 200, data: {} } }) if (path === '/dev-api/project/info/990900/budgets') return route.fulfill({ json: failList ? { code: 500, msg: '模拟列表失败' } : { code: 200, data: [] } }) if (path === '/dev-api/project/info/category/options') return route.fulfill({ json: { code: 200, data: [{ id: 1, categoryName: '材料费' }] } }) return route.continue() }) await page.goto(frontend + '/project/detail/990900') await page.getByRole('tab', { name: '分类预算', exact: true }).click() await page.getByRole('button', { name: '新增', exact: true }).click() const dialog = page.getByRole('dialog', { name: '分类预算', exact: true }) await dialog.getByRole('button', { name: '取消', exact: true }).click() await dialog.waitFor({ state: 'hidden' }) assert.equal(await page.locator('.el-message-box:visible').count(), 0) await page.getByRole('button', { name: '新增', exact: true }).click() await dialog.locator('.el-select').click() await page.getByRole('option', { name: '材料费', exact: true }).click() const amount = dialog.locator('.el-input-number input'), remark = dialog.locator('.el-form-item').filter({ hasText: '备注' }).locator('input') await amount.fill('123.45'); await amount.blur(); await remark.fill('不能丢失的预算说明') await dialog.getByRole('button', { name: '取消', exact: true }).click() await page.locator('.el-message-box').getByRole('button', { name: '继续编辑', exact: true }).click() assert.equal(await remark.inputValue(), '不能丢失的预算说明') await dialog.getByRole('button', { name: '保存', exact: true }).click() await page.getByText('模拟保存失败,保留输入', { exact: true }).waitFor() await page.waitForFunction(() => !document.querySelector('[aria-label="分类预算"] .is-loading')) assert.equal(await remark.inputValue(), '不能丢失的预算说明') assert.equal(Number(await amount.inputValue()), 123.45) assert.equal(writes.length, 1) assert.equal(writes[0].projectId, 990900) assert.equal(writes[0].expenseCategoryId, 1) failSave = false await page.waitForTimeout(1100) await dialog.getByRole('button', { name: '保存', exact: true }).click() await dialog.waitFor({ state: 'hidden' }) assert.equal(writes.length, 2) failList = true await page.getByRole('tab', { name: '总览', exact: true }).click() await page.getByRole('tab', { name: '分类预算', exact: true }).click() await page.locator('[role="alert"]').filter({ hasText: '模拟列表失败' }).getByRole('button', { name: '重试' }).waitFor() failList = false await page.locator('[role="alert"]').filter({ hasText: '模拟列表失败' }).getByRole('button', { name: '重试' }).click() await page.locator('.business-embedded-panel [role="alert"]').waitFor({ state: 'hidden' }) assert.deepEqual(errors, []) console.log('PASS budget unchanged-close, dirty-cancel, failed-save retention/retry, amount/category payload, list retry (all business writes mocked)') } finally { await browser.close() } })().catch(error => { console.error(error); process.exitCode = 1 })