gitza-mcp/tests/protocol.test.ts

75 lines
2.7 KiB
TypeScript

import { it, expect, vi } from 'vitest';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
import { createMcpServer } from '../src/server.js';
import { GitzaApi } from '../src/api.js';
it('negotiates MCP, discovers tools and returns bounded, structured results', async () => {
const api = new GitzaApi(
'https://git.example',
'token',
async () => new Response(JSON.stringify({ id: 1, full_name: 'alice/repo', name: 'repo' })),
);
const server = createMcpServer(async () => ({
api,
repositories: [{ id: 1, name: 'alice/repo' }],
write: false,
}));
const client = new Client({ name: 'protocol-test', version: '1' });
const [a, b] = InMemoryTransport.createLinkedPair();
await server.connect(a);
await client.connect(b);
try {
expect((await client.listTools()).tools).toHaveLength(15);
const result = await client.callTool({
name: 'get_repository',
arguments: { repository: 'alice/repo' },
});
expect(result.isError).not.toBe(true);
expect(result.structuredContent).toEqual({
data: { id: 1, full_name: 'alice/repo', name: 'repo' },
});
const denied = await client.callTool({
name: 'create_issue',
arguments: { repository: 'alice/repo', title: 'no' },
});
expect(denied.isError).toBe(true);
} finally {
await client.close();
await server.close();
}
});
it('redacts audit storage failures and warns that a write may have completed', async () => {
const stderr = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
const api = new GitzaApi(
'https://git.example',
'secret',
async () => new Response(JSON.stringify({ id: 1, full_name: 'alice/repo' })),
);
const server = createMcpServer(
async () => ({ api, repositories: [{ id: 1, name: 'alice/repo' }], write: true }),
async () => {
throw new Error('postgres://private:secret@internal');
},
);
const client = new Client({ name: 'audit-test', version: '1' });
const [a, b] = InMemoryTransport.createLinkedPair();
await server.connect(a);
await client.connect(b);
try {
const result = await client.callTool({
name: 'create_issue',
arguments: { repository: 'alice/repo', title: 'one issue' },
});
expect(result.isError).toBe(true);
expect(JSON.stringify(result)).toContain('AUDIT_UNAVAILABLE');
expect(JSON.stringify(result)).toContain('Check Gitza before retrying');
expect(JSON.stringify(result)).not.toContain('secret');
expect(stderr).toHaveBeenCalledWith('MCP audit write failed.\n');
} finally {
await client.close();
await server.close();
stderr.mockRestore();
}
});