import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; /** * 领料加工「全损耗」必须能收口(2026-10-15 开发者裁决 A) * * 事故(真跑,评审员 D):成品验收实收 0(全损耗)时,`recalcDemandSheetFromInspections` 的 * 完工判定只认三种情况 —— * ① 该行有 `actual_qty > 0` 的验收;② 还有 `inspection_time IS NULL` 的待验行;③ 该行被库存抵扣全覆盖。 * 全损耗时三条都不成立 ⇒ `allFulfilled=false` ⇒ `process_status` **永久停在 20(加工中)**、 * `settleDemandSheetCostOnCompletion` 从不被调用 ⇒ 材料成本悬空在制品、订单行 * `processing_settled_unit_cost` 恒 NULL(真跑:领料 13 斤 ¥39.00 卡在 20)。 * * 修法:把「**已完工但实收为 0** 的验收」(`inspection_time` 非空、未作废、非退回)也算作 * 已交付(净重 0)⇒ 与既有 `settle` 的「净重为 0」分支接上(写 `processing_settled_unit_cost = 0` * 并清账),需求单正常完工。 * * 见 docs/design/20260823-demand-sheet-cost-settlement.md(「净重为 0 ⇒ 整批记 processing_loss」) * 与 docs/design/20261015-领料加工全损耗收口.md。 */ const src = readFileSync( new URL('../src/services/processingDemandSheetService.ts', import.meta.url), 'utf8' ); function functionBody(source: string, name: string): string { const start = source.indexOf(`export async function ${name}`); expect(start, `${name} 应存在`).toBeGreaterThanOrEqual(0); const rest = source.slice(start + 1); const candidates = [ rest.indexOf('export async function'), rest.indexOf('export function'), ].filter((n) => n >= 0); const next = candidates.length ? Math.min(...candidates) : -1; return next >= 0 ? source.slice(start, start + 1 + next) : source.slice(start); } /** 只保留代码(去注释),避免注释里的同名字样让断言失去意义 */ function stripComments(text: string): string { let out = ''; let i = 0; let quote: string | null = null; while (i < text.length) { const ch = text[i]; const next = text[i + 1]; if (quote) { out += ch; if (ch === '\\') { out += next ?? ''; i += 2; continue; } if (ch === quote) quote = null; i += 1; continue; } if (ch === "'" || ch === '"' || ch === '`') { quote = ch; out += ch; i += 1; continue; } if (ch === '/' && next === '/') { while (i < text.length && text[i] !== '\n') i += 1; continue; } if (ch === '/' && next === '*') { i += 2; while (i < text.length && !(text[i] === '*' && text[i + 1] === '/')) i += 1; i += 2; continue; } out += ch; i += 1; } return out; } const body = functionBody(src, 'recalcDemandSheetFromInspections'); const code = stripComments(body); const ZERO_LOSS_GUARD = 'if (zeroLossDone.rowCount) continue;'; /** 判据本体:只查「已完工但实收 0」的验收 */ const ZERO_LOSS_SQL_MARK = 'COALESCE(i.actual_qty, 0) = 0'; /** 反向验证:把零损耗分支整体摘掉(= 修前形态) */ function withoutZeroLoss(text: string): string { const start = text.indexOf('const zeroLossDone = await db.query('); expect(start, '应存在 zeroLossDone 查询').toBeGreaterThanOrEqual(0); const end = text.indexOf(ZERO_LOSS_GUARD); expect(end, '应存在 zeroLossDone 守卫').toBeGreaterThan(start); return text.slice(0, start) + text.slice(end + ZERO_LOSS_GUARD.length); } describe('全损耗(成品净重 0)必须判定为「已交付」', () => { it('存在「已完工但实收 0」的验收判据', () => { expect(code).toContain('const zeroLossDone = await db.query('); expect(code).toContain(ZERO_LOSS_SQL_MARK); expect(code).toContain('i.inspection_time IS NOT NULL'); expect(code).toContain('COALESCE(i.compliance_status, 0) <> 30'); }); it('该分支在完工判定里被当作已交付(不置 allFulfilled=false)', () => { expect(code).toContain(ZERO_LOSS_GUARD); const at = code.indexOf(ZERO_LOSS_GUARD); const around = code.slice(Math.max(0, at - 400), at + 60); expect(around).not.toContain('allFulfilled = false'); }); it('顺序正确:待验行优先(还有待验 ⇒ 不得提前完工),零损耗分支在其后', () => { const pendingAt = code.indexOf('if (pending.rowCount)'); const zeroAt = code.indexOf('const zeroLossDone'); const coveredAt = code.indexOf('if (orderedQty > 0 && coveredQty >= orderedQty - 1e-9)'); expect(pendingAt).toBeGreaterThanOrEqual(0); expect(zeroAt).toBeGreaterThan(pendingAt); expect(coveredAt).toBeGreaterThan(zeroAt); }); it('反向验证:摘掉零损耗分支 ⇒ 判据消失(修前形态必须被本断言拒绝)', () => { const stripped = stripComments(withoutZeroLoss(body)); expect(stripped).not.toContain('zeroLossDone'); expect(stripped).not.toContain(ZERO_LOSS_SQL_MARK); // 修前只剩三种放行口径:实收>0、有待验、被抵扣覆盖 expect(stripped).toContain('if (actualQty > 0) continue;'); expect(stripped).toContain('if (pending.rowCount)'); expect(stripped).toContain('coveredQty >= orderedQty - 1e-9'); }); }); describe('与既有「净重为 0」结算分支接上(写 0 并清账,不除零)', () => { const settle = stripComments(functionBody(src, 'settleDemandSheetCostOnCompletion')); it('总净重为 0 时写 settled=0 并清账,不产生 unit_cost 除零', () => { expect(settle).toContain('if (totalInspected <= 0)'); expect(settle).toContain('writeDemandLinkedSettledUnitCost(db, id, 0)'); expect(settle).toContain('clearProcessingLedger()'); // 分摊调用必须在零净重早退之后,否则会被 0 除 const earlyReturnAt = settle.indexOf('if (totalInspected <= 0)'); const allocateAt = settle.indexOf('allocateProcessingCostByWeight('); expect(allocateAt).toBeGreaterThan(earlyReturnAt); }); it('完工后才可能调用结算(不能反过来)', () => { const completedAt = code.indexOf('const completed = allFulfilled && hasPick;'); const settleAt = code.indexOf('settleDemandSheetCostOnCompletion('); expect(completedAt).toBeGreaterThanOrEqual(0); expect(settleAt).toBeGreaterThan(completedAt); }); });