forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandsTest.java
More file actions
157 lines (123 loc) · 6.14 KB
/
CommandsTest.java
File metadata and controls
157 lines (123 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.Test;
import com.github.copilot.sdk.json.CommandContext;
import com.github.copilot.sdk.json.CommandDefinition;
import com.github.copilot.sdk.json.CommandHandler;
import com.github.copilot.sdk.json.CommandWireDefinition;
import com.github.copilot.sdk.json.PermissionHandler;
import com.github.copilot.sdk.json.ResumeSessionConfig;
import com.github.copilot.sdk.json.SessionConfig;
/**
* Unit tests for the Commands feature (CommandDefinition, CommandContext,
* SessionConfig.commands, ResumeSessionConfig.commands, and the wire
* representation).
*
* <p>
* Ported from {@code CommandsTests.cs} in the reference implementation dotnet
* SDK.
* </p>
*/
class CommandsTest {
@Test
void commandDefinitionHasRequiredProperties() {
CommandHandler handler = context -> CompletableFuture.completedFuture(null);
var cmd = new CommandDefinition().setName("deploy").setDescription("Deploy the app").setHandler(handler);
assertEquals("deploy", cmd.getName());
assertEquals("Deploy the app", cmd.getDescription());
assertNotNull(cmd.getHandler());
}
@Test
void commandContextHasAllProperties() {
var ctx = new CommandContext().setSessionId("session-1").setCommand("/deploy production")
.setCommandName("deploy").setArgs("production");
assertEquals("session-1", ctx.getSessionId());
assertEquals("/deploy production", ctx.getCommand());
assertEquals("deploy", ctx.getCommandName());
assertEquals("production", ctx.getArgs());
}
@Test
void sessionConfigCommandsAreCloned() {
CommandHandler handler = ctx -> CompletableFuture.completedFuture(null);
var config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setCommands(List.of(new CommandDefinition().setName("deploy").setHandler(handler)));
var clone = config.clone();
assertNotNull(clone.getCommands());
assertEquals(1, clone.getCommands().size());
assertEquals("deploy", clone.getCommands().get(0).getName());
// Collections should be independent — clone list is a copy
assertNotSame(config.getCommands(), clone.getCommands());
}
@Test
void resumeConfigCommandsAreCloned() {
CommandHandler handler = ctx -> CompletableFuture.completedFuture(null);
var config = new ResumeSessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setCommands(List.of(new CommandDefinition().setName("deploy").setHandler(handler)));
var clone = config.clone();
assertNotNull(clone.getCommands());
assertEquals(1, clone.getCommands().size());
assertEquals("deploy", clone.getCommands().get(0).getName());
}
@Test
void buildCreateRequestIncludesCommandWireDefinitions() {
CommandHandler handler = ctx -> CompletableFuture.completedFuture(null);
var config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setCommands(
List.of(new CommandDefinition().setName("deploy").setDescription("Deploy").setHandler(handler),
new CommandDefinition().setName("rollback").setHandler(handler)));
var request = SessionRequestBuilder.buildCreateRequest(config);
assertNotNull(request.getCommands());
assertEquals(2, request.getCommands().size());
assertEquals("deploy", request.getCommands().get(0).getName());
assertEquals("Deploy", request.getCommands().get(0).getDescription());
assertEquals("rollback", request.getCommands().get(1).getName());
assertNull(request.getCommands().get(1).getDescription());
}
@Test
void buildResumeRequestIncludesCommandWireDefinitions() {
CommandHandler handler = ctx -> CompletableFuture.completedFuture(null);
var config = new ResumeSessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL).setCommands(
List.of(new CommandDefinition().setName("deploy").setDescription("Deploy").setHandler(handler)));
var request = SessionRequestBuilder.buildResumeRequest("session-1", config);
assertNotNull(request.getCommands());
assertEquals(1, request.getCommands().size());
assertEquals("deploy", request.getCommands().get(0).getName());
assertEquals("Deploy", request.getCommands().get(0).getDescription());
}
@Test
void buildCreateRequestWithNoCommandsHasNullCommandsList() {
var config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL);
var request = SessionRequestBuilder.buildCreateRequest(config);
assertNull(request.getCommands());
}
@Test
void commandWireDefinitionHasNameAndDescription() {
var wire = new CommandWireDefinition("deploy", "Deploy the app");
assertEquals("deploy", wire.getName());
assertEquals("Deploy the app", wire.getDescription());
}
@Test
void commandWireDefinitionNullDescriptionAllowed() {
var wire = new CommandWireDefinition("rollback", null);
assertEquals("rollback", wire.getName());
assertNull(wire.getDescription());
}
@Test
void commandWireDefinitionFluentSetters() {
var wire = new CommandWireDefinition();
wire.setName("status");
wire.setDescription("Show deployment status");
assertEquals("status", wire.getName());
assertEquals("Show deployment status", wire.getDescription());
}
@Test
void commandWireDefinitionFluentSettersChaining() {
var wire = new CommandWireDefinition().setName("logs").setDescription("View application logs");
assertEquals("logs", wire.getName());
assertEquals("View application logs", wire.getDescription());
}
}