627 lines
23 KiB
C++
627 lines
23 KiB
C++
#include "protocol/protocol_message_builder.h"
|
|
#include "protocol/server_message_dispatcher.h"
|
|
|
|
#include <cassert>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include <cJSON.h>
|
|
|
|
namespace {
|
|
|
|
using namespace borla;
|
|
|
|
cJSON* BuildAndParse(
|
|
bool result,
|
|
const char* output,
|
|
size_t output_size)
|
|
{
|
|
assert(result);
|
|
assert(output_size == std::strlen(output));
|
|
cJSON* root = cJSON_ParseWithLength(output, output_size);
|
|
assert(cJSON_IsObject(root));
|
|
return root;
|
|
}
|
|
|
|
const char* StringField(const cJSON* root, const char* name)
|
|
{
|
|
const cJSON* field =
|
|
cJSON_GetObjectItemCaseSensitive(root, name);
|
|
assert(cJSON_IsString(field));
|
|
return field->valuestring;
|
|
}
|
|
|
|
ServerCommand ParseCommand(
|
|
const ServerMessageDispatcher& dispatcher,
|
|
const char* json,
|
|
size_t size)
|
|
{
|
|
ServerCommand command;
|
|
assert(dispatcher.ParseInto(json, size, &command));
|
|
return command;
|
|
}
|
|
|
|
void TestMessageBuilder()
|
|
{
|
|
ProtocolMessageBuilder builder;
|
|
char output[768]{};
|
|
size_t size = 0;
|
|
|
|
cJSON* root = BuildAndParse(
|
|
builder.BuildHello(
|
|
3, output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(StringField(root, "type"), "hello") == 0);
|
|
assert(cJSON_GetObjectItem(root, "version")->valueint == 3);
|
|
const cJSON* audio = cJSON_GetObjectItem(root, "audio_params");
|
|
assert(cJSON_IsObject(audio));
|
|
assert(cJSON_GetObjectItem(audio, "sample_rate")->valueint == 16000);
|
|
assert(cJSON_GetObjectItem(audio, "frame_duration")->valueint == 60);
|
|
const cJSON* features = cJSON_GetObjectItem(root, "features");
|
|
assert(cJSON_IsTrue(cJSON_GetObjectItem(features, "local_audio")));
|
|
assert(cJSON_IsTrue(
|
|
cJSON_GetObjectItem(features, "play_local_sequence")));
|
|
assert(cJSON_IsTrue(
|
|
cJSON_GetObjectItem(features, "barge_in_two_phase")));
|
|
assert(cJSON_IsTrue(cJSON_GetObjectItem(features, "mcp")));
|
|
cJSON_Delete(root);
|
|
|
|
MemorySnapshot memory;
|
|
std::strcpy(memory.child_name.data(), "小明");
|
|
std::strcpy(memory.brain_text.data(), "喜欢星星");
|
|
memory.interaction_count = 42;
|
|
root = BuildAndParse(
|
|
builder.BuildHello(
|
|
3, &memory, output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
const cJSON* cache =
|
|
cJSON_GetObjectItem(root, "memory_cache");
|
|
assert(cJSON_IsObject(cache));
|
|
assert(std::strcmp(
|
|
StringField(cache, "child_name"), "小明") == 0);
|
|
assert(cJSON_GetObjectItem(
|
|
cache, "interaction_count")->valueint == 42);
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildStartListening(
|
|
"session-a", output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(StringField(root, "session_id"), "session-a") == 0);
|
|
assert(std::strcmp(StringField(root, "type"), "listen") == 0);
|
|
assert(std::strcmp(StringField(root, "state"), "start") == 0);
|
|
assert(std::strcmp(StringField(root, "mode"), "auto") == 0);
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildStatus(
|
|
85, 3875, false, "2.0.0", 64,
|
|
output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(StringField(root, "type"), "status") == 0);
|
|
assert(cJSON_GetObjectItem(root, "battery")->valueint == 85);
|
|
assert(cJSON_GetObjectItem(
|
|
root, "battery_voltage_mv")->valueint == 3875);
|
|
assert(cJSON_GetObjectItem(root, "volume")->valueint == 64);
|
|
cJSON_Delete(root);
|
|
|
|
// A negative volume means "unknown": the field is omitted rather than
|
|
// reported as 0, which the parent app would render as muted.
|
|
root = BuildAndParse(
|
|
builder.BuildStatus(
|
|
85, 3875, true, "2.0.0", -1,
|
|
output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(cJSON_GetObjectItem(root, "volume") == nullptr);
|
|
assert(cJSON_IsTrue(cJSON_GetObjectItem(root, "charging")));
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildButton(
|
|
3, output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(StringField(root, "type"), "button") == 0);
|
|
assert(cJSON_GetObjectItem(root, "count")->valueint == 3);
|
|
cJSON_Delete(root);
|
|
assert(!builder.BuildButton(
|
|
2, output, sizeof(output), &size));
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildLocalPlaybackState(
|
|
"snore_light", true,
|
|
output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(
|
|
StringField(root, "type"), "play_local") == 0);
|
|
assert(std::strcmp(
|
|
StringField(root, "state"), "start") == 0);
|
|
assert(std::strcmp(
|
|
StringField(root, "group"), "snore_light") == 0);
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildGoodbye(
|
|
output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(
|
|
StringField(root, "type"), "goodbye") == 0);
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildLocalPlaybackState(
|
|
"snore_light", false,
|
|
output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(
|
|
StringField(root, "state"), "done") == 0);
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildAlarmFired(
|
|
42, 1744123460,
|
|
output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(
|
|
StringField(root, "type"), "alarm_fired") == 0);
|
|
assert(cJSON_GetObjectItem(root, "id")->valueint == 42);
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildCallAction(
|
|
"accept", "call-42",
|
|
output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(StringField(root, "type"), "call") == 0);
|
|
assert(std::strcmp(StringField(root, "state"), "accept") == 0);
|
|
assert(std::strcmp(StringField(root, "call_id"), "call-42") == 0);
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildFriendAdd(
|
|
"3c:dc:75:fb:12:fc",
|
|
"黑马",
|
|
"3c:dc:75:fa:73:08",
|
|
"BorlaF-7308",
|
|
output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(StringField(root, "type"), "friend_add") == 0);
|
|
assert(std::strcmp(
|
|
StringField(root, "remote_name"),
|
|
"BorlaF-7308") == 0);
|
|
cJSON_Delete(root);
|
|
|
|
// A mandatory upgrade announcement must omit "optional" entirely: the
|
|
// server contract treats an absent field as "device is going to install".
|
|
root = BuildAndParse(
|
|
builder.BuildOtaNotify(
|
|
"当前有最新版本,即将开始升级!请耐心等候",
|
|
false,
|
|
output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(StringField(root, "type"), "ota_notify") == 0);
|
|
assert(std::strcmp(
|
|
StringField(root, "text"),
|
|
"当前有最新版本,即将开始升级!请耐心等候") == 0);
|
|
assert(cJSON_GetObjectItem(root, "optional") == nullptr);
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildOtaNotify(
|
|
"当前设备有最新版本,是否进行升级?",
|
|
true,
|
|
output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(cJSON_IsTrue(cJSON_GetObjectItem(root, "optional")));
|
|
cJSON_Delete(root);
|
|
|
|
assert(!builder.BuildOtaNotify(
|
|
"", false, output, sizeof(output), &size));
|
|
assert(!builder.BuildOtaNotify(
|
|
nullptr, false, output, sizeof(output), &size));
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildStopListening(
|
|
"session-a", output, sizeof(output), &size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(StringField(root, "state"), "stop") == 0);
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildAbort(
|
|
"session-a",
|
|
ConversationInterruptSource::kUserInitiated,
|
|
nullptr,
|
|
output,
|
|
sizeof(output),
|
|
&size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(StringField(root, "type"), "abort") == 0);
|
|
assert(std::strcmp(
|
|
StringField(root, "reason"),
|
|
"user_initiated") == 0);
|
|
// Nothing was playing, so the field must be absent rather than empty.
|
|
assert(cJSON_GetObjectItem(root, "playback_text") == nullptr);
|
|
cJSON_Delete(root);
|
|
|
|
root = BuildAndParse(
|
|
builder.BuildAbort(
|
|
"session-a",
|
|
ConversationInterruptSource::kWakeWordDetected,
|
|
"在很久很久以前",
|
|
output,
|
|
sizeof(output),
|
|
&size),
|
|
output,
|
|
size);
|
|
assert(std::strcmp(StringField(root, "type"), "abort") == 0);
|
|
assert(std::strcmp(
|
|
StringField(root, "reason"),
|
|
"wake_word_detected") == 0);
|
|
assert(std::strcmp(
|
|
StringField(root, "playback_text"),
|
|
"在很久很久以前") == 0);
|
|
cJSON_Delete(root);
|
|
|
|
// A sentence longer than the 100-byte cap must be cut on a character
|
|
// boundary. The previous firmware cut on a byte boundary here and the
|
|
// server closed the connection with 1007 (invalid frame payload data),
|
|
// which showed up as stories stopping halfway through.
|
|
{
|
|
std::string long_sentence;
|
|
while (long_sentence.size() < 300) {
|
|
long_sentence += "小"; // 3 bytes each
|
|
}
|
|
root = BuildAndParse(
|
|
builder.BuildAbort(
|
|
"session-a",
|
|
ConversationInterruptSource::kUserInitiated,
|
|
long_sentence.c_str(),
|
|
output,
|
|
sizeof(output),
|
|
&size),
|
|
output,
|
|
size);
|
|
const char* truncated =
|
|
StringField(root, "playback_text");
|
|
const size_t truncated_size = std::strlen(truncated);
|
|
assert(truncated_size <=
|
|
ProtocolMessageBuilder::kPlaybackTextBytes);
|
|
// 100 / 3 = 33 whole characters, so 99 bytes — never 100, which would
|
|
// mean the last character was split.
|
|
assert(truncated_size % 3 == 0);
|
|
assert(std::strncmp(
|
|
truncated,
|
|
long_sentence.c_str(),
|
|
truncated_size) == 0);
|
|
cJSON_Delete(root);
|
|
}
|
|
|
|
char too_small[4]{};
|
|
assert(!builder.BuildPing(
|
|
too_small, sizeof(too_small), &size));
|
|
}
|
|
|
|
void TestServerHello()
|
|
{
|
|
ServerMessageDispatcher dispatcher;
|
|
constexpr char kValid[] =
|
|
R"({"type":"hello","session_id":"session-a",)"
|
|
R"("transport":"websocket","audio_params":{)"
|
|
R"("sample_rate":24000,"channels":1,)"
|
|
R"("format":"opus","frame_duration":60},)"
|
|
R"("server_time":1744123456,"tz_offset":28800,)"
|
|
R"("button_map":{"3":"network_switch","4":"nothing"},)"
|
|
R"("connection_error_prompts":{)"
|
|
R"("server_unreachable":"error","no_network":"no_network"},)"
|
|
R"("device_name":"黑马"})";
|
|
ServerCommand command = ParseCommand(
|
|
dispatcher, kValid, sizeof(kValid) - 1);
|
|
assert(command.type == ServerCommandType::kHello);
|
|
assert(std::strcmp(command.session_id.data(), "session-a") == 0);
|
|
assert(command.output_sample_rate == 24000);
|
|
assert(command.frame_duration_ms == 60);
|
|
assert(command.server_time_seconds == 1744123456);
|
|
assert(command.timezone_offset_seconds == 28800);
|
|
assert(std::strcmp(command.device_name.data(), "黑马") == 0);
|
|
assert(command.triple_click_action ==
|
|
ButtonAction::kNetworkSwitch);
|
|
assert(command.quadruple_click_action ==
|
|
ButtonAction::kNothing);
|
|
assert(command.has_server_unreachable_prompt);
|
|
assert(command.server_unreachable_prompt == LocalPrompt::kError);
|
|
assert(command.has_no_network_prompt);
|
|
assert(command.no_network_prompt == LocalPrompt::kNoNetwork);
|
|
|
|
ServerCommand in_place;
|
|
assert(dispatcher.ParseInto(
|
|
kValid, sizeof(kValid) - 1, &in_place));
|
|
assert(in_place.type == ServerCommandType::kHello);
|
|
assert(std::strcmp(
|
|
in_place.session_id.data(), "session-a") == 0);
|
|
assert(!dispatcher.ParseInto(
|
|
kValid, sizeof(kValid) - 1, nullptr));
|
|
|
|
constexpr char kWrongRate[] =
|
|
R"({"type":"hello","session_id":"x",)"
|
|
R"("audio_params":{"sample_rate":16000,)"
|
|
R"("frame_duration":60}})";
|
|
command = ParseCommand(
|
|
dispatcher, kWrongRate, sizeof(kWrongRate) - 1);
|
|
assert(command.type == ServerCommandType::kInvalid);
|
|
|
|
constexpr char kWrongVersion[] =
|
|
R"({"type":"hello","version":2,"session_id":"x",)"
|
|
R"("audio_params":{"sample_rate":24000,)"
|
|
R"("frame_duration":60}})";
|
|
command = ParseCommand(
|
|
dispatcher, kWrongVersion, sizeof(kWrongVersion) - 1);
|
|
assert(command.type == ServerCommandType::kInvalid);
|
|
}
|
|
|
|
void TestServerCommands()
|
|
{
|
|
ServerMessageDispatcher dispatcher;
|
|
auto parse = [&dispatcher](const char* json) {
|
|
return ParseCommand(dispatcher, json, std::strlen(json));
|
|
};
|
|
|
|
assert(parse("{").type == ServerCommandType::kInvalid);
|
|
assert(parse("{}").type == ServerCommandType::kInvalid);
|
|
assert(parse(R"({"type":"ping"})").type ==
|
|
ServerCommandType::kPing);
|
|
assert(parse(R"({"type":"pong"})").type ==
|
|
ServerCommandType::kPong);
|
|
assert(parse(R"({"type":"tts","state":"start"})").type ==
|
|
ServerCommandType::kTtsStart);
|
|
assert(parse(R"({"type":"tts","state":"stop"})").type ==
|
|
ServerCommandType::kTtsStop);
|
|
ServerCommand command = parse(
|
|
R"({"type":"stt","text":"今天天气怎么样"})");
|
|
assert(command.type == ServerCommandType::kSttText);
|
|
assert(std::strcmp(
|
|
command.conversation_text.data(),
|
|
"今天天气怎么样") == 0);
|
|
ServerCommand reused;
|
|
assert(dispatcher.ParseInto(
|
|
R"({"type":"stt","text":"复用缓冲区"})",
|
|
std::strlen(R"({"type":"stt","text":"复用缓冲区"})"),
|
|
&reused));
|
|
assert(reused.type == ServerCommandType::kSttText);
|
|
assert(dispatcher.ParseInto(
|
|
R"({"type":"ping"})",
|
|
std::strlen(R"({"type":"ping"})"),
|
|
&reused));
|
|
assert(reused.type == ServerCommandType::kPing);
|
|
assert(reused.conversation_text[0] == '\0');
|
|
command = parse(
|
|
R"({"type":"tts","state":"sentence_start",)"
|
|
R"("text":"今天是晴天"})");
|
|
assert(command.type == ServerCommandType::kTtsSentenceStart);
|
|
assert(std::strcmp(
|
|
command.conversation_text.data(),
|
|
"今天是晴天") == 0);
|
|
assert(parse(R"({"type":"stt","text":""})").type ==
|
|
ServerCommandType::kInvalid);
|
|
|
|
// llm/emotion is advisory. It must be recognised (not counted as an
|
|
// unsupported message) and must survive a missing/blank emotion field.
|
|
command = parse(R"({"type":"llm","emotion":"happy"})");
|
|
assert(command.type == ServerCommandType::kLlmEmotion);
|
|
assert(std::strcmp(
|
|
command.conversation_text.data(), "happy") == 0);
|
|
command = parse(R"({"type":"llm"})");
|
|
assert(command.type == ServerCommandType::kLlmEmotion);
|
|
assert(command.conversation_text[0] == '\0');
|
|
|
|
assert(parse(
|
|
R"({"type":"tts","state":"sentence_start"})")
|
|
.type == ServerCommandType::kInvalid);
|
|
assert(parse(R"({"type":"future"})").type ==
|
|
ServerCommandType::kUnsupported);
|
|
assert(parse(R"({"type":"wifi_config"})").type ==
|
|
ServerCommandType::kEnterProvisioning);
|
|
|
|
command =
|
|
parse(R"({"type":"control","action":"volume","volume":135})");
|
|
assert(command.type == ServerCommandType::kControl);
|
|
assert(command.control_action == RemoteControlAction::kVolume);
|
|
assert(command.control_value == 100);
|
|
command = parse(
|
|
R"({"type":"control","action":"wake_word","enabled":false})");
|
|
assert(command.type == ServerCommandType::kControl);
|
|
assert(command.control_action == RemoteControlAction::kWakeWord);
|
|
assert(command.control_value == 0);
|
|
command = parse(
|
|
R"({"type":"control","action":"barge_in","enabled":true})");
|
|
assert(command.type == ServerCommandType::kControl);
|
|
assert(command.control_action == RemoteControlAction::kBargeIn);
|
|
assert(command.control_value == 1);
|
|
command = parse(
|
|
R"({"type":"control","action":"light_sleep",)"
|
|
R"("mode":"idle_sleep"})");
|
|
assert(command.type == ServerCommandType::kControl);
|
|
assert(command.control_action == RemoteControlAction::kLightSleep);
|
|
command = parse(
|
|
R"({"type":"control","action":"led","state":"alert",)"
|
|
R"("color":"red","duration_ms":70000})");
|
|
assert(command.type == ServerCommandType::kControl);
|
|
assert(command.control_action == RemoteControlAction::kLedAlert);
|
|
assert(command.control_value == 60000);
|
|
assert(parse(
|
|
R"({"type":"control","action":"future"})")
|
|
.type == ServerCommandType::kUnsupported);
|
|
assert(parse(
|
|
R"({"type":"control","action":"barge_in","enabled":1})")
|
|
.type == ServerCommandType::kInvalid);
|
|
|
|
command = parse(R"({"type":"barge_in","state":"candidate"})");
|
|
assert(command.type == ServerCommandType::kBargeIn);
|
|
assert(command.barge_in_state == BargeInDecisionState::kCandidate);
|
|
command = parse(R"({"type":"barge_in","state":"rejected"})");
|
|
assert(command.barge_in_state == BargeInDecisionState::kRejected);
|
|
command = parse(R"({"type":"barge_in","state":"confirmed"})");
|
|
assert(command.barge_in_state == BargeInDecisionState::kConfirmed);
|
|
assert(parse(R"({"type":"barge_in","state":"unknown"})").type ==
|
|
ServerCommandType::kInvalid);
|
|
|
|
command =
|
|
parse(R"({"type":"play_local","group":"alarm_wake",)"
|
|
R"("index":2})");
|
|
assert(command.type == ServerCommandType::kPlayLocal);
|
|
assert(command.local_prompt == LocalPrompt::kAlarmWake);
|
|
assert(command.playback_priority == PlaybackPriority::kAlarm);
|
|
assert(command.prompt_index == 2);
|
|
assert(!command.soft_off_after_playback);
|
|
|
|
command = parse(
|
|
R"({"type":"play_local","group":"snore_light"})");
|
|
assert(command.type == ServerCommandType::kPlayLocal);
|
|
assert(command.local_prompt == LocalPrompt::kSnoreLight);
|
|
|
|
command = parse(
|
|
R"({"type":"play_local","group":"shutdown"})");
|
|
assert(command.type == ServerCommandType::kPlayLocal);
|
|
assert(command.soft_off_after_playback);
|
|
|
|
command = parse(
|
|
R"({"type":"play_local","group":"shutdown",)"
|
|
R"("block_reconnect":false})");
|
|
assert(command.type == ServerCommandType::kPlayLocal);
|
|
assert(!command.soft_off_after_playback);
|
|
|
|
command = parse(
|
|
R"({"type":"play_local","group":"shutdown",)"
|
|
R"("post_action":"enter_soft_off"})");
|
|
assert(command.soft_off_after_playback);
|
|
|
|
command = parse(
|
|
R"({"type":"play_local","group":"not-installed"})");
|
|
assert(command.type == ServerCommandType::kUnsupported);
|
|
|
|
command = parse(
|
|
R"({"type":"play_local_sequence","sequence":[)"
|
|
R"({"group":"activation_prompt"},)"
|
|
R"({"group":"digit_4","index":0},)"
|
|
R"({"group":"unknown"}]})");
|
|
assert(command.type == ServerCommandType::kPlayLocalSequence);
|
|
assert(command.local_sequence.count == 2);
|
|
assert(command.local_sequence.items[0].prompt ==
|
|
LocalPrompt::kActivationDevice);
|
|
assert(command.local_sequence.items[1].prompt ==
|
|
LocalPrompt::kDigit4);
|
|
assert(command.local_sequence.items[1].selector == 0);
|
|
assert(parse(
|
|
R"({"type":"play_local_sequence","sequence":[]})")
|
|
.type == ServerCommandType::kUnsupported);
|
|
|
|
command = parse(
|
|
R"({"type":"memory_sync","child_name":"小明",)"
|
|
R"("brain_text":"喜欢星星","interaction_count":43})");
|
|
assert(command.type == ServerCommandType::kMemorySync);
|
|
assert(std::strcmp(
|
|
command.memory.child_name.data(), "小明") == 0);
|
|
assert(command.memory.interaction_count == 43);
|
|
assert(parse(
|
|
R"({"type":"memory_sync","interaction_count":-1})")
|
|
.type == ServerCommandType::kInvalid);
|
|
|
|
command = parse(R"({"type":"error","code":4512})");
|
|
assert(command.type == ServerCommandType::kError);
|
|
assert(command.error_id == 4512);
|
|
|
|
command = parse(
|
|
R"({"type":"set_timer","duration":300})");
|
|
assert(command.type == ServerCommandType::kSetTimer);
|
|
assert(command.duration_seconds == 300);
|
|
assert(parse(
|
|
R"({"type":"set_timer","duration":0})")
|
|
.type == ServerCommandType::kInvalid);
|
|
|
|
command = parse(
|
|
R"({"type":"cancel_timer","scope":"all"})");
|
|
assert(command.type == ServerCommandType::kCancelTimer);
|
|
assert(command.timer_cancel_scope == TimerCancelScope::kAll);
|
|
|
|
command = parse(
|
|
R"({"type":"set_alarm","alarm":{"id":42,)"
|
|
R"("hour":7,"minute":30,"repeat_mask":62,)"
|
|
R"("fire_epoch":0,"label":"wake","enabled":true}})");
|
|
assert(command.type == ServerCommandType::kSetAlarm);
|
|
assert(command.alarm.id == 42);
|
|
assert(command.alarm.repeat_mask == 62);
|
|
|
|
command = parse(
|
|
R"({"type":"sync_alarms","alarms":[)"
|
|
R"({"id":9,"hour":20,"minute":30,)"
|
|
R"("repeat_mask":127,"enabled":true}]})");
|
|
assert(command.type == ServerCommandType::kSyncAlarms);
|
|
assert(command.alarms.count == 1);
|
|
|
|
command = parse(
|
|
R"({"type":"delete_alarm","id":-203000})");
|
|
assert(command.type == ServerCommandType::kDeleteAlarm);
|
|
assert(command.alarm_id == kBuiltInFirstDayAlarmId);
|
|
assert(parse(
|
|
R"({"type":"delete_alarm","id":-1})")
|
|
.type == ServerCommandType::kInvalid);
|
|
assert(parse(
|
|
R"({"type":"delete_alarm","id":0})")
|
|
.type == ServerCommandType::kInvalid);
|
|
|
|
command = parse(
|
|
R"({"type":"alarm_config","set_tone_index":2,)"
|
|
R"("wake_tone_index":1})");
|
|
assert(command.type == ServerCommandType::kAlarmConfig);
|
|
assert(command.wake_tone_index == 1);
|
|
|
|
command = parse(
|
|
R"({"type":"call","state":"invite",)"
|
|
R"("call_id":"call-42","caller_name":"白马"})");
|
|
assert(command.type == ServerCommandType::kCall);
|
|
assert(command.call_state == CallSignalState::kInvite);
|
|
assert(std::strcmp(command.call_id.data(), "call-42") == 0);
|
|
assert(std::strcmp(command.peer_name.data(), "白马") == 0);
|
|
|
|
command = parse(
|
|
R"({"type":"call","state":"connected",)"
|
|
R"("call_id":"call-42","peer_name":"白马"})");
|
|
assert(command.type == ServerCommandType::kCall);
|
|
assert(command.call_state == CallSignalState::kConnected);
|
|
|
|
assert(parse(
|
|
R"({"type":"call","state":"invite"})")
|
|
.type == ServerCommandType::kInvalid);
|
|
assert(parse(
|
|
R"({"type":"call","state":"unknown",)"
|
|
R"("call_id":"call-42"})")
|
|
.type == ServerCommandType::kInvalid);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
TestMessageBuilder();
|
|
TestServerHello();
|
|
TestServerCommands();
|
|
std::cout << "realtime codec tests passed\n";
|
|
return 0;
|
|
}
|