From 98ef5ccfdd061c2b4b567108f93af09e14276a65 Mon Sep 17 00:00:00 2001 From: Nicolas Bonamy Date: Tue, 29 Oct 2024 20:11:41 -0500 Subject: [PATCH] test fixes --- build/build_number.txt | 2 +- src/screens/PromptAnywhere.vue | 7 +- tests/components/message_item.test.ts | 2 +- tests/screens/prompt.test.ts | 123 +++++++++++++++++++++++--- tests/screens/settings.test.ts | 2 +- tests/unit/commander.test.ts | 4 +- 6 files changed, 118 insertions(+), 22 deletions(-) diff --git a/build/build_number.txt b/build/build_number.txt index 1ce6b02..51272ba 100644 --- a/build/build_number.txt +++ b/build/build_number.txt @@ -1 +1 @@ -339 +340 diff --git a/src/screens/PromptAnywhere.vue b/src/screens/PromptAnywhere.vue index 0f964d4..6befd24 100644 --- a/src/screens/PromptAnywhere.vue +++ b/src/screens/PromptAnywhere.vue @@ -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) @@ -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') { @@ -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() } diff --git a/tests/components/message_item.test.ts b/tests/components/message_item.test.ts index af872ee..b50bbad 100644 --- a/tests/components/message_item.test.ts +++ b/tests/components/message_item.test.ts @@ -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') diff --git a/tests/screens/prompt.test.ts b/tests/screens/prompt.test.ts index 5a8158d..5728113 100644 --- a/tests/screens/prompt.test.ts +++ b/tests/screens/prompt.test.ts @@ -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(() => { @@ -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 + } } }) @@ -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"}]') +}) diff --git a/tests/screens/settings.test.ts b/tests/screens/settings.test.ts index 8681816..d27bdc5 100644 --- a/tests/screens/settings.test.ts +++ b/tests/screens/settings.test.ts @@ -81,7 +81,7 @@ window.api = { beforeAll(() => { // init store - store.config = defaults + store.config = JSON.parse(JSON.stringify(defaults)) // wrapper document.body.innerHTML = `` diff --git a/tests/unit/commander.test.ts b/tests/unit/commander.test.ts index 8b470a6..f802fe8 100644 --- a/tests/unit/commander.test.ts +++ b/tests/unit/commander.test.ts @@ -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(() => {