130 lines
6.6 KiB
TypeScript
130 lines
6.6 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'
|
|
);
|
|
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('首轮不传 since/cursor(服务端默认窗口)+ 首轮做一次初态全量对账', () => {
|
|
expect(consumer).toContain('const firstRun = !stored.cursor && !stored.lastEventAt');
|
|
expect(consumer).toContain('let since = firstRun ? null : stored.lastEventAt || bootstrapSince()');
|
|
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('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);
|
|
});
|
|
}
|
|
});
|