Files
bolar_2/host_tests/effect_router_test.cc
2026-11-27 16:38:11 +08:00

382 lines
12 KiB
C++

#include "adapters/effect_router.h"
#include <cassert>
#include <iostream>
namespace {
using namespace borla;
class FakeAudio final : public IAudioEngine {
public:
bool Start() override
{
started = true;
return true;
}
void Stop() override { started = false; }
void ApplyPolicy(AudioPolicy value) override { policy = value; }
void SetVolume(uint8_t value) override { volume = value; }
void SetWakeWordEnabled(bool value) override
{
wake_word_enabled = value;
}
void SetBargeInEnabled(bool value) override
{
barge_in_enabled = value;
}
void SetPlaybackPaused(bool value) override
{
playback_paused = value;
}
void ReleaseWakeWord() override
{
wake_word_released = true;
}
void LogAudioMetrics(const char*) override {}
bool LogAudioFrameStats() override { return false; }
bool WaitForPlaybackIdle(uint32_t) override { return true; }
void CancelPlayback(PlaybackSessionId value) override
{
cancelled_session = value;
}
void BeginRemotePlayback(
PlaybackSessionId value,
PlaybackPriority value_priority) override
{
remote_session = value;
priority = value_priority;
}
void EndPlaybackInput(PlaybackSessionId value) override
{
ended_session = value;
}
bool SubmitOpusFrame(
PlaybackSessionId,
const uint8_t*,
size_t) override
{
return true;
}
PlaybackSessionId PlayLocalPrompt(
LocalPrompt value,
PlaybackPriority value_priority,
int16_t value_selector) override
{
prompt = value;
priority = value_priority;
selector = value_selector;
return 101;
}
PlaybackSessionId PlayLocalSequence(
const LocalPromptSequence&,
PlaybackPriority value_priority) override
{
priority = value_priority;
return 102;
}
PlaybackSessionId PlayDetachedPrompt(
LocalPrompt value,
PlaybackPriority value_priority) override
{
detached_prompt = value;
detached_priority = value_priority;
detached_count++;
return 103;
}
uint8_t OutputVolumePercent() const override { return volume; }
bool started = false;
LocalPrompt detached_prompt = LocalPrompt::kError;
PlaybackPriority detached_priority = PlaybackPriority::kBackground;
int detached_count = 0;
AudioPolicy policy = AudioPolicy::kOff;
uint8_t volume = 0;
bool wake_word_enabled = true;
bool barge_in_enabled = false;
bool playback_paused = false;
bool wake_word_released = false;
PlaybackSessionId cancelled_session = 0;
PlaybackSessionId remote_session = 0;
PlaybackSessionId ended_session = 0;
PlaybackPriority priority = PlaybackPriority::kBackground;
LocalPrompt prompt = LocalPrompt::kError;
int16_t selector = -1;
};
class FakeConnectivity final : public IConnectivityManager {
public:
void Start() override { started = true; }
void Stop() override { started = false; }
void SwitchNetwork() override { switched = true; }
bool IsOnline() const override { return online; }
ConnectivityDiagnostics GetDiagnostics() const override { return {}; }
bool started = false;
bool switched = false;
bool online = false;
};
class FakeRealtime final : public IRealtimeProtocol {
public:
void Open() override { opened = true; }
void Close() override { opened = false; }
void SendStartListening() override { start_listening_count++; }
void SendStopListening() override { stop_listening_count++; }
void SendAbortSpeaking(
ConversationInterruptSource source) override
{
abort_source = source;
abort_speaking_count++;
}
void SendGoodbye() override { goodbye_count++; }
void SendAcceptCall() override { accept_call_count++; }
void SendHangupCall() override { hangup_call_count++; }
void SendButtonClick(uint8_t click_count) override
{
button_click_count = click_count;
}
void SendLocalPlaybackState(
LocalPrompt prompt,
bool started) override
{
local_prompt = prompt;
local_started = started;
local_state_count++;
}
bool SendFriendAdd(
const char*,
const char*,
const char*,
const char*) override
{
return true;
}
bool SendOtaNotify(const char*, bool) override
{
ota_notify_count++;
return true;
}
void RequestConnectionErrorPrompt() override
{
connection_error_prompt_count++;
}
bool opened = false;
int ota_notify_count = 0;
int connection_error_prompt_count = 0;
int start_listening_count = 0;
int stop_listening_count = 0;
int abort_speaking_count = 0;
ConversationInterruptSource abort_source =
ConversationInterruptSource::kWakeWordDetected;
int goodbye_count = 0;
int accept_call_count = 0;
int hangup_call_count = 0;
uint8_t button_click_count = 0;
LocalPrompt local_prompt = LocalPrompt::kError;
bool local_started = false;
int local_state_count = 0;
};
class FakePlatform final : public IDevicePlatform {
public:
bool Initialize() override { return true; }
const DeviceIdentity& Identity() const override { return identity; }
void SetLed(LedPattern value) override { led = value; }
void ShowTemporaryLedAlert(uint32_t duration_ms) override
{
alert_duration_ms = duration_ms;
}
void ReportError(uint32_t value) override { error_id = value; }
void Reboot() override { rebooted = true; }
void EnterSoftOff(uint64_t) override { soft_off = true; }
SoftOffWakeReason ConsumeSoftOffWakeReason() override
{
return SoftOffWakeReason::kNone;
}
LedPattern led = LedPattern::kOff;
uint32_t error_id = 0;
uint32_t alert_duration_ms = 0;
bool rebooted = false;
bool soft_off = false;
DeviceIdentity identity{};
};
class FakeProvisioning final : public IProvisioningService {
public:
void Start() override { started = true; }
void Stop() override { started = false; }
bool started = false;
};
class FakeFriend final : public IFriendService {
public:
void Start() override { started = true; }
void Stop() override { started = false; }
bool started = false;
};
class FakeOta final : public IOtaService {
public:
void StartUpgrade(uint32_t value) override { request_id = value; }
uint32_t request_id = 0;
};
class FakeAlert final : public IAlertRinger {
public:
void Start(AlertKind value) override
{
started = true;
kind = value;
}
void Stop() override { started = false; }
bool started = false;
AlertKind kind = AlertKind::kReminder;
};
class FakePower final : public IPowerService {
public:
void EnterSoftOff() override { soft_off = true; }
bool soft_off = false;
};
void TestRoutesEffectsWithoutProductLogic()
{
FakeAudio audio;
FakeConnectivity connectivity;
FakeRealtime realtime;
FakePlatform platform;
FakeFriend friend_service;
FakeProvisioning provisioning;
FakeOta ota;
FakeAlert alert;
FakePower power;
EffectRouter router(
audio, connectivity, realtime, platform, friend_service,
provisioning, ota, alert, power);
RuntimeState state;
router.Handle(
AppEffect::Simple(AppEffectType::kStartConnectivity), state);
router.Handle(AppEffect::Simple(AppEffectType::kStartAudio), state);
router.Handle(AppEffect::Audio(AudioPolicy::kBargeInDuplex), state);
router.Handle(
AppEffect::Simple(AppEffectType::kSetAudioVolume, 72), state);
router.Handle(
AppEffect::Simple(
AppEffectType::kSetWakeWordEnabled, 0),
state);
router.Handle(
AppEffect::Simple(
AppEffectType::kSetBargeInEnabled, 1),
state);
router.Handle(
AppEffect::Simple(AppEffectType::kPausePlayback), state);
assert(audio.playback_paused);
// Provisioning must free the WakeNet instance before NimBLE starts: the BT
// controller needs the internal RAM and the wake word is unused there.
assert(!audio.wake_word_released);
router.Handle(
AppEffect::Simple(AppEffectType::kEnterProvisioning), state);
assert(audio.wake_word_released);
assert(provisioning.started);
router.Handle(
AppEffect::Simple(AppEffectType::kResumePlayback), state);
assert(!audio.playback_paused);
router.Handle(AppEffect::Led(LedPattern::kSpeaking), state);
router.Handle(
AppEffect::Simple(
AppEffectType::kShowTemporaryLedAlert, 5000),
state);
router.Handle(
AppEffect::Simple(AppEffectType::kStartUpgrade, 99), state);
router.Handle(
AppEffect::Simple(AppEffectType::kStartFriendMode), state);
router.Handle(
AppEffect::Simple(AppEffectType::kSendAbortSpeaking), state);
router.Handle(
AppEffect::Simple(AppEffectType::kSendGoodbye), state);
router.Handle(
AppEffect::Simple(AppEffectType::kSendAcceptCall), state);
router.Handle(
AppEffect::Simple(AppEffectType::kSendHangupCall), state);
router.Handle(
AppEffect::Simple(AppEffectType::kSendButtonClick, 4), state);
router.Handle(
AppEffect::LocalPlaybackState(
AppEffectType::kSendLocalPlaybackStarted,
LocalPrompt::kSnoreLight),
state);
router.Handle(
AppEffect::LocalPlaybackState(
AppEffectType::kSendLocalPlaybackDone,
LocalPrompt::kSnoreLight),
state);
router.Handle(
AppEffect::RemotePlayback(
AppEffectType::kBeginRemotePlayback,
77,
PlaybackPriority::kConversation),
state);
router.Handle(
AppEffect::RemotePlayback(
AppEffectType::kEndPlaybackInput, 77),
state);
router.Handle(
AppEffect::Prompt(
LocalPrompt::kBoot, PlaybackPriority::kSystem, 4),
state);
router.Handle(
AppEffect::DetachedPrompt(LocalPrompt::kWakeResponse),
state);
router.Handle(
AppEffect::Simple(
AppEffectType::kRequestConnectionErrorPrompt),
state);
assert(connectivity.started);
assert(audio.started);
assert(audio.policy == AudioPolicy::kBargeInDuplex);
assert(audio.volume == 72);
assert(!audio.wake_word_enabled);
assert(audio.barge_in_enabled);
assert(platform.led == LedPattern::kSpeaking);
assert(platform.alert_duration_ms == 5000);
assert(ota.request_id == 99);
assert(friend_service.started);
assert(realtime.abort_speaking_count == 1);
assert(realtime.abort_source ==
ConversationInterruptSource::kUserInitiated);
assert(realtime.goodbye_count == 1);
assert(realtime.accept_call_count == 1);
assert(realtime.hangup_call_count == 1);
assert(realtime.button_click_count == 4);
assert(realtime.local_state_count == 2);
assert(realtime.local_prompt == LocalPrompt::kSnoreLight);
assert(!realtime.local_started);
assert(audio.remote_session == 77);
assert(audio.ended_session == 77);
assert(audio.selector == 4);
assert(audio.prompt == LocalPrompt::kBoot);
assert(audio.priority == PlaybackPriority::kSystem);
// A detached prompt must reach the dedicated engine entry point, not the
// lifecycle-publishing one, and must not disturb the normal prompt slot.
assert(audio.detached_count == 1);
assert(audio.detached_prompt == LocalPrompt::kWakeResponse);
assert(audio.detached_priority == PlaybackPriority::kLocalPrompt);
assert(audio.prompt == LocalPrompt::kBoot);
assert(realtime.connection_error_prompt_count == 1);
}
} // namespace
int main()
{
TestRoutesEffectsWithoutProductLogic();
std::cout << "effect router tests passed\n";
return 0;
}