import { describe, it, expect, vi, beforeEach } from 'vitest';
import { send } from '../../src/tools/send.js';
import type { RegistryApiClient } from '../../src/client/api.js';
import { testAgent2 } from '../fixtures/keys.js';
describe('tools/send', () => {
let mockClient: RegistryApiClient;
beforeEach(() => {
mockClient = {
sendMessage: vi.fn(),
} as unknown as RegistryApiClient;
});
it('should send message to domain', async () => {
vi.mocked(mockClient.sendMessage).mockResolvedValue({
message_id: 'msg-123',
thread_id: 'thread-456',
});
const result = await send(
{
to: testAgent2.domain!,
body: 'Hello!',
subject: 'Greeting',
},
mockClient
);
expect(mockClient.sendMessage).toHaveBeenCalledWith({
to_origin: testAgent2.domain,
to_agent_id: undefined,
subject: 'Greeting',
body: 'Hello!',
thread_id: undefined,
metadata: undefined,
});
expect(result.success).toBe(true);
expect(result.messageId).toBe('msg-123');
expect(result.threadId).toBe('thread-456');
});
it('should send message to agent ID', async () => {
vi.mocked(mockClient.sendMessage).mockResolvedValue({
message_id: 'msg-123',
thread_id: 'thread-456',
});
const result = await send(
{
to: testAgent2.id,
body: 'Hello!',
},
mockClient
);
expect(mockClient.sendMessage).toHaveBeenCalledWith({
to_origin: '',
to_agent_id: testAgent2.id,
subject: undefined,
body: 'Hello!',
thread_id: undefined,
metadata: undefined,
});
expect(result.success).toBe(true);
});
it('should continue existing thread', async () => {
vi.mocked(mockClient.sendMessage).mockResolvedValue({
message_id: 'msg-789',
thread_id: 'thread-existing',
});
const result = await send(
{
to: testAgent2.domain!,
body: 'Follow up message',
threadId: 'thread-existing',
},
mockClient
);
expect(mockClient.sendMessage).toHaveBeenCalledWith(
expect.objectContaining({
thread_id: 'thread-existing',
})
);
expect(result.success).toBe(true);
expect(result.threadId).toBe('thread-existing');
});
it('should handle send failure', async () => {
vi.mocked(mockClient.sendMessage).mockRejectedValue(new Error('Recipient not found'));
const result = await send(
{
to: 'unknown.example.com',
body: 'Hello!',
},
mockClient
);
expect(result.success).toBe(false);
expect(result.error).toContain('Recipient not found');
});
it('should include metadata when provided', async () => {
vi.mocked(mockClient.sendMessage).mockResolvedValue({
message_id: 'msg-123',
thread_id: 'thread-456',
});
await send(
{
to: testAgent2.domain!,
body: 'Hello!',
metadata: { priority: 'high', type: 'notification' },
},
mockClient
);
expect(mockClient.sendMessage).toHaveBeenCalledWith(
expect.objectContaining({
metadata: { priority: 'high', type: 'notification' },
})
);
});
});