Addresses code review feedback about missing automated tests for the ClaudeCodeProvider. ## Changes - Added unit tests for ClaudeCodeProvider class covering constructor, validateAuth, and getClient methods - Added unit tests for ClaudeCodeLanguageModel testing lazy loading behavior and error handling - Added integration tests verifying optional dependency behavior when @anthropic-ai/claude-code is not installed ## Test Coverage 1. **Unit Tests**: - ClaudeCodeProvider: Basic functionality, no API key requirement, client creation - ClaudeCodeLanguageModel: Model initialization, lazy loading, error messages, warning generation 2. **Integration Tests**: - Optional dependency behavior when package is not installed - Clear error messages for users about missing package - Provider instantiation works but usage fails gracefully All tests pass and provide comprehensive coverage for the claude-code provider implementation.
104 lines
2.9 KiB
JavaScript
104 lines
2.9 KiB
JavaScript
import { jest } from '@jest/globals';
|
|
|
|
// Mock the claude-code SDK module
|
|
jest.unstable_mockModule('../../../src/ai-providers/custom-sdk/claude-code/index.js', () => ({
|
|
createClaudeCode: jest.fn(() => {
|
|
const provider = (modelId, settings) => ({
|
|
// Mock language model
|
|
id: modelId,
|
|
settings
|
|
});
|
|
provider.languageModel = jest.fn((id, settings) => ({ id, settings }));
|
|
provider.chat = provider.languageModel;
|
|
return provider;
|
|
})
|
|
}));
|
|
|
|
// Mock the base provider
|
|
jest.unstable_mockModule('../../../src/ai-providers/base-provider.js', () => ({
|
|
BaseAIProvider: class {
|
|
constructor() {
|
|
this.name = 'Base Provider';
|
|
}
|
|
handleError(context, error) {
|
|
throw error;
|
|
}
|
|
}
|
|
}));
|
|
|
|
// Import after mocking
|
|
const { ClaudeCodeProvider } = await import('../../../src/ai-providers/claude-code.js');
|
|
|
|
describe('ClaudeCodeProvider', () => {
|
|
let provider;
|
|
|
|
beforeEach(() => {
|
|
provider = new ClaudeCodeProvider();
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
describe('constructor', () => {
|
|
it('should set the provider name to Claude Code', () => {
|
|
expect(provider.name).toBe('Claude Code');
|
|
});
|
|
});
|
|
|
|
describe('validateAuth', () => {
|
|
it('should not throw an error (no API key required)', () => {
|
|
expect(() => provider.validateAuth({})).not.toThrow();
|
|
});
|
|
|
|
it('should not require any parameters', () => {
|
|
expect(() => provider.validateAuth()).not.toThrow();
|
|
});
|
|
|
|
it('should work with any params passed', () => {
|
|
expect(() => provider.validateAuth({
|
|
apiKey: 'some-key',
|
|
baseURL: 'https://example.com'
|
|
})).not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('getClient', () => {
|
|
it('should return a claude code client', () => {
|
|
const client = provider.getClient({});
|
|
expect(client).toBeDefined();
|
|
expect(typeof client).toBe('function');
|
|
});
|
|
|
|
it('should create client without API key or base URL', () => {
|
|
const client = provider.getClient({});
|
|
expect(client).toBeDefined();
|
|
});
|
|
|
|
it('should handle params even though they are not used', () => {
|
|
const client = provider.getClient({
|
|
baseURL: 'https://example.com',
|
|
apiKey: 'unused-key'
|
|
});
|
|
expect(client).toBeDefined();
|
|
});
|
|
|
|
it('should have languageModel and chat methods', () => {
|
|
const client = provider.getClient({});
|
|
expect(client.languageModel).toBeDefined();
|
|
expect(client.chat).toBeDefined();
|
|
expect(client.chat).toBe(client.languageModel);
|
|
});
|
|
});
|
|
|
|
describe('error handling', () => {
|
|
it('should handle client initialization errors', async () => {
|
|
// Force an error by making createClaudeCode throw
|
|
const { createClaudeCode } = await import('../../../src/ai-providers/custom-sdk/claude-code/index.js');
|
|
createClaudeCode.mockImplementationOnce(() => {
|
|
throw new Error('Mock initialization error');
|
|
});
|
|
|
|
// Create a new provider instance to use the mocked createClaudeCode
|
|
const errorProvider = new ClaudeCodeProvider();
|
|
expect(() => errorProvider.getClient({})).toThrow('Mock initialization error');
|
|
});
|
|
});
|
|
}); |