Skip to content

Commit d4eb347

Browse files
committed
test: add unit tests for SessionRequestBuilder to improve branch coverage
1 parent b8b7a7d commit d4eb347

1 file changed

Lines changed: 194 additions & 0 deletions

File tree

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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.Map;
11+
import java.util.concurrent.CompletableFuture;
12+
13+
import org.junit.jupiter.api.Test;
14+
15+
import com.github.copilot.sdk.json.CreateSessionRequest;
16+
import com.github.copilot.sdk.json.ResumeSessionConfig;
17+
import com.github.copilot.sdk.json.ResumeSessionRequest;
18+
import com.github.copilot.sdk.json.SessionConfig;
19+
import com.github.copilot.sdk.json.SessionHooks;
20+
import com.github.copilot.sdk.json.ToolDefinition;
21+
import com.github.copilot.sdk.json.UserInputResponse;
22+
23+
/**
24+
* Unit tests for {@link SessionRequestBuilder} branch coverage.
25+
* <p>
26+
* Exercises branches in buildCreateRequest, buildResumeRequest, and
27+
* configureSession that are not reached by E2E tests.
28+
*/
29+
public class SessionRequestBuilderTest {
30+
31+
// =========================================================================
32+
// buildCreateRequest
33+
// =========================================================================
34+
35+
@Test
36+
void testBuildCreateRequestNullConfig() {
37+
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(null);
38+
assertNotNull(request);
39+
assertNull(request.getModel());
40+
}
41+
42+
@Test
43+
void testBuildCreateRequestHooksNonNullButEmpty() {
44+
// Hooks object exists but hasHooks() returns false
45+
var config = new SessionConfig().setHooks(new SessionHooks());
46+
47+
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
48+
49+
assertNull(request.getHooks(), "Should be null when hooks are empty");
50+
}
51+
52+
@Test
53+
void testBuildCreateRequestHooksWithHandler() {
54+
var hooks = new SessionHooks().setOnPreToolUse((input, inv) -> CompletableFuture.completedFuture(null));
55+
var config = new SessionConfig().setHooks(hooks);
56+
57+
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
58+
59+
assertTrue(request.getHooks(), "Should be true when hooks have handlers");
60+
}
61+
62+
// =========================================================================
63+
// buildResumeRequest
64+
// =========================================================================
65+
66+
@Test
67+
void testBuildResumeRequestNullConfig() {
68+
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-1", null);
69+
assertEquals("sid-1", request.getSessionId());
70+
assertNull(request.getModel());
71+
}
72+
73+
@Test
74+
void testBuildResumeRequestWithTools() {
75+
var tool = ToolDefinition.create("my_tool", "A tool", Map.of("type", "object"),
76+
inv -> CompletableFuture.completedFuture("result"));
77+
var config = new ResumeSessionConfig().setTools(List.of(tool));
78+
79+
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-2", config);
80+
81+
assertNotNull(request.getTools());
82+
assertEquals(1, request.getTools().size());
83+
assertEquals("my_tool", request.getTools().get(0).getName());
84+
}
85+
86+
@Test
87+
void testBuildResumeRequestWithUserInputHandler() {
88+
var config = new ResumeSessionConfig()
89+
.setOnUserInputRequest((req, inv) -> CompletableFuture.completedFuture(new UserInputResponse()));
90+
91+
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-3", config);
92+
93+
assertTrue(request.getRequestUserInput());
94+
}
95+
96+
@Test
97+
void testBuildResumeRequestHooksNonNullButEmpty() {
98+
var config = new ResumeSessionConfig().setHooks(new SessionHooks());
99+
100+
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-4", config);
101+
102+
assertNull(request.getHooks(), "Should be null when hooks are empty");
103+
}
104+
105+
@Test
106+
void testBuildResumeRequestHooksWithHandler() {
107+
var hooks = new SessionHooks().setOnSessionEnd((input, inv) -> CompletableFuture.completedFuture(null));
108+
var config = new ResumeSessionConfig().setHooks(hooks);
109+
110+
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-5", config);
111+
112+
assertTrue(request.getHooks(), "Should be true when hooks have handlers");
113+
}
114+
115+
@Test
116+
void testBuildResumeRequestDisableResume() {
117+
var config = new ResumeSessionConfig().setDisableResume(true);
118+
119+
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-6", config);
120+
121+
assertTrue(request.getDisableResume());
122+
}
123+
124+
@Test
125+
void testBuildResumeRequestStreaming() {
126+
var config = new ResumeSessionConfig().setStreaming(true);
127+
128+
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-7", config);
129+
130+
assertTrue(request.getStreaming());
131+
}
132+
133+
// =========================================================================
134+
// configureSession (ResumeSessionConfig overload)
135+
// =========================================================================
136+
137+
@Test
138+
void testConfigureResumeSessionNullConfig() throws Exception {
139+
var session = createTestSession();
140+
// Should not throw
141+
SessionRequestBuilder.configureSession(session, (ResumeSessionConfig) null);
142+
}
143+
144+
@Test
145+
void testConfigureResumeSessionWithTools() throws Exception {
146+
var session = createTestSession();
147+
var tool = ToolDefinition.create("resume_tool", "desc", Map.of(),
148+
inv -> CompletableFuture.completedFuture("ok"));
149+
var config = new ResumeSessionConfig().setTools(List.of(tool));
150+
151+
SessionRequestBuilder.configureSession(session, config);
152+
153+
assertNotNull(session.getTool("resume_tool"));
154+
}
155+
156+
@Test
157+
void testConfigureResumeSessionWithUserInputHandler() throws Exception {
158+
var session = createTestSession();
159+
var config = new ResumeSessionConfig()
160+
.setOnUserInputRequest((req, inv) -> CompletableFuture.completedFuture(new UserInputResponse()));
161+
162+
SessionRequestBuilder.configureSession(session, config);
163+
164+
// Handler was registered — verify by calling handleUserInputRequest
165+
// (package-private)
166+
var response = session.handleUserInputRequest(new com.github.copilot.sdk.json.UserInputRequest()).get();
167+
assertNotNull(response);
168+
}
169+
170+
@Test
171+
void testConfigureResumeSessionWithHooks() throws Exception {
172+
var session = createTestSession();
173+
var hooks = new SessionHooks().setOnPreToolUse((input, inv) -> CompletableFuture.completedFuture(null));
174+
var config = new ResumeSessionConfig().setHooks(hooks);
175+
176+
SessionRequestBuilder.configureSession(session, config);
177+
178+
// Hooks registered — handleHooksInvoke should dispatch preToolUse
179+
var mapper = JsonRpcClient.getObjectMapper();
180+
var input = mapper.valueToTree(Map.of("toolName", "test_tool"));
181+
var result = session.handleHooksInvoke("preToolUse", input).get();
182+
assertNull(result); // handler returns null
183+
}
184+
185+
// =========================================================================
186+
// Helper
187+
// =========================================================================
188+
189+
private CopilotSession createTestSession() throws Exception {
190+
var constructor = CopilotSession.class.getDeclaredConstructor(String.class, JsonRpcClient.class, String.class);
191+
constructor.setAccessible(true);
192+
return constructor.newInstance("builder-test-session", null, null);
193+
}
194+
}

0 commit comments

Comments
 (0)