|
| 1 | +import { fetchMock } from 'cloudflare:test' |
| 2 | +import { beforeAll, describe, expect, it } from 'vitest' |
| 3 | + |
| 4 | +import { fetchCloudflareApi } from './cloudflare-api' |
| 5 | +import { McpError } from './mcp-error' |
| 6 | + |
| 7 | +beforeAll(() => { |
| 8 | + fetchMock.activate() |
| 9 | + fetchMock.disableNetConnect() |
| 10 | +}) |
| 11 | + |
| 12 | +describe('fetchCloudflareApi', () => { |
| 13 | + const baseParams = { |
| 14 | + endpoint: '/workers/scripts', |
| 15 | + accountId: 'test-account-id', |
| 16 | + apiToken: 'test-api-token', |
| 17 | + } |
| 18 | + |
| 19 | + it('returns parsed data on success', async () => { |
| 20 | + const responseData = { result: { id: 'test-script' } } |
| 21 | + fetchMock |
| 22 | + .get('https://api.cloudflare.com') |
| 23 | + .intercept({ |
| 24 | + path: '/client/v4/accounts/test-account-id/workers/scripts', |
| 25 | + method: 'GET', |
| 26 | + }) |
| 27 | + .reply(200, responseData) |
| 28 | + |
| 29 | + const result = await fetchCloudflareApi(baseParams) |
| 30 | + expect(result).toEqual(responseData) |
| 31 | + }) |
| 32 | + |
| 33 | + it('throws McpError with status 404 for not found', async () => { |
| 34 | + fetchMock |
| 35 | + .get('https://api.cloudflare.com') |
| 36 | + .intercept({ |
| 37 | + path: '/client/v4/accounts/test-account-id/workers/scripts', |
| 38 | + method: 'GET', |
| 39 | + }) |
| 40 | + .reply(404, JSON.stringify({ errors: [{ message: 'Script not found' }] })) |
| 41 | + |
| 42 | + try { |
| 43 | + await fetchCloudflareApi(baseParams) |
| 44 | + expect.unreachable() |
| 45 | + } catch (e) { |
| 46 | + expect(e).toBeInstanceOf(McpError) |
| 47 | + const err = e as McpError |
| 48 | + expect(err.code).toBe(404) |
| 49 | + expect(err.reportToSentry).toBe(false) |
| 50 | + expect(err.message).toBe('Cloudflare API request failed') |
| 51 | + expect(err.internalMessage).toContain('Script not found') |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + it('throws McpError with status 403 for forbidden', async () => { |
| 56 | + fetchMock |
| 57 | + .get('https://api.cloudflare.com') |
| 58 | + .intercept({ |
| 59 | + path: '/client/v4/accounts/test-account-id/workers/scripts', |
| 60 | + method: 'GET', |
| 61 | + }) |
| 62 | + .reply(403, JSON.stringify({ errors: [{ message: 'Forbidden' }] })) |
| 63 | + |
| 64 | + try { |
| 65 | + await fetchCloudflareApi(baseParams) |
| 66 | + expect.unreachable() |
| 67 | + } catch (e) { |
| 68 | + expect(e).toBeInstanceOf(McpError) |
| 69 | + const err = e as McpError |
| 70 | + expect(err.code).toBe(403) |
| 71 | + expect(err.reportToSentry).toBe(false) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + it('throws McpError with status 429 for rate limiting', async () => { |
| 76 | + fetchMock |
| 77 | + .get('https://api.cloudflare.com') |
| 78 | + .intercept({ |
| 79 | + path: '/client/v4/accounts/test-account-id/workers/scripts', |
| 80 | + method: 'GET', |
| 81 | + }) |
| 82 | + .reply(429, JSON.stringify({ errors: [{ message: 'Rate limited' }] })) |
| 83 | + |
| 84 | + try { |
| 85 | + await fetchCloudflareApi(baseParams) |
| 86 | + expect.unreachable() |
| 87 | + } catch (e) { |
| 88 | + expect(e).toBeInstanceOf(McpError) |
| 89 | + const err = e as McpError |
| 90 | + expect(err.code).toBe(429) |
| 91 | + expect(err.reportToSentry).toBe(false) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + it('throws McpError with status 502 for upstream 500 (bad gateway)', async () => { |
| 96 | + fetchMock |
| 97 | + .get('https://api.cloudflare.com') |
| 98 | + .intercept({ |
| 99 | + path: '/client/v4/accounts/test-account-id/workers/scripts', |
| 100 | + method: 'GET', |
| 101 | + }) |
| 102 | + .reply(500, 'Internal Server Error') |
| 103 | + |
| 104 | + try { |
| 105 | + await fetchCloudflareApi(baseParams) |
| 106 | + expect.unreachable() |
| 107 | + } catch (e) { |
| 108 | + expect(e).toBeInstanceOf(McpError) |
| 109 | + const err = e as McpError |
| 110 | + expect(err.code).toBe(502) |
| 111 | + expect(err.message).toBe('Upstream Cloudflare API unavailable') |
| 112 | + expect(err.reportToSentry).toBe(true) |
| 113 | + expect(err.internalMessage).toContain('Cloudflare API 500') |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + it('throws McpError with status 502 for upstream 502', async () => { |
| 118 | + fetchMock |
| 119 | + .get('https://api.cloudflare.com') |
| 120 | + .intercept({ |
| 121 | + path: '/client/v4/accounts/test-account-id/workers/scripts', |
| 122 | + method: 'GET', |
| 123 | + }) |
| 124 | + .reply(502, 'Bad Gateway') |
| 125 | + |
| 126 | + try { |
| 127 | + await fetchCloudflareApi(baseParams) |
| 128 | + expect.unreachable() |
| 129 | + } catch (e) { |
| 130 | + expect(e).toBeInstanceOf(McpError) |
| 131 | + const err = e as McpError |
| 132 | + expect(err.code).toBe(502) |
| 133 | + expect(err.message).toBe('Upstream Cloudflare API unavailable') |
| 134 | + expect(err.reportToSentry).toBe(true) |
| 135 | + expect(err.internalMessage).toContain('Cloudflare API 502') |
| 136 | + } |
| 137 | + }) |
| 138 | + |
| 139 | + it('preserves error text in internalMessage (not user-facing message)', async () => { |
| 140 | + const errorBody = '{"errors":[{"message":"Worker not found","code":10007}]}' |
| 141 | + fetchMock |
| 142 | + .get('https://api.cloudflare.com') |
| 143 | + .intercept({ |
| 144 | + path: '/client/v4/accounts/test-account-id/workers/scripts', |
| 145 | + method: 'GET', |
| 146 | + }) |
| 147 | + .reply(404, errorBody) |
| 148 | + |
| 149 | + try { |
| 150 | + await fetchCloudflareApi(baseParams) |
| 151 | + expect.unreachable() |
| 152 | + } catch (e) { |
| 153 | + expect(e).toBeInstanceOf(McpError) |
| 154 | + const err = e as McpError |
| 155 | + expect(err.message).toBe('Cloudflare API request failed') |
| 156 | + expect(err.internalMessage).toContain('Worker not found') |
| 157 | + } |
| 158 | + }) |
| 159 | +}) |
0 commit comments