forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionRequestBuilderTest.java
More file actions
956 lines (762 loc) · 42.3 KB
/
Copy pathSessionRequestBuilderTest.java
File metadata and controls
956 lines (762 loc) · 42.3 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.copilot.generated.rpc.SessionLimitsConfig;
import com.github.copilot.rpc.AutoModeSwitchResponse;
import com.github.copilot.rpc.CloudSessionOptions;
import com.github.copilot.rpc.CloudSessionRepository;
import com.github.copilot.rpc.CreateSessionRequest;
import com.github.copilot.rpc.DefaultAgentConfig;
import com.github.copilot.rpc.ElicitationHandler;
import com.github.copilot.rpc.ElicitationResult;
import com.github.copilot.rpc.ElicitationResultAction;
import com.github.copilot.rpc.ExitPlanModeResult;
import com.github.copilot.rpc.LargeToolOutputConfig;
import com.github.copilot.rpc.MemoryConfiguration;
import com.github.copilot.rpc.ResumeSessionConfig;
import com.github.copilot.rpc.ResumeSessionRequest;
import com.github.copilot.rpc.SessionConfig;
import com.github.copilot.rpc.SessionHooks;
import com.github.copilot.rpc.ToolDefinition;
import com.github.copilot.rpc.UserInputResponse;
/**
* Unit tests for {@link SessionRequestBuilder} branch coverage.
* <p>
* Exercises branches in buildCreateRequest, buildResumeRequest, and
* configureSession that are not reached by E2E tests.
*/
public class SessionRequestBuilderTest {
// =========================================================================
// buildCreateRequest
// =========================================================================
@Test
void testBuildCreateRequestNullConfig() {
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(null);
assertNotNull(request);
assertNull(request.getModel());
assertTrue(request.getRequestPermission(), "requestPermission should be true even for null config");
assertEquals("direct", request.getEnvValueMode(), "envValueMode should be 'direct' even for null config");
}
@Test
void testBuildCreateRequestHooksNonNullButEmpty() {
// Hooks object exists but hasHooks() returns false
var config = new SessionConfig().setHooks(new SessionHooks());
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertNull(request.getHooks(), "Should be null when hooks are empty");
}
@Test
void testBuildCreateRequestHooksWithHandler() {
var hooks = new SessionHooks().setOnPreToolUse((input, inv) -> CompletableFuture.completedFuture(null));
var config = new SessionConfig().setHooks(hooks);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertTrue(request.getHooks(), "Should be true when hooks have handlers");
}
@Test
void testBuildCreateRequestSetsEnvValueModeToDirect() {
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(new SessionConfig());
assertEquals("direct", request.getEnvValueMode());
}
@Test
void testBuildCreateRequestAlwaysSetsRequestPermissionTrue() {
// No permission handler set - requestPermission should still be true
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(new SessionConfig());
assertTrue(request.getRequestPermission(),
"requestPermission should always be true to enable deny-by-default behavior");
}
@Test
void testBuildCreateRequestSetsClientName() {
var config = new SessionConfig().setClientName("my-app");
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertEquals("my-app", request.getClientName());
}
@Test
void testBuildCreateRequestSetsReasoningSummary() {
var config = new SessionConfig().setReasoningSummary("concise");
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertEquals("concise", request.getReasoningSummary());
}
@Test
void testBuildCreateRequestSetsContextTier() {
var config = new SessionConfig().setContextTier("long_context");
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertEquals("long_context", request.getContextTier());
}
@Test
void testBuildCreateRequestSetsPluginDirectoriesAndLargeOutput() {
var largeOutput = new LargeToolOutputConfig().setEnabled(true).setMaxSizeBytes(1024L)
.setOutputDirectory("/tmp/out");
var config = new SessionConfig().setPluginDirectories(List.of("/plugins/a")).setLargeOutput(largeOutput);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertEquals(List.of("/plugins/a"), request.getPluginDirectories());
assertEquals(largeOutput, request.getLargeOutput());
}
@Test
void testBuildCreateRequestSetsMemory() {
var memory = new MemoryConfiguration().setEnabled(true);
var config = new SessionConfig().setMemory(memory);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config, "test-session-id");
assertEquals(memory, request.getMemory());
}
@Test
void testBuildCreateRequestOmitsMemoryWhenNotSet() {
var config = new SessionConfig();
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config, "test-session-id");
assertNull(request.getMemory());
}
@Test
void testBuildCreateRequestForwardsEnableSessionTelemetryWhenFalse() {
var config = new SessionConfig().setEnableSessionTelemetry(false);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertFalse(request.getEnableSessionTelemetry());
}
@Test
void testBuildCreateRequestOmitsEnableSessionTelemetryWhenNotSet() {
var config = new SessionConfig();
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertNull(request.getEnableSessionTelemetry());
}
@Test
void testBuildCreateRequestPassesThroughNullMcpOAuthTokenStorage() {
var config = new SessionConfig();
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertNull(request.getMcpOAuthTokenStorage());
}
@Test
void testBuildCreateRequestForwardsExplicitMcpOAuthTokenStorage() {
var config = new SessionConfig().setMcpOAuthTokenStorage("persistent");
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertEquals("persistent", request.getMcpOAuthTokenStorage());
}
@Test
void testBuildCreateRequestForwardsSessionPolicyOptions() {
var sessionLimits = new SessionLimitsConfig(30.0);
var config = new SessionConfig().setExcludedBuiltInAgents(List.of("explore")).setEnableCitations(true)
.setSessionLimits(sessionLimits);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config, "session-policy");
assertEquals(List.of("explore"), request.getExcludedBuiltInAgents());
assertTrue(request.getEnableCitations());
assertSame(sessionLimits, request.getSessionLimits());
}
@Test
void testBuildCreateRequestNullConfigHasNullMcpOAuthTokenStorage() {
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(null);
assertNull(request.getMcpOAuthTokenStorage());
}
// =========================================================================
// buildResumeRequest
// =========================================================================
@Test
void testBuildResumeRequestNullConfig() {
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-1", null);
assertEquals("sid-1", request.getSessionId());
assertNull(request.getModel());
assertTrue(request.getRequestPermission(), "requestPermission should be true even for null config");
assertEquals("direct", request.getEnvValueMode(), "envValueMode should be 'direct' even for null config");
}
@Test
void testBuildResumeRequestForwardsEnableSessionTelemetryWhenFalse() {
var config = new ResumeSessionConfig().setEnableSessionTelemetry(false);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-1", config);
assertFalse(request.getEnableSessionTelemetry());
}
@Test
void testBuildResumeRequestOmitsEnableSessionTelemetryWhenNotSet() {
var config = new ResumeSessionConfig();
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-1", config);
assertNull(request.getEnableSessionTelemetry());
}
@Test
void testBuildResumeRequestWithTools() {
var tool = ToolDefinition.create("my_tool", "A tool", Map.of("type", "object"),
inv -> CompletableFuture.completedFuture("result"));
var config = new ResumeSessionConfig().setTools(List.of(tool));
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-2", config);
assertNotNull(request.getTools());
assertEquals(1, request.getTools().size());
assertEquals("my_tool", request.getTools().get(0).name());
}
@Test
void testBuildResumeRequestWithUserInputHandler() {
var config = new ResumeSessionConfig()
.setOnUserInputRequest((req, inv) -> CompletableFuture.completedFuture(new UserInputResponse()));
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-3", config);
assertTrue(request.getRequestUserInput());
}
@Test
void testBuildResumeRequestHooksNonNullButEmpty() {
var config = new ResumeSessionConfig().setHooks(new SessionHooks());
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-4", config);
assertNull(request.getHooks(), "Should be null when hooks are empty");
}
@Test
void testBuildResumeRequestHooksWithHandler() {
var hooks = new SessionHooks().setOnSessionEnd((input, inv) -> CompletableFuture.completedFuture(null));
var config = new ResumeSessionConfig().setHooks(hooks);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-5", config);
assertTrue(request.getHooks(), "Should be true when hooks have handlers");
}
@Test
void testBuildResumeRequestDisableResume() {
var config = new ResumeSessionConfig().setDisableResume(true);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-6", config);
assertTrue(request.getDisableResume());
}
@Test
void testBuildResumeRequestStreaming() {
var config = new ResumeSessionConfig().setStreaming(true);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-7", config);
assertTrue(request.getStreaming());
}
@Test
void testBuildResumeRequestSetsEnvValueModeToDirect() {
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-8", new ResumeSessionConfig());
assertEquals("direct", request.getEnvValueMode());
}
@Test
void testBuildResumeRequestAlwaysSetsRequestPermissionTrue() {
// No permission handler set - requestPermission should still be true
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-9", new ResumeSessionConfig());
assertTrue(request.getRequestPermission(),
"requestPermission should always be true to enable deny-by-default behavior");
}
@Test
void testBuildResumeRequestSetsClientName() {
var config = new ResumeSessionConfig().setClientName("my-app");
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-10", config);
assertEquals("my-app", request.getClientName());
}
@Test
void testBuildCreateRequestPropagatesGranularMultitenancyFields() {
var config = new SessionConfig().setSkipEmbeddingRetrieval(true)
.setOrganizationCustomInstructions("Create org instructions")
.setEnableOnDemandInstructionDiscovery(false).setEnableFileHooks(true).setEnableHostGitOperations(false)
.setEnableSessionStore(true).setEnableSkills(false);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertTrue(request.getSkipEmbeddingRetrieval());
assertEquals("Create org instructions", request.getOrganizationCustomInstructions());
assertFalse(request.getEnableOnDemandInstructionDiscovery());
assertTrue(request.getEnableFileHooks());
assertFalse(request.getEnableHostGitOperations());
assertTrue(request.getEnableSessionStore());
assertFalse(request.getEnableSkills());
}
@Test
void testBuildResumeRequestPropagatesGranularMultitenancyFields() {
var config = new ResumeSessionConfig().setSkipEmbeddingRetrieval(false)
.setOrganizationCustomInstructions("Resume org instructions")
.setEnableOnDemandInstructionDiscovery(true).setEnableFileHooks(false).setEnableHostGitOperations(true)
.setEnableSessionStore(false).setEnableSkills(true);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-11", config);
assertFalse(request.getSkipEmbeddingRetrieval());
assertEquals("Resume org instructions", request.getOrganizationCustomInstructions());
assertTrue(request.getEnableOnDemandInstructionDiscovery());
assertFalse(request.getEnableFileHooks());
assertTrue(request.getEnableHostGitOperations());
assertFalse(request.getEnableSessionStore());
assertTrue(request.getEnableSkills());
}
@Test
void testBuildResumeRequestPassesThroughNullMcpOAuthTokenStorage() {
var config = new ResumeSessionConfig();
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-12", config);
assertNull(request.getMcpOAuthTokenStorage());
}
@Test
void testBuildResumeRequestForwardsExplicitMcpOAuthTokenStorage() {
var config = new ResumeSessionConfig().setMcpOAuthTokenStorage("persistent");
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-13", config);
assertEquals("persistent", request.getMcpOAuthTokenStorage());
}
@Test
void testBuildResumeRequestForwardsSessionPolicyOptions() {
var sessionLimits = new SessionLimitsConfig(30.0);
var config = new ResumeSessionConfig().setExcludedBuiltInAgents(List.of("explore")).setEnableCitations(true)
.setSessionLimits(sessionLimits);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-policy", config);
assertEquals(List.of("explore"), request.getExcludedBuiltInAgents());
assertTrue(request.getEnableCitations());
assertSame(sessionLimits, request.getSessionLimits());
}
@Test
void testBuildResumeRequestNullConfigHasNullMcpOAuthTokenStorage() {
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-14", null);
assertNull(request.getMcpOAuthTokenStorage());
}
@Test
void testBuildResumeRequestSetsReasoningSummary() {
var config = new ResumeSessionConfig().setReasoningSummary("none");
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-15", config);
assertEquals("none", request.getReasoningSummary());
}
@Test
void testBuildResumeRequestSetsContextTier() {
var config = new ResumeSessionConfig().setContextTier("default");
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-15", config);
assertEquals("default", request.getContextTier());
}
@Test
void testBuildResumeRequestSetsPluginDirectoriesAndLargeOutput() {
var largeOutput = new LargeToolOutputConfig().setEnabled(false).setMaxSizeBytes(2048L)
.setOutputDirectory("/tmp/resume");
var config = new ResumeSessionConfig().setPluginDirectories(List.of("/plugins/r")).setLargeOutput(largeOutput);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-16", config);
assertEquals(List.of("/plugins/r"), request.getPluginDirectories());
assertEquals(largeOutput, request.getLargeOutput());
}
@Test
void testBuildResumeRequestSetsMemory() {
var memory = new MemoryConfiguration().setEnabled(false);
var config = new ResumeSessionConfig().setMemory(memory);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-mem", config);
assertEquals(memory, request.getMemory());
}
@Test
void testBuildResumeRequestOmitsMemoryWhenNotSet() {
var config = new ResumeSessionConfig();
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-mem", config);
assertNull(request.getMemory());
}
// =========================================================================
// configureSession (ResumeSessionConfig overload)
// =========================================================================
@Test
void testConfigureResumeSessionNullConfig() throws Exception {
var session = createTestSession();
// Should not throw
SessionRequestBuilder.configureSession(session, (ResumeSessionConfig) null);
}
@Test
void testConfigureResumeSessionWithTools() throws Exception {
var session = createTestSession();
var tool = ToolDefinition.create("resume_tool", "desc", Map.of(),
inv -> CompletableFuture.completedFuture("ok"));
var config = new ResumeSessionConfig().setTools(List.of(tool));
SessionRequestBuilder.configureSession(session, config);
assertNotNull(session.getTool("resume_tool"));
}
@Test
void testConfigureResumeSessionWithUserInputHandler() throws Exception {
var session = createTestSession();
var config = new ResumeSessionConfig()
.setOnUserInputRequest((req, inv) -> CompletableFuture.completedFuture(new UserInputResponse()));
SessionRequestBuilder.configureSession(session, config);
// Handler was registered — verify by calling handleUserInputRequest
// (package-private)
var response = session.handleUserInputRequest(new com.github.copilot.rpc.UserInputRequest()).get();
assertNotNull(response);
}
@Test
void testConfigureResumeSessionWithHooks() throws Exception {
var session = createTestSession();
var hooks = new SessionHooks().setOnPreToolUse((input, inv) -> CompletableFuture.completedFuture(null));
var config = new ResumeSessionConfig().setHooks(hooks);
SessionRequestBuilder.configureSession(session, config);
// Hooks registered — handleHooksInvoke should dispatch preToolUse
var mapper = JsonRpcClient.getObjectMapper();
var input = mapper.valueToTree(Map.of("toolName", "test_tool"));
var result = session.handleHooksInvoke("preToolUse", input).get();
assertNull(result); // handler returns null
}
// =========================================================================
// Helper
// =========================================================================
private CopilotSession createTestSession() throws Exception {
var constructor = CopilotSession.class.getDeclaredConstructor(String.class, JsonRpcClient.class, String.class);
constructor.setAccessible(true);
return constructor.newInstance("builder-test-session", null, null);
}
@Test
void testBuildCreateRequestWithAgent() {
var config = new SessionConfig().setAgent("my-agent");
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config, "test-session-id");
assertEquals("my-agent", request.getAgent());
}
@Test
void testBuildResumeRequestWithAgent() {
var config = new ResumeSessionConfig().setAgent("my-agent");
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("session-id", config);
assertEquals("my-agent", request.getAgent());
}
// =========================================================================
// extractTransformCallbacks
// =========================================================================
@Test
void extractTransformCallbacks_nullSystemMessage_returnsNull() {
ExtractedTransforms result = SessionRequestBuilder.extractTransformCallbacks(null);
assertNull(result.wireSystemMessage());
assertNull(result.transformCallbacks());
}
@Test
void extractTransformCallbacks_appendMode_returnsOriginalConfig() {
var config = new com.github.copilot.rpc.SystemMessageConfig()
.setMode(com.github.copilot.SystemMessageMode.APPEND).setContent("extra content");
ExtractedTransforms result = SessionRequestBuilder.extractTransformCallbacks(config);
assertSame(config, result.wireSystemMessage());
assertNull(result.transformCallbacks());
}
@Test
void extractTransformCallbacks_customizeModeNoTransforms_returnsOriginalConfig() {
var sections = Map.of("tone", new com.github.copilot.rpc.SectionOverride()
.setAction(com.github.copilot.rpc.SectionOverrideAction.REMOVE));
var config = new com.github.copilot.rpc.SystemMessageConfig()
.setMode(com.github.copilot.SystemMessageMode.CUSTOMIZE).setSections(sections);
ExtractedTransforms result = SessionRequestBuilder.extractTransformCallbacks(config);
assertSame(config, result.wireSystemMessage());
assertNull(result.transformCallbacks());
}
@Test
void extractTransformCallbacks_customizeModeWithTransform_extractsCallbacks() {
var transformFn = (java.util.function.Function<String, CompletableFuture<String>>) content -> CompletableFuture
.completedFuture(content + " modified");
var sections = Map.of("identity", new com.github.copilot.rpc.SectionOverride().setTransform(transformFn));
var config = new com.github.copilot.rpc.SystemMessageConfig()
.setMode(com.github.copilot.SystemMessageMode.CUSTOMIZE).setSections(sections);
ExtractedTransforms result = SessionRequestBuilder.extractTransformCallbacks(config);
// Wire config should be different from original
assertNotSame(config, result.wireSystemMessage());
// Callbacks should be extracted
assertNotNull(result.transformCallbacks());
assertTrue(result.transformCallbacks().containsKey("identity"));
// Wire config should have transform action instead of callback
assertNotNull(result.wireSystemMessage().getSections());
var wireSection = result.wireSystemMessage().getSections().get("identity");
assertNotNull(wireSection);
assertEquals(com.github.copilot.rpc.SectionOverrideAction.TRANSFORM, wireSection.getAction());
assertNull(wireSection.getTransform());
}
@Test
@SuppressWarnings("deprecation")
void buildCreateRequestWithSessionId_usesProvidedSessionId() {
var config = new SessionConfig();
config.setSessionId("my-session-id");
// The deprecated single-arg overload uses the sessionId from config when set
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertEquals("my-session-id", request.getSessionId());
}
@Test
void configureSessionWithNullConfig_returnsEarly() {
// configureSession with null config should return without error
CopilotSession session = new CopilotSession("session-1", null);
// Covers the null config early-return branch (L219-220)
assertDoesNotThrow(() -> SessionRequestBuilder.configureSession(session, (SessionConfig) null));
}
@Test
void configureSessionWithCommands_registersCommands() {
CopilotSession session = new CopilotSession("session-1", null);
var cmd = new com.github.copilot.rpc.CommandDefinition().setName("deploy")
.setHandler(ctx -> CompletableFuture.completedFuture(null));
var config = new SessionConfig().setCommands(List.of(cmd));
// Covers config.getCommands() != null branch (L235-236)
SessionRequestBuilder.configureSession(session, config);
// If no exception thrown, the branch was covered
}
@Test
void configureSessionWithElicitationHandler_registersHandler() {
CopilotSession session = new CopilotSession("session-1", null);
ElicitationHandler handler = (context) -> CompletableFuture
.completedFuture(new ElicitationResult().setAction(ElicitationResultAction.CANCEL));
var config = new SessionConfig().setOnElicitationRequest(handler);
// Covers config.getOnElicitationRequest() != null branch (L238-239)
SessionRequestBuilder.configureSession(session, config);
}
@Test
void configureSessionWithOnEvent_registersEventHandler() {
CopilotSession session = new CopilotSession("session-1", null);
var config = new SessionConfig().setOnEvent(event -> {
});
// Covers config.getOnEvent() != null branch (L241-242)
SessionRequestBuilder.configureSession(session, config);
}
@Test
void configureResumedSessionWithCommands_registersCommands() {
CopilotSession session = new CopilotSession("session-1", null);
var cmd = new com.github.copilot.rpc.CommandDefinition().setName("rollback")
.setHandler(ctx -> CompletableFuture.completedFuture(null));
var config = new ResumeSessionConfig().setCommands(List.of(cmd));
// Covers ResumeSessionConfig.getCommands() != null branch (L271-272)
SessionRequestBuilder.configureSession(session, config);
}
@Test
void configureResumedSessionWithElicitationHandler_registersHandler() {
CopilotSession session = new CopilotSession("session-1", null);
ElicitationHandler handler = (context) -> CompletableFuture
.completedFuture(new ElicitationResult().setAction(ElicitationResultAction.CANCEL));
var config = new ResumeSessionConfig().setOnElicitationRequest(handler);
// Covers ResumeSessionConfig.getOnElicitationRequest() != null branch
// (L274-275)
SessionRequestBuilder.configureSession(session, config);
}
@Test
void configureResumedSessionWithOnEvent_registersEventHandler() {
CopilotSession session = new CopilotSession("session-1", null);
var config = new ResumeSessionConfig().setOnEvent(event -> {
});
// Covers ResumeSessionConfig.getOnEvent() != null branch (L277-278)
SessionRequestBuilder.configureSession(session, config);
}
@Test
void testBuildCreateRequestWithDefaultAgent() {
var defaultAgent = new DefaultAgentConfig().setExcludedTools(List.of("secret_tool"));
var config = new SessionConfig().setDefaultAgent(defaultAgent);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertNotNull(request.getDefaultAgent());
assertEquals(List.of("secret_tool"), request.getDefaultAgent().getExcludedTools());
}
@Test
void testBuildCreateRequestWithGitHubToken() {
var config = new SessionConfig().setGitHubToken("ghp_per_session_token");
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertEquals("ghp_per_session_token", request.getGitHubToken());
}
@Test
void testBuildResumeRequestWithDefaultAgent() {
var defaultAgent = new DefaultAgentConfig().setExcludedTools(List.of("secret_tool"));
var config = new ResumeSessionConfig().setDefaultAgent(defaultAgent);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("test-session", config);
assertNotNull(request.getDefaultAgent());
assertEquals(List.of("secret_tool"), request.getDefaultAgent().getExcludedTools());
}
@Test
void testBuildResumeRequestWithGitHubToken() {
var config = new ResumeSessionConfig().setGitHubToken("ghp_per_session_token");
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("test-session", config);
assertEquals("ghp_per_session_token", request.getGitHubToken());
}
// =========================================================================
// instructionDirectories propagation
// =========================================================================
@Test
void testBuildCreateRequestPropagatesInstructionDirectories() {
var dirs = List.of("/path/to/instructions", "/another/path");
var config = new SessionConfig().setInstructionDirectories(dirs);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertEquals(dirs, request.getInstructionDirectories());
}
@Test
void testBuildResumeRequestPropagatesInstructionDirectories() {
var dirs = List.of("/resume/instructions", "/other/dir");
var config = new ResumeSessionConfig().setInstructionDirectories(dirs);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-inst", config);
assertEquals(dirs, request.getInstructionDirectories());
}
// =========================================================================
// enableSessionTelemetry serialization
// =========================================================================
@Test
void testCreateRequestSerializesEnableSessionTelemetryWhenFalse() throws Exception {
var config = new SessionConfig().setEnableSessionTelemetry(false);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
var mapper = JsonRpcClient.getObjectMapper();
var json = mapper.writeValueAsString(request);
assertTrue(json.contains("\"enableSessionTelemetry\":false"),
"enableSessionTelemetry should be serialized when set to false");
}
@Test
void testCreateRequestOmitsEnableSessionTelemetryWhenNull() throws Exception {
var config = new SessionConfig();
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
var mapper = JsonRpcClient.getObjectMapper();
var json = mapper.writeValueAsString(request);
assertFalse(json.contains("enableSessionTelemetry"), "enableSessionTelemetry should be omitted when null");
}
@Test
void testResumeRequestSerializesEnableSessionTelemetryWhenFalse() throws Exception {
var config = new ResumeSessionConfig().setEnableSessionTelemetry(false);
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-tel", config);
var mapper = JsonRpcClient.getObjectMapper();
var json = mapper.writeValueAsString(request);
assertTrue(json.contains("\"enableSessionTelemetry\":false"),
"enableSessionTelemetry should be serialized when set to false");
}
@Test
void testResumeRequestOmitsEnableSessionTelemetryWhenNull() throws Exception {
var config = new ResumeSessionConfig();
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("sid-tel", config);
var mapper = JsonRpcClient.getObjectMapper();
var json = mapper.writeValueAsString(request);
assertFalse(json.contains("enableSessionTelemetry"), "enableSessionTelemetry should be omitted when null");
}
// =========================================================================
// Mode handler request flags
// =========================================================================
@Test
void testBuildCreateRequestWithExitPlanModeHandler() {
var config = new SessionConfig().setOnExitPlanMode(
(request, invocation) -> CompletableFuture.completedFuture(new ExitPlanModeResult()));
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertTrue(request.getRequestExitPlanMode());
}
@Test
void testBuildCreateRequestWithAutoModeSwitchHandler() {
var config = new SessionConfig().setOnAutoModeSwitch(
(request, invocation) -> CompletableFuture.completedFuture(AutoModeSwitchResponse.NO));
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertTrue(request.getRequestAutoModeSwitch());
}
@Test
void testBuildCreateRequestWithoutModeHandlers() {
var config = new SessionConfig();
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertNull(request.getRequestExitPlanMode());
assertNull(request.getRequestAutoModeSwitch());
}
@Test
void testBuildResumeRequestWithExitPlanModeHandler() {
var config = new ResumeSessionConfig().setOnExitPlanMode(
(request, invocation) -> CompletableFuture.completedFuture(new ExitPlanModeResult()));
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("session-1", config);
assertTrue(request.getRequestExitPlanMode());
}
@Test
void testBuildResumeRequestWithAutoModeSwitchHandler() {
var config = new ResumeSessionConfig().setOnAutoModeSwitch(
(request, invocation) -> CompletableFuture.completedFuture(AutoModeSwitchResponse.NO));
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("session-1", config);
assertTrue(request.getRequestAutoModeSwitch());
}
@Test
void configureSessionWithExitPlanModeHandler_registersHandler() {
CopilotSession session = new CopilotSession("session-1", null);
var config = new SessionConfig().setOnExitPlanMode(
(request, invocation) -> CompletableFuture.completedFuture(new ExitPlanModeResult()));
SessionRequestBuilder.configureSession(session, config);
}
@Test
void configureSessionWithAutoModeSwitchHandler_registersHandler() {
CopilotSession session = new CopilotSession("session-1", null);
var config = new SessionConfig().setOnAutoModeSwitch(
(request, invocation) -> CompletableFuture.completedFuture(AutoModeSwitchResponse.NO));
SessionRequestBuilder.configureSession(session, config);
}
@Test
void configureResumedSessionWithExitPlanModeHandler_registersHandler() {
CopilotSession session = new CopilotSession("session-1", null);
var config = new ResumeSessionConfig().setOnExitPlanMode(
(request, invocation) -> CompletableFuture.completedFuture(new ExitPlanModeResult()));
SessionRequestBuilder.configureSession(session, config);
}
@Test
void configureResumedSessionWithAutoModeSwitchHandler_registersHandler() {
CopilotSession session = new CopilotSession("session-1", null);
var config = new ResumeSessionConfig().setOnAutoModeSwitch(
(request, invocation) -> CompletableFuture.completedFuture(AutoModeSwitchResponse.NO));
SessionRequestBuilder.configureSession(session, config);
}
@Test
void testCreateRequestSerializesModeFlags() throws Exception {
var config = new SessionConfig()
.setOnExitPlanMode((r, i) -> CompletableFuture.completedFuture(new ExitPlanModeResult()))
.setOnAutoModeSwitch((r, i) -> CompletableFuture.completedFuture(AutoModeSwitchResponse.NO));
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
var mapper = JsonRpcClient.getObjectMapper();
var json = mapper.writeValueAsString(request);
assertTrue(json.contains("\"requestExitPlanMode\":true"));
assertTrue(json.contains("\"requestAutoModeSwitch\":true"));
}
@Test
void testResumeRequestSerializesModeFlags() throws Exception {
var config = new ResumeSessionConfig()
.setOnExitPlanMode((r, i) -> CompletableFuture.completedFuture(new ExitPlanModeResult()))
.setOnAutoModeSwitch((r, i) -> CompletableFuture.completedFuture(AutoModeSwitchResponse.NO));
ResumeSessionRequest request = SessionRequestBuilder.buildResumeRequest("session-1", config);
var mapper = JsonRpcClient.getObjectMapper();
var json = mapper.writeValueAsString(request);
assertTrue(json.contains("\"requestExitPlanMode\":true"));
assertTrue(json.contains("\"requestAutoModeSwitch\":true"));
}
// =========================================================================
// Cloud session options wiring
// =========================================================================
@Test
void testBuildCreateRequestPropagatesCloudSessionOptions() throws Exception {
var cloud = new CloudSessionOptions()
.setRepository(new CloudSessionRepository().setOwner("my-org").setName("my-repo").setBranch("main"));
var config = new SessionConfig().setCloud(cloud);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
assertNotNull(request.getCloud());
assertEquals("my-org", request.getCloud().getRepository().getOwner());
assertEquals("my-repo", request.getCloud().getRepository().getName());
assertEquals("main", request.getCloud().getRepository().getBranch());
}
@Test
void testBuildCreateRequestOmitsCloudWhenNull() throws Exception {
var config = new SessionConfig();
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
var mapper = JsonRpcClient.getObjectMapper();
var json = mapper.writeValueAsString(request);
assertNull(request.getCloud());
assertFalse(json.contains("\"cloud\""), "cloud should be omitted when null");
}
@Test
void testCloudSessionOptionsSerializesCorrectly() throws Exception {
var cloud = new CloudSessionOptions()
.setRepository(new CloudSessionRepository().setOwner("acme").setName("widgets").setBranch("feature-1"));
var config = new SessionConfig().setCloud(cloud);
CreateSessionRequest request = SessionRequestBuilder.buildCreateRequest(config);
var mapper = JsonRpcClient.getObjectMapper();
var json = mapper.writeValueAsString(request);
assertTrue(json.contains("\"cloud\""));
assertTrue(json.contains("\"owner\":\"acme\""));
assertTrue(json.contains("\"name\":\"widgets\""));
assertTrue(json.contains("\"branch\":\"feature-1\""));
}
// =========================================================================
// ExP assignment injection wiring
// =========================================================================
@Test
void testBuildRequestsPropagateAndSerializeExpAssignments() throws Exception {
var mapper = JsonRpcClient.getObjectMapper();
JsonNode createAssignments = mapper.readTree("{\"Configs\":[{\"Id\":\"exp-create\"}]}");
JsonNode resumeAssignments = mapper.readTree("{\"Configs\":[{\"Id\":\"exp-resume\"}]}");
var createConfig = new SessionConfig().setExpAssignments(createAssignments);
CreateSessionRequest createRequest = SessionRequestBuilder.buildCreateRequest(createConfig, "session-1");
assertEquals(createAssignments, createRequest.getExpAssignments());
var createJson = mapper.writeValueAsString(createRequest);
assertTrue(createJson.contains("\"expAssignments\""));
assertTrue(createJson.contains("\"Id\":\"exp-create\""));
var resumeConfig = new ResumeSessionConfig().setExpAssignments(resumeAssignments);
ResumeSessionRequest resumeRequest = SessionRequestBuilder.buildResumeRequest("session-1", resumeConfig);
assertEquals(resumeAssignments, resumeRequest.getExpAssignments());
var resumeJson = mapper.writeValueAsString(resumeRequest);
assertTrue(resumeJson.contains("\"expAssignments\""));
assertTrue(resumeJson.contains("\"Id\":\"exp-resume\""));
}
@Test
void testBuildRequestsOmitExpAssignmentsWhenUnset() throws Exception {
var mapper = JsonRpcClient.getObjectMapper();
CreateSessionRequest createRequest = SessionRequestBuilder.buildCreateRequest(new SessionConfig(), "session-1");
assertNull(createRequest.getExpAssignments());
var createJson = mapper.writeValueAsString(createRequest);
assertFalse(createJson.contains("\"expAssignments\""), "expAssignments should be omitted when null");
ResumeSessionRequest resumeRequest = SessionRequestBuilder.buildResumeRequest("session-1",
new ResumeSessionConfig());
assertNull(resumeRequest.getExpAssignments());
var resumeJson = mapper.writeValueAsString(resumeRequest);
assertFalse(resumeJson.contains("\"expAssignments\""), "expAssignments should be omitted when null");
}
@Test
void testClonePreservesAndForwardsExpAssignments() throws Exception {
var mapper = JsonRpcClient.getObjectMapper();
JsonNode createAssignments = mapper.readTree("{\"Configs\":[{\"Id\":\"exp-create\"}]}");
JsonNode resumeAssignments = mapper.readTree("{\"Configs\":[{\"Id\":\"exp-resume\"}]}");
var createConfig = new SessionConfig().setExpAssignments(createAssignments);
SessionConfig createClone = createConfig.clone();
assertEquals(createAssignments, createClone.getExpAssignments());
CreateSessionRequest createRequest = SessionRequestBuilder.buildCreateRequest(createClone, "session-1");
assertEquals(createAssignments, createRequest.getExpAssignments());
assertTrue(mapper.writeValueAsString(createRequest).contains("\"Id\":\"exp-create\""));
var resumeConfig = new ResumeSessionConfig().setExpAssignments(resumeAssignments);
ResumeSessionConfig resumeClone = resumeConfig.clone();
assertEquals(resumeAssignments, resumeClone.getExpAssignments());
ResumeSessionRequest resumeRequest = SessionRequestBuilder.buildResumeRequest("session-1", resumeClone);
assertEquals(resumeAssignments, resumeRequest.getExpAssignments());
assertTrue(mapper.writeValueAsString(resumeRequest).contains("\"Id\":\"exp-resume\""));
}
}