|
| 1 | +import { JsonRpcTransport } from '../../../src/client/transports/json_rpc_transport.js'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import sinon from 'sinon'; |
| 4 | +import { describe, it, beforeEach } from 'mocha'; |
| 5 | +import { MessageSendParams, TextPart } from '../../../src/types.js'; |
| 6 | +import { RequestOptions } from '../../../src/client/multitransport-client.js'; |
| 7 | +import { HTTP_EXTENSION_HEADER } from '../../../src/constants.js'; |
| 8 | +import { ServiceParameters, withA2AExtensions } from '../../../src/client/service-parameters.js'; |
| 9 | + |
| 10 | +describe('JsonRpcTransport', () => { |
| 11 | + let transport: JsonRpcTransport; |
| 12 | + let mockFetch: sinon.SinonStubbedFunction<typeof fetch>; |
| 13 | + const endpoint = 'https://test.endpoint/api'; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + mockFetch = sinon.stub(); |
| 17 | + transport = new JsonRpcTransport({ |
| 18 | + endpoint, |
| 19 | + fetchImpl: mockFetch, |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('sendMessage', () => { |
| 24 | + it('should correctly add the extension headers', async () => { |
| 25 | + const messageParams: MessageSendParams = { |
| 26 | + message: { |
| 27 | + kind: 'message', |
| 28 | + messageId: 'test-msg-1', |
| 29 | + role: 'user', |
| 30 | + parts: [ |
| 31 | + { |
| 32 | + kind: 'text', |
| 33 | + text: 'Hello, agent!', |
| 34 | + } as TextPart, |
| 35 | + ], |
| 36 | + }, |
| 37 | + }; |
| 38 | + |
| 39 | + const expectedExtensions = 'extension1,extension2'; |
| 40 | + const serviceParameters = ServiceParameters.create(withA2AExtensions(expectedExtensions)); |
| 41 | + const options: RequestOptions = { |
| 42 | + serviceParameters, |
| 43 | + }; |
| 44 | + |
| 45 | + mockFetch.resolves( |
| 46 | + new Response(JSON.stringify({ jsonrpc: '2.0', result: {}, id: 1 }), { |
| 47 | + status: 200, |
| 48 | + }) |
| 49 | + ); |
| 50 | + await transport.sendMessage(messageParams, options); |
| 51 | + const fetchArgs = mockFetch.firstCall.args[1]; |
| 52 | + const headers = fetchArgs.headers; |
| 53 | + expect((headers as any)[HTTP_EXTENSION_HEADER]).to.deep.equal(expectedExtensions); |
| 54 | + }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments