import express from 'express'; import request from 'supertest'; import { beforeEach, describe, expect, it, vi } from 'vitest'; const mocks = vi.hoisted(() => ({ countPendingPrepAmends: vi.fn(), countSupplierPendingPreparations: vi.fn(), countSupplierPriceAmendPreparations: vi.fn(), countAdminPendingPreparationsToday: vi.fn(), countPendingProcessingTasks: vi.fn(), countPendingPickingOrders: vi.fn(), countPendingInspectionsAll: vi.fn(), countPendingPurchaserTasks: vi.fn(), countPendingBranchPurchaseTasks: vi.fn(), countSupplierPendingBidInvitations: vi.fn(), countMyPendingPickingTasks: vi.fn(), countSupplierPendingQualityReports: vi.fn(), countPendingDriverDeliveryTasks: vi.fn(), getPurchaserCode: vi.fn(), getSupplierCode: vi.fn(), getDeliveryPersonCode: vi.fn(), loadTenantContext: vi.fn(), })); vi.mock('../src/middleware/auth', () => ({ authMiddleware: (req: any, res: any, next: any) => { const userId = req.headers['x-test-user-id']; if (!userId) { return res.status(401).json({ error: 'invalid_token' }); } req.userId = String(userId); req.tokenScope = String(req.headers['x-test-scope'] || 'mobile'); req.tokenRoleType = String(req.headers['x-test-role'] || ''); next(); }, })); vi.mock('../src/services/tenantScope', () => ({ loadTenantContext: mocks.loadTenantContext })); vi.mock('../src/services/prepAmendRequestService', () => ({ countPendingPrepAmends: mocks.countPendingPrepAmends, })); vi.mock('../src/services/supplierPreparationStatusService', () => ({ countSupplierPendingPreparations: mocks.countSupplierPendingPreparations, countSupplierPriceAmendPreparations: mocks.countSupplierPriceAmendPreparations, countAdminPendingPreparationsToday: mocks.countAdminPendingPreparationsToday, })); vi.mock('../src/services/processingTaskService', () => ({ countPendingProcessingTasks: mocks.countPendingProcessingTasks, })); vi.mock('../src/services/orderManagementQueryService', () => ({ countPendingPickingOrders: mocks.countPendingPickingOrders, })); vi.mock('../src/services/inspectionQueryService', () => ({ countPendingInspectionsAll: mocks.countPendingInspectionsAll, })); vi.mock('../src/services/purchaseOrderQueryService', () => ({ countPendingPurchaserTasks: mocks.countPendingPurchaserTasks, countPendingBranchPurchaseTasks: mocks.countPendingBranchPurchaseTasks, })); vi.mock('../src/services/bidRoundService', () => ({ countSupplierPendingBidInvitations: mocks.countSupplierPendingBidInvitations, })); vi.mock('../src/services/pickingService', () => ({ countMyPendingPickingTasks: mocks.countMyPendingPickingTasks, })); vi.mock('../src/services/deliveryTaskQueryService', () => ({ countPendingDriverDeliveryTasks: mocks.countPendingDriverDeliveryTasks, })); vi.mock('../src/services/qualityReportService', () => ({ countSupplierPendingQualityReports: mocks.countSupplierPendingQualityReports, })); vi.mock('../src/routes/mobile/mobileShared', () => ({ getSupplierCode: mocks.getSupplierCode, getPurchaserCode: mocks.getPurchaserCode, getDeliveryPersonCode: mocks.getDeliveryPersonCode, })); import mobileBadgeRoutes from '../src/routes/mobile/mobileBadgeRoutes'; function buildApp() { const app = express(); app.use('/api/mobile', mobileBadgeRoutes); return app; } function authed(req: request.Test, scope = 'mobile', role = '') { return req .set('x-test-user-id', 'u1') .set('x-test-scope', scope) .set('x-test-role', role); } function mockCtx(overrides: Record = {}) { return { userId: 'u1', roles: ['operator'], permissions: [], branchCode: 'B1', supplierCode: null, purchaserCode: null, isSuperAdmin: false, ...overrides, }; } describe('GET /api/mobile/badges', () => { beforeEach(() => { vi.clearAllMocks(); mocks.loadTenantContext.mockResolvedValue(mockCtx()); mocks.countPendingPrepAmends.mockResolvedValue(3); mocks.countSupplierPendingPreparations.mockResolvedValue(5); mocks.countSupplierPriceAmendPreparations.mockResolvedValue(2); mocks.countAdminPendingPreparationsToday.mockResolvedValue(4); mocks.countPendingProcessingTasks.mockResolvedValue(7); mocks.countPendingPickingOrders.mockResolvedValue(11); mocks.countPendingInspectionsAll.mockResolvedValue(1); mocks.countPendingPurchaserTasks.mockResolvedValue(4); mocks.countPendingBranchPurchaseTasks.mockResolvedValue(12); mocks.countSupplierPendingBidInvitations.mockResolvedValue(6); mocks.countMyPendingPickingTasks.mockResolvedValue(9); mocks.countSupplierPendingQualityReports.mockResolvedValue(8); mocks.countPendingDriverDeliveryTasks.mockResolvedValue(5); mocks.getSupplierCode.mockResolvedValue(undefined); mocks.getPurchaserCode.mockResolvedValue(undefined); mocks.getDeliveryPersonCode.mockResolvedValue(undefined); }); it('未登录返回 401', async () => { const res = await request(buildApp()).get('/api/mobile/badges'); expect(res.status).toBe(401); }); it('非移动端 token 返回 403', async () => { const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'supplier'); expect(res.status).toBe(403); }); it('默认返回账号有权且已支持的全部模块计数', async () => { const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'operator'); expect(res.status).toBe(200); expect(res.body.data).toEqual({ orderPickingPendingCount: 11, adminPrepPendingBadgeCount: 4, }); expect(res.body.role_type).toBe('operator'); expect(res.body.available_roles).toBeUndefined(); expect(mocks.countPendingPickingOrders).toHaveBeenCalledTimes(1); expect(mocks.countAdminPendingPreparationsToday).toHaveBeenCalledTimes(1); expect(mocks.countPendingPrepAmends).not.toHaveBeenCalled(); expect(mocks.countSupplierPendingPreparations).not.toHaveBeenCalled(); expect(mocks.countSupplierPriceAmendPreparations).not.toHaveBeenCalled(); expect(mocks.countSupplierPendingBidInvitations).not.toHaveBeenCalled(); expect(mocks.countSupplierPendingQualityReports).not.toHaveBeenCalled(); expect(mocks.countMyPendingPickingTasks).not.toHaveBeenCalled(); expect(mocks.countPendingProcessingTasks).not.toHaveBeenCalled(); expect(mocks.countPendingInspectionsAll).not.toHaveBeenCalled(); expect(mocks.countPendingPurchaserTasks).not.toHaveBeenCalled(); expect(mocks.countPendingBranchPurchaseTasks).not.toHaveBeenCalled(); }); it('供应商账号:供货/追加改价/竞价/报告上传一起返回', async () => { mocks.loadTenantContext.mockResolvedValue( mockCtx({ roles: ['supplier'], supplierCode: 'SUP1' }) ); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'supplier'); expect(res.status).toBe(200); expect(res.body.data).toEqual({ preparationTaskCount: 5, prepPriceAmendCount: 2, bidPendingQuoteCount: 6, qualityReportPendingCount: 8, }); expect(mocks.countSupplierPendingPreparations).toHaveBeenCalledWith('SUP1'); expect(mocks.countSupplierPriceAmendPreparations).toHaveBeenCalledWith('SUP1', 'B1'); expect(mocks.countSupplierPendingBidInvitations).toHaveBeenCalledWith('SUP1'); expect(mocks.countSupplierPendingQualityReports).toHaveBeenCalledWith('SUP1'); expect(mocks.getSupplierCode).not.toHaveBeenCalled(); }); it('supplier 角色但 ctx 无 supplierCode 时,回退 supplier_users 绑定解析', async () => { mocks.loadTenantContext.mockResolvedValue(mockCtx({ roles: ['supplier'], supplierCode: null })); mocks.getSupplierCode.mockResolvedValue('SUP9'); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'supplier'); expect(res.status).toBe(200); expect(res.body.data).toEqual({ preparationTaskCount: 5, prepPriceAmendCount: 2, bidPendingQuoteCount: 6, qualityReportPendingCount: 8, }); expect(mocks.countSupplierPendingPreparations).toHaveBeenCalledWith('SUP9'); expect(mocks.countSupplierPriceAmendPreparations).toHaveBeenCalledWith('SUP9', 'B1'); expect(mocks.countSupplierPendingBidInvitations).toHaveBeenCalledWith('SUP9'); expect(mocks.countSupplierPendingQualityReports).toHaveBeenCalledWith('SUP9'); }); it('supplier 角色完全未绑定供应商时计数为 0', async () => { mocks.loadTenantContext.mockResolvedValue(mockCtx({ roles: ['supplier'], supplierCode: null })); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'supplier'); expect(res.status).toBe(200); expect(res.body.data).toEqual({ preparationTaskCount: 0, prepPriceAmendCount: 0, bidPendingQuoteCount: 0, qualityReportPendingCount: 0, }); expect(mocks.countSupplierPendingPreparations).not.toHaveBeenCalled(); expect(mocks.countSupplierPriceAmendPreparations).not.toHaveBeenCalled(); expect(mocks.countSupplierPendingBidInvitations).not.toHaveBeenCalled(); expect(mocks.countSupplierPendingQualityReports).not.toHaveBeenCalled(); }); it('运营叠加供应商:管理侧与供应商侧两套键并存且互不覆盖', async () => { mocks.loadTenantContext.mockResolvedValue( mockCtx({ roles: ['operator', 'supplier'], supplierCode: 'SUP1' }) ); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'operator'); expect(res.status).toBe(200); expect(res.body.data).toEqual({ orderPickingPendingCount: 11, adminPrepPendingBadgeCount: 4, preparationTaskCount: 5, prepPriceAmendCount: 2, bidPendingQuoteCount: 6, qualityReportPendingCount: 8, }); expect(mocks.countPendingPrepAmends).not.toHaveBeenCalled(); expect(mocks.countSupplierPriceAmendPreparations).toHaveBeenCalledWith('SUP1', 'B1'); expect(mocks.countSupplierPendingBidInvitations).toHaveBeenCalledWith('SUP1'); expect(mocks.countSupplierPendingQualityReports).toHaveBeenCalledWith('SUP1'); }); it('采购员账号:purchasePendingBadgeCount', async () => { mocks.loadTenantContext.mockResolvedValue(mockCtx({ roles: ['purchaser'], purchaserCode: 'P1' })); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'purchaser'); expect(res.status).toBe(200); expect(res.body.data).toEqual({ prepAmendPendingCount: 3, purchasePendingBadgeCount: 4, }); expect(mocks.countPendingPurchaserTasks).toHaveBeenCalledWith('B1', 'P1', 'created_at'); expect(mocks.countPendingBranchPurchaseTasks).not.toHaveBeenCalled(); }); it('管理员仅绑采购员档案:也按采购员本人待采出红点,不返回全量角标', async () => { mocks.loadTenantContext.mockResolvedValue( mockCtx({ roles: ['admin'], purchaserCode: null, branchCode: 'B1' }) ); mocks.getPurchaserCode.mockResolvedValue('P9'); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'admin'); expect(res.status).toBe(200); expect(res.body.data).toEqual(expect.objectContaining({ purchasePendingBadgeCount: 4 })); expect(res.body.data.adminPurchasePendingBadgeCount).toBeUndefined(); expect(mocks.getPurchaserCode).toHaveBeenCalledWith('u1'); expect(mocks.countPendingPurchaserTasks).toHaveBeenCalledWith('B1', 'P9', 'created_at'); expect(mocks.countPendingBranchPurchaseTasks).not.toHaveBeenCalled(); }); it('仓管账号:processingPendingCount 与 inspectionPendingBadgeCount', async () => { mocks.loadTenantContext.mockResolvedValue( mockCtx({ roles: ['warehouse'], permissions: ['processing_manage', 'inspection_manage'] }) ); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'warehouse'); expect(res.status).toBe(200); expect(res.body.data).toEqual({ processingPendingCount: 7, inspectionPendingBadgeCount: 1, }); }); it('未勾仓管的管理员:即使有验收/加工权限码也不出加工/入库角标', async () => { mocks.loadTenantContext.mockResolvedValue( mockCtx({ roles: ['admin'], permissions: ['processing_manage', 'inspection_manage', 'inventory_manage'], }) ); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'admin'); expect(res.status).toBe(200); expect(res.body.data.processingPendingCount).toBeUndefined(); expect(res.body.data.inspectionPendingBadgeCount).toBeUndefined(); expect(mocks.countPendingProcessingTasks).not.toHaveBeenCalled(); expect(mocks.countPendingInspectionsAll).not.toHaveBeenCalled(); }); it('管理员勾了仓管:加工/入库角标按仓管口径返回', async () => { mocks.loadTenantContext.mockResolvedValue( mockCtx({ roles: ['admin', 'warehouse'], permissions: ['processing_manage', 'inspection_manage'], }) ); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'admin'); expect(res.status).toBe(200); expect(res.body.data).toEqual( expect.objectContaining({ processingPendingCount: 7, inspectionPendingBadgeCount: 1, }) ); }); it('分拣员账号:pickingPendingTaskCount 按当前用户计数', async () => { mocks.loadTenantContext.mockResolvedValue(mockCtx({ roles: ['picker'] })); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'picker'); expect(res.status).toBe(200); expect(res.body.data).toEqual({ pickingPendingTaskCount: 9 }); expect(mocks.countMyPendingPickingTasks).toHaveBeenCalledWith( 'u1', expect.objectContaining({ branchCode: 'B1' }) ); }); it('配送员账号:deliveryPendingBadgeCount 与 pickingPendingTaskCount', async () => { mocks.loadTenantContext.mockResolvedValue(mockCtx({ roles: ['delivery'] })); mocks.getDeliveryPersonCode.mockResolvedValue('D1'); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'delivery'); expect(res.status).toBe(200); expect(res.body.data).toEqual({ pickingPendingTaskCount: 9, deliveryPendingBadgeCount: 5, }); expect(mocks.countPendingDriverDeliveryTasks).toHaveBeenCalledWith({ personCode: 'D1', branchCode: 'B1', }); }); it('配送员未绑定配送员档案时 delivery 计数为 0', async () => { mocks.loadTenantContext.mockResolvedValue(mockCtx({ roles: ['delivery'] })); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'delivery'); expect(res.status).toBe(200); expect(res.body.data).toEqual({ pickingPendingTaskCount: 9, deliveryPendingBadgeCount: 0, }); expect(mocks.countPendingDriverDeliveryTasks).not.toHaveBeenCalled(); }); it('modules 过滤:录单员 operator 无权改价待审,未知模块省略', async () => { const res = await authed( request(buildApp()).get('/api/mobile/badges?modules=processing,unknown-mod,prep-amend-review'), 'mobile', 'operator' ); expect(res.status).toBe(200); expect(res.body.data).toEqual({}); expect(mocks.countPendingPrepAmends).not.toHaveBeenCalled(); expect(mocks.countPendingProcessingTasks).not.toHaveBeenCalled(); expect(mocks.countPendingPickingOrders).not.toHaveBeenCalled(); expect(mocks.countAdminPendingPreparationsToday).not.toHaveBeenCalled(); }); it('管理员 modules 过滤仍返回改价待审', async () => { mocks.loadTenantContext.mockResolvedValue(mockCtx({ roles: ['admin'] })); const res = await authed( request(buildApp()).get('/api/mobile/badges?modules=prep-amend-review'), 'mobile', 'admin' ); expect(res.status).toBe(200); expect(res.body.data).toEqual({ prepAmendPendingCount: 3 }); expect(mocks.countPendingPrepAmends).toHaveBeenCalledWith('B1'); }); it('单个模块计数失败只省略该键,不影响其他模块', async () => { mocks.loadTenantContext.mockResolvedValue( mockCtx({ roles: ['purchaser', 'warehouse'], purchaserCode: 'P1', permissions: ['processing_manage', 'inspection_manage'], }) ); mocks.countPendingPrepAmends.mockRejectedValue(new Error('db down')); const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined); const res = await authed(request(buildApp()).get('/api/mobile/badges'), 'mobile', 'purchaser'); consoleSpy.mockRestore(); expect(res.status).toBe(200); expect(res.body.data).toEqual({ processingPendingCount: 7, inspectionPendingBadgeCount: 1, purchasePendingBadgeCount: 4, }); expect(mocks.countPendingPickingOrders).not.toHaveBeenCalled(); expect(mocks.countAdminPendingPreparationsToday).not.toHaveBeenCalled(); }); });