gongxiangp2/components/detail-generator/DetailProjectBrowser.test.tsx

100 lines
9.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React from 'react'
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react'
import DetailProjectBrowser from './DetailProjectBrowser'
jest.mock('next/link', () => function Link({ href, children, ...props }: any) { return <a href={href} {...props}>{children}</a> })
jest.mock('./DetailCanvas', () => function Canvas({ disabled }: any) { return <div>只读:{String(disabled)}</div> })
jest.mock('./DetailLayerCanvas', () => function Canvas({ disabled }: any) { return <div>只读:{String(disabled)}</div> })
const item = { id: 'one', source: 'desktop', name: '顾家电脑桌', status: 'draft', hasDraft: true, revision: 7, screenCount: 0 }
const reply = (value: unknown, ok = true) => Promise.resolve({ ok, json: async () => value })
const originalFetch = global.fetch
beforeAll(() => {
HTMLDialogElement.prototype.showModal = function () { this.setAttribute('open', '') }
HTMLDialogElement.prototype.close = function () { this.removeAttribute('open') }
})
beforeEach(() => { global.fetch = jest.fn(() => reply({ items: [item], nextCursor: null })) as any })
afterAll(() => { global.fetch = originalFetch })
test('web projects are browse-only and retain saved draft information without create or model calls', async () => {
;(global.fetch as jest.Mock).mockImplementation((url: string) => url === '/api/detail-projects' ? reply({ items: [item], nextCursor: null }) : reply({ source: 'desktop', project: { id: 'one', document: { screens: [], brief: { note: '长1200mm,待确认材质' } } }, assets: [] }))
render(<DetailProjectBrowser />)
fireEvent.click(await screen.findByRole('button', { name: '查看 顾家电脑桌' }))
expect(await screen.findByText('长1200mm,待确认材质')).toBeInTheDocument()
expect(screen.queryByRole('button', { name: /新建|生成|编辑|保存/ })).not.toBeInTheDocument()
expect((global.fetch as jest.Mock).mock.calls.every(([, init]) => !init?.method)).toBe(true)
})
test('deletion explains cloud and client scope, cancel does not send a deletion, confirm sends one revision', async () => {
render(<DetailProjectBrowser />)
fireEvent.click(await screen.findByRole('button', { name: '删除 顾家电脑桌' }))
expect(screen.getByText(/将删除云端项目及关联草稿/)).toBeInTheDocument()
fireEvent.click(within(screen.getByRole('dialog')).getByRole('button', { name: '取消' }))
expect((global.fetch as jest.Mock).mock.calls.filter(([, init]) => init?.method === 'DELETE')).toHaveLength(0)
fireEvent.click(screen.getByRole('button', { name: '删除 顾家电脑桌' }))
;(global.fetch as jest.Mock).mockImplementation(() => reply({ deleted: true, deletedProjects: [{ id: item.id, source: item.source }] }))
fireEvent.click(within(screen.getByRole('dialog')).getByRole('button', { name: '删除项目' }))
await waitFor(() => expect(screen.queryByRole('button', { name: '查看 顾家电脑桌' })).not.toBeInTheDocument())
const calls = (global.fetch as jest.Mock).mock.calls.filter(([, init]) => init?.method === 'DELETE')
expect(calls).toHaveLength(1); expect(JSON.parse(calls[0][1].body)).toEqual({ revision: 7 })
})
test('an ambiguous deletion response keeps the project and never retries automatically', async () => {
render(<DetailProjectBrowser />)
fireEvent.click(await screen.findByRole('button', { name: '删除 顾家电脑桌' }))
;(global.fetch as jest.Mock).mockImplementation(() => Promise.reject(new Error('连接中断')))
fireEvent.click(within(screen.getByRole('dialog')).getByRole('button', { name: '删除项目' }))
expect(await screen.findByRole('alert')).toHaveTextContent('项目仍保留在列表中')
expect(screen.getByRole('button', { name: '查看 顾家电脑桌' })).toBeInTheDocument()
expect((global.fetch as jest.Mock).mock.calls.filter(([, init]) => init?.method === 'DELETE')).toHaveLength(1)
})
test('native project cards and read-only pages use the uploaded first-screen preview without loading a canvas engine', async () => {
const nativeItem = { ...item, screenCount: 1, previewSource: 'local:cover', status: 'completed', hasDraft: false, backupState: 'ready' }
;(global.fetch as jest.Mock).mockImplementation((url: string) => url === '/api/detail-projects' ? reply({ items: [nativeItem], nextCursor: null }) : reply({ source: 'desktop', project: { id: 'one', document: { version: 3, screens: [{ id: 'hero', name: '首屏', width: 1440, height: 2200, previewSource: 'local:cover' }] } }, assets: [{ source: 'local:cover', mime: 'image/png' }] }))
render(<DetailProjectBrowser />)
expect(await screen.findByText('完结')).toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: '查看 顾家电脑桌' }))
const image = await screen.findByRole('img', { name: '首屏' })
expect(image.getAttribute('src')).toBe('/api/detail-projects/desktop/one/assets/local%3Acover')
expect(screen.queryByText('只读:true')).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: /生成|编辑|保存/ })).not.toBeInTheDocument()
})
test.each([['sku', 'SKU'], ['spu', 'SPU'], ['barcode', '条码']])('native %s drafts show saved product details before any AI analysis', async (identifierType, label) => {
const brief = { name: '顾家长桌', brandName: '顾家', identifierType, identifier: 'GBGJ-CYJ0056', colorName: '胡桃木色', source: 'library', referenceImageCount: 3, dimensions: { length: '1200', width: '600', height: '750', unit: 'mm' }, note: '' }
;(global.fetch as jest.Mock).mockImplementation((url: string) => url === '/api/detail-projects' ? reply({ items: [item], nextCursor: null }) : reply({ source: 'desktop', project: { id: 'one', document: { version: 3, screens: [], brief, facts: { items: [] }, sellingPoints: { batches: [] } } }, assets: [] }))
render(<DetailProjectBrowser />)
fireEvent.click(await screen.findByRole('button', { name: '查看 顾家电脑桌' }))
const summary = await screen.findByText('产品资料与卖点')
expect(summary).toHaveClass('min-h-11')
expect(summary.parentElement).not.toHaveAttribute('open')
fireEvent.click(summary)
const info = within(summary.parentElement!)
for (const text of ['顾家长桌', '顾家', label, 'GBGJ-CYJ0056', '胡桃木色', '长 1200 × 宽 600 × 高 750 毫米', '素材库', '3 张']) expect(info.getByText(text)).toBeInTheDocument()
expect(screen.queryByRole('button', { name: /新建|生成|编辑|保存/ })).not.toBeInTheDocument()
expect((global.fetch as jest.Mock).mock.calls.every(([, init]) => !init?.method)).toBe(true)
})
test('complete native projects retain fact and selling-point text alongside partial saved dimensions', async () => {
;(global.fetch as jest.Mock).mockImplementation((url: string) => url === '/api/detail-projects' ? reply({ items: [item], nextCursor: null }) : reply({ source: 'desktop', project: { id: 'one', document: { version: 3, screens: [], brief: { name: '电脑桌', source: 'upload', dimensions: { length: '120', width: '', height: '', unit: 'cm' } }, facts: { items: [{ text: '四条支腿' }] }, sellingPoints: { batches: [{ items: ['四腿支撑结构'] }] } } }, assets: [] }))
render(<DetailProjectBrowser />)
fireEvent.click(await screen.findByRole('button', { name: '查看 顾家电脑桌' }))
expect(await screen.findByText('长 120 厘米')).toBeInTheDocument()
for (const text of ['上传产品资料', '四条支腿', '四腿支撑结构']) expect(screen.getByText(text)).toBeInTheDocument()
for (const label of ['品牌', '颜色', 'SKU', 'SPU', '条码', '参考图片']) expect(screen.queryByText(label)).not.toBeInTheDocument()
})
test('legacy Web product fields and free-text dimensions remain readable without a native brief', async () => {
const document = { screens: [], inputSource: 'new', brand: { name: '朴美' }, product: { name: '新款边桌', identifierType: 'barcode', identifier: '6901234567890', facts: '圆形台面', specifications: { color: '橡木色', dimensions: '宽120cm,深度待定' }, assetIds: ['product-one', 'product-two'], referenceAssetIds: ['scene'] }, analysis: { sellingPoints: ['简洁圆形台面'] } }
;(global.fetch as jest.Mock).mockImplementation((url: string) => url === '/api/detail-projects' ? reply({ items: [item], nextCursor: null }) : reply({ source: 'web', project: { id: 'one', document }, assets: [] }))
render(<DetailProjectBrowser />)
fireEvent.click(await screen.findByRole('button', { name: '查看 顾家电脑桌' }))
for (const text of ['新款边桌', '朴美', '条码', '6901234567890', '橡木色', '宽120cm,深度待定', '上传产品资料', '3 张', '圆形台面', '简洁圆形台面']) expect(await screen.findByText(text)).toBeInTheDocument()
})
test('empty product details omit the disclosure and never invent source, dimensions or image counts', async () => {
;(global.fetch as jest.Mock).mockImplementation((url: string) => url === '/api/detail-projects' ? reply({ items: [item], nextCursor: null }) : reply({ source: 'desktop', project: { id: 'one', document: { version: 3, screens: [], brief: { name: ' ', source: 'unknown', referenceImageCount: -1, dimensions: { length: '', width: '', height: '', unit: 'mm' } } } }, assets: [] }))
render(<DetailProjectBrowser />)
fireEvent.click(await screen.findByRole('button', { name: '查看 顾家电脑桌' }))
expect(await screen.findByText('草稿尚未生成页面,已保存资料可在客户端继续处理。')).toBeInTheDocument()
expect(screen.queryByText('产品资料与卖点')).not.toBeInTheDocument()
})