gongxiangp2/lib/__tests__/desktop-project-viewer.test.ts

57 lines
5.2 KiB
TypeScript

/** @jest-environment node */
import { getServerSession } from 'next-auth'
import { projectViewerRoute } from '@/lib/desktop-client/project-viewer'
import { deleteDesktopProject, desktopUser, getDesktopAsset, getDesktopProject, listDesktopProjects } from '@/lib/desktop-client/service'
import { detailGeneratorClientRoute } from '@/lib/detail-generator/access'
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
jest.mock('@/lib/desktop-client/service', () => ({ desktopUser: jest.fn(), listDesktopProjects: jest.fn(), getDesktopProject: jest.fn(), getDesktopAsset: jest.fn(), deleteDesktopProject: jest.fn() }))
jest.mock('@/lib/prisma', () => ({ prisma: { user: { findUnique: jest.fn(async () => ({ isActive: true, status: 'active' })) } } }))
jest.mock('@/lib/department-permission', () => ({ isSuperAdmin: jest.fn(async () => true) }))
beforeEach(() => {
jest.clearAllMocks(); process.env.DETAIL_GENERATOR_ENABLED = 'true'
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'owner' } })
;(desktopUser as jest.Mock).mockResolvedValue({ id: 'owner' })
})
test('browser list rechecks the current session and only reads the account project service', async () => {
;(listDesktopProjects as jest.Mock).mockResolvedValue({ items: [], nextCursor: null, deleted: [{ id: 'private-tombstone' }] })
const response = await projectViewerRoute(new Request('https://sc.jiansun.vip/api/detail-projects'))
expect(response.status).toBe(200); expect(await response.json()).toEqual({ items: [], nextCursor: null })
expect(desktopUser).toHaveBeenCalledWith('owner'); expect(listDesktopProjects).toHaveBeenCalledWith('owner', null)
})
test('view projection strips credentials, bindings and internal storage keys', async () => {
;(getDesktopProject as jest.Mock).mockResolvedValue({ source: 'desktop', project: { id: 'p', revision: 2, status: 'draft', document: { name: '桌子', screens: [], modelBindings: { provider: 'secret' }, apiKey: 'secret' } }, assets: [{ source: 'local:a', mime: 'image/png', storageKey: 'private/path' }] })
const response = await projectViewerRoute(new Request('https://sc.jiansun.vip/api/detail-projects/desktop/p'), ['desktop', 'p'])
const text = await response.text(); expect(text).not.toContain('secret'); expect(text).not.toContain('private/path')
})
test('native projects expose only rendered previews and creation summaries, never archives or Pi sessions', async () => {
;(getDesktopProject as jest.Mock).mockResolvedValue({ source: 'desktop', project: { id: 'p', revision: 3, status: 'completed', conversation: [{ text: 'private-session' }], document: { version: 3, name: '五屏', native: { documentSource: 'native:private-archive', files: [{ path: 'session-backup.json' }] }, screens: [{ id: 'one', width: 1440, height: 2200, previewSource: 'local:preview' }], creation: { brief: { name: '电脑桌', note: '橡木色' }, facts: { items: [] } } } }, assets: [{ source: 'local:preview', mime: 'image/png' }, { source: 'native:private-archive', mime: 'application/x-photocraft-resource', storageKey: 'private' }] })
const response = await projectViewerRoute(new Request('https://sc.jiansun.vip/api/detail-projects/desktop/p'), ['desktop', 'p'])
const value = await response.json(), text = JSON.stringify(value)
expect(value.project.document.version).toBe(3); expect(value.project.document.screens[0].previewSource).toBe('local:preview')
expect(value.assets).toEqual([{ source: 'local:preview', mime: 'image/png' }])
for (const privateValue of ['private-session','private-archive','session-backup.json','storageKey']) expect(text).not.toContain(privateValue)
})
test('browser deletion requires an allowed origin and sends the exact confirmation revision once', async () => {
const url = 'https://sc.jiansun.vip/api/detail-projects/desktop/p'
;(deleteDesktopProject as jest.Mock).mockResolvedValue({ deleted: true })
const absent = await projectViewerRoute(new Request(url, { method: 'DELETE', body: '{}' }), ['desktop', 'p'])
expect(absent.status).toBe(403); expect(deleteDesktopProject).not.toHaveBeenCalled()
const response = await projectViewerRoute(new Request(url, { method: 'DELETE', headers: { origin: 'https://sc.jiansun.vip' }, body: JSON.stringify({ revision: 4 }) }), ['desktop', 'p'])
expect(response.status).toBe(200); expect(deleteDesktopProject).toHaveBeenCalledTimes(1)
expect(deleteDesktopProject).toHaveBeenCalledWith('owner', 'desktop', 'p', { revision: 4 })
})
test('project images remain owner scoped and only image MIME types are served inline', async () => {
;(getDesktopAsset as jest.Mock).mockResolvedValue({ bytes: Buffer.from('test'), mime: 'application/octet-stream' })
const response = await projectViewerRoute(new Request('https://sc.jiansun.vip/api/detail-projects/desktop/p/assets/a'), ['desktop', 'p', 'assets', 'a'])
expect(getDesktopAsset).toHaveBeenCalledWith('owner', 'desktop', 'p', 'a'); expect(response.status).toBe(415)
})
test('web editing endpoints do not execute their action or dispatch models', async () => {
const action = jest.fn()
const response = await detailGeneratorClientRoute(action)
expect(response.status).toBe(409); expect((await response.json()).code).toBe('client_required'); expect(action).not.toHaveBeenCalled()
})