botPluginsPanel.test.ts 5.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, failAgents = false, canManage = true
  7. let agentIds: string[] = []
  8. 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 }
  9. beforeEach(() => {
  10. failInventory = false; failAgents = false; canManage = true; agentIds = []
  11. vi.mocked(apiRequest).mockReset().mockImplementation(async (path, options) => {
  12. if (options?.method && options.method !== 'GET') return {} as never
  13. if (path.endsWith('/settings')) return { configured: true, revision: 1, canManageMcp: canManage } as never
  14. if (path.endsWith('/mcp')) return { plugins: [{ ...plugin, agentIds }], canManage } as never
  15. if (path.endsWith('/apps/catalog')) return { cards: [{ slug: 'gmail', name: 'Gmail', description: '邮件' }] } as never
  16. if (path.endsWith('/apps')) {
  17. if (failInventory) throw new Error('暂时无法读取连接状态')
  18. return { authoritative: true, connections: [{ slug: 'gmail', accounts: [{ id: 'account-1', alias: '工作账号', status: 'ACTIVE' }], agentIds: [], revision: 0 }] } as never
  19. }
  20. if (failAgents) throw new Error('暂时无法读取 Bot 列表')
  21. return { agents: [{ id: 'current-bot', name: '小夭', archived: false }] } as never
  22. })
  23. })
  24. afterEach(() => { document.body.innerHTML = ''; vi.restoreAllMocks() })
  25. const render = () => mount(BotPluginsPanel, { attachTo: document.body, global: { stubs: { AppIcon: true } } })
  26. function button(wrapper: ReturnType<typeof render>, text: string) { return wrapper.findAll('button').find(b => b.text() === text)! }
  27. it('retains connected accounts on failed refresh instead of presenting them as disconnected', async () => {
  28. const wrapper = render(); await flushPromises()
  29. expect(wrapper.text()).toContain('工作账号')
  30. failInventory = true
  31. await wrapper.get('[aria-label="刷新应用连接"]').trigger('click'); await flushPromises()
  32. expect(wrapper.get('[role="alert"]').text()).toContain('暂时无法读取连接状态')
  33. expect(wrapper.text()).toContain('工作账号')
  34. expect(button(wrapper, '添加账号').exists()).toBe(true)
  35. wrapper.unmount()
  36. })
  37. it('keeps secret placeholders and invalid form input without submitting a broken configuration', async () => {
  38. const wrapper = render(); await flushPromises()
  39. await button(wrapper, 'MCP 服务').trigger('click')
  40. await button(wrapper, '编辑').trigger('click')
  41. const headers = wrapper.get<HTMLTextAreaElement>('.plugin-editor textarea')
  42. expect(JSON.parse(headers.element.value)).toEqual({ Authorization: true })
  43. await headers.setValue('{bad json')
  44. await wrapper.get('form[aria-label="MCP 服务配置"]').trigger('submit'); await flushPromises()
  45. expect(wrapper.get('[role="alert"]').text()).toContain('有效 JSON')
  46. expect(headers.element.value).toBe('{bad json')
  47. expect(vi.mocked(apiRequest).mock.calls.some(([, options]) => options?.method === 'PUT')).toBe(false)
  48. expect(wrapper.emitted('dirty-change')?.at(-1)).toEqual([true])
  49. wrapper.unmount()
  50. })
  51. it('disables host MCP management for non-admin accounts', async () => {
  52. canManage = false
  53. const wrapper = render(); await flushPromises()
  54. await button(wrapper, 'MCP 服务').trigger('click')
  55. expect(button(wrapper, '添加服务').attributes('disabled')).toBeDefined()
  56. expect(button(wrapper, '测试连接').attributes('disabled')).toBeDefined()
  57. expect(button(wrapper, '授权 Bot').attributes('disabled')).toBeDefined()
  58. wrapper.unmount()
  59. })
  60. it('rebinds stale MCP grants without resaving credentials or resetting the connection test', async () => {
  61. agentIds = ['deleted-bot']
  62. const wrapper = render(); await flushPromises()
  63. await button(wrapper, 'MCP 服务').trigger('click')
  64. expect(wrapper.text()).toContain('服务器')
  65. expect(wrapper.text()).toContain('授权已失效')
  66. await button(wrapper, '授权 Bot').trigger('click')
  67. const form = wrapper.get('form[aria-label="MCP Bot 授权"]')
  68. await form.get('input[type="checkbox"]').setValue(true)
  69. await form.trigger('submit'); await flushPromises()
  70. expect(vi.mocked(apiRequest)).toHaveBeenCalledWith('/api/app/bot-tools/mcp/plugin-1', {
  71. method: 'PATCH', body: { agentIds: ['current-bot'], revision: 2 },
  72. })
  73. expect(vi.mocked(apiRequest).mock.calls.some(([, options]) => options?.method === 'PUT')).toBe(false)
  74. expect(wrapper.emitted('dirty-change')?.at(-1)).toEqual([false])
  75. wrapper.unmount()
  76. })
  77. it('drops missing Bot ids when editing an old service while keeping its HTTP URL', async () => {
  78. agentIds = ['deleted-bot', 'current-bot']
  79. const wrapper = render(); await flushPromises()
  80. await button(wrapper, 'MCP 服务').trigger('click')
  81. await button(wrapper, '编辑').trigger('click')
  82. await wrapper.get('form[aria-label="MCP 服务配置"]').trigger('submit'); await flushPromises()
  83. const call = vi.mocked(apiRequest).mock.calls.find(([, options]) => options?.method === 'PUT')!
  84. expect(call[1]?.body).toMatchObject({ definition: { url: plugin.url, transport: 'http', agentIds: ['current-bot'], headers: { Authorization: true } } })
  85. wrapper.unmount()
  86. })
  87. it('does not treat a failed Bot inventory as deleted grants', async () => {
  88. agentIds = ['current-bot']; failAgents = true
  89. const wrapper = render(); await flushPromises()
  90. await button(wrapper, 'MCP 服务').trigger('click')
  91. expect(wrapper.text()).not.toContain('授权已失效')
  92. expect(button(wrapper, '授权 Bot').attributes('disabled')).toBeDefined()
  93. expect(button(wrapper, '编辑').attributes('disabled')).toBeDefined()
  94. wrapper.unmount()
  95. })