#include #include #include #include using Void::Coro::Task; using Void::Fsm::FsmError; using Void::Fsm::Step; namespace Fsm = Void::Fsm; // startMachine(args...) launches a full sub-FSM on the parent's // shared executor/events/services, then enters Next — a Continue // state — and the engine dispatches to on_success / on_failure by the outcome. namespace { // Sub-machine seeded by constructor arg: doubles `seed`, fails if negative. class Doubler : public Fsm::Machine { public: int seed; struct Run : State { Task run() override { if (self().seed < 0) { co_return fail("negative"); } co_return succeed(self().seed * 2); } }; using States = Fsm::States; explicit Doubler(int s = 0) : seed(s) {} }; class Calc : public Fsm::Machine { public: int input = 0; struct Start : State { Task run() override { co_return startMachine( self().input); } }; struct AfterDouble : Continue { Task on_success(int v) override { co_return succeed("doubled:" + std::to_string(v)); } Task on_failure(FsmError e) override { co_return succeed("failed:" + e.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() override { co_return succeed(); } }; using States = Fsm::States; }; class Host : public Fsm::Machine { public: struct Go : State { Task run() override { co_return startMachine(); } }; struct Done : Continue { Task on_success() override { co_return succeed(42); } }; using States = Fsm::States; }; } // namespace // @tag integration Void::Testing::Test submachine_void_result_calls_nullary_on_success() { Host fsm; const auto outcome = fsm.run_blocking(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 42); } // ── chained startMachine (a Continue state launches another) ───────────────── namespace { class Chain : public Fsm::Machine { public: struct A : State { Task run() override { co_return startMachine(3); // 3 -> 6 } }; struct B : Continue { Task on_success(int v) override { co_return startMachine(v); // 6 -> 12 } }; struct C : Continue { Task on_success(int v) override { co_return succeed(v); } }; using States = Fsm::States; }; } // namespace // @tag integration Void::Testing::Test submachine_chains_through_continue_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: struct W : State { Task run() override { co_await await_event(); co_return succeed(7); } }; using States = Fsm::States; }; class Outer : public Fsm::Machine { public: struct Begin : State { Task run() override { co_return startMachine(); } }; struct Fin : Continue { Task on_success(int v) override { co_return succeed(v); } }; 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); }