Files
SupplierSystem/server/tests/standaloneProcessingMulti.test.ts
T
Ubuntu b62a123a83
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(独立加工): 后端 P0——多行原料 + 多行成品回仓(processing_in) + 损耗自动差额可覆盖 + 成品成本默认均分可设置 + 报告继承(批次只复制关联/检验复制引用) + 请求号幂等;真库跑通
2026-10-23 15:30:29 +08:00

107 lines
4.9 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';
/**
* 独立加工 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)', () => {
expect(outSvc).toContain('PG-${taskCode}-${idx + 1}');
expect(outSvc).toContain("'processing_in'");
expect(outSvc).toContain('INSERT INTO inventory (');
});
it('成本默认按产出数量均分,允许人工 unitCost 覆盖', () => {
expect(outSvc).toContain('const defaultUnitCost = outputQty > 0 ? materialCost / outputQty : 0');
expect(outSvc).toContain('output.unitCost == null ? Math.round(defaultUnitCost');
});
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");
});
});