Debian Codegraph @

src/unittest/test.cpp

raw symbols SCIP
1 /************************************************************************************
2 *
3 * D++, A Lightweight C++ library for Discord
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 * Copyright 2021 Craig Edwards and D++ contributors
7 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 ************************************************************************************/
22 #include "test.h"
23
24 #include <dpp/dpp.h>
25 #include <dpp/unicode_emoji.h>
26 #include <dpp/restrequest.h>
27 #include <dpp/json.h>
28
29 /**
30 * @brief global lock for log output
31 */
32 std::mutex loglock;
33
34 /**
35 * @brief Type trait to check if a certain type has a build_json method
36 *
37 * @tparam T type to check for
38 */
39 template <typename T, typename = std::void_t<>>
40 struct has_build_json : std::false_type {};
41
42 template <typename T>
43 struct has_build_json<T, std::void_t<decltype(std::declval<T&>().build_json())>> : std::true_type {};
44
45 /**
46 * @brief Type trait to check if a certain type has a build_json method
47 *
48 * @tparam T type to check for
49 */
50 template <typename T>
51 constexpr bool has_build_json_v = has_build_json<T>::value;
52
53 /**
54 * @brief Type trait to check if a certain type has a fill_from_json method
55 *
56 * @tparam T type to check for
57 */
58 template <typename T, typename = void>
59 struct has_fill_from_json : std::false_type {};
60
61 template <typename T>
62 struct has_fill_from_json<T, std::void_t<decltype(std::declval<T&>().fill_from_json(std::declval<dpp::json*>()))>> : std::true_type {};
63
64 /**
65 * @brief Type trait to check if a certain type has a fill_from_json method
66 *
67 * @tparam T type to check for
68 */
69 template <typename T>
70 constexpr bool has_fill_from_json_v = has_fill_from_json<T>::value;
71
72 /* Unit tests go here */
73 int main(int argc, char *argv[])
74 {
75 try {
76
77 std::string token(get_token());
78
79 std::cout << "[" << std::fixed << std::setprecision(3) << (dpp::utility::time_f() - get_start_time()) << "]: [\u001b[36mSTART\u001b[0m] ";
80 if (offline) {
81 std::cout << "Running offline unit tests only.\n";
82 } else {
83 if (argc > 1 && std::find_if(argv + 1, argv + argc, [](const char *a) { return (std::strcmp(a, "full") == 0); }) != argv + argc) {
84 extended = true;
85 }
86 std::cout << "Running offline and " << (extended ? "extended" : "limited") << " online unit tests. Guild ID: " << TEST_GUILD_ID << " Text Channel ID: " << TEST_TEXT_CHANNEL_ID << " VC ID: " << TEST_VC_ID << " User ID: " << TEST_USER_ID << " Event ID: " << TEST_EVENT_ID << "\n";
87 }
88
89 std::string test_to_escape = "*** _This is a test_ ***\n```cpp\n\
90 int main() {\n\
91 /* Comment */\n\
92 int answer = 42;\n\
93 return answer; // ___\n\
94 };\n\
95 ```\n\
96 Markdown lol ||spoiler|| ~~strikethrough~~ `small *code* block`\n";
97
98 set_test(COMPARISON, false);
99 dpp::user u1;
100 dpp::user u2;
101 dpp::user u3;
102 u1.id = u2.id = 666;
103 u3.id = 777;
104 set_test(COMPARISON, u1 == u2 && u1 != u3);
105
106 set_test(BIGNUM, false);
107 std::string big_in{"1234567890123456789012345678901234567890"};
108 dpp::bignumber big(big_in);
109 std::string returned = big.get_number();
110 set_test(BIGNUM, big_in == returned);
111
112 set_test(BIGNUM2, false);
113 std::vector<uint64_t> vec{0xff00ff00ff00ff00, 0x1122334455667788};
114 dpp::bignumber big2(vec);
115 returned = big2.get_number(true);
116 set_test(BIGNUM2, dpp::lowercase(returned) == "1122334455667788ff00ff00ff00ff00");
117
118 set_test(BIGNUM3, false);
119 std::vector<uint64_t> ret_bin = big2.get_binary();
120 set_test(BIGNUM3, ret_bin.size() == 2 && ret_bin[0] == 0xff00ff00ff00ff00 && ret_bin[1] == 0x1122334455667788);
121
122 set_test(ERRORS, false);
123
124 /* Prepare a confirmation_callback_t in error state (400) */
125 dpp::confirmation_callback_t error_test;
126 bool error_message_success = false;
127 error_test.http_info.status = 400;
128
129 error_test.http_info.body = "{\
130 \"message\": \"Invalid Form Body\",\
131 \"code\": 50035,\
132 \"errors\": {\
133 \"options\": {\
134 \"0\": {\
135 \"name\": {\
136 \"_errors\": [\
137 {\
138 \"code\": \"STRING_TYPE_REGEX\",\
139 \"message\": \"String value did not match validation regex.\"\
140 },\
141 {\
142 \"code\": \"APPLICATION_COMMAND_INVALID_NAME\",\
143 \"message\": \"Command name is invalid\"\
144 }\
145 ]\
146 }\
147 }\
148 }\
149 }\
150 }";
151 error_message_success = (error_test.get_error().human_readable == "50035: Invalid Form Body\n\t- options[0].name: String value did not match validation regex. (STRING_TYPE_REGEX)\n\t- options[0].name: Command name is invalid (APPLICATION_COMMAND_INVALID_NAME)");
152
153 error_test.http_info.body = "{\
154 \"message\": \"Invalid Form Body\",\
155 \"code\": 50035,\
156 \"errors\": {\
157 \"type\": {\
158 \"_errors\": [\
159 {\
160 \"code\": \"BASE_TYPE_CHOICES\",\
161 \"message\": \"Value must be one of {4, 5, 9, 10, 11}.\"\
162 }\
163 ]\
164 }\
165 }\
166 }";
167 error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - type: Value must be one of {4, 5, 9, 10, 11}. (BASE_TYPE_CHOICES)");
168
169 error_test.http_info.body = "{\
170 \"message\": \"Unknown Guild\",\
171 \"code\": 10004\
172 }";
173 error_message_success = (error_message_success && error_test.get_error().human_readable == "10004: Unknown Guild");
174
175 error_test.http_info.body = "{\
176 \"message\": \"Invalid Form Body\",\
177 \"code\": 50035,\
178 \"errors\": {\
179 \"allowed_mentions\": {\
180 \"_errors\": [\
181 {\
182 \"code\": \"MESSAGE_ALLOWED_MENTIONS_PARSE_EXCLUSIVE\",\
183 \"message\": \"parse:[\\\"users\\\"] and users: [ids...] are mutually exclusive.\"\
184 }\
185 ]\
186 }\
187 }\
188 }";
189 error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - allowed_mentions: parse:[\"users\"] and users: [ids...] are mutually exclusive. (MESSAGE_ALLOWED_MENTIONS_PARSE_EXCLUSIVE)");
190
191 error_test.http_info.body = "{\
192 \"message\": \"Invalid Form Body\",\
193 \"code\": 50035,\
194 \"errors\": {\
195 \"1\": {\
196 \"options\": {\
197 \"1\": {\
198 \"description\": {\
199 \"_errors\": [\
200 {\
201 \"code\": \"BASE_TYPE_BAD_LENGTH\",\
202 \"message\": \"Must be between 1 and 100 in length.\"\
203 }\
204 ]\
205 }\
206 }\
207 }\
208 }\
209 }\
210 }";
211 error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - <array>[1].options[1].description: Must be between 1 and 100 in length. (BASE_TYPE_BAD_LENGTH)");
212
213 error_test.http_info.body = "{\
214 \"message\": \"Invalid Form Body\",\
215 \"code\": 50035,\
216 \"errors\": {\
217 \"data\": {\
218 \"poll\": {\
219 \"_errors\": [\
220 {\
221 \"code\": \"POLL_TYPE_QUESTION_ALLOWS_TEXT_ONLY\",\
222 \"message\": \"This poll type cannot include attachments, emoji or stickers with the question\"}\
223 ]\
224 }\
225 }\
226 }\
227 }";
228 error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - data.poll: This poll type cannot include attachments, emoji or stickers with the question (POLL_TYPE_QUESTION_ALLOWS_TEXT_ONLY)");
229
230 set_test(ERRORS, error_message_success);
231
232 set_test(MD_ESC_1, false);
233 set_test(MD_ESC_2, false);
234 std::string escaped1 = dpp::utility::markdown_escape(test_to_escape);
235 std::string escaped2 = dpp::utility::markdown_escape(test_to_escape, true);
236 set_test(MD_ESC_1, escaped1 == "\\*\\*\\* \\_This is a test\\_ \\*\\*\\*\n\
237 ```cpp\n\
238 int main() {\n\
239 /* Comment */\n\
240 int answer = 42;\n\
241 return answer; // ___\n\
242 };\n\
243 ```\n\
244 Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ `small *code* block`\n");
245 set_test(MD_ESC_2, escaped2 == "\\*\\*\\* \\_This is a test\\_ \\*\\*\\*\n\
246 \\`\\`\\`cpp\n\
247 int main\\(\\) {\n\
248 /\\* Comment \\*/\n\
249 int answer = 42;\n\
250 return answer; // \\_\\_\\_\n\
251 };\n\
252 \\`\\`\\`\n\
253 Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* block\\`\n");
254
255 set_test(URLENC, false);
256 set_test(URLENC, dpp::utility::url_encode("ABC123_+\\|$*/AAA[]😄") == "ABC123_%2B%5C%7C%24%2A%2FAAA%5B%5D%F0%9F%98%84");
257
258 set_test(BASE64ENC, false);
259 set_test(BASE64ENC,
260 dpp::base64_encode(reinterpret_cast<unsigned char const *>("a"), 1) == "YQ==" &&
261 dpp::base64_encode(reinterpret_cast<unsigned char const *>("bc"), 2) == "YmM=" &&
262 dpp::base64_encode(reinterpret_cast<unsigned char const *>("def"), 3) == "ZGVm" &&
263 dpp::base64_encode(reinterpret_cast<unsigned char const *>("ghij"), 4) == "Z2hpag==" &&
264 dpp::base64_encode(reinterpret_cast<unsigned char const *>("klmno"), 5) == "a2xtbm8=" &&
265 dpp::base64_encode(reinterpret_cast<unsigned char const *>("pqrstu"), 6) == "cHFyc3R1" &&
266 dpp::base64_encode(reinterpret_cast<unsigned char const *>("vwxyz12"), 7) == "dnd4eXoxMg=="
267 );
268
269 dpp::http_connect_info hci;
270 set_test(HOSTINFO, false);
271
272 hci = dpp::https_client::get_host_info("https://test.com:444");
273 bool hci_test = (hci.scheme == "https" && hci.hostname == "test.com" && hci.port == 444 && hci.is_ssl == true);
274
275 hci = dpp::https_client::get_host_info("https://test.com");
276 hci_test = hci_test && (hci.scheme == "https" && hci.hostname == "test.com" && hci.port == 443 && hci.is_ssl == true);
277
278 hci = dpp::https_client::get_host_info("http://test.com");
279 hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 80 && hci.is_ssl == false);
280
281 hci = dpp::https_client::get_host_info("http://test.com:90");
282 hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 90 && hci.is_ssl == false);
283
284 hci = dpp::https_client::get_host_info("test.com:97");
285 hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 97 && hci.is_ssl == false);
286
287 hci = dpp::https_client::get_host_info("test.com");
288 hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 80 && hci.is_ssl == false);
289
290 set_test(HOSTINFO, hci_test);
291
292 std::vector<uint8_t> testaudio = load_test_audio();
293
294 set_test(READFILE, false);
295 std::string rf_test = dpp::utility::read_file(SHARED_OBJECT);
296 FILE *fp = fopen(SHARED_OBJECT, "rb");
297 fseek(fp, 0, SEEK_END);
298 size_t off = (size_t) ftell(fp);
299 fclose(fp);
300 set_test(READFILE, off == rf_test.length());
301
302 set_test(TIMESTAMPTOSTRING, false);
303 set_test(TIMESTAMPTOSTRING, dpp::ts_to_string(1642611864) == "2022-01-19T17:04:24Z");
304
305 {
306 set_test(ROLE_COMPARE, false);
307 dpp::role role_1, role_2;
308 role_1.id = 99;
309 role_1.position = 1;
310 role_1.guild_id = 123;
311 role_1.id = 98;
312 role_2.position = 2;
313 role_2.guild_id = 123;
314 set_test(ROLE_COMPARE, role_1 < role_2 && !(role_1 > role_2) && role_1 != role_2);
315 }
316 {
317 set_test(ROLE_COMPARE_CONSIDERING_ID, false);
318 dpp::role role_1, role_2;
319 role_1.id = 99;
320 role_1.position = 2;
321 role_1.guild_id = 123;
322 role_2.id = 98;
323 role_2.position = 2;
324 role_2.guild_id = 123;
325 set_test(ROLE_COMPARE_CONSIDERING_ID, role_1 < role_2 && !(role_1 > role_2));
326 }
327
328 set_test(WEBHOOK, false);
329 try {
330 dpp::webhook test_wh("https://discord.com/api/webhooks/833047646548133537/ntCHEYYIoHSLy_GOxPx6pmM0sUoLbP101ct-WI6F-S4beAV2vaIcl_Id5loAMyQwxqhE");
331 set_test(WEBHOOK, (test_wh.token == "ntCHEYYIoHSLy_GOxPx6pmM0sUoLbP101ct-WI6F-S4beAV2vaIcl_Id5loAMyQwxqhE") && (test_wh.id == dpp::snowflake(833047646548133537)));
332 }
333 catch (const dpp::exception &) {
334 set_test(WEBHOOK, false);
335 }
336
337 { // test dpp::snowflake
338 start_test(SNOWFLAKE);
339 bool success = true;
340 dpp::snowflake s = 69420;
341 json j;
342 j["value"] = s;
343 success = dpp::snowflake_not_null(&j, "value") == 69420 && success;
344 DPP_CHECK_CONSTRUCT_ASSIGN(SNOWFLAKE, dpp::snowflake, success);
345 s = 42069;
346 success = success && (s == 42069 && s == dpp::snowflake{42069} && s == "42069");
347 success = success && (dpp::snowflake{69} < dpp::snowflake{420} && (dpp::snowflake{69} < 420));
348 s = "69420";
349 success = success && s == 69420;
350 auto conversion_test = [](dpp::snowflake sl) {
351 return sl.str();
352 };
353 s = conversion_test(std::string{"1337"});
354 success = success && s == 1337; /* THIS BREAKS (and i do not care very much): && s == conversion_test(dpp::snowflake{"1337"}); */
355 success = success && dpp::snowflake{0} == 0;
356 set_test(SNOWFLAKE, success);
357 }
358
359 { // test snowflake: std::format support
360
361 #ifdef DPP_FORMATTERS
362 set_test(SNOWFLAKE_STD_FORMAT,
363 std::format("{}",dpp::snowflake{}) == "0" &&
364 std::format("{}",dpp::snowflake{12345}) == "12345" &&
365 std::format("{} hello {}", dpp::snowflake{12345}, dpp::snowflake{54321}) == "12345 hello 54321"
366 );
367 #else
368 set_status(SNOWFLAKE_STD_FORMAT, ts_skipped);
369 #endif // DPP_FORMATTERS
370 };
371
372 { // test dpp::json_interface
373 start_test(JSON_INTERFACE);
374 struct fillable : dpp::json_interface<fillable> {
375 fillable &fill_from_json_impl(dpp::json *) {
376 return *this;
377 }
378 };
379 struct buildable : dpp::json_interface<buildable> {
380 json to_json_impl(bool = false) const {
381 return {};
382 }
383 };
384 struct fillable_and_buildable : dpp::json_interface<fillable_and_buildable> {
385 fillable_and_buildable &fill_from_json_impl(dpp::json *) {
386 return *this;
387 }
388
389 json to_json_impl(bool = false) const {
390 return {};
391 }
392 };
393 bool success = true;
394
395 DPP_CHECK(JSON_INTERFACE, has_build_json_v<dpp::json_interface<buildable>>, success);
396 DPP_CHECK(JSON_INTERFACE, !has_fill_from_json_v<dpp::json_interface<buildable>>, success);
397 DPP_CHECK(JSON_INTERFACE, has_build_json_v<buildable>, success);
398 DPP_CHECK(JSON_INTERFACE, !has_fill_from_json_v<buildable>, success);
399
400 DPP_CHECK(JSON_INTERFACE, !has_build_json_v<dpp::json_interface<fillable>>, success);
401 DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v<dpp::json_interface<fillable>>, success);
402 DPP_CHECK(JSON_INTERFACE, !has_build_json_v<fillable>, success);
403 DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v<fillable>, success);
404
405 DPP_CHECK(JSON_INTERFACE, has_build_json_v<dpp::json_interface<fillable_and_buildable>>, success);
406 DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v<dpp::json_interface<fillable_and_buildable>>, success);
407 DPP_CHECK(JSON_INTERFACE, has_build_json_v<fillable_and_buildable>, success);
408 DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v<fillable_and_buildable>, success);
409 set_test(JSON_INTERFACE, success);
410 }
411
412 { // test interaction_create_t::get_parameter
413 // create a fake interaction
414 dpp::cluster cluster("");
415 dpp::discord_client client(&cluster, 1, 1, "");
416 dpp::interaction_create_t interaction(nullptr, 0, "");
417
418 /* Check the method with subcommands */
419 set_test(GET_PARAMETER_WITH_SUBCOMMANDS, false);
420
421 dpp::command_interaction cmd_data; // command
422 cmd_data.type = dpp::ctxm_chat_input;
423 cmd_data.name = "command";
424
425 dpp::command_data_option subcommandgroup; // subcommand group
426 subcommandgroup.name = "group";
427 subcommandgroup.type = dpp::co_sub_command_group;
428
429 dpp::command_data_option subcommand; // subcommand
430 subcommand.name = "add";
431 subcommand.type = dpp::co_sub_command;
432
433 dpp::command_data_option option1; // slashcommand option
434 option1.name = "user";
435 option1.type = dpp::co_user;
436 option1.value = dpp::snowflake(189759562910400512);
437
438 dpp::command_data_option option2; // slashcommand option
439 option2.name = "checked";
440 option2.type = dpp::co_boolean;
441 option2.value = true;
442
443 // add them
444 subcommand.options.push_back(option1);
445 subcommand.options.push_back(option2);
446 subcommandgroup.options.push_back(subcommand);
447 cmd_data.options.push_back(subcommandgroup);
448 interaction.command.data = cmd_data;
449
450 dpp::snowflake value1 = std::get<dpp::snowflake>(interaction.get_parameter("user"));
451 set_test(GET_PARAMETER_WITH_SUBCOMMANDS, value1 == dpp::snowflake(189759562910400512));
452
453 /* Check the method without subcommands */
454 set_test(GET_PARAMETER_WITHOUT_SUBCOMMANDS, false);
455
456 dpp::command_interaction cmd_data2; // command
457 cmd_data2.type = dpp::ctxm_chat_input;
458 cmd_data2.name = "command";
459
460 dpp::command_data_option option3; // slashcommand option
461 option3.name = "number";
462 option3.type = dpp::co_integer;
463 option3.value = int64_t(123456);
464
465 cmd_data2.options.push_back(option3);
466 interaction.command.data = cmd_data2;
467
468 int64_t value2 = std::get<int64_t>(interaction.get_parameter("number"));
469 set_test(GET_PARAMETER_WITHOUT_SUBCOMMANDS, value2 == 123456);
470 }
471
472 { // test dpp::command_option_choice::fill_from_json
473 set_test(OPTCHOICE_DOUBLE, false);
474 set_test(OPTCHOICE_INT, false);
475 set_test(OPTCHOICE_BOOL, false);
476 set_test(OPTCHOICE_SNOWFLAKE, false);
477 set_test(OPTCHOICE_STRING, false);
478 json j;
479 dpp::command_option_choice choice;
480 j["value"] = 54.321;
481 choice.fill_from_json(&j);
482 bool success_double = std::holds_alternative<double>(choice.value);
483 j["value"] = 8223372036854775807;
484 choice.fill_from_json(&j);
485 bool success_int = std::holds_alternative<int64_t>(choice.value);
486 j["value"] = -8223372036854775807;
487 choice.fill_from_json(&j);
488 bool success_int2 = std::holds_alternative<int64_t>(choice.value);
489 j["value"] = true;
490 choice.fill_from_json(&j);
491 bool success_bool = std::holds_alternative<bool>(choice.value);
492 dpp::snowflake s(845266178036516757); // example snowflake
493 j["value"] = s;
494 choice.fill_from_json(&j);
495 bool success_snowflake = std::holds_alternative<dpp::snowflake>(choice.value) && std::get<dpp::snowflake>(choice.value) == s;
496 j["value"] = "foobar";
497 choice.fill_from_json(&j);
498 bool success_string = std::holds_alternative<std::string>(choice.value);
499 set_test(OPTCHOICE_DOUBLE, success_double);
500 set_test(OPTCHOICE_INT, success_int && success_int2);
501 set_test(OPTCHOICE_BOOL, success_bool);
502 set_test(OPTCHOICE_SNOWFLAKE, success_snowflake);
503 set_test(OPTCHOICE_STRING, success_string);
504 }
505
506 {
507 set_test(PERMISSION_CLASS, false);
508 bool success = false;
509 auto p = dpp::permission();
510 p = 16;
511 success = p == 16;
512 p |= 4;
513 success = p == 20 && success;
514 p <<= 8; // left shift
515 success = p == 5120 && success;
516 auto s = std::to_string(p);
517 success = s == "5120" && success;
518 p.set(0).add(~uint64_t{0}).remove(dpp::p_speak).set(dpp::p_administrator);
519 success = !p.has(dpp::p_administrator, dpp::p_ban_members) && success; // must return false because they're not both set
520 success = !p.has(dpp::p_administrator | dpp::p_ban_members) && success;
521 success = p.can(dpp::p_ban_members) && success;
522 success = p.can(dpp::p_speak) && success;
523
524 constexpr auto permission_test = [](dpp::permission p) constexpr noexcept {
525 bool success{true};
526
527 p.set(0).add(~uint64_t{0}).remove(dpp::p_speak).set(dpp::p_connect);
528 p.set(dpp::p_administrator, dpp::p_ban_members);
529 success = p.has(dpp::p_administrator) && success;
530 success = p.has(dpp::p_administrator) && p.has(dpp::p_ban_members) && success;
531 success = p.has(dpp::p_administrator, dpp::p_ban_members) && success;
532 success = p.has(dpp::p_administrator | dpp::p_ban_members) && success;
533 success = p.add(dpp::p_speak).has(dpp::p_administrator, dpp::p_speak) && success;
534 success = !p.remove(dpp::p_speak).has(dpp::p_administrator, dpp::p_speak) && success;
535 p.remove(dpp::p_administrator);
536 success = p.can(dpp::p_ban_members) && success;
537 success = !p.can(dpp::p_speak, dpp::p_ban_members) && success;
538 success = p.can_any(dpp::p_speak, dpp::p_ban_members) && success;
539 return success;
540 };
541 constexpr auto constexpr_success = permission_test({~uint64_t{0}}); // test in constant evaluated
542 success = permission_test({~uint64_t{0}}) && constexpr_success && success; // test at runtime
543 set_test(PERMISSION_CLASS, success);
544 }
545
546 { // dpp event classes
547 start_test(EVENT_CLASS);
548 bool success = true;
549 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::log_t, success);
550 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_scheduled_event_user_add_t, success);
551 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_scheduled_event_user_remove_t, success);
552 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_scheduled_event_create_t, success);
553 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_scheduled_event_update_t, success);
554 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_scheduled_event_delete_t, success);
555 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::automod_rule_create_t, success);
556 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::automod_rule_update_t, success);
557 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::automod_rule_delete_t, success);
558 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::automod_rule_execute_t, success);
559 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::stage_instance_create_t, success);
560 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::stage_instance_update_t, success);
561 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::stage_instance_delete_t, success);
562 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::voice_channel_effect_send_t, success);
563 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::voice_state_update_t, success);
564 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::interaction_create_t, success);
565 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::slashcommand_t, success);
566 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::button_click_t, success);
567 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::form_submit_t, success);
568 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::autocomplete_t, success);
569 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::context_menu_t, success);
570 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::message_context_menu_t, success);
571 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::user_context_menu_t, success);
572 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::select_click_t, success);
573 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_delete_t, success);
574 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_stickers_update_t, success);
575 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_join_request_delete_t, success);
576 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::channel_delete_t, success);
577 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::channel_update_t, success);
578 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::ready_t, success);
579 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::message_delete_t, success);
580 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_member_remove_t, success);
581 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::resumed_t, success);
582 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_role_create_t, success);
583 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::typing_start_t, success);
584 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::voice_track_marker_t, success);
585 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::message_reaction_add_t, success);
586 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_members_chunk_t, success);
587 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::message_reaction_remove_t, success);
588 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_create_t, success);
589 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::channel_create_t, success);
590 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::message_reaction_remove_emoji_t, success);
591 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::message_delete_bulk_t, success);
592 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_role_update_t, success);
593 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_role_delete_t, success);
594 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::channel_pins_update_t, success);
595 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::message_reaction_remove_all_t, success);
596 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::voice_server_update_t, success);
597 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_emojis_update_t, success);
598 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::presence_update_t, success);
599 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::webhooks_update_t, success);
600 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_member_add_t, success);
601 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::invite_delete_t, success);
602 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_update_t, success);
603 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_integrations_update_t, success);
604 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_member_update_t, success);
605 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::invite_create_t, success);
606 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::message_update_t, success);
607 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::user_update_t, success);
608 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::message_create_t, success);
609 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_audit_log_entry_create_t, success);
610 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_ban_add_t, success);
611 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::guild_ban_remove_t, success);
612 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::integration_create_t, success);
613 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::integration_update_t, success);
614 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::integration_delete_t, success);
615 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::thread_create_t, success);
616 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::thread_update_t, success);
617 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::thread_delete_t, success);
618 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::thread_list_sync_t, success);
619 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::thread_member_update_t, success);
620 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::thread_members_update_t, success);
621 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::voice_buffer_send_t, success);
622 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::voice_ready_t, success);
623 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::voice_receive_t, success);
624 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::voice_client_speaking_t, success);
625 DPP_CHECK_CONSTRUCT_ASSIGN(EVENT_CLASS, dpp::voice_client_disconnect_t, success);
626 set_test(EVENT_CLASS, success);
627 }
628
629
630 { // some dpp::user methods
631 dpp::user user1;
632 user1.id = 189759562910400512;
633 user1.discriminator = 0001;
634 user1.username = "brain";
635
636 set_test(USER_GET_MENTION, false);
637 set_test(USER_GET_MENTION, user1.get_mention() == "<@189759562910400512>");
638
639 set_test(USER_FORMAT_USERNAME, false);
640 set_test(USER_FORMAT_USERNAME, user1.format_username() == "brain#0001");
641
642 set_test(USER_GET_CREATION_TIME, false);
643 set_test(USER_GET_CREATION_TIME, (uint64_t) user1.get_creation_time() == 1465312605);
644
645 set_test(USER_GET_URL, false);
646
647 dpp::user user2;
648 set_test(USER_GET_URL,
649 user1.get_url() == dpp::utility::url_host + "/users/189759562910400512" &&
650 user2.get_url() == ""
651 );
652 }
653
654 { // avatar size function
655 set_test(UTILITY_AVATAR_SIZE, false);
656 bool success = false;
657 success = dpp::utility::avatar_size(0).empty();
658 success = dpp::utility::avatar_size(16) == "?size=16" && success;
659 success = dpp::utility::avatar_size(256) == "?size=256" && success;
660 success = dpp::utility::avatar_size(4096) == "?size=4096" && success;
661 success = dpp::utility::avatar_size(8192).empty() && success;
662 success = dpp::utility::avatar_size(3000).empty() && success;
663 set_test(UTILITY_AVATAR_SIZE, success);
664 }
665
666 { // cdn endpoint url getter
667 set_test(UTILITY_CDN_ENDPOINT_URL_HASH, false);
668 bool success = false;
669 success = dpp::utility::cdn_endpoint_url_hash({dpp::i_png}, "foobar/test", "", dpp::i_jpg, 0).empty();
670 success = dpp::utility::cdn_endpoint_url_hash({dpp::i_png}, "foobar/test", "", dpp::i_png, 0) == "https://cdn.discordapp.com/foobar/test.png" && success;
671 success = dpp::utility::cdn_endpoint_url_hash({dpp::i_png}, "foobar/test", "", dpp::i_png, 128) == "https://cdn.discordapp.com/foobar/test.png?size=128" && success;
672 success = dpp::utility::cdn_endpoint_url_hash({dpp::i_png, dpp::i_gif}, "foobar/test", "12345", dpp::i_gif, 0, false, true) == "https://cdn.discordapp.com/foobar/test/a_12345.gif" && success;
673 success = dpp::utility::cdn_endpoint_url_hash({dpp::i_png, dpp::i_gif}, "foobar/test", "12345", dpp::i_png, 0, false, true) == "https://cdn.discordapp.com/foobar/test/a_12345.png" && success;
674 success = dpp::utility::cdn_endpoint_url_hash({dpp::i_png, dpp::i_gif}, "foobar/test", "12345", dpp::i_png, 0, false, false) == "https://cdn.discordapp.com/foobar/test/12345.png" && success;
675 success = dpp::utility::cdn_endpoint_url_hash({dpp::i_png, dpp::i_gif}, "foobar/test", "12345", dpp::i_png, 0, true, true) == "https://cdn.discordapp.com/foobar/test/a_12345.gif" && success;
676 success = dpp::utility::cdn_endpoint_url_hash({dpp::i_png, dpp::i_gif}, "foobar/test", "", dpp::i_png, 0, true, true) == "https://cdn.discordapp.com/foobar/test.gif" && success;
677 success = dpp::utility::cdn_endpoint_url_hash({dpp::i_png, dpp::i_gif}, "foobar/test", "", dpp::i_gif, 0, false, false).empty() && success;
678 set_test(UTILITY_CDN_ENDPOINT_URL_HASH, success);
679 }
680
681 { // sticker url getter
682 set_test(STICKER_GET_URL, false);
683 dpp::sticker s;
684 s.format_type = dpp::sf_png;
685 bool success = s.get_url().empty();
686 s.id = 12345;
687 success = s.get_url() == "https://cdn.discordapp.com/stickers/12345.png" && success;
688 s.format_type = dpp::sf_gif;
689 success = s.get_url() == "https://cdn.discordapp.com/stickers/12345.gif" && success;
690 s.format_type = dpp::sf_lottie;
691 success = s.get_url() == "https://cdn.discordapp.com/stickers/12345.json" && success;
692 set_test(STICKER_GET_URL, success);
693 }
694
695 { // user url getter
696 dpp::user user1;
697 user1.id = 189759562910400512;
698 user1.username = "Brain";
699 user1.discriminator = 0001;
700
701 auto user2 = user1;
702 user2.avatar = "5532c6414c70765a28cf9448c117205f";
703
704 auto user3 = user2;
705 user3.flags |= dpp::u_animated_icon;
706
707 set_test(USER_GET_AVATAR_URL, false);
708 set_test(USER_GET_AVATAR_URL,
709 dpp::user().get_avatar_url().empty() &&
710 user1.get_avatar_url() == dpp::utility::cdn_host + "/embed/avatars/1.png" &&
711 user2.get_avatar_url() == dpp::utility::cdn_host + "/avatars/189759562910400512/5532c6414c70765a28cf9448c117205f.png" &&
712 user2.get_avatar_url(0, dpp::i_webp) == dpp::utility::cdn_host + "/avatars/189759562910400512/5532c6414c70765a28cf9448c117205f.webp" &&
713 user2.get_avatar_url(0, dpp::i_jpg) == dpp::utility::cdn_host + "/avatars/189759562910400512/5532c6414c70765a28cf9448c117205f.jpg" &&
714 user3.get_avatar_url() == dpp::utility::cdn_host + "/avatars/189759562910400512/a_5532c6414c70765a28cf9448c117205f.gif" &&
715 user3.get_avatar_url(4096, dpp::i_gif) == dpp::utility::cdn_host + "/avatars/189759562910400512/a_5532c6414c70765a28cf9448c117205f.gif?size=4096" &&
716 user3.get_avatar_url(512, dpp::i_webp) == dpp::utility::cdn_host + "/avatars/189759562910400512/a_5532c6414c70765a28cf9448c117205f.gif?size=512" &&
717 user3.get_avatar_url(512, dpp::i_jpg) == dpp::utility::cdn_host + "/avatars/189759562910400512/a_5532c6414c70765a28cf9448c117205f.gif?size=512" &&
718 user3.get_avatar_url(16, dpp::i_jpg, false) == dpp::utility::cdn_host + "/avatars/189759562910400512/a_5532c6414c70765a28cf9448c117205f.jpg?size=16" &&
719 user3.get_avatar_url(5000) == dpp::utility::cdn_host + "/avatars/189759562910400512/a_5532c6414c70765a28cf9448c117205f.gif"
720 );
721 }
722
723 { // emoji url getter
724 dpp::emoji emoji;
725 emoji.id = 825407338755653641;
726
727 set_test(EMOJI_GET_URL, false);
728 set_test(EMOJI_GET_URL, emoji.get_url() == dpp::utility::cdn_host + "/emojis/825407338755653641.png");
729 }
730
731 { // message methods
732 dpp::message m;
733 m.guild_id = 825407338755653642;
734 m.channel_id = 956230231277072415;
735 m.id = 1151617986541666386;
736
737 dpp::message m2;
738 m2.guild_id = 825407338755653642;
739 m2.channel_id = 956230231277072415;
740
741 dpp::message m3;
742 m3.guild_id = 825407338755653642;
743 m3.id = 1151617986541666386;
744
745 dpp::message m4;
746 m4.channel_id = 956230231277072415;
747 m4.id = 1151617986541666386;
748
749 dpp::message m5;
750 m5.guild_id = 825407338755653642;
751
752 dpp::message m6;
753 m6.channel_id = 956230231277072415;
754
755 dpp::message m7;
756 m7.id = 1151617986541666386;
757
758 dpp::message m8;
759
760 set_test(MESSAGE_GET_URL, false);
761 set_test(MESSAGE_GET_URL,
762 m.get_url() == dpp::utility::url_host + "/channels/825407338755653642/956230231277072415/1151617986541666386" &&
763 m2.get_url() == "" &&
764 m3.get_url() == "" &&
765 m4.get_url() == "" &&
766 m5.get_url() == "" &&
767 m6.get_url() == "" &&
768 m7.get_url() == "" &&
769 m8.get_url() == ""
770 );
771 }
772
773 { // channel methods
774 set_test(CHANNEL_SET_TYPE, false);
775 dpp::channel c;
776 c.set_flags(dpp::c_nsfw | dpp::c_video_quality_720p);
777 c.set_type(dpp::CHANNEL_CATEGORY);
778 bool before = c.is_category() && !c.is_forum();
779 c.set_type(dpp::CHANNEL_FORUM);
780 bool after = !c.is_category() && c.is_forum();
781 set_test(CHANNEL_SET_TYPE, before && after);
782
783 set_test(CHANNEL_GET_MENTION, false);
784 c.id = 825411707521728511;
785 set_test(CHANNEL_GET_MENTION, c.get_mention() == "<#825411707521728511>");
786
787 set_test(CHANNEL_GET_URL, false);
788 c.guild_id = 825407338755653642;
789
790 dpp::channel c2;
791 c2.id = 825411707521728511;
792
793 dpp::channel c3;
794 c3.guild_id = 825407338755653642;
795
796 dpp::channel c4;
797
798 set_test(CHANNEL_GET_URL,
799 c.get_url() == dpp::utility::url_host + "/channels/825407338755653642/825411707521728511" &&
800 c2.get_url() == "" &&
801 c3.get_url() == "" &&
802 c4.get_url() == ""
803 );
804 }
805
806 { // utility methods
807 set_test(UTILITY_GUILD_NAVIGATION, false);
808 auto gn1 = dpp::utility::guild_navigation(123, dpp::utility::gnt_customize);
809 auto gn2 = dpp::utility::guild_navigation(1234, dpp::utility::gnt_browse);
810 auto gn3 = dpp::utility::guild_navigation(12345, dpp::utility::gnt_guide);
811 set_test(UTILITY_GUILD_NAVIGATION, gn1 == "<123:customize>" && gn2 == "<1234:browse>" && gn3 == "<12345:guide>");
812
813 set_test(UTILITY_ICONHASH, false);
814 auto iconhash1 = dpp::utility::iconhash("a_5532c6414c70765a28cf9448c117205f");
815 set_test(UTILITY_ICONHASH, iconhash1.first == 6139187225817019994 &&
816 iconhash1.second == 2940732121894297695 &&
817 iconhash1.to_string() == "5532c6414c70765a28cf9448c117205f"
818 );
819
820 set_test(UTILITY_MAKE_URL_PARAMETERS, false);
821 auto url_params1 = dpp::utility::make_url_parameters({
822 {"foo", 15},
823 {"bar", 7}
824 });
825 auto url_params2 = dpp::utility::make_url_parameters({
826 {"foo", "hello"},
827 {"bar", "two words"}
828 });
829 set_test(UTILITY_MAKE_URL_PARAMETERS, url_params1 == "?bar=7&foo=15" && url_params2 == "?bar=two%20words&foo=hello");
830
831 set_test(UTILITY_MARKDOWN_ESCAPE, false);
832 auto escaped = dpp::utility::markdown_escape(
833 "> this is a quote\n"
834 "**some bold text**");
835 set_test(UTILITY_MARKDOWN_ESCAPE, "\\>this is a quote\\n\\*\\*some bold text\\*\\*");
836
837 set_test(UTILITY_TOKENIZE, false);
838 auto tokens = dpp::utility::tokenize("some Whitespace seperated Text to Tokenize", " ");
839 std::vector<std::string> expected_tokens = {"some", "Whitespace", "seperated", "Text", "to", "Tokenize"};
840 set_test(UTILITY_TOKENIZE, tokens == expected_tokens);
841
842 set_test(UTILITY_URL_ENCODE, false);
843 auto url_encoded = dpp::utility::url_encode("S2-^$1Nd+U!g'8+_??o?p-bla bla");
844 set_test(UTILITY_URL_ENCODE, url_encoded == "S2-%5E%241Nd%2BU%21g%278%2B_%3F%3Fo%3Fp-bla%20bla");
845
846 set_test(UTILITY_SLASHCOMMAND_MENTION, false);
847 auto mention1 = dpp::utility::slashcommand_mention(123, "name");
848 auto mention2 = dpp::utility::slashcommand_mention(123, "name", "sub");
849 auto mention3 = dpp::utility::slashcommand_mention(123, "name", "group", "sub");
850 bool success = mention1 == "</name:123>" && mention2 == "</name sub:123>" && mention3 == "</name group sub:123>";
851 set_test(UTILITY_SLASHCOMMAND_MENTION, success);
852
853 set_test(UTILITY_CHANNEL_MENTION, false);
854 auto channel_mention = dpp::utility::channel_mention(123);
855 set_test(UTILITY_CHANNEL_MENTION, channel_mention == "<#123>");
856
857 set_test(UTILITY_USER_MENTION, false);
858 auto user_mention = dpp::utility::user_mention(123);
859 set_test(UTILITY_USER_MENTION, user_mention == "<@123>");
860
861 set_test(UTILITY_ROLE_MENTION, false);
862 auto role_mention = dpp::utility::role_mention(123);
863 set_test(UTILITY_ROLE_MENTION, role_mention == "<@&123>");
864
865 set_test(UTILITY_EMOJI_MENTION, false);
866 auto emoji_mention1 = dpp::utility::emoji_mention("role1", 123, false);
867 auto emoji_mention2 = dpp::utility::emoji_mention("role2", 234, true);
868 auto emoji_mention3 = dpp::utility::emoji_mention("white_check_mark", 0, false);
869 auto emoji_mention4 = dpp::utility::emoji_mention("white_check_mark", 0, true);
870 set_test(UTILITY_EMOJI_MENTION,
871 emoji_mention1 == "<:role1:123>" &&
872 emoji_mention2 == "<a:role2:234>" &&
873 emoji_mention3 == ":white_check_mark:" &&
874 emoji_mention4 == ":white_check_mark:"
875 );
876
877 set_test(UTILITY_USER_URL, false);
878 auto user_url = dpp::utility::user_url(123);
879 set_test(UTILITY_USER_URL,
880 user_url == dpp::utility::url_host + "/users/123" &&
881 dpp::utility::user_url(0) == ""
882 );
883
884 set_test(UTILITY_MESSAGE_URL, false);
885 auto message_url = dpp::utility::message_url(1, 2, 3);
886 set_test(UTILITY_MESSAGE_URL,
887 message_url == dpp::utility::url_host + "/channels/1/2/3" &&
888 dpp::utility::message_url(0, 2, 3) == "" &&
889 dpp::utility::message_url(1, 0, 3) == "" &&
890 dpp::utility::message_url(1, 2, 0) == "" &&
891 dpp::utility::message_url(0, 0, 3) == "" &&
892 dpp::utility::message_url(0, 2, 0) == "" &&
893 dpp::utility::message_url(1, 0, 0) == "" &&
894 dpp::utility::message_url(0, 0, 0) == ""
895 );
896
897 set_test(UTILITY_CHANNEL_URL, false);
898 auto channel_url = dpp::utility::channel_url(1, 2);
899 set_test(UTILITY_CHANNEL_URL,
900 channel_url == dpp::utility::url_host + "/channels/1/2" &&
901 dpp::utility::channel_url(0, 2) == "" &&
902 dpp::utility::channel_url(1, 0) == "" &&
903 dpp::utility::channel_url(0, 0) == ""
904 );
905
906 set_test(UTILITY_THREAD_URL, false);
907 auto thread_url = dpp::utility::thread_url(1, 2);
908 set_test(UTILITY_THREAD_URL,
909 thread_url == dpp::utility::url_host + "/channels/1/2" &&
910 dpp::utility::thread_url(0, 2) == "" &&
911 dpp::utility::thread_url(1, 0) == "" &&
912 dpp::utility::thread_url(0, 0) == ""
913 );
914 }
915
916 #ifndef _WIN32
917 set_test(TIMESTRINGTOTIMESTAMP, false);
918 json tj;
919 tj["t1"] = "2022-01-19T17:18:14.506000+00:00";
920 tj["t2"] = "2022-01-19T17:18:14+00:00";
921 uint32_t inTimestamp = 1642612694;
922 set_test(TIMESTRINGTOTIMESTAMP, (uint64_t) dpp::ts_not_null(&tj, "t1") == inTimestamp && (uint64_t) dpp::ts_not_null(&tj, "t2") == inTimestamp);
923 #else
924 set_test(TIMESTRINGTOTIMESTAMP, true);
925 #endif
926
927 {
928 set_test(TS, false);
929 dpp::managed m(189759562910400512);
930 set_test(TS, ((uint64_t) m.get_creation_time()) == 1465312605);
931 }
932
933 {
934 coro_offline_tests();
935 }
936
937 std::promise<void> ready_promise;
938 std::future ready_future = ready_promise.get_future();
939
940 std::vector<std::byte> dpp_logo = load_data("DPP-Logo.png");
941
942 set_test(PRESENCE, false);
943 set_test(CLUSTER, false);
944 try {
945 dpp::cluster bot(token, dpp::i_all_intents);
946 bot.set_websocket_protocol(dpp::ws_etf);
947 set_test(CLUSTER, true);
948 set_test(CONNECTION, false);
949 set_test(GUILDCREATE, false);
950 set_test(ICONHASH, false);
951
952 set_test(MSGCOLLECT, false);
953 if (!offline) {
954 /* Intentional leak: freed on unit test end */
955 [[maybe_unused]]
956 message_collector *collect_messages = new message_collector(&bot, 25);
957 }
958
959 set_test(JSON_PARSE_ERROR, false);
960 dpp::rest_request<dpp::confirmation>(&bot, "/nonexistent", "address", "", dpp::m_get, "", [](const dpp::confirmation_callback_t &e) {
961 if (e.is_error() && e.get_error().code == 404) {
962 set_test(JSON_PARSE_ERROR, true);
963 } else {
964 set_test(JSON_PARSE_ERROR, false);
965 }
966 });
967
968 if (!offline) {
969 start_test(INVALIDUTF8);
970 bot.message_create(dpp::message(TEST_TEXT_CHANNEL_ID, "ä\xA9ü"), [](const auto &cc) {
971 set_status(INVALIDUTF8, ts_success);
972 });
973 } else {
974 set_status(INVALIDUTF8, ts_skipped);
975 }
976
977 dpp::utility::iconhash i;
978 std::string dummyval("fcffffffffffff55acaaaaaaaaaaaa66");
979 i = dummyval;
980 set_test(ICONHASH, (i.to_string() == dummyval));
981
982 /* This ensures we test both protocols, as voice is json and shard is etf */
983 bot.set_websocket_protocol(dpp::ws_etf);
984
985 /* This is near impossible to test without a 'clean room' voice channel.
986 * We attach this event just so that the decoder events are fired while we
987 * are sending audio later, this way if the audio receive code is plain unstable
988 * the test suite will crash and fail.
989 */
990 bot.on_voice_receive_combined([&](const auto &event) {
991 });
992
993 bot.on_guild_create([dpp_logo, &bot](const dpp::guild_create_t &event) {
994 dpp::guild g = event.created;
995
996 if (g.id == TEST_GUILD_ID) {
997 start_test(GUILD_EDIT);
998 g.set_icon(dpp::i_png, dpp_logo.data(), static_cast<uint32_t>(dpp_logo.size()));
999 bot.guild_edit(g, [&bot](const dpp::confirmation_callback_t &result) {
1000 if (result.is_error()) {
1001 set_status(GUILD_EDIT, ts_failed, "guild_edit 1 errored:\n" + result.get_error().human_readable);
1002 return;
1003 }
1004 dpp::guild g2 = result.get<dpp::guild>();
1005
1006 if (g2.get_icon_url().empty()) {
1007 set_status(GUILD_EDIT, ts_failed, "icon not set or not retrieved");
1008 return;
1009 }
1010 g2.remove_icon();
1011 bot.guild_edit(g2, [](const dpp::confirmation_callback_t &result) {
1012 if (result.is_error()) {
1013 set_status(GUILD_EDIT, ts_failed, "guild_edit 2 errored:\n" + result.get_error().human_readable);
1014 return;
1015 }
1016 const dpp::guild g3 = result.get<dpp::guild>();
1017 if (!g3.get_icon_url().empty()) {
1018 set_status(GUILD_EDIT, ts_failed, "icon not removed");
1019 return;
1020 }
1021 set_status(GUILD_EDIT, ts_success);
1022 });
1023 });
1024 }
1025 });
1026
1027 bot.on_ready([&ready_promise, &bot, dpp_logo](const dpp::ready_t &event) {
1028
1029 set_test(CONNECTION, true);
1030 ready_promise.set_value();
1031
1032 set_test(GETAPP, false);
1033 bot.current_application_get([&bot](const auto& res) {
1034 set_test(GETAPP, !res.is_error() && (std::get<dpp::application>(res.value)).id == bot.me.id);
1035 });
1036
1037 set_test(APPCOMMAND, false);
1038 set_test(LOGGER, false);
1039 bot.log(dpp::ll_info, "Test log message");
1040
1041 bot.guild_command_create(dpp::slashcommand().set_name("testcommand")
1042 .set_description("Test command for DPP unit test")
1043 .add_option(dpp::command_option(dpp::co_attachment, "file", "a file"))
1044 .set_application_id(bot.me.id)
1045 .add_localization("fr", "zut", "Ou est la salor dans Discord?"),
1046 TEST_GUILD_ID, [&](const dpp::confirmation_callback_t &callback) {
1047 if (!callback.is_error()) {
1048 set_test(APPCOMMAND, true);
1049 set_test(DELCOMMAND, false);
1050 dpp::slashcommand s = std::get<dpp::slashcommand>(callback.value);
1051 bot.guild_command_delete(s.id, TEST_GUILD_ID, [&](const dpp::confirmation_callback_t &callback) {
1052 if (!callback.is_error()) {
1053 dpp::message test_message(TEST_TEXT_CHANNEL_ID, "test message");
1054
1055 set_test(DELCOMMAND, true);
1056 set_test(MESSAGECREATE, false);
1057 set_test(MESSAGEEDIT, false);
1058 set_test(MESSAGERECEIVE, false);
1059 test_message.add_file("no-mime", "test");
1060 test_message.add_file("test.txt", "test", "text/plain");
1061 test_message.add_file("test.png", std::string{reinterpret_cast<const char *>(dpp_logo.data()), dpp_logo.size()}, "image/png");
1062 bot.message_create(test_message, [&bot](const dpp::confirmation_callback_t &callback) {
1063 if (!callback.is_error()) {
1064 set_test(MESSAGECREATE, true);
1065 set_test(REACT, false);
1066 dpp::message m = std::get<dpp::message>(callback.value);
1067 set_test(REACTEVENT, false);
1068 bot.message_add_reaction(m.id, TEST_TEXT_CHANNEL_ID, "😄", [](const dpp::confirmation_callback_t &callback) {
1069 if (!callback.is_error()) {
1070 set_test(REACT, true);
1071 } else {
1072 set_test(REACT, false);
1073 }
1074 });
1075 set_test(EDITEVENT, false);
1076 bot.message_edit(dpp::message(m).set_content("test edit"), [](const dpp::confirmation_callback_t &callback) {
1077 if (!callback.is_error()) {
1078 set_test(MESSAGEEDIT, true);
1079 }
1080 });
1081 }
1082 });
1083 } else {
1084 set_test(DELCOMMAND, false);
1085 }
1086 });
1087 }
1088 });
1089 });
1090
1091 bot.on_log([](const dpp::log_t &event) {
1092 if (event.severity > dpp::ll_trace) {
1093 std::lock_guard<std::mutex> locker(loglock);
1094 std::cout << "[" << std::fixed << std::setprecision(3) << (dpp::utility::time_f() - get_start_time()) << "]: ";
1095 if (event.severity == dpp::ll_error) {
1096 /* Errors are red */
1097 std::cout << "[\u001b[31m" << dpp::utility::loglevel(event.severity);
1098 } else {
1099 /* All other generic logger are cyan */
1100 std::cout << "[\u001b[36m" << dpp::utility::loglevel(event.severity);
1101 }
1102 std::cout << "\u001b[0m] " << event.message << "\n";
1103 }
1104 if (event.message == "Test log message") {
1105 set_test(LOGGER, true);
1106 }
1107 });
1108
1109 set_test(RUNONCE, false);
1110 uint8_t runs = 0;
1111 for (int x = 0; x < 10; ++x) {
1112 if (dpp::run_once<struct test_run>()) {
1113 runs++;
1114 }
1115 }
1116 set_test(RUNONCE, (runs == 1));
1117
1118 bot.on_voice_ready([&testaudio](const dpp::voice_ready_t &event) {
1119 set_test(VOICECONN, true);
1120 dpp::discord_voice_client *v = event.voice_client;
1121 set_test(VOICESEND, false);
1122 if (v && v->is_ready()) {
1123 v->send_audio_raw((uint16_t *) testaudio.data(), testaudio.size());
1124 } else {
1125 set_test(VOICESEND, false);
1126 }
1127 });
1128
1129 bot.on_invite_create([](const dpp::invite_create_t &event) {
1130 auto &inv = event.created_invite;
1131 if (!inv.code.empty() && inv.channel_id == TEST_TEXT_CHANNEL_ID && inv.guild_id == TEST_GUILD_ID && inv.created_at != 0 && inv.max_uses == 100) {
1132 set_test(INVITE_CREATE_EVENT, true);
1133 }
1134 });
1135
1136 bot.on_invite_delete([](const dpp::invite_delete_t &event) {
1137 auto &inv = event.deleted_invite;
1138 if (!inv.code.empty() && inv.channel_id == TEST_TEXT_CHANNEL_ID && inv.guild_id == TEST_GUILD_ID) {
1139 set_test(INVITE_DELETE_EVENT, true);
1140 }
1141 });
1142
1143 bot.on_voice_buffer_send([](const dpp::voice_buffer_send_t &event) {
1144 static bool sent_some_data = false;
1145
1146 if (event.buffer_size > 0) {
1147 sent_some_data = true;
1148 }
1149
1150 if (sent_some_data && event.packets_left == 0) {
1151 set_test(VOICESEND, true);
1152 }
1153 });
1154
1155 bot.on_guild_create([&bot](const dpp::guild_create_t &event) {
1156 if (event.created.id == TEST_GUILD_ID) {
1157 set_test(GUILDCREATE, true);
1158 if (event.presences.size() && event.presences.begin()->second.user_id > 0) {
1159 set_test(PRESENCE, true);
1160 }
1161 dpp::guild *g = dpp::find_guild(TEST_GUILD_ID);
1162 set_test(CACHE, false);
1163 if (g) {
1164 set_test(CACHE, true);
1165 set_test(VOICECONN, false);
1166 dpp::discord_client *s = bot.get_shard(0);
1167 s->connect_voice(g->id, TEST_VC_ID, false, false);
1168 } else {
1169 set_test(CACHE, false);
1170 }
1171 }
1172 });
1173
1174 // this helper class contains logic for the message tests, deletes the message when all tests are done
1175 class message_test_helper {
1176 private:
1177 std::mutex mutex;
1178 bool pin_tested;
1179 bool thread_tested;
1180 std::array<bool, 3> files_tested;
1181 std::array<bool, 3> files_success;
1182 dpp::snowflake channel_id;
1183 dpp::snowflake message_id;
1184 dpp::cluster &bot;
1185
1186 void delete_message_if_done() {
1187 if (files_tested == std::array{true, true, true} && pin_tested && thread_tested) {
1188 set_test(MESSAGEDELETE, false);
1189 bot.message_delete(message_id, channel_id, [](const dpp::confirmation_callback_t &callback) {
1190 set_test(MESSAGEDELETE, !callback.is_error());
1191 });
1192 }
1193 }
1194
1195 void set_pin_tested() {
1196 if (pin_tested) {
1197 return;
1198 }
1199 pin_tested = true;
1200 delete_message_if_done();
1201 }
1202
1203 void set_thread_tested() {
1204 if (thread_tested) {
1205 return;
1206 }
1207 thread_tested = true;
1208 delete_message_if_done();
1209 }
1210
1211 void set_file_tested(size_t index) {
1212 if (files_tested[index]) {
1213 return;
1214 }
1215 files_tested[index] = true;
1216 if (files_tested == std::array{true, true, true}) {
1217 set_test(MESSAGEFILE, files_success == std::array{true, true, true});
1218 }
1219 delete_message_if_done();
1220 }
1221
1222 void test_threads(const dpp::message &message) {
1223 set_test(THREAD_CREATE_MESSAGE, false);
1224 set_test(THREAD_DELETE, false);
1225 set_test(THREAD_DELETE_EVENT, false);
1226 bot.thread_create_with_message("test", message.channel_id, message.id, 60, 60, [this](const dpp::confirmation_callback_t &callback) {
1227 std::lock_guard lock(mutex);
1228 if (callback.is_error()) {
1229 set_thread_tested();
1230 } else {
1231 auto thread = callback.get<dpp::thread>();
1232 thread_id = thread.id;
1233 set_test(THREAD_CREATE_MESSAGE, true);
1234 bot.channel_delete(thread.id, [this](const dpp::confirmation_callback_t &callback) {
1235 set_test(THREAD_DELETE, !callback.is_error());
1236 set_thread_tested();
1237 });
1238 }
1239 });
1240 }
1241
1242 void test_files(const dpp::message &message) {
1243 set_test(MESSAGEFILE, false);
1244 if (message.attachments.size() == 3) {
1245 static constexpr auto check_mimetype = [](const auto &headers, std::string mimetype) {
1246 if (auto it = headers.find("content-type"); it != headers.end()) {
1247 // check that the mime type starts with what we gave : for example discord will change "text/plain" to "text/plain; charset=UTF-8"
1248 return it->second.size() >= mimetype.size() && std::equal(it->second.begin(), it->second.begin() + mimetype.size(), mimetype.begin());
1249 } else {
1250 return false;
1251 }
1252 };
1253 message.attachments[0].download([this](const dpp::http_request_completion_t &callback) {
1254 std::lock_guard lock(mutex);
1255 if (callback.status == 200 && callback.body == "test") {
1256 files_success[0] = true;
1257 }
1258 set_file_tested(0);
1259 });
1260 message.attachments[1].download([this](const dpp::http_request_completion_t &callback) {
1261 std::lock_guard lock(mutex);
1262 if (callback.status == 200 && check_mimetype(callback.headers, "text/plain") && callback.body == "test") {
1263 files_success[1] = true;
1264 }
1265 set_file_tested(1);
1266 });
1267 message.attachments[2].download([this](const dpp::http_request_completion_t &callback) {
1268 std::lock_guard lock(mutex);
1269 // do not check the contents here because discord can change compression
1270 if (callback.status == 200 && check_mimetype(callback.headers, "image/png")) {
1271 files_success[2] = true;
1272 }
1273 set_file_tested(2);
1274 });
1275 } else {
1276 set_file_tested(0);
1277 set_file_tested(1);
1278 set_file_tested(2);
1279 }
1280 }
1281
1282 void test_pin() {
1283 if (!extended) {
1284 set_pin_tested();
1285 return;
1286 }
1287 set_test(MESSAGEPIN, false);
1288 set_test(MESSAGEUNPIN, false);
1289 bot.message_pin(channel_id, message_id, [this](const dpp::confirmation_callback_t &callback) {
1290 std::lock_guard lock(mutex);
1291 if (!callback.is_error()) {
1292 set_test(MESSAGEPIN, true);
1293 bot.message_unpin(TEST_TEXT_CHANNEL_ID, message_id, [this](const dpp::confirmation_callback_t &callback) {
1294 std::lock_guard lock(mutex);
1295 if (!callback.is_error()) {
1296 set_test(MESSAGEUNPIN, true);
1297 }
1298 set_pin_tested();
1299 });
1300 } else {
1301 set_pin_tested();
1302 }
1303 });
1304 }
1305
1306 public:
1307 dpp::snowflake thread_id;
1308
1309 message_test_helper(dpp::cluster &_bot) : bot(_bot) {
1310 }
1311
1312 void run(const dpp::message &message) {
1313 pin_tested = false;
1314 thread_tested = false;
1315 files_tested = {false, false, false};
1316 files_success = {false, false, false};
1317 channel_id = message.channel_id;
1318 message_id = message.id;
1319 test_pin();
1320 test_files(message);
1321 test_threads(message);
1322 }
1323 };
1324
1325 message_test_helper message_helper(bot);
1326
1327 class thread_test_helper {
1328 public:
1329 enum event_flag {
1330 MESSAGE_CREATE = 1 << 0,
1331 MESSAGE_EDIT = 1 << 1,
1332 MESSAGE_REACT = 1 << 2,
1333 MESSAGE_REMOVE_REACT = 1 << 3,
1334 MESSAGE_DELETE = 1 << 4,
1335 EVENT_END = 1 << 5
1336 };
1337 private:
1338 std::mutex mutex;
1339 dpp::cluster &bot;
1340 bool edit_tested = false;
1341 bool members_tested = false;
1342 bool messages_tested = false;
1343 bool events_tested = false;
1344 bool get_active_tested = false;
1345 uint32_t events_tested_mask = 0;
1346 uint32_t events_to_test_mask = 0;
1347
1348 void delete_if_done() {
1349 if (edit_tested && members_tested && messages_tested && events_tested && get_active_tested) {
1350 bot.channel_delete(thread_id);
1351 }
1352 }
1353
1354 void set_events_tested() {
1355 if (events_tested) {
1356 return;
1357 }
1358 events_tested = true;
1359 delete_if_done();
1360 }
1361
1362 void set_edit_tested() {
1363 if (edit_tested) {
1364 return;
1365 }
1366 edit_tested = true;
1367 delete_if_done();
1368 }
1369
1370 void set_members_tested() {
1371 if (members_tested) {
1372 return;
1373 }
1374 members_tested = true;
1375 delete_if_done();
1376 }
1377
1378 void set_get_active_tested() {
1379 if (get_active_tested) {
1380 return;
1381 }
1382 get_active_tested = true;
1383 delete_if_done();
1384 }
1385
1386 void set_messages_tested() {
1387 if (messages_tested) {
1388 return;
1389 }
1390 messages_tested = true;
1391 delete_if_done();
1392 }
1393
1394 void set_event_tested(event_flag flag) {
1395 if (events_tested_mask & flag) {
1396 return;
1397 }
1398 events_tested_mask |= flag;
1399 for (uint32_t i = 1; i < EVENT_END; i <<= 1) {
1400 if ((events_to_test_mask & i) && (events_tested_mask & i) != i) {
1401 return;
1402 }
1403 }
1404 set_events_tested();
1405 }
1406
1407 void events_abort() {
1408 events_tested_mask |= ~events_to_test_mask;
1409 for (uint32_t i = 1; i < EVENT_END; i <<= 1) {
1410 if ((events_tested_mask & i) != i) {
1411 return;
1412 }
1413 }
1414 set_events_tested();
1415 }
1416
1417 public:
1418 /**
1419 * @Brief wrapper for set_event_tested, locking the mutex. Meant to be used from outside the class
1420 */
1421 void notify_event_tested(event_flag flag) {
1422 std::lock_guard lock{mutex};
1423
1424 set_event_tested(flag);
1425 }
1426
1427 dpp::snowflake thread_id;
1428
1429 void test_edit(const dpp::thread &thread) {
1430 std::lock_guard lock{mutex};
1431
1432 if (!edit_tested) {
1433 dpp::thread edit = thread;
1434 set_test(THREAD_EDIT, false);
1435 set_test(THREAD_UPDATE_EVENT, false);
1436 edit.name = "edited";
1437 edit.metadata.locked = true;
1438 bot.thread_edit(edit, [this](const dpp::confirmation_callback_t &callback) {
1439 std::lock_guard lock(mutex);
1440 if (!callback.is_error()) {
1441 set_test(THREAD_EDIT, true);
1442 }
1443 set_edit_tested();
1444 });
1445 }
1446 }
1447
1448 void test_get_active(const dpp::thread &thread) {
1449 std::lock_guard lock{mutex};
1450
1451 set_test(THREAD_GET_ACTIVE, false);
1452 bot.threads_get_active(TEST_GUILD_ID, [this](const dpp::confirmation_callback_t &callback) {
1453 std::lock_guard lock{mutex};
1454 if (!callback.is_error()) {
1455 const auto &threads = callback.get<dpp::active_threads>();
1456 if (auto thread_it = threads.find(thread_id); thread_it != threads.end()) {
1457 const auto &thread = thread_it->second.active_thread;
1458 const auto &member = thread_it->second.bot_member;
1459 if (thread.id == thread_id && member.has_value() && member->user_id == bot.me.id) {
1460 set_test(THREAD_GET_ACTIVE, true);
1461 }
1462 }
1463 }
1464 set_get_active_tested();
1465 });
1466 }
1467
1468 void test_members(const dpp::thread &thread) {
1469 std::lock_guard lock{mutex};
1470
1471 if (!members_tested) {
1472 if (!extended) {
1473 set_members_tested();
1474 return;
1475 }
1476 set_test(THREAD_MEMBER_ADD, false);
1477 set_test(THREAD_MEMBER_GET, false);
1478 set_test(THREAD_MEMBERS_GET, false);
1479 set_test(THREAD_MEMBER_REMOVE, false);
1480 set_test(THREAD_MEMBERS_ADD_EVENT, false);
1481 set_test(THREAD_MEMBERS_REMOVE_EVENT, false);
1482 bot.thread_member_add(thread_id, TEST_USER_ID, [this](const dpp::confirmation_callback_t &callback) {
1483 std::lock_guard lock{mutex};
1484 if (callback.is_error()) {
1485 set_members_tested();
1486 return;
1487 }
1488 set_test(THREAD_MEMBER_ADD, true);
1489 bot.thread_member_get(thread_id, TEST_USER_ID, [this](const dpp::confirmation_callback_t &callback) {
1490 std::lock_guard lock{mutex};
1491 if (callback.is_error()) {
1492 set_members_tested();
1493 return;
1494 }
1495 set_test(THREAD_MEMBER_GET, true);
1496 bot.thread_members_get(thread_id, [this](const dpp::confirmation_callback_t &callback) {
1497 std::lock_guard lock{mutex};
1498 if (callback.is_error()) {
1499 set_members_tested();
1500 return;
1501 }
1502 const auto &members = callback.get<dpp::thread_member_map>();
1503 if (members.find(TEST_USER_ID) == members.end() || members.find(bot.me.id) == members.end()) {
1504 set_members_tested();
1505 return;
1506 }
1507 set_test(THREAD_MEMBERS_GET, true);
1508 bot.thread_member_remove(thread_id, TEST_USER_ID, [this](const dpp::confirmation_callback_t &callback) {
1509 std::lock_guard lock{mutex};
1510 if (!callback.is_error()) {
1511 set_test(THREAD_MEMBER_REMOVE, true);
1512 }
1513 set_members_tested();
1514 });
1515 });
1516 });
1517 });
1518 }
1519 }
1520
1521 void test_messages(const dpp::thread &thread) {
1522 if (!extended) {
1523 set_messages_tested();
1524 set_events_tested();
1525 return;
1526 }
1527
1528 std::lock_guard lock{mutex};
1529 set_test(THREAD_MESSAGE, false);
1530 set_test(THREAD_MESSAGE_CREATE_EVENT, false);
1531 set_test(THREAD_MESSAGE_EDIT_EVENT, false);
1532 set_test(THREAD_MESSAGE_REACT_ADD_EVENT, false);
1533 set_test(THREAD_MESSAGE_REACT_REMOVE_EVENT, false);
1534 set_test(THREAD_MESSAGE_DELETE_EVENT, false);
1535 events_to_test_mask |= MESSAGE_CREATE;
1536 bot.message_create(dpp::message{"hello thread"}.set_channel_id(thread.id), [this](const dpp::confirmation_callback_t &callback) {
1537 std::lock_guard lock{mutex};
1538 if (callback.is_error()) {
1539 events_abort();
1540 set_messages_tested();
1541 return;
1542 }
1543 auto m = callback.get<dpp::message>();
1544 m.content = "hello thread?";
1545 events_to_test_mask |= MESSAGE_EDIT;
1546 bot.message_edit(m, [this, message_id = m.id](const dpp::confirmation_callback_t &callback) {
1547 std::lock_guard lock{mutex};
1548 if (callback.is_error()) {
1549 events_abort();
1550 set_messages_tested();
1551 return;
1552 }
1553 events_to_test_mask |= MESSAGE_REACT;
1554 bot.message_add_reaction(message_id, thread_id, dpp::unicode_emoji::thread, [this, message_id](const dpp::confirmation_callback_t &callback) {
1555 std::lock_guard lock{mutex};
1556 if (callback.is_error()) {
1557 events_abort();
1558 set_messages_tested();
1559 return;
1560 }
1561 events_to_test_mask |= MESSAGE_REMOVE_REACT;
1562 bot.message_delete_reaction(message_id, thread_id, bot.me.id, dpp::unicode_emoji::thread, [this, message_id](const dpp::confirmation_callback_t &callback) {
1563 std::lock_guard lock{mutex};
1564 if (callback.is_error()) {
1565 events_abort();
1566 set_messages_tested();
1567 return;
1568 }
1569 events_to_test_mask |= MESSAGE_DELETE;
1570 bot.message_delete(message_id, thread_id, [this](const dpp::confirmation_callback_t &callback) {
1571 std::lock_guard lock{mutex};
1572 set_messages_tested();
1573 if (callback.is_error()) {
1574 events_abort();
1575 return;
1576 }
1577 set_test(THREAD_MESSAGE, true);
1578 });
1579 });
1580 });
1581 });
1582 });
1583 }
1584
1585 void run(const dpp::thread &thread) {
1586 thread_id = thread.id;
1587 test_get_active(thread);
1588 test_edit(thread);
1589 test_members(thread);
1590 test_messages(thread);
1591 }
1592
1593 thread_test_helper(dpp::cluster &bot_) : bot{bot_} {
1594 }
1595 };
1596
1597 thread_test_helper thread_helper(bot);
1598
1599 bot.on_thread_create([&thread_helper](const dpp::thread_create_t &event) {
1600 if (event.created.name == "thread test") {
1601 set_test(THREAD_CREATE_EVENT, true);
1602 thread_helper.run(event.created);
1603 }
1604 });
1605
1606 bool message_tested = false;
1607 bot.on_message_create([&message_tested, &bot, &message_helper, &thread_helper](const dpp::message_create_t &event) {
1608 if (event.msg.author.id == bot.me.id) {
1609 if (event.msg.content == "test message" && !message_tested) {
1610 message_tested = true;
1611 set_test(MESSAGERECEIVE, true);
1612 message_helper.run(event.msg);
1613 set_test(MESSAGESGET, false);
1614 bot.messages_get(event.msg.channel_id, 0, event.msg.id, 0, 5, [](const dpp::confirmation_callback_t &cc) {
1615 if (!cc.is_error()) {
1616 dpp::message_map mm = std::get<dpp::message_map>(cc.value);
1617 if (mm.size()) {
1618 set_test(MESSAGESGET, true);
1619 set_test(TIMESTAMP, false);
1620 dpp::message m = mm.begin()->second;
1621 if (m.sent > 0) {
1622 set_test(TIMESTAMP, true);
1623 } else {
1624 set_test(TIMESTAMP, false);
1625 }
1626 } else {
1627 set_test(MESSAGESGET, false);
1628 }
1629 } else {
1630 set_test(MESSAGESGET, false);
1631 }
1632 });
1633 set_test(MSGCREATESEND, false);
1634 event.send("MSGCREATESEND", [&bot, ch_id = event.msg.channel_id](const auto &cc) {
1635 if (!cc.is_error()) {
1636 dpp::message m = std::get<dpp::message>(cc.value);
1637 if (m.channel_id == ch_id) {
1638 set_test(MSGCREATESEND, true);
1639 } else {
1640 bot.log(dpp::ll_debug, cc.http_info.body);
1641 set_test(MSGCREATESEND, false);
1642 }
1643 bot.message_delete(m.id, m.channel_id);
1644 } else {
1645 bot.log(dpp::ll_debug, cc.http_info.body);
1646 set_test(MSGCREATESEND, false);
1647 }
1648 });
1649 }
1650 if (event.msg.channel_id == thread_helper.thread_id && event.msg.content == "hello thread") {
1651 set_test(THREAD_MESSAGE_CREATE_EVENT, true);
1652 thread_helper.notify_event_tested(thread_test_helper::MESSAGE_CREATE);
1653 }
1654 }
1655 });
1656
1657 bot.on_message_reaction_add([&bot, &thread_helper](const dpp::message_reaction_add_t &event) {
1658 if (event.reacting_user.id == bot.me.id) {
1659 if (event.reacting_emoji.name == "😄") {
1660 set_test(REACTEVENT, true);
1661 }
1662 if (event.channel_id == thread_helper.thread_id && event.reacting_emoji.name == dpp::unicode_emoji::thread) {
1663 set_test(THREAD_MESSAGE_REACT_ADD_EVENT, true);
1664 thread_helper.notify_event_tested(thread_test_helper::MESSAGE_REACT);
1665 }
1666 }
1667 });
1668
1669 bot.on_message_reaction_remove([&bot, &thread_helper](const dpp::message_reaction_remove_t &event) {
1670 if (event.reacting_user_id == bot.me.id) {
1671 if (event.channel_id == thread_helper.thread_id && event.reacting_emoji.name == dpp::unicode_emoji::thread) {
1672 set_test(THREAD_MESSAGE_REACT_REMOVE_EVENT, true);
1673 thread_helper.notify_event_tested(thread_test_helper::MESSAGE_REMOVE_REACT);
1674 }
1675 }
1676 });
1677
1678 bot.on_message_delete([&thread_helper](const dpp::message_delete_t &event) {
1679 if (event.channel_id == thread_helper.thread_id) {
1680 set_test(THREAD_MESSAGE_DELETE_EVENT, true);
1681 thread_helper.notify_event_tested(thread_test_helper::MESSAGE_DELETE);
1682 }
1683 });
1684
1685 bool message_edit_tested = false;
1686 bot.on_message_update([&bot, &thread_helper, &message_edit_tested](const dpp::message_update_t &event) {
1687 if (event.msg.author == bot.me.id) {
1688 if (event.msg.content == "test edit" && !message_edit_tested) {
1689 message_edit_tested = true;
1690 set_test(EDITEVENT, true);
1691 }
1692 if (event.msg.channel_id == thread_helper.thread_id && event.msg.content == "hello thread?") {
1693 set_test(THREAD_MESSAGE_EDIT_EVENT, true);
1694 thread_helper.notify_event_tested(thread_test_helper::MESSAGE_EDIT);
1695 }
1696 }
1697 });
1698
1699 bot.on_thread_update([&thread_helper](const dpp::thread_update_t &event) {
1700 if (event.updating_guild.id == TEST_GUILD_ID && event.updated.id == thread_helper.thread_id && event.updated.name == "edited") {
1701 set_test(THREAD_UPDATE_EVENT, true);
1702 }
1703 });
1704
1705 bot.on_thread_members_update([&](const dpp::thread_members_update_t &event) {
1706 if (event.updating_guild->id == TEST_GUILD_ID && event.thread_id == thread_helper.thread_id) {
1707 if (std::find_if(std::begin(event.added), std::end(event.added), is_owner) != std::end(event.added)) {
1708 set_test(THREAD_MEMBERS_ADD_EVENT, true);
1709 }
1710 if (std::find_if(std::begin(event.removed_ids), std::end(event.removed_ids), is_owner) != std::end(event.removed_ids)) {
1711 set_test(THREAD_MEMBERS_REMOVE_EVENT, true);
1712 }
1713 }
1714 });
1715
1716 bot.on_thread_delete([&](const dpp::thread_delete_t &event) {
1717 if (event.deleting_guild.id == TEST_GUILD_ID && event.deleted.id == message_helper.thread_id) {
1718 set_test(THREAD_DELETE_EVENT, true);
1719 }
1720 });
1721
1722 // set to execute from this thread (main thread) after on_ready is fired
1723 auto do_online_tests = [&] {
1724 coro_online_tests(&bot);
1725 set_test(GUILD_BAN_CREATE, false);
1726 set_test(GUILD_BAN_GET, false);
1727 set_test(GUILD_BANS_GET, false);
1728 set_test(GUILD_BAN_DELETE, false);
1729 if (!offline) {
1730 // some discord accounts to test the ban stuff with...
1731 dpp::snowflake deadUser1(155149108183695360); // Dyno
1732 dpp::snowflake deadUser2(159985870458322944); // MEE6
1733 dpp::snowflake deadUser3(936929561302675456); // MidJourney Bot
1734
1735 bot.set_audit_reason("ban reason one").guild_ban_add(TEST_GUILD_ID, deadUser1, 0, [deadUser1, deadUser2, deadUser3, &bot](const dpp::confirmation_callback_t &event) {
1736 if (!event.is_error())
1737 bot.guild_ban_add(TEST_GUILD_ID, deadUser2, 0, [deadUser1, deadUser2, deadUser3, &bot](const dpp::confirmation_callback_t &event) {
1738 if (!event.is_error())
1739 bot.set_audit_reason("ban reason three").guild_ban_add(TEST_GUILD_ID, deadUser3, 0, [deadUser1, deadUser2, deadUser3, &bot](const dpp::confirmation_callback_t &event) {
1740 if (event.is_error()) {
1741 return;
1742 }
1743 set_test(GUILD_BAN_CREATE, true);
1744
1745 // get ban
1746 bot.guild_get_ban(TEST_GUILD_ID, deadUser1, [&bot, deadUser1, deadUser2, deadUser3](const dpp::confirmation_callback_t &event) {
1747 if (!event.is_error()) {
1748 dpp::ban ban = event.get<dpp::ban>();
1749 // Check the audit reason was set correctly
1750 if (ban.user_id == deadUser1 && ban.reason == "ban reason one") {
1751 set_test(GUILD_BAN_GET, true);
1752 } else {
1753 set_test(GUILD_BAN_GET, false);
1754 }
1755 // get all bans after deadUser1
1756 bot.guild_get_bans(TEST_GUILD_ID, 0, deadUser1, 50, [&bot, deadUser1, deadUser2, deadUser3](const dpp::confirmation_callback_t &event) {
1757 if (!event.is_error()) {
1758 auto bans = event.get<dpp::ban_map>();
1759 // The ban set should contain at least two bans, two of which should be deadUser2 and deadUser3,
1760 // but never deadUser1. Take into account that *other bans* might exist in the list so we can't
1761 // just assume there are three or set a limit of 3 above.
1762 set_test(
1763 GUILD_BANS_GET,
1764 bans.find(deadUser1) == bans.end()
1765 && bans.find(deadUser2) != bans.end()
1766 && bans.find(deadUser3) != bans.end()
1767 );
1768 // unban all three
1769 bot.guild_ban_delete(TEST_GUILD_ID, deadUser1, [&bot, deadUser2, deadUser3](const dpp::confirmation_callback_t &event) {
1770 if (!event.is_error()) {
1771 bot.guild_ban_delete(TEST_GUILD_ID, deadUser2, [&bot, deadUser3](const dpp::confirmation_callback_t &event) {
1772 if (!event.is_error()) {
1773 bot.guild_ban_delete(TEST_GUILD_ID, deadUser3, [](const dpp::confirmation_callback_t &event) {
1774 if (!event.is_error()) {
1775 set_test(GUILD_BAN_DELETE, true);
1776 } else {
1777 set_test(GUILD_BAN_DELETE, false);
1778 }
1779 });
1780 } else {
1781 set_test(GUILD_BAN_DELETE, false);
1782 }
1783 });
1784 } else {
1785 set_test(GUILD_BAN_DELETE, false);
1786 }
1787 });
1788 } else {
1789 /* An error in this test cascades to others failing immediately */
1790 set_test(GUILD_BANS_GET, false);
1791 set_test(GUILD_BAN_DELETE, false);
1792 }
1793 });
1794 } else {
1795 /* An error in the parent test cascades to the others failing immediately */
1796 set_test(GUILD_BAN_GET, false);
1797 set_test(GUILD_BANS_GET, false);
1798 set_test(GUILD_BAN_DELETE, false);
1799 }
1800 });
1801 });
1802 });
1803 });
1804 }
1805
1806 set_test(REQUEST_GET_IMAGE, false);
1807 if (!offline) {
1808 bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, [&bot](const dpp::http_request_completion_t &callback) {
1809 if (callback.status != 200) {
1810 return;
1811 }
1812 set_test(REQUEST_GET_IMAGE, true);
1813
1814 dpp::emoji emoji;
1815 emoji.load_image(callback.body, dpp::i_png);
1816 emoji.name = "dpp";
1817
1818 // emoji unit test with the requested image
1819 set_test(EMOJI_CREATE, false);
1820 set_test(EMOJI_GET, false);
1821 set_test(EMOJI_DELETE, false);
1822 bot.guild_emoji_create(TEST_GUILD_ID, emoji, [&bot](const dpp::confirmation_callback_t &event) {
1823 if (event.is_error()) {
1824 return;
1825 }
1826 set_test(EMOJI_CREATE, true);
1827
1828 auto created = event.get<dpp::emoji>();
1829 bot.guild_emoji_get(TEST_GUILD_ID, created.id, [&bot, created](const dpp::confirmation_callback_t &event) {
1830 if (event.is_error()) {
1831 return;
1832 }
1833 auto fetched = event.get<dpp::emoji>();
1834 if (created.id == fetched.id && created.name == fetched.name && created.flags == fetched.flags) {
1835 set_test(EMOJI_GET, true);
1836 }
1837
1838 bot.guild_emoji_delete(TEST_GUILD_ID, fetched.id, [](const dpp::confirmation_callback_t &event) {
1839 if (!event.is_error()) {
1840 set_test(EMOJI_DELETE, true);
1841 }
1842 });
1843 });
1844 });
1845 });
1846 }
1847
1848 set_test(INVITE_CREATE, false);
1849 set_test(INVITE_GET, false);
1850 set_test(INVITE_DELETE, false);
1851 set_test(GETCHANNELINVS, false);
1852 if (!offline) {
1853 dpp::channel channel;
1854 channel.id = TEST_TEXT_CHANNEL_ID;
1855 dpp::invite invite;
1856 invite.max_age = 0;
1857 invite.max_uses = 100;
1858 set_test(INVITE_CREATE_EVENT, false);
1859 bot.channel_invite_create(channel, invite, [&bot, channel, invite](const dpp::confirmation_callback_t &event) {
1860 if (event.is_error()) {
1861 return;
1862 }
1863
1864 auto created = event.get<dpp::invite>();
1865 if (!created.code.empty() && created.channel_id == TEST_TEXT_CHANNEL_ID && created.guild_id == TEST_GUILD_ID && created.inviter.id == bot.me.id) {
1866 set_test(INVITE_CREATE, true);
1867 }
1868
1869 bot.channel_invites_get(channel, [created](const dpp::confirmation_callback_t &event) {
1870 if (event.is_error()) {
1871 return;
1872 }
1873
1874 auto invites = event.get<dpp::invite_map>();
1875 if (!invites.empty() && invites.find(created.code) != invites.end()) {
1876 set_test(GETCHANNELINVS, true);
1877 }
1878 });
1879
1880 bot.invite_get(created.code, [&bot, created](const dpp::confirmation_callback_t &event) {
1881 if (!event.is_error()) {
1882 auto retrieved = event.get<dpp::invite>();
1883 if (retrieved.code == created.code && retrieved.guild_id == created.guild_id && retrieved.channel_id == created.channel_id && retrieved.inviter.id == created.inviter.id) {
1884 if (retrieved.destination_guild.flags & dpp::g_community) {
1885 set_test(INVITE_GET, retrieved.expires_at == 0);
1886 } else {
1887 set_test(INVITE_GET, true);
1888 }
1889
1890 } else {
1891 set_test(INVITE_GET, false);
1892 }
1893 } else {
1894 set_test(INVITE_GET, false);
1895 }
1896
1897 set_test(INVITE_DELETE_EVENT, false);
1898 bot.invite_delete(created.code, [](const dpp::confirmation_callback_t &event) {
1899 set_test(INVITE_DELETE, !event.is_error());
1900 });
1901 });
1902 });
1903 }
1904
1905 set_test(AUTOMOD_RULE_CREATE, false);
1906 set_test(AUTOMOD_RULE_GET, false);
1907 set_test(AUTOMOD_RULE_GET_ALL, false);
1908 set_test(AUTOMOD_RULE_DELETE, false);
1909 if (!offline) {
1910 dpp::automod_rule automodRule;
1911 automodRule.name = "automod rule (keyword type)";
1912 automodRule.trigger_type = dpp::amod_type_keyword;
1913 dpp::automod_metadata metadata1;
1914 metadata1.keywords.emplace_back("*cat*");
1915 metadata1.keywords.emplace_back("train");
1916 metadata1.keywords.emplace_back("*.exe");
1917 metadata1.regex_patterns.emplace_back("^[^a-z]$");
1918 metadata1.allow_list.emplace_back("@silent*");
1919 automodRule.trigger_metadata = metadata1;
1920 dpp::automod_action automodAction;
1921 automodAction.type = dpp::amod_action_timeout;
1922 automodAction.duration_seconds = 6000;
1923 automodRule.actions.emplace_back(automodAction);
1924
1925 bot.automod_rules_get(TEST_GUILD_ID, [&bot, automodRule](const dpp::confirmation_callback_t &event) {
1926 if (event.is_error()) {
1927 return;
1928 }
1929 auto rules = event.get<dpp::automod_rule_map>();
1930 set_test(AUTOMOD_RULE_GET_ALL, true);
1931 for (const auto &rule: rules) {
1932 if (rule.second.trigger_type == dpp::amod_type_keyword) {
1933 // delete one automod rule of type KEYWORD before creating one to make space...
1934 bot.automod_rule_delete(TEST_GUILD_ID, rule.first);
1935 }
1936 }
1937
1938 // start creating the automod rules
1939 bot.automod_rule_create(TEST_GUILD_ID, automodRule, [&bot, automodRule](const dpp::confirmation_callback_t &event) {
1940 if (event.is_error()) {
1941 return;
1942 }
1943 auto created = event.get<dpp::automod_rule>();
1944 if (created.name == automodRule.name) {
1945 set_test(AUTOMOD_RULE_CREATE, true);
1946 }
1947
1948 // get automod rule
1949 bot.automod_rule_get(TEST_GUILD_ID, created.id, [automodRule, &bot, created](const dpp::confirmation_callback_t &event) {
1950 if (event.is_error()) {
1951 return;
1952 }
1953 auto retrieved = event.get<dpp::automod_rule>();
1954 if (retrieved.name == automodRule.name &&
1955 retrieved.trigger_type == automodRule.trigger_type &&
1956 retrieved.trigger_metadata.keywords == automodRule.trigger_metadata.keywords &&
1957 retrieved.trigger_metadata.regex_patterns == automodRule.trigger_metadata.regex_patterns &&
1958 retrieved.trigger_metadata.allow_list == automodRule.trigger_metadata.allow_list && retrieved.actions.size() == automodRule.actions.size()) {
1959 set_test(AUTOMOD_RULE_GET, true);
1960 }
1961
1962 // delete the automod rule
1963 bot.automod_rule_delete(TEST_GUILD_ID, retrieved.id, [](const dpp::confirmation_callback_t &event) {
1964 if (!event.is_error()) {
1965 set_test(AUTOMOD_RULE_DELETE, true);
1966 }
1967 });
1968 });
1969 });
1970 });
1971 }
1972
1973 set_test(USER_GET, false);
1974 set_test(USER_GET_FLAGS, false);
1975 if (!offline) {
1976 bot.user_get(TEST_USER_ID, [](const dpp::confirmation_callback_t &event) {
1977 if (!event.is_error()) {
1978 auto u = std::get<dpp::user_identified>(event.value);
1979 if (u.id == TEST_USER_ID) {
1980 set_test(USER_GET, true);
1981 } else {
1982 set_test(USER_GET, false);
1983 }
1984 json j = json::parse(event.http_info.body);
1985 uint64_t raw_flags = j["public_flags"];
1986 if (j.contains("flags")) {
1987 uint64_t flags = j["flags"];
1988 raw_flags |= flags;
1989 }
1990 // testing all user flags from https://discord.com/developers/docs/resources/user#user-object-user-flags
1991 // they're manually set here because the dpp::user_flags don't match to the discord API, so we can't use them to compare with the raw flags!
1992 if (
1993 u.is_discord_employee() == ((raw_flags & (1 << 0)) != 0) &&
1994 u.is_partnered_owner() == ((raw_flags & (1 << 1)) != 0) &&
1995 u.has_hypesquad_events() == ((raw_flags & (1 << 2)) != 0) &&
1996 u.is_bughunter_1() == ((raw_flags & (1 << 3)) != 0) &&
1997 u.is_house_bravery() == ((raw_flags & (1 << 6)) != 0) &&
1998 u.is_house_brilliance() == ((raw_flags & (1 << 7)) != 0) &&
1999 u.is_house_balance() == ((raw_flags & (1 << 8)) != 0) &&
2000 u.is_early_supporter() == ((raw_flags & (1 << 9)) != 0) &&
2001 u.is_team_user() == ((raw_flags & (1 << 10)) != 0) &&
2002 u.is_bughunter_2() == ((raw_flags & (1 << 14)) != 0) &&
2003 u.is_verified_bot() == ((raw_flags & (1 << 16)) != 0) &&
2004 u.is_verified_bot_dev() == ((raw_flags & (1 << 17)) != 0) &&
2005 u.is_certified_moderator() == ((raw_flags & (1 << 18)) != 0) &&
2006 u.is_bot_http_interactions() == ((raw_flags & (1 << 19)) != 0) &&
2007 u.is_active_developer() == ((raw_flags & (1 << 22)) != 0)
2008 ) {
2009 set_test(USER_GET_FLAGS, true);
2010 } else {
2011 set_test(USER_GET_FLAGS, false);
2012 }
2013 } else {
2014 set_test(USER_GET, false);
2015 set_test(USER_GET_FLAGS, false);
2016 }
2017 });
2018 }
2019
2020 set_test(VOICE_CHANNEL_CREATE, false);
2021 set_test(VOICE_CHANNEL_EDIT, false);
2022 set_test(VOICE_CHANNEL_DELETE, false);
2023 if (!offline) {
2024 dpp::channel channel1;
2025 channel1.set_type(dpp::CHANNEL_VOICE)
2026 .set_guild_id(TEST_GUILD_ID)
2027 .set_name("voice1")
2028 .add_permission_overwrite(TEST_GUILD_ID, dpp::ot_role, 0, dpp::p_view_channel)
2029 .set_user_limit(99);
2030 bot.channel_create(channel1, [&bot, channel1](const auto &response) {
2031 if (response.is_error()) {
2032 set_test(VOICE_CHANNEL_CREATE, false);
2033 return;
2034 }
2035 dpp::channel createdChannel = std::get<dpp::channel>(response.value);
2036 if (createdChannel.name == channel1.name && createdChannel.user_limit == 99 && createdChannel.name == "voice1") {
2037 for (auto overwrite: createdChannel.permission_overwrites) {
2038 if (overwrite.id == TEST_GUILD_ID && overwrite.type == dpp::ot_role && overwrite.deny == dpp::p_view_channel) {
2039 set_test(VOICE_CHANNEL_CREATE, true);
2040 break;
2041 }
2042 }
2043
2044 // edit the voice channel
2045 createdChannel.set_name("foobar2");
2046 createdChannel.set_user_limit(2);
2047 for (auto overwrite: createdChannel.permission_overwrites) {
2048 if (overwrite.id == TEST_GUILD_ID) {
2049 overwrite.deny.set(0);
2050 overwrite.allow.set(dpp::p_view_channel);
2051 }
2052 }
2053 bot.channel_edit(createdChannel, [&bot, createdChannel](const auto &response) {
2054 if (response.is_error()) {
2055 set_test(VOICE_CHANNEL_EDIT, false);
2056 return;
2057 }
2058 dpp::channel edited = std::get<dpp::channel>(response.value);
2059 set_test(VOICE_CHANNEL_EDIT, (edited.name == "foobar2" && edited.user_limit == 2));
2060 bot.channel_delete(createdChannel.id, [](const auto &response) {
2061 set_test(VOICE_CHANNEL_DELETE, !response.is_error());
2062 });
2063 });
2064 }
2065 });
2066 }
2067
2068 set_test(FORUM_CREATION, false);
2069 set_test(FORUM_CHANNEL_GET, false);
2070 set_test(FORUM_CHANNEL_DELETE, false);
2071 if (!offline) {
2072 dpp::channel c;
2073 c.name = "test-forum-channel";
2074 c.guild_id = TEST_GUILD_ID;
2075 c.set_topic("This is a forum channel");
2076 c.set_flags(dpp::CHANNEL_FORUM);
2077 c.default_sort_order = dpp::so_creation_date;
2078 dpp::forum_tag t;
2079 t.name = "Alpha";
2080 t.emoji = "❌";
2081 c.available_tags = {t};
2082 c.default_auto_archive_duration = dpp::arc_1_day;
2083 c.default_reaction = "✅";
2084 c.default_thread_rate_limit_per_user = 10;
2085 bot.channel_create(c, [&bot](const dpp::confirmation_callback_t &event) {
2086 if (!event.is_error()) {
2087 set_test(FORUM_CREATION, true);
2088 auto channel = std::get<dpp::channel>(event.value);
2089 // retrieve the forum channel and check the values
2090 bot.channel_get(channel.id, [forum_id = channel.id, &bot](const dpp::confirmation_callback_t &event) {
2091 if (!event.is_error()) {
2092 auto channel = std::get<dpp::channel>(event.value);
2093 bot.log(dpp::ll_debug, event.http_info.body);
2094 bool tag = false;
2095 for (auto &t: channel.available_tags) {
2096 if (t.name == "Alpha" && std::holds_alternative<std::string>(t.emoji) && std::get<std::string>(t.emoji) == "❌") {
2097 tag = true;
2098 }
2099 }
2100 bool name = channel.name == "test-forum-channel";
2101 bool sort = channel.default_sort_order == dpp::so_creation_date;
2102 bool rateLimit = channel.default_thread_rate_limit_per_user == 10;
2103 set_test(FORUM_CHANNEL_GET, tag && name && sort && rateLimit);
2104 } else {
2105 set_test(FORUM_CHANNEL_GET, false);
2106 }
2107 // delete the forum channel
2108 bot.channel_delete(forum_id, [](const dpp::confirmation_callback_t &event) {
2109 if (!event.is_error()) {
2110 set_test(FORUM_CHANNEL_DELETE, true);
2111 } else {
2112 set_test(FORUM_CHANNEL_DELETE, false);
2113 }
2114 });
2115 });
2116 } else {
2117 set_test(FORUM_CREATION, false);
2118 set_test(FORUM_CHANNEL_GET, false);
2119 }
2120 });
2121 }
2122
2123 set_test(THREAD_CREATE, false);
2124 if (!offline) {
2125 bot.thread_create("thread test", TEST_TEXT_CHANNEL_ID, 60, dpp::channel_type::CHANNEL_PUBLIC_THREAD, true, 60, [&](const dpp::confirmation_callback_t &event) {
2126 if (!event.is_error()) {
2127 [[maybe_unused]] const auto &thread = event.get<dpp::thread>();
2128 set_test(THREAD_CREATE, true);
2129 }
2130 // the thread tests are in the on_thread_create event handler
2131 });
2132 }
2133
2134 start_test(POLL_CREATE);
2135 if (!offline) {
2136 dpp::message poll_msg{};
2137
2138 poll_msg.set_poll(dpp::poll{}
2139 .set_question("hello!")
2140 .add_answer("one", dpp::unicode_emoji::one)
2141 .add_answer("two", dpp::unicode_emoji::two)
2142 .add_answer("three", dpp::unicode_emoji::three)
2143 .add_answer("four")
2144 .set_duration(48)
2145 .set_allow_multiselect(true)
2146 ).set_channel_id(TEST_TEXT_CHANNEL_ID);
2147
2148 bot.message_create(poll_msg, [&bot, poll_msg](const dpp::confirmation_callback_t &result) {
2149 if (result.is_error()) {
2150 set_status(POLL_CREATE, ts_failed, result.get_error().human_readable);
2151 return;
2152 }
2153
2154 const dpp::message &m = std::get<dpp::message>(result.value);
2155
2156 if (!m.attached_poll.has_value()) {
2157 set_status(POLL_CREATE, ts_failed, "poll missing in received message");
2158 return;
2159 }
2160
2161 if (m.attached_poll->find_answer(std::numeric_limits<uint32_t>::max()) != nullptr) {
2162 set_status(POLL_CREATE, ts_failed, "poll::find_answer failed to return nullptr");
2163 return;
2164 }
2165
2166 std::array<bool, 4> correct = {false, false, false, false};
2167 int i = 0;
2168 for (const auto &[_, answer]: m.attached_poll->answers) {
2169 if (m.attached_poll->find_answer(answer.id) != &answer.media) {
2170 set_status(POLL_CREATE, ts_failed, "poll::find_answer failed to return valid answer");
2171 return;
2172 }
2173 if (answer.media.text == "one" && answer.media.emoji.name == dpp::unicode_emoji::one) {
2174 if (correct[i]) {
2175 set_status(POLL_CREATE, ts_failed, "poll answer found twice");
2176 return;
2177 }
2178 correct[i] = true;
2179 }
2180 if (answer.media.text == "two" && answer.media.emoji.name == dpp::unicode_emoji::two) {
2181 if (correct[i]) {
2182 set_status(POLL_CREATE, ts_failed, "poll answer found twice");
2183 return;
2184 }
2185 correct[i] = true;
2186 }
2187 if (answer.media.text == "three" && answer.media.emoji.name == dpp::unicode_emoji::three) {
2188 if (correct[i]) {
2189 set_status(POLL_CREATE, ts_failed, "poll answer found twice");
2190 return;
2191 }
2192 correct[i] = true;
2193 }
2194 if (answer.media.text == "four" && answer.media.emoji.name.empty()) {
2195 if (correct[i]) {
2196 set_status(POLL_CREATE, ts_failed, "poll answer found twice");
2197 return;
2198 }
2199 correct[i] = true;
2200 bot.poll_get_answer_voters(m, answer.id, 0, 100, [m, &bot](const dpp::confirmation_callback_t &result) {
2201 if (result.is_error()) {
2202 set_status(POLL_CREATE, ts_failed, "poll_get_answer_voters: " + result.get_error().human_readable);
2203 return;
2204 }
2205
2206 start_test(POLL_END);
2207 bot.poll_end(m, [message_id = m.id, channel_id = m.channel_id, &bot](const dpp::confirmation_callback_t &result) {
2208 if (result.is_error()) {
2209 set_status(POLL_END, ts_failed, result.get_error().human_readable);
2210 return;
2211 }
2212 set_status(POLL_END, ts_success);
2213 bot.message_delete(message_id, channel_id);
2214 });
2215 });
2216 }
2217 ++i;
2218 }
2219 if (correct == std::array<bool, 4>{true, true, true, true}) {
2220 set_status(POLL_CREATE, ts_success);
2221 } else {
2222 set_status(POLL_CREATE, ts_failed, "failed to find the submitted answers");
2223 }
2224 });
2225 }
2226
2227 set_test(MEMBER_GET, false);
2228 if (!offline) {
2229 bot.guild_get_member(TEST_GUILD_ID, TEST_USER_ID, [](const dpp::confirmation_callback_t &event) {
2230 if (!event.is_error()) {
2231 dpp::guild_member m = std::get<dpp::guild_member>(event.value);
2232 if (m.guild_id == TEST_GUILD_ID && m.user_id == TEST_USER_ID) {
2233 set_test(MEMBER_GET, true);
2234 } else {
2235 set_test(MEMBER_GET, false);
2236 }
2237 } else {
2238 set_test(MEMBER_GET, false);
2239 }
2240 });
2241 }
2242
2243 set_test(ROLE_CREATE, false);
2244 set_test(ROLE_EDIT, false);
2245 set_test(ROLE_DELETE, false);
2246 if (!offline) {
2247 dpp::role r;
2248 r.guild_id = TEST_GUILD_ID;
2249 r.name = "Test-Role";
2250 r.permissions.add(dpp::p_move_members);
2251 r.set_flags(dpp::r_mentionable);
2252 r.colour = dpp::colors::moon_yellow;
2253 bot.role_create(r, [&bot, r](const auto cc) {
2254 if (cc.is_error()) {
2255 set_test(ROLE_CREATE, false);
2256 set_test(ROLE_EDIT, false);
2257 set_test(ROLE_DELETE, false);
2258 return;
2259 }
2260 dpp::role createdRole = std::get<dpp::role>(cc.value);
2261 createdRole.guild_id = TEST_GUILD_ID;
2262 if (createdRole.name == r.name &&
2263 createdRole.has_move_members() &&
2264 createdRole.flags & dpp::r_mentionable &&
2265 createdRole.colour == r.colour) {
2266 createdRole.name = "Test-Role-Edited";
2267 set_test(ROLE_CREATE, true);
2268 bot.role_edit(createdRole, [&bot, createdRole](const auto &e) {
2269 if (e.is_error()) {
2270 set_test(ROLE_EDIT, false);
2271 set_test(ROLE_DELETE, false);
2272 return;
2273 }
2274 dpp::role edited = std::get<dpp::role>(e.value);
2275 set_test(ROLE_EDIT, (createdRole.id == edited.id && edited.name == "Test-Role-Edited"));
2276 bot.role_delete(TEST_GUILD_ID, createdRole.id, [](const auto &e) {
2277 set_test(ROLE_DELETE, !e.is_error());
2278 });
2279 });
2280 } else {
2281 set_test(ROLE_CREATE, false);
2282 set_test(ROLE_EDIT, false);
2283 set_test(ROLE_DELETE, false);
2284 }
2285 });
2286 }
2287 };
2288
2289 set_test(BOTSTART, false);
2290 try {
2291 if (!offline) {
2292 bot.start(dpp::st_return);
2293 set_test(BOTSTART, true);
2294 }
2295 }
2296 catch (const std::exception &) {
2297 set_test(BOTSTART, false);
2298 }
2299
2300 dpp::https_client *c{};
2301 dpp::https_client *c2{};
2302 dpp::https_client *c3{};
2303
2304 set_test(HTTPS, false);
2305 if (!offline) {
2306 dpp::multipart_content multipart = dpp::https_client::build_multipart(
2307 "{\"content\":\"test\"}", {"test.txt", "blob.blob"}, {"ABCDEFGHI", "BLOB!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"}, {"text/plain", "application/octet-stream"}
2308 );
2309 try {
2310 c = new dpp::https_client(&bot, "discord.com", 443, "/api/channels/" + std::to_string(TEST_TEXT_CHANNEL_ID) + "/messages", "POST", multipart.body,
2311 {
2312 {"Content-Type", multipart.mimetype},
2313 {"Authorization", "Bot " + token}
2314 }, false, 5, "1.1", [](dpp::https_client *c) {
2315 std::string hdr1 = c->get_header("server");
2316 std::string content1 = c->get_content();
2317 set_test(HTTPS, hdr1 == "cloudflare" && c->get_status() == 200);
2318 }
2319 );
2320 }
2321 catch (const dpp::exception &e) {
2322 set_status(HTTPS, ts_failed, e.what());
2323 }
2324
2325 set_test(HTTP, false);
2326 try {
2327 c2 = new dpp::https_client(&bot, "github.com", 80, "/", "GET", "", {}, true, 5, "1.1", [](dpp::https_client *c2) {
2328 std::string hdr2 = c2->get_header("location");
2329 std::string content2 = c2->get_content();
2330 set_test(HTTP, hdr2 == "https://github.com/" && c2->get_status() == 301);
2331 });
2332 }
2333 catch (const dpp::exception &e) {
2334 set_status(HTTP, ts_failed, e.what());
2335 }
2336
2337 set_test(MULTIHEADER, false);
2338 try {
2339 c3 = new dpp::https_client(&bot, "dl.dpp.dev", 443, "/cookietest.php", "GET", "", {}, true, 5, "1.1", [](dpp::https_client *c2) {
2340 size_t count = c2->get_header_count("set-cookie");
2341 size_t count_list = c2->get_header_list("set-cookie").size();
2342 // This test script sets a bunch of cookies when we request it.
2343 set_test(MULTIHEADER, c2->get_status() == 200 && count > 1 && count == count_list);
2344 });
2345 }
2346 catch (const dpp::exception &e) {
2347 set_status(MULTIHEADER, ts_failed, e.what());
2348 }
2349 }
2350
2351 set_test(TIMERSTART, false);
2352 dpp::timer th = bot.start_timer([ticks = 0](dpp::timer timer_handle) mutable {
2353 if (ticks == 2) {
2354 /* The simple test timer ticks every second.
2355 * If we get to 2 seconds, we know the timer is working.
2356 */
2357 set_test(TIMERSTART, true);
2358 }
2359 ticks++;
2360 }, 1);
2361
2362 set_test(USER_GET_CACHED_PRESENT, false);
2363 try {
2364 bot.user_get_cached(TEST_USER_ID, [](const auto &e) {
2365 if (e.is_error()) {
2366 set_test(USER_GET_CACHED_PRESENT, false);
2367 return;
2368 }
2369 dpp::user_identified u = std::get<dpp::user_identified>(e.value);
2370 set_test(USER_GET_CACHED_PRESENT, (u.id == TEST_USER_ID));
2371 });
2372 }
2373 catch (const std::exception &) {
2374 set_test(USER_GET_CACHED_PRESENT, false);
2375 }
2376
2377 set_test(USER_GET_CACHED_ABSENT, false);
2378 try {
2379 /* This is the snowflake ID of a discord staff member.
2380 * We assume here that staffer's discord IDs will remain constant
2381 * for long periods of time and they won't lurk in the unit test server.
2382 * If this becomes not true any more, we'll pick another well known
2383 * user ID.
2384 */
2385 bot.user_get_cached(90339695967350784, [](const auto &e) {
2386 if (e.is_error()) {
2387 set_test(USER_GET_CACHED_ABSENT, false);
2388 return;
2389 }
2390 dpp::user_identified u = std::get<dpp::user_identified>(e.value);
2391 set_test(USER_GET_CACHED_ABSENT, (u.id == 90339695967350784));
2392 });
2393 }
2394 catch (const std::exception &) {
2395 set_test(USER_GET_CACHED_ABSENT, false);
2396 }
2397
2398 set_test(TIMEDLISTENER, false);
2399 dpp::timed_listener tl(&bot, 10, bot.on_log, [&](const dpp::log_t &event) {
2400 set_test(TIMEDLISTENER, true);
2401 });
2402
2403 set_test(ONESHOT, false);
2404 bool once = false;
2405 dpp::oneshot_timer ost(&bot, 5, [&](dpp::timer timer_handle) {
2406 if (!once) {
2407 set_test(ONESHOT, true);
2408 } else {
2409 set_test(ONESHOT, false);
2410 }
2411 once = true;
2412 });
2413
2414 set_test(CUSTOMCACHE, false);
2415 dpp::cache<test_cached_object_t> testcache;
2416 test_cached_object_t *tco = new test_cached_object_t(666);
2417 tco->foo = "bar";
2418 testcache.store(tco);
2419 test_cached_object_t *found_tco = testcache.find(666);
2420 if (found_tco && found_tco->id == dpp::snowflake(666) && found_tco->foo == "bar") {
2421 set_test(CUSTOMCACHE, true);
2422 } else {
2423 set_test(CUSTOMCACHE, false);
2424 }
2425 testcache.remove(found_tco);
2426
2427 if (!offline) {
2428 if (std::future_status status = ready_future.wait_for(std::chrono::seconds(20)); status != std::future_status::timeout) {
2429 do_online_tests();
2430 }
2431 }
2432
2433 noparam_api_test(current_user_get, dpp::user_identified, CURRENTUSER);
2434 singleparam_api_test(channel_get, TEST_TEXT_CHANNEL_ID, dpp::channel, GETCHAN);
2435 singleparam_api_test(guild_get, TEST_GUILD_ID, dpp::guild, GETGUILD);
2436 singleparam_api_test_list(roles_get, TEST_GUILD_ID, dpp::role_map, GETROLES);
2437 singleparam_api_test_list(channels_get, TEST_GUILD_ID, dpp::channel_map, GETCHANS);
2438 singleparam_api_test_list(guild_get_invites, TEST_GUILD_ID, dpp::invite_map, GETGUILDINVS);
2439 multiparam_api_test_list(guild_get_bans, TEST_GUILD_ID, dpp::ban_map, GETBANS);
2440 singleparam_api_test_list(channel_pins_get, TEST_TEXT_CHANNEL_ID, dpp::message_pin_map, GETPINS);
2441 singleparam_api_test_list(guild_events_get, TEST_GUILD_ID, dpp::scheduled_event_map, GETEVENTS);
2442 twoparam_api_test(guild_event_get, TEST_GUILD_ID, TEST_EVENT_ID, dpp::scheduled_event, GETEVENT);
2443 twoparam_api_test_list(guild_event_users_get, TEST_GUILD_ID, TEST_EVENT_ID, dpp::event_member_map, GETEVENTUSERS);
2444
2445 std::this_thread::sleep_for(std::chrono::seconds(20));
2446
2447 /* Test stopping timer */
2448 set_test(TIMERSTOP, false);
2449 set_test(TIMERSTOP, bot.stop_timer(th));
2450
2451 set_test(USERCACHE, false);
2452 if (!offline) {
2453 dpp::user *u = dpp::find_user(TEST_USER_ID);
2454 set_test(USERCACHE, u);
2455 }
2456 set_test(CHANNELCACHE, false);
2457 set_test(CHANNELTYPES, false);
2458 if (!offline) {
2459 dpp::channel *c = dpp::find_channel(TEST_TEXT_CHANNEL_ID);
2460 dpp::channel *c2 = dpp::find_channel(TEST_VC_ID);
2461 set_test(CHANNELCACHE, c && c2);
2462 set_test(CHANNELTYPES, c && c->is_text_channel() && !c->is_voice_channel() && c2 && c2->is_voice_channel() && !c2->is_text_channel());
2463 }
2464
2465 wait_for_tests();
2466
2467 delete c;
2468 delete c2;
2469 delete c3;
2470
2471 }
2472 catch (const std::exception &e) {
2473 set_status(CLUSTER, ts_failed, e.what());
2474 }
2475
2476 /* Return value = number of failed tests, exit code 0 = success */
2477 return test_summary();
2478
2479 }
2480 catch (const std::exception& e) {
2481 std::cerr << "Unhandled std::exception thrown: " << std::string(e.what()) << "\n";
2482 exit(99);
2483 }
2484 catch (const std::string& e) {
2485 /* This is a safety net, none of our stuff throws strings! */
2486 std::cerr << "Unhandled std::string thrown: " << e << "\n";
2487 exit(99);
2488 }
2489 catch (...) {
2490 std::cerr << "Unhandled '...' thrown\n";
2491 exit(99);
2492 }
2493 }