import { readFileSync } from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; /** * 「占坑未取得」不得静默丢单(2026-10-18 真机 `ORD_WEB_20261018_BR002_0002`: * 一次建单中断留下空坑 ⇒ 10 分钟内该配送日所有推送被跳过且返回 200,主系统记成功、我们没导入)。 */ const importer = readFileSync(path.join(__dirname, '../src/services/mainSystemOrderImport.ts'), 'utf8'); const routes = readFileSync(path.join(__dirname, '../src/routes/mainSystemIntegration.ts'), 'utf8'); describe('① 空坑可重占窗口:10 分钟 → 2 分钟', () => { it('占坑 CAS 用 2 分钟陈旧判定', () => { expect(importer).toMatch(/created_at < NOW\(\) - INTERVAL '2 minutes'/); // 行内注释保留历史说明(10 分钟 → 2 分钟),只看 SQL 那一行 const sqlLines = importer.split('\n').filter((l) => /main_system_daily_order_imports\.created_at < NOW\(\)/.test(l)); expect(sqlLines.join('\n')).not.toMatch(/INTERVAL '10 minutes'/); }); it('陈旧空坑被重占时必须刷新 created_at(否则第二个并发轮次会再次抢到同一坑 ⇒ 重复建单)', () => { const start = importer.indexOf('async function claimDayOrder'); expect(start).toBeGreaterThan(-1); const end = importer.indexOf('/**\n * 整单台账', start); const fn = importer.slice(start, end); const upsert = fn.slice(fn.indexOf('ON CONFLICT'), fn.indexOf('RETURNING')); // 只改 updated_at 不够:并发轮次等锁后看到 created_at 仍是旧值会再次通过 CAS。 expect(upsert).toMatch(/created_at = NOW\(\)/); }); }); describe('② 对方能感知"本轮没导入":503 + Retry-After', () => { it('占坑未取得 ⇒ 503 retryable(不再 200 当成功)', () => { expect(routes).toMatch(/import_in_flight/); expect(routes).toMatch(/Retry-After/); expect(routes).toMatch(/status\(503\)/); expect(routes).toMatch(/retryable: true/); }); });