/** 使用隔离模拟接口验证真实页面的批量分类交互。 */ import assert from 'node:assert/strict' import { createRequire } from 'node:module' const require = createRequire(import.meta.url) const { chromium } = require(process.env.PLAYWRIGHT_MODULE || 'C:/Users/Administrator/.cache/codex-runtimes/codex-primary-runtime/dependencies/node/node_modules/playwright') const base = process.env.OPS_UI_URL || 'http://127.0.0.1:5173' const calls = [] const state = { customers: null, products: null, owner: null } const browser = await chromium.launch({ headless: true, channel: 'msedge' }) try { const page = await browser.newPage({ viewport: { width: 1440, height: 900 } }) const errors = [] page.on('pageerror', error => errors.push(error.message)) await page.addInitScript(() => localStorage.setItem('ys_token', 'synthetic-batch-ui')) await page.route(url => url.pathname.startsWith('/api/'), async route => { const req = route.request(), url = new URL(req.url()), path = url.pathname.replace('/api/v1', '') let data = [] if (path === '/auth/me') data = { id: 1, username: 'ui-test', roles: ['admin'], permissions: ['base:customers:view', 'base:customers:update', 'base:customers:ownership', 'base:products:view', 'base:products:update'] } else if (path.endsWith('/batch-ownership')) { const payload = req.postDataJSON(); calls.push({kind: 'ownership', ...payload}); state.owner = payload.new_owner_personnel_id; data = {updated: payload.ids.length} } else if (path.endsWith('/batch-category')) { const payload = req.postDataJSON(), kind = path.includes('customers') ? 'customers' : 'products' calls.push({kind, ...payload}); state[kind] = payload.category; data = { updated: payload.ids.length } } else if (path === '/base/customers') { data = {list: [{id: 11, company_name: '测试待分类客户', owner_name: '', customer_type: state.customers, owner_personnel_id: state.owner, owner_personnel_name: state.owner ? '测试归属人' : null, rating_score: null}], total: 1, pageNum: 1, pageSize: 20} } else if (path === '/base/products') { data = {list: [{id: 12, product_name: '测试待分类产品', spec_model: '', unit: '件', product_category: state.products, status: 1, stock_quantity: null, sale_price: null}], total: 1, pageNum: 1, pageSize: 20} } else if (path.includes('/meta/')) data = url.searchParams.get('resource') === 'personnel' ? [{id: 21, personnel_name: '测试归属人', status: 1}] : [] await route.fulfill({ json: { code: 200, message: 'success', data } }) }) for (const [kind, label, category, id] of [['customers', '测试待分类客户', '企业户', 11], ['products', '测试待分类产品', '市政水利', 12]]) { await page.goto(`${base}/base/${kind}`) const row = page.getByRole('row').filter({hasText: label}) await row.waitFor() await row.locator('label.el-checkbox').click() await page.locator('.toolbar .el-select').first().click() await page.getByRole('option', { name: category, exact: true }).click() await page.getByRole('button', {name: '批量补充分类(1)'}).click() await page.getByRole('dialog').getByRole('button', {name: '确定'}).click() await row.getByText(category, {exact: true}).waitFor() assert.equal(calls.at(-1).kind, kind) assert.deepEqual(calls.at(-1).ids, [id]) } await page.goto(`${base}/base/customers`) const customer = page.getByRole('row').filter({hasText: '测试待分类客户'}) await customer.waitFor() await customer.locator('label.el-checkbox').click() await page.locator('.toolbar .el-select').nth(1).click() await page.getByRole('option', {name: '测试归属人', exact: true}).click() await page.getByRole('button', {name: '批量补充归属(1)'}).click() await page.getByRole('dialog').getByRole('button', {name: '确定'}).click() await customer.getByText('测试归属人', {exact: true}).waitFor() assert.equal(calls.at(-1).kind, 'ownership') assert.deepEqual(calls.at(-1).ids, [11]) assert.equal(calls.at(-1).new_owner_personnel_id, 21) assert.deepEqual(errors, []) process.stdout.write('客户和产品批量分类及客户批量归属页面交互通过(模拟接口)。\n') } finally { await browser.close() }