Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Oct 30, 2024
1 parent ea34ece commit 98ef5cc
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build/build_number.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
339
340
7 changes: 1 addition & 6 deletions src/screens/PromptAnywhere.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ let addedToHistory = false
onMounted(() => {
onEvent('send-prompt', onPrompt)
onEvent('prompt-resize', onResize)
onEvent('stop-prompting', onStopGeneration)
document.addEventListener('keyup', onKeyUp)
window.api.on('show', onShow)
Expand Down Expand Up @@ -213,10 +212,6 @@ const setExpertPrompt = (id: string) => {
const prompt = store.experts.find((p) => p.id == id)
emitEvent('set-expert-prompt', prompt.prompt)
}
const onResize = (data: string) => {
const height = parseInt(data) + 18
//window.api.anywhere.resize(height)
}
const onKeyUp = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
Expand Down Expand Up @@ -286,7 +281,7 @@ const onPrompt = async ({ prompt, attachment, docrepo }: { prompt: string, attac
}
// save?
if (store.config.general.autoSavePrompt) {
if (store.config.prompt.autosave) {
saveChat()
}
Expand Down
2 changes: 1 addition & 1 deletion tests/components/message_item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ beforeAll(() => {

// init store
store.loadSettings = vi.fn()
store.config = defaultSettings
store.config = JSON.parse(JSON.stringify(defaultSettings))

// init chat
chat = new Chat('MessageList test')
Expand Down
123 changes: 112 additions & 11 deletions tests/screens/prompt.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@

import { Expert } from 'types'
import { vi, beforeAll, beforeEach, expect, test, afterAll } from 'vitest'
import { vi, beforeAll, beforeEach, expect, test, afterEach } from 'vitest'
import { enableAutoUnmount, mount } from '@vue/test-utils'
import { renderMarkdown } from '../../src/main/markdown'
import { store } from '../../src/services/store'
import Prompt from '../../src/components/Prompt.vue'
import PromptAnywhere from '../../src/screens/PromptAnywhere.vue'
import MessageItem from '../../src/components/MessageItem.vue'
import defaultSettings from '../../defaults/settings.json'
import Message from '../../src/models/message'
import LlmMock from '../mocks/llm'

enableAutoUnmount(afterAll)
import useEventBus from '../../src/composables/event_bus'
const { emitEvent } = useEventBus()

// mock llm
vi.mock('../../src/llms/llm.ts', async () => {
const LlmFactory = vi.fn()
LlmFactory.prototype.initModels = vi.fn()
LlmFactory.prototype.isEngineReady = vi.fn(() => true)
LlmFactory.prototype.getChatEngineModel = () => ({ engine: 'mock', model: 'chat' })
LlmFactory.prototype.igniteEngine = () => new LlmMock(store.config.engines.mock)
return { default: LlmFactory }
})

enableAutoUnmount(afterEach)

beforeAll(() => {

Expand All @@ -21,22 +38,29 @@ beforeAll(() => {
window.api = {
on: vi.fn(),
off: vi.fn(),
clipboard: {
writeText: vi.fn(),
},
config: {
load: vi.fn(() => JSON.parse(JSON.stringify(defaultSettings))),
},
history: {
load: vi.fn(() => []),
},
commands: {
load: vi.fn(() => []),
},
experts: {
load: vi.fn(() => {
return [
{ id: 'uuid1', type: 'system', name: 'actor1', prompt: 'prompt1', state: 'enabled' },
{ id: 'uuid2', type: 'system', name: 'actor2', prompt: 'prompt2', state: 'disabled' },
{ id: 'uuid3', type: 'user', name: 'actor3', prompt: 'prompt3', state: 'enabled' }
] as Expert[]
})
load: vi.fn(() => []),
},
anywhere: {
prompt: vi.fn(),
insert: vi.fn(),
cancel: vi.fn(),
},
markdown: {
render: renderMarkdown
}
}
})

Expand All @@ -49,10 +73,87 @@ test('Renders correctly', () => {
expect(wrapper.exists()).toBe(true)
expect(wrapper.find('.anywhere').exists()).toBe(true)
expect(wrapper.findComponent(Prompt).exists()).toBe(true)
expect(wrapper.find('.response').exists()).toBe(false)
expect(wrapper.findComponent(MessageItem).exists()).toBe(false)
})

test('Initalizes LLM and chat', async () => {
const wrapper = mount(PromptAnywhere)
wrapper.vm.onShow()
await wrapper.vm.$nextTick()
expect(wrapper.vm.llm).toBeDefined()
expect(wrapper.vm.llm.getName()).toBe('mock')
expect(wrapper.vm.chat.messages).toHaveLength(1)
})

// test('Closes on Escape', async () => {
// const wrapper = mount(PromptAnywhere)
// await wrapper.trigger('keyup.Escape')
// await wrapper.trigger('keyup.esc')
// expect(window.api.anywhere.cancel).toHaveBeenCalled()
// })

test('Renders response', async () => {
const wrapper = mount(PromptAnywhere)
wrapper.vm.response = new Message('assistant', 'This is a response')
await wrapper.vm.$nextTick()
expect(wrapper.find('.response').exists()).toBe(true)
expect(wrapper.findComponent(MessageItem).exists()).toBe(true)
})

test('Submits prompt', async () => {
const wrapper = mount(PromptAnywhere)
wrapper.vm.onShow()
await wrapper.vm.$nextTick()
emitEvent('send-prompt', { prompt: 'Hello LLM' })
await vi.waitUntil(async () => !wrapper.vm.chat.lastMessage().transient)
expect(wrapper.findComponent(MessageItem).text()).toBe('[{"role":"system","content":"You are a helpful assistant. You are here to help the user with any questions they have."},{"role":"user","content":"Hello LLM"},{"role":"assistant","content":"Be kind. Don\'t mock me"}]')
})

test('Copies response', async () => {
const wrapper = mount(PromptAnywhere)
wrapper.vm.response = new Message('assistant', 'This is a response')
await wrapper.vm.$nextTick()
wrapper.find('.copy').trigger('click')
expect(window.api.clipboard.writeText).toHaveBeenCalledWith('This is a response')
})

test('Inserts response', async () => {
const wrapper = mount(PromptAnywhere)
wrapper.vm.response = new Message('assistant', 'This is a response')
await wrapper.vm.$nextTick()
wrapper.find('.insert').trigger('click')
expect(window.api.anywhere.insert).toHaveBeenCalledWith('This is a response')
})

test('Closes', async () => {
const wrapper = mount(PromptAnywhere)
wrapper.vm.response = new Message('assistant', 'This is a response')
await wrapper.vm.$nextTick()
wrapper.find('.close').trigger('click')
expect(window.api.anywhere.cancel).toHaveBeenCalledWith()
})

test('Manages conversation', async () => {
const wrapper = mount(PromptAnywhere)
wrapper.vm.onShow()
await wrapper.vm.$nextTick()
emitEvent('send-prompt', { prompt: 'Hello LLM' })
await vi.waitUntil(async () => !wrapper.vm.chat.lastMessage().transient)
expect(wrapper.findComponent(MessageItem).text()).toBe('[{"role":"system","content":"You are a helpful assistant. You are here to help the user with any questions they have."},{"role":"user","content":"Hello LLM"},{"role":"assistant","content":"Be kind. Don\'t mock me"}]')
emitEvent('send-prompt', { prompt: 'Bye LLM' })
await vi.waitUntil(async () => !wrapper.vm.chat.lastMessage().transient)
expect(wrapper.findComponent(MessageItem).text()).toBe('[{"role":"system","content":"You are a helpful assistant. You are here to help the user with any questions they have."},{"role":"user","content":"Hello LLM"},{"role":"assistant","content":"[{\"role\":\"system\",\"content\":\"You are a helpful assistant. You are here to help the user with any questions they have.\"},{\"role\":\"user\",\"content\":\"Hello LLM\"},{\"role\":\"assistant\",\"content\":\"Be kind. Don\'t mock me\"}]\"},{\"role\":\"user\",\"content\":\"Bye LLM\"},{\"role\":\"assistant\",\"content\":"Be kind. Don\'t mock me\"}]')
})

test('Resets chat', async () => {
const wrapper = mount(PromptAnywhere)
wrapper.vm.onShow()
await wrapper.vm.$nextTick()
emitEvent('send-prompt', { prompt: 'Hello LLM' })
await vi.waitUntil(async () => !wrapper.vm.chat.lastMessage().transient)
expect(wrapper.findComponent(MessageItem).text()).toBe('[{"role":"system","content":"You are a helpful assistant. You are here to help the user with any questions they have."},{"role":"user","content":"Hello LLM"},{"role":"assistant","content":"Be kind. Don\'t mock me"}]')
wrapper.find('.clear').trigger('click')
emitEvent('send-prompt', { prompt: 'Bye LLM' })
await vi.waitUntil(async () => !wrapper.vm.chat.lastMessage().transient)
expect(wrapper.findComponent(MessageItem).text()).toBe('[{"role":"system","content":"You are a helpful assistant. You are here to help the user with any questions they have."},{"role":"user","content":"Bye LLM"},{"role":"assistant","content":"Be kind. Don\'t mock me"}]')
})
2 changes: 1 addition & 1 deletion tests/screens/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ window.api = {
beforeAll(() => {

// init store
store.config = defaults
store.config = JSON.parse(JSON.stringify(defaults))

// wrapper
document.body.innerHTML = `<dialog id="settings"></dialog>`
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/commander.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ vi.mock('../../src/automations/automator.ts', async () => {
vi.mock('../../src/llms/llm.ts', async () => {
const LlmFactory = vi.fn()
LlmFactory.prototype.getChatEngineModel = () => ({ engine: 'mock', model: 'chat' })
LlmFactory.prototype.igniteEngine = () => new LlmMock(store.config)
LlmFactory.prototype.igniteEngine = () => new LlmMock(store.config.engines.mock)
return { default: LlmFactory }
})

beforeAll(() => {
store.config = defaults
store.config = JSON.parse(JSON.stringify(defaults))
})

beforeEach(() => {
Expand Down

0 comments on commit 98ef5cc

Please sign in to comment.