Files
SupplierSystem/server/tests/independentInbound.test.ts
T
雨天andUbuntu 29fbf49481
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
feat(多模块): 代提交另一会话遗留在工作区的整批改动(内容零修改)
原改动人:雨天 <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 字节空文件,疑似误建,既未提交也未删除)
2026-10-15 11:29:15 +08:00

242 lines
14 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import path from 'node:path';
/**
* 「独立进货」(自用哨兵客户,2026-10-12 开发者立项)。
*
* 开发者口径:名字叫**独立进货**;「商品全部入库了就行,自己就完成」;
* 「注意那个主数据不卡流程,别把这种单子也强制推到那个待配送去了」;
* 「厂商应付就是我们现在的供应商结算体系,不用动」;
* 「这种单子别计入销售,因为就是纯纯的一个进货单」;成本价必填 >0。
*
* 设计:docs/design/20261012-独立进货(自用哨兵客户).md
* 关键取舍:**哨兵编码、不建 customers 行**(租户库 orders.customer_code 无外键)
* ⇒ 客户管理/绑定/预算/回款/客户结算天然看不到它,也无从删除。
*/
const svc = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
const sentinel = svc('src/services/independentInbound.ts');
const entry = svc('src/services/orderEntryScopeService.ts');
const fulfill = svc('src/services/orderWorkflowFulfillmentService.ts');
const reconcile = svc('src/services/orderStatusReconcileService.ts');
const autoAdvance = svc('src/services/orderAutoAdvanceService.ts');
const processing = svc('src/services/processingTaskService.ts');
const picking = svc('src/services/pickingService.ts');
const finance = svc('src/services/financeReconciliationService.ts');
describe('① 哨兵客户:只在录单出现,不建客户档案', () => {
it('编码/名称固定,且只落在订单上(不写入 customers)', () => {
expect(sentinel).toContain("INDEPENDENT_INBOUND_CUSTOMER_CODE = '__INDEPENDENT_INBOUND__'");
expect(sentinel).toContain("INDEPENDENT_INBOUND_CUSTOMER_NAME = '独立进货'");
// 本模块不得出现对 customers 表的写
expect(sentinel).not.toMatch(/INSERT INTO\s+customers/i);
expect(sentinel).not.toMatch(/UPDATE\s+customers/i);
});
it('录单校验放行哨兵,但仍要求录单员绑定分公司', () => {
const idx = entry.indexOf('export async function assertOrderEntryCustomerAccess');
const body = entry.slice(idx, idx + 900);
expect(body).toContain('isIndependentInboundCustomer(customerCode)');
expect(body).toContain("code: 'invalid_branch'");
// 哨兵必须在查 customers 之前短路(否则会被判「客户不存在」)
expect(body.indexOf('isIndependentInboundCustomer(customerCode)')).toBeLessThan(
body.indexOf('FROM customers')
);
});
});
describe('② 全部入库 ⇒ 直接完成(90),且绝不被推到待配送', () => {
it('主数据不卡流程(定时强推)对这类单直接 skip', () => {
const idx = autoAdvance.indexOf('export async function autoAdvanceOrderToDelivery');
const body = autoAdvance.slice(idx, idx + 3000);
expect(body).toContain('isOrderIndependentInbound');
expect(body).toContain('独立进货单,跳过定时强推');
});
it('验收完成钩子:齐套 ⇒ 结案;不齐套 ⇒ 不推进', () => {
const idx = processing.indexOf('export async function maybeAdvanceOrderAfterFulfillment');
const body = processing.slice(idx, idx + 2000);
expect(body).toContain('isOrderIndependentInbound');
expect(body).toContain('completeOrderWithoutDriverDelivery');
expect(body).toContain('独立进货:全部入库,订单直接完成');
// 该分支必须在 45/50 推进逻辑之前
expect(body.indexOf('isOrderIndependentInbound')).toBeLessThan(body.indexOf('isPickingWorkflowEnabled'));
});
it('15 分钟对账:哨兵单走单列分支并提前 return(不进 45/50/60 链)', () => {
const idx = reconcile.indexOf('if (await isOrderIndependentInbound(code, pool))');
expect(idx).toBeGreaterThan(-1);
const body = reconcile.slice(idx, idx + 1200);
expect(body).toContain('fetchOrderInspectionState');
expect(body).toContain('尚未全部入库,保持当前状态');
expect(body).toContain('return {');
// 提前 return 必须早于配送/分拣推进调用
expect(idx).toBeLessThan(reconcile.indexOf('tryAdvanceOrderToDelivery(code)'));
});
it('结案入口:独立进货允许从 40 待验收结案(普通单仍只允许 30/50)', () => {
const idx = fulfill.indexOf('const fromInspectionForIndependentInbound');
expect(idx).toBeGreaterThan(-1);
expect(fulfill.slice(idx, idx + 400)).toContain("status === 40 && (await isOrderIndependentInbound(orderCode, db))");
});
it('不补核货/拣货行(skip-inbound 补行入口对哨兵单直接返回)', () => {
const skip = svc('src/services/skipInboundLoadingService.ts');
const idx = skip.indexOf('export async function ensureSkipInboundLoadingLines');
expect(idx).toBeGreaterThan(-1);
const body = skip.slice(idx, idx + 6000);
expect(body).toContain('await isOrderIndependentInbound(code, db)');
// 必须在真正建行之前返回(函数体里第一处写 picking_items 的 SQL)
const firstInsert = body.indexOf('INSERT INTO picking_items');
expect(firstInsert).toBeGreaterThan(-1);
expect(body.indexOf('isOrderIndependentInbound')).toBeLessThan(firstInsert);
});
it('不生成拣货任务(候选查询排除哨兵单)', () => {
const idx = picking.indexOf('export async function syncMissingPickingTasks');
expect(idx).toBeGreaterThan(-1);
expect(picking.slice(idx, idx + 3000)).toContain("salesVisibleOrderSql('o')");
});
});
describe('②b 入库优先级(开发者裁决):独立进货必须入库,普通单照旧', () => {
const prep = svc('src/services/preparationFulfillmentService.ts');
it('建待验:哨兵单忽略"免入库厂商/商品直送"两个跳过集合', () => {
const idx = prep.indexOf('export async function ensureInspectionsForOrder');
const body = prep.slice(idx, idx + 3000);
expect(body).toContain('const independentInboundOrder = await isOrderIndependentInbound(orderCode, db)');
expect(body).toMatch(/independentInboundOrder\s*\?\s*new Set<string>\(\)\s*:\s*await loadSkipInboundSupplierSet\(/);
expect(body).toMatch(/independentInboundOrder\s*\?\s*new Set<string>\(\)\s*:\s*await loadDirectDeliverySkuSetForOrder\(/);
// 普通单仍走原来的两个集合
expect(body).toContain('loadSkipInboundSupplierSet(');
expect(body).toContain('loadDirectDeliverySkuSetForOrder(');
});
it('建待验的**内部**免入库判断也要对哨兵单让路(否则夹具实测 A 单也不建待验)', () => {
const reject = svc('src/services/inspectionRejectFulfillmentService.ts');
const idx = reject.indexOf('export async function createSupplierInspectionIfNeeded');
expect(idx).toBeGreaterThan(-1);
const body = reject.slice(idx, idx + 3000);
expect(body).toMatch(/!\(await isOrderIndependentInbound\(input\.orderCode, client\)\)\s*&&\s*\n?\s*\(await isSupplierSkipInbound\(input\.supplierCode, client\)\)/);
// 普通单仍按免入库标志拦(该谓词必须还在)
expect(body).toContain('isSupplierSkipInbound(input.supplierCode, client)');
});
it('30→40 覆盖判定:哨兵单不吃"免入库/直送"两条过滤,普通单原样', () => {
const idx = prep.indexOf('const independentInboundOrder = await isOrderIndependentInbound(orderCode, db);', prep.indexOf('export async function tryAdvanceOrderToInspection'));
expect(idx).toBeGreaterThan(-1);
const body = prep.slice(idx, idx + 3500);
// 哨兵单:条件整体为空 ⇒ 该行必须靠验收覆盖才能算齐套
expect(body).toMatch(/const inboundCoverageFilters = independentInboundOrder\s*\n?\s*\? ''/);
// 普通单:原样两条过滤(另有 supplierSkipInbound 契约测试钉住字面量)
expect(body).toContain("AND NOT ${supplierSkipInboundExistsSql('oi.supplier_code')}");
expect(body).toContain("AND NOT ${skuDirectDeliveryAppliesSql('oi.sku_code', 'oi.order_code')}");
// 覆盖判定 SQL 里用的是这个 fragment
expect(body).toContain('${inboundCoverageFilters}');
});
});
describe('③ 结算:供应商结算不动,客户结算不生成', () => {
it('客户结算入口(唯一实现)对哨兵单直接 skipped', () => {
const idx = finance.indexOf('async function runUpsertCustomerStatementForOrder');
const body = finance.slice(idx, idx + 800);
expect(body).toContain('isOrderIndependentInbound(code, db)');
expect(body).toContain("return 'skipped'");
});
it('供应商结算未被本功能改动(仍按 qty × unit_cost)', () => {
expect(finance).toContain('ensureSupplierStatementsForOrder');
expect(sentinel).not.toMatch(/supplier_statement/i);
});
it('成本价**不强制**(开发者修订):结案口不再拦 0 成本', () => {
expect(fulfill).not.toContain('independent_inbound_cost_required');
});
});
describe('③b 结构上零销售:录单写 0 售价、零价闸门对哨兵放行', () => {
const submit = svc('src/services/customerOrderSubmitService.ts');
const alloc = svc('src/services/orderAllocationService.ts');
it('录单落库:哨兵单 price/amount 写 0(成本价照旧)', () => {
expect(submit).toContain('revenuePrice = independentInbound ? 0 : price');
expect(submit).toContain('revenueAmount = independentInbound ? 0 : amount');
expect(submit).toContain('independentInbound ? false : price <= 0');
});
it('提交/分配/结案三处零价闸门:仅对哨兵单放行', () => {
const submitIdx = submit.indexOf('const independentInboundOrder = isIndependentInboundCustomer');
expect(submitIdx).toBeGreaterThan(-1);
expect(submit.slice(submitIdx, submitIdx + 400)).toContain('input.allowUnpriced === true || independentInboundOrder');
expect(alloc).toContain('await isOrderIndependentInbound(orderCode, client)');
expect(fulfill).toContain('await isOrderIndependentInbound(orderCode, db)');
});
it('信用校验与客户×商品映射学习:哨兵跳过(真跑撞到过)', () => {
expect(submit).toContain('isIndependentInboundCustomer(input.customerCode)');
expect(submit).toContain('__skip');
});
});
describe('②c 禁止库存抵扣(开发者:进货单不能被库存抵扣,按钮也取消)', () => {
it('服务端唯一写入点对哨兵单返回 0 ⇒ 录单后的自动抵扣/手工抵扣都不生效', () => {
const cover = svc('src/services/inventoryStockCoverService.ts');
const idx = cover.indexOf('export async function applyStockCoverForOrderItem');
expect(idx).toBeGreaterThan(-1);
/**
* 窗口取函数体开头一段(**不是**全函数):本用例只校验「哨兵守卫」与
* 「已入库/已拣货守卫」的**相对先后**,两者都在函数前段。
* ⚠️ 原为 1200,而 `loadLockedStockCoverIfInboundOrPicking` 调用点当时已在 1104 ⇒ **只剩 96 字符余量**;
* 2026-10-14 给同函数加「抵 0 原因收集」参数后涨到 1168,**窗口在标识符中间截断**(`…IfInbo`)
* ⇒ `indexOf` 得 -1、判据失效。放宽到 2000 保留同一语义(仍远早于真正的抵扣计算)。
* ⚠️ 同日又插入一条「已退货行归零」守卫(**有意**排在该守卫之前)⇒ 锚点推到 2657,
* 2000 再次截断 ⇒ 再放宽到 6000。**这类固定字符窗口已是第三次被撑破**,
* 改小前务必先确认两个锚点仍在窗口内(实测偏移:见本用例注释末),
* 否则会出现「截断 ⇒ -1 ⇒ 假失败/假通过」。
*/
const body = cover.slice(idx, idx + 6000);
expect(body).toContain('await isOrderIndependentInbound(orderCode, db)');
expect(body).toContain('return 0;');
// 必须在真正算抵扣之前(守卫之前只有入参清洗)
expect(body.indexOf('isOrderIndependentInbound')).toBeLessThan(body.indexOf('loadLockedStockCoverIfInboundOrPicking'));
});
});
describe('③c 信用校验:修在**最底层**函数上(否则录单页的预检接口仍报"客户不存在")', () => {
const budget = svc('src/services/customerBudgetService.ts');
it('checkCustomerCredit 对哨兵直接返回 allowed(覆盖所有包装/接口)', () => {
const idx = budget.indexOf('export async function checkCustomerCredit');
expect(idx).toBeGreaterThan(-1);
const body = budget.slice(idx, idx + 1600);
expect(body).toContain('isIndependentInboundCustomer(customerCode)');
expect(body).toContain("level: 'ok'");
// 必须在查 customers 之后仍无条件放行(真实客户逻辑不变)
expect(body).toContain('FROM customers WHERE customer_code = $1');
// 冻结口径:真实客户仍只有"不存在"才 block
expect(budget).toContain('不存在,无法下单');
});
it('录单页提交前预检接口链路存在(GET /finance/customer/:code/can-order → checkCustomerOrderBudget)', () => {
const routes = svc('src/modules/workflow/routes/financeRoutes.ts');
expect(routes).toContain("'/finance/customer/:customerCode/can-order'");
expect(routes).toContain('checkCustomerOrderBudget');
expect(budget).toContain('export async function checkCustomerOrderBudget');
});
});
describe('④ 不计入销售(口径谓词统一,成本口径不动)', () => {
it('统一谓词来自 new 模块', () => {
expect(sentinel).toContain('export function salesVisibleOrderSql');
// 模板里内插常量:`${alias}.customer_code IS DISTINCT FROM '${INDEPENDENT_INBOUND_CUSTOMER_CODE}'`
expect(sentinel).toContain("customer_code IS DISTINCT FROM '${INDEPENDENT_INBOUND_CUSTOMER_CODE}'");
});
it('已排除:看板 KPI、客户返利、SKU 毛利/排行', () => {
expect(svc('src/services/dashboardService.ts')).toContain('salesVisibleOrderSql');
expect(svc('src/services/customerRebateService.ts')).toContain('salesVisibleOrderSql');
expect(svc('src/services/marginQueryService.ts')).toContain('salesVisibleOrderSql');
});
});