Files
SupplierSystem/server/tests/orderBatchReportFiles.test.ts
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

163 lines
6.3 KiB
TypeScript
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.
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { readFileSync } from 'fs';
import path from 'path';
/**
* 订单详情「报告」列显示**批次报告**(2026-10-12)。
*
* 设计:`docs/design/20261012-订单详情显示批次报告.md`
*
* 用户报告「我这边没有看到报告,主系统那边也没有」——主系统侧已由第九批推送修好,
* 我方侧此前只显示**质检报告**(`quality_reports`),不显示**批次报告**
* (`inventory_batch_reports`)⇒ 抄手皮那几批明明绑了报告,订单行仍是空的。
*
* 这里钉住三件做反就要返工的事:
* ① **口径不许漂**:批次判据必须与推送侧 `collectReportPushCandidates` 同一套两来源
* (`picking_items.outbound_batches` ∪ `inventory_logs` 的 `picking_out`/`delivery_loading_out`),
* 否则会出现"我方看得到、主系统收不到"(或反之);
* ② **只显示已人工确认的报告**(`status = 30`),草稿/待确认不许出现在订单详情;
* ③ 路由只做鉴权 + 委派(与既有 `quality-report-files` 同权限集合),不写 SQL。
*/
const query = vi.hoisted(() => vi.fn());
vi.mock('../src/db', () => ({
pool: { query: (...args: unknown[]) => query(...args) },
}));
import { listOrderBatchReportFiles } from '../src/services/orderBatchReportFilesService';
const serviceSrc = readFileSync(
path.join(__dirname, '../src/services/orderBatchReportFilesService.ts'),
'utf8'
);
const pushSrc = readFileSync(
path.join(__dirname, '../src/services/mainSystemReportPushService.ts'),
'utf8'
);
const routeSrc = readFileSync(
path.join(__dirname, '../src/modules/workflow/routes/orderRoutes.ts'),
'utf8'
);
/** 取出 GET /orders/:orderCode/batch-reports 这一个 handler 的源码片段 */
function batchReportRouteBlock(): string {
const start = routeSrc.indexOf("router.get('/orders/:orderCode/batch-reports'");
expect(start, '路由必须存在').toBeGreaterThan(-1);
const end = routeSrc.indexOf('router.get(', start + 10);
return routeSrc.slice(start, end > start ? end : start + 2000);
}
describe('订单详情批次报告(我方侧可见性)', () => {
beforeEach(() => {
query.mockReset();
});
it('① 口径守卫:批次判据与推送侧同为「分拣选定 ∪ 实际出库流水」两来源', () => {
for (const src of [serviceSrc, pushSrc]) {
expect(src).toContain('outbound_batches');
expect(src).toContain("'picking_out'");
expect(src).toContain("'delivery_loading_out'");
expect(src).toContain('related_order_code');
}
// 实际出库流水必须两者都认(只看 picking_out 会漏发车兜底出库的单)
expect(serviceSrc).toMatch(/operation_type\s+IN\s*\(\s*'picking_out'\s*,\s*'delivery_loading_out'\s*\)/i);
expect(pushSrc).toMatch(/operation_type\s+IN\s*\(\s*'picking_out'\s*,\s*'delivery_loading_out'\s*\)/i);
});
it('② 只显示已人工确认的批次报告(status = 30)', () => {
expect(serviceSrc).toMatch(/r\.status\s*=\s*30/);
expect(serviceSrc).toContain('inventory_batch_report_matches');
expect(serviceSrc).toContain('inventory_batch_reports');
});
it('③ 路由只做鉴权 + 委派,且与既有质检报告列同权限集合', () => {
const block = batchReportRouteBlock();
expect(block).toContain('authMiddleware');
for (const perm of ['order_manage', 'order_entry', 'order_status_update', 'allocation_manage']) {
expect(block).toContain(`'${perm}'`);
}
expect(block).toContain('listOrderBatchReportFiles');
expect(block, '路由里不许写 SQL').not.toMatch(/SELECT\s+/i);
});
it('④ 空单号直接返回空,不打扰数据库', async () => {
await expect(listOrderBatchReportFiles(' ')).resolves.toEqual({ items: [] });
expect(query).not.toHaveBeenCalled();
});
it('⑤ 组装:按行 × 批次给出报告与签名文件(pdf/image 分类)', async () => {
query.mockResolvedValue({
rowCount: 2,
rows: [
{
our_order_item_id: '887',
sku_code: 'SKU62634064428',
item_name: '抄手皮',
batch_no: 'STK20261011HHFCUN',
report_id: '42',
report_no: 'HZ-2026-1011',
produced_at: '2026-10-08',
testing_org: '红河质安检验检测有限公司',
product_names: ['抄手皮'],
file_paths: ['抄手皮/2026/报告.pdf', '抄手皮/2026/照片.jpg', '', null],
},
],
});
const { items } = await listOrderBatchReportFiles('ORD_X');
expect(query).toHaveBeenCalledTimes(1);
expect(query.mock.calls[0][1]).toEqual(['ORD_X']);
expect(items).toHaveLength(1);
expect(items[0]).toMatchObject({
our_order_item_id: '887',
sku_code: 'SKU62634064428',
item_name: '抄手皮',
batch_no: 'STK20261011HHFCUN',
report_id: '42',
report_no: 'HZ-2026-1011',
produced_at: '2026-10-08',
testing_org: '红河质安检验检测有限公司',
product_names: ['抄手皮'],
});
// 空文件名被滤掉,剩下两个;kind 仍按扩展名分类
expect(items[0].files.map((f) => f.kind)).toEqual(['pdf', 'image']);
// T9F-5(2026-10-19):本用例的 file_paths 是**无分支前缀的旧 key**,且测试环境没有
// tenant 上下文;mint 侧已 fail-closed,所以列表保留 path/kind、URL 为空(不再同步放行)。
// 旧对象需迁移为 `<branch>/` 前缀;带 ctx 的签名路径见 t9fStorageGuardResiduals.test.ts。
expect(items[0].files[0].url).toBe('');
});
it('⑥ 字段可空(无报告号/无检测机构/无产品名)不炸', async () => {
query.mockResolvedValue({
rowCount: 1,
rows: [
{
our_order_item_id: '1',
sku_code: null,
item_name: null,
batch_no: 'STK1',
report_id: '9',
report_no: null,
produced_at: null,
testing_org: null,
product_names: null,
file_paths: null,
},
],
});
const { items } = await listOrderBatchReportFiles('ORD_Y');
expect(items[0]).toMatchObject({
sku_code: null,
item_name: null,
report_no: null,
produced_at: null,
testing_org: null,
product_names: [],
files: [],
});
});
});