22import { jest , describe , expect , test , beforeEach } from '@jest/globals' ;
33import 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
1830describe ( '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