|
| 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.List; |
| 10 | +import java.util.concurrent.CompletableFuture; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import com.github.copilot.sdk.json.CommandContext; |
| 15 | +import com.github.copilot.sdk.json.CommandDefinition; |
| 16 | +import com.github.copilot.sdk.json.CommandHandler; |
| 17 | +import com.github.copilot.sdk.json.CommandWireDefinition; |
| 18 | +import com.github.copilot.sdk.json.PermissionHandler; |
| 19 | +import com.github.copilot.sdk.json.ResumeSessionConfig; |
| 20 | +import com.github.copilot.sdk.json.SessionConfig; |
| 21 | + |
| 22 | +/** |
| 23 | + * Unit tests for the Commands feature (CommandDefinition, CommandContext, |
| 24 | + * SessionConfig.commands, ResumeSessionConfig.commands, and the wire |
| 25 | + * representation). |
| 26 | + * |
| 27 | + * <p> |
| 28 | + * Ported from {@code CommandsTests.cs} in the upstream dotnet SDK. |
| 29 | + * </p> |
| 30 | + */ |
| 31 | +class CommandsTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + void commandDefinitionHasRequiredProperties() { |
| 35 | + CommandHandler handler = context -> CompletableFuture.completedFuture(null); |
| 36 | + var cmd = new CommandDefinition().setName("deploy").setDescription("Deploy the app").setHandler(handler); |
| 37 | + |
| 38 | + assertEquals("deploy", cmd.getName()); |
| 39 | + assertEquals("Deploy the app", cmd.getDescription()); |
| 40 | + assertNotNull(cmd.getHandler()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void commandContextHasAllProperties() { |
| 45 | + var ctx = new CommandContext().setSessionId("session-1").setCommand("/deploy production") |
| 46 | + .setCommandName("deploy").setArgs("production"); |
| 47 | + |
| 48 | + assertEquals("session-1", ctx.getSessionId()); |
| 49 | + assertEquals("/deploy production", ctx.getCommand()); |
| 50 | + assertEquals("deploy", ctx.getCommandName()); |
| 51 | + assertEquals("production", ctx.getArgs()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void sessionConfigCommandsAreCloned() { |
| 56 | + CommandHandler handler = ctx -> CompletableFuture.completedFuture(null); |
| 57 | + var config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL) |
| 58 | + .setCommands(List.of(new CommandDefinition().setName("deploy").setHandler(handler))); |
| 59 | + |
| 60 | + var clone = config.clone(); |
| 61 | + |
| 62 | + assertNotNull(clone.getCommands()); |
| 63 | + assertEquals(1, clone.getCommands().size()); |
| 64 | + assertEquals("deploy", clone.getCommands().get(0).getName()); |
| 65 | + |
| 66 | + // Collections should be independent — clone list is a copy |
| 67 | + assertNotSame(config.getCommands(), clone.getCommands()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void resumeConfigCommandsAreCloned() { |
| 72 | + CommandHandler handler = ctx -> CompletableFuture.completedFuture(null); |
| 73 | + var config = new ResumeSessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL) |
| 74 | + .setCommands(List.of(new CommandDefinition().setName("deploy").setHandler(handler))); |
| 75 | + |
| 76 | + var clone = config.clone(); |
| 77 | + |
| 78 | + assertNotNull(clone.getCommands()); |
| 79 | + assertEquals(1, clone.getCommands().size()); |
| 80 | + assertEquals("deploy", clone.getCommands().get(0).getName()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void buildCreateRequestIncludesCommandWireDefinitions() { |
| 85 | + CommandHandler handler = ctx -> CompletableFuture.completedFuture(null); |
| 86 | + var config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setCommands( |
| 87 | + List.of(new CommandDefinition().setName("deploy").setDescription("Deploy").setHandler(handler), |
| 88 | + new CommandDefinition().setName("rollback").setHandler(handler))); |
| 89 | + |
| 90 | + var request = SessionRequestBuilder.buildCreateRequest(config); |
| 91 | + |
| 92 | + assertNotNull(request.getCommands()); |
| 93 | + assertEquals(2, request.getCommands().size()); |
| 94 | + assertEquals("deploy", request.getCommands().get(0).getName()); |
| 95 | + assertEquals("Deploy", request.getCommands().get(0).getDescription()); |
| 96 | + assertEquals("rollback", request.getCommands().get(1).getName()); |
| 97 | + assertNull(request.getCommands().get(1).getDescription()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void buildResumeRequestIncludesCommandWireDefinitions() { |
| 102 | + CommandHandler handler = ctx -> CompletableFuture.completedFuture(null); |
| 103 | + var config = new ResumeSessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setCommands( |
| 104 | + List.of(new CommandDefinition().setName("deploy").setDescription("Deploy").setHandler(handler))); |
| 105 | + |
| 106 | + var request = SessionRequestBuilder.buildResumeRequest("session-1", config); |
| 107 | + |
| 108 | + assertNotNull(request.getCommands()); |
| 109 | + assertEquals(1, request.getCommands().size()); |
| 110 | + assertEquals("deploy", request.getCommands().get(0).getName()); |
| 111 | + assertEquals("Deploy", request.getCommands().get(0).getDescription()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void buildCreateRequestWithNoCommandsHasNullCommandsList() { |
| 116 | + var config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL); |
| 117 | + |
| 118 | + var request = SessionRequestBuilder.buildCreateRequest(config); |
| 119 | + |
| 120 | + assertNull(request.getCommands()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void commandWireDefinitionHasNameAndDescription() { |
| 125 | + var wire = new CommandWireDefinition("deploy", "Deploy the app"); |
| 126 | + |
| 127 | + assertEquals("deploy", wire.getName()); |
| 128 | + assertEquals("Deploy the app", wire.getDescription()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void commandWireDefinitionNullDescriptionAllowed() { |
| 133 | + var wire = new CommandWireDefinition("rollback", null); |
| 134 | + |
| 135 | + assertEquals("rollback", wire.getName()); |
| 136 | + assertNull(wire.getDescription()); |
| 137 | + } |
| 138 | +} |
0 commit comments