原改动人:雨天 <oncwnuHbbvAwYZMdPF78PsWNPadI@git.weixin.qq.com> 代为提交:Lead 会话(本 commit 内容与该会话工作区逐字节一致,未做任何修改), 目的是不让在研成果丢失、并让工作树回到干净可部署状态。 提交前由 Lead 实跑验证: - 前端 npx tsc --noEmit 0 错误;后端 server npx tsc --noEmit 0 错误 - 本批新增的 7 个测试 54/54 通过;Lead 侧契约测试(散客录单/文案/成本列)42/42 通过 - bash tools/design-consistency/run-all.sh --verify:通过 8 / 未通过 1(check-ai-cost,既有红)/ 未执行 1(verify-facts,本地库不可连)—— 与基线一致 新增能力(各含 docs/design 设计文): - 独立进货不得有配送任务:生成侧兜底 + 补建候选 + 查询侧三道防线(实测 5 张独立进货单里 4 张有僵尸 delivery_tasks) - 主系统追加行零价闸门尊重其已声明的 allowUnpriced(否则整批分配行静默丢失) - 库存抵扣为 0 的原因可见(不再静默,原因判定与抵扣量同源并落到日志与「库存抵扣」入口) - 退货/删单跟随改独立 2 分钟周期(MAIN_SYSTEM_RETURN_FOLLOW_SEC,置 0 退回旧行为) - 加工领料可用本单自己的预留(放行必须与本单预留消费成对,避免短送与同一批算两次) - 出库拍照预览、主系统实收 / SKU 映射 / 单位变更 inbox 等配套 同一批包含对已废弃旧方案的回滚(design doc 与 README 索引行一并移除): - 独立进货建客户档案(方案甲)→ 回到哨兵方案;随之删除 migrations/20261044_independent_inbound_customer.sql - 分类新建去重(同名复用已有) - 分拣标签纸张尺寸预设 / 分拣标签可选客户名与区域 未包含:src/pages/orders/OrderManagementPage.tsx(0 字节空文件,疑似误建,既未提交也未删除)
151 lines
6.9 KiB
TypeScript
151 lines
6.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')") ? i : -1))
|
||
.filter((i) => i >= 0);
|
||
// 列表 / 司机列表 / 司机待办计数 / 配送员统计 共 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*<>/);
|
||
});
|
||
});
|