|
| 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.Map; |
| 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.Test; |
| 17 | + |
| 18 | +import com.github.copilot.sdk.generated.ExitPlanModeCompletedEvent; |
| 19 | +import com.github.copilot.sdk.generated.ExitPlanModeRequestedEvent; |
| 20 | +import com.github.copilot.sdk.json.AutoModeSwitchRequest; |
| 21 | +import com.github.copilot.sdk.json.AutoModeSwitchResponse; |
| 22 | +import com.github.copilot.sdk.json.CopilotClientOptions; |
| 23 | +import com.github.copilot.sdk.json.ExitPlanModeRequest; |
| 24 | +import com.github.copilot.sdk.json.ExitPlanModeResult; |
| 25 | +import com.github.copilot.sdk.json.MessageOptions; |
| 26 | +import com.github.copilot.sdk.json.PermissionHandler; |
| 27 | +import com.github.copilot.sdk.json.SessionConfig; |
| 28 | + |
| 29 | +/** |
| 30 | + * E2E tests for exit-plan-mode and auto-mode-switch handler APIs. |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * Ported from {@code ModeHandlersE2ETests.cs} in the reference implementation |
| 34 | + * dotnet SDK. |
| 35 | + * </p> |
| 36 | + */ |
| 37 | +public class ModeHandlersTest { |
| 38 | + |
| 39 | + private static final String TOKEN = "mode-handler-token"; |
| 40 | + |
| 41 | + private static E2ETestContext ctx; |
| 42 | + |
| 43 | + @BeforeAll |
| 44 | + static void setup() throws Exception { |
| 45 | + ctx = E2ETestContext.create(); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterAll |
| 49 | + static void teardown() throws Exception { |
| 50 | + if (ctx != null) { |
| 51 | + ctx.close(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private CopilotClient createAuthenticatedClient() { |
| 56 | + Map<String, String> env = new HashMap<>(ctx.getEnvironment()); |
| 57 | + env.put("COPILOT_DEBUG_GITHUB_API_URL", ctx.getProxyUrl()); |
| 58 | + |
| 59 | + return ctx.createClient(new CopilotClientOptions().setEnvironment(env)); |
| 60 | + } |
| 61 | + |
| 62 | + private void configureAuthenticatedUser() throws Exception { |
| 63 | + ctx.setCopilotUserByToken(TOKEN, "mode-handler-user", "individual_pro", ctx.getProxyUrl(), |
| 64 | + "https://localhost:1/telemetry", "mode-handler-tracking-id"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void shouldInvokeExitPlanModeHandlerWhenModelUsesTool() throws Exception { |
| 69 | + final String summary = "Greeting file implementation plan"; |
| 70 | + configureAuthenticatedUser(); |
| 71 | + |
| 72 | + var handlerCalled = new CompletableFuture<ExitPlanModeRequest>(); |
| 73 | + |
| 74 | + try (var client = createAuthenticatedClient()) { |
| 75 | + var session = client.createSession(new SessionConfig().setGitHubToken(TOKEN) |
| 76 | + .setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setOnExitPlanMode((request, invocation) -> { |
| 77 | + handlerCalled.complete(request); |
| 78 | + return CompletableFuture.completedFuture(new ExitPlanModeResult().setApproved(true) |
| 79 | + .setSelectedAction("interactive").setFeedback("Approved by the Java E2E test")); |
| 80 | + })).get(30, TimeUnit.SECONDS); |
| 81 | + |
| 82 | + var requestedEvent = new CompletableFuture<ExitPlanModeRequestedEvent>(); |
| 83 | + var completedEvent = new CompletableFuture<ExitPlanModeCompletedEvent>(); |
| 84 | + |
| 85 | + session.on(event -> { |
| 86 | + if (event instanceof ExitPlanModeRequestedEvent requested |
| 87 | + && summary.equals(requested.getData().summary())) { |
| 88 | + requestedEvent.complete(requested); |
| 89 | + } else if (event instanceof ExitPlanModeCompletedEvent completed |
| 90 | + && Boolean.TRUE.equals(completed.getData().approved()) |
| 91 | + && "interactive".equals(completed.getData().selectedAction())) { |
| 92 | + completedEvent.complete(completed); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + var response = session.sendAndWait(new MessageOptions().setPrompt( |
| 97 | + "Create a brief implementation plan for adding a greeting.txt file, then request approval with exit_plan_mode.") |
| 98 | + .setMode("plan")).get(120, TimeUnit.SECONDS); |
| 99 | + |
| 100 | + var request = handlerCalled.get(10, TimeUnit.SECONDS); |
| 101 | + assertEquals(summary, request.getSummary()); |
| 102 | + assertNotNull(request.getActions()); |
| 103 | + assertTrue(request.getActions().contains("interactive")); |
| 104 | + assertNotNull(request.getPlanContent()); |
| 105 | + |
| 106 | + var reqEvent = requestedEvent.get(10, TimeUnit.SECONDS); |
| 107 | + assertEquals(request.getSummary(), reqEvent.getData().summary()); |
| 108 | + |
| 109 | + var compEvent = completedEvent.get(10, TimeUnit.SECONDS); |
| 110 | + assertTrue(compEvent.getData().approved()); |
| 111 | + assertEquals("interactive", compEvent.getData().selectedAction()); |
| 112 | + |
| 113 | + assertNotNull(response); |
| 114 | + |
| 115 | + session.close(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void shouldInvokeAutoModeSwitchHandlerWhenRateLimited() throws Exception { |
| 121 | + configureAuthenticatedUser(); |
| 122 | + |
| 123 | + var handlerCalled = new CompletableFuture<AutoModeSwitchRequest>(); |
| 124 | + |
| 125 | + try (var client = createAuthenticatedClient()) { |
| 126 | + var session = client.createSession( |
| 127 | + new SessionConfig().setGitHubToken(TOKEN).setOnPermissionRequest(PermissionHandler.APPROVE_ALL) |
| 128 | + .setOnAutoModeSwitch((request, invocation) -> { |
| 129 | + handlerCalled.complete(request); |
| 130 | + return CompletableFuture.completedFuture(AutoModeSwitchResponse.YES); |
| 131 | + })) |
| 132 | + .get(30, TimeUnit.SECONDS); |
| 133 | + |
| 134 | + session.sendAndWait(new MessageOptions() |
| 135 | + .setPrompt("Explain that auto mode recovered from a rate limit in one short sentence.")) |
| 136 | + .get(30, TimeUnit.SECONDS); |
| 137 | + |
| 138 | + var request = handlerCalled.get(30, TimeUnit.SECONDS); |
| 139 | + assertEquals("user_weekly_rate_limited", request.getErrorCode()); |
| 140 | + assertEquals(1.0, request.getRetryAfterSeconds()); |
| 141 | + |
| 142 | + session.close(); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments