#include #include #include #include #include using Void::Coro::Task; using Void::Fsm::MachineOptions; using Void::Fsm::State; using Void::Fsm::StateKey; namespace Fsm = Void::Fsm; using namespace std::chrono_literals; // ── fluent events + free jumps ────────────────────────────────────────────── namespace { struct Add { int n; }; // Sums two Add events, posted in one fluent send() call. class Sum : public Fsm::Machine { public: using Success = int; int total = 0; struct States { struct Take : State { Task run() { auto a = co_await await_event(); self().total += a.n; auto b = co_await await_event(); self().total += b.n; co_return succeed(self().total); } }; }; }; } // namespace // @tag integration Void::Testing::Test send_posts_multiple_events() { Sum fsm; fsm.start(); fsm.send(Add{3}, Add{4}); // one fluent call, two events const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 7); } namespace { // A and C are not connected by any declared edge; goto_state() jumps // from A to C freely. class Hop : public Fsm::Machine { public: using Success = int; struct States { struct A : State { Task run() { co_return goto_state(); } }; struct B : State { Task run() { co_return succeed(1); } }; struct C : State { Task run() { co_return succeed(3); } }; }; }; } // namespace // @tag integration Void::Testing::Test goto_state_jumps_to_any_registered_state() { Hop fsm; const auto outcome = fsm.run_blocking(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 3); // went A -> C, skipping B } namespace { struct Tick { }; // Loops in Loop on each Tick; an external jump() redirects at the // next boundary. class Jumper : public Fsm::Machine { public: using Success = int; struct States { struct Loop : State { Task run() { co_await await_event(); co_return next(); } }; struct Done : State { Task run() { co_return succeed(99); } }; }; explicit Jumper(MachineOptions o = {}) : Fsm::Machine(o) {} }; } // namespace Void::Testing::Test jump_redirects_at_next_boundary() { Jumper fsm{MachineOptions{.own_worker = false}}; fsm.start(); while (fsm.pump() > 0) { } // enter Loop, park on Tick fsm.send(Tick{}); while (fsm.pump() > 0) { } // Loop -> next -> park again fsm.jump(); // request a jump fsm.send(Tick{}); while (fsm.pump() > 0) { } // Loop finishes -> boundary applies the jump -> Done const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 99); } // ── lifecycle control ─────────────────────────────────────────────────────── namespace { // Increments a machine-owned counter each run (data survives reset()). class Counter : public Fsm::Machine { public: using Success = int; int runs = 0; struct States { struct Once : State { Task run() { co_return succeed(++self().runs); } }; }; }; } // namespace // @tag integration Void::Testing::Test restart_reruns_the_machine() { Counter fsm; const auto first = fsm.run_blocking(); Void::Testing::expect_eq(*first, 1); fsm.restart(); const auto second = fsm.wait(); Void::Testing::expect_eq(*second, 2); } namespace { class Pick : public Fsm::Machine { public: using Success = int; struct States { struct A : State { Task run() { co_return succeed(1); } }; struct B : State { Task run() { co_return succeed(2); } }; }; // initial = A }; } // namespace // @tag integration Void::Testing::Test restart_at_changes_initial_state() { Pick fsm; const auto first = fsm.run_blocking(); Void::Testing::expect_eq(*first, 1); // A fsm.restart_at(); const auto second = fsm.wait(); Void::Testing::expect_eq(*second, 2); // B } // ── runtime mutation (Idle only) ──────────────────────────────────────────── namespace { class Mut : public Fsm::Machine { public: using Success = int; struct States { struct A : State { Task run() { co_return succeed(1); } }; struct B : State { Task run() { co_return succeed(2); } }; struct C : State { Task run() { co_return succeed(3); } }; }; // Not in States — added via replace<>. struct Extra : State { Task run() { co_return succeed(7); } }; }; } // namespace Void::Testing::Test remove_and_set_initial_before_start() { Mut fsm; Void::Testing::expect(fsm.has_state()); fsm.remove(); Void::Testing::expect(!fsm.has_state()); Void::Testing::expect_eq(fsm.describe().size(), std::size_t{2}); fsm.set_initial(); const auto outcome = fsm.run_blocking(); Void::Testing::expect_eq(*outcome, 2); } Void::Testing::Test replace_swaps_a_state_and_keeps_initial() { Mut fsm; // A was initial -> Extra becomes initial. fsm.replace(); Void::Testing::expect(!fsm.has_state()); Void::Testing::expect(fsm.has_state()); const auto outcome = fsm.run_blocking(); Void::Testing::expect_eq(*outcome, 7); } // ── pause / resume ────────────────────────────────────────────────────────── namespace { struct Go { }; class Sleeper : public Fsm::Machine { public: using Success = int; struct States { struct W : State { Task run() { co_await await_event(); co_return succeed(5); } }; }; }; } // namespace // @tag integration Void::Testing::Test pause_holds_events_until_resume() { Sleeper fsm; fsm.start(); fsm.pause(); fsm.post(Go{}); // buffered while paused, not delivered std::this_thread::sleep_for(20ms); Void::Testing::expect(!fsm.finished()); // still parked, event held fsm.resume(); const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 5); }