95 lines
5.0 KiB
TypeScript
95 lines
5.0 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
/**
|
||
* 批次报告推送改「事件驱动队列」(2026-10-24 开发者:
|
||
* 「肯定不能每次推送都扫描全部呀 应该是存在改动变化的才会进行推送 而不是全盘扫描 且 优化也需要做」)。
|
||
*
|
||
* 口径:
|
||
* · 变化点入队(报告绑定/替换/删除、导入建单/追加)→ worker 只按精确目标(inventory_id / our_order_code)推送;
|
||
* · 全量扫描降级为**每日一次兜底**;
|
||
* · 队列处理有进程内在途互斥;队列行更新必须显式定型(真机踩过 `inconsistent types deduced for parameter $2`)。
|
||
*/
|
||
const queue = readFileSync(new URL('../src/services/reportPushQueueService.ts', import.meta.url), 'utf8');
|
||
const push = readFileSync(new URL('../src/services/mainSystemReportPushService.ts', import.meta.url), 'utf8');
|
||
const report = readFileSync(new URL('../src/services/inventoryBatchReportService.ts', import.meta.url), 'utf8');
|
||
const scheduler = readFileSync(new URL('../src/services/scheduledJobs.ts', import.meta.url), 'utf8');
|
||
const importer = readFileSync(new URL('../src/services/mainSystemOrderImport.ts', import.meta.url), 'utf8');
|
||
const picking = readFileSync(new URL('../src/services/pickingService.ts', import.meta.url), 'utf8');
|
||
const fulfillment = readFileSync(new URL('../src/services/orderWorkflowFulfillmentService.ts', import.meta.url), 'utf8');
|
||
const outbound = readFileSync(new URL('../src/services/inventoryOutboundService.ts', import.meta.url), 'utf8');
|
||
const isolationMigration = readFileSync(
|
||
new URL('../../migrations/20261076_report_push_queue_branch_isolation.sql', import.meta.url),
|
||
'utf8'
|
||
);
|
||
const migration = readFileSync(
|
||
new URL('../../migrations/20261075_report_push_queue_and_indexes.sql', import.meta.url),
|
||
'utf8'
|
||
);
|
||
|
||
describe('报告推送队列', () => {
|
||
it('队列表 + 索引迁移存在(含高频过滤列)', () => {
|
||
expect(migration).toContain('main_system_report_push_queue');
|
||
expect(migration).toContain('idx_inventory_logs_order_sku_batch');
|
||
expect(migration).toContain('idx_picking_items_order_item');
|
||
expect(migration).toContain('idx_order_items_branch_sku');
|
||
expect(migration).toContain('idx_batch_report_matches_batch_no');
|
||
});
|
||
|
||
it('入队幂等(唯一 pending)+ 处理在途互斥', () => {
|
||
expect(queue).toContain('ON CONFLICT DO NOTHING');
|
||
expect(queue).toContain('let queueRunning = false;');
|
||
expect(queue).toContain('if (queueRunning) return { claimed: 0');
|
||
});
|
||
|
||
it('worker 只按精确目标收候选(不再全量扫)', () => {
|
||
expect(push).toContain('export async function collectReportPushCandidatesScoped');
|
||
expect(push).toContain('($3::uuid IS NULL OR m.inventory_id = $3::uuid)');
|
||
expect(push).toContain('($4::text IS NULL OR oi.order_code = $4)');
|
||
expect(queue).toContain('collectReportPushCandidatesScoped');
|
||
});
|
||
|
||
it('报告变化点全部入队:绑定 / 补挂 / 替换 / 删除(删除走 void)', () => {
|
||
const enqueues = report.match(/enqueueReportPush(Many)?\(/g) || [];
|
||
expect(enqueues.length).toBeGreaterThanOrEqual(3);
|
||
expect(report).toContain("kind: 'void' as const");
|
||
});
|
||
|
||
it('导入建单/追加后入队(新单消耗已有报告批次也能立刻推)', () => {
|
||
expect(importer).toContain("enqueueReportPush({");
|
||
expect(importer).toContain('ourOrderCode: result.order_code');
|
||
});
|
||
|
||
it('调度:平时只处理队列,全量扫描降为每日一次兜底', () => {
|
||
expect(scheduler).toContain("import('./reportPushQueueService')");
|
||
expect(scheduler).toContain('processReportPushQueueForAllBranches(50)');
|
||
expect(scheduler).toContain('lastReportFullSweepDay');
|
||
});
|
||
|
||
it('队列行更新显式定型(防 parameter 类型歧义导致永不更新)', () => {
|
||
expect(queue).toContain("SET status = $2::varchar");
|
||
expect(queue).toContain("CASE WHEN $2::text = 'done'");
|
||
});
|
||
|
||
it('候选来源也会入队:分拣保存 / 分拣完成出库 / 发车出库(核查项 1)', () => {
|
||
expect(picking).toContain("reason: '分拣保存/批次计划更新'");
|
||
expect(fulfillment).toContain('分拣完成出库(picking_out)');
|
||
expect(outbound).toContain('发车核货出库(delivery_loading_out)');
|
||
});
|
||
|
||
it('僵尸任务收口:重试用尽 → failed 并告警(核查项 3)', () => {
|
||
expect(queue).toContain('export async function failExhaustedReportPushTasks');
|
||
expect(queue).toContain("SET status = 'failed'");
|
||
expect(queue).toContain('重试已用尽');
|
||
});
|
||
|
||
it('按分公司处理队列 + 分支隔离 RLS(核查项 6)', () => {
|
||
expect(queue).toContain('export async function processReportPushQueueForAllBranches');
|
||
expect(queue).toContain('runWithTenantDbContext');
|
||
expect(scheduler).toContain('processReportPushQueueForAllBranches(50)');
|
||
expect(isolationMigration).toContain('ENABLE ROW LEVEL SECURITY');
|
||
expect(isolationMigration).toContain('FORCE ROW LEVEL SECURITY');
|
||
expect(isolationMigration).toContain("current_setting('app.branch_code', true)");
|
||
});
|
||
});
|