#include #include #include #include using Void::Coro::Task; using Void::Fsm::ChildFailure; using Void::Fsm::ChildSuccess; using Void::Fsm::FsmError; using Void::Fsm::Step; namespace Fsm = Void::Fsm; // startMachine(data) launches a full sub-FSM on the parent's shared // executor/events/services (startup data routed to Sub's initial-state // startup-entry), then enters Next. The engine dispatches the sub's typed // outcome to Next's signature-matched handler — a member taking // `ChildSuccess auto` (success) or `ChildFailure auto` (failure). No override, // no fixed names. namespace { // Sub-machine seeded via its startup-entry (run(int)): doubles the seed, // fails (typed FsmError) if negative. class Doubler : public Fsm::Machine { public: using Success = int; using Failure = Void::Fsm::FsmError; struct Run : State { Task run(int seed) { if (seed < 0) { co_return fail("negative"); } co_return succeed(seed * 2); } }; using States = Fsm::States; }; class Calc : public Fsm::Machine { public: using Success = std::string; int input = 0; struct Start : State { Task run() { co_return startMachine( self().input); } }; struct AfterDouble : State { Task ok(ChildSuccess auto s) { co_return succeed("doubled:" + std::to_string(s.value)); } Task no(ChildFailure auto f) { co_return succeed("failed:" + f.value.message); } }; using States = Fsm::States; explicit Calc(int in) : input(in) {} }; } // namespace // @tag integration Void::Testing::Test submachine_success_dispatches_on_success() { Calc fsm{5}; const auto outcome = fsm.run_blocking(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, std::string{"doubled:10"}); } // @tag integration Void::Testing::Test submachine_failure_dispatches_on_failure() { Calc fsm{-1}; const auto outcome = fsm.run_blocking(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, std::string{"failed:negative"}); } // ── void-result sub-machine ───────────────────────────────────────────────── namespace { class Pinger : public Fsm::Machine { public: struct Ping : State { Task run() { co_return succeed(); } }; using States = Fsm::States; }; class Host : public Fsm::Machine { public: using Success = int; struct Go : State { Task run() { co_return startMachine(); } }; struct Done : State { // void-result sub: ChildSuccess box has no value. Task ok(ChildSuccess auto) { co_return succeed(42); } }; using States = Fsm::States; }; } // namespace // @tag integration Void::Testing::Test submachine_void_result_calls_success_handler() { Host fsm; const auto outcome = fsm.run_blocking(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 42); } // ── chained startMachine (a continuation state launches another) ───────────── namespace { class Chain : public Fsm::Machine { public: using Success = int; struct A : State { Task run() { co_return startMachine(3); // 3 -> 6 } }; struct B : State { Task ok(ChildSuccess auto s) { co_return startMachine(s.value); // 6 -> 12 } }; struct C : State { Task ok(ChildSuccess auto s) { co_return succeed(s.value); } }; using States = Fsm::States; }; } // namespace // @tag integration Void::Testing::Test submachine_chains_through_continuation_states() { Chain fsm; const auto outcome = fsm.run_blocking(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 12); } // ── the sub-machine shares the parent's event mailbox ──────────────────────── namespace { struct Tick { }; class WaitSub : public Fsm::Machine { public: using Success = int; struct W : State { Task run() { co_await await_event(); co_return succeed(7); } }; using States = Fsm::States; }; class Outer : public Fsm::Machine { public: using Success = int; struct Begin : State { Task run() { co_return startMachine(); } }; struct Fin : State { Task ok(ChildSuccess auto s) { co_return succeed(s.value); } }; using States = Fsm::States; }; } // namespace // @tag integration Void::Testing::Test submachine_shares_parent_event_mailbox() { Outer fsm; fsm.start(); fsm.post(Tick{}); // delivered to the sub-machine awaiting on the shared ctx const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 7); }