Remove the two old FSMs ahead of the inheritance-based rewrite

Deletes the compile-time Void::StateMachine and the builder-based runtime
Void::Fsm (Definition/Machine/Event/PluginBehavior + their standalone
tests, the FSM behaviour plugins, and the FSM x Plugin integration test
and its CMake wiring). The general-purpose Plugin module and its stateful
hot-reload tests are untouched.

Both are being replaced by a single inheritance + coroutine workflow FSM
(next phase). 936 tests green (51 old FSM/StateMachine tests removed).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-14 00:26:30 +03:00
1 parent 6b5f61f00a
commit 04cf1998bd
20 files changed
+3 -3793

No files matched your search

-18
View File
@@ -116,22 +116,4 @@ if(TARGET Void::Plugin)
target_include_directories(VoidTests PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/plugins")
add_dependencies(VoidTests VoidStatefulPluginV1 VoidStatefulPluginV2)
# FSM behaviour plugins (states/actions provided by a plugin) for the
# FSM x Plugin hot-reload integration test. Same state names across
# versions so swap_definition adopts the live configuration.
if(TARGET Void::Fsm)
add_library(VoidFsmPluginV1 SHARED
"${CMAKE_CURRENT_SOURCE_DIR}/plugins/FsmPluginV1.cpp")
add_library(VoidFsmPluginV2 SHARED
"${CMAKE_CURRENT_SOURCE_DIR}/plugins/FsmPluginV2.cpp")
foreach(_fp VoidFsmPluginV1 VoidFsmPluginV2)
target_link_libraries(${_fp} PRIVATE Void::Plugin Void::Fsm)
endforeach()
target_compile_definitions(VoidTests PRIVATE
"VOID_FSM_PLUGIN_V1_PATH=\"$<TARGET_FILE:VoidFsmPluginV1>\""
"VOID_FSM_PLUGIN_V2_PATH=\"$<TARGET_FILE:VoidFsmPluginV2>\""
)
add_dependencies(VoidTests VoidFsmPluginV1 VoidFsmPluginV2)
endif()
endif()
-158
View File
@@ -1,158 +0,0 @@
#include <Void/Fsm/Machine.hpp>
#include <Void/Fsm/PluginBehavior.hpp>
#include <Void/Plugin/Manager.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <atomic>
#include <chrono>
#include <filesystem>
#include <memory>
#include <thread>
#include <vector>
#if defined(VOID_FSM_PLUGIN_V1_PATH) && defined(VOID_FSM_PLUGIN_V2_PATH) && \
__has_include(<dlfcn.h>)
using namespace Void::Fsm;
using Void::Plugin::PluginManager;
namespace
{
void install(const std::filesystem::path& src,
const std::filesystem::path& dst)
{
auto staging = dst;
staging += ".incoming";
std::filesystem::copy_file(
src, staging, std::filesystem::copy_options::overwrite_existing);
std::filesystem::rename(staging, dst);
}
// Build a machine definition from the plugin's behaviour pack.
std::shared_ptr<const MachineDef> def_from(const PluginManager& mgr)
{
auto* sym = reinterpret_cast<fsm_behaviors_fn>(
mgr.raw_symbol("FsmPlugin", kFsmBehaviorsSymbol));
Void::Testing::expect(sym != nullptr);
if (sym == nullptr) {
return nullptr;
}
auto built = build_machine_def(*sym());
Void::Testing::expect(built.has_value());
return built.value_or(nullptr);
}
} // namespace
// A machine whose states and actions come entirely from a plugin keeps
// running across a plugin reload: the new definition is built from the
// reloaded behaviour pack and swapped in between run-to-completion steps.
Void::Testing::Test internal_fsm_plugin_definition_swap()
{
Void::FileSystem::TempDir tmp{"void_fsmplugin"};
const auto so = tmp.path() / "fsm.so";
std::filesystem::copy_file(VOID_FSM_PLUGIN_V1_PATH, so);
PluginManager mgr;
Void::Testing::expect(mgr.load(so).has_value());
auto def = def_from(mgr);
Void::Testing::expect(def != nullptr);
Machine machine{def, MachineOptions{.own_worker = false}};
machine.start();
machine.post(Event{*def->event_id("start"), {}});
machine.run_pending();
Void::Testing::expect(machine.is_in("busy"));
// Reload to v2 (adds a "skip" transition; same state names) and swap
// the definition in. The machine keeps its active "busy" leaf.
install(VOID_FSM_PLUGIN_V2_PATH, so);
Void::Testing::expect(mgr.reload("FsmPlugin").has_value());
auto def2 = def_from(mgr);
Void::Testing::expect(def2 != nullptr);
machine.swap_definition(def2);
machine.run_pending();
Void::Testing::expect(machine.is_in("busy")); // configuration survived
// Finish the v1 cycle, then exercise the v2-only "skip" transition.
machine.post(Event{*def2->event_id("finish"), {}});
machine.run_pending();
Void::Testing::expect(machine.is_in("done"));
machine.post(Event{*def2->event_id("reset"), {}});
machine.run_pending();
Void::Testing::expect(machine.is_in("idle"));
machine.post(Event{*def2->event_id("skip"), {}}); // new in v2
machine.run_pending();
Void::Testing::expect(machine.is_in("done"));
}
// @tag stress, concurrency
Void::Testing::Test internal_fsm_plugin_hot_reload_under_load()
{
Void::FileSystem::TempDir tmp{"void_fsmplugin"};
const auto so = tmp.path() / "fsm.so";
std::filesystem::copy_file(VOID_FSM_PLUGIN_V1_PATH, so);
PluginManager mgr;
Void::Testing::expect(mgr.load(so).has_value());
auto def = def_from(mgr);
// Worker-driven machine; the plugin's .so stays mapped (the manager's
// dlclose_on_swap=false) so its action code remains valid for frames
// in flight. Pin a handle for the machine's lifetime as belt-and-
// suspenders against the .so unmapping.
auto pin = mgr.acquire("FsmPlugin");
Void::Testing::expect(static_cast<bool>(pin));
Machine machine{def};
machine.start();
const auto start = *def->event_id("start");
const auto finish = *def->event_id("finish");
const auto reset = *def->event_id("reset");
std::atomic<bool> stop{false};
std::atomic<long long> posts{0};
std::vector<std::thread> producers;
producers.reserve(4);
for (int p = 0; p < 4; ++p) {
producers.emplace_back([&] {
while (!stop.load(std::memory_order_relaxed)) {
machine.post(Event{start, {}});
machine.post(Event{finish, {}});
machine.post(Event{reset, {}});
posts.fetch_add(3, std::memory_order_relaxed);
std::this_thread::yield();
}
});
}
// Rebuild + swap the definition repeatedly while events flow.
for (int i = 0; i < 5; ++i) {
const char* src =
(i % 2 == 0) ? VOID_FSM_PLUGIN_V2_PATH : VOID_FSM_PLUGIN_V1_PATH;
install(src, so);
Void::Testing::expect(mgr.reload("FsmPlugin").has_value());
machine.swap_definition(def_from(mgr));
std::this_thread::sleep_for(std::chrono::milliseconds{3});
}
stop.store(true);
for (auto& t : producers) {
t.join();
}
// Drain and confirm the machine is still coherent: exactly one of the
// three leaves is active.
(void)machine.send_wait(Event{reset, {}});
const int active = static_cast<int>(machine.is_in("idle")) +
static_cast<int>(machine.is_in("busy")) +
static_cast<int>(machine.is_in("done"));
Void::Testing::expect_eq(active, 1);
Void::Testing::expect(posts.load() > 0);
machine.stop();
}
#endif // FSM plugins available
-70
View File
@@ -1,70 +0,0 @@
#include <Void/Fsm/Definition.hpp>
#include <Void/Fsm/PluginBehavior.hpp>
#include <Void/Plugin/Plugin.hpp>
#include <atomic>
#include <string_view>
// FSM behaviour plugin, v1. Defines a 3-state cycle (idle -> busy -> done
// -> idle) and counts how many times its action ran. v2 keeps the same
// state/event names so the host can swap_definition seamlessly.
namespace
{
std::atomic<int> g_action_runs{0};
class FsmPlugin final : public Void::Plugin::IPlugin,
public Void::Fsm::IFsmBehaviorPack
{
public:
[[nodiscard]] std::string_view name() const noexcept override
{
return "FsmPlugin";
}
void register_into(Void::Fsm::Builder& b) override
{
using namespace Void::Fsm;
auto idle = b.state("idle");
auto busy = b.state("busy");
auto done = b.state("done");
b.initial(idle);
const auto start = b.event("start");
const auto finish = b.event("finish");
const auto reset = b.event("reset");
b.transition(idle, start, busy)
.action(SyncAction{[](Machine&, const Event&) {
g_action_runs.fetch_add(1, std::memory_order_relaxed);
}});
b.transition(busy, finish, done);
b.transition(done, reset, idle);
}
};
} // namespace
extern "C" {
void* void_plugin_create() { return new FsmPlugin; }
void void_plugin_destroy(void* p) { delete static_cast<FsmPlugin*>(p); }
Void::Plugin::PluginInfo void_plugin_info()
{
return {
.abi_version = Void::Plugin::ABI_VERSION,
.name = "FsmPlugin",
.plugin_version = "1.0.0",
.description = "FSM behaviour plugin (v1)",
};
}
// Stateless factory: returns a fresh pack the host uses only during
// definition build. It does not need to be the IPlugin instance.
Void::Fsm::IFsmBehaviorPack* void_plugin_fsm_behaviors()
{
static FsmPlugin pack;
return &pack;
}
} // extern "C"
-71
View File
@@ -1,71 +0,0 @@
#include <Void/Fsm/Definition.hpp>
#include <Void/Fsm/PluginBehavior.hpp>
#include <Void/Plugin/Plugin.hpp>
#include <atomic>
#include <string_view>
// FSM behaviour plugin, v2. Same state/event names as v1 (so the host
// adopts the active configuration seamlessly on swap_definition) plus a
// new "skip" transition idle -> done that proves the reloaded behaviour
// is live.
namespace
{
std::atomic<int> g_action_runs{0};
class FsmPlugin final : public Void::Plugin::IPlugin,
public Void::Fsm::IFsmBehaviorPack
{
public:
[[nodiscard]] std::string_view name() const noexcept override
{
return "FsmPlugin";
}
void register_into(Void::Fsm::Builder& b) override
{
using namespace Void::Fsm;
auto idle = b.state("idle");
auto busy = b.state("busy");
auto done = b.state("done");
b.initial(idle);
const auto start = b.event("start");
const auto finish = b.event("finish");
const auto reset = b.event("reset");
const auto skip = b.event("skip"); // new in v2
b.transition(idle, start, busy)
.action(SyncAction{[](Machine&, const Event&) {
g_action_runs.fetch_add(1, std::memory_order_relaxed);
}});
b.transition(busy, finish, done);
b.transition(done, reset, idle);
b.transition(idle, skip, done); // new behaviour
}
};
} // namespace
extern "C" {
void* void_plugin_create() { return new FsmPlugin; }
void void_plugin_destroy(void* p) { delete static_cast<FsmPlugin*>(p); }
Void::Plugin::PluginInfo void_plugin_info()
{
return {
.abi_version = Void::Plugin::ABI_VERSION,
.name = "FsmPlugin",
.plugin_version = "2.0.0",
.description = "FSM behaviour plugin (v2)",
};
}
Void::Fsm::IFsmBehaviorPack* void_plugin_fsm_behaviors()
{
static FsmPlugin pack;
return &pack;
}
} // extern "C"
-247
View File
@@ -1,247 +0,0 @@
#include <Void/Fsm/Definition.hpp>
#include <Void/Fsm/Machine.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <vector>
using namespace Void::Fsm;
namespace
{
// Embedder-driven machine (no worker thread): post + run_pending make
// the tests fully deterministic.
struct Bench {
std::shared_ptr<const MachineDef> def;
std::unique_ptr<Machine> machine;
std::vector<std::string> log;
explicit Bench(Builder& b)
{
auto built = b.build();
Void::Testing::expect(built.has_value());
def = *built;
machine = std::make_unique<Machine>(
def, MachineOptions{.own_worker = false});
}
SendResult step(EventId ev)
{
machine->post(Event{ev, {}});
machine->run_pending();
return SendResult::Handled; // result checked via send paths
}
};
} // namespace
Void::Testing::Test basic_initial_state_entered_on_start()
{
Builder b;
auto idle = b.state("idle");
auto run = b.state("running");
b.initial(idle);
(void)run;
Bench bench{b};
bench.machine->start();
Void::Testing::expect(bench.machine->is_in("idle"));
Void::Testing::expect(!bench.machine->is_in("running"));
Void::Testing::expect(bench.machine->running());
}
Void::Testing::Test basic_transition_changes_state()
{
Builder b;
auto idle = b.state("idle");
auto run = b.state("running");
b.initial(idle);
const auto go = b.event("go");
b.transition(idle, go, run);
Bench bench{b};
bench.machine->start();
bench.machine->post(Event{go, {}});
bench.machine->run_pending();
Void::Testing::expect(bench.machine->is_in("running"));
Void::Testing::expect(!bench.machine->is_in("idle"));
}
Void::Testing::Test basic_entry_exit_and_action_ordering()
{
std::vector<std::string> log;
Builder b;
auto a = b.state("a");
auto c = b.state("c");
b.initial(a);
const auto go = b.event("go");
b.on_entry(a, SyncAction{[&](Machine&, const Event&) {
log.push_back("enter a");
}});
b.on_exit(a, SyncAction{[&](Machine&, const Event&) {
log.push_back("exit a");
}});
b.on_entry(c, SyncAction{[&](Machine&, const Event&) {
log.push_back("enter c");
}});
b.transition(a, go, c).action(SyncAction{
[&](Machine&, const Event&) { log.push_back("action"); }});
Bench bench{b};
bench.machine->start();
bench.machine->post(Event{go, {}});
bench.machine->run_pending();
const std::vector<std::string> expected{"enter a", "exit a", "action",
"enter c"};
Void::Testing::expect(log == expected);
}
Void::Testing::Test basic_guard_blocks_and_allows()
{
Builder b;
auto a = b.state("a");
auto c = b.state("c");
b.initial(a);
const auto go = b.event("go");
bool open = false;
b.transition(a, go, c).guard(
[&](const Machine&, const Event&) { return open; });
Bench bench{b};
bench.machine->start();
bench.machine->post(Event{go, {}});
bench.machine->run_pending();
Void::Testing::expect(bench.machine->is_in("a"));
open = true;
bench.machine->post(Event{go, {}});
bench.machine->run_pending();
Void::Testing::expect(bench.machine->is_in("c"));
}
Void::Testing::Test basic_internal_transition_no_exit_entry()
{
std::vector<std::string> log;
Builder b;
auto a = b.state("a");
b.initial(a);
const auto tick = b.event("tick");
b.on_entry(a, SyncAction{[&](Machine&, const Event&) {
log.push_back("enter");
}});
b.on_exit(a, SyncAction{[&](Machine&, const Event&) {
log.push_back("exit");
}});
b.internal(a, tick).action(SyncAction{
[&](Machine&, const Event&) { log.push_back("internal"); }});
Bench bench{b};
bench.machine->start();
bench.machine->post(Event{tick, {}});
bench.machine->run_pending();
const std::vector<std::string> expected{"enter", "internal"};
Void::Testing::expect(log == expected);
Void::Testing::expect(bench.machine->is_in("a"));
}
Void::Testing::Test basic_self_transition_exits_and_reenters()
{
std::vector<std::string> log;
Builder b;
auto a = b.state("a");
b.initial(a);
const auto reset = b.event("reset");
b.on_entry(a, SyncAction{[&](Machine&, const Event&) {
log.push_back("enter");
}});
b.on_exit(a, SyncAction{[&](Machine&, const Event&) {
log.push_back("exit");
}});
b.transition(a, reset, a);
Bench bench{b};
bench.machine->start();
bench.machine->post(Event{reset, {}});
bench.machine->run_pending();
const std::vector<std::string> expected{"enter", "exit", "enter"};
Void::Testing::expect(log == expected);
}
Void::Testing::Test basic_payload_reaches_action()
{
int received = 0;
Builder b;
auto a = b.state("a");
auto c = b.state("c");
b.initial(a);
const auto go = b.event("go");
b.transition(a, go, c).action(
SyncAction{[&](Machine&, const Event& e) {
received = std::any_cast<int>(e.payload);
}});
Bench bench{b};
bench.machine->start();
bench.machine->post(Event{go, 42});
bench.machine->run_pending();
Void::Testing::expect_eq(received, 42);
}
Void::Testing::Test basic_unhandled_event_keeps_state()
{
Builder b;
auto a = b.state("a");
b.initial(a);
const auto noop = b.event("noop");
(void)noop;
Bench bench{b};
bench.machine->start();
bench.machine->post(Event{noop, {}});
bench.machine->run_pending();
Void::Testing::expect(bench.machine->is_in("a"));
}
Void::Testing::Test basic_post_rejected_before_start_and_after_stop()
{
Builder b;
auto a = b.state("a");
b.initial(a);
const auto go = b.event("go");
Bench bench{b};
Void::Testing::expect(!bench.machine->post(Event{go, {}}));
bench.machine->start();
Void::Testing::expect(bench.machine->post(Event{go, {}}));
bench.machine->run_pending();
bench.machine->stop();
Void::Testing::expect(!bench.machine->post(Event{go, {}}));
Void::Testing::expect(!bench.machine->running());
}
Void::Testing::Test builder_rejects_composite_without_initial()
{
Builder b;
auto parent = b.state("parent");
(void)b.state("c1", parent);
(void)b.state("c2", parent);
b.initial(parent); // parent initial of root, but no initial child
auto built = b.build();
Void::Testing::expect(!built.has_value());
}
Void::Testing::Test builder_defaults_first_top_level_as_initial()
{
Builder b;
(void)b.state("first");
(void)b.state("second");
Bench bench{b};
bench.machine->start();
Void::Testing::expect(bench.machine->is_in("first"));
}
-221
View File
@@ -1,221 +0,0 @@
#include <Void/Fsm/Definition.hpp>
#include <Void/Fsm/Machine.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <atomic>
#include <thread>
#include <vector>
using namespace Void::Fsm;
#ifdef VOID_HAS_ENUM_REFLECTION
namespace
{
enum class Phase { Ping, Pong };
enum class Signal { Flip };
} // namespace
Void::Testing::Test enum_reflection_builds_states_and_events()
{
Builder b;
b.states_from_enum<Phase>();
b.initial(b.state_of(Phase::Ping));
const auto flip = b.event(Signal::Flip);
b.transition(b.state_of(Phase::Ping), flip, b.state_of(Phase::Pong));
b.transition(b.state_of(Phase::Pong), flip, b.state_of(Phase::Ping));
auto def = b.build();
Void::Testing::expect(def.has_value());
Void::Testing::expect((*def)->state_id("Ping").has_value());
Void::Testing::expect((*def)->event_id("Flip").has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
Void::Testing::expect(m.is_in("Ping"));
Void::Testing::expect(m.post(Signal::Flip));
m.run_pending();
Void::Testing::expect(m.is_in("Pong"));
// Serialized state uses enumerator names — stable across reloads.
const auto json = m.serialize_state();
Void::Testing::expect_eq(json["leaves"][0].as_string(),
std::string{"Pong"});
}
#endif // VOID_HAS_ENUM_REFLECTION
// @tag stress, concurrency
Void::Testing::Test concurrent_posts_all_processed_in_producer_order()
{
constexpr int producers = 8;
constexpr int per_producer = 10'000;
// counter state with an internal self-loop that consumes "bump"
// events carrying {producer, seq} payloads.
struct Payload {
int producer;
int seq;
};
std::vector<int> next_seq(producers, 0);
std::atomic<long long> handled{0};
bool order_ok = true;
Builder b;
auto counting = b.state("counting");
b.initial(counting);
const auto bump = b.event("bump");
b.internal(counting, bump)
.action(SyncAction{[&](Machine&, const Event& e) {
const auto p = std::any_cast<Payload>(e.payload);
// Per-producer FIFO must hold (single consumer).
if (next_seq[static_cast<std::size_t>(p.producer)] != p.seq) {
order_ok = false;
}
++next_seq[static_cast<std::size_t>(p.producer)];
++handled;
}});
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def}; // own worker
m.start();
std::atomic<bool> go{false};
std::vector<std::thread> threads;
threads.reserve(producers);
for (int p = 0; p < producers; ++p) {
threads.emplace_back([&m, &go, bump, p] {
go.wait(false);
for (int i = 0; i < per_producer; ++i) {
while (!m.post(Event{bump, Payload{p, i}})) {
std::this_thread::yield();
}
}
});
}
go = true;
go.notify_all();
for (auto& t : threads) {
t.join();
}
// Everything posted before this send_wait is processed before it.
(void)m.send_wait(Event{bump, Payload{0, per_producer}});
++next_seq[0]; // account for the flush event
m.stop();
Void::Testing::expect_eq(
handled.load(),
static_cast<long long>(producers) * per_producer + 1);
Void::Testing::expect(order_ok);
}
// @tag stress, concurrency
Void::Testing::Test snapshot_reads_never_tear_under_load()
{
Builder b;
auto ping = b.state("ping");
auto pong = b.state("pong");
b.initial(ping);
const auto flip = b.event("flip");
b.transition(ping, flip, pong);
b.transition(pong, flip, ping);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def};
m.start();
std::atomic<bool> stop{false};
std::atomic<long long> reads{0};
bool sane = true;
std::vector<std::thread> readers;
readers.reserve(4);
for (int r = 0; r < 4; ++r) {
readers.emplace_back([&] {
while (!stop.load(std::memory_order_relaxed)) {
const auto snap = m.current();
// Exactly one leaf, and it is one of the two states.
if (snap.leaf_count != 1) {
sane = false;
}
++reads;
}
});
}
for (int i = 0; i < 20'000; ++i) {
m.post(Event{flip, {}});
}
(void)m.send_wait(Event{flip, {}});
stop = true;
for (auto& t : readers) {
t.join();
}
m.stop();
Void::Testing::expect(sane);
Void::Testing::expect(reads.load() > 0);
}
// @tag stress, concurrency
Void::Testing::Test hot_swap_under_sustained_load()
{
auto make = [](bool variant) {
Builder b;
auto ping = b.state("ping");
auto pong = b.state("pong");
b.initial(ping);
const auto flip = b.event("flip");
b.transition(ping, flip, pong);
b.transition(pong, flip, ping);
if (variant) {
// identical topology, extra no-op event
(void)b.event("extra");
}
auto def = b.build();
Void::Testing::expect(def.has_value());
return *def;
};
Machine m{make(false)};
m.start();
std::atomic<bool> stop{false};
std::vector<std::thread> producers;
producers.reserve(4);
const auto flip = *m.definition()->event_id("flip");
for (int p = 0; p < 4; ++p) {
producers.emplace_back([&m, &stop, flip] {
while (!stop.load(std::memory_order_relaxed)) {
(void)m.post(Event{flip, {}});
// Pace producers so the backlog stays bounded — the test
// exercises swap-under-load, not queue growth.
std::this_thread::yield();
}
});
}
for (int i = 0; i < 50; ++i) {
m.swap_definition(make(i % 2 == 1));
std::this_thread::sleep_for(std::chrono::microseconds{200});
}
stop = true;
for (auto& t : producers) {
t.join();
}
(void)m.send_wait(Event{flip, {}});
// Machine is still coherent: exactly one of the two states active.
const bool in_ping = m.is_in("ping");
const bool in_pong = m.is_in("pong");
Void::Testing::expect(in_ping != in_pong);
m.stop();
}
-336
View File
@@ -1,336 +0,0 @@
#include <Void/Fsm/Definition.hpp>
#include <Void/Fsm/Machine.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <string>
#include <thread>
#include <vector>
using namespace Void::Fsm;
// ── deferred events ───────────────────────────────────────────────────────────
Void::Testing::Test defer_stashes_until_state_change()
{
int handled = 0;
Builder b;
auto busy = b.state("busy");
auto idle = b.state("idle");
b.initial(busy);
const auto job = b.event("job");
const auto done = b.event("done");
b.defer(busy, job);
b.transition(busy, done, idle);
b.internal(idle, job).action(
SyncAction{[&](Machine&, const Event&) { ++handled; }});
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{job, {}}); // deferred in busy
m.post(Event{job, {}}); // deferred in busy
m.run_pending();
Void::Testing::expect_eq(handled, 0);
m.post(Event{done, {}}); // busy -> idle, deferred jobs re-inject
m.run_pending();
Void::Testing::expect_eq(handled, 2);
}
Void::Testing::Test defer_keeps_fifo_order()
{
std::vector<int> seen;
Builder b;
auto busy = b.state("busy");
auto idle = b.state("idle");
b.initial(busy);
const auto job = b.event("job");
const auto done = b.event("done");
b.defer(busy, job);
b.transition(busy, done, idle);
b.internal(idle, job).action(
SyncAction{[&](Machine&, const Event& e) {
seen.push_back(std::any_cast<int>(e.payload));
}});
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{job, 1});
m.post(Event{job, 2});
m.post(Event{job, 3});
m.post(Event{done, {}});
m.run_pending();
const std::vector<int> expected{1, 2, 3};
Void::Testing::expect(seen == expected);
}
// ── orthogonal regions ────────────────────────────────────────────────────────
Void::Testing::Test parallel_enters_all_regions()
{
Builder b;
auto sys = b.state("sys");
auto net = b.state("net", sys);
auto net_d = b.state("net_down", net);
auto net_u = b.state("net_up", net);
auto disk = b.state("disk", sys);
auto d_ok = b.state("disk_ok", disk);
auto d_bad = b.state("disk_bad", disk);
b.initial(sys);
b.parallel(sys);
b.initial(net_d);
b.initial(d_ok);
(void)net_u;
(void)d_bad;
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
Void::Testing::expect(m.is_in("net_down"));
Void::Testing::expect(m.is_in("disk_ok"));
Void::Testing::expect_eq(m.current().leaf_count, std::uint32_t{2});
}
Void::Testing::Test parallel_regions_react_independently()
{
Builder b;
auto sys = b.state("sys");
auto net = b.state("net", sys);
auto net_d = b.state("net_down", net);
auto net_u = b.state("net_up", net);
auto disk = b.state("disk", sys);
auto d_ok = b.state("disk_ok", disk);
auto d_bad = b.state("disk_bad", disk);
b.initial(sys);
b.parallel(sys);
b.initial(net_d);
b.initial(d_ok);
const auto net_event = b.event("net_restored");
const auto disk_event = b.event("disk_failed");
b.transition(net_d, net_event, net_u);
b.transition(d_ok, disk_event, d_bad);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{net_event, {}});
m.run_pending();
Void::Testing::expect(m.is_in("net_up"));
Void::Testing::expect(m.is_in("disk_ok")); // untouched
m.post(Event{disk_event, {}});
m.run_pending();
Void::Testing::expect(m.is_in("net_up"));
Void::Testing::expect(m.is_in("disk_bad"));
}
Void::Testing::Test parallel_one_event_can_fire_in_both_regions()
{
Builder b;
auto sys = b.state("sys");
auto r1 = b.state("r1", sys);
auto a1 = b.state("a1", r1);
auto b1 = b.state("b1", r1);
auto r2 = b.state("r2", sys);
auto a2 = b.state("a2", r2);
auto b2 = b.state("b2", r2);
b.initial(sys);
b.parallel(sys);
b.initial(a1);
b.initial(a2);
const auto flip = b.event("flip");
b.transition(a1, flip, b1);
b.transition(a2, flip, b2);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{flip, {}});
m.run_pending();
Void::Testing::expect(m.is_in("b1"));
Void::Testing::expect(m.is_in("b2"));
}
// ── timers ────────────────────────────────────────────────────────────────────
Void::Testing::Test timeout_fires_after_duration()
{
Builder b;
auto waiting = b.state("waiting");
auto expired = b.state("expired");
b.initial(waiting);
const auto timeout_ev = b.event("timed_out");
b.timeout(waiting, std::chrono::milliseconds{20}, timeout_ev);
b.transition(waiting, timeout_ev, expired);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.run_pending();
Void::Testing::expect(m.is_in("waiting")); // not yet
std::this_thread::sleep_for(std::chrono::milliseconds{30});
m.run_pending();
Void::Testing::expect(m.is_in("expired"));
}
Void::Testing::Test timeout_cancelled_by_leaving_state()
{
Builder b;
auto waiting = b.state("waiting");
auto active = b.state("active");
auto expired = b.state("expired");
b.initial(waiting);
const auto timeout_ev = b.event("timed_out");
const auto go = b.event("go");
b.timeout(waiting, std::chrono::milliseconds{20}, timeout_ev);
b.transition(waiting, timeout_ev, expired);
b.transition(waiting, go, active);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{go, {}});
m.run_pending();
std::this_thread::sleep_for(std::chrono::milliseconds{30});
m.run_pending();
Void::Testing::expect(m.is_in("active")); // timer was disarmed
}
// @tag integration
Void::Testing::Test timeout_with_own_worker_thread()
{
Builder b;
auto waiting = b.state("waiting");
auto expired = b.state("expired");
b.initial(waiting);
const auto timeout_ev = b.event("timed_out");
b.timeout(waiting, std::chrono::milliseconds{15}, timeout_ev);
b.transition(waiting, timeout_ev, expired);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def}; // own worker
m.start();
for (int i = 0; i < 200 && !m.is_in("expired"); ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds{5});
}
Void::Testing::expect(m.is_in("expired"));
m.stop();
}
// ── coroutine actions / awaitable send ────────────────────────────────────────
Void::Testing::Test coro_action_completes_within_step()
{
std::vector<std::string> log;
Builder b;
auto a = b.state("a");
auto c = b.state("c");
b.initial(a);
const auto go = b.event("go");
b.transition(a, go, c).action(
Action{[&](Machine&, const Event&) -> Void::Coro::Task<void> {
log.push_back("action begin");
auto inner = []() -> Void::Coro::Task<int> { co_return 7; };
const int v = co_await inner();
log.push_back("inner " + std::to_string(v));
log.push_back("action end");
}});
b.on_entry(c, SyncAction{[&](Machine&, const Event&) {
log.push_back("enter c");
}});
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{go, {}});
m.run_pending();
const std::vector<std::string> expected{
"action begin", "inner 7", "action end", "enter c"};
Void::Testing::expect(log == expected);
}
// @tag integration
Void::Testing::Test send_wait_returns_step_result()
{
Builder b;
auto a = b.state("a");
auto c = b.state("c");
b.initial(a);
const auto go = b.event("go");
const auto noop = b.event("noop");
b.transition(a, go, c);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def}; // worker drives; send_wait blocks the caller
m.start();
Void::Testing::expect(m.send_wait(Event{go, {}}) ==
SendResult::Handled);
Void::Testing::expect(m.is_in("c"));
Void::Testing::expect(m.send_wait(Event{noop, {}}) ==
SendResult::Unhandled);
m.stop();
Void::Testing::expect(m.send_wait(Event{go, {}}) ==
SendResult::Dropped);
}
// @tag integration
Void::Testing::Test co_await_send_resolves_with_result()
{
Builder b;
auto a = b.state("a");
auto c = b.state("c");
b.initial(a);
const auto go = b.event("go");
b.transition(a, go, c);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def};
m.start();
std::optional<SendResult> result;
Void::Async::ManualExecutor ex;
Void::Async::spawn(
ex,
[](Machine& machine, Void::Async::Executor& resume_on,
std::optional<SendResult>& out) -> Void::Coro::Task<void> {
out = co_await machine.send(
Event{*machine.definition()->event_id("go"), {}},
&resume_on);
}(m, ex, result));
// Drain until the awaitable resumes us back on `ex` with the result.
for (int i = 0; i < 500 && !result.has_value(); ++i) {
if (ex.drain() == 0) {
std::this_thread::sleep_for(std::chrono::milliseconds{1});
}
}
Void::Testing::expect(result.has_value());
Void::Testing::expect(*result == SendResult::Handled);
Void::Testing::expect(m.is_in("c"));
m.stop();
}
-239
View File
@@ -1,239 +0,0 @@
#include <Void/Fsm/Definition.hpp>
#include <Void/Fsm/Machine.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <memory>
#include <string>
#include <vector>
using namespace Void::Fsm;
namespace
{
struct Trace {
std::vector<std::string> log;
SyncAction enter(std::string name)
{
return [this, name](Machine&, const Event&) {
log.push_back("+" + name);
};
}
SyncAction exit(std::string name)
{
return [this, name](Machine&, const Event&) {
log.push_back("-" + name);
};
}
};
} // namespace
Void::Testing::Test hierarchy_descends_into_nested_initial()
{
Trace t;
Builder b;
auto outer = b.state("outer");
auto inner = b.state("inner", outer);
auto deep = b.state("deep", inner);
b.initial(outer);
b.initial(inner);
b.initial(deep);
b.on_entry(outer, t.enter("outer"));
b.on_entry(inner, t.enter("inner"));
b.on_entry(deep, t.enter("deep"));
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
const std::vector<std::string> expected{"+outer", "+inner", "+deep"};
Void::Testing::expect(t.log == expected);
Void::Testing::expect(m.is_in("outer"));
Void::Testing::expect(m.is_in("inner"));
Void::Testing::expect(m.is_in("deep"));
}
Void::Testing::Test hierarchy_lca_exit_entry_order()
{
Trace t;
Builder b;
auto top = b.state("top");
auto a = b.state("a", top);
auto a1 = b.state("a1", a);
auto c = b.state("c", top);
auto c1 = b.state("c1", c);
b.initial(top);
b.initial(a);
b.initial(a1);
b.initial(c1);
const auto hop = b.event("hop");
b.on_exit(a1, t.exit("a1"));
b.on_exit(a, t.exit("a"));
b.on_exit(top, t.exit("top"));
b.on_entry(c, t.enter("c"));
b.on_entry(c1, t.enter("c1"));
b.transition(a1, hop, c1);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
t.log.clear();
m.post(Event{hop, {}});
m.run_pending();
// LCA(a1, c1) = top: exit a1, a (innermost first; top stays),
// then enter c, c1 (outermost first).
const std::vector<std::string> expected{"-a1", "-a", "+c", "+c1"};
Void::Testing::expect(t.log == expected);
Void::Testing::expect(m.is_in("top"));
Void::Testing::expect(m.is_in("c1"));
}
Void::Testing::Test hierarchy_inner_transition_wins_over_outer()
{
Builder b;
auto outer = b.state("outer");
auto in1 = b.state("in1", outer);
auto in2 = b.state("in2", outer);
auto away = b.state("away");
b.initial(outer);
b.initial(in1);
const auto go = b.event("go");
b.transition(outer, go, away); // outer handler
b.transition(in1, go, in2); // inner handler must win
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{go, {}});
m.run_pending();
Void::Testing::expect(m.is_in("in2"));
Void::Testing::expect(!m.is_in("away"));
}
Void::Testing::Test hierarchy_outer_handles_when_inner_does_not()
{
Builder b;
auto outer = b.state("outer");
auto in1 = b.state("in1", outer);
auto away = b.state("away");
b.initial(outer);
b.initial(in1);
const auto leave = b.event("leave");
b.transition(outer, leave, away);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{leave, {}});
m.run_pending();
Void::Testing::expect(m.is_in("away"));
Void::Testing::expect(!m.is_in("outer"));
}
Void::Testing::Test hierarchy_shallow_history_restores_child()
{
Builder b;
auto work = b.state("work");
auto w1 = b.state("w1", work);
auto w2 = b.state("w2", work);
auto pause = b.state("pause");
b.initial(work);
b.initial(w1);
b.history(work, History::Shallow);
const auto next = b.event("next");
const auto stop_ = b.event("stop");
const auto resume = b.event("resume");
b.transition(w1, next, w2);
b.transition(work, stop_, pause);
b.transition(pause, resume, work);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{next, {}}); // w1 -> w2
m.post(Event{stop_, {}}); // -> pause (records w2)
m.run_pending();
Void::Testing::expect(m.is_in("pause"));
m.post(Event{resume, {}}); // -> work, history -> w2
m.run_pending();
Void::Testing::expect(m.is_in("w2"));
Void::Testing::expect(!m.is_in("w1"));
}
Void::Testing::Test hierarchy_deep_history_restores_leaves()
{
Builder b;
auto work = b.state("work");
auto sub = b.state("sub", work);
auto s1 = b.state("s1", sub);
auto s2 = b.state("s2", sub);
auto pause = b.state("pause");
b.initial(work);
b.initial(sub);
b.initial(s1);
b.history(work, History::Deep);
const auto next = b.event("next");
const auto stop_ = b.event("stop");
const auto resume = b.event("resume");
b.transition(s1, next, s2);
b.transition(work, stop_, pause);
b.transition(pause, resume, work);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{next, {}});
m.post(Event{stop_, {}});
m.run_pending();
Void::Testing::expect(m.is_in("pause"));
m.post(Event{resume, {}});
m.run_pending();
// Deep history re-activates the full chain down to s2 (shallow would
// have restored sub and then default-descended back into s1).
Void::Testing::expect(m.is_in("s2"));
Void::Testing::expect(!m.is_in("s1"));
}
Void::Testing::Test hierarchy_no_history_reenters_initial()
{
Builder b;
auto work = b.state("work");
auto w1 = b.state("w1", work);
auto w2 = b.state("w2", work);
auto pause = b.state("pause");
b.initial(work);
b.initial(w1);
const auto next = b.event("next");
const auto stop_ = b.event("stop");
const auto resume = b.event("resume");
b.transition(w1, next, w2);
b.transition(work, stop_, pause);
b.transition(pause, resume, work);
auto def = b.build();
Void::Testing::expect(def.has_value());
Machine m{*def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{next, {}});
m.post(Event{stop_, {}});
m.post(Event{resume, {}});
m.run_pending();
Void::Testing::expect(m.is_in("w1")); // back to initial, not w2
}
-186
View File
@@ -1,186 +0,0 @@
#include <Void/Fsm/Definition.hpp>
#include <Void/Fsm/Machine.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <memory>
#include <string>
using namespace Void::Fsm;
namespace
{
// work{ sub{ s1, s2 } }, pause — with deep history on work.
std::shared_ptr<const MachineDef> make_def()
{
Builder b;
auto work = b.state("work");
auto sub = b.state("sub", work);
auto s1 = b.state("s1", sub);
auto s2 = b.state("s2", sub);
auto pause = b.state("pause");
b.initial(work);
b.initial(sub);
b.initial(s1);
b.history(work, History::Deep);
b.transition(s1, b.event("next"), s2);
b.transition(work, b.event("stop"), pause);
b.transition(pause, b.event("resume"), work);
auto def = b.build();
Void::Testing::expect(def.has_value());
return *def;
}
} // namespace
Void::Testing::Test serialize_captures_leaves_by_name()
{
auto def = make_def();
Machine m{def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{*def->event_id("next"), {}});
m.run_pending();
const auto json = m.serialize_state();
Void::Testing::expect(json.is_object());
const auto& leaves = json["leaves"].as_array();
Void::Testing::expect_eq(leaves.size(), std::size_t{1});
Void::Testing::expect_eq(leaves[0].as_string(), std::string{"s2"});
}
Void::Testing::Test serialize_restore_round_trip()
{
auto def = make_def();
Void::Json::Value saved;
{
Machine m{def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{*def->event_id("next"), {}}); // s1 -> s2
m.run_pending();
saved = m.serialize_state();
m.stop();
}
Machine restored{def, MachineOptions{.own_worker = false}};
Void::Testing::expect(restored.restore_state(saved).has_value());
restored.start();
Void::Testing::expect(restored.is_in("s2"));
Void::Testing::expect(restored.is_in("work"));
Void::Testing::expect(!restored.is_in("s1"));
// The restored machine behaves identically: stop/resume goes back
// to s2 via deep history captured in the serialized state.
restored.post(Event{*def->event_id("stop"), {}});
restored.post(Event{*def->event_id("resume"), {}});
restored.run_pending();
Void::Testing::expect(restored.is_in("s2"));
}
Void::Testing::Test serialize_preserves_history_records()
{
auto def = make_def();
Void::Json::Value saved;
{
Machine m{def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{*def->event_id("next"), {}}); // -> s2
m.post(Event{*def->event_id("stop"), {}}); // -> pause, hist {s2}
m.run_pending();
saved = m.serialize_state();
m.stop();
}
Machine restored{def, MachineOptions{.own_worker = false}};
Void::Testing::expect(restored.restore_state(saved).has_value());
restored.start();
Void::Testing::expect(restored.is_in("pause"));
restored.post(Event{*def->event_id("resume"), {}});
restored.run_pending();
Void::Testing::expect(restored.is_in("s2")); // history survived
}
Void::Testing::Test restore_rejects_running_machine_and_garbage()
{
auto def = make_def();
Machine m{def, MachineOptions{.own_worker = false}};
Void::Testing::expect(
!m.restore_state(Void::Json::Value{42}).has_value());
m.start();
const auto state = m.serialize_state();
Void::Testing::expect(!m.restore_state(state).has_value());
}
Void::Testing::Test restore_with_unknown_states_falls_back_to_initial()
{
auto def = make_def();
auto bogus = Void::Json::parse(
R"({"__fsm":1,"leaves":["no_such_state"],"history":{}})");
Void::Testing::expect(bogus.has_value());
Machine m{def, MachineOptions{.own_worker = false}};
Void::Testing::expect(m.restore_state(*bogus).has_value());
m.start();
Void::Testing::expect(m.is_in("s1")); // initial configuration
}
// ── hot definition swap ───────────────────────────────────────────────────────
Void::Testing::Test swap_same_names_keeps_configuration()
{
auto def = make_def();
Machine m{def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{*def->event_id("next"), {}}); // -> s2
m.run_pending();
int entries = 0;
// New definition: same state names, one extra transition + an entry
// probe that must NOT run during a seamless swap.
Builder b;
auto work = b.state("work");
auto sub = b.state("sub", work);
auto s1 = b.state("s1", sub);
auto s2 = b.state("s2", sub);
auto pause = b.state("pause");
b.initial(work);
b.initial(sub);
b.initial(s1);
b.on_entry(s2, SyncAction{[&](Machine&, const Event&) {
++entries;
}});
b.transition(s2, b.event("back"), s1); // new behavior
b.transition(work, b.event("stop"), pause);
auto next_def = b.build();
Void::Testing::expect(next_def.has_value());
m.swap_definition(*next_def);
m.run_pending();
Void::Testing::expect(m.is_in("s2")); // configuration survived
Void::Testing::expect_eq(entries, 0); // no re-entry side effects
// New transition is live.
m.post(Event{*(*next_def)->event_id("back"), {}});
m.run_pending();
Void::Testing::expect(m.is_in("s1"));
}
Void::Testing::Test swap_with_missing_states_reenters_initial()
{
auto def = make_def();
Machine m{def, MachineOptions{.own_worker = false}};
m.start();
m.post(Event{*def->event_id("next"), {}}); // -> s2
m.run_pending();
Builder b;
(void)b.state("fresh_a");
(void)b.state("fresh_b");
auto next_def = b.build();
Void::Testing::expect(next_def.has_value());
m.swap_definition(*next_def);
m.run_pending();
Void::Testing::expect(m.is_in("fresh_a"));
}
@@ -1,114 +0,0 @@
#include <Void/StateMachine/StateMachine.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
// ── Simple traffic-light FSM ──────────────────────────────────────────────────
struct Red {};
struct Green {};
struct Yellow {};
struct GoEvent {};
struct SlowEvent {};
struct StopEvent {};
using TrafficLight = Void::StateMachine::StateMachine<
std::tuple<Red, Green, Yellow>,
std::tuple<
Void::StateMachine::Transition<Red, GoEvent, Green>,
Void::StateMachine::Transition<Green, SlowEvent, Yellow>,
Void::StateMachine::Transition<Yellow, StopEvent, Red>
>>;
Void::Testing::Test initial_state_is_first()
{
TrafficLight fsm;
Void::Testing::expect(fsm.is<Red>());
}
Void::Testing::Test valid_transition_taken()
{
TrafficLight fsm;
const bool ok = fsm.send(GoEvent{});
Void::Testing::expect(ok);
Void::Testing::expect(fsm.is<Green>());
}
Void::Testing::Test invalid_event_not_taken()
{
TrafficLight fsm;
const bool ok = fsm.send(StopEvent{}); // Red has no StopEvent arc
Void::Testing::expect(!ok);
Void::Testing::expect(fsm.is<Red>());
}
Void::Testing::Test full_cycle()
{
TrafficLight fsm;
Void::Testing::expect(fsm.send(GoEvent{}));
Void::Testing::expect(fsm.send(SlowEvent{}));
Void::Testing::expect(fsm.send(StopEvent{}));
Void::Testing::expect(fsm.is<Red>());
}
Void::Testing::Test entry_callback_fired()
{
TrafficLight fsm;
bool fired = false;
fsm.on_enter<Green>([&](const Green&) { fired = true; });
fsm.send(GoEvent{});
Void::Testing::expect(fired);
}
Void::Testing::Test exit_callback_fired()
{
TrafficLight fsm;
bool fired = false;
fsm.on_exit<Red>([&](const Red&) { fired = true; });
fsm.send(GoEvent{});
Void::Testing::expect(fired);
}
Void::Testing::Test callbacks_not_fired_on_invalid_transition()
{
TrafficLight fsm;
bool entry = false, exit_cb = false;
fsm.on_enter<Green>([&](const Green&) { entry = true; });
fsm.on_exit<Red>([&](const Red&) { exit_cb = true; });
fsm.send(StopEvent{}); // invalid from Red
Void::Testing::expect(!entry);
Void::Testing::expect(!exit_cb);
}
Void::Testing::Test state_index_matches_variant_order()
{
TrafficLight fsm;
Void::Testing::expect_eq(fsm.state_index(), std::size_t{0}); // Red is first
fsm.send(GoEvent{});
Void::Testing::expect_eq(fsm.state_index(), std::size_t{1}); // Green is second
}
// ── Two-state toggle ──────────────────────────────────────────────────────────
struct Off {};
struct On {};
struct Toggle {};
using Switch = Void::StateMachine::StateMachine<
std::tuple<Off, On>,
std::tuple<
Void::StateMachine::Transition<Off, Toggle, On>,
Void::StateMachine::Transition<On, Toggle, Off>
>>;
Void::Testing::Test toggle_back_and_forth()
{
Switch sw;
Void::Testing::expect(sw.is<Off>());
sw.send(Toggle{});
Void::Testing::expect(sw.is<On>());
sw.send(Toggle{});
Void::Testing::expect(sw.is<Off>());
}