47 lines
2.5 KiB
TypeScript
47 lines
2.5 KiB
TypeScript
/** @jest-environment node */
|
|
|
|
import { getServerSession } from 'next-auth'
|
|
import { DingWorkflowMetricApiError, getTeamCapacityInsights } from '@/lib/ding-workflow-sync'
|
|
|
|
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
|
|
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
|
|
jest.mock('@/lib/ding-workflow-sync', () => {
|
|
const actual = jest.requireActual('@/lib/ding-workflow-sync')
|
|
return { ...actual, getTeamCapacityInsights: jest.fn() }
|
|
})
|
|
|
|
const { GET } = require('@/app/api/department-dashboard/team-capacity/route') as typeof import('@/app/api/department-dashboard/team-capacity/route')
|
|
|
|
describe('GET /api/department-dashboard/team-capacity', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'manager-1' } })
|
|
;(getTeamCapacityInsights as jest.Mock).mockResolvedValue({ departments: [], summary: { weekStart: '2026-07-13T00:00:00.000Z', weekEnd: '2026-09-07T00:00:00.000Z', departmentCount: 0, syncStatus: 'ready', lastSyncedAt: null } })
|
|
})
|
|
|
|
it('returns capacity insights and disables caching', async () => {
|
|
const response = await GET()
|
|
expect(response.status).toBe(200)
|
|
expect(getTeamCapacityInsights).toHaveBeenCalledWith({ user: { id: 'manager-1' } })
|
|
await expect(response.json()).resolves.toEqual({ success: true, data: { departments: [], summary: { weekStart: '2026-07-13T00:00:00.000Z', weekEnd: '2026-09-07T00:00:00.000Z', departmentCount: 0, syncStatus: 'ready', lastSyncedAt: null } } })
|
|
expect(response.headers.get('cache-control')).toBe('private, no-store, max-age=0')
|
|
})
|
|
|
|
it('maps metric errors to their status', async () => {
|
|
;(getTeamCapacityInsights as jest.Mock).mockRejectedValue(new DingWorkflowMetricApiError('无权查看品牌中心流程指标', 403))
|
|
const response = await GET()
|
|
expect(response.status).toBe(403)
|
|
await expect(response.json()).resolves.toEqual({ success: false, error: '无权查看品牌中心流程指标' })
|
|
})
|
|
|
|
it('hides unexpected error details', async () => {
|
|
;(getTeamCapacityInsights as jest.Mock).mockRejectedValue(new Error('database password leaked'))
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined)
|
|
const response = await GET()
|
|
expect(response.status).toBe(500)
|
|
await expect(response.json()).resolves.toEqual({ success: false, error: '团队产能读取失败,请稍后重试' })
|
|
expect(errorSpy).toHaveBeenCalled()
|
|
errorSpy.mockRestore()
|
|
})
|
|
})
|