|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 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 | + * http://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 | +package io.agentscope.core.formatter.gemini.dto; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +import io.agentscope.core.util.JsonUtils; |
| 22 | +import java.util.Map; |
| 23 | +import org.junit.jupiter.api.DisplayName; |
| 24 | +import org.junit.jupiter.api.Tag; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +@Tag("unit") |
| 28 | +@DisplayName("GeminiPart Unit Tests") |
| 29 | +class GeminiPartTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + @DisplayName("Should set and get top level fields") |
| 33 | + void testTopLevelFields() { |
| 34 | + GeminiPart part = new GeminiPart(); |
| 35 | + GeminiPart.GeminiFunctionCall functionCall = |
| 36 | + new GeminiPart.GeminiFunctionCall("call_1", "lookup", Map.of("k", "v")); |
| 37 | + GeminiPart.GeminiFunctionResponse functionResponse = |
| 38 | + new GeminiPart.GeminiFunctionResponse("resp_1", "lookup", Map.of("output", "ok")); |
| 39 | + GeminiPart.GeminiBlob blob = new GeminiPart.GeminiBlob("image/png", "ZmFrZQ=="); |
| 40 | + GeminiPart.GeminiFileData fileData = |
| 41 | + new GeminiPart.GeminiFileData("text/plain", "gs://bucket/file.txt"); |
| 42 | + |
| 43 | + part.setText("hello"); |
| 44 | + part.setFunctionCall(functionCall); |
| 45 | + part.setFunctionResponse(functionResponse); |
| 46 | + part.setInlineData(blob); |
| 47 | + part.setFileData(fileData); |
| 48 | + part.setThought(true); |
| 49 | + part.setSignature("sig"); |
| 50 | + part.setThoughtSignature("thought-sig"); |
| 51 | + |
| 52 | + assertEquals("hello", part.getText()); |
| 53 | + assertEquals(functionCall, part.getFunctionCall()); |
| 54 | + assertEquals(functionResponse, part.getFunctionResponse()); |
| 55 | + assertEquals(blob, part.getInlineData()); |
| 56 | + assertEquals(fileData, part.getFileData()); |
| 57 | + assertEquals(true, part.getThought()); |
| 58 | + assertEquals("sig", part.getSignature()); |
| 59 | + assertEquals("thought-sig", part.getThoughtSignature()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + @DisplayName("Should support function call constructors and setters") |
| 64 | + void testFunctionCallConstructors() { |
| 65 | + GeminiPart.GeminiFunctionCall fromTwoArgs = |
| 66 | + new GeminiPart.GeminiFunctionCall("get_weather", Map.of("city", "SF")); |
| 67 | + GeminiPart.GeminiFunctionCall fromThreeArgs = |
| 68 | + new GeminiPart.GeminiFunctionCall("call_2", "get_weather", Map.of("city", "LA")); |
| 69 | + |
| 70 | + fromTwoArgs.setId("call_3"); |
| 71 | + fromTwoArgs.setName("weather"); |
| 72 | + fromTwoArgs.setArgs(Map.of("city", "Tokyo")); |
| 73 | + |
| 74 | + assertEquals("call_3", fromTwoArgs.getId()); |
| 75 | + assertEquals("weather", fromTwoArgs.getName()); |
| 76 | + assertEquals(Map.of("city", "Tokyo"), fromTwoArgs.getArgs()); |
| 77 | + assertEquals("call_2", fromThreeArgs.getId()); |
| 78 | + assertEquals("get_weather", fromThreeArgs.getName()); |
| 79 | + assertEquals(Map.of("city", "LA"), fromThreeArgs.getArgs()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + @DisplayName("Should support function response constructors and setters") |
| 84 | + void testFunctionResponseConstructors() { |
| 85 | + GeminiPart.GeminiFunctionResponse fromTwoArgs = |
| 86 | + new GeminiPart.GeminiFunctionResponse("lookup", Map.of("output", "Paris")); |
| 87 | + GeminiPart.GeminiFunctionResponse fromThreeArgs = |
| 88 | + new GeminiPart.GeminiFunctionResponse("id_1", "lookup", Map.of("output", "Berlin")); |
| 89 | + |
| 90 | + fromTwoArgs.setId("id_2"); |
| 91 | + fromTwoArgs.setName("lookup_city"); |
| 92 | + fromTwoArgs.setResponse(Map.of("output", "Rome")); |
| 93 | + |
| 94 | + assertEquals("id_2", fromTwoArgs.getId()); |
| 95 | + assertEquals("lookup_city", fromTwoArgs.getName()); |
| 96 | + assertEquals(Map.of("output", "Rome"), fromTwoArgs.getResponse()); |
| 97 | + assertEquals("id_1", fromThreeArgs.getId()); |
| 98 | + assertEquals("lookup", fromThreeArgs.getName()); |
| 99 | + assertEquals(Map.of("output", "Berlin"), fromThreeArgs.getResponse()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + @DisplayName("Should support blob and file data constructors and setters") |
| 104 | + void testBlobAndFileData() { |
| 105 | + GeminiPart.GeminiBlob blob = new GeminiPart.GeminiBlob(); |
| 106 | + blob.setMimeType("audio/mp3"); |
| 107 | + blob.setData("YXVkaW8="); |
| 108 | + assertEquals("audio/mp3", blob.getMimeType()); |
| 109 | + assertEquals("YXVkaW8=", blob.getData()); |
| 110 | + |
| 111 | + GeminiPart.GeminiFileData fileData = new GeminiPart.GeminiFileData(); |
| 112 | + fileData.setMimeType("application/pdf"); |
| 113 | + fileData.setFileUri("https://example.com/a.pdf"); |
| 114 | + assertEquals("application/pdf", fileData.getMimeType()); |
| 115 | + assertEquals("https://example.com/a.pdf", fileData.getFileUri()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + @DisplayName("Should serialize functionCall without id field") |
| 120 | + void testFunctionCallSerializationIgnoresId() { |
| 121 | + GeminiPart part = new GeminiPart(); |
| 122 | + part.setFunctionCall( |
| 123 | + new GeminiPart.GeminiFunctionCall( |
| 124 | + "internal-id", "get_capital", Map.of("country", "Japan"))); |
| 125 | + |
| 126 | + String json = JsonUtils.getJsonCodec().toJson(part); |
| 127 | + |
| 128 | + assertTrue(json.contains("\"functionCall\"")); |
| 129 | + assertTrue(json.contains("\"name\":\"get_capital\"")); |
| 130 | + assertTrue(json.contains("\"args\":{\"country\":\"Japan\"}")); |
| 131 | + assertTrue(!json.contains("internal-id")); |
| 132 | + } |
| 133 | +} |
0 commit comments