|
| 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.HashMap; |
| 10 | +import java.util.concurrent.CompletableFuture; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.AfterAll; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import com.github.copilot.sdk.json.AutoModeSwitchRequest; |
| 18 | +import com.github.copilot.sdk.json.AutoModeSwitchResponse; |
| 19 | +import com.github.copilot.sdk.json.CopilotClientOptions; |
| 20 | +import com.github.copilot.sdk.json.ExitPlanModeRequest; |
| 21 | +import com.github.copilot.sdk.json.ExitPlanModeResult; |
| 22 | +import com.github.copilot.sdk.json.MessageOptions; |
| 23 | +import com.github.copilot.sdk.json.PermissionHandler; |
| 24 | +import com.github.copilot.sdk.json.SessionConfig; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests for mode handler (exit-plan-mode and auto-mode-switch) functionality. |
| 28 | + * |
| 29 | + * <p> |
| 30 | + * These tests use the shared CapiProxy infrastructure for deterministic API |
| 31 | + * response replay. Snapshots are stored in test/snapshots/mode_handlers/. |
| 32 | + * </p> |
| 33 | + */ |
| 34 | +public class ModeHandlersTest { |
| 35 | + |
| 36 | + private static final String TOKEN = "mode-handler-token"; |
| 37 | + |
| 38 | + private static E2ETestContext ctx; |
| 39 | + |
| 40 | + @BeforeAll |
| 41 | + static void setup() throws Exception { |
| 42 | + ctx = E2ETestContext.create(); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterAll |
| 46 | + static void teardown() throws Exception { |
| 47 | + if (ctx != null) { |
| 48 | + ctx.close(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Verifies that exit-plan-mode handler is invoked when model uses |
| 54 | + * exit_plan_mode tool. |
| 55 | + * |
| 56 | + * @see Snapshot: |
| 57 | + * mode_handlers/should_invoke_exit_plan_mode_handler_when_model_uses_tool |
| 58 | + */ |
| 59 | + @Test |
| 60 | + void testShouldInvokeExitPlanModeHandlerWhenModelUsesTool() throws Exception { |
| 61 | + ctx.configureForTest("mode_handlers", "should_invoke_exit_plan_mode_handler_when_model_uses_tool"); |
| 62 | + configureAuthenticatedUser(); |
| 63 | + |
| 64 | + final var exitPlanModeRequests = new java.util.ArrayList<ExitPlanModeRequest>(); |
| 65 | + final String[] sessionIdHolder = new String[1]; |
| 66 | + |
| 67 | + var config = new SessionConfig().setGitHubToken(TOKEN).setOnPermissionRequest(PermissionHandler.APPROVE_ALL) |
| 68 | + .setOnExitPlanMode((request, invocation) -> { |
| 69 | + exitPlanModeRequests.add(request); |
| 70 | + assertEquals(sessionIdHolder[0], invocation.getSessionId()); |
| 71 | + return CompletableFuture.completedFuture(new ExitPlanModeResult().setApproved(true) |
| 72 | + .setSelectedAction("interactive").setFeedback("Approved by the Java E2E test")); |
| 73 | + }); |
| 74 | + |
| 75 | + try (CopilotClient client = createAuthenticatedClient()) { |
| 76 | + CopilotSession session = client.createSession(config).get(); |
| 77 | + sessionIdHolder[0] = session.getSessionId(); |
| 78 | + |
| 79 | + var response = session.sendAndWait(new MessageOptions().setMode("plan") |
| 80 | + .setPrompt("Create a brief implementation plan for adding a greeting.txt file, " |
| 81 | + + "then request approval with exit_plan_mode.")) |
| 82 | + .get(120, TimeUnit.SECONDS); |
| 83 | + |
| 84 | + assertNotNull(response, "Response should not be null"); |
| 85 | + assertFalse(exitPlanModeRequests.isEmpty(), "Should have received exit plan mode requests"); |
| 86 | + |
| 87 | + ExitPlanModeRequest request = exitPlanModeRequests.get(0); |
| 88 | + assertEquals("Greeting file implementation plan", request.getSummary()); |
| 89 | + assertNotNull(request.getActions()); |
| 90 | + assertNotNull(request.getRecommendedAction()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Verifies that auto-mode-switch handler is invoked when rate limited. |
| 96 | + * |
| 97 | + * @see Snapshot: |
| 98 | + * mode_handlers/should_invoke_auto_mode_switch_handler_when_rate_limited |
| 99 | + */ |
| 100 | + @Test |
| 101 | + void testShouldInvokeAutoModeSwitchHandlerWhenRateLimited() throws Exception { |
| 102 | + ctx.configureForTest("mode_handlers", "should_invoke_auto_mode_switch_handler_when_rate_limited"); |
| 103 | + configureAuthenticatedUser(); |
| 104 | + |
| 105 | + final var autoModeSwitchRequests = new java.util.ArrayList<AutoModeSwitchRequest>(); |
| 106 | + final String[] sessionIdHolder = new String[1]; |
| 107 | + |
| 108 | + var config = new SessionConfig().setGitHubToken(TOKEN).setOnPermissionRequest(PermissionHandler.APPROVE_ALL) |
| 109 | + .setOnAutoModeSwitch((request, invocation) -> { |
| 110 | + autoModeSwitchRequests.add(request); |
| 111 | + assertEquals(sessionIdHolder[0], invocation.getSessionId()); |
| 112 | + return CompletableFuture.completedFuture(AutoModeSwitchResponse.YES); |
| 113 | + }); |
| 114 | + |
| 115 | + try (CopilotClient client = createAuthenticatedClient()) { |
| 116 | + CopilotSession session = client.createSession(config).get(); |
| 117 | + sessionIdHolder[0] = session.getSessionId(); |
| 118 | + |
| 119 | + String messageId = session |
| 120 | + .send(new MessageOptions() |
| 121 | + .setPrompt("Explain that auto mode recovered from a rate limit in one short sentence.")) |
| 122 | + .get(30, TimeUnit.SECONDS); |
| 123 | + assertNotNull(messageId, "Message ID should not be null"); |
| 124 | + |
| 125 | + // Wait for the auto mode switch handler to be invoked |
| 126 | + for (int i = 0; i < 30 && autoModeSwitchRequests.isEmpty(); i++) { |
| 127 | + Thread.sleep(1000); |
| 128 | + } |
| 129 | + |
| 130 | + assertFalse(autoModeSwitchRequests.isEmpty(), "Should have received auto mode switch requests"); |
| 131 | + |
| 132 | + AutoModeSwitchRequest request = autoModeSwitchRequests.get(0); |
| 133 | + assertEquals("user_weekly_rate_limited", request.getErrorCode()); |
| 134 | + assertEquals(1.0, request.getRetryAfterSeconds()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private CopilotClient createAuthenticatedClient() { |
| 139 | + var env = new HashMap<>(ctx.getEnvironment()); |
| 140 | + env.put("COPILOT_DEBUG_GITHUB_API_URL", ctx.getProxyUrl()); |
| 141 | + |
| 142 | + var options = new CopilotClientOptions().setEnvironment(env); |
| 143 | + return ctx.createClient(options); |
| 144 | + } |
| 145 | + |
| 146 | + private void configureAuthenticatedUser() throws Exception { |
| 147 | + ctx.setCopilotUserByToken(TOKEN, "mode-handler-user", "individual_pro", ctx.getProxyUrl(), |
| 148 | + "https://localhost:1/telemetry", "mode-handler-tracking-id"); |
| 149 | + } |
| 150 | +} |
0 commit comments