import { readFileSync } from 'fs'; import path from 'path'; import { describe, expect, it } from 'vitest'; import { allocateMergeInspectionQtys } from '../src/services/weighStationService'; /** * 称端合并入库:超量(实收 > 同商品合计待入)不再是无解死路。 * 背景与实测见 docs/design/20260917-weigh-over-qty-confirm.md */ const readSource = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8'); describe('allocateMergeInspectionQtys:超量归属', () => { const lines = [ { id: 'a', prepare_qty: 10 }, { id: 'b', prepare_qty: 10 }, ]; it('未确认超量时:只按各行 prepare 分配,余量留在 unallocated', () => { const r = allocateMergeInspectionQtys(lines, 30, { confirmOverQty: false }); expect(r.sumPrepare).toBe(20); expect(r.allocations).toEqual([ { id: 'a', qty: 10 }, { id: 'b', qty: 10 }, ]); expect(r.unallocated).toBe(10); }); it('已确认超量时:余量记到最后一行,unallocated 归零', () => { const r = allocateMergeInspectionQtys(lines, 30, { confirmOverQty: true }); expect(r.allocations).toEqual([ { id: 'a', qty: 10 }, { id: 'b', qty: 20 }, ]); expect(r.unallocated).toBe(0); }); it('合并层固定按「已确认」调用:任何入参都不会留下 unallocated', () => { // 服务端已不再把 confirmOverQty 绑定到入参(源码契约见下),这里锁住纯函数语义 const r = allocateMergeInspectionQtys(lines, 30, { confirmOverQty: true }); expect(r.unallocated).toBe(0); expect(r.allocations.reduce((s, x) => s + x.qty, 0)).toBe(30); }); it('实收小于合计待入时行为不变(无溢出)', () => { const r = allocateMergeInspectionQtys(lines, 15, { confirmOverQty: false }); expect(r.allocations).toEqual([{ id: 'a', qty: 10 }, { id: 'b', qty: 5 }]); expect(r.unallocated).toBe(0); }); }); describe('服务端源码契约:超量一律不拦,照实入账 + 留痕', () => { const weigh = readSource('src/services/weighStationService.ts'); it('合并层不再有 need_confirm_over 拦截(两个按钮都不拦)', () => { expect(weigh).not.toContain("code: 'need_confirm_over'"); expect(weigh).not.toContain('请确认超量后再入库'); expect(weigh).toContain('confirmOverQty: true'); }); it('超量必须留痕并可查:最后一行写 over_qty_confirmed / over_qty / 来源', () => { for (const key of ['over_qty_confirmed', 'over_qty', 'over_qty_sum_prepare', 'over_qty_confirm_source']) { expect(weigh, key).toContain(key); } expect(weigh).toContain('withOverQtyTrace'); expect(weigh).toContain('i === allocations.length - 1'); // 来源要能区分:偏差入库 / 显式传参 / 自动接受 for (const src of ['force_complete', 'confirm_over_qty', 'auto_accept']) { expect(weigh, src).toContain(src); } }); it('承载超量的那一行同时满足过手超量闸门(口径一致,避免半提交)', () => { expect(weigh).toContain('carriesOverQty ||'); }); it('响应回传超量事实(称端/审计可见)', () => { expect(weigh).toContain('overQty,'); expect(weigh).toContain('overQtyConfirmed:'); }); });