|
| 1 | +import { beforeEach, expect, it, vi } from 'vitest' |
| 2 | +import { defaultPolicy } from '../../src/chat/core/types' |
| 3 | + |
| 4 | +const mocks = vi.hoisted(() => ({ |
| 5 | + policy: vi.fn(), |
| 6 | + credentials: vi.fn(), |
| 7 | + accounts: vi.fn(), |
| 8 | + plugins: vi.fn(), |
| 9 | + runtimeAccounts: vi.fn(), |
| 10 | + kody: vi.fn(), |
| 11 | +})) |
| 12 | +vi.mock('../../src/chat/workspace-policy.server', () => ({ |
| 13 | + readWorkspacePolicy: mocks.policy, |
| 14 | +})) |
| 15 | +vi.mock('../../src/chat/server/credentials', () => ({ |
| 16 | + readCredentials: mocks.credentials, |
| 17 | +})) |
| 18 | +vi.mock('../../src/chat/server/mcp-accounts', () => ({ |
| 19 | + McpAccounts: class { |
| 20 | + configuredServers = mocks.accounts |
| 21 | + }, |
| 22 | +})) |
| 23 | +vi.mock('../../src/chat/server/plugin-connections', () => ({ |
| 24 | + pluginMcpConnections: mocks.plugins, |
| 25 | +})) |
| 26 | +vi.mock('../../src/chat/server/mcp-account-runtime', () => ({ |
| 27 | + runtimeMcpAccounts: mocks.runtimeAccounts, |
| 28 | +})) |
| 29 | +vi.mock('../../src/chat/server/kody', () => ({ kodyConnection: mocks.kody })) |
| 30 | +import { connectedMcpServers } from '../../src/chat/server/mcp-connections' |
| 31 | + |
| 32 | +const env = { |
| 33 | + ENCRYPTION_KEY: 'synthetic-fixture', |
| 34 | + KODY_ORIGIN: 'https://example.test', |
| 35 | +} |
| 36 | +const scope = { workspaceId: 'fixture' } |
| 37 | +beforeEach(() => { |
| 38 | + vi.resetAllMocks() |
| 39 | + mocks.policy.mockResolvedValue(defaultPolicy) |
| 40 | + mocks.credentials.mockResolvedValue(null) |
| 41 | + mocks.accounts.mockResolvedValue([]) |
| 42 | + mocks.plugins.mockResolvedValue([]) |
| 43 | + mocks.runtimeAccounts.mockResolvedValue([]) |
| 44 | +}) |
| 45 | + |
| 46 | +it('lists authorized accounts while the independent credential read is pending', async () => { |
| 47 | + let release = () => {} |
| 48 | + const gate = new Promise<null>((resolve) => { |
| 49 | + release = () => resolve(null) |
| 50 | + }) |
| 51 | + mocks.credentials.mockReturnValue(gate) |
| 52 | + const result = connectedMcpServers( |
| 53 | + env, |
| 54 | + 'fixture-user', |
| 55 | + defaultPolicy, |
| 56 | + undefined, |
| 57 | + scope, |
| 58 | + ) |
| 59 | + try { |
| 60 | + await vi.waitFor(() => expect(mocks.accounts).toHaveBeenCalledOnce()) |
| 61 | + release() |
| 62 | + await expect(result).resolves.toEqual([]) |
| 63 | + } finally { |
| 64 | + release() |
| 65 | + await result |
| 66 | + } |
| 67 | +}) |
| 68 | + |
| 69 | +it('does not read credentials or accounts when workspace authorization fails', async () => { |
| 70 | + mocks.policy.mockRejectedValue(new Error('Synthetic revoked membership')) |
| 71 | + await expect( |
| 72 | + connectedMcpServers(env, 'fixture-user', defaultPolicy, undefined, scope), |
| 73 | + ).resolves.toEqual([]) |
| 74 | + expect(mocks.credentials).not.toHaveBeenCalled() |
| 75 | + expect(mocks.accounts).not.toHaveBeenCalled() |
| 76 | +}) |
| 77 | + |
| 78 | +it('does not read Kody credentials when that capability is disabled', async () => { |
| 79 | + await expect( |
| 80 | + connectedMcpServers( |
| 81 | + env, |
| 82 | + 'fixture-user', |
| 83 | + { ...defaultPolicy, allowKody: false }, |
| 84 | + undefined, |
| 85 | + scope, |
| 86 | + ), |
| 87 | + ).resolves.toEqual([]) |
| 88 | + expect(mocks.credentials).not.toHaveBeenCalled() |
| 89 | + expect(mocks.accounts).toHaveBeenCalledOnce() |
| 90 | +}) |
| 91 | + |
| 92 | +it('settles the account read before reporting a credential failure', async () => { |
| 93 | + let release = () => {} |
| 94 | + const gate = new Promise<[]>((resolve) => { |
| 95 | + release = () => resolve([]) |
| 96 | + }) |
| 97 | + mocks.accounts.mockReturnValue(gate) |
| 98 | + mocks.credentials.mockRejectedValue(new Error('Synthetic credential failure')) |
| 99 | + let settled = false |
| 100 | + const result = connectedMcpServers( |
| 101 | + env, |
| 102 | + 'fixture-user', |
| 103 | + defaultPolicy, |
| 104 | + undefined, |
| 105 | + scope, |
| 106 | + ) |
| 107 | + const observed = result.then( |
| 108 | + () => { |
| 109 | + settled = true |
| 110 | + }, |
| 111 | + () => { |
| 112 | + settled = true |
| 113 | + }, |
| 114 | + ) |
| 115 | + try { |
| 116 | + await vi.waitFor(() => expect(mocks.accounts).toHaveBeenCalledOnce()) |
| 117 | + expect(settled).toBe(false) |
| 118 | + release() |
| 119 | + await expect(result).rejects.toThrow('Synthetic credential failure') |
| 120 | + } finally { |
| 121 | + release() |
| 122 | + await observed |
| 123 | + } |
| 124 | +}) |
| 125 | + |
| 126 | +it('resolves a selected Kody connection with fresh runtime authorization', async () => { |
| 127 | + mocks.credentials.mockResolvedValue({ |
| 128 | + kody: { access_token: 'metadata-token' }, |
| 129 | + }) |
| 130 | + mocks.kody.mockResolvedValue({ |
| 131 | + id: 'kody', |
| 132 | + label: 'Kody', |
| 133 | + url: 'https://example.test/mcp', |
| 134 | + accessToken: 'fresh-runtime-token', |
| 135 | + }) |
| 136 | + const result = await connectedMcpServers( |
| 137 | + env, |
| 138 | + 'fixture-user', |
| 139 | + defaultPolicy, |
| 140 | + 'kody', |
| 141 | + scope, |
| 142 | + ) |
| 143 | + expect(result).toHaveLength(1) |
| 144 | + expect(result[0].accessToken).toBe('fresh-runtime-token') |
| 145 | + expect(mocks.kody).toHaveBeenCalledWith(env, 'fixture-user') |
| 146 | + expect(mocks.accounts).not.toHaveBeenCalled() |
| 147 | +}) |
| 148 | + |
| 149 | +it('does not read unrelated Kody credentials when resolving a selected MCP account', async () => { |
| 150 | + await expect( |
| 151 | + connectedMcpServers( |
| 152 | + env, |
| 153 | + 'fixture-user', |
| 154 | + defaultPolicy, |
| 155 | + 'mcp:fixture-account', |
| 156 | + scope, |
| 157 | + ), |
| 158 | + ).resolves.toEqual([]) |
| 159 | + expect(mocks.credentials).not.toHaveBeenCalled() |
| 160 | + expect(mocks.runtimeAccounts).toHaveBeenCalledWith( |
| 161 | + env, |
| 162 | + { workspaceId: 'fixture', userId: 'fixture-user' }, |
| 163 | + { id: 'fixture-account' }, |
| 164 | + ) |
| 165 | +}) |
0 commit comments