Files
Ubuntu 5fa2bbfd55
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

413 lines
13 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 页面流程 API 集成测试(模拟各页面关键写操作)
* 用法: node server/scripts/test-page-flows.js
*/
const { currentCycleCode } = require('./test-helpers.js');
const { guardTestTarget } = require('./test-target-guard.js');
// OPS-2:默认打本系统 API(3022);非本机目标必须 API_BASE + ALLOW_REMOTE=1(TTY 确认)
const DEFAULT_TEST_API_BASE = 'http://127.0.0.1:3022/api';
const explicitApiBase = String(process.env.API_BASE || '').trim();
const BASE = guardTestTarget({
base: explicitApiBase || DEFAULT_TEST_API_BASE,
explicit: Boolean(explicitApiBase),
label: 'API_BASE',
});
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);
});