Skip to content

Commit 2f5c95a

Browse files
google-genai-botcopybara-github
authored andcommitted
test:Add unit tests
cleanup:Add policy headers to all test files PiperOrigin-RevId: 829115628
1 parent 9360f24 commit 2f5c95a

File tree

14 files changed

+296
-40
lines changed

14 files changed

+296
-40
lines changed

core/src/main/java/com/google/adk/flows/llmflows/Identity.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
3535
StringBuilder builder =
3636
new StringBuilder()
3737
.append("You are an agent. Your internal name is ")
38-
.append("\"")
3938
.append(agent.name())
40-
.append("\"")
4139
.append(".");
4240
if (!Strings.isNullOrEmpty(agent.description())) {
43-
builder.append(" The description about you is \"").append(agent.description()).append("\".");
41+
builder.append(" The description about you is ").append(agent.description());
4442
}
4543
return Single.just(
4644
RequestProcessor.RequestProcessingResult.create(

core/src/main/java/com/google/adk/models/LlmRequest.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.google.adk.models;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
20-
import static com.google.common.base.Preconditions.checkState;
2119
import static com.google.common.collect.ImmutableList.toImmutableList;
2220
import static com.google.common.collect.ImmutableMap.toImmutableMap;
2321

@@ -172,29 +170,14 @@ public final Builder appendInstructions(List<String> instructions) {
172170
return liveConnectConfig(liveCfg.toBuilder().systemInstruction(newLiveSi).build());
173171
}
174172

175-
private Content addInstructions(
176-
Optional<Content> currentSystemInstruction, List<String> additionalInstructions) {
177-
checkArgument(
178-
currentSystemInstruction.isEmpty()
179-
|| currentSystemInstruction.get().parts().map(parts -> parts.size()).orElse(0) <= 1,
180-
"At most one instruction is supported.");
181-
182-
// Either append to the existing instruction, or create a new one.
183-
String instructions = String.join("\n\n", additionalInstructions);
184-
185-
Optional<Part> part =
186-
currentSystemInstruction
187-
.flatMap(Content::parts)
188-
.flatMap(parts -> parts.stream().findFirst());
189-
if (part.isEmpty() || part.get().text().isEmpty()) {
190-
part = Optional.of(Part.fromText(instructions));
191-
} else {
192-
part = Optional.of(Part.fromText(part.get().text().get() + "\n\n" + instructions));
193-
}
194-
checkState(part.isPresent(), "Failed to create instruction.");
173+
private Content addInstructions(Optional<Content> currentSi, List<String> newInst) {
174+
ImmutableList.Builder<Part> parts = ImmutableList.builder();
175+
currentSi.flatMap(Content::parts).ifPresent(parts::addAll);
176+
177+
newInst.stream().map(Part::fromText).forEach(parts::add);
195178

196-
String role = currentSystemInstruction.flatMap(Content::role).orElse("user");
197-
return Content.builder().parts(part.get()).role(role).build();
179+
String role = currentSi.flatMap(Content::role).orElse("user");
180+
return Content.builder().parts(parts.build()).role(role).build();
198181
}
199182

200183
@CanIgnoreReturnValue

core/src/test/java/com/google/adk/agents/CallbacksTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 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+
117
package com.google.adk.agents;
218

319
import static com.google.adk.testing.TestUtils.createEvent;

core/src/test/java/com/google/adk/agents/InstructionTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 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+
117
package com.google.adk.agents;
218

319
import static com.google.adk.testing.TestUtils.createInvocationContext;

core/src/test/java/com/google/adk/agents/ParallelAgentTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 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+
117
package com.google.adk.agents;
218

319
import static com.google.adk.testing.TestUtils.createInvocationContext;

core/src/test/java/com/google/adk/agents/RunConfigTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 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+
117
package com.google.adk.agents;
218

319
import static com.google.common.truth.Truth.assertThat;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2025 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.events;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.adk.tools.ToolConfirmation;
22+
import com.google.common.collect.ImmutableMap;
23+
import com.google.genai.types.Part;
24+
import java.util.concurrent.ConcurrentHashMap;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
@RunWith(JUnit4.class)
30+
public final class EventActionsTest {
31+
32+
private static final Part PART = Part.builder().text("text").build();
33+
private static final ToolConfirmation TOOL_CONFIRMATION =
34+
ToolConfirmation.builder().hint("hint").confirmed(true).build();
35+
36+
@Test
37+
public void toBuilder_createsBuilderWithSameValues() {
38+
EventActions eventActionsWithSkipSummarization =
39+
EventActions.builder().skipSummarization(true).build();
40+
41+
EventActions eventActionsAfterRebuild = eventActionsWithSkipSummarization.toBuilder().build();
42+
43+
assertThat(eventActionsAfterRebuild).isEqualTo(eventActionsWithSkipSummarization);
44+
}
45+
46+
@Test
47+
public void merge_mergesAllFields() {
48+
EventActions eventActions1 =
49+
EventActions.builder()
50+
.skipSummarization(true)
51+
.stateDelta(new ConcurrentHashMap<>(ImmutableMap.of("key1", "value1")))
52+
.artifactDelta(new ConcurrentHashMap<>(ImmutableMap.of("artifact1", PART)))
53+
.requestedAuthConfigs(
54+
new ConcurrentHashMap<>(
55+
ImmutableMap.of("config1", new ConcurrentHashMap<>(ImmutableMap.of("k", "v")))))
56+
.requestedToolConfirmations(
57+
new ConcurrentHashMap<>(ImmutableMap.of("tool1", TOOL_CONFIRMATION)))
58+
.build();
59+
EventActions eventActions2 =
60+
EventActions.builder()
61+
.stateDelta(new ConcurrentHashMap<>(ImmutableMap.of("key2", "value2")))
62+
.artifactDelta(new ConcurrentHashMap<>(ImmutableMap.of("artifact2", PART)))
63+
.transferToAgent("agentId")
64+
.escalate(true)
65+
.requestedAuthConfigs(
66+
new ConcurrentHashMap<>(
67+
ImmutableMap.of("config2", new ConcurrentHashMap<>(ImmutableMap.of("k", "v")))))
68+
.requestedToolConfirmations(
69+
new ConcurrentHashMap<>(ImmutableMap.of("tool2", TOOL_CONFIRMATION)))
70+
.endInvocation(true)
71+
.build();
72+
73+
EventActions merged = eventActions1.toBuilder().merge(eventActions2).build();
74+
75+
assertThat(merged.skipSummarization()).hasValue(true);
76+
assertThat(merged.stateDelta()).containsExactly("key1", "value1", "key2", "value2");
77+
assertThat(merged.artifactDelta()).containsExactly("artifact1", PART, "artifact2", PART);
78+
assertThat(merged.transferToAgent()).hasValue("agentId");
79+
assertThat(merged.escalate()).hasValue(true);
80+
assertThat(merged.requestedAuthConfigs())
81+
.containsExactly(
82+
"config1",
83+
new ConcurrentHashMap<>(ImmutableMap.of("k", "v")),
84+
"config2",
85+
new ConcurrentHashMap<>(ImmutableMap.of("k", "v")));
86+
assertThat(merged.requestedToolConfirmations())
87+
.containsExactly("tool1", TOOL_CONFIRMATION, "tool2", TOOL_CONFIRMATION);
88+
assertThat(merged.endInvocation()).hasValue(true);
89+
}
90+
}

core/src/test/java/com/google/adk/events/EventTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 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+
117
package com.google.adk.events;
218

319
import static com.google.common.truth.Truth.assertThat;

core/src/test/java/com/google/adk/flows/llmflows/InstructionsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ public void processRequest_agentInstructionAndGlobalInstruction_bothAreAppendedT
257257
instructionsProcessor.processRequest(context, initialRequest).blockingGet();
258258

259259
assertThat(result.updatedRequest().getSystemInstructions())
260-
.containsExactly("Global instruction.\n\nAgent instruction.");
260+
.containsExactly("Global instruction.", "Agent instruction.")
261+
.inOrder();
261262
}
262263
}

core/src/test/java/com/google/adk/models/LlmRequestTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ public void appendInstructions_existingInstruction_appendsNewInstructionCorrectl
120120
assertThat(request.config()).isPresent();
121121
Content systemInstruction = request.config().get().systemInstruction().get();
122122
assertThat(systemInstruction.role()).hasValue("system");
123-
assertThat(systemInstruction.parts().get()).hasSize(1);
124-
assertThat(systemInstruction.parts().get().get(0).text())
125-
.hasValue(initialInstructionText + "\n\n" + newInstructionText);
123+
assertThat(systemInstruction.parts().get()).hasSize(2);
124+
assertThat(systemInstruction.parts().get().get(0).text()).hasValue(initialInstructionText);
125+
assertThat(systemInstruction.parts().get().get(1).text()).hasValue(newInstructionText);
126126

127127
assertThat(request.liveConnectConfig().systemInstruction()).isPresent();
128128
Content liveSystemInstruction = request.liveConnectConfig().systemInstruction().get();
@@ -154,9 +154,10 @@ public void appendInstructions_existingInstruction_appendsNewInstructionCorrectl
154154
assertThat(request.liveConnectConfig().systemInstruction()).isPresent();
155155
Content liveSystemInstruction = request.liveConnectConfig().systemInstruction().get();
156156
assertThat(liveSystemInstruction.role()).hasValue("system"); // Role preserved
157-
assertThat(liveSystemInstruction.parts().get()).hasSize(1);
157+
assertThat(liveSystemInstruction.parts().get()).hasSize(2);
158158
assertThat(liveSystemInstruction.parts().get().get(0).text())
159-
.hasValue(initialLiveInstructionText + "\n\n" + newInstructionText);
159+
.hasValue(initialLiveInstructionText);
160+
assertThat(liveSystemInstruction.parts().get().get(1).text()).hasValue(newInstructionText);
160161

161162
// Assertions for main config (should get the new instruction with default role)
162163
assertThat(request.config()).isPresent();
@@ -195,16 +196,16 @@ public void appendInstructions_multipleInstructions_appendsAllInOrder() {
195196

196197
assertThat(request.config()).isPresent();
197198
Content systemInstruction = request.config().get().systemInstruction().get();
198-
assertThat(systemInstruction.parts().get()).hasSize(1);
199-
assertThat(systemInstruction.parts().get().get(0).text())
200-
.hasValue(instruction1 + "\n\n" + instruction2);
199+
assertThat(systemInstruction.parts().get()).hasSize(2);
200+
assertThat(systemInstruction.parts().get().get(0).text()).hasValue(instruction1);
201+
assertThat(systemInstruction.parts().get().get(1).text()).hasValue(instruction2);
201202

202203
assertThat(request.liveConnectConfig().systemInstruction()).isPresent();
203204
Content liveSystemInstruction = request.liveConnectConfig().systemInstruction().get();
204205
assertThat(liveSystemInstruction.role()).hasValue("user");
205-
assertThat(liveSystemInstruction.parts().get()).hasSize(1);
206-
assertThat(liveSystemInstruction.parts().get().get(0).text())
207-
.hasValue(instruction1 + "\n\n" + instruction2);
206+
assertThat(liveSystemInstruction.parts().get()).hasSize(2);
207+
assertThat(liveSystemInstruction.parts().get().get(0).text()).hasValue(instruction1);
208+
assertThat(liveSystemInstruction.parts().get().get(1).text()).hasValue(instruction2);
208209
}
209210

210211
@Test
@@ -305,7 +306,7 @@ public void getSystemInstructions_whenPresent_returnsList() {
305306
.appendInstructions(ImmutableList.of(instruction1, instruction2))
306307
.build();
307308
assertThat(request.getSystemInstructions())
308-
.containsExactly(instruction1 + "\n\n" + instruction2)
309+
.containsExactly(instruction1, instruction2)
309310
.inOrder();
310311
}
311312
}

0 commit comments

Comments
 (0)