botPluginsPanel.test.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { mount, flushPromises } from '@vue/test-utils'
  2. import { beforeEach, afterEach, expect, it, vi } from 'vitest'
  3. import BotPluginsPanel from '@/components/workspace/BotPluginsPanel.vue'
  4. import { apiRequest } from '@/api/client'
  5. vi.mock('@/api/client', () => ({ apiRequest: vi.fn() }))
  6. let failInventory = false, canManage = true
  7. const plugin = { id: 'plugin-1', name: '现有插件', transport: 'http', url: 'https://mcp.example.test', envKeys: [], headerKeys: ['Authorization'], agentIds: [], enabled: false, revision: 2, toolCount: 1, testedAt: 1 }
  8. beforeEach(() => {
  9. failInventory = false; canManage = true
  10. vi.mocked(apiRequest).mockReset().mockImplementation(async (path, options) => {
  11. if (options?.method && options.method !== 'GET') return {} as never
  12. if (path.endsWith('/settings')) return { configured: true, revision: 1, canManageMcp: canManage } as never
  13. if (path.endsWith('/mcp')) return { plugins: [plugin], canManage } as never
  14. if (path.endsWith('/apps/catalog')) return { cards: [{ slug: 'gmail', name: 'Gmail', description: '邮件' }] } as never
  15. if (path.endsWith('/apps')) {
  16. if (failInventory) throw new Error('暂时无法读取连接状态')
  17. return { authoritative: true, connections: [{ slug: 'gmail', accounts: [{ id: 'account-1', alias: '工作账号', status: 'ACTIVE' }], agentIds: [], revision: 0 }] } as never
  18. }
  19. return { agents: [] } as never
  20. })
  21. })
  22. afterEach(() => { document.body.innerHTML = ''; vi.restoreAllMocks() })
  23. const render = () => mount(BotPluginsPanel, { attachTo: document.body, global: { stubs: { AppIcon: true } } })
  24. function button(wrapper: ReturnType<typeof render>, text: string) { return wrapper.findAll('button').find(b => b.text() === text)! }
  25. it('retains connected accounts on failed refresh instead of presenting them as disconnected', async () => {
  26. const wrapper = render(); await flushPromises()
  27. expect(wrapper.text()).toContain('工作账号')
  28. failInventory = true
  29. await wrapper.get('[aria-label="刷新应用连接"]').trigger('click'); await flushPromises()
  30. expect(wrapper.get('[role="alert"]').text()).toContain('暂时无法读取连接状态')
  31. expect(wrapper.text()).toContain('工作账号')
  32. expect(button(wrapper, '添加账号').exists()).toBe(true)
  33. wrapper.unmount()
  34. })
  35. it('keeps secret placeholders and invalid form input without submitting a broken configuration', async () => {
  36. const wrapper = render(); await flushPromises()
  37. await button(wrapper, 'MCP 服务').trigger('click')
  38. await button(wrapper, '编辑').trigger('click')
  39. const headers = wrapper.get<HTMLTextAreaElement>('.plugin-editor textarea')
  40. expect(JSON.parse(headers.element.value)).toEqual({ Authorization: true })
  41. await headers.setValue('{bad json')
  42. await wrapper.get('form[aria-label="MCP 服务配置"]').trigger('submit'); await flushPromises()
  43. expect(wrapper.get('[role="alert"]').text()).toContain('有效 JSON')
  44. expect(headers.element.value).toBe('{bad json')
  45. expect(vi.mocked(apiRequest).mock.calls.some(([, options]) => options?.method === 'PUT')).toBe(false)
  46. expect(wrapper.emitted('dirty-change')?.at(-1)).toEqual([true])
  47. wrapper.unmount()
  48. })
  49. it('disables host MCP management for non-admin accounts', async () => {
  50. canManage = false
  51. const wrapper = render(); await flushPromises()
  52. await button(wrapper, 'MCP 服务').trigger('click')
  53. expect(button(wrapper, '添加服务').attributes('disabled')).toBeDefined()
  54. expect(button(wrapper, '测试连接').attributes('disabled')).toBeDefined()
  55. wrapper.unmount()
  56. })