#include #include #include #include #include #include using Void::Coro::Task; using Void::Fsm::MachineOptions; using Void::Fsm::State; using Void::Fsm::StateKey; // Composite sub-states: a parent state runs a child region (run_region) that // shares the parent's executor, event mailbox and services. The child's // Outcome drives the parent's next transition; a parent-level await_any lets // the region react to several event types (cooperative cancel). namespace { struct Tick { }; struct Cancel { }; // Child region: waits for either Tick (succeed) or Cancel (fail), // handling both at one level via await_any. class Work : public Void::Fsm::Machine { public: using Success = std::string; using Failure = Void::Fsm::FsmError; struct States { struct Waiting : State { Task run() { auto ev = co_await await_any(); if (std::holds_alternative(ev)) { co_return fail("cancelled"); } co_return succeed(std::string{"ticked"}); } }; }; explicit Work(MachineOptions opts = {}) : Void::Fsm::Machine(opts) { } }; // Parent: a composite Running state enters the Work region and maps its // result — success passes the value through, failure routes to a sibling. class Job : public Void::Fsm::Machine { public: using Success = std::string; struct States { struct Running : State { Task run() { auto r = co_await run_region(); if (r.has_value()) { co_return succeed(*r); } co_return next(); } }; struct Aborted : State { Task run() { co_return succeed(std::string{"aborted"}); } }; }; explicit Job(MachineOptions opts = {}) : Void::Fsm::Machine(opts) { } }; } // namespace // @tag integration Void::Testing::Test composite_region_result_drives_parent() { Job fsm; fsm.start(); fsm.post(Tick{}); // region succeeds -> parent passes "ticked" through const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, std::string{"ticked"}); } // @tag integration Void::Testing::Test parent_cancel_routes_to_sibling() { Job fsm; fsm.start(); fsm.post(Cancel{}); // region fails -> parent transitions to Aborted const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, std::string{"aborted"}); } // state_path() reports the parent→child chain while a region is active. // Embedder mode keeps the single consumer on this thread, so the read is // deterministic. Void::Testing::Test state_path_reports_parent_child() { Job fsm{MachineOptions{.own_worker = false}}; fsm.start(); // Drive until the region parks on await_any: parent in Running, child in // Waiting. while (fsm.pump() > 0) { } const std::vector expected{"Running", "Waiting"}; Void::Testing::expect(fsm.state_path() == expected); // Release the region and run to completion. fsm.post(Tick{}); const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, std::string{"ticked"}); // Region gone → path collapses to the (final) parent state only. Void::Testing::expect_eq(fsm.state_path().size(), std::size_t{1}); }