#include #include #include #include #include using Void::Coro::Task; using Void::Fsm::MachineOptions; using Void::Fsm::Step; namespace { // A linear workflow: Start -> Work (loops 3x) -> success(count). class Counter : public Void::Fsm::Machine { public: int count = 0; std::vector* log = nullptr; struct Start : State { Task run() override { auto& m = static_cast(machine()); if (m.log) { m.log->push_back("start"); } co_return next(); } }; struct Work : State { Task run() override { auto& m = static_cast(machine()); ++m.count; if (m.log) { m.log->push_back("work" + std::to_string(m.count)); } if (m.count >= 3) { co_return succeed(m.count); } co_return next(); } }; Counter() : Void::Fsm::Machine(MachineOptions{.own_worker = false}) { initial(); add(); } }; // A workflow that fails. class Failing : public Void::Fsm::Machine { public: struct Boom : State { Task run() override { co_return fail("kaboom"); } }; Failing() : Void::Fsm::Machine(MachineOptions{.own_worker = false}) { initial(); } }; } // namespace Void::Testing::Test fsm_runs_to_success_with_result() { std::vector log; Counter fsm; fsm.log = &log; const auto outcome = fsm.run_blocking(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 3); Void::Testing::expect(fsm.status() == Void::Fsm::Status::Succeeded); const std::vector expected{"start", "work1", "work2", "work3"}; Void::Testing::expect(log == expected); } Void::Testing::Test fsm_failure_outcome() { Failing fsm; const auto outcome = fsm.run_blocking(); Void::Testing::expect(!outcome.has_value()); Void::Testing::expect_eq(outcome.error().message, std::string{"kaboom"}); Void::Testing::expect(fsm.status() == Void::Fsm::Status::Failed); } Void::Testing::Test fsm_pump_drives_to_completion() { Counter fsm; fsm.start(); Void::Testing::expect(!fsm.finished()); // driver queued, not yet run fsm.pump(); // Start -> Work* -> success Void::Testing::expect(fsm.finished()); Void::Testing::expect_eq(*fsm.wait(), 3); } Void::Testing::Test fsm_transition_to_unregistered_fails() { struct Lonely : Void::Fsm::Machine { struct Other : State { Task run() override { co_return succeed(); } }; struct Start : State { Task run() override { co_return next(); } }; Lonely() : Void::Fsm::Machine(MachineOptions{.own_worker = false}) { initial(); // Other intentionally not registered } } fsm; const auto outcome = fsm.run_blocking(); Void::Testing::expect(!outcome.has_value()); } Void::Testing::Test fsm_void_result_succeeds() { struct Trivial : Void::Fsm::Machine { struct Done : State { Task run() override { co_return succeed(); } }; Trivial() : Void::Fsm::Machine(MachineOptions{.own_worker = false}) { initial(); } } fsm; const auto outcome = fsm.run_blocking(); Void::Testing::expect(outcome.has_value()); } Void::Testing::Test fsm_dynamic_state_registration() { // Two configurations of the same machine type: with and without the // optional Express state. struct Flow : Void::Fsm::Machine { bool express; struct Express : State { Task run() override { co_return succeed(99); } }; struct Normal : State { Task run() override { co_return succeed(1); } }; struct Start : State { Task run() override { auto& m = static_cast(machine()); if (m.has_state()) { co_return next(); } co_return next(); } }; explicit Flow(bool e) : Void::Fsm::Machine(MachineOptions{.own_worker = false}), express(e) { initial(); add(); add_if(express); } }; Flow with{true}; Void::Testing::expect_eq(*with.run_blocking(), 99); Flow without{false}; Void::Testing::expect_eq(*without.run_blocking(), 1); }