2026-09-01 12:09:08 +08:00

55 lines
2.2 KiB
TypeScript

/** @jest-environment node */
import { getServerSession } from 'next-auth'
import { getMonthlyLaunchSummary, DingWorkflowMetricApiError } 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,
getMonthlyLaunchSummary: jest.fn(),
}
})
const { GET } = require('@/app/api/department-dashboard/core-metrics/monthly-launches/route') as typeof import('@/app/api/department-dashboard/core-metrics/monthly-launches/route')
describe('GET /api/department-dashboard/core-metrics/monthly-launches', () => {
beforeEach(() => {
jest.clearAllMocks()
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'manager-1' } })
;(getMonthlyLaunchSummary as jest.Mock).mockResolvedValue({ completed: 2, ongoing: 1, total: 3 })
})
it('passes month filter to the service', async () => {
const response = await GET(new Request('https://sc.jiansun.vip/api/department-dashboard/core-metrics/monthly-launches?month=2026-08'))
expect(response.status).toBe(200)
expect(getMonthlyLaunchSummary).toHaveBeenCalledWith(
{ user: { id: 'manager-1' } },
{ month: '2026-08', startDate: null, endDate: null },
)
})
it('accepts inclusive custom date range aliases', async () => {
const response = await GET(new Request('https://sc.jiansun.vip/api/department-dashboard/core-metrics/monthly-launches?start=2026-08-01&end=2026-08-31'))
expect(response.status).toBe(200)
expect(getMonthlyLaunchSummary).toHaveBeenCalledWith(
{ user: { id: 'manager-1' } },
{ month: null, startDate: '2026-08-01', endDate: '2026-08-31' },
)
})
it('returns service errors with their status', async () => {
;(getMonthlyLaunchSummary as jest.Mock).mockRejectedValue(new DingWorkflowMetricApiError('month格式应为YYYY-MM', 400))
const response = await GET(new Request('https://sc.jiansun.vip/api/department-dashboard/core-metrics/monthly-launches?month=2026/08'))
expect(response.status).toBe(400)
await expect(response.json()).resolves.toEqual({ success: false, error: 'month格式应为YYYY-MM' })
})
})