mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +00:00
feat(chat): support markups, attachments, and timestamps in agent 1-to-1 chat (#368)
* feat(chat): support markups, attachments, and timestamps in agent 1-to-1 chat * feat(chat): improve message card layout with inline badges and integrated assets * fix(chat): enable image attachment thumbnails in 1-to-1 chat cards * fix(chat): address code review feedback on input state, mentions, i18n, and session deletion * fix(chat): replace ring with border color on selected message card
This commit is contained in:
@@ -6,6 +6,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { ChatbotSidebar } from './chatbot-sidebar'
|
||||
import { useChatbotStore, AGENT_PREFERENCE_STORAGE_KEY } from '@/ui/stores/chatbot'
|
||||
import { client } from '@/ui/api/client'
|
||||
import type { ChatMessage } from '@shumai/dtos'
|
||||
|
||||
const localStorageMock = (() => {
|
||||
let store: Record<string, string> = {}
|
||||
@@ -190,4 +191,281 @@ describe('ChatbotSidebar - Agent Selection & Preference Persistence', () => {
|
||||
expect(localStorage.getItem(AGENT_PREFERENCE_STORAGE_KEY)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
it('renders user messages with markup badge, timestamp, and attachments and triggers onSelectMessage on click when matched', async () => {
|
||||
vi.mocked(client.api.projects[':projectId']['chat-agents'].$get).mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => [{ id: 'agent-1', name: 'Agent 1', type: 'chat', enabled: true }],
|
||||
} as unknown as Awaited<
|
||||
ReturnType<(typeof client.api.projects)[':projectId']['chat-agents']['$get']>
|
||||
>)
|
||||
|
||||
const mockSelectMessage = vi.fn()
|
||||
|
||||
const mockMessage = {
|
||||
id: 'msg-1',
|
||||
role: 'custom',
|
||||
customType: 'shumai_message',
|
||||
content: 'Fix the color in this scene',
|
||||
details: {
|
||||
annotation: true,
|
||||
annotations: [{ type: 'rectangle', x: 5, y: 10 }],
|
||||
position: { type: 'time', seconds: 15 },
|
||||
currentAsset: { id: 'file-123', name: 'video.mp4', type: 'file' },
|
||||
attachedFiles: [
|
||||
{ id: 'att-1', name: 'color-ref.png', type: 'image', mediaType: 'image/png' },
|
||||
],
|
||||
},
|
||||
} as unknown as ChatMessage
|
||||
|
||||
useChatbotStore.setState({
|
||||
messages: [mockMessage],
|
||||
})
|
||||
|
||||
const { getByText } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatbotSidebar
|
||||
projectId="proj-1"
|
||||
contextAssetId="file-123"
|
||||
onSelectMessage={mockSelectMessage}
|
||||
/>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
expect(getByText('Fix the color in this scene')).toBeTruthy()
|
||||
expect(getByText('00:15')).toBeTruthy()
|
||||
expect(getByText('color-ref.png')).toBeTruthy()
|
||||
|
||||
const msgCardInner = getByText('Fix the color in this scene').closest('.bg-primary')
|
||||
expect(msgCardInner?.className).not.toContain('ring-2')
|
||||
|
||||
const msgCard = getByText('Fix the color in this scene').closest('div')
|
||||
msgCard?.click()
|
||||
|
||||
expect(mockSelectMessage).toHaveBeenCalledWith(mockMessage)
|
||||
})
|
||||
|
||||
it('renders selected message card with border-blue-500 instead of ring when selectedMessageId matches', async () => {
|
||||
const mockMessage = {
|
||||
id: 'msg-selected',
|
||||
role: 'custom',
|
||||
customType: 'shumai_message',
|
||||
content: 'Selected message content',
|
||||
details: {
|
||||
position: { type: 'time', seconds: 10 },
|
||||
currentAsset: { id: 'file-123', name: 'video.mp4', type: 'file' },
|
||||
},
|
||||
} as unknown as ChatMessage
|
||||
|
||||
useChatbotStore.setState({
|
||||
messages: [mockMessage],
|
||||
})
|
||||
|
||||
const { getByText } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatbotSidebar
|
||||
projectId="proj-1"
|
||||
contextAssetId="file-123"
|
||||
selectedMessageId="msg-selected"
|
||||
/>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
const cardInner = getByText('Selected message content').closest('.bg-primary')
|
||||
expect(cardInner?.className).toContain('border-blue-500')
|
||||
expect(cardInner?.className).not.toContain('ring')
|
||||
})
|
||||
|
||||
it('does not trigger onSelectMessage on click when currentAsset does not match', async () => {
|
||||
vi.mocked(client.api.projects[':projectId']['chat-agents'].$get).mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => [{ id: 'agent-1', name: 'Agent 1', type: 'chat', enabled: true }],
|
||||
} as unknown as Awaited<
|
||||
ReturnType<(typeof client.api.projects)[':projectId']['chat-agents']['$get']>
|
||||
>)
|
||||
|
||||
const mockSelectMessage = vi.fn()
|
||||
|
||||
const mockMessage = {
|
||||
id: 'msg-2',
|
||||
role: 'custom',
|
||||
customType: 'shumai_message',
|
||||
content: 'Other file comment',
|
||||
details: {
|
||||
annotation: true,
|
||||
annotations: [{ type: 'rectangle', x: 5, y: 10 }],
|
||||
position: { type: 'time', seconds: 20 },
|
||||
currentAsset: { id: 'other-file-456', name: 'other.mp4', type: 'file' },
|
||||
},
|
||||
} as unknown as ChatMessage
|
||||
|
||||
useChatbotStore.setState({
|
||||
messages: [mockMessage],
|
||||
})
|
||||
|
||||
const { getByText } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatbotSidebar
|
||||
projectId="proj-1"
|
||||
contextAssetId="file-123"
|
||||
onSelectMessage={mockSelectMessage}
|
||||
/>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
const msgCard = getByText('Other file comment').closest('div')
|
||||
msgCard?.click()
|
||||
|
||||
expect(mockSelectMessage).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('renders full-width message card with inline badges, attachments and assets sections', async () => {
|
||||
vi.mocked(client.api.projects[':projectId']['chat-agents'].$get).mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => [{ id: 'agent-1', name: 'Agent 1', type: 'chat', enabled: true }],
|
||||
} as unknown as Awaited<
|
||||
ReturnType<(typeof client.api.projects)[':projectId']['chat-agents']['$get']>
|
||||
>)
|
||||
|
||||
const mockMessage = {
|
||||
id: 'msg-full',
|
||||
role: 'custom',
|
||||
customType: 'shumai_message',
|
||||
content: 'Review this scene carefully',
|
||||
details: {
|
||||
annotation: true,
|
||||
annotations: [
|
||||
{
|
||||
type: 'box',
|
||||
color: '#ff0000',
|
||||
points: [
|
||||
[0, 0],
|
||||
[10, 10],
|
||||
],
|
||||
},
|
||||
],
|
||||
position: { type: 'time', seconds: 45 },
|
||||
currentAsset: { id: 'file-123', name: 'clip.mp4', type: 'file' },
|
||||
attachedFiles: [
|
||||
{ id: 'att-1', name: 'reference-mood.jpg', type: 'image', mediaType: 'image/jpeg' },
|
||||
],
|
||||
referencedAssets: [
|
||||
{ id: 'asset-ref-1', name: 'Storyboard.pdf', type: 'file' },
|
||||
{ id: 'asset-ref-2', name: 'Audio Stems', type: 'folder' },
|
||||
],
|
||||
},
|
||||
} as unknown as ChatMessage
|
||||
|
||||
useChatbotStore.setState({
|
||||
messages: [mockMessage],
|
||||
})
|
||||
|
||||
const { getByText } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatbotSidebar projectId="proj-1" contextAssetId="file-123" />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
// Inline text and timestamp
|
||||
expect(getByText('Review this scene carefully')).toBeTruthy()
|
||||
expect(getByText('00:45')).toBeTruthy()
|
||||
|
||||
// Attachments section
|
||||
expect(getByText('Attachments')).toBeTruthy()
|
||||
expect(getByText('reference-mood.jpg')).toBeTruthy()
|
||||
|
||||
// Assets section
|
||||
expect(getByText('Assets')).toBeTruthy()
|
||||
expect(getByText('Storyboard.pdf')).toBeTruthy()
|
||||
expect(getByText('Audio Stems')).toBeTruthy()
|
||||
|
||||
// Card should be full width (w-full)
|
||||
const cardEl = getByText('Review this scene carefully').closest('.bg-primary')
|
||||
expect(cardEl?.className).toContain('w-full')
|
||||
})
|
||||
|
||||
it('asks for confirmation before deleting a session and deletes when confirmed', async () => {
|
||||
const mockDeleteSession = vi.fn()
|
||||
const originalDeleteSession = useChatbotStore.getState().deleteSession
|
||||
useChatbotStore.setState({
|
||||
isHistoryMode: true,
|
||||
historySessions: [
|
||||
{
|
||||
id: 'sess-1',
|
||||
agentId: 'agent-1',
|
||||
userId: 'user-1',
|
||||
assetId: 'asset-1',
|
||||
userCommentId: null,
|
||||
name: 'Important Chat',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
],
|
||||
deleteSession: mockDeleteSession,
|
||||
})
|
||||
|
||||
const { useTeamContextStore } = await import('@/ui/stores/team-context')
|
||||
useTeamContextStore.setState({ teamId: 'team-123' })
|
||||
|
||||
window.confirm = vi.fn()
|
||||
const confirmSpy = vi.spyOn(window, 'confirm')
|
||||
|
||||
// 1. User cancels confirmation
|
||||
confirmSpy.mockReturnValue(false)
|
||||
const { getByTitle } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatbotSidebar projectId="proj-1" />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
const deleteBtn = getByTitle('Delete Session')
|
||||
deleteBtn.click()
|
||||
expect(confirmSpy).toHaveBeenCalled()
|
||||
expect(mockDeleteSession).not.toHaveBeenCalled()
|
||||
|
||||
// 2. User confirms deletion
|
||||
confirmSpy.mockReturnValue(true)
|
||||
deleteBtn.click()
|
||||
expect(mockDeleteSession).toHaveBeenCalledWith('team-123', 'sess-1')
|
||||
|
||||
confirmSpy.mockRestore()
|
||||
useChatbotStore.setState({ deleteSession: originalDeleteSession })
|
||||
})
|
||||
|
||||
it('renders thinking_level_change messages properly in the chat log', () => {
|
||||
const mockThinkingChangeMsg = {
|
||||
id: 'think-1',
|
||||
role: 'thinking_level_change',
|
||||
content: 'Thinking level changed to high',
|
||||
timestamp: Date.now(),
|
||||
} as unknown as ChatMessage
|
||||
|
||||
useChatbotStore.setState({
|
||||
messages: [mockThinkingChangeMsg],
|
||||
})
|
||||
|
||||
const { getByText } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatbotSidebar projectId="proj-1" />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
expect(getByText('Thinking level changed to high')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('disables message input when teamId is not available', async () => {
|
||||
const { useTeamContextStore } = await import('@/ui/stores/team-context')
|
||||
useTeamContextStore.setState({ teamId: null, projectTeamMap: {} })
|
||||
useChatbotStore.setState({ selectedAgentId: 'agent-1' })
|
||||
|
||||
const { container } = render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatbotSidebar projectId="proj-1" />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
const inputArea = container.querySelector('[contenteditable="false"]')
|
||||
expect(inputArea).toBeTruthy()
|
||||
expect(inputArea?.getAttribute('contenteditable')).toBe('false')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user