| 1 | import { afterEach, describe, expect, it, vi } from 'vitest';
|
| 2 | import { callOpenClaspTool, sendMcpA2ARequest } from '../connectors/botpress/src/mcp.js';
|
| 3 |
|
| 4 | afterEach(() => {
|
| 5 | vi.unstubAllGlobals();
|
| 6 | vi.restoreAllMocks();
|
| 7 | });
|
| 8 |
|
| 9 | describe('Botpress OpenClasp MCP bridge', () => {
|
| 10 | it('calls a tool with the paired bearer token and parses an SSE result', async () => {
|
| 11 | const fetchMock = vi.fn().mockResolvedValue(
|
| 12 | new Response(
|
| 13 | `event: message\ndata: ${JSON.stringify({
|
| 14 | jsonrpc: '2.0',
|
| 15 | id: 'request',
|
| 16 | result: { content: [{ type: 'text', text: JSON.stringify({ ready: true }) }] },
|
| 17 | })}\n\n`,
|
| 18 | { status: 200, headers: { 'content-type': 'text/event-stream' } },
|
| 19 | ),
|
| 20 | );
|
| 21 | vi.stubGlobal('fetch', fetchMock);
|
| 22 |
|
| 23 | await expect(
|
| 24 | callOpenClaspTool('https://openclasp.dev/', 'oc_at_secret', 'openclasp_search_agents', {
|
| 25 | query: 'buyer',
|
| 26 | }),
|
| 27 | ).resolves.toEqual({ ready: true });
|
| 28 | expect(fetchMock).toHaveBeenCalledWith(
|
| 29 | 'https://openclasp.dev/mcp',
|
| 30 | expect.objectContaining({
|
| 31 | method: 'POST',
|
| 32 | headers: expect.objectContaining({ authorization: 'Bearer oc_at_secret' }),
|
| 33 | }),
|
| 34 | );
|
| 35 | });
|
| 36 |
|
| 37 | it('sends a returned MCP A2A request directly to the peer', async () => {
|
| 38 | const fetchMock = vi.fn().mockResolvedValue(new Response('{}', { status: 200 }));
|
| 39 | const logMock = vi.spyOn(console, 'info').mockImplementation(() => undefined);
|
| 40 | vi.stubGlobal('fetch', fetchMock);
|
| 41 |
|
| 42 | await expect(
|
| 43 | sendMcpA2ARequest({
|
| 44 | a2a: {
|
| 45 | endpoint: 'https://peer.example/a2a',
|
| 46 | bearerToken: 'session-token',
|
| 47 | request: {
|
| 48 | params: {
|
| 49 | message: {
|
| 50 | role: 'user',
|
| 51 | parts: [{ kind: 'text', text: 'private message body' }],
|
| 52 | },
|
| 53 | },
|
| 54 | },
|
| 55 | },
|
| 56 | plan: { interactionId: 'interaction-1' },
|
| 57 | }),
|
| 58 | ).resolves.toBe(true);
|
| 59 | expect(fetchMock).toHaveBeenCalledWith(
|
| 60 | 'https://peer.example/a2a',
|
| 61 | expect.objectContaining({ method: 'POST' }),
|
| 62 | );
|
| 63 | expect(logMock).toHaveBeenCalledWith(
|
| 64 | '[openclasp-a2a]',
|
| 65 | expect.objectContaining({
|
| 66 | event: 'mcp_a2a_request.completed',
|
| 67 | interactionId: 'interaction-1',
|
| 68 | statusCode: 200,
|
| 69 | }),
|
| 70 | );
|
| 71 | expect(JSON.stringify(logMock.mock.calls)).not.toContain('private message body');
|
| 72 | expect(JSON.stringify(logMock.mock.calls)).not.toContain('session-token');
|
| 73 | expect(JSON.stringify(logMock.mock.calls)).not.toContain('peer.example');
|
| 74 | });
|
| 75 | });
|