#include #include #include #include #include using Void::Coro::Task; using Void::Fsm::Step; namespace Fsm = Void::Fsm; // Phase-6 reactive DSL: deferred events (held + replayed after a transition), // entry/exit actions (on.enter/on.exit), and internal transitions (handle an // event without leaving the state). namespace { struct Click { }; struct Done { }; // Busy defers Click (held, not dropped) and runs enter/exit actions; on // Done it transitions to Idle, which then handles the replayed Click. class Flow : public Fsm::Machine { public: using Success = int; std::vector log; struct Busy : State { void transitions(TransitionBuilder& on) { on.enter([](Flow& m) { m.log.push_back("enter"); }); on.exit([](Flow& m) { m.log.push_back("exit"); }); on.template defer(); on.template event().template to(); } }; struct Idle : State { void transitions(TransitionBuilder& on) { on.template event() .action([](Flow& m, const Click&) { m.log.push_back("click"); }) .template to(); } }; struct End : State { Task run() { co_return succeed(1); } }; using States = Fsm::States; }; } // namespace // @tag integration Void::Testing::Test deferred_event_replays_after_transition() { Flow fsm; fsm.start(); fsm.send(Click{}); // deferred while in Busy (not dropped) fsm.send(Done{}); // Busy -> Idle; the held Click is replayed -> End const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 1); // enter/exit actions fired around Busy; the deferred Click was handled. const std::vector expected{"enter", "exit", "click"}; Void::Testing::expect(fsm.log == expected); } // ── internal transition ───────────────────────────────────────────────────── namespace { struct Tick { }; struct Stop { }; // Counter handles Tick internally (no exit/enter, no transition); Stop // leaves with the accumulated count. class Counter : public Fsm::Machine { public: using Success = int; int ticks = 0; int enters = 0; struct Run : State { void transitions(TransitionBuilder& on) { on.enter([](Counter& m) { ++m.enters; }); on.template event().internal( [](Counter& m, const Tick&) { ++m.ticks; }); on.template event().template to(); } }; struct Done : State { Task run() { co_return succeed(self().ticks); } }; using States = Fsm::States; }; } // namespace // @tag integration Void::Testing::Test internal_transition_stays_in_state() { Counter fsm; fsm.start(); fsm.send(Tick{}, Tick{}, Tick{}); // handled internally, no re-entry fsm.send(Stop{}); const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 3); // three internal ticks Void::Testing::expect_eq(fsm.enters, 1); // entered Run exactly once }