forked from 0xeb/fastmcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_types.cpp
More file actions
162 lines (133 loc) · 4.4 KB
/
Copy pathjson_types.cpp
File metadata and controls
162 lines (133 loc) · 4.4 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
#include "fastmcpp/client/types.hpp"
#include "fastmcpp/types.hpp"
#include "fastmcpp/util/json.hpp"
#include <cassert>
#include <iostream>
#include <string>
using fastmcpp::util::json::dump;
using fastmcpp::util::json::dump_pretty;
using fastmcpp::util::json::json;
using fastmcpp::util::json::parse;
void test_icon_serialization()
{
// Icon round-trip
fastmcpp::Icon icon;
icon.src = "https://example.com/icon.png";
icon.mime_type = "image/png";
icon.sizes = std::vector<std::string>{"48x48", "96x96"};
json j = icon;
assert(j["src"] == "https://example.com/icon.png");
assert(j["mimeType"] == "image/png");
assert(j["sizes"].size() == 2);
auto icon2 = j.get<fastmcpp::Icon>();
assert(icon2.src == icon.src);
assert(icon2.mime_type == icon.mime_type);
assert(icon2.sizes->size() == 2);
std::cout << " Icon serialization: PASS\n";
}
void test_toolinfo_title_icons()
{
fastmcpp::client::ToolInfo tool;
tool.name = "my_tool";
tool.title = "My Tool Title";
tool.description = "A test tool";
tool.inputSchema = json{{"type", "object"}};
fastmcpp::Icon icon;
icon.src = "https://example.com/tool.png";
tool.icons = std::vector<fastmcpp::Icon>{icon};
json j = tool;
assert(j["name"] == "my_tool");
assert(j["title"] == "My Tool Title");
assert(j.contains("icons"));
auto tool2 = j.get<fastmcpp::client::ToolInfo>();
assert(tool2.title.has_value());
assert(*tool2.title == "My Tool Title");
assert(tool2.icons.has_value());
std::cout << " ToolInfo title/icons: PASS\n";
}
void test_resourceinfo_title_icons()
{
fastmcpp::client::ResourceInfo res;
res.uri = "file:///test.txt";
res.name = "test.txt";
res.title = "Test File";
fastmcpp::Icon icon;
icon.src = "data:image/png;base64,abc";
res.icons = std::vector<fastmcpp::Icon>{icon};
json j = res;
assert(j["title"] == "Test File");
assert(j.contains("icons"));
auto res2 = j.get<fastmcpp::client::ResourceInfo>();
assert(res2.title.has_value());
assert(res2.icons.has_value());
std::cout << " ResourceInfo title/icons: PASS\n";
}
void test_resourcetemplate_title_icons()
{
fastmcpp::client::ResourceTemplate tmpl;
tmpl.uriTemplate = "file:///{name}";
tmpl.name = "file_template";
tmpl.title = "File Template";
fastmcpp::Icon icon;
icon.src = "/icons/file.svg";
tmpl.icons = std::vector<fastmcpp::Icon>{icon};
json j = tmpl;
assert(j["title"] == "File Template");
assert(j.contains("icons"));
auto tmpl2 = j.get<fastmcpp::client::ResourceTemplate>();
assert(tmpl2.title.has_value());
assert(tmpl2.icons.has_value());
std::cout << " ResourceTemplate title/icons: PASS\n";
}
void test_promptinfo_title_icons()
{
fastmcpp::client::PromptInfo prompt;
prompt.name = "code_review";
prompt.title = "Code Review Prompt";
fastmcpp::Icon icon;
icon.src = "https://example.com/review.png";
prompt.icons = std::vector<fastmcpp::Icon>{icon};
json j = prompt;
assert(j["title"] == "Code Review Prompt");
assert(j.contains("icons"));
auto prompt2 = j.get<fastmcpp::client::PromptInfo>();
assert(prompt2.title.has_value());
assert(prompt2.icons.has_value());
std::cout << " PromptInfo title/icons: PASS\n";
}
void test_types_without_optional_fields()
{
fastmcpp::client::ToolInfo tool;
tool.name = "simple";
tool.inputSchema = json{{"type", "object"}};
json j = tool;
assert(!j.contains("title"));
assert(!j.contains("icons"));
auto tool2 = j.get<fastmcpp::client::ToolInfo>();
assert(!tool2.title.has_value());
assert(!tool2.icons.has_value());
std::cout << " Types without optional fields: PASS\n";
}
int main()
{
std::cout << "Testing JSON type serialization:\n";
auto j = parse("{\"a\":1,\"b\":[true,\"x\"]}");
assert(j["a"].get<int>() == 1);
auto s = dump(j);
auto sp = dump_pretty(j, 2);
assert(!s.empty());
assert(!sp.empty());
fastmcpp::Id id{"abc"};
json jid = id;
auto id2 = jid.get<fastmcpp::Id>();
assert(id2.value == "abc");
std::cout << " Basic JSON operations: PASS\n";
test_icon_serialization();
test_toolinfo_title_icons();
test_resourceinfo_title_icons();
test_resourcetemplate_title_icons();
test_promptinfo_title_icons();
test_types_without_optional_fields();
std::cout << "All JSON type tests PASSED!\n";
return 0;
}