69 lines
2.8 KiB
TypeScript
69 lines
2.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { renderHook } from '@testing-library/react';
|
|
import { useMcpTools, type McpToolInfo } from './use-mcp-tools';
|
|
|
|
// Servidores MCP reais não são iniciados: o comando Tauri devolve lista vazia.
|
|
const invokeMock = vi.hoisted(() => vi.fn(async () => []));
|
|
vi.mock('@tauri-apps/api/core', () => ({ invoke: invokeMock }));
|
|
|
|
const tool = (server_name: string, name: string, input_schema?: unknown): McpToolInfo => ({
|
|
server_name,
|
|
tool: { name, description: `desc ${name}`, input_schema },
|
|
});
|
|
|
|
function generate(tools: McpToolInfo[], selected?: string[]) {
|
|
const { result } = renderHook(() => useMcpTools());
|
|
return result.current.generateToolsContext(tools, selected);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
});
|
|
|
|
describe('generateToolsContext', () => {
|
|
it('sem ferramentas: string vazia', () => {
|
|
expect(generate([])).toBe('');
|
|
});
|
|
|
|
it('servidores selecionados sem ferramentas correspondentes: string vazia', () => {
|
|
expect(generate([tool('fs', 'read')], ['web'])).toBe('');
|
|
});
|
|
|
|
it('sem seleção: cabeçalho genérico e todas as ferramentas', () => {
|
|
const ctx = generate([tool('fs', 'read'), tool('web', 'search')]);
|
|
expect(ctx).toContain('Você tem acesso aos servidores MCP listados abaixo.');
|
|
expect(ctx).toContain('### Servidor: fs');
|
|
expect(ctx).toContain('### Servidor: web');
|
|
});
|
|
|
|
it('seleção vazia: comporta-se como sem seleção', () => {
|
|
const ctx = generate([tool('fs', 'read')], []);
|
|
expect(ctx).toContain('Você tem acesso aos servidores MCP listados abaixo.');
|
|
expect(ctx).toContain('- **read**: desc read');
|
|
});
|
|
|
|
it('com seleção: cita os servidores e omite os demais', () => {
|
|
const ctx = generate([tool('fs', 'read'), tool('web', 'search'), tool('db', 'query')], ['web', 'db']);
|
|
expect(ctx).toContain('servidores MCP selecionados: web, db.');
|
|
expect(ctx).not.toContain('### Servidor: fs');
|
|
expect(ctx).toContain('- **search**');
|
|
expect(ctx).toContain('- **query**');
|
|
});
|
|
|
|
it('duas ferramentas do mesmo servidor: um único cabeçalho de servidor', () => {
|
|
const ctx = generate([tool('fs', 'read'), tool('fs', 'write')]);
|
|
expect(ctx.match(/### Servidor: fs/g)).toHaveLength(1);
|
|
expect(ctx.indexOf('- **read**')).toBeLessThan(ctx.indexOf('- **write**'));
|
|
});
|
|
|
|
it('schema com propriedades: lista parâmetros e usa "any" quando falta o tipo', () => {
|
|
const ctx = generate([tool('fs', 'read', { properties: { path: { type: 'string' }, flags: {} } })]);
|
|
expect(ctx).toContain(' - Parâmetros: path (string), flags (any)\n');
|
|
});
|
|
|
|
it('schema sem properties: não lista parâmetros', () => {
|
|
const ctx = generate([tool('fs', 'read', { type: 'object' })]);
|
|
expect(ctx).not.toContain('Parâmetros');
|
|
});
|
|
});
|