// Smoke tests of the MCP consent card (Phase 32.2 UI-SPEC C1): a signed-in // administrator opens /connect?request=…, keeps the ticks and clicks Allow, // and the browser leaves for the server-issued redirect_uri through // window.location.assign, never through the router. Fixtures are neutral. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { enableAutoUnmount, flushPromises } from '@vue/test-utils' import type { MCPConsent } from '../../src/api/types' import { API, mountApp, requestsTo, resetState, type Route } from '../helpers' const REDIRECT = 'https://agent.example.test/callback?code=abc123&state=xyz' function consentView(overrides: Partial = {}): MCPConsent { return { request_id: 'req1', client: { id: 'client-1', name: 'Acme Agent', redirect_host: 'agent.example.test', logo_url: '' }, admin: { login: 'dev', name: 'Dana Dev', email: 'dev@example.test' }, scopes: [ { name: 'admin:read', label: 'Read the admin', description: 'Read every admin list.', read_only: true, known: true }, { name: 'acme', label: 'Acme', description: 'Use the acme tools.', read_only: false, known: true }, ], offline_access: true, consent_required: true, expires_at: '2026-11-21T12:00:00Z', ...overrides, } } function consentRoutes(view: MCPConsent = consentView(), decide?: Route): Record { return { [`GET ${API}/mcp/consent/req1`]: { body: { data: view, meta: {} } }, [`POST ${API}/mcp/consent/req1`]: decide ?? { body: { data: { redirect_uri: REDIRECT }, meta: {} } }, } } let assign: ReturnType beforeEach(() => { resetState() assign = vi.fn() vi.spyOn(window.location, 'assign').mockImplementation(assign) }) afterEach(() => { vi.restoreAllMocks() document.body.innerHTML = '' }) enableAutoUnmount(afterEach) describe('consent card (UI-SPEC C1)', () => { it('Allow posts the ticked scopes and Stay connected, then leaves for redirect_uri without the router', async () => { const { wrapper, calls, router } = await mountApp('/connect?request=req1', consentRoutes()) const push = vi.spyOn(router, 'push') const replace = vi.spyOn(router, 'replace') expect(router.currentRoute.value.name).toBe('connect') const card = wrapper.find('[data-connect-state="ready"]') expect(card.exists()).toBe(true) // Standalone: no navigation shell around the card. expect(wrapper.find('nav[aria-label="backend::lang.nav.plugins"]').exists()).toBe(false) expect(card.find('[data-connect-host]').text()).toBe('agent.example.test') const scopeBoxes = card.findAll('[data-scope] input[type="checkbox"]') expect(scopeBoxes).toHaveLength(2) expect(scopeBoxes.map((box) => (box.element as HTMLInputElement).checked)).toEqual([true, true]) const offline = card.find('[data-connect-offline] input[type="checkbox"]') expect((offline.element as HTMLInputElement).checked).toBe(true) // Nothing is posted before the click. expect(requestsTo(calls, 'POST', `${API}/mcp/consent/req1`)).toHaveLength(0) await card.trigger('submit') await flushPromises() const posts = requestsTo(calls, 'POST', `${API}/mcp/consent/req1`) expect(posts).toHaveLength(1) expect(posts[0]!.headers.get('X-Requested-With')).toBe('XMLHttpRequest') expect(await posts[0]!.json()).toEqual({ decision: 'allow', scopes: ['admin:read', 'acme'], offline_access: true }) expect(assign).toHaveBeenCalledWith(REDIRECT) for (const spy of [push, replace]) { expect(spy).not.toHaveBeenCalledWith(REDIRECT) for (const call of spy.mock.calls) { expect(JSON.stringify(call)).not.toContain('agent.example.test') } } }) })