Files
SupplierSystem/server/tests/mainSystemEvents.test.ts
T
Ubuntu 0f25a92fec
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
feat(主系统对接): 接入 5 项配合——事件补账消费(游标+去重)、备货 cursor/order_code 定向拉取、批量状态、批量撤报告;接收端确认已在生产;全量拉取降为30分钟
2026-10-24 13:47:05 +08:00

103 lines
5.1 KiB
TypeScript

import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { classifyEvent } from '../src/services/mainSystemEventConsumer';
/**
* 主系统 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'
);
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('事件消费纪律:server_time 起点 + next_cursor 持久化 + 翻页到 has_more=false + 去重', () => {
expect(consumer).toContain('bootstrapSince');
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()');
});
});
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);
});
}
});