来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
104 lines
5.1 KiB
TypeScript
104 lines
5.1 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import path from 'path';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
/**
|
||
* 仓内加工成本清零自愈的 SQL 契约。
|
||
*
|
||
* 背景(2026-09-24):该函数的 UPDATE 原本把 `WAREHOUSE_DEMAND_PO_LINK_SQL`(含
|
||
* `po.source_id = di.order_item_id::text` 的**逐行相关 EXISTS**)直接嵌进 WHERE ——
|
||
* `id::text` 非 sargable,导致每行采购单全扫一次 `order_item_fulfillments`
|
||
* (本地实测 3533 行 × 6067 行 ≈ 2100 万次行扫描;单次 943ms 且 **0 行被更新**,
|
||
* 占采购列表接口总耗时 95%)。现改为「先把需求关联的采购单号算成小集合再 IN」。
|
||
*
|
||
* ⚠️ 本文件的存在意义:该「需求关联」口径现在有**两处表示**(本函数的集合查询、
|
||
* 以及模块常量 `WAREHOUSE_DEMAND_PO_LINK_SQL`)。将来口径变更必须**两处同改**,
|
||
* 否则自愈会静默漏改/多改成本字段。下面的断言把两边必须一致的结构钉住。
|
||
*/
|
||
|
||
const SRC = readFileSync(
|
||
path.join(__dirname, '../src/services/processingDemandSheetService.ts'),
|
||
'utf8'
|
||
);
|
||
|
||
/** 取出 healWarehousePurchaseZeroCosts 的整段函数体(到下一个 export 为止) */
|
||
function healBody(): string {
|
||
const start = SRC.indexOf('export async function healWarehousePurchaseZeroCosts');
|
||
expect(start).toBeGreaterThan(-1);
|
||
const rest = SRC.slice(start);
|
||
const end = rest.indexOf('export async function', 10);
|
||
return end > 0 ? rest.slice(0, end) : rest;
|
||
}
|
||
|
||
/** 只取 `WITH demand_codes AS ( … )` 这一段,避免把函数里其它 SQL 一起算进来 */
|
||
function demandCte(): string {
|
||
const body = healBody();
|
||
const start = body.indexOf('WITH demand_codes AS (');
|
||
expect(start).toBeGreaterThan(-1);
|
||
const end = body.indexOf('UPDATE purchase_orders po', start);
|
||
expect(end).toBeGreaterThan(start);
|
||
return body.slice(start, end);
|
||
}
|
||
|
||
describe('仓内加工成本清零自愈:SQL 契约', () => {
|
||
it('必须用「先算小集合再 IN」,不得回退成 WHERE 里逐行 EXISTS', () => {
|
||
const body = healBody();
|
||
expect(body).toContain('WITH demand_codes AS');
|
||
expect(body).toContain('IN (SELECT pc FROM demand_codes)');
|
||
// 旧写法(逐行相关 EXISTS)不得回来
|
||
expect(body).not.toContain("WAREHOUSE_DEMAND_PO_LINK_SQL.replace('$STATUS', '$2')");
|
||
expect(body).not.toContain('${demandExists}');
|
||
});
|
||
|
||
it('集合的两条分支必须覆盖常量的两个析取项(履约单号 / source_id 反查)', () => {
|
||
const cte = demandCte();
|
||
// 分支 A ↔ 常量里的 `f.purchase_code = po.purchase_code`
|
||
expect(cte).toContain('JOIN order_item_fulfillments f ON f.order_item_id = di.order_item_id');
|
||
expect(cte).toContain('WHERE f.purchase_code IS NOT NULL');
|
||
// 分支 B ↔ 常量里的 `po.source_id = di.order_item_id::text`
|
||
expect(cte).toContain('JOIN purchase_orders po2 ON po2.source_id = di.order_item_id::text');
|
||
// 两条分支的规模必须对称:各带一次「需求单 + 未作废」
|
||
expect(cte.match(/pt\.source_type = 'demand'/g)?.length).toBe(2);
|
||
expect(cte.match(/pt\.process_status < \$2/g)?.length).toBe(2);
|
||
});
|
||
|
||
it('集合与入参必须共用同一个状态参数($2 ↔ PROCESS_STATUS.rejected),防止只改一处', () => {
|
||
const body = healBody();
|
||
expect(body).toContain('pt.process_status < $2');
|
||
expect(body).toContain('PROCESS_STATUS.rejected');
|
||
// 不得硬编码状态值或串用 $1($1 是分公司)
|
||
expect(body).not.toContain('process_status < 50');
|
||
expect(body).not.toContain('process_status < $1');
|
||
});
|
||
|
||
it('廉价守卫必须保留(否则自愈退化成全表扫描 + 全表更新)', () => {
|
||
const body = healBody();
|
||
expect(body).toContain('COALESCE(po.actual_price, 0) <> 0');
|
||
expect(body).toContain('COALESCE(po.purchase_price, 0) <> 0');
|
||
expect(body).toContain('COALESCE(po.total_amount, 0) <> 0');
|
||
// self_processed 行仍必须走同一条 UPDATE(不得另起一条只清 self_processed 的语句)
|
||
expect(body).toContain('COALESCE(po.self_processed, FALSE) = TRUE');
|
||
});
|
||
|
||
it('只清成本三项,不得顺手改数量/状态(财务口径外的字段一律不碰)', () => {
|
||
const body = healBody();
|
||
const setBlock = body.slice(body.indexOf('SET actual_price = 0'), body.indexOf('WHERE ('));
|
||
expect(setBlock).toContain('actual_price = 0');
|
||
expect(setBlock).toContain('purchase_price = 0');
|
||
expect(setBlock).toContain('total_amount = 0');
|
||
for (const forbidden of ['purchase_qty', 'actual_qty', 'purchase_status', 'self_processed =']) {
|
||
expect(setBlock).not.toContain(forbidden);
|
||
}
|
||
});
|
||
|
||
it('模块常量与其它调用点不得被删(本次只替换自愈这一处的求值方式)', () => {
|
||
expect(SRC).toContain('const WAREHOUSE_DEMAND_PO_LINK_SQL = `');
|
||
expect(SRC).toContain('WAREHOUSE_DEMAND_PURCHASE_EXISTS_SQL');
|
||
// 常量仍被其它站点复用(自愈函数不再是它的调用方之一)
|
||
const uses = SRC.match(/WAREHOUSE_DEMAND_PO_LINK_SQL\.replace\('\$STATUS'/g)?.length ?? 0;
|
||
expect(uses).toBeGreaterThanOrEqual(3);
|
||
const heal = healBody();
|
||
expect(heal).not.toContain('WAREHOUSE_DEMAND_PO_LINK_SQL.replace(');
|
||
});
|
||
});
|