Skip to main content

ProgramSimulator

Struct ProgramSimulator 

Source
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>

Source

pub fn new(model: M) -> Self

Create a new simulator with the given model.

The model is not initialized until init is called.

Source

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.

Source

pub fn init(&mut self)

Initialize the model once and execute its startup commands.

Should be called once before injecting events or capturing frames.

Source

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.

Source

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.

Source

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.

Source

pub fn now(&self) -> Duration

Current deterministic monotonic simulator time.

Source

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.

Source

pub fn tick(&mut self)

Deliver one tick immediately without advancing deterministic time.

Source

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.

Source

pub fn active_subscription_ids(&self) -> &[SubId]

IDs of subscriptions active at the last deterministic reconciliation.

Source

pub fn deliver_subscription( &mut self, id: SubId, msg: M::Message, ) -> Result<(), SimulatorError>

Deliver one message from an active declarative subscription.

Source

pub fn report_error(&mut self, error: impl Into<String>)

Inject a runtime error through Model::on_error.

Source

pub fn cancel(&mut self)

Cancel the simulated runtime and complete shutdown exactly once.

Source

pub fn shutdown(&mut self)

Run Model::on_shutdown and clear simulated subscriptions exactly once.

Source

pub fn is_shutdown(&self) -> bool

Whether the one-shot shutdown contract has completed.

Source

pub fn errors(&self) -> &[String]

Errors delivered through Model::on_error.

Source

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.

Source

pub fn frames(&self) -> &[Buffer]

Get all captured frame buffers.

Source

pub fn last_frame(&self) -> Option<&Buffer>

Get the most recently captured frame buffer, if any.

Source

pub fn frame_count(&self) -> usize

Get the number of captured frames.

Source

pub fn model(&self) -> &M

Get a reference to the model.

Source

pub fn model_mut(&mut self) -> &mut M

Get a mutable reference to the model.

Source

pub fn pool(&self) -> &GraphemePool

Access the simulator grapheme pool used to render captured frames.

Source

pub fn is_running(&self) -> bool

Check if the simulated program is still running.

Returns false after a Cmd::Quit has been executed.

Source

pub fn tick_rate(&self) -> Option<Duration>

Get the current tick rate (if any).

Source

pub fn logs(&self) -> &[String]

Get all log messages emitted via Cmd::Log.

Source

pub fn command_log(&self) -> &[CmdRecord]

Get the command execution log.

Source

pub fn clear_frames(&mut self)

Clear all captured frames.

Source

pub fn clear_logs(&mut self)

Clear all logs.

Auto Trait Implementations§

§

impl<M> !RefUnwindSafe for ProgramSimulator<M>

§

impl<M> !UnwindSafe for ProgramSimulator<M>

§

impl<M> Freeze for ProgramSimulator<M>
where M: Freeze,

§

impl<M> Send for ProgramSimulator<M>
where M: Send,

§

impl<M> Sync for ProgramSimulator<M>
where M: Sync,

§

impl<M> Unpin for ProgramSimulator<M>
where M: Unpin,

§

impl<M> UnsafeUnpin for ProgramSimulator<M>
where M: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more