pub struct ProgramSimulator<M: Model> { /* private fields */ }Expand description
Deterministic simulator for Model testing.
Runs model logic without any terminal or IO dependencies. Events can be injected, messages sent directly, and frames captured for snapshot testing.
Implementations§
Source§impl<M: Model> ProgramSimulator<M>
impl<M: Model> ProgramSimulator<M>
Sourcepub fn new(model: M) -> Self
pub fn new(model: M) -> Self
Create a new simulator with the given model.
The model is not initialized until init is called.
Sourcepub fn with_registry(model: M, registry: Arc<StateRegistry>) -> Self
pub fn with_registry(model: M, registry: Arc<StateRegistry>) -> Self
Create a new simulator with the given model and persistence registry.
When provided, Cmd::SaveState/Cmd::RestoreState will flush/load
through the registry, mirroring runtime behavior.
Sourcepub fn init(&mut self)
pub fn init(&mut self)
Initialize the model once and execute its startup commands.
Should be called once before injecting events or capturing frames.
Sourcepub fn inject_events(&mut self, events: &[Event])
pub fn inject_events(&mut self, events: &[Event])
Inject terminal events into the model.
Each event is converted to a message via From<Event> and dispatched
through Model::update(). Commands returned from update are executed.
Sourcepub fn inject_event(&mut self, event: Event)
pub fn inject_event(&mut self, event: Event)
Inject a single terminal event into the model.
The event is converted to a message via From<Event> and dispatched
through Model::update(). Commands returned from update are executed.
Sourcepub fn send(&mut self, msg: M::Message)
pub fn send(&mut self, msg: M::Message)
Send a specific message to the model.
The message is dispatched through Model::update() and returned
commands are executed.
Sourcepub fn advance_time(&mut self, delta: Duration) -> usize
pub fn advance_time(&mut self, delta: Duration) -> usize
Advance deterministic time and deliver every scheduled tick now due.
Positive tick intervals are delivered at their exact deadlines. A zero interval remains manually driven to avoid an unbounded loop.
Sourcepub fn poll_subscriptions(&mut self) -> &[SubId] ⓘ
pub fn poll_subscriptions(&mut self) -> &[SubId] ⓘ
Reconcile the model’s declarative subscriptions without starting background threads.
Tests explicitly deliver subscription messages with
deliver_subscription, keeping ordering
and time fully deterministic.
Sourcepub fn active_subscription_ids(&self) -> &[SubId] ⓘ
pub fn active_subscription_ids(&self) -> &[SubId] ⓘ
IDs of subscriptions active at the last deterministic reconciliation.
Sourcepub fn deliver_subscription(
&mut self,
id: SubId,
msg: M::Message,
) -> Result<(), SimulatorError>
pub fn deliver_subscription( &mut self, id: SubId, msg: M::Message, ) -> Result<(), SimulatorError>
Deliver one message from an active declarative subscription.
Sourcepub fn report_error(&mut self, error: impl Into<String>)
pub fn report_error(&mut self, error: impl Into<String>)
Inject a runtime error through Model::on_error.
Sourcepub fn shutdown(&mut self)
pub fn shutdown(&mut self)
Run Model::on_shutdown and clear simulated subscriptions exactly once.
Sourcepub fn is_shutdown(&self) -> bool
pub fn is_shutdown(&self) -> bool
Whether the one-shot shutdown contract has completed.
Sourcepub fn capture_frame(&mut self, width: u16, height: u16) -> &Buffer
pub fn capture_frame(&mut self, width: u16, height: u16) -> &Buffer
Capture the current frame at the given dimensions.
Calls Model::view() to render into a fresh buffer and stores the
result. Returns a reference to the captured buffer.
Sourcepub fn last_frame(&self) -> Option<&Buffer>
pub fn last_frame(&self) -> Option<&Buffer>
Get the most recently captured frame buffer, if any.
Sourcepub fn frame_count(&self) -> usize
pub fn frame_count(&self) -> usize
Get the number of captured frames.
Sourcepub fn pool(&self) -> &GraphemePool
pub fn pool(&self) -> &GraphemePool
Access the simulator grapheme pool used to render captured frames.
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
Check if the simulated program is still running.
Returns false after a Cmd::Quit has been executed.
Sourcepub fn command_log(&self) -> &[CmdRecord]
pub fn command_log(&self) -> &[CmdRecord]
Get the command execution log.
Sourcepub fn clear_frames(&mut self)
pub fn clear_frames(&mut self)
Clear all captured frames.
Sourcepub fn clear_logs(&mut self)
pub fn clear_logs(&mut self)
Clear all logs.