import { describe, it, expect, vi, beforeEach } from 'vitest';
import { lookup } from '../../src/tools/lookup.js';
import type { RegistryApiClient } from '../../src/client/api.js';
import { testAgent1, testAgent2 } from '../fixtures/keys.js';
describe('tools/lookup', () => {
let mockClient: RegistryApiClient;
beforeEach(() => {
mockClient = {
lookupById: vi.fn(),
lookupByDomain: vi.fn(),
discover: vi.fn(),
} as unknown as RegistryApiClient;
});
describe('lookup by ID', () => {
it('should lookup agent by ID', async () => {
vi.mocked(mockClient.lookupById).mockResolvedValue(testAgent1);
const result = await lookup({ agentId: testAgent1.id }, mockClient);
expect(mockClient.lookupById).toHaveBeenCalledWith(testAgent1.id);
expect(result.found).toBe(true);
expect(result.agent?.id).toBe(testAgent1.id);
expect(result.agent?.name).toBe(testAgent1.name);
expect(result.agent?.keys).toHaveLength(1);
});
it('should handle not found', async () => {
vi.mocked(mockClient.lookupById).mockRejectedValue(new Error('Agent not found'));
const result = await lookup({ agentId: '99999999-9999-9999-9999-999999999999' }, mockClient);
expect(result.found).toBe(false);
expect(result.error).toContain('Agent not found');
});
});
describe('lookup by domain', () => {
it('should lookup agent by domain', async () => {
vi.mocked(mockClient.lookupByDomain).mockResolvedValue(testAgent1);
const result = await lookup({ domain: testAgent1.domain! }, mockClient);
expect(mockClient.lookupByDomain).toHaveBeenCalledWith(testAgent1.domain);
expect(result.found).toBe(true);
expect(result.agent?.domain).toBe(testAgent1.domain);
});
it('should handle domain not found', async () => {
vi.mocked(mockClient.lookupByDomain).mockRejectedValue(new Error('Domain not registered'));
const result = await lookup({ domain: 'unknown.example.com' }, mockClient);
expect(result.found).toBe(false);
expect(result.error).toContain('Domain not registered');
});
});
describe('discovery search', () => {
it('should search by query', async () => {
vi.mocked(mockClient.discover).mockResolvedValue({
data: [testAgent1, testAgent2],
total: 2,
page: 1,
pageSize: 20,
hasMore: false,
});
const result = await lookup({ query: 'test' }, mockClient);
expect(mockClient.discover).toHaveBeenCalledWith({
query: 'test',
capabilities: undefined,
});
expect(result.found).toBe(true);
expect(result.agents).toHaveLength(2);
expect(result.total).toBe(2);
expect(result.hasMore).toBe(false);
});
it('should filter by capabilities', async () => {
vi.mocked(mockClient.discover).mockResolvedValue({
data: [testAgent2],
total: 1,
page: 1,
pageSize: 20,
hasMore: false,
});
const result = await lookup({ capabilities: ['translate'] }, mockClient);
expect(mockClient.discover).toHaveBeenCalledWith({
query: undefined,
capabilities: ['translate'],
});
expect(result.found).toBe(true);
expect(result.agents).toHaveLength(1);
});
it('should return empty results when no matches', async () => {
vi.mocked(mockClient.discover).mockResolvedValue({
data: [],
total: 0,
page: 1,
pageSize: 20,
hasMore: false,
});
const result = await lookup({ query: 'nonexistent' }, mockClient);
expect(result.found).toBe(false);
expect(result.agents).toHaveLength(0);
expect(result.total).toBe(0);
});
});
});