#include #include #include #include #include #include #include #include using Void::Coro::Task; using Void::Fsm::MachineOptions; using Void::Fsm::State; using Void::Fsm::StateKey; using Void::Fsm::Transition; using namespace std::chrono_literals; // ── retry / next_or // ─────────────────────────────────────────────────────────── namespace { // Attempt fails the first `fail_n` times, then succeeds. with_retry // keeps trying; next_or routes exhaustion to a fallback. class Flaky : public Void::Fsm::Machine { public: using Success = std::string; using Failure = Void::Fsm::FsmError; int fail_n = 0; int attempts = 0; int retries; // how many tries with_retry gets struct States { struct Try : State { Task run() { auto& m = self(); const bool ok = co_await with_retry( m.retries, 1ms, [&m]() -> Task { ++m.attempts; co_return m.attempts > m.fail_n; }); co_return next_or( ok ? succeed(std::string{"ok"}) : fail("exhausted")); } }; struct Fallback : State { Task run() { co_return succeed(std::string{"fallback"}); } }; }; explicit Flaky(int fails, int tries) : fail_n(fails), retries(tries) { } // own worker }; } // namespace // @tag integration Void::Testing::Test retry_eventually_succeeds() { Flaky fsm{2, 5}; // fails twice, 5 tries available -> succeeds on 3rd const auto outcome = fsm.run_blocking(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, std::string{"ok"}); Void::Testing::expect_eq(fsm.attempts, 3); } // @tag integration Void::Testing::Test retry_exhausts_routes_to_fallback() { Flaky fsm{10, 3}; // always fails within 3 tries -> next_or const auto outcome = fsm.run_blocking(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, std::string{"fallback"}); Void::Testing::expect_eq(fsm.attempts, 3); } // ── interceptors // ────────────────────────────────────────────────────────────── namespace { class Pipe : public Void::Fsm::Machine { public: using Success = int; struct States { struct A : State { Task run() { co_return next(); } }; struct B : State { Task run() { co_return succeed(1); } }; }; Pipe() : Void::Fsm::Machine(MachineOptions{.own_worker = false}) { } }; } // namespace Void::Testing::Test interceptor_before_after_order() { Pipe fsm; std::vector events; fsm.add_interceptor( {.before = [&](const Transition& t) { events.push_back("before:" + t.to); }, .after = [&](std::string_view s) { events.push_back("after:" + std::string{s}); }}); (void)fsm.run_blocking(); // A entered (before:A), A runs+finishes (after:A), B entered // (before:B), B runs (after:B). const std::vector expected{"before:A", "after:A", "before:B", "after:B"}; Void::Testing::expect(events == expected); } Void::Testing::Test interceptors_run_in_registration_order() { Pipe fsm; std::vector order; fsm.add_interceptor( {.before = [&](const Transition&) { order.push_back(1); }}); fsm.add_interceptor( {.before = [&](const Transition&) { order.push_back(2); }}); (void)fsm.run_blocking(); // Two transitions (A, B) x two interceptors → 1,2,1,2. const std::vector expected{1, 2, 1, 2}; Void::Testing::expect(order == expected); } // ── introspection / diagram // ─────────────────────────────────────────────────── namespace { struct Go { }; struct Cancel { }; // Imperative event handling: Idle awaits Go or Cancel and routes. class Flow : public Void::Fsm::Machine { public: using Success = int; struct States { struct Idle : State { Task run() { auto ev = co_await await_any(); co_return std::holds_alternative(ev) ? next() : next(); } }; struct Active : State { Task run() { co_return next(); } }; struct Done : State { Task run() { co_return succeed(0); } }; }; Flow() {} }; } // namespace // describe() is node-level: behaviour (and therefore transitions) is chosen // imperatively inside each state's entry, so edges are not statically knowable. Void::Testing::Test describe_lists_states_node_level() { Flow fsm; const auto states = fsm.describe(); Void::Testing::expect_eq(states.size(), std::size_t{3}); // Registration order; Idle is the initial state. Void::Testing::expect_eq(states[0].name, std::string{"Idle"}); Void::Testing::expect(states[0].initial); Void::Testing::expect_eq(states[1].name, std::string{"Active"}); Void::Testing::expect(!states[1].initial); Void::Testing::expect_eq(states[2].name, std::string{"Done"}); } Void::Testing::Test to_mermaid_lists_nodes_and_initial() { Flow fsm; const auto diagram = fsm.to_mermaid(); std::cout << diagram << std::endl; Void::Testing::expect(diagram.find("stateDiagram-v2") != std::string::npos); Void::Testing::expect(diagram.find("[*] --> Idle") != std::string::npos); // One node per registered state. Void::Testing::expect(diagram.find("\n Idle\n") != std::string::npos); Void::Testing::expect(diagram.find("\n Active\n") != std::string::npos); Void::Testing::expect(diagram.find("\n Done\n") != std::string::npos); }