- 下单即建分拣/配送,待取货/散客/独立进货守卫 - 30/40/45 直接发车/分拣,入库与分拣二选一 - 分拣完成补上游、清理 pending 验收,直接配送补分拣 - 库存抵扣/计划/实收 FIFO,软扣部分扣减,并发出库串行 - 结束配送不被库存不足/待入库残留阻断 - 待验收并发唯一索引(含 name-only),finalize 共享连接修复 - 售后自动绑定订单明细、客户取货/采购并发修复 - 仓库表格自适应、弹窗滚动锁闪烁修复
176 lines
7.9 KiB
TypeScript
176 lines
7.9 KiB
TypeScript
/**
|
||
* 独立进货单不得有配送任务(2026-10-14 开发者报障)
|
||
*
|
||
* 报障:「独立进货的还会存在配送单」——`ORD_WEB_20261014_BR002_0002`(哨兵客户
|
||
* `__INDEPENDENT_INBOUND__`)在配送管理里显示成待配送的「配送单」。
|
||
*
|
||
* 真库实测(BR002 + BR003 共 5 张独立进货单,4 张有 delivery_tasks、task_status=10):
|
||
* · 建单侧:`ensureDeliveryTaskForOrder` 原有「商品直送」「整单免入库」两道守卫,
|
||
* **独缺第三类独立进货** ⇒ 从两道守卫中间穿过;
|
||
* · 补建侧:独立进货单入库即 90,天然满足 `order_status >= 50` 的补建候选 ⇒
|
||
* 每次打开配送管理页都会被"补建"一个配送任务。
|
||
*
|
||
* 本测试钉住三层修复的每一层,并对每层给出「注入旧码必须 FAIL」的反向验证。
|
||
* 见 docs/design/20261014-独立进货不得有配送任务.md
|
||
*/
|
||
import { readFileSync } from 'fs';
|
||
import { describe, expect, it, vi } from 'vitest';
|
||
|
||
vi.mock('../src/db', () => ({
|
||
pool: { query: vi.fn(), connect: vi.fn() },
|
||
getRawPoolQuery: () => vi.fn(),
|
||
}));
|
||
|
||
import { salesVisibleOrderSql } from '../src/services/independentInbound';
|
||
|
||
const fulfillmentSource = readFileSync(
|
||
new URL('../src/services/orderWorkflowFulfillmentService.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
const queryServiceSource = readFileSync(
|
||
new URL('../src/services/deliveryTaskQueryService.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
|
||
/** 按「下一个 export 声明」切出函数体,避免依赖固定字符窗口(函数一长就会失准) */
|
||
function functionBody(source: string, name: string): string {
|
||
const start = source.indexOf(`export async function ${name}`);
|
||
expect(start, `${name} 应存在`).toBeGreaterThanOrEqual(0);
|
||
const rest = source.slice(start + 1);
|
||
const nextAsync = rest.indexOf('export async function');
|
||
const nextSync = rest.indexOf('export function');
|
||
const candidates = [nextAsync, nextSync].filter((n) => n >= 0);
|
||
const next = candidates.length ? Math.min(...candidates) : -1;
|
||
return next >= 0 ? source.slice(start, start + 1 + next) : source.slice(start);
|
||
}
|
||
|
||
describe('第 1 层:生成侧兜底(ensureDeliveryTaskForOrder)', () => {
|
||
const body = functionBody(fulfillmentSource, 'ensureDeliveryTaskForOrder');
|
||
|
||
it('含独立进货守卫,且复用既有 isOrderIndependentInbound', () => {
|
||
expect(body).toContain('isOrderIndependentInbound(code');
|
||
});
|
||
|
||
it('守卫在 pool.connect() 之前返回(否则池满时自锁)', () => {
|
||
const guardAt = body.indexOf('isOrderIndependentInbound(code');
|
||
// 必须匹配真正的调用而非注释里的同名文本(注释里也写了 `pool.connect()`)
|
||
const connectAt = body.indexOf('await pool.connect()');
|
||
expect(guardAt).toBeGreaterThanOrEqual(0);
|
||
expect(connectAt).toBeGreaterThanOrEqual(0);
|
||
expect(guardAt).toBeLessThan(connectAt);
|
||
});
|
||
|
||
it('守卫命中即返回空串(不建任务),与既有两道守卫同形', () => {
|
||
const line = body.split('\n').find((l) => l.includes('isOrderIndependentInbound(code'))!;
|
||
expect(line).toContain("return ''");
|
||
// 三道守卫都要在同一个 `existingClient || pool` 口径上判定
|
||
expect(line).toContain('existingClient || pool');
|
||
});
|
||
|
||
it('反向验证:去掉这一行守卫 ⇒ 断言失败(注入旧码必须 FAIL)', () => {
|
||
const injected = body
|
||
.split('\n')
|
||
.filter((l) => !l.includes('isOrderIndependentInbound(code'))
|
||
.join('\n');
|
||
expect(injected).not.toContain('isOrderIndependentInbound(code');
|
||
// 原实现里 guard 与 return 是同一行;注入后该行消失,正是本次缺陷的形态
|
||
expect(injected).toContain('isOrderOnlyDirectDelivery(code');
|
||
});
|
||
|
||
it('未分配任务自动派单(syncUnassignedDeliveryTasks)也不得捞独立进货单', () => {
|
||
// 该函数是定时任务写路径:给「无配送员且 order_status>=50」的任务自动派单。
|
||
// 独立进货单未删且状态恒 90,恰好满足旧判据 ⇒ 一旦库里有残留任务就会被派成真任务。
|
||
const syncBody = functionBody(fulfillmentSource, 'syncUnassignedDeliveryTasks');
|
||
expect(syncBody).toContain("salesVisibleOrderSql('o')");
|
||
expect(syncBody).toContain('INNER JOIN orders o');
|
||
expect(syncBody).toContain('order_status >= 50');
|
||
});
|
||
});
|
||
|
||
describe('第 2 层:配送任务补建候选', () => {
|
||
for (const fn of ['ensureDeliveryTasksForDeliveryDay', 'ensureMissingDeliveryTasksForBranch']) {
|
||
it(`${fn} 的候选订单排除独立进货`, () => {
|
||
const body = functionBody(queryServiceSource, fn);
|
||
expect(body).toContain("salesVisibleOrderSql('o')");
|
||
expect(body).toContain('FROM orders o');
|
||
expect(body).toContain('order_status, 0) >= 50');
|
||
});
|
||
|
||
it(`反向验证:${fn} 去掉谓词 ⇒ 只剩状态判据(即缺陷形态)`, () => {
|
||
const body = functionBody(queryServiceSource, fn);
|
||
const injected = body
|
||
.split('\n')
|
||
.filter((l) => !l.includes("salesVisibleOrderSql('o')"))
|
||
.join('\n');
|
||
expect(injected).not.toContain('salesVisibleOrderSql');
|
||
expect(injected).toContain('order_status, 0) >= 50');
|
||
});
|
||
}
|
||
});
|
||
|
||
describe('第 3 层:面向人的配送查询', () => {
|
||
it('每一个 orderVisibleToDeliverySql 落点都紧跟独立进货谓词', () => {
|
||
const lines = queryServiceSource.split('\n');
|
||
const sites = lines
|
||
.map((l, i) => (
|
||
l.includes("orderVisibleToDeliverySql('o')") ||
|
||
l.includes("orderVisibleToDeliveryManagementSql('o')") ? i : -1
|
||
))
|
||
.filter((i) => i >= 0);
|
||
// 管理列表(management 包装)/ 司机列表 / 司机待办计数 / 配送员统计 共 4 处
|
||
expect(sites.length).toBeGreaterThanOrEqual(4);
|
||
for (const i of sites) {
|
||
const nearby = lines.slice(i, i + 3).join('\n');
|
||
expect(nearby, `第 ${i + 1} 行落点缺独立进货谓词`).toContain("salesVisibleOrderSql('o')");
|
||
}
|
||
});
|
||
|
||
it('反向验证:删掉全部谓词 ⇒ 每个落点都不再排除独立进货', () => {
|
||
const injected = queryServiceSource
|
||
.split('\n')
|
||
// 连同 import 行一起去掉,否则残留的 import 名会让断言失去意义
|
||
.filter((l) => !l.includes('salesVisibleOrderSql'))
|
||
.join('\n');
|
||
expect(injected).not.toContain('salesVisibleOrderSql');
|
||
expect(injected).toContain("orderVisibleToDeliverySql('o')");
|
||
});
|
||
});
|
||
|
||
describe('判据本身:IS DISTINCT FROM 而非 <>(NULL 安全)', () => {
|
||
it('salesVisibleOrderSql 必须 NULL 安全', () => {
|
||
const sql = salesVisibleOrderSql('o');
|
||
expect(sql).toContain('IS DISTINCT FROM');
|
||
expect(sql).toContain('__INDEPENDENT_INBOUND__');
|
||
// `<>` 会因为 customer_code IS NULL 把整批真实客户订单滤掉
|
||
expect(sql).not.toMatch(/o\.customer_code\s*<>/);
|
||
});
|
||
|
||
it('反向验证:换成 <> 的写法必须被本断言拒绝', () => {
|
||
const bad = `o.customer_code <> '__INDEPENDENT_INBOUND__'`;
|
||
expect(bad).not.toContain('IS DISTINCT FROM');
|
||
expect(bad).toMatch(/o\.customer_code\s*<>/);
|
||
});
|
||
});
|
||
|
||
describe('独立进货/散客录单不得有分拣任务', () => {
|
||
const pickingSource = readFileSync(
|
||
new URL('../src/services/pickingService.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
|
||
it('createPickingTask 唯一入口同时拦散客录单与独立进货', () => {
|
||
const body = functionBody(pickingSource, 'createPickingTask');
|
||
expect(body).toContain('isRetailOrder(orderCode, pool)');
|
||
expect(body).toContain('isOrderIndependentInbound(orderCode, pool)');
|
||
});
|
||
|
||
it('独立进货守卫在取数据库连接前返回,避免池满自锁', () => {
|
||
const body = functionBody(pickingSource, 'createPickingTask');
|
||
const guardAt = body.indexOf('isOrderIndependentInbound(orderCode, pool)');
|
||
const connectAt = body.indexOf('await pool.connect()');
|
||
expect(guardAt).toBeGreaterThanOrEqual(0);
|
||
expect(connectAt).toBeGreaterThanOrEqual(0);
|
||
expect(guardAt).toBeLessThan(connectAt);
|
||
});
|
||
});
|