|
17 | 17 | package com.google.adk.sessions; |
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static java.nio.charset.StandardCharsets.UTF_8; |
20 | 21 |
|
21 | | -import com.fasterxml.jackson.databind.JsonNode; |
22 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
23 | | -import com.google.adk.JsonBaseModel; |
24 | | -import com.google.adk.events.Event; |
25 | 22 | import com.google.genai.types.Blob; |
26 | 23 | import com.google.genai.types.Content; |
27 | 24 | import com.google.genai.types.Part; |
28 | | -import java.nio.charset.StandardCharsets; |
29 | | -import java.time.Instant; |
| 25 | +import java.util.Base64; |
30 | 26 | import org.junit.Test; |
31 | 27 | import org.junit.runner.RunWith; |
32 | 28 | import org.junit.runners.JUnit4; |
33 | 29 |
|
34 | 30 | @RunWith(JUnit4.class) |
35 | 31 | public class SessionUtilsTest { |
36 | 32 |
|
37 | | - private static final ObjectMapper objectMapper = JsonBaseModel.getMapper(); |
38 | | - private static final byte[] PAYLOAD = "payload".getBytes(StandardCharsets.UTF_8); |
| 33 | + private static final byte[] payload = "payload".getBytes(UTF_8); |
39 | 34 |
|
40 | | - private static Content contentWithInlineData() { |
| 35 | + private static Content contentWithInlineData(byte[] data) { |
41 | 36 | return Content.builder() |
42 | 37 | .role("user") |
43 | 38 | .parts( |
44 | 39 | Part.builder() |
45 | | - .inlineData(Blob.builder().mimeType("image/png").data(PAYLOAD).build()) |
| 40 | + .inlineData( |
| 41 | + Blob.builder().mimeType("image/png").displayName("cat.png").data(data).build()) |
46 | 42 | .build()) |
47 | 43 | .build(); |
48 | 44 | } |
49 | 45 |
|
50 | 46 | @Test |
51 | | - public void encodeContent_inlineData_preservesMimeType() { |
52 | | - Content encoded = SessionUtils.encodeContent(contentWithInlineData()); |
| 47 | + public void encodeContent_inlineData_encodesDataAndKeepsMetadata() { |
| 48 | + Content encoded = SessionUtils.encodeContent(contentWithInlineData(payload)); |
53 | 49 |
|
54 | | - Blob encodedBlob = encoded.parts().get().get(0).inlineData().get(); |
55 | | - assertThat(encodedBlob.mimeType()).hasValue("image/png"); |
| 50 | + Blob blob = encoded.parts().get().get(0).inlineData().get(); |
| 51 | + assertThat(Base64.getDecoder().decode(blob.data().get())).isEqualTo(payload); |
| 52 | + assertThat(blob.mimeType()).hasValue("image/png"); |
| 53 | + assertThat(blob.displayName()).hasValue("cat.png"); |
56 | 54 | } |
57 | 55 |
|
58 | 56 | @Test |
59 | | - public void decodeContent_inlineData_preservesMimeType() { |
60 | | - Content decoded = SessionUtils.decodeContent(contentWithInlineData()); |
| 57 | + public void decodeContent_base64InlineData_decodesDataAndKeepsMetadata() { |
| 58 | + Content decoded = |
| 59 | + SessionUtils.decodeContent(contentWithInlineData(Base64.getEncoder().encode(payload))); |
61 | 60 |
|
62 | | - Blob decodedBlob = decoded.parts().get().get(0).inlineData().get(); |
63 | | - assertThat(decodedBlob.mimeType()).hasValue("image/png"); |
| 61 | + Blob blob = decoded.parts().get().get(0).inlineData().get(); |
| 62 | + assertThat(blob.data().get()).isEqualTo(payload); |
| 63 | + assertThat(blob.mimeType()).hasValue("image/png"); |
| 64 | + assertThat(blob.displayName()).hasValue("cat.png"); |
64 | 65 | } |
65 | 66 |
|
66 | 67 | @Test |
67 | 68 | public void decodeContent_encodeContent_roundTripsInlineData() { |
68 | 69 | Content roundTripped = |
69 | | - SessionUtils.decodeContent(SessionUtils.encodeContent(contentWithInlineData())); |
| 70 | + SessionUtils.decodeContent(SessionUtils.encodeContent(contentWithInlineData(payload))); |
70 | 71 |
|
71 | 72 | Blob blob = roundTripped.parts().get().get(0).inlineData().get(); |
| 73 | + assertThat(blob.data().get()).isEqualTo(payload); |
72 | 74 | assertThat(blob.mimeType()).hasValue("image/png"); |
73 | | - assertThat(blob.data().get()).isEqualTo(PAYLOAD); |
74 | | - } |
75 | | - |
76 | | - /** The MIME type has to survive the JSON the Vertex AI Sessions API stores the event in. */ |
77 | | - @Test |
78 | | - public void convertEventToJson_inlineData_persistsMimeType() throws Exception { |
79 | | - Event event = |
80 | | - Event.builder() |
81 | | - .author("user") |
82 | | - .invocationId("inv-123") |
83 | | - .timestamp(Instant.parse("2023-01-01T00:00:00Z").toEpochMilli()) |
84 | | - .content(contentWithInlineData()) |
85 | | - .build(); |
86 | | - |
87 | | - JsonNode jsonNode = objectMapper.readTree(SessionJsonConverter.convertEventToJson(event)); |
88 | | - |
89 | | - JsonNode inlineData = jsonNode.get("content").get("parts").get(0).get("inlineData"); |
90 | | - assertThat(inlineData.has("mimeType")).isTrue(); |
91 | | - assertThat(inlineData.get("mimeType").asText()).isEqualTo("image/png"); |
| 75 | + assertThat(blob.displayName()).hasValue("cat.png"); |
92 | 76 | } |
93 | 77 | } |
0 commit comments