Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

5.1 KB
Raw Blame
import type { RefObject } from 'react'
import type { ChatConfig } from '@/app/components/base/chat/types'
import type { AppConversationData, AppData, AppMeta, ConversationItem } from '@/models/share'
import { useMediaQuery } from '@langgenius/dify-ui/use-media-query'
import { screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
import ChatWithHistory from '@/app/components/base/chat/chat-with-history'
import { useChatWithHistoryContext } from '@/app/components/base/chat/chat-with-history/context'
import { useChatWithHistory } from '@/app/components/base/chat/chat-with-history/hooks'
import { renderWithConsoleQuery as render } from '@/test/console/query-data'

vi.mock('@/app/components/base/chat/chat-with-history/hooks', () => ({
  useChatWithHistory: vi.fn(),
}))

vi.mock('@/app/components/base/chat/chat-with-history/chat-wrapper', () => {
  const ChatThemeProbe = () => {
    const { theme } = useChatWithHistoryContext()

    return (
      <span aria-label="chat theme">
        {theme?.primaryColor}:{String(theme?.chatColorThemeInverted)}
      </span>
    )
  }

  return { default: ChatThemeProbe }
})

vi.mock('@langgenius/dify-ui/use-media-query', () => ({
  useMediaQuery: vi.fn(),
}))

vi.mock('@/next/navigation', () => ({
  useRouter: vi.fn(() => ({
    push: vi.fn(),
    replace: vi.fn(),
    prefetch: vi.fn(),
  })),
  usePathname: vi.fn(() => '/'),
  useSearchParams: vi.fn(() => new URLSearchParams()),
  useParams: vi.fn(() => ({})),
}))

type HookReturn = ReturnType<typeof useChatWithHistory>

const mockAppData = {
  site: { title: 'Test Chat', chat_color_theme: 'blue', chat_color_theme_inverted: false },
} as unknown as AppData

const defaultHookReturn: HookReturn = {
  isInstalledApp: false,
  appId: 'test-app-id',
  currentConversationId: '',
  currentConversationItem: undefined,
  handleConversationIdInfoChange: vi.fn(),
  appData: mockAppData,
  appParams: {} as ChatConfig,
  appMeta: {} as AppMeta,
  appPinnedConversationData: {
    data: [] as ConversationItem[],
    has_more: false,
    limit: 20,
  } as AppConversationData,
  appConversationData: {
    data: [] as ConversationItem[],
    has_more: false,
    limit: 20,
  } as AppConversationData,
  appConversationDataLoading: false,
  appChatListData: {
    data: [] as ConversationItem[],
    has_more: false,
    limit: 20,
  } as AppConversationData,
  appChatListDataLoading: false,
  appPrevChatTree: [],
  pinnedConversationList: [],
  conversationList: [],
  setShowNewConversationItemInList: vi.fn(),
  newConversationInputs: {},
  newConversationInputsRef: { current: {} } as unknown as RefObject<Record<string, unknown>>,
  handleNewConversationInputsChange: vi.fn(),
  inputsForms: [],
  handleNewConversation: vi.fn(),
  handleStartChat: vi.fn(),
  handleChangeConversation: vi.fn(),
  handlePinConversation: vi.fn(),
  handleUnpinConversation: vi.fn(),
  conversationDeleting: false,
  handleDeleteConversation: vi.fn(),
  conversationRenaming: false,
  handleRenameConversation: vi.fn(),
  handleNewConversationCompleted: vi.fn(),
  newConversationId: '',
  chatShouldReloadKey: 'test-reload-key',
  handleFeedback: vi.fn(),
  currentChatInstanceRef: { current: { handleStop: vi.fn() } },
  sidebarCollapseState: false,
  handleSidebarCollapse: vi.fn(),
  clearChatList: false,
  setClearChatList: vi.fn(),
  isResponding: false,
  setIsResponding: vi.fn(),
  currentConversationInputs: {},
  setCurrentConversationInputs: vi.fn(),
  allInputsHidden: false,
  initUserVariables: {},
}

describe('Base Chat Flow', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    vi.mocked(useMediaQuery).mockReturnValue(false)
    vi.mocked(useChatWithHistory).mockReturnValue(defaultHookReturn)
  })

  // Chat-with-history shell integration across layout, responsive shell, and theme setup.
  describe('Chat With History Shell', () => {
    it('uses the site title for the document', () => {
      render(<ChatWithHistory />)
      expect(document.title).toBe('Test Chat - Dify')
    })

    it('renders a new theme when site configuration changes', () => {
      const { rerender } = render(<ChatWithHistory />)

      expect(screen.getByLabelText('chat theme')).toHaveTextContent('blue:false')

      vi.mocked(useChatWithHistory).mockReturnValue({
        ...defaultHookReturn,
        appData: {
          ...mockAppData,
          site: {
            ...mockAppData.site,
            chat_color_theme: '#654321',
            chat_color_theme_inverted: true,
          },
        },
      })
      rerender(<ChatWithHistory />)

      expect(screen.getByLabelText('chat theme')).toHaveTextContent('#654321:true')
    })

    it('falls back to the mobile loading shell when site metadata is unavailable', () => {
      vi.mocked(useMediaQuery).mockReturnValue(true)
      vi.mocked(useChatWithHistory).mockReturnValue({
        ...defaultHookReturn,
        appData: null,
        appChatListDataLoading: true,
      })

      render(<ChatWithHistory />)

      expect(document.title).toBe('Chat - Dify')
      expect(screen.getByRole('progressbar', { name: 'common.loading' })).toBeInTheDocument()
    })
  })
})