modelChoiceDialog.test.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { mount } from '@vue/test-utils'
  2. import { afterEach, describe, expect, it } from 'vitest'
  3. import { nextTick } from 'vue'
  4. import ModelChoiceDialog from '@/components/composer/ModelChoiceDialog.vue'
  5. const options = [
  6. { id: 'gpt-5.6-terra', name: 'gpt-5.6-terra', provider: 'openai', supportsReasoning: true, isDefault: true },
  7. { id: 'gpt-5.5', name: 'GPT 5.5', provider: 'openai' },
  8. { id: 'claude-sonnet', name: 'Claude Sonnet', provider: 'anthropic' },
  9. ]
  10. afterEach(() => { document.body.innerHTML = '' })
  11. describe('ModelChoiceDialog', () => {
  12. it('groups and filters models while preserving the selected state', async () => {
  13. const wrapper = mount(ModelChoiceDialog, { attachTo: document.body, props: { open: true, options, selectedId: 'openai:gpt-5.6-terra' } })
  14. await nextTick()
  15. const dialog = document.querySelector('[role="dialog"][aria-labelledby="model-dialog-title"]')
  16. expect(dialog?.textContent).toContain('OpenAI')
  17. expect(dialog?.textContent).toContain('Anthropic')
  18. expect(dialog?.querySelector('.model-dialog__item--active')?.textContent).toContain('gpt-5.6-terra')
  19. const input = document.querySelector<HTMLInputElement>('input[placeholder="搜索模型名称或 ID"]')!
  20. input.value = 'claude'
  21. input.dispatchEvent(new Event('input'))
  22. await nextTick()
  23. expect(dialog?.textContent).toContain('Claude Sonnet')
  24. expect(dialog?.textContent).not.toContain('gpt-5.5')
  25. document.querySelector<HTMLButtonElement>('.model-dialog__item')!.click()
  26. expect(wrapper.emitted('select')?.[0]).toEqual(['anthropic:claude-sonnet'])
  27. wrapper.unmount()
  28. })
  29. it('closes with Escape when no model switch is running', () => {
  30. const wrapper = mount(ModelChoiceDialog, { attachTo: document.body, props: { open: true, options } })
  31. window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
  32. expect(wrapper.emitted('close')).toHaveLength(1)
  33. wrapper.unmount()
  34. })
  35. })