- 库存抵扣成本按目标抵扣量幂等更新,作废/退货清 cover_qty;多批次展示按加权均价;商品成本排行纳入库存抵扣成本并改标签为“成本” - 主系统事件:allocated 定向 order_code、returned 等批量状态核对、clamped/全量兜底下调频、失败不计 applied - 订单完成:零价闸门排除已退货行;供应商系统关闭“主数据不卡流程”入口与执行接口 - 迁移 20261080/20261081;补充相关契约/回归测试
178 lines
7.2 KiB
TypeScript
178 lines
7.2 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
||
/**
|
||
* task-25:`importMainSystemOrders({onlyMainOrderCodes})` 定向拉取。
|
||
* 2 个 supplier binding × 2 个去重 orderCode ⇒ 4 次 `fetchActivePreparationsForOrder`,
|
||
* 不再调全量 `fetchMainSystemPreparations`;失败跳过、全失败 unavailable、空 rows 不产生 group。
|
||
*/
|
||
|
||
const mocks = vi.hoisted(() => ({
|
||
listAutoPullable: vi.fn(),
|
||
listActive: vi.fn(),
|
||
fetchFull: vi.fn(),
|
||
fetchByOrder: vi.fn(),
|
||
}));
|
||
|
||
vi.mock('../src/services/branchAccessCode', () => ({
|
||
listAutoPullableBindingsForBranch: (...args: unknown[]) => mocks.listAutoPullable(...args),
|
||
listActiveBindingsForBranch: (...args: unknown[]) => mocks.listActive(...args),
|
||
}));
|
||
vi.mock('../src/services/mainSystemPreparations', () => ({
|
||
fetchMainSystemPreparations: (...args: unknown[]) => mocks.fetchFull(...args),
|
||
fetchActivePreparationsForOrder: (...args: unknown[]) => mocks.fetchByOrder(...args),
|
||
}));
|
||
|
||
const { pullPreparationsForOrders } = await import('../src/services/mainSystemBranchPull');
|
||
|
||
const binding = (supplierCode: string) => ({
|
||
branchCode: 'BR002',
|
||
accessCode: 'SCQEM2X7B6P5',
|
||
mainBranchCode: 'BR002',
|
||
mainSupplierCode: supplierCode,
|
||
});
|
||
|
||
const okByOrder = ({ supplierCode, orderCode }: { supplierCode: string; orderCode: string }) => ({
|
||
kind: 'ok' as const,
|
||
supplierCode,
|
||
branchCode: 'BR002',
|
||
orders: [
|
||
{
|
||
orderCode,
|
||
deliveryDate: '2026-10-21',
|
||
taskDay: '2026-10-21',
|
||
rows: [{ orderCode, skuCode: 'SKU1', quantity: 1 }],
|
||
},
|
||
],
|
||
rowCount: 1,
|
||
});
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks();
|
||
mocks.listActive.mockResolvedValue([]);
|
||
});
|
||
|
||
describe('pullPreparationsForOrders:按订单定向', () => {
|
||
it('2 binding × 2 去重 order ⇒ 4 次逐单调用,0 次全量', async () => {
|
||
mocks.listAutoPullable.mockResolvedValue([binding('SUP_A'), binding('SUP_B')]);
|
||
mocks.fetchByOrder.mockImplementation(async (input: { supplierCode: string; orderCode: string }) =>
|
||
okByOrder(input)
|
||
);
|
||
|
||
const r = await pullPreparationsForOrders('BR002', ['ORD_1', 'ORD_2', 'ORD_1']);
|
||
|
||
expect(r.kind).toBe('ok');
|
||
expect(mocks.fetchFull).toHaveBeenCalledTimes(0);
|
||
expect(mocks.fetchByOrder).toHaveBeenCalledTimes(4);
|
||
const calls = mocks.fetchByOrder.mock.calls.map(([arg]) => ({
|
||
supplierCode: arg.supplierCode,
|
||
orderCode: arg.orderCode,
|
||
}));
|
||
expect(calls).toEqual([
|
||
{ supplierCode: 'SUP_A', orderCode: 'ORD_1' },
|
||
{ supplierCode: 'SUP_A', orderCode: 'ORD_2' },
|
||
{ supplierCode: 'SUP_B', orderCode: 'ORD_1' },
|
||
{ supplierCode: 'SUP_B', orderCode: 'ORD_2' },
|
||
]);
|
||
if (r.kind === 'ok') {
|
||
expect(r.pulls).toHaveLength(2);
|
||
expect(r.pulls.map((p) => p.outcome.orders.map((o) => o.orderCode))).toEqual([
|
||
['ORD_1', 'ORD_2'],
|
||
['ORD_1', 'ORD_2'],
|
||
]);
|
||
expect(r.pulls.every((p) => p.outcome.rowCount === 2)).toBe(true);
|
||
}
|
||
});
|
||
|
||
it('2 单中 1 单失败 ⇒ 该 supplier 整家无 pull,另一 supplier 正常', async () => {
|
||
mocks.listAutoPullable.mockResolvedValue([binding('SUP_A'), binding('SUP_B')]);
|
||
mocks.fetchByOrder.mockImplementation(async (input: { supplierCode: string; orderCode: string }) =>
|
||
input.supplierCode === 'SUP_B' && input.orderCode === 'ORD_2'
|
||
? { kind: 'unavailable', reason: 'forbidden' }
|
||
: okByOrder(input)
|
||
);
|
||
|
||
const r = await pullPreparationsForOrders('BR002', ['ORD_1', 'ORD_2']);
|
||
expect(r.kind).toBe('ok');
|
||
if (r.kind === 'ok') {
|
||
expect(r.failures).toHaveLength(1);
|
||
expect(r.failures[0]).toMatchObject({ supplierCode: 'SUP_B' });
|
||
expect(r.failures[0].reason).toContain('ORD_2');
|
||
// 全有或全无:SUP_B 的 ORD_1 虽然成功,但 ORD_2 失败 ⇒ 整个 supplier 不 push
|
||
expect(r.pulls.map((p) => p.supplierCode)).toEqual(['SUP_A']);
|
||
expect(r.pulls[0].outcome.orders.map((o) => o.orderCode)).toEqual(['ORD_1', 'ORD_2']);
|
||
}
|
||
});
|
||
|
||
it('全部失败 ⇒ unavailable', async () => {
|
||
mocks.listAutoPullable.mockResolvedValue([binding('SUP_A'), binding('SUP_B')]);
|
||
mocks.fetchByOrder.mockResolvedValue({ kind: 'unavailable', reason: 'forbidden' });
|
||
const r = await pullPreparationsForOrders('BR002', ['ORD_1', 'ORD_2']);
|
||
expect(r.kind).toBe('unavailable');
|
||
if (r.kind === 'unavailable') expect(r.reason).toContain('forbidden');
|
||
});
|
||
|
||
it('空 rows 的 order 不产生伪 group', async () => {
|
||
mocks.listAutoPullable.mockResolvedValue([binding('SUP_A')]);
|
||
mocks.fetchByOrder.mockImplementation(async (input: { supplierCode: string; orderCode: string }) => ({
|
||
...okByOrder(input),
|
||
orders: [{ orderCode: input.orderCode, deliveryDate: null, taskDay: null, rows: [] }],
|
||
rowCount: 0,
|
||
}));
|
||
const r = await pullPreparationsForOrders('BR002', ['ORD_1']);
|
||
expect(r.kind).toBe('ok');
|
||
if (r.kind === 'ok') {
|
||
expect(r.pulls[0].outcome.orders).toEqual([]);
|
||
expect(r.pulls[0].outcome.rowCount).toBe(0);
|
||
}
|
||
});
|
||
|
||
it('定向导入的 settle 只筛本次 order 台账行(源码契约)', () => {
|
||
const source = readFileSync(
|
||
new URL('../src/services/mainSystemOrderImport.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
const settle = source.slice(
|
||
source.indexOf('async function settleRowsNoLongerOurs'),
|
||
source.indexOf('async function', source.indexOf('async function settleRowsNoLongerOurs') + 10)
|
||
);
|
||
expect(settle).toContain('onlyMainOrderCodes?: string[]');
|
||
expect(settle).toContain('const wanted = params.onlyMainOrderCodes?.length');
|
||
expect(settle).toContain('wanted.has(String(row.main_order_code');
|
||
// 定向调用点必须把 onlyMainOrderCodes 传进 settle
|
||
expect(source).toMatch(/settleRowsNoLongerOurs\(\{[\s\S]*?onlyMainOrderCodes/);
|
||
});
|
||
|
||
it('import 对 pull.failures 记日志并计入 failed(不静默)', () => {
|
||
const source = readFileSync(
|
||
new URL('../src/services/mainSystemOrderImport.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
expect(source).toContain('for (const failure of pull.failures || [])');
|
||
expect(source).toContain('拉取失败,本轮跳过、计入 failed');
|
||
expect(source).toContain("status: 'failed'");
|
||
});
|
||
|
||
it('定向 truncated / 空 groups 跳过 settle,交给全量兜底(源码契约)', () => {
|
||
const source = readFileSync(
|
||
new URL('../src/services/mainSystemOrderImport.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
expect(source).toContain('const skipSettleForTargeted =');
|
||
expect(source).toContain('one.outcome.truncated === true || groups.length === 0');
|
||
expect(source).toContain('if (skipSettleForTargeted)');
|
||
expect(source).toContain('跳过缺货结算,等待全量兜底');
|
||
// 全量路径仍不传 onlyMainOrderCodes 给 settle
|
||
expect(source).toMatch(/\.\.\.\(onlyMainOrderCodes\?\.length \? \{ onlyMainOrderCodes \} : \{\}\)/);
|
||
});
|
||
|
||
it('not_bound 语义与全量版一致', async () => {
|
||
mocks.listAutoPullable.mockResolvedValue([]);
|
||
mocks.listActive.mockResolvedValue([binding('SUP_A')]);
|
||
const r = await pullPreparationsForOrders('BR002', ['ORD_1']);
|
||
expect(r.kind).toBe('not_bound');
|
||
expect(mocks.fetchFull).not.toHaveBeenCalled();
|
||
expect(mocks.fetchByOrder).not.toHaveBeenCalled();
|
||
});
|
||
});
|