|
| 1 | +import { describe, test, expect, beforeEach, afterEach, mock } from 'bun:test'; |
| 2 | +import { GithubTokenRefreshService } from './github-token-refresh.service'; |
| 3 | + |
| 4 | +const mockConfig = { |
| 5 | + getPhoenixApiUrl: () => undefined as string | undefined, |
| 6 | + getPhoenixApiKey: () => undefined as string | undefined, |
| 7 | + getPhoenixAgentId: () => undefined as string | undefined, |
| 8 | +}; |
| 9 | + |
| 10 | +describe('GithubTokenRefreshService', () => { |
| 11 | + let service: GithubTokenRefreshService; |
| 12 | + const envBackup: Record<string, string | undefined> = {}; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + envBackup.MCP_CONFIG_JSON = process.env.MCP_CONFIG_JSON; |
| 16 | + delete process.env.MCP_CONFIG_JSON; |
| 17 | + service = new GithubTokenRefreshService(mockConfig as never); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + service.onModuleDestroy(); |
| 22 | + process.env.MCP_CONFIG_JSON = envBackup.MCP_CONFIG_JSON; |
| 23 | + }); |
| 24 | + |
| 25 | + test('skips refresh when Phoenix config is missing', async () => { |
| 26 | + mockConfig.getPhoenixApiUrl = () => undefined; |
| 27 | + mockConfig.getPhoenixApiKey = () => undefined; |
| 28 | + mockConfig.getPhoenixAgentId = () => undefined; |
| 29 | + |
| 30 | + const result = await service.refreshToken(); |
| 31 | + expect(result).toBeNull(); |
| 32 | + }); |
| 33 | + |
| 34 | + test('fetches token and updates MCP_CONFIG_JSON', async () => { |
| 35 | + mockConfig.getPhoenixApiUrl = () => 'https://phoenix.test'; |
| 36 | + mockConfig.getPhoenixApiKey = () => 'plgr_test123'; |
| 37 | + mockConfig.getPhoenixAgentId = () => '42'; |
| 38 | + |
| 39 | + const originalFetch = globalThis.fetch; |
| 40 | + globalThis.fetch = mock(async () => |
| 41 | + new Response(JSON.stringify({ token: 'ghs_fresh_token', expires_in: 3000 }), { |
| 42 | + status: 200, |
| 43 | + headers: { 'Content-Type': 'application/json' }, |
| 44 | + }) |
| 45 | + ) as typeof fetch; |
| 46 | + |
| 47 | + try { |
| 48 | + const result = await service.refreshToken(); |
| 49 | + |
| 50 | + expect(result).toBe('ghs_fresh_token'); |
| 51 | + expect(globalThis.fetch).toHaveBeenCalledWith( |
| 52 | + 'https://phoenix.test/api/agents/42/github_token', |
| 53 | + { |
| 54 | + method: 'GET', |
| 55 | + headers: { Authorization: 'Bearer plgr_test123' }, |
| 56 | + } |
| 57 | + ); |
| 58 | + |
| 59 | + // Verify MCP_CONFIG_JSON was updated |
| 60 | + const config = JSON.parse(process.env.MCP_CONFIG_JSON!); |
| 61 | + expect(config.mcpServers.github.env.GITHUB_PERSONAL_ACCESS_TOKEN).toBe( |
| 62 | + 'ghs_fresh_token' |
| 63 | + ); |
| 64 | + } finally { |
| 65 | + globalThis.fetch = originalFetch; |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + test('returns null on 404 (no GitHub App installed)', async () => { |
| 70 | + mockConfig.getPhoenixApiUrl = () => 'https://phoenix.test'; |
| 71 | + mockConfig.getPhoenixApiKey = () => 'plgr_test123'; |
| 72 | + mockConfig.getPhoenixAgentId = () => '42'; |
| 73 | + |
| 74 | + const originalFetch = globalThis.fetch; |
| 75 | + globalThis.fetch = mock(async () => |
| 76 | + new Response('Not Found', { status: 404 }) |
| 77 | + ) as typeof fetch; |
| 78 | + |
| 79 | + try { |
| 80 | + const result = await service.refreshToken(); |
| 81 | + expect(result).toBeNull(); |
| 82 | + } finally { |
| 83 | + globalThis.fetch = originalFetch; |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + test('returns null on server error', async () => { |
| 88 | + mockConfig.getPhoenixApiUrl = () => 'https://phoenix.test'; |
| 89 | + mockConfig.getPhoenixApiKey = () => 'plgr_test123'; |
| 90 | + mockConfig.getPhoenixAgentId = () => '42'; |
| 91 | + |
| 92 | + const originalFetch = globalThis.fetch; |
| 93 | + globalThis.fetch = mock(async () => |
| 94 | + new Response('Internal Server Error', { status: 500 }) |
| 95 | + ) as typeof fetch; |
| 96 | + |
| 97 | + try { |
| 98 | + const result = await service.refreshToken(); |
| 99 | + expect(result).toBeNull(); |
| 100 | + } finally { |
| 101 | + globalThis.fetch = originalFetch; |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + test('returns null on network error', async () => { |
| 106 | + mockConfig.getPhoenixApiUrl = () => 'https://phoenix.test'; |
| 107 | + mockConfig.getPhoenixApiKey = () => 'plgr_test123'; |
| 108 | + mockConfig.getPhoenixAgentId = () => '42'; |
| 109 | + |
| 110 | + const originalFetch = globalThis.fetch; |
| 111 | + globalThis.fetch = mock(async () => { |
| 112 | + throw new Error('ECONNREFUSED'); |
| 113 | + }) as typeof fetch; |
| 114 | + |
| 115 | + try { |
| 116 | + const result = await service.refreshToken(); |
| 117 | + expect(result).toBeNull(); |
| 118 | + } finally { |
| 119 | + globalThis.fetch = originalFetch; |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + test('preserves existing MCP config when updating token', async () => { |
| 124 | + process.env.MCP_CONFIG_JSON = JSON.stringify({ |
| 125 | + mcpServers: { |
| 126 | + 'playgrounds-dev': { serverUrl: 'https://test/mcp' }, |
| 127 | + Sentry: { serverUrl: 'https://sentry/mcp' }, |
| 128 | + }, |
| 129 | + }); |
| 130 | + |
| 131 | + mockConfig.getPhoenixApiUrl = () => 'https://phoenix.test'; |
| 132 | + mockConfig.getPhoenixApiKey = () => 'plgr_test123'; |
| 133 | + mockConfig.getPhoenixAgentId = () => '42'; |
| 134 | + |
| 135 | + const originalFetch = globalThis.fetch; |
| 136 | + globalThis.fetch = mock(async () => |
| 137 | + new Response(JSON.stringify({ token: 'ghs_new_token', expires_in: 3000 }), { |
| 138 | + status: 200, |
| 139 | + headers: { 'Content-Type': 'application/json' }, |
| 140 | + }) |
| 141 | + ) as typeof fetch; |
| 142 | + |
| 143 | + try { |
| 144 | + await service.refreshToken(); |
| 145 | + |
| 146 | + const config = JSON.parse(process.env.MCP_CONFIG_JSON!); |
| 147 | + expect(config.mcpServers['playgrounds-dev'].serverUrl).toBe( |
| 148 | + 'https://test/mcp' |
| 149 | + ); |
| 150 | + expect(config.mcpServers.Sentry.serverUrl).toBe('https://sentry/mcp'); |
| 151 | + expect(config.mcpServers.github.env.GITHUB_PERSONAL_ACCESS_TOKEN).toBe( |
| 152 | + 'ghs_new_token' |
| 153 | + ); |
| 154 | + } finally { |
| 155 | + globalThis.fetch = originalFetch; |
| 156 | + } |
| 157 | + }); |
| 158 | +}); |
0 commit comments