- 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 超长修复
165 lines
6.6 KiB
TypeScript
165 lines
6.6 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
||
/**
|
||
* BIZ-9(2026-10-19,T2):改派往返 A→他人→A 必须能复活;删行(removed)继续阻断。
|
||
*
|
||
* 旧实现把「改派走」和「删商品行」都记成 `cancelled`,且台账 upsert 对已有对照 COALESCE 优先保留
|
||
* ⇒ 同一 `prep_id` 再次分配回来时:要么被 `cancelled` 永久挡住,要么 `our_order_item_id` 仍指向
|
||
* 已被移除的旧订单行,永远配不上。
|
||
*
|
||
* 新口径:
|
||
* · `reallocated`(改派)不阻断,且导入命中时用本次映射覆盖旧对照并复位 `imported`;
|
||
* · `removed`/`cancelled`/`returned`/`completed`/`suspended` 仍阻断;
|
||
* · 台账唯一键仍是 `(branch_code, prep_id)`(不扩成每改派一行)。
|
||
*/
|
||
|
||
const H = vi.hoisted(() => ({ poolQuery: vi.fn() }));
|
||
|
||
vi.mock('../src/db', () => ({
|
||
pool: { query: (...args: unknown[]) => H.poolQuery(...args), connect: vi.fn() },
|
||
query: (...args: unknown[]) => H.poolQuery(...args),
|
||
getRawPoolQuery: () => H.poolQuery,
|
||
}));
|
||
|
||
import { needsAppendToOrder } from '../src/services/mainSystemOrderImport';
|
||
import { recordImportedPrepItems } from '../src/services/mainSystemPrepLedger';
|
||
|
||
type Ledger = Map<string, { disposition: string; our_order_item_id: string | null }>;
|
||
|
||
/** 内存台账 + 按 recordImportedPrepItems 的 upsert CASE 语义模拟(参数位置与源码一致) */
|
||
function installUpsertFake(ledger: Ledger): void {
|
||
H.poolQuery.mockImplementation(async (sql: string, params: unknown[] = []) => {
|
||
const text = String(sql);
|
||
if (/INSERT INTO public\.main_system_order_import_items/i.test(text)) {
|
||
const prepId = String(params[2]);
|
||
const incomingLink = (params[4] as string | null) ?? null;
|
||
const incomingDisposition = String(params[11]);
|
||
const existing = ledger.get(prepId);
|
||
if (!existing) {
|
||
ledger.set(prepId, { disposition: incomingDisposition, our_order_item_id: incomingLink });
|
||
} else {
|
||
let nextDisposition = existing.disposition;
|
||
if (existing.disposition === 'pending') nextDisposition = incomingDisposition;
|
||
else if (existing.disposition === 'skipped_stockout' && incomingLink) nextDisposition = incomingDisposition;
|
||
else if (existing.disposition === 'reallocated' && incomingLink) nextDisposition = incomingDisposition;
|
||
const nextLink =
|
||
existing.disposition === 'reallocated'
|
||
? incomingLink
|
||
: existing.our_order_item_id ?? incomingLink;
|
||
ledger.set(prepId, { disposition: nextDisposition, our_order_item_id: nextLink });
|
||
}
|
||
return { rows: [], rowCount: 1 };
|
||
}
|
||
return { rows: [], rowCount: 0 };
|
||
});
|
||
}
|
||
|
||
const PREP_A = '11111111-1111-4111-8111-111111111111';
|
||
const ITEM_OLD = '22222222-2222-4222-8222-222222222222';
|
||
const ITEM_NEW = '33333333-3333-4333-8333-333333333333';
|
||
|
||
function prepRow(id = PREP_A) {
|
||
return { id, itemName: '肉片', prepareQty: 2, isStockout: false };
|
||
}
|
||
|
||
function ourItems(id = ITEM_NEW) {
|
||
return [{ id, item_name: '肉片' }];
|
||
}
|
||
|
||
function mappingFor(itemId = ITEM_NEW): Map<string, string> {
|
||
return new Map([[PREP_A, itemId]]);
|
||
}
|
||
|
||
beforeEach(() => {
|
||
H.poolQuery.mockReset();
|
||
});
|
||
|
||
describe('BIZ-9:改派 A→他人→A 可复活', () => {
|
||
it('① 改派走后 disposition=reallocated,且该行需要重新追加(不能因旧对照永久挡住)', () => {
|
||
const row = prepRow();
|
||
const ledgerRow = { prep_id: PREP_A, our_order_item_id: ITEM_OLD, disposition: 'reallocated' };
|
||
expect(needsAppendToOrder(row, [ledgerRow])).toBe(true);
|
||
});
|
||
|
||
it('② E1 再次分配 A ⇒ upsert 复位 imported,并把旧对照换成新订单行', async () => {
|
||
const ledger: Ledger = new Map([[PREP_A, { disposition: 'reallocated', our_order_item_id: ITEM_OLD }]]);
|
||
installUpsertFake(ledger);
|
||
|
||
await recordImportedPrepItems({
|
||
branchCode: 'BR002',
|
||
mainOrderCode: 'ORD_WEB_A',
|
||
supplierCode: 'INTERNAL',
|
||
ourOrderCode: 'ORD_API_1',
|
||
prepRows: [prepRow()],
|
||
ourItems: ourItems(),
|
||
mapping: mappingFor(),
|
||
});
|
||
|
||
expect(ledger.get(PREP_A)).toEqual({ disposition: 'imported', our_order_item_id: ITEM_NEW });
|
||
});
|
||
|
||
it('④ 重复投递幂等:复活后已有对照 ⇒ 不再追加', () => {
|
||
const row = prepRow();
|
||
const ledgerRow = { prep_id: PREP_A, our_order_item_id: ITEM_NEW, disposition: 'imported' };
|
||
expect(needsAppendToOrder(row, [ledgerRow])).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe('BIZ-9:removed 是硬删语义,继续阻断', () => {
|
||
it('③ item_removed 后同 prep_id 再出现 ⇒ 不追加(不复活)', async () => {
|
||
const row = prepRow();
|
||
const ledgerRow = { prep_id: PREP_A, our_order_item_id: null, disposition: 'removed' };
|
||
expect(needsAppendToOrder(row, [ledgerRow])).toBe(false);
|
||
|
||
const ledger: Ledger = new Map([[PREP_A, { disposition: 'removed', our_order_item_id: null }]]);
|
||
installUpsertFake(ledger);
|
||
await recordImportedPrepItems({
|
||
branchCode: 'BR002',
|
||
mainOrderCode: 'ORD_WEB_A',
|
||
supplierCode: 'INTERNAL',
|
||
ourOrderCode: 'ORD_API_1',
|
||
prepRows: [prepRow()],
|
||
ourItems: ourItems(),
|
||
mapping: mappingFor(),
|
||
});
|
||
// 即使 E1 把行带回来,removed 仍保持阻断(不会被复位成 imported)
|
||
expect(ledger.get(PREP_A)?.disposition).toBe('removed');
|
||
});
|
||
|
||
it('cancelled/returned/completed/suspended 同样阻断', () => {
|
||
const row = prepRow();
|
||
for (const disposition of ['cancelled', 'returned', 'completed', 'suspended']) {
|
||
expect(needsAppendToOrder(row, [{ prep_id: PREP_A, our_order_item_id: null, disposition }])).toBe(false);
|
||
}
|
||
});
|
||
|
||
it('台账唯一键仍是 (branch_code, prep_id),不扩成"每改派一行"', async () => {
|
||
const ledger: Ledger = new Map();
|
||
installUpsertFake(ledger);
|
||
await recordImportedPrepItems({
|
||
branchCode: 'BR002',
|
||
mainOrderCode: 'ORD_WEB_A',
|
||
supplierCode: 'INTERNAL',
|
||
ourOrderCode: 'ORD_API_1',
|
||
prepRows: [prepRow()],
|
||
ourItems: ourItems(),
|
||
mapping: mappingFor(),
|
||
});
|
||
const insert = H.poolQuery.mock.calls.find(([sql]: [string]) =>
|
||
/INSERT INTO public\.main_system_order_import_items/i.test(String(sql))
|
||
);
|
||
expect(String(insert![0])).toContain('ON CONFLICT (branch_code, prep_id) DO UPDATE');
|
||
// 同一 prep_id 重复导入只更新同一行(内存台账始终一个 key)
|
||
await recordImportedPrepItems({
|
||
branchCode: 'BR002',
|
||
mainOrderCode: 'ORD_WEB_A',
|
||
supplierCode: 'INTERNAL',
|
||
ourOrderCode: 'ORD_API_1',
|
||
prepRows: [prepRow()],
|
||
ourItems: ourItems(),
|
||
mapping: mappingFor(),
|
||
});
|
||
expect(ledger.size).toBe(1);
|
||
});
|
||
});
|