import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; /** * 独立加工 P0 契约(2026-10-23 开发者裁决): * 原料多行 / 成品多行 / 损耗自动差额可人工覆盖 / 成品新批次回仓 / 成本默认均分可人工设置 / * 损耗只留痕不进账本 / 报告继承可选 / 幂等请求号 / 老调用(单原料)不回归。 * * 这里只钉**行为契约**(源码级),真实数据路径由 dev 库端到端验证(见设计文档 §5 证据)。 */ const createSvc = readFileSync(new URL('../src/services/processingStandaloneService.ts', import.meta.url), 'utf8'); const outSvc = readFileSync(new URL('../src/services/processingStandaloneOutputService.ts', import.meta.url), 'utf8'); const routes = readFileSync( new URL('../src/modules/workflow/routes/inspectionRoutes.ts', import.meta.url), 'utf8' ); describe('创建:多行原料 + 幂等', () => { it('支持 materials 多行,且老调用(skuCode+qty)仍按单行处理', () => { expect(createSvc).toContain('Array.isArray(input.materials) && input.materials.length'); expect(createSvc).toContain('[{ skuCode: input.skuCode, qty: input.qty }]'); expect(createSvc).toContain('merged.set(code, roundQty((merged.get(code) || 0) + qty))'); }); it('逐行校验可用量与 FIFO 扣减(每行独立)', () => { expect(createSvc).toContain('for (const [idx, material] of materials.entries())'); expect(createSvc).toContain('getClaimableStockQty(client, material.skuCode, branchCode, null)'); expect(createSvc).toContain("operationType: 'processing_out'"); }); it('幂等:同分公司 + 同请求号直接返回既有单,不重复扣库存', () => { expect(createSvc).toContain('WHERE branch_code = $1 AND client_request_no = $2'); expect(createSvc).toContain('client_request_no'); }); it('原料行落库含抵减批次 JSON(报告继承与对账要用)', () => { expect(createSvc).toContain('deducted_batches'); expect(createSvc).toContain('JSON.stringify('); }); }); describe('完工:成品回仓 / 损耗 / 成本 / 报告继承', () => { it('产出 > 投入 一律拒绝', () => { expect(outSvc).toContain("'output_exceeds_input'"); expect(outSvc).toContain('不能大于原料投入合计'); }); it('损耗默认自动差额,允许人工覆盖并留痕(loss_manual)', () => { expect(outSvc).toContain('const autoLoss = roundQty(materialQty - outputQty)'); expect(outSvc).toContain('loss_manual: lossManual'); }); it('产出+损耗 不得大于投入', () => { expect(outSvc).toContain("'loss_over_input'"); }); it('成品新批次入库 + 加工入库流水(processing_in)', () => { // 批次号用**任务 uuid 前缀**(不能用 task_code:删单后取号会重用 ⇒ 撞 inventory 唯一键) expect(outSvc).toContain('PG-${taskKey}-${idx + 1}'); expect(outSvc).toContain("const taskKey = taskId.replace(/-/g, '').slice(0, 8);"); expect(outSvc).toContain("'processing_in'"); expect(outSvc).toContain('INSERT INTO inventory ('); }); it('成品默认继承原料批次的生产日期 / 保质期 / 产地', () => { expect(outSvc).toContain('SELECT batch_no, production_date, expiry_date, shelf_life, origin'); expect(outSvc).toContain('const primaryMaterialBatch'); // 主批取最早到期(日期/产地同一批),字段为空再回退其它原料批次 expect(outSvc).toContain('batchTime(a.expiry_date) - batchTime(b.expiry_date)'); expect(outSvc).toContain("pickBatchField('production_date')"); expect(outSvc).toContain("pickBatchField('expiry_date')"); expect(outSvc).toContain("pickBatchField('origin')"); // 写入 inventory 批次:production_date / expiry_date / shelf_life / origin 四个字段都带上 expect(outSvc).toContain('branch_code, production_date, expiry_date, shelf_life, origin, spec'); expect(outSvc).toContain('inheritedProductionDate'); expect(outSvc).toContain('inheritedExpiryDate'); expect(outSvc).toContain('inheritedOrigin'); }); /** * 保质期**原文**必须一起继承:库存管理「修改」弹窗改生产日期时,服务端用 * 「生产日期 + 保质期」重算有效期(推不出则清空)。只给算好的 expiry_date 不给原文, * 操作员改一次生产日期就会把有效期弄丢。 */ it('保质期原文(shelf_life)也继承,改生产日期重算有效期不会丢', () => { expect(outSvc).toContain("pickBatchField('shelf_life')"); expect(outSvc).toContain('const inheritedShelfLife'); expect(outSvc).toContain('inheritedShelfLife,'); }); it('成本默认按产出数量均分,允许人工 unitCost 覆盖', () => { expect(outSvc).toContain('row.unitCost == null ? Math.round((materialCost / outputQty) * 10000) / 10000'); expect(outSvc).toContain('const unitCost = plan.unitCost;'); }); it('总价值守恒:舍入残差补到最后一行(损耗越多单价越高,但总额 = 原料成本)', () => { expect(outSvc).toContain('const allAutoCost = outputs.every((row) => row.unitCost == null);'); expect(outSvc).toContain('const residue = roundMoney(materialCost - sumAmount);'); expect(outSvc).toContain('last.amount = roundMoney(last.amount + residue);'); // 人工改价时不得被残差悄悄改掉 expect(outSvc).toContain('if (allAutoCost && costPlans.length) {'); }); it('成品入库同时写库存持有成本(不写采购成本,损耗不新开账本行)', () => { expect(outSvc).toContain('upsertInventoryHoldingCost'); expect(outSvc).toContain("costSource: 'processing_output'"); expect(outSvc).not.toContain('purchase'); }); it('批次报告继承:只复制关联(match_source=inherit),不新增报告行', () => { expect(outSvc).toContain("'inherit'"); expect(outSvc).toContain('inventory_batch_report_matches'); // 批次报告路径里不允许出现"新建报告本体"的插入(报告本体共享) const batchBlock = outSvc.slice( outSvc.indexOf('if (output.inheritBatchReport'), outSvc.indexOf('if (output.inheritInspectionReport') ); expect(batchBlock).not.toContain('INSERT INTO inventory_batch_reports'); }); it('检验报告继承:原料批次有检验照片时,新建一条「继承」报告并挂到成品批次', () => { const inspectBlock = outSvc.slice(outSvc.indexOf('if (output.inheritInspectionReport')); expect(inspectBlock).toContain("report_type, file_paths"); expect(inspectBlock).toContain("'inspection_inherit'"); expect(inspectBlock).toContain('inspection_photos'); }); it('已完成单不能重复申报', () => { expect(outSvc).toContain("'already_completed'"); }); }); describe('路由:多行原料入参 + 完工 + 行查询', () => { it('create 接受 materials 与 client_request_no', () => { expect(routes).toContain('req.body?.materials'); expect(routes).toContain('clientRequestNo'); }); it('新增完工与行查询路由', () => { expect(routes).toContain("/processing/standalone/:taskId/complete"); expect(routes).toContain("/processing/standalone/:taskId/lines"); }); });