import { describe, expect, it, vi } from 'vitest'; import { RedditBotApiClient, RedditBotApiError } from '../src/mcp/api-client.js'; describe('RedditBotApiClient', () => { it('uses a bearer token and parses JSON responses', async () => { const fetchImplementation = vi.fn(async () => new Response(JSON.stringify({ running: true }), { status: 200 })); const client = new RedditBotApiClient({ baseUrl: 'http://redditbot.test/', token: 'read-token', fetchImplementation: fetchImplementation as typeof fetch, }); await expect(client.get('/api/v1/control')).resolves.toEqual({ running: true }); expect(fetchImplementation).toHaveBeenCalledWith('http://redditbot.test/api/v1/control', expect.objectContaining({ headers: expect.objectContaining({ Authorization: 'Bearer read-token' }), })); }); it('returns structured API errors to MCP tools', async () => { const client = new RedditBotApiClient({ token: 'read-token', fetchImplementation: vi.fn(async () => new Response(JSON.stringify({ error: 'Agent credential lacks the required access' }), { status: 403 })) as typeof fetch, }); await expect(client.post('/api/v1/control/start')).rejects.toEqual( new RedditBotApiError('Agent credential lacks the required access', 403), ); }); });