310 lines
15 KiB
TypeScript
310 lines
15 KiB
TypeScript
/**
|
||
* 仓内加工原材料采购(阶段 1)契约测试
|
||
* 设计:docs/design/20261102-warehouse-processing-raw-material-auto-purchase.md
|
||
*/
|
||
import { readFileSync, existsSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
import {
|
||
estimateRawMaterialQty,
|
||
planRawMaterialPurchaseQty,
|
||
normalizeProcessingBom,
|
||
hasProcessingBomForOutput,
|
||
} from '../src/services/processingRawMaterialService';
|
||
import { parseProcessingRawPurchaseConfig } from '../src/services/processingRawPurchaseConfigService';
|
||
|
||
const ROOT = path.join(__dirname, '../..');
|
||
const read = (rel: string) => readFileSync(path.join(ROOT, rel), 'utf8');
|
||
|
||
describe('processing_bom 解析与损耗估算', () => {
|
||
it('只接受合法项,按加工方式去重(后者丢弃)', () => {
|
||
const entries = normalizeProcessingBom([
|
||
{ method: '去皮', material_sku: 'SKU_RAW', loss_rate: 0.11 },
|
||
{ method: '去皮', material_sku: 'SKU_OTHER', loss_rate: 0.2 },
|
||
{ method: '', material_sku: 'SKU_X', loss_rate: 0.1 },
|
||
{ method: '切丝', material_sku: '', loss_rate: 0.1 },
|
||
{ method: '切块', material_sku: 'SKU_Y', loss_rate: 1.2 },
|
||
{ method: '切丁', material_sku: 'SKU_Z', loss_rate: 0 },
|
||
]);
|
||
expect(entries).toEqual([
|
||
{ method: '去皮', material_sku: 'SKU_RAW', loss_rate: 0.11 },
|
||
// method 为空 = 商品级通用原材料(2026-11-02 单原料改版)
|
||
{ method: '', material_sku: 'SKU_X', loss_rate: 0.1 },
|
||
{ method: '切丁', material_sku: 'SKU_Z', loss_rate: 0 },
|
||
]);
|
||
});
|
||
|
||
it('非数组/脏数据返回空数组', () => {
|
||
expect(normalizeProcessingBom(null)).toEqual([]);
|
||
expect(normalizeProcessingBom('[]')).toEqual([]);
|
||
expect(normalizeProcessingBom([null, 1, 'x'])).toEqual([]);
|
||
});
|
||
|
||
it('下单后端硬校验仓内加工 BOM(防止接口/小程序绕过前端)', () => {
|
||
const submit = read('server/src/services/customerOrderSubmitService.ts');
|
||
expect(submit).toContain('processing_bom_required');
|
||
expect(submit).toContain('hasProcessingBomForOutput');
|
||
expect(submit).toContain('请先在商品管理配置原材料后再下单');
|
||
});
|
||
|
||
it('原料验收入库同步原料锚点状态(镜像自采路径按 purchase_code 回写)', () => {
|
||
const service = read('server/src/services/processingRawMaterialService.ts');
|
||
expect(service).toContain('po.purchase_code = ANY($4::text[])');
|
||
expect(service).toContain('r.purchase_status < 30');
|
||
});
|
||
|
||
it('加工成品自采行成本只读,完工后占位单自动关闭', () => {
|
||
const finished = read('server/src/services/processingFinishedOutputService.ts');
|
||
expect(finished).toContain('UPDATE purchase_orders');
|
||
expect(finished).toContain('self_processed = true');
|
||
expect(finished).toContain('purchase_status = 30');
|
||
const web = read('src/pages/purchase/PurchaseManagement.tsx');
|
||
expect(web).not.toContain('canZeroWarehouseDemandCost');
|
||
expect(web).toContain('const canEditPurchaseFields = (purchase: PurchaseOrder) =>\n canCompletePurchase(purchase) || canAmendPurchaseActual(purchase);');
|
||
});
|
||
|
||
it('需求单未领料不允许提交加工完工', () => {
|
||
const service = read('server/src/services/processingTaskService.ts');
|
||
expect(service).toContain("source_type || '') === 'demand'");
|
||
expect(service).toContain('material_not_picked');
|
||
expect(service).toContain('请先领料后再提交加工完工');
|
||
});
|
||
|
||
it('新订单不再复用已完工/待验收加工单', () => {
|
||
const service = read('server/src/services/processingDemandSheetService.ts');
|
||
expect(service).toContain('只复用「未开始/加工中」需求单');
|
||
expect(service).toContain('PROCESS_STATUS.pending');
|
||
expect(service).toMatch(/process_status < \$5[\s\S]{0,200}30,/);
|
||
const migration = read('migrations/20261102_processing_demand_reuse_open_only.sql');
|
||
expect(migration).toContain('process_status < 30');
|
||
expect(migration).toContain('idx_processing_demand_sheet_key');
|
||
});
|
||
|
||
it('生成原料采购不写「加工单…成品×…」备注', () => {
|
||
const service = read('server/src/services/processingRawMaterialService.ts');
|
||
expect(service).not.toContain('原料预计 ${plannedQty}');
|
||
expect(service).toContain('planRawMaterialPurchaseQty(demandQty, bomEntry.loss_rate)');
|
||
expect(service).toContain('10, NULL, NULL)');
|
||
});
|
||
|
||
it('BOM 读取:按方式 → 通用 method 为空 → 首条兜底(与 Web 同口径)', () => {
|
||
const service = read('server/src/services/processingRawMaterialService.ts');
|
||
expect(service).toContain('entries.find((entry) => !entry.method)');
|
||
// 2026-11-03 改判:旧实现的「恰好 1 条才兜底」已删,改为与 Web 一致的 entries[0]
|
||
expect(service).not.toContain('entries.length === 1 ? entries[0] : null');
|
||
expect(service).toContain('entries[0] ||');
|
||
// 加工方式为空不再直接判死
|
||
expect(service).not.toContain('if (!sku || !procMethod) return null;');
|
||
});
|
||
|
||
it('生成采购计划量向上取整为整数(不落 15.714 类小数)', () => {
|
||
expect(planRawMaterialPurchaseQty(11, 0.3)).toBe(15); // 11×1.3=14.3 → 15
|
||
expect(planRawMaterialPurchaseQty(7.8, 0)).toBe(8);
|
||
expect(planRawMaterialPurchaseQty(5, 0)).toBe(5);
|
||
expect(planRawMaterialPurchaseQty(0, 0.3)).toBe(0);
|
||
});
|
||
|
||
it('损耗率按成品比例估算数量(原料 = 成品 × (1+损耗率))', () => {
|
||
expect(estimateRawMaterialQty(32, 0.11)).toBe(35.52); // 32 × 1.11
|
||
expect(estimateRawMaterialQty(32, 0)).toBe(32);
|
||
expect(estimateRawMaterialQty(0, 0.2)).toBe(0);
|
||
// 极端损耗上限 95%,避免除零
|
||
expect(estimateRawMaterialQty(1, 2)).toBe(1.95); // 损耗上限 95%
|
||
});
|
||
});
|
||
|
||
describe('自动采购开关', () => {
|
||
it('默认开启,只有显式 enabled=false 才关闭', () => {
|
||
expect(parseProcessingRawPurchaseConfig(null).enabled).toBe(true);
|
||
expect(parseProcessingRawPurchaseConfig(undefined).enabled).toBe(true);
|
||
expect(parseProcessingRawPurchaseConfig(true).enabled).toBe(true);
|
||
expect(parseProcessingRawPurchaseConfig({ enabled: true }).enabled).toBe(true);
|
||
expect(parseProcessingRawPurchaseConfig({ enabled: false }).enabled).toBe(false);
|
||
expect(parseProcessingRawPurchaseConfig({ enabled: 'false' }).enabled).toBe(true);
|
||
});
|
||
|
||
it('同步时按分公司开关跳过(关闭时不建/不改草稿)', () => {
|
||
const service = read('server/src/services/processingRawMaterialService.ts');
|
||
expect(service).toContain('isProcessingRawPurchaseEnabled');
|
||
expect(service).toContain("if (!(await isEnabledForBranch(branchCode))) continue;");
|
||
});
|
||
});
|
||
|
||
describe('迁移与租户名单', () => {
|
||
const migration = read('migrations/20261107_processing_raw_material_purchase.sql');
|
||
|
||
it('新增 processing_bom 列与 processing_raw_purchases 表并登记 tenant_business_tables', () => {
|
||
expect(migration).toContain('processing_bom JSONB');
|
||
expect(migration).toContain('processing_raw_purchases');
|
||
expect(migration).toContain('tenant_business_tables()');
|
||
expect(migration).toMatch(/processing_raw_purchases.*tenant_business_tables|tenant_business_tables.*processing_raw_purchases/s);
|
||
});
|
||
|
||
it('原料采购唯一键按 加工单 + 原料', () => {
|
||
expect(migration).toContain('UNIQUE (demand_task_id, material_sku)');
|
||
});
|
||
});
|
||
|
||
describe('同步/提交/回写规则(源码契约)', () => {
|
||
const service = read('server/src/services/processingRawMaterialService.ts');
|
||
|
||
it('只自动改待采购(10),已提交/已入库只标提示', () => {
|
||
expect(service).toContain('if (Number(row.purchase_status) === 10)');
|
||
expect(service).toContain('请人工确认');
|
||
expect(service).toContain('需求已变化');
|
||
});
|
||
|
||
it('提交采购走独立入库待验单(正常入库流程)', () => {
|
||
expect(service).toContain('createIndependentInboundInspection');
|
||
expect(service).toContain("purchase_status = 20");
|
||
expect(service).toContain('purchase_status = $5');
|
||
});
|
||
|
||
it('成本价 0 守卫:未开允许 0 开关时拒绝', () => {
|
||
expect(service).toContain('isZeroSupplyCostAllowed');
|
||
expect(service).toContain('成本价须大于 0(除非开启允许成本为 0)');
|
||
});
|
||
|
||
it('已采购待入库(20):未验收前 Web 可改数量/成本,并同步待验单', () => {
|
||
expect(service).toContain('status !== 10 && status !== 20');
|
||
expect(service).toContain('原料已验收入库,不能再修改');
|
||
expect(service).toContain('UPDATE inspections');
|
||
expect(service).toContain('prepare_qty = $2::numeric');
|
||
expect(service).toContain('purchase_price = $3::numeric');
|
||
});
|
||
|
||
it('取消仓内加工:未入库的原料采购取消;已入库按独立入库保留', () => {
|
||
expect(service).toContain('取消仓内加工,原料采购取消(未入库)');
|
||
expect(service).toContain('原料已入库,按独立入库保留');
|
||
expect(service).toContain('DELETE FROM inspections');
|
||
expect(service).toContain('purchase_status = 40');
|
||
});
|
||
|
||
it('需求单同步后挂钩,但不阻断订单', () => {
|
||
const demand = read('server/src/services/processingDemandSheetService.ts');
|
||
expect(demand).toContain('syncRawMaterialPurchasesForDemandTasksSafely');
|
||
expect(demand).toContain('不阻断订单');
|
||
});
|
||
|
||
it('验收完成后回写原料采购已入库', () => {
|
||
const inspection = read('server/src/services/inspectionComplianceService.ts');
|
||
expect(inspection).toContain('syncProcessingRawPurchaseInboundFromInspection');
|
||
});
|
||
|
||
it('不再暴露独立原料采购列表/编辑/提交接口;仅保留 BOM 变更同步入口', () => {
|
||
const routes = read('server/src/modules/workflow/routes/purchaseRoutes.ts');
|
||
expect(routes).not.toContain("router.get('/purchase-orders/processing-raw-materials'");
|
||
expect(routes).not.toContain("router.patch('/purchase-orders/processing-raw-materials/:id'");
|
||
expect(routes).not.toContain("router.post('/purchase-orders/processing-raw-materials/:id/submit'");
|
||
expect(routes).toContain('/purchase-orders/processing-raw-materials/sync');
|
||
});
|
||
|
||
it('仓内加工成品不生成待验;原材料独立入库待验才是入库列表来源', () => {
|
||
const prep = read('server/src/services/preparationFulfillmentService.ts');
|
||
expect(prep).toContain('requiresWarehouseProcessing(item.processing_method, item.processing_owner)');
|
||
expect(prep).toContain('DELETE FROM inspections i');
|
||
expect(prep).toContain("TRIM(COALESCE(oi.processing_owner, '')) = 'warehouse'");
|
||
expect(prep).toContain("TRIM(oi.processing_method) <> '原材料'");
|
||
const migration = read('migrations/20261111_processing_finished_pending_inspection_cleanup.sql');
|
||
expect(migration).toContain('inspection_time IS NULL');
|
||
expect(migration).toContain("processing_owner, ''");
|
||
});
|
||
|
||
it('仓内加工成品占位自采单不生成待验', () => {
|
||
const inspection = read('server/src/services/inspectionComplianceService.ts');
|
||
expect(inspection).toContain('self_processed');
|
||
expect(inspection).toContain('if (Boolean(po.self_processed)) return;');
|
||
});
|
||
|
||
it('原料采购完成的待验是独立入库(入库列表可见)', () => {
|
||
const inspection = read('server/src/services/inspectionComplianceService.ts');
|
||
expect(inspection).toContain("'processing_raw_material'");
|
||
expect(inspection).toContain('independent_inbound');
|
||
const migration = read('migrations/20261112_processing_raw_inspection_visibility.sql');
|
||
expect(migration).toContain('independent_inbound');
|
||
expect(migration).toContain('processing_raw_material');
|
||
});
|
||
|
||
it('加工原料镜像到 purchase_orders,小程序/Web 共用自采列表', () => {
|
||
const service = read('server/src/services/processingRawMaterialService.ts');
|
||
expect(service).toContain('upsertRawPurchaseOrderMirror');
|
||
expect(service).toContain("'processing_raw_material'");
|
||
expect(service).toContain('INSERT INTO purchase_orders');
|
||
expect(service).toContain('syncProcessingRawPurchaseFromPurchaseOrder');
|
||
expect(read('server/src/services/purchaseOperationService.ts')).toContain(
|
||
'syncProcessingRawPurchaseFromPurchaseOrder'
|
||
);
|
||
const migration = read('migrations/20261110_processing_raw_purchase_mirror.sql');
|
||
expect(migration).toContain('processing_raw_material');
|
||
expect(migration).toContain('CEIL(planned_qty)');
|
||
});
|
||
|
||
it('BOM 保存后可同步未完工加工单', () => {
|
||
expect(service).toContain('syncRawMaterialPurchasesForOutputSku');
|
||
expect(service).toContain("pt.source_type = 'demand'");
|
||
expect(service).toContain('COALESCE(pt.process_status, 0) < 40');
|
||
});
|
||
|
||
it('加工单删除前保护已提交/已入库的原料采购', () => {
|
||
const demand = read('server/src/services/processingDemandSheetService.ts');
|
||
expect(demand).toContain('processing_raw_purchases');
|
||
expect(demand).toContain('purchase_status >= 20');
|
||
});
|
||
|
||
it('待验独立入库被退回时原料采购回到待采购', () => {
|
||
const spi = read('server/src/services/selfPurchaseStockInboundService.ts');
|
||
expect(spi).toContain('processing_raw_purchases');
|
||
expect(spi).toContain('purchase_status = 10');
|
||
});
|
||
});
|
||
|
||
describe('设计文档存在', () => {
|
||
it('阶段 1/2 设计与验收标准落地', () => {
|
||
const doc = path.join(ROOT, 'docs/design/20261102-warehouse-processing-raw-material-auto-purchase.md');
|
||
expect(existsSync(doc)).toBe(true);
|
||
const text = readFileSync(doc, 'utf8');
|
||
expect(text).toContain('阶段 1');
|
||
expect(text).toContain('阶段 2');
|
||
});
|
||
});
|
||
|
||
/**
|
||
* BOM 命中口径与 Web 对齐(2026-11-03):
|
||
* 旧实现要求加工方式非空、且只有「恰好 1 条」才兜底首条 ⇒ 小程序判 has_bom=false 退回旧界面,
|
||
* 而 Web 弹窗用 `按方式 → 空方式通用 → entries[0]` 三条序,两边看到的不一样。
|
||
*/
|
||
describe('BOM 命中口径与 Web 对齐', () => {
|
||
const fakeDb = (processingBom: unknown) =>
|
||
({
|
||
query: async () => ({ rows: [{ processing_bom: processingBom }] }),
|
||
}) as any;
|
||
|
||
it('加工方式为空但商品配了 BOM ⇒ 仍命中(旧实现直接返回 false)', async () => {
|
||
const db = fakeDb([{ method: '去皮', material_sku: 'SKU_RAW', loss_rate: 0.1 }]);
|
||
expect(await hasProcessingBomForOutput(db, 'BR002', 'SKU_OUT', '')).toBe(true);
|
||
});
|
||
|
||
it('多条 BOM 且都不匹配加工方式 ⇒ 取首条(与 Web 的 entries[0] 一致)', async () => {
|
||
const db = fakeDb([
|
||
{ method: '去皮', material_sku: 'SKU_A', loss_rate: 0.1 },
|
||
{ method: '切丝', material_sku: 'SKU_B', loss_rate: 0.2 },
|
||
]);
|
||
expect(await hasProcessingBomForOutput(db, 'BR002', 'SKU_OUT', '切块')).toBe(true);
|
||
});
|
||
|
||
it('空方式条目优先于首条', async () => {
|
||
const db = fakeDb([
|
||
{ method: '去皮', material_sku: 'SKU_A', loss_rate: 0.1 },
|
||
{ method: '', material_sku: 'SKU_COMMON', loss_rate: 0.05 },
|
||
]);
|
||
const entry = await hasProcessingBomForOutput(db, 'BR002', 'SKU_OUT', '切块');
|
||
expect(entry).toBe(true);
|
||
});
|
||
|
||
it('成品 SKU 为空或 BOM 为空 ⇒ false(不放宽到「什么都没有也算有」)', async () => {
|
||
expect(await hasProcessingBomForOutput(fakeDb([]), 'BR002', '', '去皮')).toBe(false);
|
||
expect(await hasProcessingBomForOutput(fakeDb([]), 'BR002', 'SKU_OUT', '去皮')).toBe(false);
|
||
expect(await hasProcessingBomForOutput(fakeDb(null), 'BR002', 'SKU_OUT', '去皮')).toBe(false);
|
||
});
|
||
});
|