From 391c48a36e87e3a2a72978f4e391b21fb5954400 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Fri, 3 Apr 2026 05:26:38 -0700 Subject: [PATCH] chore: Update tests for ToolRequestConfirmationAction PiperOrigin-RevId: 894023229 --- .../ToolRequestConfirmationActionTest.java | 70 ++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/com/google/adk/flows/llmflows/ToolRequestConfirmationActionTest.java b/core/src/test/java/com/google/adk/flows/llmflows/ToolRequestConfirmationActionTest.java index 6e34095c2..25e55524e 100644 --- a/core/src/test/java/com/google/adk/flows/llmflows/ToolRequestConfirmationActionTest.java +++ b/core/src/test/java/com/google/adk/flows/llmflows/ToolRequestConfirmationActionTest.java @@ -34,6 +34,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.genai.types.Content; +import com.google.genai.types.FunctionCall; import com.google.genai.types.FunctionDeclaration; import com.google.genai.types.Part; import com.google.genai.types.Schema; @@ -72,8 +73,29 @@ public Single> runAsync(Map args, ToolContex } } + private static class NormalTool extends BaseTool { + NormalTool() { + super("normal_tool", "Normal tool."); + } + + @Override + public Optional declaration() { + return Optional.of( + FunctionDeclaration.builder() + .name(name()) + .description(description()) + .parameters(Schema.builder().type("OBJECT").build()) + .build()); + } + + @Override + public Single> runAsync(Map args, ToolContext toolContext) { + return Single.just(ImmutableMap.of("result", "success")); + } + } + @Test - public void toolRequestConfirmation_eventHasNonNullId() { + public void toolRequestConfirmation_generatesConfirmationEvent() { Content requestConfirmationCallContent = Content.fromParts(Part.fromFunctionCall("request_confirmation_tool", ImmutableMap.of())); Content response1 = Content.fromParts(Part.fromText("response1")); @@ -107,7 +129,51 @@ public void toolRequestConfirmation_eventHasNonNullId() { .collect(toImmutableList()); assertThat(confirmationEvents).isNotEmpty(); - assertThat(confirmationEvents.get(0).id()).isNotNull(); + + Event confirmationEvent = confirmationEvents.get(0); + assertThat(confirmationEvent.id()).isNotNull(); + + FunctionCall functionCall = confirmationEvent.functionCalls().get(0); + assertThat(functionCall.args().isPresent()).isTrue(); + + Map args = functionCall.args().get(); + assertThat(args).containsKey("toolConfirmation"); + assertThat(args).containsKey("originalFunctionCall"); + } + + @Test + public void normalTool_doesNotGenerateConfirmationEvent() { + Content normalCallContent = + Content.fromParts(Part.fromFunctionCall("normal_tool", ImmutableMap.of())); + Content response1 = Content.fromParts(Part.fromText("response1")); + + var testLlm = + createTestLlm( + Flowable.just(createLlmResponse(normalCallContent)), + Flowable.just(createLlmResponse(response1))); + + LlmAgent rootAgent = + createTestAgentBuilder(testLlm) + .name("root_agent") + .tools(ImmutableList.of(new NormalTool())) + .build(); + InvocationContext invocationContext = createInvocationContext(rootAgent); + + Runner runner = getRunnerAndCreateSession(rootAgent, invocationContext.session()); + + ImmutableList confirmationEvents = + runRunner(runner, invocationContext).stream() + .filter( + e -> + e.functionCalls().stream() + .anyMatch( + f -> + Objects.equals( + f.name().orElse(""), + Functions.REQUEST_CONFIRMATION_FUNCTION_CALL_NAME))) + .collect(toImmutableList()); + + assertThat(confirmationEvents).isEmpty(); } private Runner getRunnerAndCreateSession(LlmAgent agent, Session session) {