Files
VoidTeamspeak/tests/test_modern_api.cpp
faumaray 89911ff107 Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-15 21:08:28 +03:00

288 lines
10 KiB
C++

#include "check.hpp"
#include <Void/TeamSpeak/TeamSpeak.hpp>
#include "../src/EventParser.hpp"
#include <chrono>
#include <type_traits>
#include <utility>
using namespace Void::TeamSpeak;
static_assert(!std::is_copy_constructible_v<Client>);
static_assert(std::is_move_constructible_v<Client>);
static_assert(std::is_same_v<decltype(Client{}.id()), ClientId>);
static_assert(std::is_same_v<decltype(ChannelGroup{}.id), ChannelGroupId>);
static_assert(std::is_same_v<decltype(Permission{}.id), PermissionId>);
static_assert(std::is_same_v<decltype(Ban{}.id), BanId>);
static_assert(
std::is_same_v<decltype(RemoteClient{}.databaseId), ClientDatabaseId>);
static_assert(
std::is_same_v<decltype(ServerInfo{}.defaultServerGroup), ServerGroupId>);
static_assert(std::is_same_v<decltype(Events{}.clientUpdated),
std::function<void(const ClientUpdated &)>>);
static_assert(std::is_same_v<decltype(Events{}.talkStatusChanged),
std::function<void(const TalkStatusChanged &)>>);
static_assert(std::is_same_v<decltype(Events{}.channelChanged),
std::function<void(const ChannelChanged &)>>);
static_assert(std::is_same_v<decltype(ChannelChanged{}.patch), ChannelPatch>);
static_assert(std::is_same_v<decltype(Events{}.serverUpdated),
std::function<void(const ServerUpdated &)>>);
static_assert(std::is_same_v<decltype(Events{}.fileTransferStatus),
std::function<void(const FileTransferStatus &)>>);
static_assert(std::is_same_v<decltype(std::declval<Client &>().deleteFile("x")),
Void::Result::Result<void, Error>>);
static_assert(
std::is_same_v<decltype(std::declval<Client &>().ban(ClientId{1})),
Void::Result::Result<BanId, Error>>);
static_assert(std::is_same_v<decltype(std::declval<Client &>().createChannel(
std::declval<const ChannelCreateOptions &>())),
Void::Result::Result<ChannelId, Error>>);
static_assert(std::is_same_v<decltype(std::declval<Client &>().serverInfo()),
Void::Result::Result<ServerInfo, Error>>);
static_assert(std::is_same_v<decltype(std::declval<Client &>().bans()),
Void::Result::Result<std::vector<Ban>, Error>>);
static_assert(std::is_same_v<
decltype(std::declval<Client &>().clientDetails(ClientId{1})),
Void::Result::Result<ClientDetails, Error>>);
static_assert(
std::is_same_v<decltype(std::declval<Client &>().subscribeAllChannels()),
Void::Result::Result<void, Error>>);
TEST_CASE(modern_build_command_escapes_fields)
{
const auto command = buildCommand(
"sendtextmessage", {{"msg", "hello world|all"}, {"target", "7"}});
CHECK_EQ(command,
std::string("sendtextmessage msg=hello\\sworld\\pall target=7"));
}
TEST_CASE(strong_ids_do_not_implicitly_convert)
{
const ClientId client{42};
const ChannelId channel{42};
CHECK(client.value == 42);
CHECK(channel.value == 42);
CHECK(static_cast<bool>(client));
CHECK(static_cast<bool>(channel));
}
TEST_CASE(whisper_targets_report_empty_state)
{
WhisperTargets targets;
CHECK(targets.empty());
targets.clients.push_back(ClientId{7});
CHECK(!targets.empty());
}
TEST_CASE(channel_edit_options_preserve_explicit_false)
{
ChannelEditOptions options;
CHECK(options.empty());
options.defaultChannel = false;
CHECK(!options.empty());
}
TEST_CASE(channel_patch_preserves_fields_omitted_by_sparse_events)
{
Channel cached{.id = ChannelId{7},
.parent = ChannelId{2},
.orderAfter = ChannelId{6},
.name = "Original",
.topic = "Keep topic",
.description = "Keep description",
.codec = 4,
.codecQuality = 8,
.maxClients = 32,
.maxFamilyClients = 64,
.neededTalkPower = 25,
.iconId = 99,
.passwordProtected = true,
.permanent = true,
.defaultChannel = true,
.unlimitedClients = true};
const ChannelPatch patch{.name = "Renamed"};
patch.applyTo(cached);
CHECK_EQ(cached.name, std::string{"Renamed"});
CHECK_EQ(cached.topic, std::string{"Keep topic"});
CHECK_EQ(cached.description, std::string{"Keep description"});
CHECK(cached.parent == std::optional{ChannelId{2}});
CHECK(cached.orderAfter == ChannelId{6});
CHECK(cached.codec == 4);
CHECK(cached.codecQuality == 8);
CHECK(cached.maxClients == 32);
CHECK(cached.maxFamilyClients == 64);
CHECK(cached.neededTalkPower == 25);
CHECK(cached.iconId == 99);
CHECK(cached.passwordProtected);
CHECK(cached.permanent);
CHECK(cached.defaultChannel);
CHECK(cached.unlimitedClients);
}
TEST_CASE(channel_patch_applies_explicit_empty_zero_false_and_root_values)
{
Channel cached{.id = ChannelId{7},
.parent = ChannelId{2},
.orderAfter = ChannelId{6},
.topic = "Topic",
.description = "Description",
.maxClients = 32,
.iconId = 99,
.passwordProtected = true,
.defaultChannel = true};
const ChannelPatch patch{.parent = std::optional<ChannelId>{},
.orderAfter = ChannelId{},
.topic = "",
.description = "",
.maxClients = 0,
.iconId = 0,
.passwordProtected = false,
.defaultChannel = false};
CHECK(!patch.empty());
patch.applyTo(cached);
CHECK(!cached.parent);
CHECK(!static_cast<bool>(cached.orderAfter));
CHECK(cached.topic.empty());
CHECK(cached.description.empty());
CHECK(cached.maxClients == 0);
CHECK(cached.iconId == 0);
CHECK(!cached.passwordProtected);
CHECK(!cached.defaultChannel);
}
TEST_CASE(channel_events_decode_only_fields_present_in_the_notification)
{
const CommandFields record{{"cid", "42"},
{"channel_name", "Renamed"},
{"channel_flag_default", "0"},
{"reasonid", "10"},
{"invokerid", "7"}};
const auto event =
Detail::parseChannelChanged("notifychanneledited", record);
CHECK(event.has_value());
CHECK(event->kind == ChannelChangeKind::Updated);
CHECK(event->id == ChannelId{42});
CHECK(event->invoker == ClientId{7});
CHECK(event->reasonId == 10);
CHECK(event->patch.name == std::optional<std::string>{"Renamed"});
CHECK(event->patch.defaultChannel == std::optional<bool>{false});
CHECK(!event->patch.topic);
CHECK(!event->patch.description);
CHECK(!event->patch.codec);
CHECK(!event->patch.parent);
CHECK(!event->patch.orderAfter);
}
TEST_CASE(channel_move_events_preserve_explicit_root_and_zero_order)
{
const CommandFields record{{"cid", "42"}, {"cpid", "0"}, {"order", "0"}};
const auto event =
Detail::parseChannelChanged("notifychannelmoved", record);
CHECK(event.has_value());
CHECK(event->patch.parent.has_value());
CHECK(!*event->patch.parent);
CHECK(event->patch.orderAfter == std::optional{ChannelId{}});
Channel cached{.id = ChannelId{42},
.parent = ChannelId{2},
.orderAfter = ChannelId{6},
.name = "Keep"};
event->patch.applyTo(cached);
CHECK(!cached.parent);
CHECK(!static_cast<bool>(cached.orderAfter));
CHECK_EQ(cached.name, std::string{"Keep"});
}
TEST_CASE(channel_events_reject_invalid_identity_and_ignore_invalid_fields)
{
CHECK(!Detail::parseChannelChanged("notifychannelmoved", {{"cpid", "2"}}));
CHECK(!Detail::parseChannelChanged("unrelated", {{"cid", "42"}}));
const auto event = Detail::parseChannelChanged(
"notifychanneledited",
{{"cid", "42"}, {"channel_codec", "not-a-number"}});
CHECK(event.has_value());
CHECK(!event->patch.codec);
}
TEST_CASE(ban_models_permanent_and_timed_duration)
{
const Ban permanent{.duration = std::chrono::seconds::zero()};
const Ban timed{.duration = std::chrono::minutes{10}};
CHECK(permanent.permanent());
CHECK(!timed.permanent());
}
TEST_CASE(remote_client_derives_public_status_flags)
{
const RemoteClient query{
.type = 1, .inputHardware = false, .outputHardware = false};
CHECK(query.isQuery());
CHECK(query.muted());
CHECK(query.deafened());
const RemoteClient voice;
CHECK(!voice.isQuery());
CHECK(!voice.muted());
CHECK(!voice.deafened());
}
TEST_CASE(administration_operations_reject_invalid_identifiers)
{
Client client;
const auto ban = client.ban(ClientId{});
const auto unban = client.unban(BanId{});
const auto edit =
client.editChannel(ChannelId{}, ChannelEditOptions{.name = "name"});
const auto reorder = client.reorderChannel(ChannelId{}, std::nullopt);
const auto remove = client.deleteChannel(ChannelId{});
const auto addGroup =
client.addServerGroupMember(ServerGroupId{}, ClientDatabaseId{1});
const auto move = client.move(ClientId{1}, ChannelId{});
const auto kick = client.kick(ClientId{}, KickScope::Channel, "reason");
const auto details = client.clientDetails(ClientId{});
CHECK(!ban && ban.error().code == Error::Code::InvalidArgument);
CHECK(!unban && unban.error().code == Error::Code::InvalidArgument);
CHECK(!edit && edit.error().code == Error::Code::InvalidArgument);
CHECK(!reorder && reorder.error().code == Error::Code::InvalidArgument);
CHECK(!remove && remove.error().code == Error::Code::InvalidArgument);
CHECK(!addGroup && addGroup.error().code == Error::Code::InvalidArgument);
CHECK(!move && move.error().code == Error::Code::InvalidArgument);
CHECK(!kick && kick.error().code == Error::Code::InvalidArgument);
CHECK(!details && details.error().code == Error::Code::InvalidArgument);
}
TEST_CASE(administration_operations_validate_payloads_before_transport)
{
Client client;
const auto missingName = client.createChannel(ChannelCreateOptions{});
const auto emptyEdit = client.editChannel(ChannelId{1}, {});
const auto invalidQuality = client.createChannel(
ChannelCreateOptions{.name = "Talk", .codecQuality = 11});
const auto invalidDuration = client.ban(
ClientId{1}, BanOptions{.duration = std::chrono::seconds::zero()});
const auto emptyMessage = client.sendServerMessage("");
const auto missingTarget = client.sendPrivateMessage(ClientId{}, "hello");
CHECK(!missingName &&
missingName.error().code == Error::Code::InvalidArgument);
CHECK(!emptyEdit && emptyEdit.error().code == Error::Code::InvalidArgument);
CHECK(!invalidQuality &&
invalidQuality.error().code == Error::Code::InvalidArgument);
CHECK(!invalidDuration &&
invalidDuration.error().code == Error::Code::InvalidArgument);
CHECK(!emptyMessage &&
emptyMessage.error().code == Error::Code::InvalidArgument);
CHECK(!missingTarget &&
missingTarget.error().code == Error::Code::InvalidArgument);
}