/** * 免入库 / 商品直送:完成备货后禁止改供应商。 * 设计见 docs/design/20260924-supplier-switch-lock-after-prep.md */ import { readFileSync } from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; import { decideSupplierSwitchLock, supplierSwitchLockError, supplierSwitchLockMessage, } from '../src/services/preparationSwitchLock'; const SRC = path.join(__dirname, '..', 'src'); const read = (rel: string) => readFileSync(path.join(SRC, rel), 'utf8'); describe('免入库/直送 完成备货后禁止改供应商 · 决策表', () => { it('未完成一律放行(完成前的正常分配调整不受限)', () => { expect( decideSupplierSwitchLock({ completed: false, skuDirectDelivery: true, currentSupplierSkipInbound: true, }) ).toBeNull(); }); it('已完成 + SKU 直送 → direct_delivery', () => { expect( decideSupplierSwitchLock({ completed: true, skuDirectDelivery: true }) ).toBe('direct_delivery'); }); it('已完成 + 当前供应商免入库 → skip_inbound', () => { expect( decideSupplierSwitchLock({ completed: true, currentSupplierSkipInbound: true }) ).toBe('skip_inbound'); }); it('已完成 + 当前非免入库(含自采)→ 放行:自采/普通供应商切给免入库供应商也要能用', () => { expect( decideSupplierSwitchLock({ completed: true, skuDirectDelivery: false, currentSupplierSkipInbound: false, }) ).toBeNull(); }); it('错误码与文案按原因区分', () => { const direct = supplierSwitchLockError('direct_delivery', '湿水粉'); expect((direct as { code?: string }).code).toBe('direct_delivery_locked'); expect((direct as { status?: number }).status).toBe(409); expect(direct.message).toContain('湿水粉'); expect(direct.message).toContain('直送'); const skip = supplierSwitchLockError('skip_inbound', '湿水粉'); expect((skip as { code?: string }).code).toBe('skip_inbound_locked'); expect(skip.message).toContain('免入库'); expect(supplierSwitchLockMessage('skip_inbound')).toContain('该商品'); }); }); describe('锁定守卫必须覆盖全部改供应商的门', () => { const doors: Array<[string, string]> = [ ['订单明细保存(含手动分配改供应商)', 'services/orderManageItemsService.ts'], ['备货页分类重新分配 /preparation/reassign-category', 'modules/workflow/routes/preparationRoutes.ts'], ['缺货转自采 preparationStockoutService', 'services/preparationStockoutService.ts'], ]; for (const [label, rel] of doors) { it(`${label} 调用 findSupplierSwitchLock`, () => { const src = read(rel); expect(src).toContain('findSupplierSwitchLock'); expect(src).toContain('supplierSwitchLockError'); }); } it('订单明细保存的守卫只加在"无验收入库"分支(有入库的走改挂,不受限)', () => { const src = read('services/orderManageItemsService.ts'); const idx = src.indexOf('findSupplierSwitchLock'); expect(idx).toBeGreaterThan(0); // 守卫之前应能看到"该行对该供应商无验收入库"的判定(orderItemHasInspectionInboundForSupplier) const before = src.slice(Math.max(0, idx - 1500), idx); expect(before).toContain('orderItemHasInspectionInboundForSupplier'); }); }); describe('回归:锁定查询的 SQL 类型安全(曾因此 500)', () => { /** * 事故:`purchase_orders.source_id` 是 **character varying**(不是 uuid), * 写成 `po.source_id = $2::uuid` 会抛 42883 * `operator does not exist: character varying = uuid`,表现为「改供应商保存直接 500」。 * 必须按文本比较(对 varchar 与 uuid 列都成立)。 */ it('source_id 必须按文本比较,不得强转 uuid', () => { // 只看可执行代码:本文件注释里为说明事故写了错误写法,不能误伤 const code = read('services/preparationSwitchLock.ts') .split('\n') .filter((line) => !line.trim().startsWith('//')) .join('\n'); expect(code).toContain('po.source_id::text = $2::text'); expect(code).not.toMatch(/source_id\s*=\s*\$2::uuid/); }); it('uuid 列(order_item_fulfillments.order_item_id)仍按 uuid 比较', () => { const src = read('services/preparationSwitchLock.ts'); expect(src).toContain('f.order_item_id = $2::uuid'); }); it('判定必须带上 itemId 才能覆盖"完成态自采单"分支(曾因漏传而漏测)', () => { const src = read('services/orderManageItemsService.ts'); const idx = src.indexOf('await findSupplierSwitchLock(client, {'); expect(idx).toBeGreaterThan(0); const call = src.slice(idx, idx + 400); expect(call).toContain('itemId: id'); expect(call).toContain('skuCode:'); expect(call).toContain('currentSupplierCode: fromCode'); }); });