|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { mount, flushPromises } from '@vue/test-utils' |
| 3 | +import { createPinia, setActivePinia } from 'pinia' |
| 4 | +import { createRouter, createWebHistory } from 'vue-router' |
| 5 | +import ServerCard from '@/components/ServerCard.vue' |
| 6 | +import ServerDetail from '@/views/ServerDetail.vue' |
| 7 | +import { useServersStore } from '@/stores/servers' |
| 8 | +import { canStartOAuthLogin } from '@/utils/oauthActions' |
| 9 | +import type { Server } from '@/types' |
| 10 | + |
| 11 | +let server: Server |
| 12 | +vi.mock('@/services/api', () => { |
| 13 | + const ok = (data: unknown = {}) => Promise.resolve({ success: true, data }) |
| 14 | + return { default: { |
| 15 | + getServers: vi.fn(() => ok({ servers: [server] })), |
| 16 | + getToolApprovals: vi.fn(() => ok({ tools: [], count: 0 })), |
| 17 | + getToolDiff: vi.fn(() => ok({})), |
| 18 | + getServerTools: vi.fn(() => ok({ tools: [] })), |
| 19 | + getSecurityOverview: vi.fn(() => ok({ scanners_enabled: 0, docker_available: true })), |
| 20 | + listScanners: vi.fn(() => ok([])), |
| 21 | + getScanReport: vi.fn(() => Promise.resolve({ success: false, error: 'no report' })), |
| 22 | + getScanStatus: vi.fn(() => ok({ status: 'completed' })), |
| 23 | + getServerLogs: vi.fn(() => ok({ logs: [] })), |
| 24 | + } } |
| 25 | +}) |
| 26 | + |
| 27 | +beforeEach(() => { |
| 28 | + setActivePinia(createPinia()) |
| 29 | + server = { |
| 30 | + name: 'notion', protocol: 'http', url: 'https://mcp.notion.com/mcp', |
| 31 | + enabled: true, connected: false, connecting: true, authenticated: true, |
| 32 | + tool_count: 0, oauth: { client_id: 'test' }, |
| 33 | + last_error: 'OAuth authorization during MCP init failed: context deadline exceeded', |
| 34 | + health: { level: 'unhealthy', admin_state: 'enabled', summary: 'OAuth error', action: 'restart', actions: ['restart', 'view_logs'] }, |
| 35 | + } as Server |
| 36 | +}) |
| 37 | + |
| 38 | +describe('manual OAuth sign-in', () => { |
| 39 | + it('stays available while reconnecting and after a timeout or logout', () => { |
| 40 | + expect(canStartOAuthLogin(server)).toBe(true) |
| 41 | + expect(canStartOAuthLogin({ ...server, authenticated: false })).toBe(true) |
| 42 | + expect(canStartOAuthLogin({ ...server, connected: true, connecting: false, health: undefined, last_error: undefined })).toBe(true) |
| 43 | + }) |
| 44 | + it('does not offer OAuth on disabled, stdio, or unauthenticated non-OAuth HTTP servers', () => { |
| 45 | + expect(canStartOAuthLogin({ ...server, enabled: false })).toBe(false) |
| 46 | + expect(canStartOAuthLogin({ ...server, protocol: 'stdio' })).toBe(false) |
| 47 | + expect(canStartOAuthLogin({ ...server, oauth: undefined, authenticated: false, last_error: 'network timeout', health: undefined })).toBe(false) |
| 48 | + }) |
| 49 | + it('recognizes auto-discovered OAuth without explicit config', () => { |
| 50 | + expect(canStartOAuthLogin({ ...server, oauth: undefined, authenticated: false })).toBe(true) |
| 51 | + }) |
| 52 | + it('starts a fresh OAuth flow from the card even when reconnect is the primary action', async () => { |
| 53 | + const spy = vi.spyOn(useServersStore(), 'triggerOAuthLogin').mockResolvedValue(true) |
| 54 | + const card = mount(ServerCard, { props: { server }, global: { stubs: { RouterLink: true } } }) |
| 55 | + const action = card.find('[data-test="server-card-menu-login"]') |
| 56 | + expect(action.text()).toBe('Sign in again') |
| 57 | + expect(action.attributes('disabled')).toBeUndefined() |
| 58 | + await action.trigger('click'); await flushPromises() |
| 59 | + expect(spy).toHaveBeenCalledWith('notion') |
| 60 | + await card.setProps({ server: { ...server, authenticated: false } }) |
| 61 | + expect(card.find('[data-test="server-card-menu-login"]').text()).toBe('Sign in') |
| 62 | + }) |
| 63 | + it('offers sign-in again from the detail Actions menu during an OAuth reconnect error', async () => { |
| 64 | + const router = createRouter({ history: createWebHistory(), routes: [{ path: '/servers/:serverName', component: { template: '<div/>' } }] }) |
| 65 | + await router.push('/servers/notion'); await router.isReady() |
| 66 | + const pinia = createPinia(); setActivePinia(pinia) |
| 67 | + const spy = vi.spyOn(useServersStore(), 'triggerOAuthLogin').mockResolvedValue(true) |
| 68 | + const detail = mount(ServerDetail, { props: { serverName: 'notion' }, global: { plugins: [pinia, router] } }) |
| 69 | + await flushPromises(); await flushPromises() |
| 70 | + const action = detail.find('[data-test="server-detail-menu-login"]') |
| 71 | + expect(action.text()).toBe('Sign in again') |
| 72 | + expect(action.attributes('disabled')).toBeUndefined() |
| 73 | + await action.trigger('click'); await flushPromises() |
| 74 | + expect(spy).toHaveBeenCalledWith('notion') |
| 75 | + }) |
| 76 | +}) |
0 commit comments