PromptDetail.test.tsx 3.1 KB
import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import PromptDetail from '../pages/Prompts/PromptDetail';

const mockPrompt = {
  id: 'test-id-1',
  template_type: 'general',
  title: '通用质检提示词',
  code: 'TEMPLATE-GENERAL-001',
  system_prompt: '你是一个专业的质检审核助手。',
  user_prompt: '请审核以下内容:{{content}}',
  variables: [{ name: 'content', type: 'string' }],
  version: 3,
  status: 'published',
  created_at: '2026-06-01T10:00:00Z',
  updated_at: '2026-06-15T10:00:00Z',
  versions: [
    {
      id: 'v3-id',
      version: 3,
      system_prompt: '你是一个专业的质检审核助手。',
      user_prompt: '请审核以下内容:{{content}}',
      variables: [{ name: 'content', type: 'string' }],
      created_at: '2026-06-15T10:00:00Z',
    },
    {
      id: 'v2-id',
      version: 2,
      system_prompt: '你是一个质检审核助手。',
      user_prompt: '请审核:{{content}}',
      variables: [{ name: 'content', type: 'string' }],
      created_at: '2026-06-10T10:00:00Z',
    },
    {
      id: 'v1-id',
      version: 1,
      system_prompt: '你是一个质检审核助手。',
      user_prompt: null,
      variables: null,
      created_at: '2026-06-01T10:00:00Z',
    },
  ],
};

vi.mock('../stores/promptStore', () => ({
  usePromptStore: vi.fn(() => ({
    detail: mockPrompt,
    detailLoading: false,
    getPromptDetail: vi.fn().mockResolvedValue(mockPrompt),
  })),
}));

describe('PromptDetail', () => {
  const baseProps = {
    open: true,
    promptId: 'test-id-1',
    onClose: vi.fn(),
  };

  it('渲染模板详情标题', () => {
    render(<PromptDetail {...baseProps} />);
    expect(screen.getByText('模板详情')).toBeInTheDocument();
  });

  it('渲染基本信息区域', () => {
    render(<PromptDetail {...baseProps} />);
    expect(screen.getByText('基本信息')).toBeInTheDocument();
    expect(screen.getByText('通用质检提示词')).toBeInTheDocument();
    expect(screen.getByText(/TEMPLATE-GENERAL-001/)).toBeInTheDocument();
  });

  it('渲染当前内容区域', () => {
    render(<PromptDetail {...baseProps} />);
    expect(screen.getByText('当前内容')).toBeInTheDocument();
    const promptElements = screen.getAllByText('你是一个专业的质检审核助手。');
    expect(promptElements.length).toBeGreaterThanOrEqual(1);
  });

  it('渲染版本历史区域', () => {
    render(<PromptDetail {...baseProps} />);
    expect(screen.getByText('版本历史')).toBeInTheDocument();
    expect(screen.getByText('3 个版本')).toBeInTheDocument();
  });

  it('渲染版本号标签', () => {
    render(<PromptDetail {...baseProps} />);
    const v3Elements = screen.getAllByText('v3');
    expect(v3Elements.length).toBeGreaterThanOrEqual(1);
    expect(screen.getByText('v2')).toBeInTheDocument();
    expect(screen.getByText('v1')).toBeInTheDocument();
  });

  it('渲染当前版本标记', () => {
    render(<PromptDetail {...baseProps} />);
    const elements = screen.getAllByText('当前版本');
    expect(elements.length).toBeGreaterThanOrEqual(1);
  });
});