Files
SupplierSystem/server/tests/prepBeforeInspection.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

194 lines
7.1 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
const { query, release, connect } = vi.hoisted(() => {
const query = vi.fn();
const release = vi.fn();
const connect = vi.fn(async () => ({ query, release }));
return { query, release, connect };
});
vi.mock('../src/db', () => ({
pool: { connect, query },
getRawPoolQuery: () => vi.fn(),
}));
vi.mock('../src/services/inspectionRejectFulfillmentService', async (importOriginal) => {
const actual = await importOriginal<typeof import('../src/services/inspectionRejectFulfillmentService')>();
return {
...actual,
createSupplierInspectionIfNeeded: vi.fn(async () => false),
hasOpenPendingInspection: vi.fn(async () => false),
hasSuccessfulInboundInspection: vi.fn(async () => false),
prunePendingInspectionsWhenInboundCovered: vi.fn(async () => 0),
pruneStalePendingInspectionsAfterPass: vi.fn(async () => 0),
pruneDuplicateSelfPurchasePendingInspections: vi.fn(async () => 0),
};
});
vi.mock('../src/services/processingTaskService', () => ({
ensureProcessingTaskForInspection: vi.fn(async () => undefined),
syncProcessingTasksForOrder: vi.fn(async () => undefined),
}));
vi.mock('../src/services/orderItemFulfillmentService', () => ({
isOrderItemFulfillmentValid: vi.fn(async () => true),
listUncoveredOrderItems: vi.fn(async () => []),
}));
vi.mock('../src/services/orderInspectionStateService', () => ({
isOrderItemInboundCovered: vi.fn(async () => false),
isMergeGuestOrderItem: vi.fn(async () => false),
}));
vi.mock('../src/services/inspectionDisplayService', () => ({
lookupInspectionItemName: vi.fn(async () => '商品'),
}));
import {
countPendingSupplierPreparations,
demotePrematureInspectionStatus,
ensureInspectionsForOrder,
tryAdvanceOrderToInspection,
} from '../src/services/preparationFulfillmentService';
import { computeEffectiveOrderStatus } from '../src/services/orderStatusDisplayService';
import { createSupplierInspectionIfNeeded } from '../src/services/inspectionRejectFulfillmentService';
describe('prep before inspection gate', () => {
beforeEach(() => {
query.mockReset();
release.mockReset();
connect.mockClear();
vi.mocked(createSupplierInspectionIfNeeded).mockClear();
});
it('countPendingSupplierPreparations 以 prepare_status < 30 为准', async () => {
query.mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 2 }] });
const cnt = await countPendingSupplierPreparations('ORD1');
expect(cnt).toBe(2);
const sql = String(query.mock.calls[0][0]);
expect(sql).toMatch(/prepare_status.*<\s*30/);
expect(sql).not.toMatch(/NOT EXISTS/);
});
it('备货未完成时 ensureInspectionsForOrder 不建待验', async () => {
const clientQuery = vi.fn(async (sql: string) => {
const s = String(sql);
if (s.includes('DELETE FROM inspections i')) {
return { rowCount: 1, rows: [{ id: 'ghost-1' }] };
}
if (s.includes('FROM supplier_preparations sp') && s.includes('prepare_qty')) {
return {
rowCount: 1,
rows: [
{
order_code: 'ORD1',
sku_code: 'SKU_DRY',
supplier_code: 'SUP1',
prepare_qty: 10,
purchase_price: 5,
item_name: '干杂',
prepare_status: 10,
},
],
};
}
if (s.includes('FROM order_items oi')) {
return {
rowCount: 1,
rows: [
{
id: 'item-1',
sku_code: 'SKU_DRY',
quantity: 10,
price: 8,
item_name: '干杂',
supplier_code: 'SUP1',
stock_cover_qty: 0,
},
],
};
}
if (s.includes('order_item_fulfillments')) {
return { rowCount: 1, rows: [{}] };
}
return { rowCount: 0, rows: [] };
});
const created = await ensureInspectionsForOrder('ORD1', 'BR001', {
client: { query: clientQuery } as any,
});
expect(created).toBe(0);
expect(createSupplierInspectionIfNeeded).not.toHaveBeenCalled();
});
it('tryAdvance:备货未完成不进 40;误进 40 可回退', async () => {
// 按 SQL 形状提供数据(实现对账路径会先查散客/状态/核货行,顺序不再固定,故不用 once 链)
const installAdvanceMocks = (status: number) => {
query.mockReset();
query.mockImplementation(async (sql: string) => {
const s = String(sql);
if (s.includes('SELECT customer_code FROM orders')) return { rows: [{ customer_code: 'C001' }], rowCount: 1 };
if (s.includes('SELECT order_status') && s.includes('FROM orders')) {
return { rows: [{ order_status: status, branch_code: 'BR001' }], rowCount: 1 };
}
if (s.includes('COUNT(*)') && s.includes('FROM supplier_preparations')) return { rows: [{ cnt: 1 }], rowCount: 1 };
if (s.includes('COUNT(*)') && s.includes('FROM purchase_orders')) return { rows: [{ cnt: 0 }], rowCount: 1 };
if (s.includes('DELETE FROM inspections')) return { rows: [{ id: 'i1' }], rowCount: 1 };
if (s.includes('UPDATE orders') && s.includes('SET order_status = 30')) {
return { rows: [{ order_code: 'ORD1' }], rowCount: 1 };
}
return { rows: [], rowCount: 0 };
});
};
installAdvanceMocks(40); // 误进 40 ⇒ 回退 30
const demoted = await tryAdvanceOrderToInspection('ORD1');
expect(demoted).toMatchObject({ advanced: false, newStatus: 30 });
installAdvanceMocks(30); // 待备货 + 有未完成备货 ⇒ 不得推进
const blocked = await tryAdvanceOrderToInspection('ORD1');
expect(blocked.advanced).toBe(false);
expect(String(blocked.message)).toMatch(/备货未完成/);
});
it('demotePrematureInspectionStatus 在 pending 时回退 40→30', async () => {
query.mockImplementation(async (sql: string) => {
const s = String(sql);
if (s.includes('COUNT(*)') && s.includes('FROM supplier_preparations')) return { rows: [{ cnt: 2 }], rowCount: 1 };
if (s.includes('COUNT(*)') && s.includes('FROM purchase_orders')) return { rows: [{ cnt: 0 }], rowCount: 1 };
if (s.includes('DELETE FROM inspections')) return { rows: [{ id: 'ghost' }], rowCount: 1 };
if (s.includes('SELECT order_status') && s.includes('FROM orders')) {
return { rows: [{ order_status: 40 }], rowCount: 1 };
}
if (s.includes('UPDATE orders')) return { rows: [{ order_code: 'ORD1' }], rowCount: 1 };
return { rows: [], rowCount: 0 };
});
const result = await demotePrematureInspectionStatus('ORD1');
expect(result.demoted).toBe(true);
expect(result.pruned).toBe(1);
});
it('computeEffectiveOrderStatus:有未完成备货时不显示 40', () => {
const status = computeEffectiveOrderStatus(
40,
{
allocationFulfilled: true,
pendingPreps: 1,
pendingPurchases: 0,
itemCount: 3,
inspectionCount: 2,
pendingProcessing: 0,
pendingPicks: 0,
outboundPicks: 0,
deliveryStarted: false,
deliveryEnded: false,
customerSigned: false,
},
'pending',
true
);
expect(status).toBe(30);
});
});