import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; /** * 订单管理列表的「库存抵扣」小按钮(2026-10-10 开发者要求): * 「这块分配来的订单补一个小按钮『库存抵扣』,点击后走库存抵扣的路线。 * 应对就是我的订单已经分配过来了,但是**库存忘记上新了**这种情况 —— * 因为库存抵扣在分配过来、创建订单时就执行过了。」 * * 钉三件事: * ① 按钮只在**主系统分配过来的单**且未进入配送阶段(<50)时出现; * ② 点击调的是**既有分配路线**(后端 `reprocessOrderAllocation`),不是自己新写抵扣; * ③ 有在途锁(防连点)。 */ const route = readFileSync( new URL('../src/modules/workflow/routes/orderRoutes.ts', import.meta.url), 'utf8' ); /** 逻辑落在 service(路由层不得内嵌 SQL —— `routeLayerBoundary.test.ts` 棘轮) */ const allocSvc = readFileSync( new URL('../src/services/orderAllocationService.ts', import.meta.url), 'utf8' ); const page = readFileSync( new URL('../../src/pages/orders/orderManagement/OrderManagementPage.tsx', import.meta.url), 'utf8' ); const api = readFileSync(new URL('../../src/utils/workflowApi.ts', import.meta.url), 'utf8'); const zh = readFileSync(new URL('../../src/i18n/locales/zh-CN/orders.ts', import.meta.url), 'utf8'); describe('后端:POST /orders/:orderCode/inventory-offset', () => { it('只给主系统分配单用,且配送阶段(>=50)拒绝', () => { const svcIdx = allocSvc.indexOf('export async function applyInventoryOffsetForMainSystemOrder'); expect(svcIdx, '未找到库存抵扣服务函数').toBeGreaterThan(-1); const svc = allocSvc.slice(svcIdx, svcIdx + 3200); expect(svc).toContain('main_system_order_imports'); expect(svc).toContain("kind: 'not_main_system_order'"); expect(svc).toContain("kind: 'too_late'"); expect(svc).toContain('status >= 50'); // 路由只是薄壳:错误码映射 + 调用服务(SQL 一律在 service 里) const idx = route.indexOf("'/orders/:orderCode/inventory-offset'"); expect(idx).toBeGreaterThan(-1); // 只取本路由自己的函数体(到下一个 router.post 为止),别把邻座路由的 SQL 算进来 const next = route.indexOf('router.post(', idx + 10); const body = route.slice(idx, next > idx ? next : idx + 3200); expect(body).toContain('applyInventoryOffsetForMainSystemOrder({'); expect(body).toContain("error: 'inventory_offset_too_late'"); expect(body).not.toContain('SELECT '); }); it('走既有分配路线(reprocessOrderAllocation),不自己实现抵扣', () => { const svcIdx = allocSvc.indexOf('export async function applyInventoryOffsetForMainSystemOrder'); const svc = allocSvc.slice(svcIdx, svcIdx + 4200); expect(svc).toContain('await reprocessOrderAllocation('); expect(svc).toContain('orderCode,'); /** * 2026-10-13:这个入口必须带 `preferItemSuppliers: true`(只落已有供应商、未指定行留待分配)。 * 不带选项时 `useItemSuppliers = !smartEnabled`,而智能分配**默认开** ⇒ 点一次「库存抵扣」 * 就把未分配的行交给规则引擎分配给厂商(真库实测:挂上「测试仓上」且备货直接供应完成, * 抵扣从此轮不到它),与开发者「不能替他直接分配好给谁」正相反。 */ expect(svc).toContain('preferItemSuppliers: true'); // 不直接调 applyStockCoverForOrder(那会留下"既抵扣又采购"的双记) expect(svc).not.toContain('applyStockCoverForOrder('); // 返回可展示的结果(抵扣增量) expect(svc).toContain('coveredDelta'); expect(svc).toContain('message:'); }); }); describe('前端:按钮的出现条件与在途锁', () => { it('只在主系统分配单且状态 < 50 时出现', () => { const idx = page.indexOf('const canApplyInventoryOffset'); expect(idx, '未找到出现条件').toBeGreaterThan(-1); const fn = page.slice(idx, idx + 240); expect(fn).toContain('order.is_main_system_order === true'); expect(fn).toContain('Number(order.order_status) < 50'); // 列表行里用它包住按钮(不是无条件渲染) expect(page).toMatch(/canApplyInventoryOffset\(o\) \? \(/); }); it('有在途锁:点击期间禁用、结束后释放', () => { const idx = page.indexOf('const handleInventoryOffset'); expect(idx).toBeGreaterThan(-1); const body = page.slice(idx, idx + 800); expect(body).toContain('setInventoryOffsetCode(order.order_code)'); expect(body).toContain('finally'); expect(body).toContain('setInventoryOffsetCode(null)'); expect(page).toContain('disabled={inventoryOffsetCode === o.order_code}'); }); it('文案:按钮「库存抵扣」中文为开发者原话,三语键齐', () => { expect(zh).toContain("'list.inventoryOffset': '库存抵扣'"); expect(api).toContain('applyInventoryOffsetApi'); expect(api).toContain('/inventory-offset'); }); });