gongxiangp2/app/api/desktop/[...path]/route.deliveries.test.ts

47 lines
2.6 KiB
TypeScript

/** @jest-environment node */
import { GET, POST } from './route'
import { requireDesktop } from '@/lib/desktop-client/service'
import { createDesktopDelivery, getDesktopDelivery, withDesktopDeliveryOutbound } from '@/lib/desktop-client/deliveries'
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
jest.mock('@/lib/desktop-client/service', () => ({ requireDesktop: jest.fn() }))
jest.mock('@/lib/desktop-client/deliveries', () => ({
createDesktopDelivery: jest.fn(), getDesktopDelivery: jest.fn(), withDesktopDeliveryOutbound: jest.fn(),
}))
const endpoint = 'https://platform.test/api/desktop/deliveries'
const account = { id: 'owner', name: '登录账号' }
const receipt = {
requestId: 'request', materialId: 'material', materialType: '自动上架-详情',
state: 'completed' as const, revisionId: 'revision',
}
beforeEach(() => {
jest.resetAllMocks()
jest.mocked(requireDesktop).mockResolvedValue({ id: 'session', account } as Awaited<ReturnType<typeof requireDesktop>>)
})
test('POST preserves the legacy receipt fields and returns the outbound receipt', async () => {
jest.mocked(createDesktopDelivery).mockResolvedValue(receipt)
jest.mocked(withDesktopDeliveryOutbound).mockResolvedValue({ ...receipt, eventId: 'event', outboundStatus: 'pending' })
const input = { projectId: 'project', requestId: 'request' }
const response = await POST(new Request(endpoint, { method: 'POST', body: JSON.stringify(input) }), {
params: { path: ['deliveries'] },
})
expect(createDesktopDelivery).toHaveBeenCalledWith('owner', input, expect.any(Function))
expect(await response.json()).toEqual({ ...receipt, eventId: 'event', outboundStatus: 'pending' })
})
test('GET returns the outbound receipt with listing status and preserves the missing receipt response', async () => {
const context = { params: { path: ['deliveries', 'project', 'detail'] } }
const stored = { ...receipt, listingStatus: null }
jest.mocked(getDesktopDelivery).mockResolvedValueOnce(stored).mockResolvedValueOnce(null)
jest.mocked(withDesktopDeliveryOutbound).mockResolvedValue({ ...stored, eventId: 'event', outboundStatus: 'accepted' })
const request = new Request(endpoint + '/project/detail')
expect(await (await GET(request, context)).json()).toEqual({ ...stored, eventId: 'event', outboundStatus: 'accepted' })
expect(getDesktopDelivery).toHaveBeenCalledWith('owner', 'project', 'detail')
expect(await (await GET(request, context)).json()).toEqual({ delivery: null })
expect(withDesktopDeliveryOutbound).toHaveBeenCalledTimes(1)
})