agentManagementDialog.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 { deleteModelService, listModelServices, saveDuplexVoiceSettings, saveLegacyModelService, saveModelService, validateModelService } from '@/api/agentManagement'
  6. vi.mock('@/api/agentManagement', () => ({
  7. listModelServices: vi.fn(async () => ({ endpoints: [], current: {} })),
  8. 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' }]),
  9. listModelCatalog: vi.fn(async () => [
  10. { slug: 'opencode-free', name: 'OpenCode Free', models: ['free-a'], isCurrent: false },
  11. { slug: 'custom:tingly', name: 'tingly', models: ['omni'], isCurrent: true },
  12. ]),
  13. saveLegacyModelService: vi.fn(async () => undefined),
  14. saveModelService: vi.fn(async () => ({ endpoints: [], current: {} })),
  15. validateModelService: vi.fn(async () => ({ ok: true, reachable: true, message: '', models: ['model-a', 'model-b'] })),
  16. activateModelService: vi.fn(async () => undefined),
  17. deleteModelService: vi.fn(async () => undefined),
  18. getDuplexVoiceSettings: vi.fn(async () => ({ hasApiKey: true, voices: [{ id: 'voice-a', name: '音色 A' }], currentVoiceId: 'voice-a', updatedAt: 1 })),
  19. saveDuplexVoiceSettings: vi.fn(async input => ({ hasApiKey: true, ...input, updatedAt: 2 })),
  20. }))
  21. const profile = { name: 'default', displayName: '丫头', isDefault: true }
  22. afterEach(() => { document.body.innerHTML = ''; vi.restoreAllMocks(); vi.clearAllMocks() })
  23. describe('Agent management dialog', () => {
  24. it('keeps non-admin users on identity management only', async () => {
  25. const wrapper = mount(AgentIdentityDialog, { attachTo: document.body, props: { open: true, profile, isAdmin: false } })
  26. await nextTick()
  27. expect(document.querySelector('[aria-label="Agent 管理"]')?.textContent).toContain('Bots 头像')
  28. expect(document.querySelector('.management-tabs')).toBeNull()
  29. expect(document.body.textContent).not.toContain('模型服务')
  30. wrapper.unmount()
  31. })
  32. it('shows admin model and global duplex voice panels', async () => {
  33. const wrapper = mount(AgentIdentityDialog, { attachTo: document.body, props: { open: true, profile, isAdmin: true } })
  34. await nextTick()
  35. const tabs = [...document.querySelectorAll<HTMLButtonElement>('.management-tabs button')]
  36. expect(tabs.map(tab => tab.textContent)).toEqual(['身份', '模型服务', '双流语音'])
  37. tabs[1]!.click(); await flushPromises()
  38. expect(document.body.textContent).toContain('9119 Provider 与模型')
  39. expect(document.body.textContent).toContain('OpenCode Free')
  40. expect(document.body.textContent).toContain('custom:tingly')
  41. expect(document.body.textContent).toContain('只读')
  42. document.querySelectorAll<HTMLButtonElement>('.management-tabs button')[2]!.click(); await nextTick(); await nextTick()
  43. expect(document.body.textContent).toContain('整个 yaoyao 安装共享的全局设置')
  44. expect(document.querySelector<HTMLInputElement>('input[type="password"]')?.placeholder).toContain('留空保持不变')
  45. wrapper.unmount()
  46. })
  47. it('discovers and saves a model service without overwriting an unchanged secret', async () => {
  48. const wrapper = mount(AgentIdentityDialog, { attachTo: document.body, props: { open: true, profile, isAdmin: true } })
  49. await nextTick()
  50. document.querySelectorAll<HTMLButtonElement>('.management-tabs button')[1]!.click(); await flushPromises()
  51. ;[...document.querySelectorAll<HTMLButtonElement>('button')].find(button => button.textContent?.includes('新增服务'))!.click()
  52. await nextTick()
  53. const inputs = [...document.querySelectorAll<HTMLInputElement>('.service-editor input:not([type="checkbox"])')]
  54. for (const [input, value] of [[inputs[0], 'Local'], [inputs[1], 'http://127.0.0.1:9000/v1'], [inputs[2], 'model-a']] as const) {
  55. input!.value = value; input!.dispatchEvent(new Event('input'))
  56. }
  57. ;[...document.querySelectorAll<HTMLButtonElement>('.service-editor button')].find(button => button.textContent?.includes('测试连接'))!.click()
  58. await nextTick(); await nextTick()
  59. expect(validateModelService).toHaveBeenCalledOnce()
  60. expect(document.querySelector<HTMLTextAreaElement>('.service-editor textarea')?.value).toContain('model-b')
  61. ;[...document.querySelectorAll<HTMLButtonElement>('.service-editor button')].find(button => button.textContent?.includes('保存模型服务'))!.click()
  62. await nextTick(); await nextTick()
  63. expect(saveModelService).toHaveBeenCalledOnce()
  64. expect(vi.mocked(saveModelService).mock.calls[0]?.[1]).not.toHaveProperty('api_key')
  65. wrapper.unmount()
  66. })
  67. it('edits an existing legacy custom provider and replaces its visible model list', async () => {
  68. const wrapper = mount(AgentIdentityDialog, { attachTo: document.body, props: { open: true, profile, isAdmin: true } })
  69. await nextTick()
  70. document.querySelectorAll<HTMLButtonElement>('.management-tabs button')[1]!.click(); await flushPromises()
  71. const legacyCard = [...document.querySelectorAll<HTMLElement>('.service-card')].find(card => card.textContent?.includes('custom:tingly'))!
  72. legacyCard.querySelector<HTMLButtonElement>('button')!.click(); await nextTick()
  73. const modelInput = document.querySelectorAll<HTMLInputElement>('.service-editor input:not([type="checkbox"])')[2]!
  74. modelInput.value = 'omni-2'; modelInput.dispatchEvent(new Event('input'))
  75. const models = document.querySelector<HTMLTextAreaElement>('.service-editor textarea')!
  76. models.value = 'omni\nomni-2'; models.dispatchEvent(new Event('input'))
  77. document.querySelector<HTMLFormElement>('.service-editor')!.dispatchEvent(new Event('submit'))
  78. await nextTick(); await nextTick()
  79. expect(saveLegacyModelService).toHaveBeenCalledWith('default', 'custom:tingly', expect.objectContaining({ model: 'omni-2', models: ['omni', 'omni-2'] }))
  80. wrapper.unmount()
  81. })
  82. it('confirms model deletion and rejects duplicate duplex voice ids', async () => {
  83. 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: '' } })
  84. vi.spyOn(window, 'confirm').mockReturnValueOnce(false).mockReturnValueOnce(true)
  85. const wrapper = mount(AgentIdentityDialog, { attachTo: document.body, props: { open: true, profile, isAdmin: true } })
  86. await nextTick()
  87. document.querySelectorAll<HTMLButtonElement>('.management-tabs button')[1]!.click(); await flushPromises()
  88. const deleteButton = [...document.querySelectorAll<HTMLButtonElement>('.service-card button')].find(button => button.textContent === '删除')!
  89. deleteButton.click(); await nextTick()
  90. expect(deleteModelService).not.toHaveBeenCalled()
  91. deleteButton.click(); await nextTick(); await nextTick()
  92. expect(deleteModelService).toHaveBeenCalledWith('default', 'local')
  93. vi.mocked(saveDuplexVoiceSettings).mockClear()
  94. document.querySelectorAll<HTMLButtonElement>('.management-tabs button')[2]!.click(); await nextTick(); await nextTick()
  95. ;[...document.querySelectorAll<HTMLButtonElement>('.voice-list-heading button')][0]!.click(); await nextTick()
  96. const idInputs = [...document.querySelectorAll<HTMLInputElement>('.voice-row label:first-child input')]
  97. idInputs[1]!.value = idInputs[0]!.value; idInputs[1]!.dispatchEvent(new Event('input'))
  98. document.querySelector<HTMLFormElement>('.voice-panel form')!.dispatchEvent(new Event('submit'))
  99. await nextTick()
  100. expect(document.querySelector('[role="alert"]')?.textContent).toContain('音色 ID 重复')
  101. expect(saveDuplexVoiceSettings).not.toHaveBeenCalled()
  102. wrapper.unmount()
  103. })
  104. })