|
|
@ -13,6 +13,7 @@ import { EventTrackingSrc } from './tracking'; |
|
|
|
import { Role } from './utils'; |
|
|
|
import { Role } from './utils'; |
|
|
|
|
|
|
|
|
|
|
|
const mockedUseOpenAiStreamState = { |
|
|
|
const mockedUseOpenAiStreamState = { |
|
|
|
|
|
|
|
messages: [], |
|
|
|
setMessages: jest.fn(), |
|
|
|
setMessages: jest.fn(), |
|
|
|
reply: 'I am a robot', |
|
|
|
reply: 'I am a robot', |
|
|
|
streamStatus: StreamStatus.IDLE, |
|
|
|
streamStatus: StreamStatus.IDLE, |
|
|
@ -43,6 +44,7 @@ describe('GenAIButton', () => { |
|
|
|
describe('when LLM plugin is not configured', () => { |
|
|
|
describe('when LLM plugin is not configured', () => { |
|
|
|
beforeAll(() => { |
|
|
|
beforeAll(() => { |
|
|
|
jest.mocked(useOpenAIStream).mockReturnValue({ |
|
|
|
jest.mocked(useOpenAIStream).mockReturnValue({ |
|
|
|
|
|
|
|
messages: [], |
|
|
|
error: undefined, |
|
|
|
error: undefined, |
|
|
|
streamStatus: StreamStatus.IDLE, |
|
|
|
streamStatus: StreamStatus.IDLE, |
|
|
|
reply: 'Some completed genereated text', |
|
|
|
reply: 'Some completed genereated text', |
|
|
@ -66,7 +68,11 @@ describe('GenAIButton', () => { |
|
|
|
const setMessagesMock = jest.fn(); |
|
|
|
const setMessagesMock = jest.fn(); |
|
|
|
const setShouldStopMock = jest.fn(); |
|
|
|
const setShouldStopMock = jest.fn(); |
|
|
|
beforeEach(() => { |
|
|
|
beforeEach(() => { |
|
|
|
|
|
|
|
setMessagesMock.mockClear(); |
|
|
|
|
|
|
|
setShouldStopMock.mockClear(); |
|
|
|
|
|
|
|
|
|
|
|
jest.mocked(useOpenAIStream).mockReturnValue({ |
|
|
|
jest.mocked(useOpenAIStream).mockReturnValue({ |
|
|
|
|
|
|
|
messages: [], |
|
|
|
error: undefined, |
|
|
|
error: undefined, |
|
|
|
streamStatus: StreamStatus.IDLE, |
|
|
|
streamStatus: StreamStatus.IDLE, |
|
|
|
reply: 'Some completed genereated text', |
|
|
|
reply: 'Some completed genereated text', |
|
|
@ -103,6 +109,20 @@ describe('GenAIButton', () => { |
|
|
|
expect(setMessagesMock).toHaveBeenCalledWith([{ content: 'Generate X', role: 'system' as Role }]); |
|
|
|
expect(setMessagesMock).toHaveBeenCalledWith([{ content: 'Generate X', role: 'system' as Role }]); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('should call the messages when they are provided as callback', async () => { |
|
|
|
|
|
|
|
const onGenerate = jest.fn(); |
|
|
|
|
|
|
|
const messages = jest.fn().mockReturnValue([{ content: 'Generate X', role: 'system' as Role }]); |
|
|
|
|
|
|
|
const onClick = jest.fn(); |
|
|
|
|
|
|
|
setup({ onGenerate, messages, temperature: 3, onClick, eventTrackingSrc }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const generateButton = await screen.findByRole('button'); |
|
|
|
|
|
|
|
await fireEvent.click(generateButton); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expect(messages).toHaveBeenCalledTimes(1); |
|
|
|
|
|
|
|
expect(setMessagesMock).toHaveBeenCalledTimes(1); |
|
|
|
|
|
|
|
expect(setMessagesMock).toHaveBeenCalledWith([{ content: 'Generate X', role: 'system' as Role }]); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should call the onClick callback', async () => { |
|
|
|
it('should call the onClick callback', async () => { |
|
|
|
const onGenerate = jest.fn(); |
|
|
|
const onGenerate = jest.fn(); |
|
|
|
const onClick = jest.fn(); |
|
|
|
const onClick = jest.fn(); |
|
|
@ -121,6 +141,7 @@ describe('GenAIButton', () => { |
|
|
|
|
|
|
|
|
|
|
|
beforeEach(() => { |
|
|
|
beforeEach(() => { |
|
|
|
jest.mocked(useOpenAIStream).mockReturnValue({ |
|
|
|
jest.mocked(useOpenAIStream).mockReturnValue({ |
|
|
|
|
|
|
|
messages: [], |
|
|
|
error: undefined, |
|
|
|
error: undefined, |
|
|
|
streamStatus: StreamStatus.GENERATING, |
|
|
|
streamStatus: StreamStatus.GENERATING, |
|
|
|
reply: 'Some incomplete generated text', |
|
|
|
reply: 'Some incomplete generated text', |
|
|
@ -177,7 +198,11 @@ describe('GenAIButton', () => { |
|
|
|
const setMessagesMock = jest.fn(); |
|
|
|
const setMessagesMock = jest.fn(); |
|
|
|
const setShouldStopMock = jest.fn(); |
|
|
|
const setShouldStopMock = jest.fn(); |
|
|
|
beforeEach(() => { |
|
|
|
beforeEach(() => { |
|
|
|
|
|
|
|
setMessagesMock.mockClear(); |
|
|
|
|
|
|
|
setShouldStopMock.mockClear(); |
|
|
|
|
|
|
|
|
|
|
|
jest.mocked(useOpenAIStream).mockReturnValue({ |
|
|
|
jest.mocked(useOpenAIStream).mockReturnValue({ |
|
|
|
|
|
|
|
messages: [], |
|
|
|
error: new Error('Something went wrong'), |
|
|
|
error: new Error('Something went wrong'), |
|
|
|
streamStatus: StreamStatus.IDLE, |
|
|
|
streamStatus: StreamStatus.IDLE, |
|
|
|
reply: '', |
|
|
|
reply: '', |
|
|
@ -242,5 +267,19 @@ describe('GenAIButton', () => { |
|
|
|
|
|
|
|
|
|
|
|
await waitFor(() => expect(onClick).toHaveBeenCalledTimes(1)); |
|
|
|
await waitFor(() => expect(onClick).toHaveBeenCalledTimes(1)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('should call the messages when they are provided as callback', async () => { |
|
|
|
|
|
|
|
const onGenerate = jest.fn(); |
|
|
|
|
|
|
|
const messages = jest.fn().mockReturnValue([{ content: 'Generate X', role: 'system' as Role }]); |
|
|
|
|
|
|
|
const onClick = jest.fn(); |
|
|
|
|
|
|
|
setup({ onGenerate, messages, temperature: 3, onClick, eventTrackingSrc }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const generateButton = await screen.findByRole('button'); |
|
|
|
|
|
|
|
await fireEvent.click(generateButton); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expect(messages).toHaveBeenCalledTimes(1); |
|
|
|
|
|
|
|
expect(setMessagesMock).toHaveBeenCalledTimes(1); |
|
|
|
|
|
|
|
expect(setMessagesMock).toHaveBeenCalledWith([{ content: 'Generate X', role: 'system' as Role }]); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|