189 lines
5.7 KiB
C++
189 lines
5.7 KiB
C++
#include "echoion/core/TeamSpeakDirectoryService.hpp"
|
|
#include "echoion/core/detail/RpcValidation.hpp"
|
|
|
|
#include <Void/Data/Json.hpp>
|
|
|
|
#include <cassert>
|
|
#include <memory>
|
|
#include <span>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
class FakePlugin final : public Echoion::Api::IPluginInterface
|
|
{
|
|
public:
|
|
|
|
Echoion::Api::Result<void> initialize() override { return {}; }
|
|
Echoion::Api::Result<void> shutdown() override { return {}; }
|
|
Echoion::Api::Result<std::vector<Echoion::Api::ClientInfo>>
|
|
getClients() override
|
|
{
|
|
return clients;
|
|
}
|
|
Echoion::Api::Result<std::optional<Echoion::Api::ClientInfo>>
|
|
getClient(uint64_t id) override
|
|
{
|
|
for (const auto& client : clients) {
|
|
if (client.id == id) {
|
|
return client;
|
|
}
|
|
}
|
|
return std::optional<Echoion::Api::ClientInfo>{};
|
|
}
|
|
Echoion::Api::Result<void> moveClient(uint64_t, uint64_t) override
|
|
{
|
|
return {};
|
|
}
|
|
Echoion::Api::Result<void> kickClient(uint64_t,
|
|
const std::string&) override
|
|
{
|
|
return {};
|
|
}
|
|
Echoion::Api::Result<std::vector<Echoion::Api::ChannelInfo>>
|
|
getChannels() const override
|
|
{
|
|
return channels;
|
|
}
|
|
Echoion::Api::Result<std::optional<Echoion::Api::ChannelInfo>>
|
|
getChannel(uint64_t id) const override
|
|
{
|
|
for (const auto& channel : channels) {
|
|
if (channel.id == id) {
|
|
return channel;
|
|
}
|
|
}
|
|
return std::optional<Echoion::Api::ChannelInfo>{};
|
|
}
|
|
Echoion::Api::Result<void>
|
|
sendTextMessage(uint64_t, const std::string&) override
|
|
{
|
|
return {};
|
|
}
|
|
Echoion::Api::Result<void>
|
|
sendChannelMessage(uint64_t, const std::string&) override
|
|
{
|
|
return {};
|
|
}
|
|
Echoion::Api::Result<void>
|
|
sendServerMessage(const std::string&) override
|
|
{
|
|
return {};
|
|
}
|
|
Echoion::Api::Result<void>
|
|
sendAudioData(const std::span<const short>&) override
|
|
{
|
|
return {};
|
|
}
|
|
Echoion::Api::Result<void>
|
|
setWhisperList(const std::vector<uint64_t>&) override
|
|
{
|
|
return {};
|
|
}
|
|
Echoion::Api::Result<void>
|
|
connectToServer(const Echoion::Api::ServerConfig&) override
|
|
{
|
|
return {};
|
|
}
|
|
Echoion::Api::Result<void> disconnectFromServer() override
|
|
{
|
|
return {};
|
|
}
|
|
bool isConnected() const override { return connected; }
|
|
Echoion::Api::Result<void> reconnect() override { return {}; }
|
|
void registerEventCallback(
|
|
const Echoion::Api::EventCallback&) override
|
|
{
|
|
}
|
|
void unregisterEventCallback(
|
|
const Echoion::Api::EventCallback&) override
|
|
{
|
|
}
|
|
Echoion::Api::Result<Echoion::Api::ServerInfo>
|
|
getTeamSpeakServerInfo() override
|
|
{
|
|
return Echoion::Api::ServerInfo{
|
|
.name = "Fixture", .welcomeMessage = "", .maxClients = 64};
|
|
}
|
|
|
|
std::vector<Echoion::Api::ClientInfo> clients;
|
|
std::vector<Echoion::Api::ChannelInfo> channels;
|
|
bool connected = true;
|
|
};
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
using Echoion::Api::ChannelInfo;
|
|
using Echoion::Api::ClientInfo;
|
|
using Echoion::Core::TeamSpeakDirectoryService;
|
|
|
|
const auto validId = Void::Json::parse(R"({"id":42})");
|
|
const auto fractionalId = Void::Json::parse(R"({"id":1.5})");
|
|
const auto negativeId = Void::Json::parse(R"({"id":-1})");
|
|
const auto overflowingId =
|
|
Void::Json::parse(R"({"id":18446744073709551616})");
|
|
const auto enormousId = Void::Json::parse(R"({"id":1e100})");
|
|
assert(validId && Echoion::Core::Detail::uintField(*validId, "id") == 42);
|
|
assert(fractionalId &&
|
|
!Echoion::Core::Detail::uintField(*fractionalId, "id"));
|
|
assert(negativeId && !Echoion::Core::Detail::uintField(*negativeId, "id"));
|
|
assert(overflowingId &&
|
|
!Echoion::Core::Detail::uintField(*overflowingId, "id"));
|
|
assert(enormousId && !Echoion::Core::Detail::uintField(*enormousId, "id"));
|
|
|
|
auto plugin = std::make_shared<FakePlugin>();
|
|
plugin->channels = {
|
|
ChannelInfo{.id = 2, .name = "Second", .parentId = 0, .order = 1},
|
|
ChannelInfo{.id = 1, .name = "First", .parentId = 0, .order = 0},
|
|
ChannelInfo{.id = 3, .name = "Child", .parentId = 1, .order = 0}};
|
|
plugin->clients = {ClientInfo{.id = 10,
|
|
.databaseId = 110,
|
|
.nickname = "Visible",
|
|
.uniqueIdentifier = "secret-uid",
|
|
.channelId = 1,
|
|
.isTalking = true,
|
|
.serverGroups = {7}},
|
|
ClientInfo{.id = 11,
|
|
.databaseId = std::nullopt,
|
|
.nickname = "Query Client",
|
|
.uniqueIdentifier = "query-secret",
|
|
.channelId = 1,
|
|
.isQuery = true,
|
|
.serverGroups = {}}};
|
|
|
|
TeamSpeakDirectoryService service;
|
|
service.setPluginInterface(plugin);
|
|
const auto publicJson = Void::Json::serialize(service.publicJson());
|
|
assert(publicJson.contains("Visible"));
|
|
assert(publicJson.find("First") < publicJson.find("Second"));
|
|
assert(!publicJson.contains("Query Client"));
|
|
assert(!publicJson.contains("secret-uid"));
|
|
assert(!publicJson.contains("databaseId"));
|
|
assert(!publicJson.contains("groups"));
|
|
|
|
const auto adminJson = Void::Json::serialize(service.adminJson());
|
|
assert(adminJson.contains("secret-uid"));
|
|
assert(adminJson.contains("databaseId"));
|
|
assert(!adminJson.contains("Query Client"));
|
|
|
|
const auto ordered = TeamSpeakDirectoryService::orderChannels(
|
|
{ChannelInfo{.id = 2, .name = "", .parentId = std::nullopt, .order = 1},
|
|
ChannelInfo{.id = 1, .name = "", .parentId = std::nullopt, .order = 0},
|
|
ChannelInfo{
|
|
.id = 9, .name = "", .parentId = std::nullopt, .order = 99}});
|
|
assert(ordered.size() == 3);
|
|
assert(ordered[0].id == 1);
|
|
assert(ordered[1].id == 2);
|
|
assert(ordered[2].id == 9);
|
|
|
|
plugin->connected = false;
|
|
plugin->clients.clear();
|
|
plugin->channels.clear();
|
|
service.handleEvent(Echoion::Api::PluginEventType::ChannelUpdated);
|
|
const auto disconnected = Void::Json::serialize(service.publicJson());
|
|
assert(disconnected.contains("\"connected\":false"));
|
|
assert(!disconnected.contains("Visible"));
|
|
}
|