1018 lines
39 KiB
C++
1018 lines
39 KiB
C++
#include "core/state_reducer.h"
|
|
#include "core/remote_behavior.h"
|
|
|
|
#include <cassert>
|
|
#include <iostream>
|
|
|
|
namespace {
|
|
|
|
using namespace borla;
|
|
|
|
RuntimeState BootToRunning()
|
|
{
|
|
RuntimeState state;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kBootRequested)).next;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kNetworkConnected)).next;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kRealtimeConnected)).next;
|
|
return state;
|
|
}
|
|
|
|
bool HasEffect(const TransitionResult& transition, AppEffectType type)
|
|
{
|
|
for (size_t i = 0; i < transition.effect_count; ++i) {
|
|
if (transition.effects[i].type == type) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
size_t EffectIndex(
|
|
const TransitionResult& transition,
|
|
AppEffectType type)
|
|
{
|
|
for (size_t i = 0; i < transition.effect_count; ++i) {
|
|
if (transition.effects[i].type == type) {
|
|
return i;
|
|
}
|
|
}
|
|
return transition.effect_count;
|
|
}
|
|
|
|
const AppEffect* FindEffect(
|
|
const TransitionResult& transition,
|
|
AppEffectType type)
|
|
{
|
|
for (size_t i = 0; i < transition.effect_count; ++i) {
|
|
if (transition.effects[i].type == type) {
|
|
return &transition.effects[i];
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void TestBootSequence()
|
|
{
|
|
const TransitionResult boot = ReduceState(
|
|
RuntimeState{}, AppEvent::Make(AppEventType::kBootRequested));
|
|
assert(HasEffect(boot, AppEffectType::kStartAudio));
|
|
const AppEffect* prompt =
|
|
FindEffect(boot, AppEffectType::kPlayLocalPrompt);
|
|
assert(prompt != nullptr);
|
|
assert(prompt->local_prompt == LocalPrompt::kBoot);
|
|
assert(prompt->playback_priority == PlaybackPriority::kSystem);
|
|
|
|
const RuntimeState state = BootToRunning();
|
|
assert(state.lifecycle == LifecycleState::kRunning);
|
|
assert(state.connectivity == ConnectivityState::kOnline);
|
|
assert(state.realtime == RealtimeState::kOpen);
|
|
assert(state.conversation == ConversationState::kIdle);
|
|
assert(ValidateRuntimeState(state).valid);
|
|
}
|
|
|
|
void TestConversationPlaybackDrain()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected)).next;
|
|
assert(state.conversation == ConversationState::kListening);
|
|
|
|
constexpr uint32_t kSessionId = 42;
|
|
TransitionResult transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kTtsStarted, kSessionId));
|
|
const AppEffect* begin =
|
|
FindEffect(transition, AppEffectType::kBeginRemotePlayback);
|
|
assert(begin != nullptr);
|
|
assert(begin->correlation_id == kSessionId);
|
|
assert(begin->playback_priority == PlaybackPriority::kConversation);
|
|
state = transition.next;
|
|
assert(state.conversation == ConversationState::kSpeaking);
|
|
assert(state.playback == PlaybackState::kStreaming);
|
|
|
|
transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kTtsStreamEnded, kSessionId));
|
|
const AppEffect* end =
|
|
FindEffect(transition, AppEffectType::kEndPlaybackInput);
|
|
assert(end != nullptr);
|
|
assert(end->correlation_id == kSessionId);
|
|
state = transition.next;
|
|
assert(state.playback == PlaybackState::kDraining);
|
|
assert(state.conversation == ConversationState::kSpeaking);
|
|
|
|
const TransitionResult stale = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kPlaybackDrained, 41));
|
|
assert(stale.next.playback == PlaybackState::kDraining);
|
|
assert(stale.next.playback_session_id == kSessionId);
|
|
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kPlaybackDrained, kSessionId)).next;
|
|
assert(state.playback == PlaybackState::kIdle);
|
|
assert(state.conversation == ConversationState::kListening);
|
|
assert(ValidateRuntimeState(state).valid);
|
|
}
|
|
|
|
void TestLocalPlaybackPreemptsConversationState()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected)).next;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kTtsStarted, 88)).next;
|
|
|
|
const TransitionResult transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kPlaybackPreempted,
|
|
88,
|
|
0,
|
|
0x80000000U));
|
|
assert(transition.next.playback == PlaybackState::kIdle);
|
|
assert(transition.next.playback_session_id == 0);
|
|
assert(transition.next.conversation == ConversationState::kIdle);
|
|
assert(HasEffect(transition, AppEffectType::kSendAbortSpeaking));
|
|
assert(HasEffect(transition, AppEffectType::kApplyAudioPolicy));
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
}
|
|
|
|
void TestDisconnectRecovery()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected)).next;
|
|
const TransitionResult disconnected = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kNetworkDisconnected));
|
|
state = disconnected.next;
|
|
|
|
assert(state.connectivity == ConnectivityState::kConnecting);
|
|
assert(state.realtime == RealtimeState::kClosed);
|
|
assert(state.conversation == ConversationState::kIdle);
|
|
assert(state.playback == PlaybackState::kIdle);
|
|
assert(HasEffect(disconnected, AppEffectType::kCloseRealtime));
|
|
assert(HasEffect(disconnected, AppEffectType::kStartConnectivity));
|
|
assert(ValidateRuntimeState(state).valid);
|
|
}
|
|
|
|
void TestExclusiveUpgradeState()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kUpgradeRequested, 7)).next;
|
|
|
|
assert(state.lifecycle == LifecycleState::kUpgrading);
|
|
assert(state.connectivity == ConnectivityState::kOnline);
|
|
assert(state.realtime == RealtimeState::kClosed);
|
|
assert(state.conversation == ConversationState::kIdle);
|
|
assert(!HasEffect(
|
|
ReduceState(
|
|
BootToRunning(),
|
|
AppEvent::Make(AppEventType::kUpgradeRequested, 7)),
|
|
AppEffectType::kStopConnectivity));
|
|
assert(ValidateRuntimeState(state).valid);
|
|
}
|
|
|
|
void TestSingleClickControlsConversation()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
|
|
TransitionResult transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kButtonClicked, 0, 1));
|
|
assert(transition.next.conversation == ConversationState::kListening);
|
|
assert(HasEffect(transition, AppEffectType::kSendStartListening));
|
|
assert(
|
|
EffectIndex(transition, AppEffectType::kSendStartListening) <
|
|
EffectIndex(transition, AppEffectType::kApplyAudioPolicy));
|
|
|
|
state = transition.next;
|
|
transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kButtonClicked, 0, 1));
|
|
assert(transition.next.conversation == ConversationState::kIdle);
|
|
assert(HasEffect(transition, AppEffectType::kSendStopListening));
|
|
assert(HasEffect(transition, AppEffectType::kCancelPlayback));
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
}
|
|
|
|
void TestSingleClickInterruptsSpeaking()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected)).next;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kTtsStarted, 88)).next;
|
|
|
|
const TransitionResult transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kButtonClicked, 0, 1));
|
|
assert(transition.next.conversation == ConversationState::kListening);
|
|
assert(transition.next.playback == PlaybackState::kIdle);
|
|
assert(transition.next.playback_session_id == 0);
|
|
assert(HasEffect(transition, AppEffectType::kCancelPlayback));
|
|
assert(HasEffect(transition, AppEffectType::kSendAbortSpeaking));
|
|
assert(HasEffect(transition, AppEffectType::kSendStartListening));
|
|
const AppEffect* abort = FindEffect(
|
|
transition, AppEffectType::kSendAbortSpeaking);
|
|
assert(abort != nullptr);
|
|
assert(abort->interrupt_source ==
|
|
ConversationInterruptSource::kUserInitiated);
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
}
|
|
|
|
void TestServerArbitratesNaturalBargeIn()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected)).next;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kTtsStarted, 100)).next;
|
|
|
|
const TransitionResult candidate = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kBargeInCandidate));
|
|
assert(candidate.next.conversation == ConversationState::kSpeaking);
|
|
assert(candidate.next.playback == PlaybackState::kStreaming);
|
|
assert(HasEffect(candidate, AppEffectType::kPausePlayback));
|
|
assert(!HasEffect(candidate, AppEffectType::kCancelPlayback));
|
|
assert(!HasEffect(candidate, AppEffectType::kSendAbortSpeaking));
|
|
assert(!HasEffect(candidate, AppEffectType::kSendStartListening));
|
|
assert(ValidateRuntimeState(candidate.next).valid);
|
|
|
|
const TransitionResult rejected = ReduceState(
|
|
candidate.next,
|
|
AppEvent::Make(AppEventType::kBargeInRejected));
|
|
assert(rejected.next.conversation == ConversationState::kSpeaking);
|
|
assert(HasEffect(rejected, AppEffectType::kResumePlayback));
|
|
assert(!HasEffect(rejected, AppEffectType::kCancelPlayback));
|
|
|
|
const TransitionResult confirmed = ReduceState(
|
|
candidate.next,
|
|
AppEvent::Make(AppEventType::kBargeInConfirmed));
|
|
assert(confirmed.next.conversation == ConversationState::kIdle);
|
|
assert(confirmed.next.playback == PlaybackState::kIdle);
|
|
assert(HasEffect(confirmed, AppEffectType::kResumePlayback));
|
|
assert(HasEffect(confirmed, AppEffectType::kCancelPlayback));
|
|
assert(!HasEffect(confirmed, AppEffectType::kSendAbortSpeaking));
|
|
assert(!HasEffect(confirmed, AppEffectType::kSendStartListening));
|
|
assert(ValidateRuntimeState(confirmed.next).valid);
|
|
|
|
const RuntimeState draining = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kTtsStreamEnded, 100)).next;
|
|
assert(draining.playback == PlaybackState::kDraining);
|
|
const TransitionResult tail_candidate = ReduceState(
|
|
draining,
|
|
AppEvent::Make(AppEventType::kBargeInCandidate));
|
|
assert(HasEffect(tail_candidate, AppEffectType::kPausePlayback));
|
|
assert(!HasEffect(tail_candidate, AppEffectType::kCancelPlayback));
|
|
|
|
state = BootToRunning();
|
|
const TransitionResult ignored = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kBargeInCandidate));
|
|
assert(ignored.next.conversation == ConversationState::kIdle);
|
|
assert(!HasEffect(ignored, AppEffectType::kPausePlayback));
|
|
}
|
|
|
|
void TestWakeWordInterruptsSpeaking()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected)).next;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kTtsStarted, 99)).next;
|
|
|
|
const TransitionResult transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected));
|
|
assert(transition.next.conversation == ConversationState::kListening);
|
|
assert(transition.next.playback == PlaybackState::kIdle);
|
|
assert(HasEffect(transition, AppEffectType::kCancelPlayback));
|
|
assert(HasEffect(transition, AppEffectType::kSendAbortSpeaking));
|
|
assert(HasEffect(transition, AppEffectType::kSendStartListening));
|
|
const AppEffect* abort = FindEffect(
|
|
transition, AppEffectType::kSendAbortSpeaking);
|
|
assert(abort != nullptr);
|
|
assert(abort->interrupt_source ==
|
|
ConversationInterruptSource::kWakeWordDetected);
|
|
// The wake word always answers "我在" out loud — including when it cuts a
|
|
// playing reply. A button press through the same path stays silent.
|
|
assert(HasEffect(transition, AppEffectType::kPlayDetachedPrompt));
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
|
|
state = BootToRunning();
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected)).next;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kTtsStarted, 101)).next;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kTtsStreamEnded, 101)).next;
|
|
assert(state.playback == PlaybackState::kDraining);
|
|
|
|
const TransitionResult tail_transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected));
|
|
assert(tail_transition.next.conversation ==
|
|
ConversationState::kListening);
|
|
assert(tail_transition.next.playback == PlaybackState::kIdle);
|
|
assert(HasEffect(tail_transition, AppEffectType::kCancelPlayback));
|
|
assert(HasEffect(tail_transition, AppEffectType::kSendAbortSpeaking));
|
|
assert(HasEffect(tail_transition, AppEffectType::kSendStartListening));
|
|
assert(ValidateRuntimeState(tail_transition.next).valid);
|
|
}
|
|
|
|
// Entering provisioning must be audible, and finishing it must be audible.
|
|
// Both prompts existed in the previous firmware and were embedded in this one
|
|
// but never played, leaving a first-boot device completely silent.
|
|
void TestProvisioningIsAudible()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
TransitionResult entered = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kProvisioningRequested));
|
|
assert(entered.next.lifecycle == LifecycleState::kProvisioning);
|
|
assert(HasEffect(entered, AppEffectType::kPlayDetachedPrompt));
|
|
const size_t prompt_index =
|
|
EffectIndex(entered, AppEffectType::kPlayDetachedPrompt);
|
|
assert(entered.effects[prompt_index].local_prompt ==
|
|
LocalPrompt::kProvisioning);
|
|
assert(entered.effects[prompt_index].playback_priority ==
|
|
PlaybackPriority::kSystem);
|
|
// The prompt must survive the teardown that happens in the same
|
|
// transition, so it has to be ordered after the playback cancellation.
|
|
assert(prompt_index >
|
|
EffectIndex(entered, AppEffectType::kCancelPlayback));
|
|
// Detached on purpose: provisioning is an exclusive lifecycle and the
|
|
// invariant checker requires playback to stay idle inside it.
|
|
assert(entered.next.playback == PlaybackState::kIdle);
|
|
assert(ValidateRuntimeState(entered.next).valid);
|
|
|
|
TransitionResult saved = ReduceState(
|
|
entered.next,
|
|
AppEvent::Make(AppEventType::kProvisioningWifiSaved));
|
|
assert(HasEffect(saved, AppEffectType::kPlayDetachedPrompt));
|
|
assert(saved.effects[EffectIndex(saved, AppEffectType::kPlayDetachedPrompt)]
|
|
.local_prompt == LocalPrompt::kWifiSuccess);
|
|
// Credentials being saved does not itself leave provisioning; the
|
|
// miniprogram still drives the reboot, as the previous firmware did.
|
|
assert(saved.next.lifecycle == LifecycleState::kProvisioning);
|
|
assert(ValidateRuntimeState(saved.next).valid);
|
|
|
|
// Outside provisioning the event is inert rather than producing a stray
|
|
// "配网成功啦" in the middle of a conversation.
|
|
TransitionResult stray = ReduceState(
|
|
BootToRunning(),
|
|
AppEvent::Make(AppEventType::kProvisioningWifiSaved));
|
|
assert(!HasEffect(stray, AppEffectType::kPlayDetachedPrompt));
|
|
assert(stray.next.lifecycle == LifecycleState::kRunning);
|
|
}
|
|
|
|
void TestButtonLifecycleRequests()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
TransitionResult transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kButtonClicked, 0, 2));
|
|
assert(transition.next.lifecycle == LifecycleState::kProvisioning);
|
|
assert(HasEffect(transition, AppEffectType::kEnterProvisioning));
|
|
assert(HasEffect(transition, AppEffectType::kSendGoodbye));
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
|
|
// Long press plays the shutdown prompt first and only powers down once it
|
|
// has drained, matching the previous firmware. Going straight to
|
|
// kEnterSoftOff left the device silent on shutdown.
|
|
state = BootToRunning();
|
|
transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kButtonLongPressed));
|
|
assert(transition.next.lifecycle == LifecycleState::kShuttingDown);
|
|
assert(!HasEffect(transition, AppEffectType::kEnterSoftOff));
|
|
assert(HasEffect(transition, AppEffectType::kPlayLocalPrompt));
|
|
assert(transition.effects[
|
|
EffectIndex(transition, AppEffectType::kPlayLocalPrompt)]
|
|
.local_prompt == LocalPrompt::kShutdown);
|
|
assert(transition.next.pending_soft_off_after_playback);
|
|
assert(HasEffect(transition, AppEffectType::kSendGoodbye));
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
|
|
const TransitionResult drained = ReduceState(
|
|
transition.next,
|
|
AppEvent::Make(
|
|
AppEventType::kPlaybackDrained,
|
|
transition.next.playback_session_id));
|
|
assert(drained.next.lifecycle == LifecycleState::kSoftOff);
|
|
assert(HasEffect(drained, AppEffectType::kEnterSoftOff));
|
|
assert(ValidateRuntimeState(drained.next).valid);
|
|
}
|
|
|
|
void TestServerLocalPromptAndShutdownDrain()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
TransitionResult transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kLocalPromptRequested,
|
|
3,
|
|
static_cast<int32_t>(LocalPrompt::kWelcome),
|
|
static_cast<uint32_t>(PlaybackPriority::kLocalPrompt)));
|
|
const AppEffect* prompt =
|
|
FindEffect(transition, AppEffectType::kPlayLocalPrompt);
|
|
assert(prompt != nullptr);
|
|
assert(prompt->local_prompt == LocalPrompt::kWelcome);
|
|
assert(prompt->prompt_selector == 2);
|
|
state = transition.next;
|
|
assert(state.conversation == ConversationState::kIdle);
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kLocalPromptStarted,
|
|
301,
|
|
static_cast<int32_t>(LocalPrompt::kWelcome)));
|
|
state = transition.next;
|
|
assert(state.local_prompt_active);
|
|
assert(state.active_local_prompt == LocalPrompt::kWelcome);
|
|
assert(state.playback_session_id == 301);
|
|
assert(HasEffect(
|
|
transition, AppEffectType::kSendLocalPlaybackStarted));
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kPlaybackDrained, 301));
|
|
state = transition.next;
|
|
assert(!state.local_prompt_active);
|
|
assert(HasEffect(
|
|
transition, AppEffectType::kSendLocalPlaybackDone));
|
|
assert(state.conversation == ConversationState::kListening);
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
RuntimeState alert_state = state;
|
|
alert_state.alert_ringing = true;
|
|
transition = ReduceState(
|
|
alert_state,
|
|
AppEvent::Make(
|
|
AppEventType::kLocalPromptRequested,
|
|
0,
|
|
static_cast<int32_t>(LocalPrompt::kWelcome),
|
|
static_cast<uint32_t>(PlaybackPriority::kLocalPrompt)));
|
|
assert(!HasEffect(
|
|
transition, AppEffectType::kPlayLocalPrompt));
|
|
assert(transition.next.alert_ringing);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kShutdownPromptRequested));
|
|
state = transition.next;
|
|
assert(state.lifecycle == LifecycleState::kShuttingDown);
|
|
assert(state.playback == PlaybackState::kDraining);
|
|
assert(state.playback_session_id == 0);
|
|
assert(state.pending_soft_off_after_playback);
|
|
prompt = FindEffect(transition, AppEffectType::kPlayLocalPrompt);
|
|
assert(prompt != nullptr);
|
|
assert(prompt->local_prompt == LocalPrompt::kShutdown);
|
|
assert(HasEffect(transition, AppEffectType::kSendGoodbye));
|
|
assert(HasEffect(transition, AppEffectType::kCloseRealtime));
|
|
assert(HasEffect(transition, AppEffectType::kStopConnectivity));
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kPlaybackDrained, 0x80000001U));
|
|
assert(transition.next.lifecycle == LifecycleState::kSoftOff);
|
|
assert(transition.next.playback == PlaybackState::kIdle);
|
|
assert(!transition.next.pending_soft_off_after_playback);
|
|
assert(HasEffect(transition, AppEffectType::kEnterSoftOff));
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
}
|
|
|
|
void TestConfiguredMultiClickActions()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
TransitionResult transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kButtonClicked,
|
|
0,
|
|
3,
|
|
static_cast<uint32_t>(
|
|
ButtonAction::kNetworkSwitch)));
|
|
assert(HasEffect(
|
|
transition, AppEffectType::kSwitchNetwork));
|
|
assert(!transition.next.friend_mode);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kButtonClicked,
|
|
0,
|
|
4,
|
|
static_cast<uint32_t>(
|
|
ButtonAction::kFriendMode)));
|
|
assert(HasEffect(
|
|
transition, AppEffectType::kStartFriendMode));
|
|
assert(transition.next.friend_mode);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kButtonClicked,
|
|
0,
|
|
3,
|
|
static_cast<uint32_t>(
|
|
ButtonAction::kNothing)));
|
|
assert(HasEffect(
|
|
transition, AppEffectType::kSendButtonClick));
|
|
assert(!HasEffect(
|
|
transition, AppEffectType::kStartFriendMode));
|
|
assert(!HasEffect(
|
|
transition, AppEffectType::kSwitchNetwork));
|
|
assert(!transition.next.friend_mode);
|
|
}
|
|
|
|
void TestSingleClickControlsCalls()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kIncomingCall)).next;
|
|
|
|
TransitionResult transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kButtonClicked, 0, 1));
|
|
assert(transition.next.call == CallState::kRinging);
|
|
assert(HasEffect(transition, AppEffectType::kCancelPlayback));
|
|
assert(HasEffect(transition, AppEffectType::kSendAcceptCall));
|
|
|
|
constexpr uint32_t kCallSession = 0x80000012U;
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kCallConnected,
|
|
kCallSession)).next;
|
|
assert(state.playback == PlaybackState::kStreaming);
|
|
assert(state.playback_session_id == kCallSession);
|
|
transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kButtonClicked, 0, 1));
|
|
assert(transition.next.call == CallState::kActive);
|
|
assert(HasEffect(transition, AppEffectType::kSendHangupCall));
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
|
|
state = BootToRunning();
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kOutgoingCall,
|
|
kCallSession));
|
|
assert(transition.next.call == CallState::kCalling);
|
|
const AppEffect* wait_prompt =
|
|
FindEffect(transition, AppEffectType::kPlayLocalPrompt);
|
|
assert(wait_prompt != nullptr);
|
|
assert(wait_prompt->local_prompt == LocalPrompt::kCallWait);
|
|
state = transition.next;
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kCallConnected,
|
|
kCallSession));
|
|
assert(transition.next.call == CallState::kActive);
|
|
const AppEffect* playback =
|
|
FindEffect(transition, AppEffectType::kBeginRemotePlayback);
|
|
assert(playback != nullptr);
|
|
assert(playback->playback_priority == PlaybackPriority::kCall);
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
|
|
transition = ReduceState(
|
|
transition.next,
|
|
AppEvent::Make(
|
|
AppEventType::kCallEnded,
|
|
kCallSession));
|
|
assert(transition.next.call == CallState::kInactive);
|
|
assert(transition.next.playback == PlaybackState::kIdle);
|
|
assert(HasEffect(transition, AppEffectType::kCancelPlayback));
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
}
|
|
|
|
void TestIdleSleepAndAlertFlow()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
TransitionResult transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kIdleCheckDue));
|
|
state = transition.next;
|
|
assert(state.idle_check_prompt);
|
|
assert(state.playback == PlaybackState::kDraining);
|
|
assert(FindEffect(
|
|
transition, AppEffectType::kPlayLocalPrompt)
|
|
->local_prompt == LocalPrompt::kIdleCheck);
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kLocalPromptStarted,
|
|
501,
|
|
static_cast<int32_t>(LocalPrompt::kIdleCheck)))
|
|
.next;
|
|
assert(state.playback_session_id == 501);
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kPlaybackDrained, 501))
|
|
.next;
|
|
assert(!state.idle_check_prompt);
|
|
assert(state.conversation == ConversationState::kListening);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kIdleResponseExpired));
|
|
state = transition.next;
|
|
assert(state.idle_sleeping);
|
|
assert(state.realtime == RealtimeState::kClosed);
|
|
assert(HasEffect(transition, AppEffectType::kCloseRealtime));
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kAlarmDue));
|
|
state = transition.next;
|
|
assert(state.alert_ringing);
|
|
assert(!state.idle_sleeping);
|
|
assert(state.realtime == RealtimeState::kOpening);
|
|
assert(HasEffect(transition, AppEffectType::kStartAlert));
|
|
assert(HasEffect(transition, AppEffectType::kOpenRealtime));
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
transition = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kButtonClicked, 0, 1));
|
|
assert(!transition.next.alert_ringing);
|
|
assert(HasEffect(transition, AppEffectType::kStopAlert));
|
|
}
|
|
|
|
void TestAlertDeferredUntilStartupCompletes()
|
|
{
|
|
RuntimeState state;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kBootRequested)).next;
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kAlarmDue)).next;
|
|
assert(state.pending_alert);
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kNetworkConnected)).next;
|
|
const TransitionResult connected = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kRealtimeConnected));
|
|
assert(connected.next.lifecycle == LifecycleState::kRunning);
|
|
assert(connected.next.alert_ringing);
|
|
assert(!connected.next.pending_alert);
|
|
assert(HasEffect(connected, AppEffectType::kStartAlert));
|
|
assert(ValidateRuntimeState(connected.next).valid);
|
|
}
|
|
|
|
void TestFourClicksRequestNetworkSwitch()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
const TransitionResult transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kButtonClicked, 0, 4));
|
|
assert(HasEffect(transition, AppEffectType::kSwitchNetwork));
|
|
|
|
state.lifecycle = LifecycleState::kUpgrading;
|
|
const TransitionResult upgrading = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kButtonClicked, 0, 4));
|
|
assert(!HasEffect(upgrading, AppEffectType::kSwitchNetwork));
|
|
}
|
|
|
|
void TestTripleClickFriendMode()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
TransitionResult transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kButtonClicked, 0, 3));
|
|
state = transition.next;
|
|
assert(state.friend_mode);
|
|
assert(state.realtime == RealtimeState::kClosed);
|
|
assert(HasEffect(transition, AppEffectType::kCloseRealtime));
|
|
assert(HasEffect(transition, AppEffectType::kStartFriendMode));
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kButtonClicked, 0, 3));
|
|
state = transition.next;
|
|
assert(state.friend_mode);
|
|
assert(state.realtime == RealtimeState::kClosed);
|
|
assert(HasEffect(transition, AppEffectType::kStopFriendMode));
|
|
assert(!HasEffect(transition, AppEffectType::kOpenRealtime));
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kFriendModeEnded,
|
|
0,
|
|
0,
|
|
1));
|
|
state = transition.next;
|
|
assert(!state.friend_mode);
|
|
assert(state.realtime == RealtimeState::kOpening);
|
|
assert(HasEffect(transition, AppEffectType::kOpenRealtime));
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
state = BootToRunning();
|
|
state.call = CallState::kActive;
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kButtonClicked, 0, 3));
|
|
assert(!transition.next.friend_mode);
|
|
assert(!HasEffect(
|
|
transition, AppEffectType::kStartFriendMode));
|
|
}
|
|
|
|
void TestLocalSequenceLifecycle()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kWakeWordDetected)).next;
|
|
TransitionResult transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kLocalSequenceRequested, 7));
|
|
state = transition.next;
|
|
assert(state.conversation == ConversationState::kIdle);
|
|
assert(state.playback == PlaybackState::kIdle);
|
|
assert(HasEffect(
|
|
transition, AppEffectType::kSendStopListening));
|
|
assert(HasEffect(
|
|
transition, AppEffectType::kCancelPlayback));
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kLocalSequenceStarted, 701))
|
|
.next;
|
|
assert(state.conversation == ConversationState::kSpeaking);
|
|
assert(state.playback == PlaybackState::kDraining);
|
|
assert(state.playback_session_id == 701);
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kPlaybackDrained, 701))
|
|
.next;
|
|
assert(state.conversation == ConversationState::kListening);
|
|
assert(state.playback == PlaybackState::kIdle);
|
|
assert(ValidateRuntimeState(state).valid);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kActivationReplayDue, 7));
|
|
assert(transition.next.conversation == ConversationState::kIdle);
|
|
assert(HasEffect(
|
|
transition, AppEffectType::kSendStopListening));
|
|
|
|
state = BootToRunning();
|
|
state.call = CallState::kActive;
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kActivationReplayDue, 7));
|
|
assert(transition.next.call == CallState::kActive);
|
|
assert(transition.effect_count == 0);
|
|
}
|
|
|
|
void TestRemoteRuntimeControls()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
TransitionResult transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kAudioVolumeRequested, 0, 72));
|
|
const AppEffect* volume =
|
|
FindEffect(transition, AppEffectType::kSetAudioVolume);
|
|
assert(volume != nullptr);
|
|
assert(volume->correlation_id == 72);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kWakeWordControlRequested, 0, 0));
|
|
const AppEffect* wake = FindEffect(
|
|
transition, AppEffectType::kSetWakeWordEnabled);
|
|
assert(wake != nullptr);
|
|
assert(wake->correlation_id == 0);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kBargeInControlRequested, 0, 1));
|
|
const AppEffect* barge = FindEffect(
|
|
transition, AppEffectType::kSetBargeInEnabled);
|
|
assert(barge != nullptr);
|
|
assert(barge->correlation_id == 1);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kRemoteIdleSleepRequested));
|
|
assert(transition.next.idle_sleeping);
|
|
assert(transition.next.realtime == RealtimeState::kClosed);
|
|
assert(HasEffect(
|
|
transition, AppEffectType::kSendGoodbye));
|
|
assert(HasEffect(
|
|
transition, AppEffectType::kCloseRealtime));
|
|
wake = FindEffect(
|
|
transition, AppEffectType::kSetWakeWordEnabled);
|
|
assert(wake != nullptr);
|
|
assert(wake->correlation_id == 1);
|
|
assert(ValidateRuntimeState(transition.next).valid);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kLedAlertRequested, 0, 5000));
|
|
const AppEffect* led_alert = FindEffect(
|
|
transition, AppEffectType::kShowTemporaryLedAlert);
|
|
assert(led_alert != nullptr);
|
|
assert(led_alert->correlation_id == 5000);
|
|
|
|
transition = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kAudioVolumeRequested, 0, 101));
|
|
assert(!HasEffect(
|
|
transition, AppEffectType::kSetAudioVolume));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void TestInterruptedReplyPlaysErrorPrompt()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kTtsStarted, 900))
|
|
.next;
|
|
assert(state.conversation == ConversationState::kSpeaking);
|
|
|
|
// Playback of a server reply dies (stall timeout / decode failure).
|
|
const TransitionResult failed = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kPlaybackFailed, 900));
|
|
assert(HasEffect(
|
|
failed, AppEffectType::kRequestConnectionErrorPrompt));
|
|
assert(HasEffect(failed, AppEffectType::kReportError));
|
|
assert(failed.next.conversation == ConversationState::kIdle);
|
|
assert(ValidateRuntimeState(failed.next).valid);
|
|
|
|
// A failing local prompt already has its own feedback path and must not
|
|
// stack a second error prompt on top.
|
|
RuntimeState local = BootToRunning();
|
|
local = ReduceState(
|
|
local,
|
|
AppEvent::Make(
|
|
AppEventType::kLocalPromptStarted,
|
|
901,
|
|
static_cast<int32_t>(LocalPrompt::kStoryExpired)))
|
|
.next;
|
|
assert(local.local_prompt_active);
|
|
const TransitionResult local_failed = ReduceState(
|
|
local, AppEvent::Make(AppEventType::kPlaybackFailed, 901));
|
|
assert(!HasEffect(
|
|
local_failed, AppEffectType::kRequestConnectionErrorPrompt));
|
|
|
|
// Idle playback failure (nothing was being spoken) stays silent too.
|
|
RuntimeState idle = BootToRunning();
|
|
const TransitionResult idle_failed = ReduceState(
|
|
idle, AppEvent::Make(AppEventType::kPlaybackFailed, 0));
|
|
assert(!HasEffect(
|
|
idle_failed, AppEffectType::kRequestConnectionErrorPrompt));
|
|
}
|
|
|
|
void TestWakeWordPlaysAcknowledgement()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
assert(state.conversation == ConversationState::kIdle);
|
|
|
|
const TransitionResult woken = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected));
|
|
// The acknowledgement must be detached: the device is already listening
|
|
// and must stay there, so no kPlayLocalPrompt / kSpeaking transition.
|
|
assert(woken.next.conversation == ConversationState::kListening);
|
|
assert(HasEffect(woken, AppEffectType::kPlayDetachedPrompt));
|
|
assert(!HasEffect(woken, AppEffectType::kPlayLocalPrompt));
|
|
assert(HasEffect(woken, AppEffectType::kSendStartListening));
|
|
assert(ValidateRuntimeState(woken.next).valid);
|
|
for (size_t i = 0; i < woken.effect_count; ++i) {
|
|
if (woken.effects[i].type ==
|
|
AppEffectType::kPlayDetachedPrompt) {
|
|
assert(woken.effects[i].local_prompt ==
|
|
LocalPrompt::kWakeResponse);
|
|
}
|
|
}
|
|
|
|
// A button press is not a wake word and stays silent.
|
|
const TransitionResult clicked = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kButtonClicked, 0, 1));
|
|
assert(clicked.next.conversation == ConversationState::kListening);
|
|
assert(!HasEffect(clicked, AppEffectType::kPlayDetachedPrompt));
|
|
}
|
|
|
|
void TestServerWelcomeSkippedWhileListening()
|
|
{
|
|
// Wake word opens the session: "我在" plays, microphone is live.
|
|
RuntimeState state = BootToRunning();
|
|
state = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected))
|
|
.next;
|
|
assert(state.conversation == ConversationState::kListening);
|
|
|
|
// The server greets the new session with play_local welcome. It must be
|
|
// acked (started + done) but NOT played: no playback teardown, no
|
|
// stop-listening, and the turn stays open.
|
|
const TransitionResult greeted = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kLocalPromptRequested,
|
|
0,
|
|
static_cast<int32_t>(LocalPrompt::kWelcome),
|
|
static_cast<uint32_t>(PlaybackPriority::kLocalPrompt)));
|
|
assert(greeted.next.conversation == ConversationState::kListening);
|
|
assert(!HasEffect(greeted, AppEffectType::kPlayLocalPrompt));
|
|
assert(!HasEffect(greeted, AppEffectType::kCancelPlayback));
|
|
assert(!HasEffect(greeted, AppEffectType::kSendStopListening));
|
|
assert(HasEffect(greeted, AppEffectType::kSendLocalPlaybackStarted));
|
|
assert(HasEffect(greeted, AppEffectType::kSendLocalPlaybackDone));
|
|
|
|
// Outside a live turn the welcome still plays normally.
|
|
RuntimeState idle = BootToRunning();
|
|
const TransitionResult played = ReduceState(
|
|
idle,
|
|
AppEvent::Make(
|
|
AppEventType::kLocalPromptRequested,
|
|
0,
|
|
static_cast<int32_t>(LocalPrompt::kWelcome),
|
|
static_cast<uint32_t>(PlaybackPriority::kLocalPrompt)));
|
|
assert(HasEffect(played, AppEffectType::kPlayLocalPrompt));
|
|
}
|
|
|
|
void TestWakeAcknowledgedEvenAfterWelcome()
|
|
{
|
|
RuntimeState state = BootToRunning();
|
|
|
|
// Welcome greeting plays and drains into listening, then the turn ends.
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kLocalPromptStarted,
|
|
501,
|
|
static_cast<int32_t>(LocalPrompt::kWelcome)))
|
|
.next;
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(AppEventType::kPlaybackDrained, 501))
|
|
.next;
|
|
assert(state.conversation == ConversationState::kListening);
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kConversationStopRequested))
|
|
.next;
|
|
assert(state.conversation == ConversationState::kIdle);
|
|
|
|
// Every wake word answers "我在" out loud — including the first one right
|
|
// after the welcome greeting. Product decision: the acknowledgement is
|
|
// unconditional.
|
|
const TransitionResult first = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected));
|
|
assert(first.next.conversation == ConversationState::kListening);
|
|
assert(HasEffect(first, AppEffectType::kPlayDetachedPrompt));
|
|
state = first.next;
|
|
|
|
state = ReduceState(
|
|
state,
|
|
AppEvent::Make(
|
|
AppEventType::kConversationStopRequested))
|
|
.next;
|
|
const TransitionResult second = ReduceState(
|
|
state, AppEvent::Make(AppEventType::kWakeWordDetected));
|
|
assert(HasEffect(second, AppEffectType::kPlayDetachedPrompt));
|
|
}
|
|
|
|
int main()
|
|
{
|
|
TestBootSequence();
|
|
TestConversationPlaybackDrain();
|
|
TestLocalPlaybackPreemptsConversationState();
|
|
TestDisconnectRecovery();
|
|
TestExclusiveUpgradeState();
|
|
TestSingleClickControlsConversation();
|
|
TestSingleClickInterruptsSpeaking();
|
|
TestWakeWordInterruptsSpeaking();
|
|
TestServerArbitratesNaturalBargeIn();
|
|
TestButtonLifecycleRequests();
|
|
TestProvisioningIsAudible();
|
|
TestServerLocalPromptAndShutdownDrain();
|
|
TestConfiguredMultiClickActions();
|
|
TestSingleClickControlsCalls();
|
|
TestIdleSleepAndAlertFlow();
|
|
TestAlertDeferredUntilStartupCompletes();
|
|
TestFourClicksRequestNetworkSwitch();
|
|
TestTripleClickFriendMode();
|
|
TestLocalSequenceLifecycle();
|
|
TestRemoteRuntimeControls();
|
|
TestInterruptedReplyPlaysErrorPrompt();
|
|
TestWakeWordPlaysAcknowledgement();
|
|
TestServerWelcomeSkippedWhileListening();
|
|
TestWakeAcknowledgedEvenAfterWelcome();
|
|
|
|
std::cout << "state reducer tests passed\n";
|
|
return 0;
|
|
}
|