import { beforeEach, describe, expect, it, vi } from 'vitest'; const { query, getSupplierPrepPriceAmendConfig } = vi.hoisted(() => ({ query: vi.fn(), getSupplierPrepPriceAmendConfig: vi.fn(), })); vi.mock('../src/db', () => ({ pool: { query, connect: vi.fn() }, getRawPoolQuery: () => vi.fn(), })); vi.mock('../src/services/preparationPriceAmendConfigService', () => ({ getSupplierPrepPriceAmendConfig, })); import { countSupplierPendingPreparations, countSupplierPriceAmendPreparations, } from '../src/services/supplierPreparationStatusService'; import { countPendingProcessingTasks } from '../src/services/processingTaskService'; import { countPendingPickingOrders } from '../src/services/orderManagementQueryService'; import { countPendingInspectionsAll } from '../src/services/inspectionQueryService'; import { countPendingPurchaserTasks, countPendingBranchPurchaseTasks } from '../src/services/purchaseOrderQueryService'; import { countPendingDriverDeliveryTasks } from '../src/services/deliveryTaskQueryService'; describe('mobile badge count SQL', () => { beforeEach(() => { query.mockReset(); getSupplierPrepPriceAmendConfig.mockReset(); getSupplierPrepPriceAmendConfig.mockResolvedValue({ window_hours: 24, enabled: true }); }); it('countSupplierPendingPreparations 对齐 /preparations active 视口径且无 LIMIT', async () => { query.mockResolvedValueOnce({ rows: [{ c: 4 }] }); const count = await countSupplierPendingPreparations('SUP1'); expect(count).toBe(4); const [sql, params] = query.mock.calls[0]; const text = String(sql); expect(text).toContain('FROM supplier_preparations'); expect(text).toContain('sp.supplier_code = $1'); expect(text).toMatch(/sp\.prepare_status\s*<\s*30/); expect(text).toContain('is_history_repair'); expect(text).toContain('_o_active.deleted_at IS NULL'); expect(text).not.toMatch(/LIMIT/i); expect(params).toEqual(['SUP1']); }); it('countSupplierPendingPreparations 空供应商直接返回 0 且不打库', async () => { expect(await countSupplierPendingPreparations(' ')).toBe(0); expect(query).not.toHaveBeenCalled(); }); it('countSupplierPriceAmendPreparations 对齐 view=price_amend 且无 LIMIT', async () => { query.mockResolvedValueOnce({ rows: [{ c: 3 }] }); const count = await countSupplierPriceAmendPreparations('SUP1', 'B1'); expect(count).toBe(3); expect(getSupplierPrepPriceAmendConfig).toHaveBeenCalledWith('B1'); const [sql, params] = query.mock.calls[0]; const text = String(sql); expect(text).toContain('FROM supplier_preparations'); expect(text).toMatch(/sp\.prepare_status\s*=\s*30/); expect(text).toContain('transferred_to_purchase'); expect(text).toContain('is_stockout'); expect(text).toContain("INTERVAL '1 hour'"); expect(text).toContain('is_history_repair'); expect(text).toContain('_o_active.deleted_at IS NULL'); expect(text).not.toMatch(/LIMIT/i); expect(params).toEqual(['SUP1', 24]); }); it('countSupplierPriceAmendPreparations window_hours=0 返回 0 且不 COUNT 备货表', async () => { getSupplierPrepPriceAmendConfig.mockResolvedValueOnce({ window_hours: 0, enabled: false }); expect(await countSupplierPriceAmendPreparations('SUP1', 'B1')).toBe(0); expect(query).not.toHaveBeenCalled(); }); it('countPendingProcessingTasks 覆盖今天+下单日轴 work/inspect 且含无订单需求单、无 LIMIT', async () => { query.mockResolvedValueOnce({ rows: [{ c: 9 }] }); const count = await countPendingProcessingTasks(); expect(count).toBe(9); const text = String(query.mock.calls[0][0]); expect(text).toContain('FROM processing_tasks'); expect(text).toContain('LEFT JOIN orders o'); expect(text).toMatch(/pt\.process_status\s+IN\s*\(\s*10\s*,\s*20\s*,\s*30\s*\)/); expect(text).toContain('pt.order_code IS NULL'); expect(text).toContain('_o_active.deleted_at IS NULL'); expect(text).toContain("AT TIME ZONE 'Asia/Shanghai'"); expect(text).toContain('pt.created_at'); expect(text).not.toContain('o.delivery_date'); expect(text).not.toMatch(/LIMIT/i); }); it('countPendingInspectionsAll 覆盖全部待验且订单未删除、无日期、无 LIMIT', async () => { query.mockResolvedValueOnce({ rows: [{ c: 2 }] }); const count = await countPendingInspectionsAll(); expect(count).toBe(2); const text = String(query.mock.calls[0][0]); expect(text).toContain('FROM inspections'); expect(text).toContain('i.inspection_time IS NULL'); expect(text).toContain('_o_active.deleted_at IS NULL'); expect(text).not.toContain('LEFT JOIN orders'); expect(text).not.toContain('delivery_date'); expect(text).not.toMatch(/LIMIT/i); expect(text).not.toContain('source_type'); }); it('countPendingPurchaserTasks 覆盖今天+下单日待采、本人或自由池、无 LIMIT', async () => { query.mockResolvedValueOnce({ rows: [{ c: 4 }] }); const count = await countPendingPurchaserTasks('B1', 'P1'); expect(count).toBe(4); const [sql, params] = query.mock.calls[0]; const text = String(sql); expect(text).toContain('FROM purchase_orders'); expect(text).toContain('LEFT JOIN orders o'); expect(text).toMatch(/po\.purchase_status\s+IN\s*\(\s*10\s*,\s*32\s*\)/); expect(text).toContain('_o_active.deleted_at IS NULL'); expect(text).toContain('o.created_at'); expect(text).toContain("AT TIME ZONE 'Asia/Shanghai'"); expect(text).not.toMatch(/LIMIT/i); expect(params).toEqual(['B1', 'P1']); }); it('countPendingBranchPurchaseTasks 覆盖今天+下单日待采、分公司全部、无 LIMIT', async () => { query.mockResolvedValueOnce({ rows: [{ c: 7 }] }); const count = await countPendingBranchPurchaseTasks('B1'); expect(count).toBe(7); const [sql, params] = query.mock.calls[0]; const text = String(sql); expect(text).toContain('FROM purchase_orders'); expect(text).toContain('LEFT JOIN orders o'); expect(text).toMatch(/po\.purchase_status\s+IN\s*\(\s*10\s*,\s*32\s*\)/); expect(text).toContain('_o_active.deleted_at IS NULL'); expect(text).toContain('o.created_at'); expect(text).not.toContain('purchaser_code = $2'); expect(text).not.toMatch(/LIMIT/i); expect(params).toEqual(['B1']); }); it('countPendingPickingOrders 数今天配送日待处理 status<50、排除垃圾桶且无 LIMIT', async () => { query.mockResolvedValueOnce({ rows: [{ c: 6 }] }); const count = await countPendingPickingOrders(); expect(count).toBe(6); const text = String(query.mock.calls[0][0]); expect(text).toContain('FROM orders o'); expect(text).toMatch(/o\.order_status\s*<\s*50/); expect(text).toContain('o.deleted_at IS NULL'); expect(text).toContain('o.delivery_date::date'); expect(text).toContain("AT TIME ZONE 'Asia/Shanghai'"); expect(text).not.toMatch(/LIMIT/i); }); it('countPendingDriverDeliveryTasks 数今天配送日未完成且无 LIMIT', async () => { query.mockResolvedValueOnce({ rows: [{ c: 3 }] }); const count = await countPendingDriverDeliveryTasks({ personCode: 'D1', branchCode: 'B1' }); expect(count).toBe(3); const [sql, params] = query.mock.calls[0]; const text = String(sql); expect(text).toContain('FROM delivery_tasks dt'); expect(text).toContain('INNER JOIN orders o'); expect(text).toMatch(/dt\.task_status[\s\S]*<\s*30/); expect(text).toMatch(/o\.order_status[\s\S]*>=\s*50/); expect(text).toContain('o.delivery_date'); expect(text).toContain("AT TIME ZONE 'Asia/Shanghai'"); expect(text).not.toMatch(/LIMIT/i); expect(params[0]).toBe('D1'); expect(params[1]).toBe('B1'); }); });