OpenAIEmbeddings.test.tsโข5.57 kB
/**
* Unit tests for OpenAIEmbeddings
*/
import { jest } from '@jest/globals';
import { OpenAIEmbeddings } from '../../embeddings/providers/OpenAIEmbeddings';
describe('OpenAIEmbeddings', () => {
describe('Constructor', () => {
it('should create instance with API key and default model', () => {
const embeddings = new OpenAIEmbeddings('test-api-key');
const info = embeddings.getModelInfo();
expect(info.provider).toBe('openai');
expect(info.model).toBe('text-embedding-3-small');
expect(info.dimensions).toBe(1536);
expect(info.available).toBe(false); // Not initialized yet
});
it('should create instance with custom model', () => {
const embeddings = new OpenAIEmbeddings('test-key', 'text-embedding-ada-002', 1536);
const info = embeddings.getModelInfo();
expect(info.model).toBe('text-embedding-ada-002');
expect(info.dimensions).toBe(1536);
});
it('should use default dimensions if not specified', () => {
const embeddings = new OpenAIEmbeddings('test-key', 'text-embedding-3-small');
const info = embeddings.getModelInfo();
expect(info.dimensions).toBe(1536);
});
it('should create instance with custom dimensions', () => {
const embeddings = new OpenAIEmbeddings('test-key', 'custom-model', 3072);
const info = embeddings.getModelInfo();
expect(info.dimensions).toBe(3072);
});
});
describe('isAvailable', () => {
it('should return false before initialization', () => {
const embeddings = new OpenAIEmbeddings('test-key');
expect(embeddings.isAvailable()).toBe(false);
});
});
describe('embed', () => {
it('should throw error when not initialized', async () => {
const embeddings = new OpenAIEmbeddings('test-key');
await expect(embeddings.embed('test')).rejects.toThrow('not available');
});
});
describe('embedBatch', () => {
it('should throw error when not initialized', async () => {
const embeddings = new OpenAIEmbeddings('test-key');
await expect(embeddings.embedBatch(['test1', 'test2'])).rejects.toThrow('not initialized');
});
});
describe('getModelInfo', () => {
it('should return correct model information', () => {
const embeddings = new OpenAIEmbeddings('test-key', 'test-model', 768);
const info = embeddings.getModelInfo();
expect(info).toEqual({
provider: 'openai',
model: 'test-model',
dimensions: 768,
available: false,
});
});
});
describe('Initialize - Failure Cases', () => {
it('should handle missing API key', async () => {
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
const embeddings = new OpenAIEmbeddings('');
await embeddings.initialize();
expect(embeddings.isAvailable()).toBe(false);
consoleWarnSpy.mockRestore();
});
it('should handle missing OpenAI package', async () => {
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
const embeddings = new OpenAIEmbeddings('test-key');
await embeddings.initialize();
// Should log warning about missing package
expect(embeddings.isAvailable()).toBe(false);
consoleWarnSpy.mockRestore();
});
it('should not throw during initialization failure', async () => {
const embeddings = new OpenAIEmbeddings('invalid-key');
// Should not throw, just mark as unavailable
await expect(embeddings.initialize()).resolves.not.toThrow();
expect(embeddings.isAvailable()).toBe(false);
});
});
describe('Edge Cases', () => {
it('should handle empty API key', () => {
const embeddings = new OpenAIEmbeddings('');
const info = embeddings.getModelInfo();
expect(info.available).toBe(false);
});
it('should handle various model names', () => {
const models = [
'text-embedding-3-small',
'text-embedding-3-large',
'text-embedding-ada-002',
'custom-model',
];
for (const model of models) {
const embeddings = new OpenAIEmbeddings('test-key', model);
const info = embeddings.getModelInfo();
expect(info.model).toBe(model);
expect(info.dimensions).toBeGreaterThan(0);
}
});
it('should handle unknown model with default dimensions', () => {
const embeddings = new OpenAIEmbeddings('test-key', 'unknown-model');
const info = embeddings.getModelInfo();
// Should use fallback dimensions (1536)
expect(info.dimensions).toBe(1536);
});
});
describe('API Key Handling', () => {
it('should store API key from constructor', () => {
const embeddings = new OpenAIEmbeddings('my-secret-key');
// Verify it doesn't throw during construction
expect(embeddings).toBeDefined();
});
it('should handle whitespace in API key', () => {
const embeddings = new OpenAIEmbeddings(' test-key ');
expect(embeddings).toBeDefined();
});
});
describe('Model Information', () => {
it('should provide complete model info before initialization', () => {
const embeddings = new OpenAIEmbeddings('test-key', 'test-model', 512);
const info = embeddings.getModelInfo();
expect(info).toHaveProperty('provider');
expect(info).toHaveProperty('model');
expect(info).toHaveProperty('dimensions');
expect(info).toHaveProperty('available');
expect(info.provider).toBe('openai');
});
});
});