Files
Library/tests/standalone/Fsm/Direct.cpp
T
faumarayandClaude Fable 5 9e9eb838ea FSM: typed Success/Failure result model (reflected nested types)
A machine is now Machine<Derived> (dropping the ResultT template param) and
declares its result types as nested types reflected *by name* — Success and
Failure (a struct or a using-alias), each defaulting to void:

    class Login : public Machine<Login> {
      using Success = Token;     // or a nested struct; default void
      struct Failure { int code; };
    };

- Outcome = std::expected<Success, Failure> (void failure -> std::monostate
  error arm). succeed(Success{...}) / fail(Failure{...}) carry the typed
  values (nullary when void). Resolved by plain dependent-name lookup
  (Detail/Result.hpp), no P2996.
- Engine/system faults (no initial state, unregistered target, no entry)
  travel a SEPARATE FsmError channel: status()==Faulted + system_error(),
  never the typed Failure. New Step::Kind::Fault carries it.
- CRTP wrinkle: Derived is incomplete at base instantiation, so the engine
  stores the result type-erased (std::any) and rebuilds the typed Outcome in
  wait()/run_blocking()/take_outcome() — function templates defaulted on
  Derived. run_child/run_region/start_machine reconstruct OutcomeOf<Sub> at
  the boundary (Sub is complete).
- Step carries Success/Failure payloads as std::any; the reactive DSL
  (finish/fail) and Continue<Sub> are reparameterised off the nested types.

Interim: Continue<Sub> keeps the named on_success/on_failure form (Phase 2
replaces it with signature-inferred ChildSuccess/ChildFailure). All FSM
tests migrated to the nested-type form. 995 tests (992 green, 3 skipped),
ASan clean, -Werror clean; the lone intermittent failure is the
pre-existing racy plugin hot-reload-under-load test (flaky on HEAD too).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-14 21:14:43 +03:00

