|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 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 | + |
| 17 | +package com.google.adk.agents; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.databind.JsonNode; |
| 22 | +import com.google.adk.JsonBaseModel; |
| 23 | +import com.google.genai.types.Content; |
| 24 | +import com.google.genai.types.Part; |
| 25 | +import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.junit.runners.JUnit4; |
| 29 | + |
| 30 | +@RunWith(JUnit4.class) |
| 31 | +public final class LiveRequestQueueTest { |
| 32 | + |
| 33 | + private static final Content CONTENT = Content.fromParts(Part.fromText("context")); |
| 34 | + |
| 35 | + @Test |
| 36 | + public void content_defaultsToCompletingTurn() { |
| 37 | + LiveRequestQueue queue = new LiveRequestQueue(); |
| 38 | + TestSubscriber<LiveRequest> subscriber = queue.get().test(); |
| 39 | + |
| 40 | + queue.content(CONTENT); |
| 41 | + |
| 42 | + subscriber.assertValueCount(1); |
| 43 | + LiveRequest request = subscriber.values().get(0); |
| 44 | + assertThat(request.content()).hasValue(CONTENT); |
| 45 | + assertThat(request.partial()).hasValue(false); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void content_withPartialTrue_preservesValue() { |
| 50 | + LiveRequestQueue queue = new LiveRequestQueue(); |
| 51 | + TestSubscriber<LiveRequest> subscriber = queue.get().test(); |
| 52 | + |
| 53 | + queue.content(CONTENT, true); |
| 54 | + |
| 55 | + subscriber.assertValueCount(1); |
| 56 | + LiveRequest request = subscriber.values().get(0); |
| 57 | + assertThat(request.content()).hasValue(CONTENT); |
| 58 | + assertThat(request.partial()).hasValue(true); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void content_withPartialFalse_preservesValue() { |
| 63 | + LiveRequestQueue queue = new LiveRequestQueue(); |
| 64 | + TestSubscriber<LiveRequest> subscriber = queue.get().test(); |
| 65 | + |
| 66 | + queue.content(CONTENT, false); |
| 67 | + |
| 68 | + subscriber.assertValueCount(1); |
| 69 | + LiveRequest request = subscriber.values().get(0); |
| 70 | + assertThat(request.content()).hasValue(CONTENT); |
| 71 | + assertThat(request.partial()).hasValue(false); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void liveRequest_builderDefaultsToNonPartial() { |
| 76 | + LiveRequest request = LiveRequest.builder().content(CONTENT).build(); |
| 77 | + |
| 78 | + assertThat(request.partial()).hasValue(false); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void liveRequest_jsonRoundTrip_preservesPartialTrue() { |
| 83 | + LiveRequest request = LiveRequest.builder().content(CONTENT).partial(true).build(); |
| 84 | + |
| 85 | + JsonNode json = JsonBaseModel.getMapper().valueToTree(request); |
| 86 | + LiveRequest restored = LiveRequest.fromJsonString(request.toJson()); |
| 87 | + |
| 88 | + assertThat(json.get("partial").booleanValue()).isTrue(); |
| 89 | + assertThat(json.has("turnComplete")).isFalse(); |
| 90 | + assertThat(restored.content()).hasValue(CONTENT); |
| 91 | + assertThat(restored.partial()).hasValue(true); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void liveRequest_jsonRoundTrip_preservesPartialFalse() { |
| 96 | + LiveRequest request = LiveRequest.builder().content(CONTENT).partial(false).build(); |
| 97 | + |
| 98 | + JsonNode json = JsonBaseModel.getMapper().valueToTree(request); |
| 99 | + LiveRequest restored = LiveRequest.fromJsonString(request.toJson()); |
| 100 | + |
| 101 | + assertThat(json.get("partial").booleanValue()).isFalse(); |
| 102 | + assertThat(json.has("turnComplete")).isFalse(); |
| 103 | + assertThat(restored.content()).hasValue(CONTENT); |
| 104 | + assertThat(restored.partial()).hasValue(false); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void liveRequest_jsonWithoutPartial_defaultsToNonPartial() { |
| 109 | + LiveRequest request = |
| 110 | + LiveRequest.fromJsonString( |
| 111 | + "{\"content\":{\"role\":\"user\",\"parts\":[{\"text\":\"context\"}]}}"); |
| 112 | + |
| 113 | + assertThat(request.content()).hasValue(CONTENT); |
| 114 | + assertThat(request.partial()).hasValue(false); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void liveRequest_toBuilder_preservesPartial() { |
| 119 | + LiveRequest request = LiveRequest.builder().content(CONTENT).partial(true).build(); |
| 120 | + |
| 121 | + assertThat(request.toBuilder().build().partial()).hasValue(true); |
| 122 | + } |
| 123 | +} |
0 commit comments