import { describe, expect, it, vi, beforeEach } from 'vitest'; import { readFileSync } from 'node:fs'; import path from 'node:path'; /** * 「区域顺序」弹层的**区域候选来源**(2026-10-12 开发者报障): * 「当前明明存在区域,但是为什么分拣这块无法设置区域顺序、显示为无区域」。 * * 真库根因(BR003 实测): * · 区域实际记在**批次**上 —— `inventory.storage_zone`:冷库A 9 批 / 冷库B 3 批 / 冷库C 1 批; * · `skus.storage_zone`(商品默认区)**全租户 0 行**;zone 拣货单还没拆过(0 张); * · 而原实现只查 `skus` ∪ 已建 zone 任务 ⇒ 两个来源都空 ⇒ 接口 `zones: []` ⇒ 弹层「暂无区域」。 * * 修法:候选来源与**拆分口径同源**(`pickingRouteSortService`:批次实际区优先 → 回退商品默认区), * 且整段在**租户上下文**里查(跨 schema 的租户表用裸 pool 会读不到 —— `pickingWorkflowConfig` 的既有教训)。 */ const serviceSrc = readFileSync( path.join(__dirname, '../src/services/pickingConfigService.ts'), 'utf8' ); const query = vi.hoisted(() => vi.fn()); const withTenant = vi.hoisted(() => vi.fn()); let inTenant: string | null = null; vi.mock('../src/db', () => ({ pool: { query: (...args: unknown[]) => query(...args), connect: vi.fn() }, getRawPoolQuery: () => query, })); vi.mock('../src/services/tenantDbContext', () => ({ runWithTenantDbContext: (...args: unknown[]) => withTenant(...args), })); import { listPickingZones } from '../src/services/pickingConfigService'; beforeEach(() => { query.mockReset(); withTenant.mockReset(); inTenant = null; withTenant.mockImplementation(async (branch: string, fn: (c: unknown) => unknown) => { inTenant = branch; try { return await fn({ query: (...args: unknown[]) => query(...args) }); } finally { inTenant = null; } }); }); describe('区域顺序:候选区域来源', () => { it('① 主来源是**批次实际区**(`inventory.storage_zone`,仅本分公司可扣批次)', () => { const start = serviceSrc.indexOf('export async function listPickingZones'); expect(start).toBeGreaterThan(-1); const body = serviceSrc.slice(start, start + 2600); // 与拆分口径同源:可扣库存 + 区域非空 + 本分公司 expect(body).toMatch(/FROM inventory inv/); expect(body).toMatch(/inv\.available_qty > 0/); expect(body).toMatch(/NULLIF\(TRIM\(inv\.storage_zone\), ''\) IS NOT NULL/); expect(body).toMatch(/inv\.branch_code = \$1/); // 保留原有两个来源(商品默认区、已建 zone 任务)与「未设区域」哨兵排除 expect(body).toMatch(/FROM skus s/); expect(body).toMatch(/pt\.scope_type = 'zone'/); expect(body).toMatch(/pt\.scope_key <> '\$\{UNZONED_SCOPE_KEY\}'/); }); it('② 必须走租户上下文(有分公司时),否则跨 schema 读不到', () => { const start = serviceSrc.indexOf('export async function listPickingZones'); const body = serviceSrc.slice(start, start + 2600); expect(body).toMatch(/runWithTenantDbContext\(branch/); }); it('③ 真库事实:冷库A/B/C 来自批次区(判据与拆分口径同一列)', () => { const routeSort = readFileSync( path.join(__dirname, '../src/services/pickingRouteSortService.ts'), 'utf8' ); // 拆分口径也是"批次实际区优先 → 回退商品默认区",两处必须同列,否则弹层排的区与拆单的区对不上 expect(routeSort).toMatch(/COALESCE\(NULLIF\(TRIM\(bz\.storage_zone\), ''\), NULLIF\(TRIM\(s\.storage_zone\), ''\)\) AS zone/); expect(routeSort).toMatch(/FROM inventory inv/); }); it('④ 行为:返回三库区,且确实在租户上下文里查过', async () => { query.mockResolvedValue({ rowCount: 3, rows: [{ zone: '冷库A' }, { zone: '冷库B' }, { zone: '冷库C' }], }); const zones = await listPickingZones('BR003'); expect(zones).toEqual(['冷库A', '冷库B', '冷库C']); expect(withTenant).toHaveBeenCalledWith('BR003', expect.any(Function)); const [sql, params] = query.mock.calls[0]; expect(sql).toContain('FROM inventory inv'); expect(params).toEqual(['BR003']); }); it('⑤ 没有分公司上下文时退回裸查询(不抛错)', async () => { query.mockResolvedValue({ rowCount: 0, rows: [] }); const zones = await listPickingZones(null); expect(zones).toEqual([]); expect(withTenant).not.toHaveBeenCalled(); expect(query.mock.calls[0][1]).toEqual([null]); }); }); const routeSrc = readFileSync( path.join(__dirname, '../src/modules/workflow/routes/pickingRoutes.ts'), 'utf8' ); /** * 保存「区域顺序」后自动重拆(2026-10-13,开发者选定方案 B)。 * 原来 `PUT /picking/zone-order` 只存顺序、不重拆 ⇒ 调完顺序要手动点「按当前模式重拆」。 * 注:那个按钮已于 2026-10-13 按开发者要求删除(「这个按钮就不需要了」)——现重拆只在 * 「改拣货模式」与「保存区域顺序」两处自动发生。 */ describe('区域顺序保存后自动重拆', () => { it('PUT /picking/zone-order 保存后重拆未开工拣货单并回传 rebuild(可 rebuild:false 关)', () => { const start = routeSrc.indexOf("router.put('/picking/zone-order'"); expect(start).toBeGreaterThan(-1); const body = routeSrc.slice(start, start + 1200); expect(body).toContain('rebuildUntouchedOpenPickingTasks(branchCode, ctx)'); expect(body).toMatch(/rebuild: rebuildResult/); expect(body).toMatch(/req\.body\?\.rebuild !== false/); }); it('前端保存区域顺序后按重拆结果提示并刷新列表', () => { const page = readFileSync( path.join(__dirname, '../../src/pages/picking/PickingManagement.tsx'), 'utf8' ); const start = page.indexOf('const handleSaveZoneOrder'); expect(start).toBeGreaterThan(-1); const body = page.slice(start, start + 1400); expect(body).toContain('data?.rebuild'); expect(body).toContain('modeRebuilt'); expect(body).toContain('await fetchAll()'); }); });