/** * 页面流程 API 集成测试(模拟各页面关键写操作) * 用法: node server/scripts/test-page-flows.js */ const { currentCycleCode } = require('./test-helpers.js'); const BASE = process.env.API_BASE || 'http://127.0.0.1:3001/api'; const results = []; function pass(name, detail = '') { results.push({ name, ok: true, detail }); console.log(` ✓ ${name}${detail ? ` — ${detail}` : ''}`); } function fail(name, detail = '') { results.push({ name, ok: false, detail }); console.log(` ✗ ${name}${detail ? ` — ${detail}` : ''}`); } async function jsonFetch(path, options = {}) { const res = await fetch(`${BASE}${path}`, { ...options, headers: { 'Content-Type': 'application/json', ...(options.headers || {}) }, }); const body = await res.json().catch(() => ({})); return { res, body }; } async function login() { const attempts = [ { email: 'superadmin@meoo.local', password: 'Superadmin@123' }, { email: 'superadmin@meoo.local', password: 'SuperAdmin@123' }, { username: 'superadmin', password: 'Superadmin@123' }, { username: 'superadmin', password: 'E2eTest@123' }, ]; for (const cred of attempts) { const { res, body } = await jsonFetch('/auth/token', { method: 'POST', body: JSON.stringify(cred), }); if (res.ok && body.access_token) return body.access_token; } throw new Error('login failed'); } async function query(token, spec) { const { res, body } = await jsonFetch('/data/query', { method: 'POST', headers: { Authorization: `Bearer ${token}` }, body: JSON.stringify(spec), }); return { ok: res.ok && !body.error, body }; } async function main() { console.log('\n=== 页面流程 API 测试 ===\n'); let token; try { token = await login(); pass('登录 superadmin'); } catch (e) { fail('登录 superadmin', e.message); process.exit(1); } // 1. 订单管理 - 新建订单流程 console.log('\n[1] 订单管理新建订单'); const orderCode = `ORD${Date.now()}`; const cycleCode = currentCycleCode(); const bidCode = `BID${Date.now()}`; const createOrder = await query(token, { table: 'orders', action: 'insert', insertData: { order_code: orderCode, bid_code: bidCode, cycle_code: cycleCode, customer_code: 'CUST001', order_status: 20, total_amount: 11, total_weight: 2, delivery_date: '2026-06-10', delivery_time_slot: 'morning', }, }); if (createOrder.ok) pass('创建订单 orders'); else fail('创建订单 orders', createOrder.body.error?.message); const createItems = await query(token, { table: 'order_items', action: 'insert', insertData: [ { order_code: orderCode, sku_code: 'SKU001', quantity: 2, unit: '斤', price: 5.5, amount: 11, item_status: 10, sort_order: 1, }, ], }); if (createItems.ok) pass('创建订单明细 order_items'); else fail('创建订单明细 order_items', createItems.body.error?.message); // 2. 订单分配流程 console.log('\n[2] 订单分配'); const suppliersQ = await query(token, { table: 'suppliers', action: 'select', select: 'supplier_code, company_name', filters: [{ op: 'in', column: 'status', value: [10, 20] }], }); if (suppliersQ.ok && suppliersQ.body.data?.length) pass('供应商列表', `${suppliersQ.body.data.length} 家`); else fail('供应商列表', suppliersQ.body.error?.message); const skuSupQ = await query(token, { table: 'sku_suppliers', action: 'select', select: '*', filters: [{ op: 'eq', column: 'status', value: 1 }], }); if (skuSupQ.ok && skuSupQ.body.data?.length) pass('SKU-供应商关系', `${skuSupQ.body.data.length} 条`); else fail('SKU-供应商关系', skuSupQ.body.error?.message); if (createItems.ok && skuSupQ.body.data?.length) { const rel = skuSupQ.body.data.find((r) => r.sku_code === 'SKU001') || skuSupQ.body.data[0]; const itemQ = await query(token, { table: 'order_items', action: 'select', select: 'id, sku_code, quantity', filters: [{ op: 'eq', column: 'order_code', value: orderCode }], }); const item = itemQ.body.data?.[0]; if (item) { const updItem = await query(token, { table: 'order_items', action: 'update', updateData: { supplier_code: rel.supplier_code, price: rel.supply_price, amount: Number(rel.supply_price) * Number(item.quantity), }, filters: [{ op: 'eq', column: 'id', value: item.id }], }); if (updItem.ok) pass('更新订单明细供应商'); else fail('更新订单明细供应商', updItem.body.error?.message); const prep = await query(token, { table: 'supplier_preparations', action: 'insert', insertData: { order_code: orderCode, supplier_code: rel.supplier_code, sku_code: item.sku_code, prepare_qty: item.quantity, purchase_price: rel.supply_price, prepare_status: 10, }, }); if (prep.ok) pass('创建备货记录 supplier_preparations'); else fail('创建备货记录 supplier_preparations', prep.body.error?.message); const taskCode = `AT${Date.now()}`; const task = await query(token, { table: 'allocation_tasks', action: 'insert', insertData: { task_code: taskCode, order_code: orderCode, sku_code: item.sku_code, supplier_code: rel.supplier_code, allocated_price: rel.supply_price, allocated_qty: item.quantity, task_status: 20, }, }); if (task.ok) pass('创建分配任务 allocation_tasks'); else fail('创建分配任务 allocation_tasks', task.body.error?.message); const orderUpd = await query(token, { table: 'orders', action: 'update', updateData: { order_status: 30 }, filters: [{ op: 'eq', column: 'order_code', value: orderCode }], }); if (orderUpd.ok) pass('更新订单状态为待备货'); else fail('更新订单状态为待备货', orderUpd.body.error?.message); } } // 3. 异常工单 console.log('\n[3] 异常管理'); const ticketCode = `EXC${Date.now()}`; const createTicket = await query(token, { table: 'exception_tickets', action: 'insert', insertData: { ticket_code: ticketCode, ticket_type: 'stockout', ticket_status: 10, priority: 2, related_order_code: orderCode, handle_remark: '测试工单\n缺货需换品', }, }); if (createTicket.ok) pass('创建异常工单 exception_tickets'); else fail('创建异常工单 exception_tickets', createTicket.body.error?.message); if (createTicket.ok) { const ticketId = createTicket.body.data?.[0]?.id || createTicket.body.data?.id; const processTicket = await query(token, { table: 'exception_tickets', action: 'update', updateData: { ticket_status: 30, handle_remark: '已处理' }, filters: [{ op: 'eq', column: 'id', value: ticketId }], }); if (processTicket.ok) pass('处理异常工单'); else fail('处理异常工单', processTicket.body.error?.message); const log = await query(token, { table: 'operation_logs', action: 'insert', insertData: { target_table: 'exception_tickets', target_id: ticketId, operation_type: 'resolve_ticket', new_value: { ticket_status: 30 }, }, }); if (log.ok) pass('异常工单操作日志'); else fail('异常工单操作日志', log.body.error?.message); } // 4. 采购管理 console.log('\n[4] 采购管理'); const purchaseCode = `PUR${Date.now()}`; const createPurchase = await query(token, { table: 'purchase_orders', action: 'insert', insertData: { purchase_code: purchaseCode, order_code: orderCode, sku_code: 'SKU001', supplier_code: 'SUP001', purchase_qty: 10, purchase_price: 4.5, purchase_status: 10, total_amount: 45, }, }); if (createPurchase.ok) pass('创建采购单 purchase_orders'); else fail('创建采购单 purchase_orders', createPurchase.body.error?.message); if (createPurchase.ok) { const approvePurchase = await query(token, { table: 'purchase_orders', action: 'update', updateData: { purchase_status: 20, remark: 'AI审核通过' }, filters: [{ op: 'eq', column: 'purchase_code', value: purchaseCode }], }); if (approvePurchase.ok) pass('采购单审核通过'); else fail('采购单审核通过', approvePurchase.body.error?.message); await query(token, { table: 'purchase_orders', action: 'delete', filters: [{ op: 'eq', column: 'purchase_code', value: purchaseCode }], }); } // 5. 验收管理 console.log('\n[5] 验收管理'); const inspId = createTicket.body.data?.[0]?.id; const inspectionsQ = await query(token, { table: 'inspections', action: 'select', select: 'id', limit: 1, }); if (inspectionsQ.ok && inspectionsQ.body.data?.length) { const insp = inspectionsQ.body.data[0]; const submitInsp = await query(token, { table: 'inspections', action: 'update', updateData: { actual_qty: 9.5, quality_rating: 'A', loss_qty: 0.5, loss_reason: '运输损耗', inspection_time: new Date().toISOString(), }, filters: [{ op: 'eq', column: 'id', value: insp.id }], }); if (submitInsp.ok) pass('提交验收结果 inspections'); else fail('提交验收结果 inspections', submitInsp.body.error?.message); } else { pass('验收记录查询', '无现有记录,跳过更新'); } // 6. 客户管理权限数据 console.log('\n[6] 客户/权限'); const permQ = await query(token, { table: 'permissions', action: 'select', select: 'permission_code', filters: [{ op: 'eq', column: 'permission_code', value: 'customer_manage' }], single: true, }); if (permQ.ok && permQ.body.data) pass('customer_manage 权限存在'); else fail('customer_manage 权限存在'); const bidQ = await query(token, { table: 'bid_prices', action: 'select', select: '*', filters: [{ op: 'eq', column: 'status', value: 1 }], }); if (bidQ.ok && bidQ.body.data?.length) pass('中标价 bid_prices', `${bidQ.body.data.length} 条`); else fail('中标价 bid_prices', bidQ.body.error?.message || '无数据'); // 7. 录单 order_status=20 console.log('\n[7] 录单代录'); const entryOrderCode = `ORD${Date.now() + 1}`; const entryOrder = await query(token, { table: 'orders', action: 'insert', insertData: { order_code: entryOrderCode, bid_code: `BID${Date.now() + 1}`, cycle_code: currentCycleCode(), customer_code: 'CUST001', order_status: 20, total_amount: 0, total_weight: 3, }, }); if (entryOrder.ok) pass('录单创建 order_status=20'); else fail('录单创建 order_status=20', entryOrder.body.error?.message); const allocList = await query(token, { table: 'orders', action: 'select', select: 'order_code, order_status', filters: [{ op: 'in', column: 'order_status', value: [20, 30] }], }); if (allocList.ok && allocList.body.data?.some((o) => o.order_code === entryOrderCode || o.order_code === orderCode)) { pass('分配页可查询待分配订单'); } else { fail('分配页可查询待分配订单'); } // cleanup console.log('\n[8] 清理测试数据'); await query(token, { table: 'allocation_tasks', action: 'delete', filters: [{ op: 'eq', column: 'order_code', value: orderCode }], }); await query(token, { table: 'supplier_preparations', action: 'delete', filters: [{ op: 'eq', column: 'order_code', value: orderCode }], }); await query(token, { table: 'orders', action: 'delete', filters: [{ op: 'eq', column: 'order_code', value: orderCode }], }); await query(token, { table: 'orders', action: 'delete', filters: [{ op: 'eq', column: 'order_code', value: entryOrderCode }], }); await query(token, { table: 'exception_tickets', action: 'delete', filters: [{ op: 'eq', column: 'ticket_code', value: ticketCode }], }); pass('清理测试数据'); const passed = results.filter((r) => r.ok).length; const failed = results.filter((r) => !r.ok); console.log('\n=== 测试汇总 ==='); console.log(`通过: ${passed}/${results.length}`); if (failed.length) { console.log('\n失败项:'); failed.forEach((r) => console.log(` - ${r.name}: ${r.detail}`)); } else { console.log('全部通过 ✓'); } console.log(''); process.exit(failed.length ? 1 : 0); } main().catch((e) => { console.error(e); process.exit(1); });