Skip to content

Commit

Permalink
added test cases for condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohini-Microsoft committed Oct 10, 2024
1 parent 7db77e8 commit e5a8e87
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { render, screen, fireEvent } from '@testing-library/react'
import { ChatMessageContainer } from './ChatMessageContainer'
import { ChatMessageContainer, parseCitationFromMessage } from './ChatMessageContainer'
import { type ChatMessage } from '../../api/models'
import { Answer } from '../Answer'
jest.mock('remark-supersub', () => () => {})
Expand Down Expand Up @@ -151,4 +151,35 @@ describe('ChatMessageContainer', () => {
fireEvent.click(buttonEle)
expect(mockOnCitationClicked).not.toHaveBeenCalled()
})

it('returns citations when message role is "tool" and content is valid JSON', () => {
const message: ChatMessage = {
role: 'tool',
content: JSON.stringify({ citations: [{ filepath: 'path/to/file', chunk_id: '1' }] }),
id: '1',
date: ''
}
const citations = parseCitationFromMessage(message)
expect(citations).toEqual([{ filepath: 'path/to/file', chunk_id: '1' }])
})
it('returns an empty array when message role is "tool" and content is invalid JSON', () => {
const message: ChatMessage = {
role: 'tool',
content: 'invalid JSON',
id: '1',
date: ''
}
const citations = parseCitationFromMessage(message)
expect(citations).toEqual([])
})
it('returns an empty array when message role is not "tool"', () => {
const message: ChatMessage = {
role: 'user',
content: JSON.stringify({ citations: [{ filepath: 'path/to/file', chunk_id: '1' }] }),
id: '1',
date: ''
}
const citations = parseCitationFromMessage(message)
expect(citations).toEqual([])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ChatMessageContainerProps = {
showLoadingMessage: boolean;
};

const parseCitationFromMessage = (message: ChatMessage) => {
export const parseCitationFromMessage = (message: ChatMessage) => {
if (message?.role && message?.role === "tool") {
try {
const toolMessage = JSON.parse(message.content) as ToolMessageContent;
Expand Down

0 comments on commit e5a8e87

Please sign in to comment.