Files
SupplierSystem/server/tests/inboundCoverageAfterLineReturn.test.ts
T
Ubuntu deb3e72750
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
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

126 lines
6.4 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 { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import {
inboundCoverageVoidedSql,
inventoryLogVoidedByReturnSql,
notVoidedByLineReturnSql,
prepInboundVoidedSql,
selfPurchaseInboundVoidedSql,
} from '../src/services/inboundCoverageSql';
/**
* 「被整行退货冲销的入库,不得再当作本商品已入库的依据」。
*
* 背景(2026-09-22,BR004 ORD_WEB_20260922_BR004_0001 实证):退货红冲只在行级发生
* (order_items.is_returned + order_item_returns),验收行按设计保留;而多处判定用
* order_code + sku_code,于是同 SKU 新加的行继承了被红冲行的入库(详情「入库完成」、
* 自采单被关到 30、加工单净重沿用旧值、新单一条待验都不生成)。
* 见 docs/design/20260922-inbound-attribution-after-order-item-return.md
*/
const readSrc = (rel: string) => readFileSync(new URL(`../src/${rel}`, import.meta.url), 'utf8');
describe('inboundCoverageSql 片段语义', () => {
it('自采路径按「采购单 → 订单行」精确归属,并要求该行已红冲', () => {
const sql = selfPurchaseInboundVoidedSql('i');
expect(sql).toContain('FROM purchase_orders po');
expect(sql).toContain('po.source_id');
expect(sql).toContain('FROM order_item_fulfillments f');
expect(sql).toContain('COALESCE(roi.is_returned, false) = true');
expect(sql).toContain("i.processing_details->>'purchase_code'");
expect(sql).toContain("processing_details->'purchase_codes'");
});
it('性能红线:不得使用非 sargable 的字符串转型连接,且归属集合只算一次', () => {
// 2026-09-22 线上事故:`roi.id::text = po.source_id` 让每次求值退化成嵌套全扫,
// 50 单的列表查询 >60s(线上 350–450s,56 个会话卡死)。
for (const frag of [
selfPurchaseInboundVoidedSql('i'),
prepInboundVoidedSql('i'),
inventoryLogVoidedByReturnSql('il'),
]) {
expect(frag).not.toMatch(/id::text\s*=/);
expect(frag).not.toMatch(/::text\s*=\s*\w+\./);
}
// 归属集合走「非相关子查询 + IN」→ Postgres 只算一次并哈希
const sql = selfPurchaseInboundVoidedSql('i');
expect(sql).toContain('IN (');
expect(sql).toMatch(/SELECT po\.purchase_code/);
});
it('NULL 陷阱:无 purchase_code 时也必须返回确定布尔(否则 NOT(...) 会过滤掉整行)', () => {
const sql = selfPurchaseInboundVoidedSql('i');
// x IN (子查询) 在 x 为 NULL 时求值为 NULL;调用方写的是 NOT (本片段),
// NULL 会让整行被 WHERE 过滤 → BR002 实测数百行误判为"未入库"
expect(sql).toContain('COALESCE(');
expect(sql).toMatch(/IN \(\s*\n?\s*\(?\s*SELECT po\.purchase_code/);
});
it('备货路径按身份 + 退货时点,并带「退货当时还有别的在用行就不排除」护栏', () => {
const sql = prepInboundVoidedSql('i');
expect(sql).toContain('FROM order_item_returns r');
expect(sql).toContain('r.created_at >= i.inspection_time');
expect(sql).toContain('i.inspection_time IS NOT NULL');
// 护栏:BR002/BR003 实测存在「同单+同供应商+同SKU 多行」,不能误伤
expect(sql).toContain('FROM order_items other');
expect(sql).toContain('COALESCE(other.is_returned, false) = false');
expect(sql).toContain('other.created_at <= r.created_at');
// 供应商等价口径与仓库既有 sqlSelfPurchaseSupplierEquiv 对齐
expect(sql).toContain("UPPER(TRIM(roi.supplier_code)) IN ('INTERNAL', 'UNASSIGNED')");
});
it('入库流水视角:同批次存在退货回冲流水即视为已冲销', () => {
const sql = inventoryLogVoidedByReturnSql('il');
expect(sql).toContain('FROM inventory_logs rl');
expect(sql).toContain("rl.operation_type = 'order_item_return'");
expect(sql).toContain('rl.batch_no = il.batch_no');
});
it('统一入口是两者的并集,且**不含任何写操作**(不改历史数据)', () => {
const sql = inboundCoverageVoidedSql('i');
expect(sql).toContain(selfPurchaseInboundVoidedSql('i'));
expect(sql).toContain(prepInboundVoidedSql('i'));
for (const frag of [sql, notVoidedByLineReturnSql('i'), inventoryLogVoidedByReturnSql('il')]) {
expect(frag).not.toMatch(/\b(DELETE|UPDATE|INSERT)\b/i);
expect(frag).not.toContain('inbound_batch_no =');
expect(frag).not.toContain('actual_qty =');
}
});
});
describe('判定点接线(源码合同)', () => {
it('inspectionRejectFulfillmentService:自采已入库 + 覆盖兜底两处都收口', () => {
const src = readSrc('services/inspectionRejectFulfillmentService.ts');
expect(src).toContain("import { notVoidedByLineReturnSql } from './inboundCoverageSql'");
// hasSuccessfulInboundInspection 兜底分支 与 hasSuccessfulSelfPurchaseSkuInbound
const hits = src.match(/AND \$\{notVoidedByLineReturnSql\('i'\)\}/g) || [];
expect(hits.length).toBe(2);
expect(src).toContain('FROM inspections i\n WHERE i.order_code IS NOT DISTINCT FROM $1');
});
it('orderInspectionStateService:ORDER_ITEM_INBOUND_COVERED_SQL 两个 inspections 分支都收口', () => {
const src = readSrc('services/orderInspectionStateService.ts');
expect(src).toContain("import { notVoidedByLineReturnSql } from './inboundCoverageSql'");
const hits = src.match(/AND \$\{notVoidedByLineReturnSql\('i'\)\}/g) || [];
expect(hits.length).toBe(2); // SKU 级本单验收分支 + 履约/采购单兜底分支
});
it('inbound_done(Web + 小程序两份):两个分支都收口', () => {
for (const rel of ['services/orderManageItemsService.ts', 'routes/mobile/mobileAdminRoutes.ts']) {
const src = readSrc(rel);
expect(src, rel).toContain('inventoryLogVoidedByReturnSql');
expect(src, rel).toContain('notVoidedByLineReturnSql');
expect(src, rel).toContain("AND NOT ${inventoryLogVoidedByReturnSql('il')}");
expect(src, rel).toContain("AND ${notVoidedByLineReturnSql('insp')}");
}
});
it('加工单净重汇总与备货待验创建判定都收口', () => {
expect(readSrc('services/processingDemandSheetService.ts')).toContain(
"AND ${notVoidedByLineReturnSql('i')}`"
);
const prep = readSrc('services/preparationFulfillmentService.ts');
expect(prep).toContain("import { notVoidedByLineReturnSql } from './inboundCoverageSql'");
expect(prep).toContain("AND ${notVoidedByLineReturnSql('i')}");
});
});