gitgram/aitimemachine/a2a/test/server/jsonrpc_transport_handler.spec.ts
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

164 lines
6.6 KiB
TypeScript

import { describe, it, beforeEach, afterEach, expect, vi, type Mock } from 'vitest';
import { JsonRpcTransportHandler } from '../../src/server/transports/jsonrpc/jsonrpc_transport_handler.js';
import { A2ARequestHandler } from '../../src/server/request_handler/a2a_request_handler.js';
import { JSONRPCErrorResponse, JSONRPCRequest } from '../../src/index.js';
describe('JsonRpcTransportHandler', () => {
let mockRequestHandler: A2ARequestHandler;
let transportHandler: JsonRpcTransportHandler;
beforeEach(() => {
mockRequestHandler = {
getAgentCard: vi.fn(),
getAuthenticatedExtendedAgentCard: vi.fn(),
sendMessage: vi.fn(),
sendMessageStream: vi.fn(),
getTask: vi.fn(),
cancelTask: vi.fn(),
setTaskPushNotificationConfig: vi.fn(),
getTaskPushNotificationConfig: vi.fn(),
listTaskPushNotificationConfigs: vi.fn(),
deleteTaskPushNotificationConfig: vi.fn(),
resubscribe: vi.fn(),
};
transportHandler = new JsonRpcTransportHandler(mockRequestHandler);
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('Check JSON-RPC request format', () => {
it('should return a parse error for an invalid JSON string', async () => {
const invalidJson = '{ "jsonrpc": "2.0", "method": "foo", "id": 1, }'; // trailing comma
const response = (await transportHandler.handle(invalidJson)) as JSONRPCErrorResponse;
expect(response.error.code).to.equal(-32700); // Parse error
});
it('should return a parse error for a non-string/non-object request body', async () => {
const response = (await transportHandler.handle(123)) as JSONRPCErrorResponse;
expect(response.error.code).to.equal(-32700); // Parse error
expect(response.error.message).to.equal('Invalid request body type.');
});
it('should return an invalid request error for missing jsonrpc property', async () => {
const request = { method: 'foo', id: 1 };
const response = (await transportHandler.handle(request)) as JSONRPCErrorResponse;
expect(response.error.code).to.equal(-32600); // Invalid Request
expect(response.error.message).to.equal('Invalid JSON-RPC Request.');
expect(response.id).to.equal(1);
});
it('should return an invalid request error for incorrect jsonrpc version', async () => {
const request = { jsonrpc: '1.0', method: 'foo', id: 1 };
const response = (await transportHandler.handle(request)) as JSONRPCErrorResponse;
expect(response.error.code).to.equal(-32600); // Invalid Request
expect(response.error.message).to.equal('Invalid JSON-RPC Request.');
expect(response.id).to.equal(1);
});
it('should return an invalid request error for missing method property', async () => {
const request = { jsonrpc: '2.0', id: 1 };
const response = (await transportHandler.handle(request)) as JSONRPCErrorResponse;
expect(response.error.code).to.equal(-32600); // Invalid Request
expect(response.error.message).to.equal('Invalid JSON-RPC Request.');
expect(response.id).to.equal(1);
});
it('should return an invalid request error for non-string method property', async () => {
const request = { jsonrpc: '2.0', method: 123, id: 1 };
const response = (await transportHandler.handle(request)) as JSONRPCErrorResponse;
expect(response.error.code).to.equal(-32600); // Invalid Request
expect(response.error.message).to.equal('Invalid JSON-RPC Request.');
expect(response.id).to.equal(1);
});
it('should return an invalid request error for invalid id type (object)', async () => {
const request = { jsonrpc: '2.0', method: 'foo', id: {} };
const response = (await transportHandler.handle(request)) as JSONRPCErrorResponse;
expect(response.error.code).to.equal(-32600); // Invalid Request
expect(response.error.message).to.equal('Invalid JSON-RPC Request.');
expect(response.id).to.deep.equal({});
});
it('should return an invalid request error for invalid id type (float)', async () => {
const request = { jsonrpc: '2.0', method: 'foo', id: 1.23 };
const response = (await transportHandler.handle(request)) as JSONRPCErrorResponse;
expect(response.error.code).to.equal(-32600); // Invalid Request
expect(response.error.message).to.equal('Invalid JSON-RPC Request.');
expect(response.id).to.equal(1.23);
});
it('should handle valid request with string id', async () => {
const request = {
jsonrpc: '2.0',
method: 'message/send',
id: 'abc-123',
params: {},
};
const response = await transportHandler.handle(request);
expect(response).to.have.property('result');
});
it('should handle valid request with integer id', async () => {
const request = {
jsonrpc: '2.0',
method: 'message/send',
id: 456,
params: {},
};
const response = await transportHandler.handle(request);
expect(response).to.have.property('result');
});
it('should handle valid request with null id', async () => {
const request: JSONRPCRequest = {
jsonrpc: '2.0',
method: 'message/send',
id: null,
params: {},
};
(mockRequestHandler.getAuthenticatedExtendedAgentCard as Mock).mockResolvedValue({
card: 'data',
});
const response = await transportHandler.handle(request);
expect(response).to.have.property('result');
});
const invalidParamsCases = [
{ name: 'null', params: null },
{ name: 'undefined', params: undefined },
{ name: 'a string', params: 'invalid' },
{ name: 'an array', params: [1, 2, 3] },
{ name: 'an object with an empty string key', params: { '': 'invalid' } },
];
invalidParamsCases.forEach(({ name, params }) => {
it(`should return an invalid params error if params are ${name}`, async () => {
const request = {
jsonrpc: '2.0',
method: 'message/send',
id: 1,
params,
};
const response = (await transportHandler.handle(request)) as JSONRPCErrorResponse;
expect(response.error.code).to.equal(-32602); // Invalid Params
expect(response.error.message).to.equal('Invalid method parameters.');
expect(response.id).to.equal(1);
});
});
it('should handle valid request with params as dict', async () => {
const request = {
jsonrpc: '2.0',
method: 'message/send',
id: 456,
params: { this: 'is a dict' },
};
const response = await transportHandler.handle(request);
expect(response).to.have.property('result');
});
});
});