gongxiangp2/lib/__tests__/desktop-template-packs-route.test.ts
WuYongJie 93fc28f1bb feat(client): add native template slots and safe cloud version restore
Support explicit current-output A/B fill, protected edited drafts, scoped cloud update offers and atomic native restore with stale-open CAS. Preserve original unknown requests, improve Pi conversation continuity and native DPI/navigation reads, and publish the authenticated template metadata contract.

Validated: native backend 172 passed/5 environment tests ignored, UI 110, Pi 53, template/rule API 24 and native cloud contract 6; strict Clippy, layering and wasm. Real S01 selected B, edited/undo-redo, PCraft reopen and layered PSD roundtrip pass with exact pixels. Full nine-output generation and Photoshop acceptance remain open.
2026-10-23 21:27:06 +08:00

60 lines
3.9 KiB
TypeScript

/** @jest-environment node */
import { GET } from '@/app/api/desktop/template-packs/route'
import { requireDesktop } from '@/lib/desktop-client/service'
import { DesktopClientError } from '@/lib/desktop-client/contract.mjs'
import gujia from '@/lib/desktop-client/template-packs/gj-desk-dnz0385-native-v1.json'
jest.mock('@/lib/desktop-client/service', () => ({ requireDesktop: jest.fn() }))
const url = `https://platform.test/api/desktop/template-packs?brandId=${gujia.brandId}&categoryId=${gujia.categoryId}`
describe('native template GET route always reauthorizes', () => {
beforeEach(() => jest.clearAllMocks())
test('missing, expired and revoked authority cannot receive package data or cache hits', async () => {
for (const status of [401, 403]) {
;(requireDesktop as jest.Mock).mockRejectedValueOnce(new DesktopClientError('无权访问', status, 'denied'))
const response = await GET(new Request(url, { headers: { 'If-None-Match': '"some-existing-value"' } }))
expect(response.status).toBe(status)
expect(await response.json()).not.toHaveProperty('items')
expect(response.headers.get('cache-control')).toBe('private, no-store')
expect(response.headers.get('etag')).toBeNull()
}
expect(requireDesktop).toHaveBeenCalledTimes(2)
})
test('valid package, empty pairing, malformed or duplicate parameters all keep private responses', async () => {
;(requireDesktop as jest.Mock).mockResolvedValue({ account: { id: 'owner' } })
const response = await GET(new Request(url))
expect(response.status).toBe(200)
const body = await response.json()
expect(body.items).toHaveLength(1); expect(body.items[0].pack.brandId).toBe(gujia.brandId)
const missing = await GET(new Request(url.replace(gujia.categoryId, '00000000-0000-4000-8000-000000000000')))
expect(missing.status).toBe(200); expect((await missing.json()).items).toEqual([])
for (const query of ['', `?brandId=${gujia.brandId}`, `?brandId=invalid&categoryId=${gujia.categoryId}`, `?brandId=${gujia.brandId}&brandId=${gujia.brandId}&categoryId=${gujia.categoryId}`]) {
const response = await GET(new Request('https://platform.test/api/desktop/template-packs' + query))
expect(response.status).toBe(400)
expect(response.headers.get('cache-control')).toBe('private, no-store')
expect(await response.json()).not.toHaveProperty('items')
}
expect(requireDesktop).toHaveBeenCalledTimes(6)
})
test('a matching ETag is checked only after fresh permission and parameter validation', async () => {
;(requireDesktop as jest.Mock).mockResolvedValue({ account: { id: 'owner' } })
const first = await GET(new Request(url)), etag = first.headers.get('etag')!
expect(etag).toMatch(/^"[a-f0-9]{64}"$/)
const hit = await GET(new Request(url, { headers: { 'If-None-Match': etag } }))
expect(hit.status).toBe(304); expect(await hit.text()).toBe('')
expect(hit.headers.get('cache-control')).toBe('private, no-store')
;(requireDesktop as jest.Mock).mockRejectedValueOnce(new DesktopClientError('授权已撤销', 403, 'denied'))
const denied = await GET(new Request(url, { headers: { 'If-None-Match': etag } }))
expect(denied.status).toBe(403); expect(await denied.json()).not.toHaveProperty('items')
const malformed = await GET(new Request('https://platform.test/api/desktop/template-packs', { headers: { 'If-None-Match': etag } }))
expect(malformed.status).toBe(400); expect(requireDesktop).toHaveBeenCalledTimes(4)
})
test('unexpected failures do not expose internal source paths or server diagnostics', async () => {
;(requireDesktop as jest.Mock).mockRejectedValueOnce(new Error('D:\\private\\connection-password.txt'))
const response = await GET(new Request(url))
expect(response.status).toBe(500)
const body = await response.json()
expect(body).toEqual({ error: '模板暂时无法读取', code: 'internal_error' })
expect(response.headers.get('x-content-type-options')).toBe('nosniff')
})
})