-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_client_session.cpp
More file actions
848 lines (678 loc) · 25.2 KB
/
Copy pathtest_client_session.cpp
File metadata and controls
848 lines (678 loc) · 25.2 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
// Copyright (c) 2025 Elias Bachaalany
// SPDX-License-Identifier: MIT
#include <copilot/client.hpp>
#include <copilot/session.hpp>
#include <gtest/gtest.h>
using namespace copilot;
// =============================================================================
// Client Tests (unit tests for isolated functionality)
// =============================================================================
TEST(ClientTest, DefaultConstruction)
{
Client client;
EXPECT_EQ(client.state(), ConnectionState::Disconnected);
}
TEST(ClientTest, ConstructionWithOptions)
{
ClientOptions opts;
opts.log_level = LogLevel::Debug;
opts.auto_start = false;
opts.port = 8080;
Client client(opts);
EXPECT_EQ(client.state(), ConnectionState::Disconnected);
}
TEST(ClientTest, MutuallyExclusiveOptions)
{
ClientOptions opts;
opts.cli_url = "localhost:8080";
opts.use_stdio = true;
EXPECT_THROW(Client client(opts), std::invalid_argument);
}
TEST(ClientTest, MutuallyExclusiveOptionsWithCliPath)
{
ClientOptions opts;
opts.cli_url = "localhost:8080";
opts.cli_path = "/usr/bin/copilot";
EXPECT_THROW(Client client(opts), std::invalid_argument);
}
TEST(ClientTest, CliUrlPortOnly)
{
ClientOptions opts;
opts.cli_url = "8080";
opts.use_stdio = false;
// Should not throw - port-only URL is valid
Client client(opts);
EXPECT_EQ(client.state(), ConnectionState::Disconnected);
}
TEST(ClientTest, CliUrlWithHost)
{
ClientOptions opts;
opts.cli_url = "example.com:9090";
opts.use_stdio = false;
Client client(opts);
EXPECT_EQ(client.state(), ConnectionState::Disconnected);
}
TEST(ClientTest, CliUrlWithScheme)
{
ClientOptions opts;
opts.cli_url = "https://example.com:443";
opts.use_stdio = false;
Client client(opts);
EXPECT_EQ(client.state(), ConnectionState::Disconnected);
}
// =============================================================================
// Subscription Tests
// =============================================================================
TEST(SubscriptionTest, DefaultConstruction)
{
Subscription sub;
// Default constructed subscription should not crash
sub.unsubscribe();
}
TEST(SubscriptionTest, UnsubscribeCalledOnDestruction)
{
bool unsubscribed = false;
{
Subscription sub([&unsubscribed]() { unsubscribed = true; });
EXPECT_FALSE(unsubscribed);
}
EXPECT_TRUE(unsubscribed);
}
TEST(SubscriptionTest, ManualUnsubscribe)
{
int unsubscribe_count = 0;
Subscription sub([&unsubscribe_count]() { unsubscribe_count++; });
sub.unsubscribe();
EXPECT_EQ(unsubscribe_count, 1);
// Second unsubscribe should not call handler again
sub.unsubscribe();
EXPECT_EQ(unsubscribe_count, 1);
}
TEST(SubscriptionTest, MoveConstruction)
{
bool unsubscribed = false;
Subscription sub1([&unsubscribed]() { unsubscribed = true; });
Subscription sub2(std::move(sub1));
// Moving should not trigger unsubscribe
EXPECT_FALSE(unsubscribed);
// Destroying moved-from should not trigger unsubscribe
sub1 = Subscription();
EXPECT_FALSE(unsubscribed);
}
TEST(SubscriptionTest, MoveAssignment)
{
int count1 = 0;
int count2 = 0;
Subscription sub1([&count1]() { count1++; });
Subscription sub2([&count2]() { count2++; });
// Move assign sub2 into sub1 - should unsubscribe sub1
sub1 = std::move(sub2);
EXPECT_EQ(count1, 1);
EXPECT_EQ(count2, 0);
// Destroying sub1 should call sub2's handler
sub1 = Subscription();
EXPECT_EQ(count2, 1);
}
// =============================================================================
// Tool Tests
// =============================================================================
TEST(ToolTest, ToolDefinition)
{
Tool tool;
tool.name = "test_tool";
tool.description = "A test tool";
tool.parameters_schema =
json{{"type", "object"}, {"properties", {{"input", {{"type", "string"}}}}}};
tool.handler = [](const ToolInvocation& inv) -> ToolResultObject
{
ToolResultObject result;
result.text_result_for_llm = "Success";
return result;
};
EXPECT_EQ(tool.name, "test_tool");
EXPECT_EQ(tool.description, "A test tool");
// Test handler execution
ToolInvocation inv;
inv.session_id = "sess-1";
inv.tool_call_id = "call-1";
inv.tool_name = "test_tool";
inv.arguments = json{{"input", "hello"}};
auto result = tool.handler(inv);
EXPECT_EQ(result.text_result_for_llm, "Success");
EXPECT_EQ(result.result_type, ToolResultType::Success);
}
// =============================================================================
// Configuration Types Tests
// =============================================================================
TEST(SessionConfigTest, DefaultValues)
{
SessionConfig config;
EXPECT_FALSE(config.session_id.has_value());
EXPECT_FALSE(config.model.has_value());
EXPECT_TRUE(config.tools.empty());
EXPECT_FALSE(config.system_message.has_value());
EXPECT_FALSE(config.available_tools.has_value());
EXPECT_FALSE(config.excluded_tools.has_value());
EXPECT_FALSE(config.provider.has_value());
EXPECT_FALSE(config.streaming);
}
TEST(ResumeSessionConfigTest, DefaultValues)
{
ResumeSessionConfig config;
EXPECT_TRUE(config.tools.empty());
EXPECT_FALSE(config.provider.has_value());
EXPECT_FALSE(config.streaming);
}
TEST(ClientOptionsTest, DefaultValues)
{
ClientOptions opts;
EXPECT_FALSE(opts.cli_path.has_value());
EXPECT_FALSE(opts.cli_args.has_value());
EXPECT_FALSE(opts.cwd.has_value());
EXPECT_EQ(opts.port, 0);
EXPECT_TRUE(opts.use_stdio);
EXPECT_FALSE(opts.cli_url.has_value());
EXPECT_EQ(opts.log_level, LogLevel::Info);
EXPECT_TRUE(opts.auto_start);
// auto_restart is deprecated and now defaults to false (was true; never actually
// used in the C++ port and upstream nodejs SDK marks it deprecated/no-op).
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4996)
#endif
EXPECT_FALSE(opts.auto_restart);
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic pop
#elif defined(_MSC_VER)
# pragma warning(pop)
#endif
EXPECT_FALSE(opts.environment.has_value());
// New v0.1.49 fields - defaults
EXPECT_FALSE(opts.tcp_connection_token.has_value());
EXPECT_FALSE(opts.copilot_home.has_value());
EXPECT_FALSE(opts.session_idle_timeout_seconds.has_value());
EXPECT_FALSE(opts.remote);
}
// =============================================================================
// MessageOptions Tests
// =============================================================================
TEST(MessageOptionsTest, JsonSerialization)
{
MessageOptions opts;
opts.prompt = "Hello, world!";
opts.mode = "plan";
json j = opts;
EXPECT_EQ(j["prompt"], "Hello, world!");
EXPECT_EQ(j["mode"], "plan");
EXPECT_FALSE(j.contains("attachments"));
}
TEST(MessageOptionsTest, WithAttachments)
{
MessageOptions opts;
opts.prompt = "Analyze this file";
opts.attachments =
std::vector<UserMessageAttachment>{{AttachmentType::File, "/path/to/file.txt", "file.txt"}};
json j = opts;
EXPECT_EQ(j["prompt"], "Analyze this file");
EXPECT_TRUE(j.contains("attachments"));
EXPECT_EQ(j["attachments"].size(), 1);
EXPECT_EQ(j["attachments"][0]["type"], "file");
EXPECT_EQ(j["attachments"][0]["path"], "/path/to/file.txt");
}
// =============================================================================
// PermissionRequest Tests
// =============================================================================
TEST(PermissionRequestTest, Serialization)
{
PermissionRequest req;
req.kind = "tool_execution";
req.tool_call_id = "call-123";
req.extension_data["toolName"] = "bash";
req.extension_data["arguments"] = json{{"command", "ls"}};
json j = req;
EXPECT_EQ(j["kind"], "tool_execution");
EXPECT_EQ(j["toolCallId"], "call-123");
EXPECT_EQ(j["toolName"], "bash");
}
TEST(PermissionRequestTest, Deserialization)
{
json j = {
{"kind", "tool_execution"},
{"toolCallId", "call-456"},
{"toolName", "read_file"},
{"filePath", "/etc/passwd"}
};
auto req = j.get<PermissionRequest>();
EXPECT_EQ(req.kind, "tool_execution");
EXPECT_TRUE(req.tool_call_id.has_value());
EXPECT_EQ(*req.tool_call_id, "call-456");
EXPECT_EQ(req.extension_data["toolName"], "read_file");
EXPECT_EQ(req.extension_data["filePath"], "/etc/passwd");
}
TEST(PermissionRequestResultTest, Approved)
{
PermissionRequestResult result;
result.kind = "approved";
json j = result;
EXPECT_EQ(j["kind"], "approved");
EXPECT_FALSE(j.contains("rules"));
}
TEST(PermissionRequestResultTest, Denied)
{
PermissionRequestResult result;
result.kind = "denied-no-approval-rule-and-could-not-request-from-user";
json j = result;
EXPECT_EQ(j["kind"], "denied-no-approval-rule-and-could-not-request-from-user");
EXPECT_FALSE(j.contains("rules"));
}
TEST(PermissionRequestResultTest, ApprovedWithRules)
{
PermissionRequestResult result;
result.kind = "approved";
result.rules = std::vector<json>{{{"type", "allow"}, {"pattern", "*"}}};
json j = result;
EXPECT_EQ(j["kind"], "approved");
EXPECT_TRUE(j.contains("rules"));
EXPECT_EQ(j["rules"].size(), 1);
}
// =============================================================================
// Request Builder Tests (validates JSON payload shape without CLI)
// =============================================================================
TEST(SessionCreateRequestTest, IncludesToolsWhenProvided)
{
SessionConfig config;
Tool tool1;
tool1.name = "calculator";
tool1.description = "Perform math calculations";
tool1.parameters_schema = json{
{"type", "object"},
{"properties", {{"expression", {{"type", "string"}}}}},
{"required", {"expression"}}};
Tool tool2;
tool2.name = "weather";
tool2.description = "Get weather info";
config.tools = {tool1, tool2};
json request = build_session_create_request(config);
EXPECT_TRUE(request.contains("tools"));
EXPECT_EQ(request["tools"].size(), 2);
// Verify first tool
EXPECT_EQ(request["tools"][0]["name"], "calculator");
EXPECT_EQ(request["tools"][0]["description"], "Perform math calculations");
EXPECT_TRUE(request["tools"][0].contains("parameters"));
EXPECT_EQ(request["tools"][0]["parameters"]["type"], "object");
// Verify second tool
EXPECT_EQ(request["tools"][1]["name"], "weather");
EXPECT_EQ(request["tools"][1]["description"], "Get weather info");
EXPECT_FALSE(request["tools"][1].contains("parameters"));
}
TEST(SessionCreateRequestTest, ExcludesToolsWhenEmpty)
{
SessionConfig config;
// tools is empty by default
json request = build_session_create_request(config);
EXPECT_FALSE(request.contains("tools"));
}
TEST(SessionCreateRequestTest, IncludesOtherFields)
{
SessionConfig config;
config.model = "claude-3-opus";
config.session_id = "sess-123";
config.streaming = true;
config.available_tools = std::vector<std::string>{"read", "write"};
config.excluded_tools = std::vector<std::string>{"bash"};
json request = build_session_create_request(config);
EXPECT_EQ(request["model"], "claude-3-opus");
EXPECT_EQ(request["sessionId"], "sess-123");
EXPECT_EQ(request["streaming"], true);
EXPECT_EQ(request["availableTools"].size(), 2);
EXPECT_EQ(request["excludedTools"].size(), 1);
}
TEST(SessionCreateRequestTest, IncludesSystemMessage)
{
SessionConfig config;
SystemMessageConfig sys_msg;
sys_msg.content = "You are a helpful assistant.";
sys_msg.mode = SystemMessageMode::Append;
config.system_message = sys_msg;
json request = build_session_create_request(config);
EXPECT_TRUE(request.contains("systemMessage"));
EXPECT_EQ(request["systemMessage"]["content"], "You are a helpful assistant.");
EXPECT_EQ(request["systemMessage"]["mode"], "append");
}
TEST(SessionResumeRequestTest, IncludesToolsWhenProvided)
{
ResumeSessionConfig config;
Tool tool;
tool.name = "custom_tool";
tool.description = "A custom tool for testing";
tool.parameters_schema = json{
{"type", "object"},
{"properties", {{"input", {{"type", "string"}}}}}};
config.tools = {tool};
json request = build_session_resume_request("sess-456", config);
EXPECT_EQ(request["sessionId"], "sess-456");
EXPECT_TRUE(request.contains("tools"));
EXPECT_EQ(request["tools"].size(), 1);
EXPECT_EQ(request["tools"][0]["name"], "custom_tool");
EXPECT_EQ(request["tools"][0]["description"], "A custom tool for testing");
EXPECT_TRUE(request["tools"][0].contains("parameters"));
}
TEST(SessionResumeRequestTest, ExcludesToolsWhenEmpty)
{
ResumeSessionConfig config;
// tools is empty by default
json request = build_session_resume_request("sess-789", config);
EXPECT_EQ(request["sessionId"], "sess-789");
EXPECT_FALSE(request.contains("tools"));
}
TEST(SessionResumeRequestTest, IncludesOtherFields)
{
ResumeSessionConfig config;
config.streaming = true;
config.provider = json{{"baseUrl", "https://api.example.com"}};
json request = build_session_resume_request("sess-abc", config);
EXPECT_EQ(request["sessionId"], "sess-abc");
EXPECT_EQ(request["streaming"], true);
EXPECT_TRUE(request.contains("provider"));
EXPECT_EQ(request["provider"]["baseUrl"], "https://api.example.com");
}
// =============================================================================
// Combined Configuration Tests (all options together)
// =============================================================================
TEST(SessionCreateRequestTest, CombinedToolsAndMcpServers)
{
SessionConfig config;
// Add custom tool
Tool tool;
tool.name = "calculator";
tool.description = "Math calculations";
tool.parameters_schema = json{{"type", "object"}};
config.tools = {tool};
// Add MCP server
McpLocalServerConfig mcp_config;
mcp_config.type = "local";
mcp_config.command = "npx";
mcp_config.args = {"mcp-server"};
mcp_config.tools = {"*"};
std::map<std::string, json> mcp_servers;
mcp_servers["test-mcp"] = mcp_config;
config.mcp_servers = mcp_servers;
json request = build_session_create_request(config);
// Both tools and mcpServers should be present
EXPECT_TRUE(request.contains("tools"));
EXPECT_EQ(request["tools"].size(), 1);
EXPECT_EQ(request["tools"][0]["name"], "calculator");
EXPECT_TRUE(request.contains("mcpServers"));
EXPECT_TRUE(request["mcpServers"].contains("test-mcp"));
EXPECT_EQ(request["mcpServers"]["test-mcp"]["type"], "local");
EXPECT_EQ(request["mcpServers"]["test-mcp"]["command"], "npx");
}
TEST(SessionCreateRequestTest, CombinedToolsAndCustomAgents)
{
SessionConfig config;
// Add custom tool
Tool tool;
tool.name = "data_tool";
tool.description = "Data operations";
config.tools = {tool};
// Add custom agent
CustomAgentConfig agent;
agent.name = "analyst";
agent.display_name = "Data Analyst";
agent.description = "Analyzes data";
agent.prompt = "You are a data analyst.";
agent.tools = std::vector<std::string>{"data_tool"};
config.custom_agents = std::vector<CustomAgentConfig>{agent};
json request = build_session_create_request(config);
// Both tools and customAgents should be present
EXPECT_TRUE(request.contains("tools"));
EXPECT_EQ(request["tools"].size(), 1);
EXPECT_TRUE(request.contains("customAgents"));
EXPECT_EQ(request["customAgents"].size(), 1);
EXPECT_EQ(request["customAgents"][0]["name"], "analyst");
EXPECT_EQ(request["customAgents"][0]["displayName"], "Data Analyst");
EXPECT_TRUE(request["customAgents"][0].contains("tools"));
EXPECT_EQ(request["customAgents"][0]["tools"][0], "data_tool");
}
TEST(SessionCreateRequestTest, AllConfigOptionsCombined)
{
SessionConfig config;
// Model and session
config.model = "claude-3-opus";
config.session_id = "combined-session";
config.streaming = true;
// System message
SystemMessageConfig sys_msg;
sys_msg.content = "You are a helpful assistant.";
sys_msg.mode = SystemMessageMode::Append;
config.system_message = sys_msg;
// Custom tool
Tool tool;
tool.name = "helper_tool";
tool.description = "Helps with tasks";
tool.parameters_schema = json{
{"type", "object"},
{"properties", {{"task", {{"type", "string"}}}}}};
config.tools = {tool};
// MCP servers
McpLocalServerConfig mcp_local;
mcp_local.type = "local";
mcp_local.command = "node";
mcp_local.args = {"server.js"};
mcp_local.tools = {"tool1", "tool2"};
McpRemoteServerConfig mcp_remote;
mcp_remote.type = "sse";
mcp_remote.url = "http://localhost:8080/sse";
mcp_remote.tools = {"*"};
std::map<std::string, json> mcp_servers;
mcp_servers["local-server"] = mcp_local;
mcp_servers["remote-server"] = mcp_remote;
config.mcp_servers = mcp_servers;
// Custom agents
CustomAgentConfig agent1;
agent1.name = "agent-alpha";
agent1.display_name = "Alpha Agent";
agent1.description = "First agent";
agent1.prompt = "You are alpha.";
CustomAgentConfig agent2;
agent2.name = "agent-beta";
agent2.display_name = "Beta Agent";
agent2.description = "Second agent";
agent2.prompt = "You are beta.";
agent2.infer = false;
agent2.tools = std::vector<std::string>{"read", "write"};
config.custom_agents = std::vector<CustomAgentConfig>{agent1, agent2};
// Tool restrictions
config.available_tools = std::vector<std::string>{"read", "write", "helper_tool"};
config.excluded_tools = std::vector<std::string>{"dangerous_tool"};
// Provider
config.provider = json{{"baseUrl", "https://api.custom.com"}, {"apiKey", "sk-xxx"}};
// Permission handler should flip requestPermission on (handler itself is not serialized)
config.on_permission_request = [](const PermissionRequest&) -> PermissionRequestResult
{
PermissionRequestResult r;
r.kind = "approved";
return r;
};
json request = build_session_create_request(config);
// Verify all fields are present
EXPECT_EQ(request["model"], "claude-3-opus");
EXPECT_EQ(request["sessionId"], "combined-session");
EXPECT_EQ(request["streaming"], true);
EXPECT_TRUE(request.contains("systemMessage"));
EXPECT_EQ(request["systemMessage"]["mode"], "append");
EXPECT_TRUE(request.contains("tools"));
EXPECT_EQ(request["tools"].size(), 1);
EXPECT_EQ(request["tools"][0]["name"], "helper_tool");
EXPECT_TRUE(request.contains("mcpServers"));
EXPECT_EQ(request["mcpServers"].size(), 2);
EXPECT_TRUE(request["mcpServers"].contains("local-server"));
EXPECT_TRUE(request["mcpServers"].contains("remote-server"));
EXPECT_TRUE(request.contains("customAgents"));
EXPECT_EQ(request["customAgents"].size(), 2);
EXPECT_TRUE(request.contains("availableTools"));
EXPECT_EQ(request["availableTools"].size(), 3);
EXPECT_TRUE(request.contains("excludedTools"));
EXPECT_EQ(request["excludedTools"].size(), 1);
EXPECT_TRUE(request.contains("provider"));
EXPECT_EQ(request["provider"]["baseUrl"], "https://api.custom.com");
EXPECT_TRUE(request.contains("requestPermission"));
EXPECT_TRUE(request["requestPermission"].get<bool>());
}
TEST(SessionResumeRequestTest, AllConfigOptionsCombined)
{
ResumeSessionConfig config;
// Streaming
config.streaming = true;
// Custom tool
Tool tool;
tool.name = "resume_tool";
tool.description = "Tool added on resume";
config.tools = {tool};
// MCP servers
McpRemoteServerConfig mcp;
mcp.type = "http";
mcp.url = "https://mcp.example.com/api";
mcp.tools = {"*"};
std::map<std::string, json> mcp_servers;
mcp_servers["resume-mcp"] = mcp;
config.mcp_servers = mcp_servers;
// Custom agents
CustomAgentConfig agent;
agent.name = "resume-agent";
agent.display_name = "Resume Agent";
agent.description = "Agent added on resume";
agent.prompt = "You help with resumed sessions.";
config.custom_agents = std::vector<CustomAgentConfig>{agent};
// Provider
config.provider = json{{"baseUrl", "https://api.resume.com"}};
config.on_permission_request = [](const PermissionRequest&) -> PermissionRequestResult
{
PermissionRequestResult r;
r.kind = "approved";
return r;
};
json request = build_session_resume_request("resume-session-id", config);
// Verify all fields
EXPECT_EQ(request["sessionId"], "resume-session-id");
EXPECT_EQ(request["streaming"], true);
EXPECT_TRUE(request.contains("tools"));
EXPECT_EQ(request["tools"].size(), 1);
EXPECT_EQ(request["tools"][0]["name"], "resume_tool");
EXPECT_TRUE(request.contains("mcpServers"));
EXPECT_TRUE(request["mcpServers"].contains("resume-mcp"));
EXPECT_TRUE(request.contains("customAgents"));
EXPECT_EQ(request["customAgents"].size(), 1);
EXPECT_EQ(request["customAgents"][0]["name"], "resume-agent");
EXPECT_TRUE(request.contains("provider"));
EXPECT_TRUE(request.contains("requestPermission"));
EXPECT_TRUE(request["requestPermission"].get<bool>());
}
TEST(SessionCreateRequestTest, McpServerWithAllOptions)
{
SessionConfig config;
// MCP server with all options
McpLocalServerConfig mcp;
mcp.type = "local";
mcp.command = "python";
mcp.args = {"-m", "mcp_server", "--port", "8080"};
mcp.tools = {"tool_a", "tool_b"};
mcp.timeout = 30000;
mcp.cwd = "/path/to/server";
mcp.env = std::map<std::string, std::string>{
{"API_KEY", "secret"},
{"DEBUG", "true"}};
std::map<std::string, json> mcp_servers;
mcp_servers["full-config-server"] = mcp;
config.mcp_servers = mcp_servers;
json request = build_session_create_request(config);
EXPECT_TRUE(request.contains("mcpServers"));
auto& server = request["mcpServers"]["full-config-server"];
EXPECT_EQ(server["type"], "local");
EXPECT_EQ(server["command"], "python");
EXPECT_EQ(server["args"].size(), 4);
EXPECT_EQ(server["tools"].size(), 2);
EXPECT_EQ(server["timeout"], 30000);
EXPECT_EQ(server["cwd"], "/path/to/server");
EXPECT_TRUE(server.contains("env"));
EXPECT_EQ(server["env"]["API_KEY"], "secret");
}
TEST(SessionCreateRequestTest, CustomAgentWithMcpServers)
{
SessionConfig config;
// Agent-specific MCP server
McpRemoteServerConfig agent_mcp;
agent_mcp.type = "sse";
agent_mcp.url = "http://localhost:9090/sse";
agent_mcp.tools = {"*"};
std::map<std::string, json> agent_mcp_servers;
agent_mcp_servers["agent-mcp"] = agent_mcp;
CustomAgentConfig agent;
agent.name = "mcp-equipped-agent";
agent.display_name = "MCP Agent";
agent.description = "Agent with its own MCP servers";
agent.prompt = "You have access to special MCP tools.";
agent.mcp_servers = agent_mcp_servers;
agent.tools = std::vector<std::string>{"read", "write"};
agent.infer = true;
config.custom_agents = std::vector<CustomAgentConfig>{agent};
json request = build_session_create_request(config);
EXPECT_TRUE(request.contains("customAgents"));
auto& agent_json = request["customAgents"][0];
EXPECT_EQ(agent_json["name"], "mcp-equipped-agent");
EXPECT_TRUE(agent_json.contains("mcpServers"));
EXPECT_TRUE(agent_json["mcpServers"].contains("agent-mcp"));
EXPECT_EQ(agent_json["mcpServers"]["agent-mcp"]["type"], "sse");
EXPECT_TRUE(agent_json.contains("tools"));
EXPECT_EQ(agent_json["infer"], true);
}
// =============================================================================
// URL Parsing Edge Case Tests
// =============================================================================
TEST(ClientOptions, InvalidPortTooHigh)
{
ClientOptions opts;
opts.cli_url = "70000";
opts.use_stdio = false;
// Port 70000 exceeds valid range, falls through to hostname parsing
Client client(opts);
EXPECT_EQ(client.state(), ConnectionState::Disconnected);
}
TEST(ClientOptions, InvalidPortZero)
{
ClientOptions opts;
opts.cli_url = "0";
opts.use_stdio = false;
// Port 0 is not in valid range (1-65535), falls through to hostname parsing
Client client(opts);
EXPECT_EQ(client.state(), ConnectionState::Disconnected);
}
TEST(ClientOptions, InvalidPortNegative)
{
ClientOptions opts;
opts.cli_url = "-1";
opts.use_stdio = false;
// Negative port not valid, falls through to hostname parsing
Client client(opts);
EXPECT_EQ(client.state(), ConnectionState::Disconnected);
}
TEST(ClientOptions, AuthDefaultWithToken)
{
ClientOptions opts;
opts.github_token = "ghp_test123";
opts.auto_start = false;
Client client(opts);
// use_logged_in_user should default to false when github_token is set
EXPECT_EQ(client.state(), ConnectionState::Disconnected);
}
TEST(ClientOptions, AuthDefaultWithoutToken)
{
ClientOptions opts;
opts.auto_start = false;
Client client(opts);
// use_logged_in_user should default to true without token
EXPECT_EQ(client.state(), ConnectionState::Disconnected);
}