290 lines
6.7 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <thread>
using Void::Coro::Task;
using Void::Fsm::MachineOptions;
using Void::Fsm::Step;
namespace Fsm = Void::Fsm;
using namespace std::chrono_literals;
// ── fluent events + free jumps ──────────────────────────────────────────────
namespace
{
struct Add {
int n;
};
// Sums two Add events, posted in one fluent send() call.
class Sum : public Fsm::Machine<Sum>
{
public:
using Success = int;
int total = 0;
struct Take : State {
Task<Step> run() override
{
auto a = co_await await_event<Add>();
self().total += a.n;
auto b = co_await await_event<Add>();
self().total += b.n;
co_return succeed(self().total);
}
};
using States = Fsm::States<Take>;
};
} // namespace
// @tag integration
Void::Testing::Test send_posts_multiple_events()
{
Sum fsm;
fsm.start();
fsm.send(Add{3}, Add{4}); // one fluent call, two events
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 7);
}
namespace
{
// A and C are not connected by any declared edge; goto_state<C>() jumps
// from A to C freely.
class Hop : public Fsm::Machine<Hop>
{
public:
using Success = int;
struct A : State {
Task<Step> run() override { co_return goto_state<C>(); }
};
struct B : State {
Task<Step> run() override { co_return succeed(1); }
};
struct C : State {
Task<Step> run() override { co_return succeed(3); }
};
using States = Fsm::States<A, B, C>;
};
} // namespace
// @tag integration
Void::Testing::Test goto_state_jumps_to_any_registered_state()
{
Hop fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 3); // went A -> C, skipping B
}
namespace
{
struct Tick {
};
// Loops in Loop on each Tick; an external jump<Done>() redirects at the
// next boundary.
class Jumper : public Fsm::Machine<Jumper>
{
public:
using Success = int;
struct Loop : State {
Task<Step> run() override
{
co_await await_event<Tick>();
co_return next<Loop>();
}
};
struct Done : State {
Task<Step> run() override { co_return succeed(99); }
};
using States = Fsm::States<Loop, Done>;
explicit Jumper(MachineOptions o = {}) : Fsm::Machine<Jumper>(o) {}
};
} // namespace
Void::Testing::Test jump_redirects_at_next_boundary()
{
Jumper fsm{MachineOptions{.own_worker = false}};
fsm.start();
while (fsm.pump() > 0) {
} // enter Loop, park on Tick
fsm.send(Tick{});
while (fsm.pump() > 0) {
} // Loop -> next<Loop> -> park again
fsm.jump<Jumper::Done>(); // request a jump
fsm.send(Tick{});
while (fsm.pump() > 0) {
} // Loop finishes -> boundary applies the jump -> Done
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 99);
}
// ── lifecycle control ───────────────────────────────────────────────────────
namespace
{
// Increments a machine-owned counter each run (data survives reset()).
class Counter : public Fsm::Machine<Counter>
{
public:
using Success = int;
int runs = 0;
struct Once : State {
Task<Step> run() override
{
co_return succeed(++self().runs);
}
};
using States = Fsm::States<Once>;
};
} // namespace
// @tag integration
Void::Testing::Test restart_reruns_the_machine()
{
Counter fsm;
const auto first = fsm.run_blocking();
Void::Testing::expect_eq(*first, 1);
fsm.restart();
const auto second = fsm.wait();
Void::Testing::expect_eq(*second, 2);
}
namespace
{
class Pick : public Fsm::Machine<Pick>
{
public:
using Success = int;
struct A : State {
Task<Step> run() override { co_return succeed(1); }
};
struct B : State {
Task<Step> run() override { co_return succeed(2); }
};
using States = Fsm::States<A, B>; // initial = A
};
} // namespace
// @tag integration
Void::Testing::Test restart_at_changes_initial_state()
{
Pick fsm;
const auto first = fsm.run_blocking();
Void::Testing::expect_eq(*first, 1); // A
fsm.restart_at<Pick::B>();
const auto second = fsm.wait();
Void::Testing::expect_eq(*second, 2); // B
}
// ── runtime mutation (Idle only) ────────────────────────────────────────────
namespace
{
class Mut : public Fsm::Machine<Mut>
{
public:
using Success = int;
struct A : State {
Task<Step> run() override { co_return succeed(1); }
};
struct B : State {
Task<Step> run() override { co_return succeed(2); }
};
struct C : State {
Task<Step> run() override { co_return succeed(3); }
};
// Not in States<> — added via replace<>.
struct Extra : State {
Task<Step> run() override { co_return succeed(7); }
};
using States = Fsm::States<A, B, C>;
};
} // namespace
Void::Testing::Test remove_and_set_initial_before_start()
{
Mut fsm;
Void::Testing::expect(fsm.has_state<Mut::C>());
fsm.remove<Mut::C>();
Void::Testing::expect(!fsm.has_state<Mut::C>());
Void::Testing::expect_eq(fsm.describe().size(), std::size_t{2});
fsm.set_initial<Mut::B>();
const auto outcome = fsm.run_blocking();
Void::Testing::expect_eq(*outcome, 2);
}
Void::Testing::Test replace_swaps_a_state_and_keeps_initial()
{
Mut fsm;
fsm.replace<Mut::A, Mut::Extra>(); // A was initial -> Extra becomes initial
Void::Testing::expect(!fsm.has_state<Mut::A>());
Void::Testing::expect(fsm.has_state<Mut::Extra>());
const auto outcome = fsm.run_blocking();
Void::Testing::expect_eq(*outcome, 7);
}
// ── pause / resume ──────────────────────────────────────────────────────────
namespace
{
struct Go {
};
class Sleeper : public Fsm::Machine<Sleeper>
{
public:
using Success = int;
struct W : State {
Task<Step> run() override
{
co_await await_event<Go>();
co_return succeed(5);
}
};
using States = Fsm::States<W>;
};
} // namespace
// @tag integration
Void::Testing::Test pause_holds_events_until_resume()
{
Sleeper fsm;
fsm.start();
fsm.pause();
fsm.post(Go{}); // buffered while paused, not delivered
std::this_thread::sleep_for(20ms);
Void::Testing::expect(!fsm.finished()); // still parked, event held
fsm.resume();
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 5);
}