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.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
import type { ReactElement, RefObject } from 'react'
import type { ChatConfig } from '../../types'
import type { Theme } from '../theme/theme'
import type { AppData, AppMeta, ConversationItem } from '@/models/share'
import { useMediaQuery } from '@langgenius/dify-ui/use-media-query'
import { screen } from '@testing-library/react'
import { vi } from 'vite-plus/test'
import { renderWithConsoleQuery } from '@/test/console/query-data'
import { useEmbeddedChatbot } from '../hooks'
import EmbeddedChatbot from '../index'
let mockBrandingWorkspaceLogo = ''
const render = (ui: ReactElement) =>
renderWithConsoleQuery(ui, {
systemFeatures: {
branding: { enabled: true, workspace_logo: mockBrandingWorkspaceLogo },
},
})
vi.mock('../hooks', () => ({
useEmbeddedChatbot: vi.fn(),
}))
vi.mock('@langgenius/dify-ui/use-media-query', () => ({
useMediaQuery: vi.fn(),
}))
vi.mock('../chat-wrapper', () => ({
__esModule: true,
default: () => <div>chat area</div>,
}))
vi.mock('../header', () => ({
__esModule: true,
default: ({ theme }: { theme?: Theme }) => (
<div role="banner">
<span>chat header</span>
<span aria-label="chat theme">
{theme?.primaryColor}:{String(theme?.chatColorThemeInverted)}
</span>
</div>
),
}))
type EmbeddedChatbotHookReturn = ReturnType<typeof useEmbeddedChatbot>
const createAppData = (chatColorTheme = 'blue', chatColorThemeInverted = false): AppData => ({
app_id: 'app-1',
can_replace_logo: true,
custom_config: {
remove_webapp_brand: false,
replace_webapp_logo: '',
},
enable_site: true,
end_user_id: 'user-1',
site: {
title: 'Embedded App',
chat_color_theme: chatColorTheme,
chat_color_theme_inverted: chatColorThemeInverted,
},
})
const createHookReturn = (
overrides: Partial<EmbeddedChatbotHookReturn> = {},
): EmbeddedChatbotHookReturn => {
const base: EmbeddedChatbotHookReturn = {
appSourceType: 'webApp' as EmbeddedChatbotHookReturn['appSourceType'],
isInstalledApp: false,
appId: 'app-1',
currentConversationId: '',
currentConversationItem: undefined,
removeConversationIdInfo: vi.fn(),
handleConversationIdInfoChange: vi.fn(),
appData: createAppData(),
appParams: {} as ChatConfig,
appMeta: { tool_icons: {} } as AppMeta,
appPinnedConversationData: { data: [], has_more: false, limit: 20 },
appConversationData: { data: [], has_more: false, limit: 20 },
appConversationDataLoading: false,
appChatListData: { data: [], has_more: false, limit: 20 },
appChatListDataLoading: false,
appPrevChatList: [],
pinnedConversationList: [] as ConversationItem[],
conversationList: [] as ConversationItem[],
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(),
handleNewConversationCompleted: vi.fn(),
newConversationId: '',
chatShouldReloadKey: 'reload-key',
allowResetChat: true,
handleFeedback: vi.fn(),
currentChatInstanceRef: { current: { handleStop: vi.fn() } },
clearChatList: false,
setClearChatList: vi.fn(),
isResponding: false,
setIsResponding: vi.fn(),
currentConversationInputs: {},
setCurrentConversationInputs: vi.fn(),
allInputsHidden: false,
initUserVariables: {},
}
return {
...base,
...overrides,
}
}
describe('EmbeddedChatbot index', () => {
beforeEach(() => {
vi.clearAllMocks()
mockBrandingWorkspaceLogo = ''
vi.mocked(useMediaQuery).mockReturnValue(true)
vi.mocked(useEmbeddedChatbot).mockReturnValue(createHookReturn())
})
describe('Loading and chat content', () => {
it('should show loading state before chat content', () => {
vi.mocked(useEmbeddedChatbot).mockReturnValue(
createHookReturn({ appChatListDataLoading: true }),
)
const { rerender } = render(<EmbeddedChatbot />)
expect(screen.getByRole('progressbar')).toBeInTheDocument()
expect(screen.queryByText('chat area')).not.toBeInTheDocument()
vi.mocked(useEmbeddedChatbot).mockReturnValue(createHookReturn())
rerender(<EmbeddedChatbot />)
expect(screen.queryByRole('progressbar')).not.toBeInTheDocument()
expect(screen.getByText('chat area')).toBeInTheDocument()
})
})
describe('Theme ownership', () => {
it('keeps themes isolated between chat roots', () => {
vi.mocked(useEmbeddedChatbot)
.mockReturnValueOnce(
createHookReturn({
appData: createAppData('#FF0000'),
}),
)
.mockReturnValueOnce(
createHookReturn({
appData: createAppData('#00FF00', true),
}),
)
render(
<>
<EmbeddedChatbot />
<EmbeddedChatbot />
</>,
)
expect(screen.getAllByLabelText('chat theme')).toHaveLength(2)
expect(screen.getAllByLabelText('chat theme')[0]).toHaveTextContent('#FF0000:false')
expect(screen.getAllByLabelText('chat theme')[1]).toHaveTextContent('#00FF00:true')
})
it('renders a new theme when site configuration changes', () => {
vi.mocked(useEmbeddedChatbot).mockReturnValue(
createHookReturn({
appData: createAppData('#123456'),
}),
)
const { rerender } = render(<EmbeddedChatbot />)
expect(screen.getByLabelText('chat theme')).toHaveTextContent('#123456:false')
vi.mocked(useEmbeddedChatbot).mockReturnValue(
createHookReturn({
appData: createAppData('#654321', true),
}),
)
rerender(<EmbeddedChatbot />)
expect(screen.getByLabelText('chat theme')).toHaveTextContent('#654321:true')
})
})
describe('Powered by branding', () => {
it('should show workspace logo on mobile when branding is enabled', () => {
mockBrandingWorkspaceLogo = 'https://example.com/workspace-logo.png'
render(<EmbeddedChatbot />)
expect(screen.getByText('share.chat.poweredBy')).toBeInTheDocument()
expect(screen.getByAltText('logo')).toHaveAttribute(
'src',
'https://example.com/workspace-logo.png',
)
})
it('should show custom logo when workspace branding logo is unavailable', () => {
vi.mocked(useEmbeddedChatbot).mockReturnValue(
createHookReturn({
appData: {
app_id: 'app-1',
can_replace_logo: true,
custom_config: {
remove_webapp_brand: false,
replace_webapp_logo: 'https://example.com/custom-logo.png',
},
enable_site: true,
end_user_id: 'user-1',
site: {
title: 'Embedded App',
chat_color_theme: 'blue',
chat_color_theme_inverted: false,
},
},
}),
)
render(<EmbeddedChatbot />)
expect(screen.getByText('share.chat.poweredBy')).toBeInTheDocument()
expect(screen.getByAltText('logo')).toHaveAttribute(
'src',
'https://example.com/custom-logo.png',
)
})
it('should hide powered by section when branding is removed', () => {
vi.mocked(useEmbeddedChatbot).mockReturnValue(
createHookReturn({
appData: {
app_id: 'app-1',
can_replace_logo: true,
custom_config: {
remove_webapp_brand: true,
replace_webapp_logo: '',
},
enable_site: true,
end_user_id: 'user-1',
site: {
title: 'Embedded App',
chat_color_theme: 'blue',
chat_color_theme_inverted: false,
},
},
}),
)
render(<EmbeddedChatbot />)
expect(screen.queryByText('share.chat.poweredBy')).not.toBeInTheDocument()
})
it('should not show powered by section on desktop', () => {
vi.mocked(useMediaQuery).mockReturnValue(false)
vi.mocked(useEmbeddedChatbot).mockReturnValue(createHookReturn({ appData: null }))
render(<EmbeddedChatbot />)
expect(screen.queryByText('share.chat.poweredBy')).not.toBeInTheDocument()
expect(screen.getByText('chat header')).toBeInTheDocument()
})
})
})