Files
Library/tests/standalone/Fsm/Nesting.cpp
T
faumarayandClaude Fable 5 e941a48007 FSM v2 (A): CRTP typed access, States<> auto-registration, lifecycle, observers
Maintainability core, breaking the state API (all FSM tests migrated):
- Machine<Derived, ResultT> (CRTP). State::self() returns Derived& — drops
  the static_cast<Derived&>(machine()) boilerplate from every state.
- `using States = Fsm::States<...>` auto-registers every state in the base
  ctor (first entry = initial unless `using Initial = S`); no more manual
  add<S>()/initial<S>() for static machines. next<S>() static_asserts S is
  in the list (unregistered targets are now a COMPILE error, not a runtime
  fail); next_dynamic<S>() + add<S>()/add_if<S>() remain for dynamic
  machines.
- Per-state lifecycle: StateBase gains virtual on_enter()/on_exit()
  (Coro::Task<void>, default no-op) run around run(); the engine visit is
  enter -> run -> exit, so entry/exit actions split out of run(). run()
  now has a default (fails) so reactive states (Phase B) needn't define it.
- Observability: virtual on_transition(from,to) (default logs via Void::Log)
  + observe(fn) called on every transition (and the initial entry) with a
  Transition{from,to,step}. state_path()/state_names() added.

Migrated Basic/Events/Nesting/Serialize/Concurrency + the Orchestrator and
service-hot-reload tests to CRTP + self() + States<>; new Lifecycle suite
(enter/run/exit ordering, observer edges, default-run failure). 968 green,
ASan+UBSan clean, -Werror clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-14 01:03:43 +03:00

139 lines
3.1 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
using Void::Coro::Task;
using Void::Fsm::MachineOptions;
using Void::Fsm::Step;
namespace
{
// Child machine: computes a value and succeeds.
class Subtotal : public Void::Fsm::Machine<Subtotal, int>
{
public:
int seed;
struct Compute : State {
Task<Step> run() override
{
auto& m = self();
co_return succeed(m.seed * 10);
}
};
explicit Subtotal(int s)
: Void::Fsm::Machine<Subtotal, int>(
MachineOptions{.own_worker = false}),
seed(s)
{
initial<Compute>();
}
};
// Child machine that fails.
class FailingChild : public Void::Fsm::Machine<FailingChild, int>
{
public:
struct Boom : State {
Task<Step> run() override { co_return fail("child boom"); }
};
FailingChild()
: Void::Fsm::Machine<FailingChild, int>(
MachineOptions{.own_worker = false})
{
initial<Boom>();
}
};
// Parent: enters a child, uses its result to decide the outcome.
class Parent : public Void::Fsm::Machine<Parent, int>
{
public:
struct Start : State {
Task<Step> run() override
{
auto a = co_await run_child<Subtotal>(3); // 30
auto b = co_await run_child<Subtotal>(4); // 40
if (!a.has_value() || !b.has_value()) {
co_return fail("child failed");
}
co_return succeed(*a + *b);
}
};
Parent()
: Void::Fsm::Machine<Parent, int>(
MachineOptions{.own_worker = false})
{
initial<Start>();
}
};
class ParentOfFailure : public Void::Fsm::Machine<ParentOfFailure, int>
{
public:
struct Start : State {
Task<Step> run() override
{
auto r = co_await run_child<FailingChild>();
if (!r.has_value()) {
co_return fail("propagated: " + r.error().message);
}
co_return succeed(*r);
}
};
ParentOfFailure()
: Void::Fsm::Machine<ParentOfFailure, int>(
MachineOptions{.own_worker = false})
{
initial<Start>();
}
};
} // namespace
Void::Testing::Test fsm_nested_child_result_drives_parent()
{
Parent fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 70); // 30 + 40
}
Void::Testing::Test fsm_nested_child_failure_propagates()
{
ParentOfFailure fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(!outcome.has_value());
Void::Testing::expect_eq(outcome.error().message,
std::string{"propagated: child boom"});
}
// @tag integration
Void::Testing::Test fsm_nesting_with_worker_thread()
{
Parent fsm{}; // embedder by default here
// Re-run as worker-driven to exercise the shared executor + worker.
struct WorkerParent : Void::Fsm::Machine<WorkerParent, int> {
struct Start : State {
Task<Step> run() override
{
auto a = co_await run_child<Subtotal>(5); // 50
co_return succeed(a.value_or(-1));
}
};
WorkerParent() { initial<Start>(); } // own worker
} wfsm;
const auto outcome = wfsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 50);
(void)fsm;
}