gongxiangp2/lib/__tests__/desktop-template-catchall.test.ts

41 lines
2.1 KiB
TypeScript

/** @jest-environment node */
import { GET } from '@/app/api/desktop/[...path]/route'
import { requireDesktop } from '@/lib/desktop-client/service'
import { DesktopClientError } from '@/lib/desktop-client/contract.mjs'
import pack from '@/lib/desktop-client/template-packs/gj-desk-dnz0385-native-v1.json'
jest.mock('@/lib/desktop-client/service', () => ({ requireDesktop: jest.fn() }))
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
const context = { params: { path: ['template-packs'] } }
const url = `https://platform.test/api/desktop/template-packs?brandId=${pack.brandId}&categoryId=${pack.categoryId}`
describe('preserved live desktop template endpoint', () => {
beforeEach(() => jest.clearAllMocks())
test('checks current device authority before returning template data', async () => {
for (const status of [401, 403]) {
;(requireDesktop as jest.Mock).mockRejectedValueOnce(new DesktopClientError('无权访问', status, 'denied'))
const response = await GET(new Request(url), context)
expect(response.status).toBe(status)
expect(response.headers.get('cache-control')).toBe('private, no-store')
expect(await response.json()).not.toHaveProperty('items')
}
expect(requireDesktop).toHaveBeenCalledTimes(2)
})
test('returns the exact brand package and rejects missing or duplicate parameters', async () => {
;(requireDesktop as jest.Mock).mockResolvedValue({ account: { id: 'owner' } })
const response = await GET(new Request(url), context)
expect(response.status).toBe(200)
expect((await response.json()).items[0]).toMatchObject({ id: pack.id, contentHash: pack.contentHash })
for (const query of ['', `?brandId=${pack.brandId}`, `?brandId=${pack.brandId}&brandId=${pack.brandId}&categoryId=${pack.categoryId}`]) {
const invalid = await GET(new Request('https://platform.test/api/desktop/template-packs' + query), context)
expect(invalid.status).toBe(400)
expect(invalid.headers.get('cache-control')).toBe('private, no-store')
expect(await invalid.json()).not.toHaveProperty('items')
}
})
})