Skip to content

Commit d79e1f5

Browse files
committed
fix remote test runs in github actions
1 parent 18763cb commit d79e1f5

File tree

2 files changed

+100
-16
lines changed

2 files changed

+100
-16
lines changed

src/providers/OpenAIEmbeddingProvider.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// src/providers/OpenAIEmbeddingProvider.js
12
import BaseEmbeddingProvider from './BaseEmbeddingProvider.js';
23
import axios from 'axios';
34
import debug from 'debug';
@@ -14,6 +15,8 @@ class OpenAIEmbeddingProvider extends BaseEmbeddingProvider {
1415

1516
this.apiKey = options.apiKey;
1617
this.model = options.model || 'text-embedding-3-small';
18+
19+
// Create axios instance with default config
1720
this.client = axios.create({
1821
baseURL: 'https://api.openai.com/v1',
1922
headers: {
@@ -34,12 +37,15 @@ class OpenAIEmbeddingProvider extends BaseEmbeddingProvider {
3437
input: texts
3538
});
3639

40+
if (!response.data || !response.data.data) {
41+
throw new Error('Unexpected response format from OpenAI API');
42+
}
43+
3744
const embeddings = response.data.data.map(item => item.embedding);
38-
3945
log(`Successfully got embeddings for batch`);
4046
return embeddings;
4147
} catch (error) {
42-
if (error.response?.data) {
48+
if (error.response?.data?.error) {
4349
throw new Error(`OpenAI API error: ${error.response.data.error.message}`);
4450
}
4551
throw error;

src/providers/providers.test.js

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,49 @@
22
import { jest, describe, expect, test, beforeEach } from '@jest/globals';
33
import OpenAIEmbeddingProvider from '../../src/providers/OpenAIEmbeddingProvider.js';
44

5-
// Mock axios
6-
await jest.unstable_mockModule('axios', () => ({
7-
default: {
8-
create: jest.fn(() => ({
9-
post: jest.fn().mockResolvedValue({
10-
data: {
11-
data: [{ embedding: Array(1536).fill(0.1) }]
5+
// Mock the axios module
6+
jest.mock('axios', () => {
7+
const mockPost = jest.fn().mockResolvedValue({
8+
data: {
9+
data: [{ embedding: Array(1536).fill(0.1) }]
10+
}
11+
});
12+
13+
return {
14+
default: {
15+
create: jest.fn(() => ({
16+
post: mockPost,
17+
defaults: {
18+
headers: {
19+
common: {}
20+
}
1221
}
13-
})
14-
}))
15-
}
16-
}));
22+
}))
23+
}
24+
};
25+
});
26+
27+
// Import axios after mocking
28+
import axios from 'axios';
1729

1830
describe('Embedding Providers', () => {
1931
let provider;
2032
const mockApiKey = 'test-key-123';
33+
let mockAxiosPost;
2134

2235
beforeEach(() => {
23-
// Reset mocks
36+
// Clear all mocks
2437
jest.clearAllMocks();
2538

39+
// Create new provider instance
2640
provider = new OpenAIEmbeddingProvider({
2741
apiKey: mockApiKey,
2842
model: 'text-embedding-3-small',
2943
dimensions: 1536
3044
});
45+
46+
// Get reference to the mock post function
47+
mockAxiosPost = axios.create().post;
3148
});
3249

3350
test('should initialize with proper configuration', () => {
@@ -36,9 +53,21 @@ describe('Embedding Providers', () => {
3653
});
3754

3855
test('should get embeddings from the OpenAI provider', async () => {
56+
// Set up mock response for single text
57+
mockAxiosPost.mockResolvedValueOnce({
58+
data: {
59+
data: [{ embedding: Array(1536).fill(0.1) }]
60+
}
61+
});
62+
3963
const texts = ['test text'];
4064
const result = await provider.getEmbeddings(texts);
4165

66+
expect(mockAxiosPost).toHaveBeenCalledWith('/embeddings', {
67+
model: 'text-embedding-3-small',
68+
input: texts
69+
});
70+
4271
expect(result).toHaveLength(1);
4372
expect(result[0]).toHaveLength(1536);
4473
expect(typeof result[0][0]).toBe('number');
@@ -49,23 +78,72 @@ describe('Embedding Providers', () => {
4978
test('should handle empty input', async () => {
5079
const result = await provider.getEmbeddings([]);
5180
expect(result).toHaveLength(0);
81+
expect(mockAxiosPost).not.toHaveBeenCalled();
5282
});
5383

5484
test('should handle batch processing', async () => {
85+
// Set up mock response for batch
86+
mockAxiosPost.mockResolvedValueOnce({
87+
data: {
88+
data: Array(5).fill({ embedding: Array(1536).fill(0.1) })
89+
}
90+
});
91+
5592
const texts = Array(5).fill('test text');
5693
const result = await provider.getEmbeddings(texts);
94+
95+
expect(mockAxiosPost).toHaveBeenCalledWith('/embeddings', {
96+
model: 'text-embedding-3-small',
97+
input: texts
98+
});
99+
57100
expect(result).toHaveLength(5);
58101
expect(result[0]).toHaveLength(1536);
59102
});
60103

61104
test('should respect batch size limits', async () => {
62-
const provider = new OpenAIEmbeddingProvider({
105+
// Create provider with small batch size
106+
const smallBatchProvider = new OpenAIEmbeddingProvider({
63107
apiKey: mockApiKey,
64108
batchSize: 2
65109
});
66110

111+
// Set up mock responses for each batch
112+
mockAxiosPost
113+
.mockResolvedValueOnce({
114+
data: {
115+
data: Array(2).fill({ embedding: Array(1536).fill(0.1) })
116+
}
117+
})
118+
.mockResolvedValueOnce({
119+
data: {
120+
data: Array(2).fill({ embedding: Array(1536).fill(0.1) })
121+
}
122+
})
123+
.mockResolvedValueOnce({
124+
data: {
125+
data: Array(1).fill({ embedding: Array(1536).fill(0.1) })
126+
}
127+
});
128+
67129
const texts = Array(5).fill('test text');
68-
const result = await provider.getEmbeddings(texts);
130+
const result = await smallBatchProvider.getEmbeddings(texts);
131+
69132
expect(result).toHaveLength(5);
133+
expect(mockAxiosPost).toHaveBeenCalledTimes(3); // Should be called three times for batches of 2,2,1
134+
});
135+
136+
test('should handle API errors', async () => {
137+
mockAxiosPost.mockRejectedValueOnce({
138+
response: {
139+
data: {
140+
error: {
141+
message: 'Test API error'
142+
}
143+
}
144+
}
145+
});
146+
147+
await expect(provider.getEmbeddings(['test'])).rejects.toThrow('Test API error');
70148
});
71149
});

0 commit comments

Comments
 (0)