58 lines
2.1 KiB
TypeScript
58 lines
2.1 KiB
TypeScript
/** @jest-environment node */
|
|
|
|
import { getServerSession } from 'next-auth'
|
|
import { GET } from '@/app/api/materials/upload/product-package/status/route'
|
|
import {
|
|
getLatestTaskDetailUploadJobStatus,
|
|
getTaskDetailUploadJobStatus,
|
|
} from '@/lib/task-detail-upload-jobs'
|
|
|
|
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
|
|
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
|
|
jest.mock('@/lib/task-detail-upload-jobs', () => ({
|
|
getLatestTaskDetailUploadJobStatus: jest.fn(),
|
|
getTaskDetailUploadJobStatus: jest.fn(),
|
|
}))
|
|
|
|
function requestWithQuery(query: string) {
|
|
const url = new URL(`http://localhost/api/materials/upload/product-package/status${query}`)
|
|
return { nextUrl: { searchParams: url.searchParams } } as any
|
|
}
|
|
|
|
describe('product-package status route', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'user-1' } })
|
|
})
|
|
|
|
test('未登录拒绝查询', async () => {
|
|
;(getServerSession as jest.Mock).mockResolvedValue(null)
|
|
const response = await GET(requestWithQuery('?jobId=job-1'))
|
|
expect(response.status).toBe(401)
|
|
})
|
|
|
|
test('按 jobId 查询时保持用户隔离', async () => {
|
|
;(getTaskDetailUploadJobStatus as jest.Mock).mockResolvedValue(null)
|
|
const response = await GET(requestWithQuery('?jobId=job-1'))
|
|
expect(response.status).toBe(404)
|
|
expect(getTaskDetailUploadJobStatus).toHaveBeenCalledWith('job-1', 'user-1')
|
|
})
|
|
|
|
test('关闭上传页后可按 materialId 找到最新任务', async () => {
|
|
;(getLatestTaskDetailUploadJobStatus as jest.Mock).mockResolvedValue({
|
|
jobId: 'job-1',
|
|
materialId: 'material-1',
|
|
status: 'processing',
|
|
stage: 'zips',
|
|
progress: 80,
|
|
})
|
|
const response = await GET(requestWithQuery('?materialId=material-1'))
|
|
const body = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(body.data.jobId).toBe('job-1')
|
|
expect(getLatestTaskDetailUploadJobStatus).toHaveBeenCalledWith('material-1', 'user-1')
|
|
expect(getTaskDetailUploadJobStatus).not.toHaveBeenCalled()
|
|
})
|
|
})
|