|
17 | 17 | package com.google.adk.events; |
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.junit.Assert.assertThrows; |
20 | 21 |
|
21 | 22 | import com.google.adk.sessions.State; |
22 | 23 | import com.google.common.collect.ImmutableMap; |
23 | 24 | import com.google.common.collect.ImmutableSet; |
24 | 25 | import com.google.genai.types.Content; |
25 | 26 | import com.google.genai.types.Part; |
| 27 | +import java.util.Map; |
26 | 28 | import java.util.concurrent.ConcurrentHashMap; |
27 | 29 | import org.junit.Test; |
28 | 30 | import org.junit.runner.RunWith; |
@@ -130,4 +132,37 @@ public void jsonSerialization_works() throws Exception { |
130 | 132 | assertThat(deserialized).isEqualTo(eventActions); |
131 | 133 | assertThat(deserialized.deletedArtifactIds()).containsExactly("d1", "d2"); |
132 | 134 | } |
| 135 | + |
| 136 | + @Test |
| 137 | + @SuppressWarnings("unchecked") // the nested map is known to be Map<String, Object> |
| 138 | + public void merge_deeplyMergesStateDelta() { |
| 139 | + EventActions eventActions1 = EventActions.builder().build(); |
| 140 | + eventActions1.stateDelta().put("a", 1); |
| 141 | + eventActions1.stateDelta().put("b", ImmutableMap.of("nested1", 10, "nested2", 20)); |
| 142 | + eventActions1.stateDelta().put("c", 100); |
| 143 | + EventActions eventActions2 = EventActions.builder().build(); |
| 144 | + eventActions2.stateDelta().put("a", 2); |
| 145 | + eventActions2.stateDelta().put("b", ImmutableMap.of("nested2", 22, "nested3", 30)); |
| 146 | + eventActions2.stateDelta().put("d", 200); |
| 147 | + |
| 148 | + EventActions merged = eventActions1.toBuilder().merge(eventActions2).build(); |
| 149 | + |
| 150 | + assertThat(merged.stateDelta().keySet()).containsExactly("a", "b", "c", "d"); |
| 151 | + assertThat(merged.stateDelta()).containsEntry("a", 2); |
| 152 | + assertThat((Map<String, Object>) merged.stateDelta().get("b")) |
| 153 | + .containsExactly("nested1", 10, "nested2", 22, "nested3", 30); |
| 154 | + assertThat(merged.stateDelta()).containsEntry("c", 100); |
| 155 | + assertThat(merged.stateDelta()).containsEntry("d", 200); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void merge_failsOnMismatchedKeyTypesNestedInStateDelta() { |
| 160 | + EventActions eventActions1 = EventActions.builder().build(); |
| 161 | + eventActions1.stateDelta().put("nested", ImmutableMap.of("a", 1)); |
| 162 | + EventActions eventActions2 = EventActions.builder().build(); |
| 163 | + eventActions2.stateDelta().put("nested", ImmutableMap.of(1, 2)); |
| 164 | + |
| 165 | + assertThrows( |
| 166 | + IllegalArgumentException.class, () -> eventActions1.toBuilder().merge(eventActions2)); |
| 167 | + } |
133 | 168 | } |
0 commit comments