|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.ollama.api; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 26 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | +import static org.mockito.Mockito.when; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link OllamaApiHelper} |
| 34 | + * |
| 35 | + * @author Sun Yuhan |
| 36 | + */ |
| 37 | +@ExtendWith(MockitoExtension.class) |
| 38 | +class OllamaApiHelperTests { |
| 39 | + |
| 40 | + @Test |
| 41 | + void isStreamingToolCallWhenResponseIsNullShouldReturnFalse() { |
| 42 | + boolean result = OllamaApiHelper.isStreamingToolCall(null); |
| 43 | + assertThat(result).isFalse(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void isStreamingToolCallWhenMessageIsNullShouldReturnFalse() { |
| 48 | + OllamaApi.ChatResponse response = mock(OllamaApi.ChatResponse.class); |
| 49 | + when(response.message()).thenReturn(null); |
| 50 | + |
| 51 | + boolean result = OllamaApiHelper.isStreamingToolCall(response); |
| 52 | + assertThat(result).isFalse(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void isStreamingToolCallWhenToolCallsIsNullShouldReturnFalse() { |
| 57 | + OllamaApi.ChatResponse response = mock(OllamaApi.ChatResponse.class); |
| 58 | + OllamaApi.Message message = mock(OllamaApi.Message.class); |
| 59 | + when(response.message()).thenReturn(message); |
| 60 | + when(message.toolCalls()).thenReturn(null); |
| 61 | + |
| 62 | + boolean result = OllamaApiHelper.isStreamingToolCall(response); |
| 63 | + assertThat(result).isFalse(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void isStreamingToolCallWhenToolCallsIsEmptyShouldReturnFalse() { |
| 68 | + OllamaApi.ChatResponse response = mock(OllamaApi.ChatResponse.class); |
| 69 | + OllamaApi.Message message = mock(OllamaApi.Message.class); |
| 70 | + when(response.message()).thenReturn(message); |
| 71 | + when(message.toolCalls()).thenReturn(Collections.emptyList()); |
| 72 | + |
| 73 | + boolean result = OllamaApiHelper.isStreamingToolCall(response); |
| 74 | + assertThat(result).isFalse(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void isStreamingToolCallWhenToolCallsHasElementsShouldReturnTrue() { |
| 79 | + OllamaApi.ChatResponse response = mock(OllamaApi.ChatResponse.class); |
| 80 | + OllamaApi.Message message = mock(OllamaApi.Message.class); |
| 81 | + List<OllamaApi.Message.ToolCall> toolCalls = Arrays.asList(mock(OllamaApi.Message.ToolCall.class)); |
| 82 | + when(response.message()).thenReturn(message); |
| 83 | + when(message.toolCalls()).thenReturn(toolCalls); |
| 84 | + |
| 85 | + boolean result = OllamaApiHelper.isStreamingToolCall(response); |
| 86 | + assertThat(result).isTrue(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void isStreamingDoneWhenResponseIsNullShouldReturnFalse() { |
| 91 | + boolean result = OllamaApiHelper.isStreamingDone(null); |
| 92 | + assertThat(result).isFalse(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void isStreamingDoneWhenDoneIsFalseShouldReturnFalse() { |
| 97 | + OllamaApi.ChatResponse response = mock(OllamaApi.ChatResponse.class); |
| 98 | + when(response.done()).thenReturn(false); |
| 99 | + |
| 100 | + boolean result = OllamaApiHelper.isStreamingDone(response); |
| 101 | + assertThat(result).isFalse(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void isStreamingDoneWhenDoneReasonIsNotStopShouldReturnFalse() { |
| 106 | + OllamaApi.ChatResponse response = mock(OllamaApi.ChatResponse.class); |
| 107 | + when(response.done()).thenReturn(true); |
| 108 | + when(response.doneReason()).thenReturn("other"); |
| 109 | + |
| 110 | + boolean result = OllamaApiHelper.isStreamingDone(response); |
| 111 | + assertThat(result).isFalse(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void isStreamingDoneWhenDoneIsTrueAndDoneReasonIsStopShouldReturnTrue() { |
| 116 | + OllamaApi.ChatResponse response = mock(OllamaApi.ChatResponse.class); |
| 117 | + when(response.done()).thenReturn(true); |
| 118 | + when(response.doneReason()).thenReturn("stop"); |
| 119 | + |
| 120 | + boolean result = OllamaApiHelper.isStreamingDone(response); |
| 121 | + assertThat(result).isTrue(); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void mergeWhenBothResponsesHaveValuesShouldMergeCorrectly() { |
| 126 | + Instant previousCreatedAt = Instant.now().minusSeconds(10); |
| 127 | + OllamaApi.Message previousMessage = OllamaApi.Message.builder(OllamaApi.Message.Role.ASSISTANT) |
| 128 | + .content("Previous content") |
| 129 | + .thinking("Previous thinking") |
| 130 | + .images(Arrays.asList("image1")) |
| 131 | + .toolCalls(Arrays.asList(mock(OllamaApi.Message.ToolCall.class))) |
| 132 | + .toolName("Previous tool") |
| 133 | + .build(); |
| 134 | + |
| 135 | + OllamaApi.ChatResponse previous = new OllamaApi.ChatResponse("previous-model", previousCreatedAt, |
| 136 | + previousMessage, "previous-reason", false, 100L, 50L, 10, 200L, 5, 100L); |
| 137 | + |
| 138 | + Instant currentCreatedAt = Instant.now(); |
| 139 | + OllamaApi.Message currentMessage = OllamaApi.Message.builder(OllamaApi.Message.Role.USER) |
| 140 | + .content("Current content") |
| 141 | + .thinking("Current thinking") |
| 142 | + .images(Arrays.asList("image2")) |
| 143 | + .toolCalls(Arrays.asList(mock(OllamaApi.Message.ToolCall.class))) |
| 144 | + .toolName("Current tool") |
| 145 | + .build(); |
| 146 | + |
| 147 | + OllamaApi.ChatResponse current = new OllamaApi.ChatResponse("current-model", currentCreatedAt, currentMessage, |
| 148 | + "stop", true, 200L, 100L, 20, 400L, 10, 200L); |
| 149 | + |
| 150 | + OllamaApi.ChatResponse result = OllamaApiHelper.merge(previous, current); |
| 151 | + |
| 152 | + assertThat(result.model()).isEqualTo("previous-modelcurrent-model"); |
| 153 | + assertThat(result.createdAt()).isEqualTo(currentCreatedAt); |
| 154 | + assertThat(result.message().content()).isEqualTo("Previous contentCurrent content"); |
| 155 | + assertThat(result.message().thinking()).isEqualTo("Previous thinkingCurrent thinking"); |
| 156 | + assertThat(result.message().role()).isEqualTo(OllamaApi.Message.Role.USER); |
| 157 | + assertThat(result.message().images()).containsExactly("image1", "image2"); |
| 158 | + assertThat(result.message().toolCalls()).hasSize(2); |
| 159 | + assertThat(result.message().toolName()).isEqualTo("Previous toolCurrent tool"); |
| 160 | + assertThat(result.doneReason()).isEqualTo("stop"); |
| 161 | + assertThat(result.done()).isTrue(); |
| 162 | + assertThat(result.totalDuration()).isEqualTo(300L); |
| 163 | + assertThat(result.loadDuration()).isEqualTo(150L); |
| 164 | + assertThat(result.promptEvalCount()).isEqualTo(30); |
| 165 | + assertThat(result.promptEvalDuration()).isEqualTo(600L); |
| 166 | + assertThat(result.evalCount()).isEqualTo(15); |
| 167 | + assertThat(result.evalDuration()).isEqualTo(300L); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void mergeStringsShouldConcatenate() { |
| 172 | + OllamaApi.Message previousMessage = OllamaApi.Message.builder(OllamaApi.Message.Role.ASSISTANT) |
| 173 | + .content("Hello") |
| 174 | + .thinking("Think") |
| 175 | + .toolName("Tool") |
| 176 | + .build(); |
| 177 | + OllamaApi.ChatResponse previous = new OllamaApi.ChatResponse("model1", Instant.now(), previousMessage, |
| 178 | + "reason1", false, null, null, null, null, null, null); |
| 179 | + |
| 180 | + OllamaApi.Message currentMessage = OllamaApi.Message.builder(OllamaApi.Message.Role.ASSISTANT) |
| 181 | + .content(" World") |
| 182 | + .thinking("ing") |
| 183 | + .toolName("Box") |
| 184 | + .build(); |
| 185 | + OllamaApi.ChatResponse current = new OllamaApi.ChatResponse("model2", Instant.now(), currentMessage, "reason2", |
| 186 | + true, null, null, null, null, null, null); |
| 187 | + |
| 188 | + OllamaApi.ChatResponse result = OllamaApiHelper.merge(previous, current); |
| 189 | + |
| 190 | + assertThat(result.model()).isEqualTo("model1model2"); |
| 191 | + assertThat(result.message().content()).isEqualTo("Hello World"); |
| 192 | + assertThat(result.message().thinking()).isEqualTo("Thinking"); |
| 193 | + assertThat(result.message().toolName()).isEqualTo("ToolBox"); |
| 194 | + assertThat(result.doneReason()).isEqualTo("reason2"); |
| 195 | + assertThat(result.done()).isTrue(); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + void mergeNumbersShouldSum() { |
| 200 | + OllamaApi.Message dummyMessage = OllamaApi.Message.builder(OllamaApi.Message.Role.ASSISTANT).build(); |
| 201 | + |
| 202 | + OllamaApi.ChatResponse previous = new OllamaApi.ChatResponse(null, null, dummyMessage, null, null, 100L, 50L, |
| 203 | + 10, 200L, 5, 100L); |
| 204 | + |
| 205 | + OllamaApi.ChatResponse current = new OllamaApi.ChatResponse(null, null, dummyMessage, null, null, 200L, 100L, |
| 206 | + 20, 400L, 10, 200L); |
| 207 | + |
| 208 | + OllamaApi.ChatResponse result = OllamaApiHelper.merge(previous, current); |
| 209 | + |
| 210 | + assertThat(result.totalDuration()).isEqualTo(300L); |
| 211 | + assertThat(result.loadDuration()).isEqualTo(150L); |
| 212 | + assertThat(result.promptEvalCount()).isEqualTo(30); |
| 213 | + assertThat(result.promptEvalDuration()).isEqualTo(600L); |
| 214 | + assertThat(result.evalCount()).isEqualTo(15); |
| 215 | + assertThat(result.evalDuration()).isEqualTo(300L); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + void mergeListsShouldCombine() { |
| 220 | + OllamaApi.Message previousMessage = OllamaApi.Message.builder(OllamaApi.Message.Role.ASSISTANT) |
| 221 | + .images(Arrays.asList("image1", "image2")) |
| 222 | + .build(); |
| 223 | + OllamaApi.ChatResponse previous = new OllamaApi.ChatResponse(null, null, previousMessage, null, null, null, |
| 224 | + null, null, null, null, null); |
| 225 | + |
| 226 | + OllamaApi.Message currentMessage = OllamaApi.Message.builder(OllamaApi.Message.Role.ASSISTANT) |
| 227 | + .images(Arrays.asList("image3", "image4")) |
| 228 | + .build(); |
| 229 | + OllamaApi.ChatResponse current = new OllamaApi.ChatResponse(null, null, currentMessage, null, null, null, null, |
| 230 | + null, null, null, null); |
| 231 | + |
| 232 | + OllamaApi.ChatResponse result = OllamaApiHelper.merge(previous, current); |
| 233 | + |
| 234 | + assertThat(result.message().images()).containsExactly("image1", "image2", "image3", "image4"); |
| 235 | + } |
| 236 | + |
| 237 | +} |
0 commit comments