1616
1717package org .springframework .ai .mistralai ;
1818
19+ import java .util .Arrays ;
1920import java .util .List ;
2021
2122import org .junit .jupiter .api .Test ;
2829
2930import static org .assertj .core .api .Assertions .assertThat ;
3031import static org .mockito .ArgumentMatchers .any ;
32+ import static org .mockito .Mockito .verify ;
3133import static org .mockito .Mockito .when ;
3234
3335/**
@@ -77,7 +79,7 @@ void testDimensionsForCodestralEmbedModel() {
7779 void testDimensionsFallbackForUnknownModel () {
7880 MistralAiApi mockApi = createMockApiWithEmbeddingResponse (512 );
7981
80- // Use a model name that doesn't exist in KNOWN_EMBEDDING_DIMENSIONS
82+ // Use a model name that doesn't exist in knownEmbeddingDimensions.
8183 MistralAiEmbeddingOptions options = MistralAiEmbeddingOptions .builder ().withModel ("unknown-model" ).build ();
8284
8385 MistralAiEmbeddingModel model = MistralAiEmbeddingModel .builder ()
@@ -87,17 +89,23 @@ void testDimensionsFallbackForUnknownModel() {
8789 .retryTemplate (RetryUtils .DEFAULT_RETRY_TEMPLATE )
8890 .build ();
8991
90- // Should fall back to super.dimensions() which detects dimensions from the API
91- // response
92+ // For the first call, it should fall back to super.dimensions() which detects
93+ // dimensions from the API response.
9294 assertThat (model .dimensions ()).isEqualTo (512 );
95+
96+ // For the second call, it should use the cache mechanism.
97+ assertThat (model .dimensions ()).isEqualTo (512 );
98+
99+ // Verify that super.dimensions() has been called once.
100+ verify (mockApi ).embeddings (any ());
93101 }
94102
95103 @ Test
96104 void testAllEmbeddingModelsHaveDimensionMapping () {
97- // This test ensures that KNOWN_EMBEDDING_DIMENSIONS map stays in sync with the
98- // EmbeddingModel enum
105+ // This test ensures that knownEmbeddingDimensions map stays in sync with the
106+ // EmbeddingModel enum.
99107 // If a new model is added to the enum but not to the dimensions map, this test
100- // will help catch it
108+ // will help catch it.
101109
102110 for (MistralAiApi .EmbeddingModel embeddingModel : MistralAiApi .EmbeddingModel .values ()) {
103111 MistralAiApi mockApi = createMockApiWithEmbeddingResponse (1024 );
@@ -138,16 +146,13 @@ private MistralAiApi createMockApiWithEmbeddingResponse(int dimensions) {
138146
139147 // Create a mock embedding response with the specified dimensions
140148 float [] embedding = new float [dimensions ];
141- for (int i = 0 ; i < dimensions ; i ++) {
142- embedding [i ] = 0.1f ;
143- }
149+ Arrays .fill (embedding , 0.1f );
144150
145151 MistralAiApi .Embedding embeddingData = new MistralAiApi .Embedding (0 , embedding , "embedding" );
146152
147153 MistralAiApi .Usage usage = new MistralAiApi .Usage (10 , 0 , 10 );
148154
149- MistralAiApi .EmbeddingList embeddingList = new MistralAiApi .EmbeddingList ("object" , List .of (embeddingData ),
150- "model" , usage );
155+ var embeddingList = new MistralAiApi .EmbeddingList <>("object" , List .of (embeddingData ), "model" , usage );
151156
152157 when (mockApi .embeddings (any ())).thenReturn (ResponseEntity .ok (embeddingList ));
153158
0 commit comments