- 库存抵扣成本按目标抵扣量幂等更新,作废/退货清 cover_qty;多批次展示按加权均价;商品成本排行纳入库存抵扣成本并改标签为“成本” - 主系统事件:allocated 定向 order_code、returned 等批量状态核对、clamped/全量兜底下调频、失败不计 applied - 订单完成:零价闸门排除已退货行;供应商系统关闭“主数据不卡流程”入口与执行接口 - 迁移 20261080/20261081;补充相关契约/回归测试
433 lines
20 KiB
TypeScript
433 lines
20 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { classifyEvent, resolveEventSince } from '../src/services/mainSystemEventConsumer';
|
|
|
|
const cursorRuntime = vi.hoisted(() => ({
|
|
query: vi.fn(),
|
|
fetchEvents: vi.fn(),
|
|
listSuppliers: vi.fn(),
|
|
getToken: vi.fn(),
|
|
follow: vi.fn(),
|
|
lookupBatch: vi.fn(),
|
|
fetchActiveForOrder: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../src/db', () => ({
|
|
pool: { query: (...args: unknown[]) => cursorRuntime.query(...args) },
|
|
}));
|
|
vi.mock('../src/services/mainSystemBranchApi', () => ({
|
|
listSupplierCodesForBranch: (...args: unknown[]) => cursorRuntime.listSuppliers(...args),
|
|
}));
|
|
vi.mock('../src/services/mainSystem/token', () => ({
|
|
getMainSystemToken: (...args: unknown[]) => cursorRuntime.getToken(...args),
|
|
}));
|
|
vi.mock('../src/services/mainSystem/api/events', () => ({
|
|
fetchSupplierEvents: (...args: unknown[]) => cursorRuntime.fetchEvents(...args),
|
|
}));
|
|
vi.mock('../src/services/tenantDbContext', () => ({
|
|
runWithTenantDbContext: async (_branchCode: string, fn: () => Promise<unknown>) => fn(),
|
|
}));
|
|
vi.mock('../src/services/mainSystemReturnFollow', () => ({
|
|
followMainSystemPrepChanges: (...args: unknown[]) => cursorRuntime.follow(...args),
|
|
}));
|
|
vi.mock('../src/services/mainSystemPreparations', () => ({
|
|
lookupPreparationStatesBatch: (...args: unknown[]) => cursorRuntime.lookupBatch(...args),
|
|
fetchActivePreparationsForOrder: (...args: unknown[]) => cursorRuntime.fetchActiveForOrder(...args),
|
|
}));
|
|
|
|
/**
|
|
* 主系统 2026-10-24 定稿的 5 项配合,我方接收端(①事件消费 ②定向拉取/cursor ③批量状态 ④批量撤下 ⑤binding.changed)。
|
|
* 开发者要求:**只在本地联调,不部署;保留现有全量兜底、不删旧链路**。
|
|
*/
|
|
const config = readFileSync(new URL('../src/services/mainSystem/config.ts', import.meta.url), 'utf8');
|
|
const events = readFileSync(new URL('../src/services/mainSystem/api/events.ts', import.meta.url), 'utf8');
|
|
const preps = readFileSync(new URL('../src/services/mainSystem/api/preparations.ts', import.meta.url), 'utf8');
|
|
const reports = readFileSync(new URL('../src/services/mainSystem/api/qualityReports.ts', import.meta.url), 'utf8');
|
|
const consumer = readFileSync(new URL('../src/services/mainSystemEventConsumer.ts', import.meta.url), 'utf8');
|
|
const scheduler = readFileSync(new URL('../src/services/scheduledJobs.ts', import.meta.url), 'utf8');
|
|
const webhook = readFileSync(new URL('../src/routes/mainSystemIntegration.ts', import.meta.url), 'utf8');
|
|
const migration = readFileSync(
|
|
new URL('../../migrations/20261078_main_system_event_cursor.sql', import.meta.url),
|
|
'utf8'
|
|
);
|
|
const perSupplierMigration = readFileSync(
|
|
new URL('../../migrations/20261079_event_cursor_per_supplier.sql', import.meta.url),
|
|
'utf8'
|
|
);
|
|
|
|
describe('主系统 5 项配合:我方接收端', () => {
|
|
it('① 事件接口:路径 + since/cursor/limit + server_time/next_cursor/has_more/clamped 解析', () => {
|
|
expect(config).toContain("events: '/api/supplier/events'");
|
|
expect(events).toContain('since');
|
|
expect(events).toContain('cursor');
|
|
expect(events).toContain('serverTime');
|
|
expect(events).toContain('nextCursor');
|
|
expect(events).toContain('hasMore');
|
|
expect(events).toContain('clamped');
|
|
});
|
|
|
|
it('② 备货:order_code 定向拉取 + cursor 翻页(offset 兼容保留)', () => {
|
|
expect(preps).toContain('fetchActivePreparationsForOrder');
|
|
expect(preps).toContain('order_code=');
|
|
expect(preps).toContain('nextCursor');
|
|
expect(preps).toContain('...(cursor ? { cursor } : {})');
|
|
// 老兼容:offset 仍传
|
|
expect(preps).toContain('offset,');
|
|
});
|
|
|
|
it('③ 批量备货状态:新接口且老 lookup 保留', () => {
|
|
expect(config).toContain("preparationsStatusBatch: '/api/supplier/preparations/status-batch'");
|
|
expect(preps).toContain('lookupPreparationStatesBatch');
|
|
expect(preps).toContain('prep_ids');
|
|
expect(preps).toContain('export async function lookupMainSystemPreparationStates');
|
|
});
|
|
|
|
it('④ 批量撤下:新接口且老单条保留;报告推送多行时优先批量', () => {
|
|
expect(config).toContain("qualityReportVoidBatch: '/api/supplier/quality-reports/void'");
|
|
expect(reports).toContain('voidSupplierBatchReportsBatch');
|
|
expect(reports).toContain('report_ids');
|
|
expect(reports).toContain('export async function voidSupplierBatchReport');
|
|
});
|
|
|
|
it('⑤ binding.changed 接收端已存在且验签(HMAC sha256 常量时间比较)', () => {
|
|
expect(webhook).toContain('isBindingChangedEvent');
|
|
expect(webhook).toContain('verifyWebhookSignature');
|
|
expect(webhook).toContain("crypto.createHmac('sha256'");
|
|
expect(webhook).toContain('timingSafeEqual');
|
|
});
|
|
|
|
it('一码多绑:按**每个 supplier_code 各拉一遍**(不取第一条绑定)+ 游标/去重按 (分公司,供应商)', () => {
|
|
expect(consumer).toContain('listSupplierCodesForBranch');
|
|
expect(consumer).toContain('for (const supplierCode of supplierCodes)');
|
|
expect(consumer).toContain('main_supplier_code = $2');
|
|
expect(consumer).toContain('main_supplier_code, event_id');
|
|
expect(perSupplierMigration).toContain('PRIMARY KEY (branch_code, main_supplier_code)');
|
|
expect(perSupplierMigration).toContain('PRIMARY KEY (branch_code, main_supplier_code, event_id)');
|
|
});
|
|
|
|
it('首轮 server_time → 之后 cursor 优先;无 cursor 时 lastEventAt/serverTime/bootstrap 兜底', () => {
|
|
expect(consumer).toContain('const firstRun = !stored.cursor && !stored.lastEventAt');
|
|
expect(consumer).toContain('const since = resolveEventSince(stored)');
|
|
expect(consumer).toContain('return stored.lastEventAt || stored.serverTime || null');
|
|
expect(consumer).toContain('SELECT cursor, last_event_at, server_time');
|
|
expect(resolveEventSince({ cursor: null, lastEventAt: null, serverTime: null })).toBeNull();
|
|
expect(resolveEventSince({ cursor: null, lastEventAt: null, serverTime: '2026-10-20T10:00:00.000Z' })).toBe(
|
|
'2026-10-20T10:00:00.000Z'
|
|
);
|
|
expect(
|
|
resolveEventSince({
|
|
cursor: null,
|
|
lastEventAt: '2026-10-20T09:00:00.000Z',
|
|
serverTime: '2026-10-20T10:00:00.000Z',
|
|
})
|
|
).toBe('2026-10-20T09:00:00.000Z');
|
|
expect(resolveEventSince({ cursor: 'CUR', lastEventAt: '2026-10-20T10:00:00.000Z', serverTime: null })).toBeNull();
|
|
expect(consumer).toContain('summary.initialFullReconcile = true');
|
|
expect(consumer).toContain('followMainSystemPrepChanges(branchCode)');
|
|
});
|
|
|
|
it('cursor 非法(400)⇒ 重置游标并标记需全量兜底', () => {
|
|
expect(consumer).toContain("res.kind === 'invalid_cursor'");
|
|
expect(consumer).toContain("reason: 'cursor_invalid_reset'");
|
|
expect(events).toContain("kind: 'invalid_cursor'");
|
|
expect(events).toContain('Number(result.status) === 400');
|
|
});
|
|
|
|
it('事件消费纪律:server_time 起点 + next_cursor 持久化 + 翻页到 has_more=false + 去重', () => {
|
|
expect(consumer).toContain('resolveEventSince');
|
|
expect(consumer).toContain('saveCursor');
|
|
expect(consumer).toContain('markEventSeen');
|
|
expect(consumer).toContain('if (!res.hasMore || !res.nextCursor) break;');
|
|
expect(migration).toContain('main_system_event_cursors');
|
|
expect(migration).toContain('main_system_events_seen');
|
|
});
|
|
|
|
it('未知事件即使带 order_code 也只忽略(不触发全量跟随;本地实测抓到的放大点)', () => {
|
|
const dispatchBlock = consumer.slice(consumer.indexOf('async function dispatchEvent'), consumer.indexOf('/** 所有有绑定的分公司'));
|
|
expect(dispatchBlock).toContain("if (action === 'ignored') return 'ignored';");
|
|
});
|
|
|
|
it('全量兜底保留:事件 2 分钟 + 全量 active 拉取降为 30 分钟(不删旧链路)', () => {
|
|
expect(scheduler).toContain('MAIN_SYSTEM_EVENT_LOCK_KEY');
|
|
expect(scheduler).toContain("}, 2 * 60 * 1000);");
|
|
expect(scheduler).toContain('MAIN_SYSTEM_FULL_ACTIVE_PULL_MINUTES');
|
|
expect(scheduler).toContain('fullReturnFollowIntervalMs()');
|
|
// 旧链路仍在
|
|
expect(scheduler).toContain('runReturnFollowReconcile()');
|
|
});
|
|
});
|
|
|
|
it('runtime:首轮无事件记住 server_time,第二轮 since=该值;有 cursor 时 since=null', async () => {
|
|
let row: { cursor: string | null; last_event_at: string | null; server_time: string | null } = {
|
|
cursor: null,
|
|
last_event_at: null,
|
|
server_time: null,
|
|
};
|
|
cursorRuntime.query.mockImplementation(async (sql: string, params: unknown[] = []) => {
|
|
const text = String(sql);
|
|
if (/SELECT cursor, last_event_at, server_time/i.test(text)) {
|
|
return { rows: row ? [{ ...row }] : [], rowCount: row ? 1 : 0 };
|
|
}
|
|
if (/INSERT INTO public\.main_system_event_cursors/i.test(text)) {
|
|
row = {
|
|
cursor: (params[2] as string | null) ?? null,
|
|
last_event_at: (params[3] as string | null) ?? null,
|
|
server_time: (params[4] as string | null) ?? null,
|
|
};
|
|
return { rows: [], rowCount: 1 };
|
|
}
|
|
return { rows: [], rowCount: 1 };
|
|
});
|
|
cursorRuntime.listSuppliers.mockResolvedValue(['SUP_A']);
|
|
cursorRuntime.getToken.mockResolvedValue({
|
|
kind: 'ok',
|
|
token: 'TOKEN',
|
|
branchCode: 'BR002',
|
|
expiresAt: Date.now() + 60_000,
|
|
});
|
|
cursorRuntime.follow.mockResolvedValue(undefined);
|
|
cursorRuntime.fetchEvents.mockResolvedValue({
|
|
kind: 'ok',
|
|
events: [],
|
|
serverTime: '2026-10-20T10:00:00.000Z',
|
|
nextCursor: null,
|
|
hasMore: false,
|
|
clamped: false,
|
|
});
|
|
|
|
const { consumeMainSystemEventsForBranch } = await import('../src/services/mainSystemEventConsumer');
|
|
await consumeMainSystemEventsForBranch({ branchCode: 'BR002' });
|
|
await consumeMainSystemEventsForBranch({ branchCode: 'BR002' });
|
|
|
|
expect(cursorRuntime.fetchEvents).toHaveBeenCalledTimes(2);
|
|
expect((cursorRuntime.fetchEvents.mock.calls[0][0] as { since?: string | null }).since).toBeNull();
|
|
expect((cursorRuntime.fetchEvents.mock.calls[1][0] as { since?: string | null }).since).toBe(
|
|
'2026-10-20T10:00:00.000Z'
|
|
);
|
|
|
|
// cursor 存在时只带 cursor,不再带 since
|
|
row = {
|
|
cursor: 'CUR_1',
|
|
last_event_at: '2026-10-20T10:00:00.000Z',
|
|
server_time: '2026-10-20T10:00:00.000Z',
|
|
};
|
|
cursorRuntime.fetchEvents.mockClear();
|
|
await consumeMainSystemEventsForBranch({ branchCode: 'BR002' });
|
|
const arg = cursorRuntime.fetchEvents.mock.calls[0][0] as { since?: string | null; cursor?: string | null };
|
|
expect(arg.since).toBeNull();
|
|
expect(arg.cursor).toBe('CUR_1');
|
|
});
|
|
|
|
it('verify_order:批量事实命中才定向拉单 + 定向 follow,禁止无参全量 follow', async () => {
|
|
vi.clearAllMocks();
|
|
let row: { cursor: string | null; last_event_at: string | null; server_time: string | null } = {
|
|
cursor: null,
|
|
last_event_at: '2026-10-20T09:59:00.000Z',
|
|
server_time: '2026-10-20T09:59:00.000Z',
|
|
};
|
|
cursorRuntime.query.mockImplementation(async (sql: string, params: unknown[] = []) => {
|
|
const text = String(sql);
|
|
if (/SELECT cursor, last_event_at, server_time/i.test(text)) {
|
|
return { rows: [{ ...row }], rowCount: 1 };
|
|
}
|
|
if (/INSERT INTO public\.main_system_event_cursors/i.test(text)) {
|
|
row = {
|
|
cursor: (params[2] as string | null) ?? null,
|
|
last_event_at: (params[3] as string | null) ?? null,
|
|
server_time: (params[4] as string | null) ?? null,
|
|
};
|
|
return { rows: [], rowCount: 1 };
|
|
}
|
|
if (/FROM public\.main_system_order_import_items/i.test(text) && /main_order_code = \$2/i.test(text)) {
|
|
return { rows: [{ prep_id: 'P1' }], rowCount: 1 };
|
|
}
|
|
return { rows: [], rowCount: 1 };
|
|
});
|
|
cursorRuntime.listSuppliers.mockResolvedValue(['SUP_A']);
|
|
cursorRuntime.getToken.mockResolvedValue({
|
|
kind: 'ok',
|
|
token: 'TOKEN',
|
|
branchCode: 'BR002',
|
|
expiresAt: Date.now() + 60_000,
|
|
});
|
|
cursorRuntime.fetchEvents.mockResolvedValue({
|
|
kind: 'ok',
|
|
events: [{
|
|
id: 'E1',
|
|
requestId: null,
|
|
event: 'preparations.returned',
|
|
orderCode: 'ORD_1',
|
|
supplierCode: 'SUP_A',
|
|
status: null,
|
|
attempt: null,
|
|
createdAt: '2026-10-20T10:00:00.000Z',
|
|
}],
|
|
serverTime: '2026-10-20T10:00:00.000Z',
|
|
nextCursor: 'CUR_2',
|
|
hasMore: false,
|
|
clamped: false,
|
|
});
|
|
cursorRuntime.lookupBatch.mockResolvedValue({
|
|
kind: 'ok',
|
|
rows: [{ prepId: 'P1', prepareStatus: 30, returnCode: null, supplierCode: 'SUP_A' }],
|
|
missing: ['P1'],
|
|
});
|
|
const activeOutcome = {
|
|
kind: 'ok',
|
|
supplierCode: 'SUP_A',
|
|
branchCode: 'BR002',
|
|
orders: [{ orderCode: 'ORD_1', deliveryDate: '2026-10-21', taskDay: '2026-10-21', rows: [] }],
|
|
rowCount: 0,
|
|
};
|
|
cursorRuntime.fetchActiveForOrder.mockResolvedValue(activeOutcome);
|
|
cursorRuntime.follow.mockResolvedValue({});
|
|
|
|
const { consumeMainSystemEventsForBranch } = await import('../src/services/mainSystemEventConsumer');
|
|
const summary = await consumeMainSystemEventsForBranch({ branchCode: 'BR002' });
|
|
|
|
expect(summary.triggeredReconcile).toBe(1);
|
|
expect(cursorRuntime.lookupBatch).toHaveBeenCalledWith({ supplierCode: 'SUP_A', prepIds: ['P1'] });
|
|
expect(cursorRuntime.fetchActiveForOrder).toHaveBeenCalledWith({ supplierCode: 'SUP_A', orderCode: 'ORD_1' });
|
|
expect(cursorRuntime.follow).toHaveBeenCalledTimes(1);
|
|
expect(cursorRuntime.follow).toHaveBeenCalledWith('BR002', [activeOutcome], {
|
|
onlyMainOrderCodes: ['ORD_1'],
|
|
});
|
|
});
|
|
|
|
it('clamped 显式标记全量兜底;targeted_pull 导入失败不恒计 applied', () => {
|
|
expect(consumer).toContain('if (res.clamped)');
|
|
const clampedBlock = consumer.slice(consumer.indexOf('if (res.clamped)'), consumer.indexOf('progressed = true;', consumer.indexOf('if (res.clamped)')));
|
|
expect(clampedBlock).not.toContain('summary.needsFullReconcile = true');
|
|
expect(consumer).toContain('needsFullReconcile: res.clamped');
|
|
expect(consumer).toContain("imported.kind !== 'ok' || imported.failed > 0");
|
|
expect(consumer).toContain('不恒计 applied');
|
|
});
|
|
|
|
it('R-1 clamped 两轮:全量 follow 0 次,只落 needsFullReconcile', async () => {
|
|
vi.clearAllMocks();
|
|
let row: { cursor: string | null; last_event_at: string | null; server_time: string | null } = {
|
|
cursor: null,
|
|
last_event_at: '2026-10-20T09:59:00.000Z',
|
|
server_time: '2026-10-20T09:59:00.000Z',
|
|
};
|
|
const saved: unknown[][] = [];
|
|
cursorRuntime.query.mockImplementation(async (sql: string, params: unknown[] = []) => {
|
|
const text = String(sql);
|
|
if (/SELECT cursor, last_event_at, server_time/i.test(text)) return { rows: [{ ...row }], rowCount: 1 };
|
|
if (/INSERT INTO public\.main_system_event_cursors/i.test(text)) {
|
|
saved.push(params);
|
|
row = {
|
|
cursor: (params[2] as string | null) ?? null,
|
|
last_event_at: (params[3] as string | null) ?? null,
|
|
server_time: (params[4] as string | null) ?? null,
|
|
};
|
|
return { rows: [], rowCount: 1 };
|
|
}
|
|
return { rows: [], rowCount: 1 };
|
|
});
|
|
cursorRuntime.listSuppliers.mockResolvedValue(['SUP_A']);
|
|
cursorRuntime.getToken.mockResolvedValue({ kind: 'ok', token: 'T', branchCode: 'BR002', expiresAt: Date.now() + 60_000 });
|
|
cursorRuntime.fetchEvents.mockResolvedValue({
|
|
kind: 'ok', events: [], serverTime: '2026-10-20T10:00:00.000Z', nextCursor: null, hasMore: false, clamped: true,
|
|
});
|
|
const { consumeMainSystemEventsForBranch } = await import('../src/services/mainSystemEventConsumer');
|
|
const one = await consumeMainSystemEventsForBranch({ branchCode: 'BR002' });
|
|
const two = await consumeMainSystemEventsForBranch({ branchCode: 'BR002' });
|
|
expect((one as { needsFullReconcile?: boolean }).needsFullReconcile).not.toBe(true);
|
|
expect((two as { needsFullReconcile?: boolean }).needsFullReconcile).not.toBe(true);
|
|
expect(cursorRuntime.follow).toHaveBeenCalledTimes(0);
|
|
expect(saved).toHaveLength(2);
|
|
expect(saved.every((params) => params[5] === true)).toBe(true);
|
|
});
|
|
|
|
it('R-2 无 server_time/无事件:首轮持久化本地起点,第二轮 firstRun=false', async () => {
|
|
vi.clearAllMocks();
|
|
let row: { cursor: string | null; last_event_at: string | null; server_time: string | null } = {
|
|
cursor: null, last_event_at: null, server_time: null,
|
|
};
|
|
cursorRuntime.query.mockImplementation(async (sql: string, params: unknown[] = []) => {
|
|
const text = String(sql);
|
|
if (/SELECT cursor, last_event_at, server_time/i.test(text)) return { rows: [{ ...row }], rowCount: 1 };
|
|
if (/INSERT INTO public\.main_system_event_cursors/i.test(text)) {
|
|
row = {
|
|
cursor: (params[2] as string | null) ?? null,
|
|
last_event_at: (params[3] as string | null) ?? null,
|
|
server_time: (params[4] as string | null) ?? null,
|
|
};
|
|
return { rows: [], rowCount: 1 };
|
|
}
|
|
return { rows: [], rowCount: 1 };
|
|
});
|
|
cursorRuntime.listSuppliers.mockResolvedValue(['SUP_A']);
|
|
cursorRuntime.getToken.mockResolvedValue({ kind: 'ok', token: 'T', branchCode: 'BR002', expiresAt: Date.now() + 60_000 });
|
|
cursorRuntime.fetchEvents.mockResolvedValue({
|
|
kind: 'ok', events: [], serverTime: null, nextCursor: null, hasMore: false, clamped: false,
|
|
});
|
|
cursorRuntime.follow.mockResolvedValue({});
|
|
const { consumeMainSystemEventsForBranch } = await import('../src/services/mainSystemEventConsumer');
|
|
const first = await consumeMainSystemEventsForBranch({ branchCode: 'BR002' });
|
|
const second = await consumeMainSystemEventsForBranch({ branchCode: 'BR002' });
|
|
expect(first.initialFullReconcile).toBe(true);
|
|
expect(second.initialFullReconcile).toBeUndefined();
|
|
expect(cursorRuntime.follow).toHaveBeenCalledTimes(1);
|
|
expect(row.last_event_at).toBeTruthy();
|
|
});
|
|
|
|
it('R-3 event.supplierCode 为空:verify_order 直接 ignored,不调 batch/active', async () => {
|
|
vi.clearAllMocks();
|
|
cursorRuntime.query.mockImplementation(async (sql: string) => {
|
|
const text = String(sql);
|
|
if (/SELECT cursor, last_event_at, server_time/i.test(text)) {
|
|
return { rows: [{ cursor: null, last_event_at: '2026-10-20T09:59:00.000Z', server_time: '2026-10-20T09:59:00.000Z' }], rowCount: 1 };
|
|
}
|
|
return { rows: [], rowCount: 1 };
|
|
});
|
|
cursorRuntime.listSuppliers.mockResolvedValue(['SUP_A']);
|
|
cursorRuntime.getToken.mockResolvedValue({ kind: 'ok', token: 'T', branchCode: 'BR002', expiresAt: Date.now() + 60_000 });
|
|
cursorRuntime.fetchEvents.mockResolvedValue({
|
|
kind: 'ok',
|
|
events: [{
|
|
id: 'E1', requestId: null, event: 'preparations.returned', orderCode: 'ORD_1',
|
|
supplierCode: null, status: null, attempt: null, createdAt: '2026-10-20T10:00:00.000Z',
|
|
}],
|
|
serverTime: '2026-10-20T10:00:00.000Z', nextCursor: 'CUR_2', hasMore: false, clamped: false,
|
|
});
|
|
const { consumeMainSystemEventsForBranch } = await import('../src/services/mainSystemEventConsumer');
|
|
const summary = await consumeMainSystemEventsForBranch({ branchCode: 'BR002' });
|
|
expect(summary.ignored).toBe(1);
|
|
expect(cursorRuntime.lookupBatch).not.toHaveBeenCalled();
|
|
expect(cursorRuntime.fetchActiveForOrder).not.toHaveBeenCalled();
|
|
expect(cursorRuntime.follow).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('R-4 prep_id 查询保留 2000 安全上限、不再 slice 200', () => {
|
|
expect(consumer).toContain('LIMIT 2000');
|
|
expect(consumer).not.toContain('LIMIT 200`');
|
|
expect(consumer).toContain('.slice(0, 2000)');
|
|
expect(consumer).not.toContain('.slice(0, 200)');
|
|
});
|
|
|
|
it('binding.changed 同分支 10 分钟内重复只 ignored(源码契约)', () => {
|
|
expect(consumer).toContain('const BINDING_SYNC_INTERVAL_MS = 10 * 60 * 1000');
|
|
expect(consumer).toContain('lastBindingSyncAt.get(branchCode)');
|
|
expect(consumer).toContain("if (Date.now() - last < BINDING_SYNC_INTERVAL_MS) return 'ignored'");
|
|
});
|
|
|
|
describe('事件分类', () => {
|
|
const cases: Array<[string, string]> = [
|
|
['preparations.allocated', 'targeted_pull'],
|
|
['preparations.returned', 'verify_order'],
|
|
['order.cancelled', 'verify_order'],
|
|
['item_removed', 'verify_order'],
|
|
['reallocated', 'verify_order'],
|
|
['preparations.allocated.failed', 'verify_order'],
|
|
['binding.changed', 'binding_sync'],
|
|
['unknown.event', 'ignored'],
|
|
];
|
|
for (const [name, expected] of cases) {
|
|
it(`${name} → ${expected}`, () => {
|
|
expect(classifyEvent({ event: name, orderCode: 'ORD_X' })).toBe(expected);
|
|
});
|
|
}
|
|
});
|