-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
409 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
packages/js/core/src/components/chat/chat-room/chat-room.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import {AiChat, createAiChat} from '@nlux-dev/core/src'; | ||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; | ||
import {adapterBuilder} from '../../../../utils/adapterBuilder'; | ||
import {AdapterController} from '../../../../utils/adapters'; | ||
import {submit, type} from '../../../../utils/userInteractions'; | ||
import {waitForRenderCycle} from '../../../../utils/wait'; | ||
|
||
describe('When pre-destroy event handler is used with a Vanilla JS Component', () => { | ||
let rootElement: HTMLElement; | ||
let adapterController: AdapterController | undefined = undefined; | ||
let aiChat: AiChat | undefined; | ||
|
||
beforeEach(() => { | ||
rootElement = document.createElement('div'); | ||
document.body.append(rootElement); | ||
}); | ||
|
||
afterEach(() => { | ||
adapterController = undefined; | ||
aiChat?.unmount(); | ||
rootElement?.remove(); | ||
aiChat = undefined; | ||
}); | ||
|
||
it('should be called when the component is about to get destroyed', async () => { | ||
const preDestroyCallback = vi.fn(); | ||
|
||
adapterController = adapterBuilder().withFetchText().create(); | ||
aiChat = createAiChat() | ||
.withAdapter(adapterController.adapter) | ||
.on('preDestroy', preDestroyCallback); | ||
|
||
await waitForRenderCycle(); | ||
expect(preDestroyCallback).not.toHaveBeenCalled(); | ||
|
||
aiChat.mount(rootElement); | ||
await waitForRenderCycle(); | ||
expect(preDestroyCallback).not.toHaveBeenCalled(); | ||
|
||
aiChat.unmount(); | ||
await waitForRenderCycle(); | ||
expect(preDestroyCallback).toHaveBeenCalledOnce(); | ||
|
||
expect(preDestroyCallback).toHaveBeenCalledWith({ | ||
aiChatProps: expect.objectContaining({ | ||
adapter: adapterController.adapter, | ||
}), | ||
conversationHistory: [], | ||
}); | ||
}); | ||
|
||
it('should be called with conversation history and options', async () => { | ||
const preDestroyCallback = vi.fn(); | ||
|
||
adapterController = adapterBuilder().withFetchText().create(); | ||
aiChat = createAiChat() | ||
.withAdapter(adapterController.adapter) | ||
.withClassName('my-class') | ||
.withConversationOptions({ | ||
scrollWhenGenerating: false, | ||
}) | ||
.withInitialConversation([ | ||
{ | ||
role: 'user', | ||
message: 'Hello', | ||
}, | ||
{ | ||
role: 'ai', | ||
message: 'Hi', | ||
}, | ||
]) | ||
.on('preDestroy', preDestroyCallback); | ||
|
||
|
||
aiChat.mount(rootElement); | ||
await waitForRenderCycle(); | ||
|
||
await type('Tell me a joke'); | ||
await waitForRenderCycle(); | ||
|
||
await submit(); | ||
await waitForRenderCycle(); | ||
|
||
adapterController.resolve('Why did the chicken cross the road? To get to the other side.'); | ||
await waitForRenderCycle(); | ||
|
||
aiChat.unmount(); | ||
await waitForRenderCycle(); | ||
|
||
expect(preDestroyCallback).toHaveBeenCalledWith(expect.objectContaining({ | ||
aiChatProps: expect.objectContaining({ | ||
adapter: adapterController.adapter, | ||
className: 'my-class', | ||
conversationOptions: { | ||
scrollWhenGenerating: false, | ||
}, | ||
initialConversation: [ | ||
{ | ||
role: 'user', | ||
message: 'Hello', | ||
}, | ||
{ | ||
role: 'ai', | ||
message: 'Hi', | ||
}, | ||
], | ||
}), | ||
conversationHistory: [ | ||
{ | ||
role: 'user', | ||
message: 'Hello', | ||
}, | ||
{ | ||
role: 'ai', | ||
message: 'Hi', | ||
}, | ||
{ | ||
role: 'user', | ||
message: 'Tell me a joke', | ||
}, | ||
{ | ||
role: 'ai', | ||
message: 'Why did the chicken cross the road? To get to the other side.', | ||
}, | ||
], | ||
})); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import {AiChat, createAiChat} from '@nlux-dev/core/src'; | ||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; | ||
import {adapterBuilder} from '../../../../utils/adapterBuilder'; | ||
import {AdapterController} from '../../../../utils/adapters'; | ||
import {waitForRenderCycle} from '../../../../utils/wait'; | ||
|
||
describe('When ready event handler is used with a Vanilla JS Component', () => { | ||
let rootElement: HTMLElement; | ||
let adapterController: AdapterController | undefined = undefined; | ||
let aiChat: AiChat | undefined; | ||
|
||
beforeEach(() => { | ||
rootElement = document.createElement('div'); | ||
document.body.append(rootElement); | ||
}); | ||
|
||
afterEach(() => { | ||
adapterController = undefined; | ||
aiChat?.unmount(); | ||
rootElement?.remove(); | ||
aiChat = undefined; | ||
}); | ||
|
||
it('should be called when the component is ready', async () => { | ||
const readyCallback = vi.fn(); | ||
|
||
adapterController = adapterBuilder().withFetchText().create(); | ||
aiChat = createAiChat() | ||
.withAdapter(adapterController.adapter) | ||
.on('ready', readyCallback); | ||
|
||
await waitForRenderCycle(); | ||
expect(readyCallback).not.toHaveBeenCalled(); | ||
|
||
aiChat.mount(rootElement); | ||
await waitForRenderCycle(); | ||
expect(readyCallback).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it('should be called with initialisation options', async () => { | ||
const readyCallback = vi.fn(); | ||
|
||
adapterController = adapterBuilder().withFetchText().create(); | ||
aiChat = createAiChat() | ||
.withAdapter(adapterController.adapter) | ||
.on('ready', readyCallback) | ||
.withClassName('test-class'); | ||
|
||
await waitForRenderCycle(); | ||
expect(readyCallback).not.toHaveBeenCalled(); | ||
|
||
aiChat.mount(rootElement); | ||
await waitForRenderCycle(); | ||
|
||
expect(readyCallback).toHaveBeenCalledWith(expect.objectContaining({ | ||
aiChatProps: { | ||
adapter: adapterController.adapter, | ||
className: 'test-class', | ||
}, | ||
})); | ||
}); | ||
}); |
Oops, something went wrong.