#include #include #include #include #include #include using namespace Void::Fsm; namespace { // work{ sub{ s1, s2 } }, pause — with deep history on work. std::shared_ptr 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")); }