opus-client.test.tsā¢3.82 kB
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { OpusClient } from './opus-client.js';
import type { ShortcutData } from './types.js';
// Create mock function that will be reused
const mockCreate = vi.fn();
// Mock the Anthropic SDK
vi.mock('@anthropic-ai/sdk', () => {
return {
default: vi.fn().mockImplementation(() => ({
messages: {
create: mockCreate,
},
})),
};
});
describe('OpusClient', () => {
let opusClient: OpusClient;
beforeEach(() => {
vi.clearAllMocks();
opusClient = new OpusClient('test-api-key');
});
describe('queryShortcuts', () => {
it('should call Anthropic API with correct parameters', async () => {
const mockShortcuts: ShortcutData[] = [
{
os: 'ubuntu',
desktop: null,
application: 'tmux',
file: 'tmux',
categories: [
{
name: 'Session Management',
shortcuts: [
{ keys: 'Prefix + D', description: 'Detach session' },
],
},
],
},
];
mockCreate.mockResolvedValue({
content: [
{
type: 'text',
text: 'To detach a tmux session, use: Prefix + D',
},
],
});
const query = 'how do I detach a tmux session?';
const result = await opusClient.queryShortcuts(query, mockShortcuts);
expect(mockCreate).toHaveBeenCalledWith({
model: 'claude-opus-4-20250514',
max_tokens: 4096,
system: expect.stringContaining('keyboard shortcuts expert'),
messages: [
{
role: 'user',
content: expect.stringContaining(query),
},
],
});
expect(result).toBe('To detach a tmux session, use: Prefix + D');
});
it('should include shortcut data in the prompt', async () => {
const mockShortcuts: ShortcutData[] = [
{
os: 'ubuntu',
desktop: 'gnome',
application: 'firefox',
file: 'firefox',
categories: [
{
name: 'Tab Management',
shortcuts: [
{ keys: 'Ctrl + T', description: 'New tab' },
],
},
],
},
];
mockCreate.mockResolvedValue({
content: [{ type: 'text', text: 'Use Ctrl + T for new tab' }],
});
await opusClient.queryShortcuts('open new tab', mockShortcuts);
const callArgs = mockCreate.mock.calls[0][0];
const userMessage = callArgs.messages[0].content;
expect(userMessage).toContain('firefox');
expect(userMessage).toContain('Ctrl + T');
expect(userMessage).toContain('New tab');
});
it('should handle empty response', async () => {
mockCreate.mockResolvedValue({
content: [],
});
const result = await opusClient.queryShortcuts('test query', []);
expect(result).toBe('No response generated');
});
it('should handle non-text content blocks', async () => {
mockCreate.mockResolvedValue({
content: [
{ type: 'image', source: 'data:...' },
],
});
const result = await opusClient.queryShortcuts('test query', []);
expect(result).toBe('No response generated');
});
it('should use API key from constructor', () => {
const customClient = new OpusClient('custom-key');
// Verify the client was constructed (implementation-specific)
expect(customClient).toBeInstanceOf(OpusClient);
});
it('should use API key from environment if not provided', () => {
process.env.ANTHROPIC_API_KEY = 'env-key';
const envClient = new OpusClient();
expect(envClient).toBeInstanceOf(OpusClient);
});
});
});