Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed May 16, 2024
1 parent 2376ef0 commit eb77b6c
Show file tree
Hide file tree
Showing 11 changed files with 385 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 7 additions & 11 deletions src/screens/Wait.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<template>
<div class="wait">
<Loader />
<BIconXCircleFill class="cancel" @click="onCancel" />
<div class="wait" @mouseenter="hover=true" @mouseleave="hover=false">
<Loader v-show="!hover"/>
<BIconXCircleFill v-show="hover" class="cancel" @click="onCancel" />
Thinking…
</div>
</template>

<script setup>
import { ref } from 'vue'
import Loader from '../components/Loader.vue'
const hover = ref(false)
const onCancel = () => {
window.api.commands.cancel()
window.api.anywhere.cancel()
}
</script>
Expand Down Expand Up @@ -44,15 +48,7 @@ const onCancel = () => {
margin: 4px;
width: 10px;
height: 10px;
display: none;
}
.wait:hover .cancel {
display: inline-block;
}
.wait:hover .loader {
display: none;
}
</style>
2 changes: 2 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,15 @@ declare global {
commands?: {
load?(): Command[]
save?(commands: Command[]): void
cancel?(): void
closePalette?(): void
run?(command: Command): void
getPrompt?(id: string): string
}
anywhere?: {
prompt?(text: string): void
cancel?(): void
resize?(width: number, height: number): void
}
prompts?: {
load?(): Prompt[]
Expand Down
96 changes: 96 additions & 0 deletions tests/screens/commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

import { vi, beforeEach, expect, test } from 'vitest'
import { mount } from '@vue/test-utils'
import Commands from '../../src/screens/Commands.vue'
import defaults from '../../defaults/settings.json'

window.api = {
config: {
load: vi.fn(() => defaults),
},
commands: {
load: vi.fn(() => [
{ id: 1, icon: '1', label: 'Command 1', shortcut: '1', action: 'chat_window', state: 'enabled' },
{ id: 2, icon: '2', label: 'Command 2', shortcut: '2', action: 'paste_below', state: 'enabled' },
{ id: 3, icon: '3', label: 'Command 3', shortcut: '3', action: 'paste_in_place', state: 'enabled' },
{ id: 4, icon: '4', label: 'Command 4', shortcut: '4', action: 'clipboard_copy', state: 'enabled' },
{ id: 5, icon: '5', label: 'Command 5', shortcut: '5', action: 'chat_window', state: 'disabled' },
]),
run: vi.fn(),
cancel: vi.fn(),
closePalette: vi.fn(),
}
}

beforeEach(() => {
vi.clearAllMocks()
})

test('Renders correctly', () => {
const wrapper = mount(Commands)
expect(wrapper.exists()).toBe(true)
expect(wrapper.find('.commands').exists()).toBe(true)
expect(wrapper.findAll('.command')).toHaveLength(4)

for (let i=0; i<4; i++) {
const command = wrapper.findAll('.command').at(i)
expect(command.find('.icon').text()).toBe(`${i+1}`)
expect(command.find('.label').text()).toBe(`Command ${i+1}`)
}
})

// test('Closes on Escape', async () => {
// const wrapper = mount(Commands)
// await wrapper.trigger('keyup', { key: 'Escape' })
// expect(window.api.commands.closePalette).toHaveBeenCalled()
// })

test('Runs command on click', async () => {
const wrapper = mount(Commands, { props: { extra: { textId: 6 }}})
const command = wrapper.findAll('.command').at(0)
await command.trigger('click')
expect(window.api.commands.run).toHaveBeenCalledWith({
command: {
action: 'chat_window',
icon: '1',
id: 1,
shortcut: '1',
label: 'Command 1',
state: 'enabled',
},
textId: 6,
})
})

// test('Runs command on shortcut', async () => {
// const wrapper = mount(Commands, { props: { extra: { textId: 6 }}})
// await wrapper.trigger('keyup', { key: '2' })
// expect(window.api.commands.run).toHaveBeenCalledWith({
// command: {
// action: 'paste_below',
// icon: '2',
// id: 2,
// shortcut: '2',
// label: 'Command 2',
// state: 'enabled',
// },
// textId: 6,
// })
// })

// test('Uses chat on shift', async () => {
// const wrapper = mount(Commands, { props: { extra: { textId: 6 }}})
// await wrapper.trigger('keyup', { key: '3', shiftKey: true})
// expect(window.api.commands.run).toHaveBeenCalledWith({
// command: {
// action: 'chat_window',
// icon: '3',
// id: 3,
// shortcut: '3',
// label: 'Command 3',
// state: 'enabled',
// },
// textId: 6,
// })

// })
90 changes: 90 additions & 0 deletions tests/screens/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

import { vi, beforeEach, expect, test } from 'vitest'
import { mount } from '@vue/test-utils'
import { store } from '../../src/services/store'
import Main from '../../src/screens/Main.vue'
import Sidebar from '../../src/components/Sidebar.vue'
import ChatArea from '../../src/components/ChatArea.vue'
import Settings from '../../src/screens/Settings.vue'
import defaults from '../../defaults/settings.json'
import * as _Assistant from '../../src/services/assistant'

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

window.api = {
config: {
load: vi.fn(() => defaults),
},
commands: {
load: vi.fn(() => []),
},
prompts: {
load: vi.fn(() => []),
},
history: {
load: vi.fn(() => []),
},
on: vi.fn(),
}

vi.mock('../../src/services/assistant', async () => {
const Assistant = vi.fn()
Assistant.prototype.setChat = vi.fn()
Assistant.prototype.initLlm = vi.fn()
Assistant.prototype.hasLlm = vi.fn(() => true)
Assistant.prototype.prompt = vi.fn()
Assistant.prototype.stop = vi.fn()
return { default: Assistant }
})

beforeEach(() => {
vi.clearAllMocks()
})

test('Renders correctly', () => {
const wrapper = mount(Main)
expect(wrapper.exists()).toBe(true)
expect(wrapper.find('.main').exists()).toBe(true)
expect(wrapper.findComponent(Sidebar).exists()).toBe(true)
expect(wrapper.findComponent(ChatArea).exists()).toBe(true)
expect(wrapper.findComponent(Settings).exists()).toBe(true)
})

test('Resets assistant', async () => {
mount(Main)
emitEvent('newChat')
expect(_Assistant.default.prototype.setChat).toHaveBeenCalledWith(null)
})

test('Attach/Detach file', async () => {
mount(Main)
expect(store.pendingAttachment).toBeNull()
emitEvent('attachFile', 'file')
expect(store.pendingAttachment).toBe('file')
emitEvent('detachFile')
expect(store.pendingAttachment).toBeNull()
})

test('Sends prompt', async () => {
mount(Main)
emitEvent('sendPrompt', 'prompt')
expect(_Assistant.default.prototype.initLlm).toHaveBeenCalled()
expect(_Assistant.default.prototype.prompt).toHaveBeenCalledWith('prompt', { attachment: null }, expect.any(Function))
})

test('Sends prompt with attachment', async () => {
mount(Main)
emitEvent('attachFile', 'file')
expect(store.pendingAttachment).toBe('file')
emitEvent('sendPrompt', 'prompt')
expect(_Assistant.default.prototype.initLlm).toHaveBeenCalled()
expect(_Assistant.default.prototype.prompt).toHaveBeenCalledWith('prompt', { attachment: 'file' }, expect.any(Function))
})

test('Stop assistant', async () => {
mount(Main)
emitEvent('stopAssistant')
expect(_Assistant.default.prototype.stop).toHaveBeenCalled()
})

43 changes: 43 additions & 0 deletions tests/screens/prompt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

import { vi, beforeEach, expect, test } from 'vitest'
import { mount } from '@vue/test-utils'
import Prompt from '../../src/components/Prompt.vue'
import PromptAnywhere from '../../src/screens/PromptAnywhere.vue'
import defaults from '../../defaults/settings.json'

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

window.api = {
config: {
load: vi.fn(() => defaults),
},
anywhere: {
prompt: vi.fn(),
cancel: vi.fn(),
resize: vi.fn(),
},
}

beforeEach(() => {
vi.clearAllMocks()
})

test('Renders correctly', () => {
const wrapper = mount(PromptAnywhere)
expect(wrapper.exists()).toBe(true)
expect(wrapper.find('.anywhere').exists()).toBe(true)
expect(wrapper.findComponent(Prompt).exists()).toBe(true)
})

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

test('Prompts on Enter', async () => {
const wrapper = mount(PromptAnywhere)
emitEvent('sendPrompt', 'prompt')
expect(window.api.anywhere.prompt).toHaveBeenCalled()
})
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,21 @@ const checkVisibility = (visible: number) => {
}
}

// window
let runAtLogin = false
window.api = {
platform: 'darwin',
on: vi.fn(),
runAtLogin: {
get: () => runAtLogin,
set: vi.fn((state) => {
runAtLogin = state
})
},
}

beforeAll(() => {

// window
window.api = {
platform: 'darwin',
on: vi.fn(),
runAtLogin: {
get: () => runAtLogin,
set: vi.fn((state) => {
runAtLogin = state
})
},
}

// init store
store.config = defaults

Expand Down
40 changes: 40 additions & 0 deletions tests/screens/wait.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import { vi, expect, test } from 'vitest'
import { mount } from '@vue/test-utils'
import Wait from '../../src/screens/Wait.vue'

window.api = {
commands: {
cancel: vi.fn()
},
anywhere: {
cancel: vi.fn()
}
}

test('Renders correctly', () => {
const wrapper = mount(Wait)
expect(wrapper.exists()).toBe(true)
expect(wrapper.find('.wait').exists()).toBe(true)
expect(wrapper.find('.loader').exists()).toBe(true)
expect(wrapper.find('.cancel').exists()).toBe(true)
expect(wrapper.find('.loader').isVisible()).toBe(true)
expect(wrapper.find('.cancel').isVisible()).toBe(false)
})

test('Toggles cancel Button', async () => {
const wrapper = mount(Wait)
await wrapper.find('.wait').trigger('mouseenter')
expect(wrapper.find('.loader').isVisible()).toBe(false)
expect(wrapper.find('.cancel').isVisible()).toBe(true)
await wrapper.find('.wait').trigger('mouseleave')
expect(wrapper.find('.loader').isVisible()).toBe(true)
expect(wrapper.find('.cancel').isVisible()).toBe(false)
})

test('Cancels command', () => {
const wrapper = mount(Wait)
wrapper.find('.cancel').trigger('click')
expect(window.api.commands.cancel).toHaveBeenCalled()
expect(window.api.anywhere.cancel).toHaveBeenCalled()
})
Loading

0 comments on commit eb77b6c

Please sign in to comment.