|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +package com.github.copilot.sdk; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.*; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.CompletableFuture; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | + |
| 14 | +import org.junit.jupiter.api.AfterAll; |
| 15 | +import org.junit.jupiter.api.BeforeAll; |
| 16 | +import org.junit.jupiter.api.Disabled; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import com.github.copilot.sdk.json.MessageOptions; |
| 20 | +import com.github.copilot.sdk.json.SessionConfig; |
| 21 | +import com.github.copilot.sdk.json.UserInputRequest; |
| 22 | +import com.github.copilot.sdk.json.UserInputResponse; |
| 23 | + |
| 24 | +/** |
| 25 | + * Tests for user input handler (ask_user) functionality. |
| 26 | + * |
| 27 | + * <p> |
| 28 | + * These tests use the shared CapiProxy infrastructure for deterministic API |
| 29 | + * response replay. Snapshots are stored in test/snapshots/ask-user/. |
| 30 | + * </p> |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * <b>Note:</b> These tests require the latest test harness with ask_user |
| 34 | + * support. |
| 35 | + * </p> |
| 36 | + */ |
| 37 | +@Disabled("Requires test harness update with ask_user support - see upstream PR #269") |
| 38 | +public class AskUserTest { |
| 39 | + |
| 40 | + private static E2ETestContext ctx; |
| 41 | + |
| 42 | + @BeforeAll |
| 43 | + static void setup() throws Exception { |
| 44 | + ctx = E2ETestContext.create(); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterAll |
| 48 | + static void teardown() throws Exception { |
| 49 | + if (ctx != null) { |
| 50 | + ctx.close(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testUserInputHandlerInvokedWhenModelUsesAskUserTool() throws Exception { |
| 56 | + ctx.configureForTest("ask-user", "should_invoke_user_input_handler_when_model_uses_ask_user_tool"); |
| 57 | + |
| 58 | + List<UserInputRequest> userInputRequests = new ArrayList<>(); |
| 59 | + final String[] sessionIdHolder = new String[1]; |
| 60 | + |
| 61 | + SessionConfig config = new SessionConfig().setOnUserInputRequest((request, invocation) -> { |
| 62 | + userInputRequests.add(request); |
| 63 | + assertEquals(sessionIdHolder[0], invocation.getSessionId()); |
| 64 | + |
| 65 | + // Return the first choice if available, otherwise a freeform answer |
| 66 | + String answer = (request.getChoices() != null && !request.getChoices().isEmpty()) |
| 67 | + ? request.getChoices().get(0) |
| 68 | + : "freeform answer"; |
| 69 | + boolean wasFreeform = request.getChoices() == null || request.getChoices().isEmpty(); |
| 70 | + |
| 71 | + return CompletableFuture |
| 72 | + .completedFuture(new UserInputResponse().setAnswer(answer).setWasFreeform(wasFreeform)); |
| 73 | + }); |
| 74 | + |
| 75 | + try (CopilotClient client = ctx.createClient()) { |
| 76 | + CopilotSession session = client.createSession(config).get(); |
| 77 | + sessionIdHolder[0] = session.getSessionId(); |
| 78 | + |
| 79 | + session.sendAndWait(new MessageOptions().setPrompt( |
| 80 | + "Ask me to choose between 'Option A' and 'Option B' using the ask_user tool. Wait for my response before continuing.")) |
| 81 | + .get(60, TimeUnit.SECONDS); |
| 82 | + |
| 83 | + // Should have received at least one user input request |
| 84 | + assertFalse(userInputRequests.isEmpty(), "Should have received user input requests"); |
| 85 | + |
| 86 | + // The request should have a question |
| 87 | + assertTrue(userInputRequests.stream().anyMatch(r -> r.getQuestion() != null && !r.getQuestion().isEmpty()), |
| 88 | + "User input request should have a question"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testUserInputRequestWithChoices() throws Exception { |
| 94 | + ctx.configureForTest("ask-user", "should_receive_choices_in_user_input_request"); |
| 95 | + |
| 96 | + List<UserInputRequest> userInputRequests = new ArrayList<>(); |
| 97 | + |
| 98 | + SessionConfig config = new SessionConfig().setOnUserInputRequest((request, invocation) -> { |
| 99 | + userInputRequests.add(request); |
| 100 | + |
| 101 | + // Pick the first choice |
| 102 | + String answer = (request.getChoices() != null && !request.getChoices().isEmpty()) |
| 103 | + ? request.getChoices().get(0) |
| 104 | + : "default"; |
| 105 | + |
| 106 | + return CompletableFuture.completedFuture(new UserInputResponse().setAnswer(answer).setWasFreeform(false)); |
| 107 | + }); |
| 108 | + |
| 109 | + try (CopilotClient client = ctx.createClient()) { |
| 110 | + CopilotSession session = client.createSession(config).get(); |
| 111 | + |
| 112 | + session.sendAndWait(new MessageOptions().setPrompt( |
| 113 | + "Use the ask_user tool to ask me to pick between exactly two options: 'Red' and 'Blue'. These should be provided as choices. Wait for my answer.")) |
| 114 | + .get(60, TimeUnit.SECONDS); |
| 115 | + |
| 116 | + // Should have received a request |
| 117 | + assertFalse(userInputRequests.isEmpty(), "Should have received user input requests"); |
| 118 | + |
| 119 | + // At least one request should have choices |
| 120 | + assertTrue(userInputRequests.stream().anyMatch(r -> r.getChoices() != null && !r.getChoices().isEmpty()), |
| 121 | + "At least one request should have choices"); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testFreeformUserInputResponse() throws Exception { |
| 127 | + ctx.configureForTest("ask-user", "should_handle_freeform_user_input_response"); |
| 128 | + |
| 129 | + List<UserInputRequest> userInputRequests = new ArrayList<>(); |
| 130 | + String freeformAnswer = "This is my custom freeform answer that was not in the choices"; |
| 131 | + |
| 132 | + SessionConfig config = new SessionConfig().setOnUserInputRequest((request, invocation) -> { |
| 133 | + userInputRequests.add(request); |
| 134 | + |
| 135 | + // Return a freeform answer (not from choices) |
| 136 | + return CompletableFuture |
| 137 | + .completedFuture(new UserInputResponse().setAnswer(freeformAnswer).setWasFreeform(true)); |
| 138 | + }); |
| 139 | + |
| 140 | + try (CopilotClient client = ctx.createClient()) { |
| 141 | + CopilotSession session = client.createSession(config).get(); |
| 142 | + |
| 143 | + var response = session.sendAndWait(new MessageOptions().setPrompt( |
| 144 | + "Ask me a question using ask_user and then include my answer in your response. The question should be 'What is your favorite color?'")) |
| 145 | + .get(60, TimeUnit.SECONDS); |
| 146 | + |
| 147 | + // Should have received a request |
| 148 | + assertFalse(userInputRequests.isEmpty(), "Should have received user input requests"); |
| 149 | + |
| 150 | + // The model's response should be defined |
| 151 | + assertNotNull(response, "Response should not be null"); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments