Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Nov 5, 2024
1 parent b340420 commit 3bd5cbc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 deletions.
10 changes: 8 additions & 2 deletions src/screens/PromptAnywhere.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ onMounted(() => {
})
onUnmounted(() => {
document.removeEventListener('keydown', onKeyDown)
document.removeEventListener('keyup', onKeyUp)
audioPlayer.removeListener(onAudioPlayerStatus)
window.api.off('show', onShow)
})
Expand Down Expand Up @@ -232,7 +234,9 @@ const onClear = () => {
onStopGeneration()
// keep the first message (instuctions)
chat.value.messages = chat.value.messages.slice(0, 1)
chat.value.messages = [
new Message('system', generator.getSystemInstructions())
]
// reset response
response.value = null
Expand Down Expand Up @@ -388,7 +392,9 @@ const onContinueConversation = async () => {
// make sure it is saved
await saveChat()
console.log('continue')
// continue
window.api.anywhere.continue(chat.value.uuid)
cleanUp()
Expand Down
8 changes: 6 additions & 2 deletions src/services/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ export interface GenerationOpts extends LlmCompletionOpts {
sources?: boolean
}

export default class {
export default class Generator {

config: Configuration
stopGeneration: boolean
stream: AsyncIterable<LlmChunk>
llm: LlmEngine

static addDateAndTimeToSystemInstr = true

constructor(config: Configuration) {
this.config = config
this.stream = null
Expand Down Expand Up @@ -151,7 +153,9 @@ export default class {
//else instr += ' Always reply in the user language unless expicitely asked to do otherwise.'

// add date and time
instr += ' Current date and time is ' + new Date().toLocaleString() + '.'
if (Generator.addDateAndTimeToSystemInstr) {
instr += ' Current date and time is ' + new Date().toLocaleString() + '.'
}

// done
return instr
Expand Down
70 changes: 30 additions & 40 deletions tests/screens/prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import { vi, beforeAll, beforeEach, expect, test, afterEach } from 'vitest'
import { enableAutoUnmount, mount } from '@vue/test-utils'
import { useWindowMock, useNavigatorMock } from '../mocks/window'
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 Generator from '../../src/services/generator'
import Message from '../../src/models/message'
import LlmMock from '../mocks/llm'

Expand All @@ -27,6 +26,7 @@ vi.mock('../../src/llms/llm.ts', async () => {
enableAutoUnmount(afterEach)

beforeAll(() => {
Generator.addDateAndTimeToSystemInstr = false
useNavigatorMock()
useWindowMock()
})
Expand All @@ -35,6 +35,15 @@ beforeEach(() => {
vi.clearAllMocks()
})

const 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)
return wrapper
}

test('Renders correctly', () => {
const wrapper = mount(PromptAnywhere)
expect(wrapper.exists()).toBe(true)
Expand Down Expand Up @@ -72,11 +81,7 @@ test('Renders response', async () => {
})

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)
const wrapper = await prompt()
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"}]')
})

Expand Down Expand Up @@ -105,23 +110,15 @@ test('Closes when click on icon', async () => {
})

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)
const wrapper = await prompt()
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)
const wrapper = await prompt()
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' })
Expand All @@ -142,12 +139,7 @@ test('Brings back chat', async () => {
})

test('Saves chat', async () => {
const wrapper = mount(PromptAnywhere)
wrapper.vm.onShow()
await wrapper.vm.$nextTick()
//const chatId = wrapper.vm.chat.uuid
emitEvent('send-prompt', { prompt: 'Hello LLM' })
await vi.waitUntil(async () => !wrapper.vm.chat.lastMessage().transient)
const wrapper = await prompt()
expect(wrapper.vm.chat.title).toBeNull()
wrapper.find('.continue').trigger('click')
await wrapper.vm.$nextTick()
Expand All @@ -169,38 +161,36 @@ test('Auto saves chat', async () => {
expect(window.api.history?.save).toHaveBeenCalled()
})

test('Supports keyboard shortcuts', 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)

// copy
vi.clearAllMocks()
test('Supports keyboard copy', async () => {
await prompt()
document.dispatchEvent(new KeyboardEvent('keydown', { metaKey: true, key: 'c' }));
expect(window.api.clipboard?.writeText).toHaveBeenCalled()
})

// insert
vi.clearAllMocks()
test('Supports keyboard insert', async () => {
await prompt()
document.dispatchEvent(new KeyboardEvent('keydown', { metaKey: true, key: 'i' }));
expect(window.api.anywhere?.insert).toHaveBeenCalled()
})

// continue
vi.clearAllMocks()
test('Supports keyboard save', async () => {
const wrapper = await prompt()
document.dispatchEvent(new KeyboardEvent('keydown', { metaKey: true, key: 's' }));
await wrapper.vm.$nextTick()
expect(window.api.history?.save).toHaveBeenCalled()
await wrapper.vm.$nextTick()
expect(window.api.anywhere?.continue).toHaveBeenCalled()
})

// clear
vi.clearAllMocks()
test('Supports keyboard clear', async () => {
const wrapper = await prompt()
expect(wrapper.vm.response).not.toBeNull()
document.dispatchEvent(new KeyboardEvent('keydown', { metaKey: true, key: 'x' }));
expect(wrapper.vm.response).toBeNull()
})

// quit
vi.clearAllMocks()
test('Supports keyboard close', async () => {
await prompt()
document.dispatchEvent(new KeyboardEvent('keyup', { key: 'Escape' }));
expect(window.api.anywhere.close).toHaveBeenCalled()
})
2 changes: 2 additions & 0 deletions tests/unit/assistant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useWindowMock } from '../mocks/window'
import { store } from '../../src/services/store'
import defaults from '../../defaults/settings.json'
import Assistant, { AssistantCompletionOpts } from '../../src/services/assistant'
import Generator from '../../src/services/generator'
import Attachment from '../../src/models/attachment'
import Message from '../../src/models/message'
import Chat from '../../src/models/chat'
Expand All @@ -25,6 +26,7 @@ vi.mock('../../src/services/download.ts', async () => {
})

beforeAll(() => {
Generator.addDateAndTimeToSystemInstr = false
useWindowMock()
})

Expand Down

0 comments on commit 3bd5cbc

Please sign in to comment.