agentManagementDialog.test.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { flushPromises, mount } from '@vue/test-utils'
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { nextTick } from 'vue'
  4. import AgentIdentityDialog from '@/components/app/AgentIdentityDialog.vue'
  5. import AgentIdentityPanel from '@/components/app/AgentIdentityPanel.vue'
  6. import ModelServicesPanel from '@/components/app/ModelServicesPanel.vue'
  7. import { deleteModelService, listModelServices, saveDuplexVoiceSettings, saveLegacyModelService, saveModelService, validateModelService } from '@/api/agentManagement'
  8. vi.mock('@/api/agentManagement', () => ({
  9. listModelServices: vi.fn(async () => ({ endpoints: [], current: {} })),
  10. listLegacyModelServices: vi.fn(async () => [{ id: 'custom:tingly', name: 'tingly', base_url: 'http://tingly.test/v1', model: 'omni', models: ['omni'], discover_models: true, has_api_key: true, can_edit_api_key: true, is_current: true, source: 'legacy' }]),
  11. listModelCatalog: vi.fn(async () => [
  12. { slug: 'opencode-free', name: 'OpenCode Free', models: ['free-a'], isCurrent: false },
  13. { slug: 'custom:tingly', name: 'tingly', models: ['omni'], isCurrent: true },
  14. ]),
  15. saveLegacyModelService: vi.fn(async () => undefined),
  16. saveModelService: vi.fn(async () => ({ endpoints: [], current: {} })),
  17. validateModelService: vi.fn(async () => ({ ok: true, reachable: true, message: '', models: ['model-a', 'model-b'] })),
  18. activateModelService: vi.fn(async () => undefined),
  19. deleteModelService: vi.fn(async () => undefined),
  20. getDuplexVoiceSettings: vi.fn(async () => ({ hasApiKey: true, voices: [{ id: 'voice-a', name: '音色 A' }], currentVoiceId: 'voice-a', updatedAt: 1 })),
  21. saveDuplexVoiceSettings: vi.fn(async input => ({ hasApiKey: true, ...input, updatedAt: 2 })),
  22. }))
  23. const profile = { name: 'default', displayName: '丫头', isDefault: true }
  24. afterEach(() => { document.body.innerHTML = ''; vi.restoreAllMocks(); vi.clearAllMocks() })
  25. describe('Agent management dialog', () => {
  26. it('keeps non-admin users on identity management only', async () => {
  27. const wrapper = mount(AgentIdentityDialog, { attachTo: document.body, props: { open: true, profile, isAdmin: false } })
  28. await nextTick()
  29. expect(document.querySelector('[aria-label="Agent 管理"]')?.textContent).toContain('Bots 头像')
  30. expect(document.querySelector('.management-tabs')).toBeNull()
  31. expect(document.body.textContent).not.toContain('模型服务')
  32. wrapper.unmount()
  33. })
  34. it('shows admin model and global duplex voice panels', async () => {
  35. const wrapper = mount(AgentIdentityDialog, { attachTo: document.body, props: { open: true, profile, isAdmin: true } })
  36. await nextTick()
  37. const tabs = [...document.querySelectorAll<HTMLButtonElement>('.management-tabs button')]
  38. expect(tabs.map(tab => tab.textContent)).toEqual(['身份', '模型服务', '双流语音'])
  39. tabs[1]!.click(); await flushPromises()
  40. expect(document.body.textContent).toContain('9119 Provider 与模型')
  41. expect(document.body.textContent).toContain('OpenCode Free')
  42. expect(document.body.textContent).toContain('custom:tingly')
  43. expect(document.body.textContent).toContain('只读')
  44. document.querySelectorAll<HTMLButtonElement>('.management-tabs button')[2]!.click(); await nextTick(); await nextTick()
  45. expect(document.body.textContent).toContain('整个 yaoyao 安装共享的全局设置')
  46. expect(document.querySelector<HTMLInputElement>('input[type="password"]')?.placeholder).toContain('留空保持不变')
  47. wrapper.unmount()
  48. })
  49. it('discovers and saves a model service without overwriting an unchanged secret', async () => {
  50. const wrapper = mount(AgentIdentityDialog, { attachTo: document.body, props: { open: true, profile, isAdmin: true } })
  51. await nextTick()
  52. document.querySelectorAll<HTMLButtonElement>('.management-tabs button')[1]!.click(); await flushPromises()
  53. ;[...document.querySelectorAll<HTMLButtonElement>('button')].find(button => button.textContent?.includes('新增服务'))!.click()
  54. await nextTick()
  55. const inputs = [...document.querySelectorAll<HTMLInputElement>('.service-editor input:not([type="checkbox"])')]
  56. for (const [input, value] of [[inputs[0], 'Local'], [inputs[1], 'http://127.0.0.1:9000/v1'], [inputs[2], 'model-a']] as const) {
  57. input!.value = value; input!.dispatchEvent(new Event('input'))
  58. }
  59. ;[...document.querySelectorAll<HTMLButtonElement>('.service-editor button')].find(button => button.textContent?.includes('测试连接'))!.click()
  60. await nextTick(); await nextTick()
  61. expect(validateModelService).toHaveBeenCalledOnce()
  62. expect(document.querySelector<HTMLTextAreaElement>('.service-editor textarea')?.value).toContain('model-b')
  63. ;[...document.querySelectorAll<HTMLButtonElement>('.service-editor button')].find(button => button.textContent?.includes('保存模型服务'))!.click()
  64. await nextTick(); await nextTick()
  65. expect(saveModelService).toHaveBeenCalledOnce()
  66. expect(vi.mocked(saveModelService).mock.calls[0]?.[1]).not.toHaveProperty('api_key')
  67. wrapper.unmount()
  68. })
  69. it('edits an existing legacy custom provider and replaces its visible model list', async () => {
  70. const wrapper = mount(AgentIdentityDialog, { attachTo: document.body, props: { open: true, profile, isAdmin: true } })
  71. await nextTick()
  72. document.querySelectorAll<HTMLButtonElement>('.management-tabs button')[1]!.click(); await flushPromises()
  73. const legacyCard = [...document.querySelectorAll<HTMLElement>('.service-card')].find(card => card.textContent?.includes('custom:tingly'))!
  74. legacyCard.querySelector<HTMLButtonElement>('button')!.click(); await nextTick()
  75. const modelInput = document.querySelectorAll<HTMLInputElement>('.service-editor input:not([type="checkbox"])')[2]!
  76. modelInput.value = 'omni-2'; modelInput.dispatchEvent(new Event('input'))
  77. const models = document.querySelector<HTMLTextAreaElement>('.service-editor textarea')!
  78. models.value = 'omni\nomni-2'; models.dispatchEvent(new Event('input'))
  79. document.querySelector<HTMLFormElement>('.service-editor')!.dispatchEvent(new Event('submit'))
  80. await nextTick(); await nextTick()
  81. expect(saveLegacyModelService).toHaveBeenCalledWith('default', 'custom:tingly', expect.objectContaining({ model: 'omni-2', models: ['omni', 'omni-2'] }))
  82. wrapper.unmount()
  83. })
  84. it('confirms model deletion and rejects duplicate duplex voice ids', async () => {
  85. vi.mocked(listModelServices).mockResolvedValueOnce({ endpoints: [{ id: 'local', name: 'Local', base_url: 'http://127.0.0.1:9000/v1', model: 'model-a', models: ['model-a'], discover_models: true, has_api_key: true, is_current: false }], current: { provider: '', model: '', base_url: '' } })
  86. vi.spyOn(window, 'confirm').mockReturnValueOnce(false).mockReturnValueOnce(true)
  87. const wrapper = mount(AgentIdentityDialog, { attachTo: document.body, props: { open: true, profile, isAdmin: true } })
  88. await nextTick()
  89. document.querySelectorAll<HTMLButtonElement>('.management-tabs button')[1]!.click(); await flushPromises()
  90. const deleteButton = [...document.querySelectorAll<HTMLButtonElement>('.service-card button')].find(button => button.textContent === '删除')!
  91. deleteButton.click(); await nextTick()
  92. expect(deleteModelService).not.toHaveBeenCalled()
  93. deleteButton.click(); await nextTick(); await nextTick()
  94. expect(deleteModelService).toHaveBeenCalledWith('default', 'local')
  95. vi.mocked(saveDuplexVoiceSettings).mockClear()
  96. document.querySelectorAll<HTMLButtonElement>('.management-tabs button')[2]!.click(); await nextTick(); await nextTick()
  97. ;[...document.querySelectorAll<HTMLButtonElement>('.voice-list-heading button')][0]!.click(); await nextTick()
  98. const idInputs = [...document.querySelectorAll<HTMLInputElement>('.voice-row label:first-child input')]
  99. idInputs[1]!.value = idInputs[0]!.value; idInputs[1]!.dispatchEvent(new Event('input'))
  100. document.querySelector<HTMLFormElement>('.voice-panel form')!.dispatchEvent(new Event('submit'))
  101. await nextTick()
  102. expect(document.querySelector('[role="alert"]')?.textContent).toContain('音色 ID 重复')
  103. expect(saveDuplexVoiceSettings).not.toHaveBeenCalled()
  104. wrapper.unmount()
  105. })
  106. it('preserves an identity draft across same-profile refreshes and resets only after save acknowledgement', async () => {
  107. const wrapper = mount(AgentIdentityPanel, { props: { profile, resetVersion: 0 } })
  108. const name = wrapper.get<HTMLInputElement>('input[autocomplete="off"]')
  109. await name.setValue('本地未保存名称')
  110. await wrapper.setProps({ profile: { ...profile, agentName: '远端刷新名称' } })
  111. expect(name.element.value).toBe('本地未保存名称')
  112. await wrapper.setProps({ resetVersion: 1 })
  113. expect(name.element.value).toBe('远端刷新名称')
  114. })
  115. it('does not mark an untouched editor dirty or clear a saved key after typing then erasing', async () => {
  116. const wrapper = mount(ModelServicesPanel, { props: { profile: 'default' } })
  117. await flushPromises()
  118. const legacyCard = wrapper.findAll<HTMLElement>('.service-card').find(card => card.text().includes('custom:tingly'))!
  119. await legacyCard.get('button').trigger('click')
  120. expect(wrapper.emitted('dirty-change')?.at(-1)).toEqual([false])
  121. const key = wrapper.get<HTMLInputElement>('.service-editor input[type="password"]')
  122. await key.setValue('temporary-key')
  123. await key.setValue('')
  124. expect(wrapper.emitted('dirty-change')?.at(-1)).toEqual([false])
  125. await wrapper.get<HTMLFormElement>('.service-editor').trigger('submit')
  126. await flushPromises()
  127. expect(saveLegacyModelService).toHaveBeenCalledOnce()
  128. expect(vi.mocked(saveLegacyModelService).mock.calls[0]?.[2]).not.toHaveProperty('api_key')
  129. })
  130. })