sim-logic 0.1.0

A code-first application and logic layer for Sim;Engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Runtime regressions for events.

use super::*;

#[test]
fn typed_events_are_ordered_repeatable_and_scoped_to_one_stage_invocation()
-> Result<(), Box<dyn Error>> {
    let mut config = AppConfig::default();
    config.set_time(TimeConfig::new(Duration::from_millis(10), 8)?);
    let mut application = Application::<TestAction>::new(config)?;
    let camera = camera()?;
    let world = application.register_world("event-world", move |world| {
        world.spawn(camera)?;
        world.insert_resource(EventObservation::default())?;
        Ok(())
    })?;

    application.add_system(Stage::Startup, send_startup_events);
    application.add_system(Stage::Startup, read_startup_events_first);
    application.add_system(Stage::Startup, read_startup_events_second);
    application.add_system(Stage::FixedUpdate, read_fixed_before_writer);
    application.add_system(Stage::FixedUpdate, send_fixed_event);
    application.add_system(Stage::FixedUpdate, read_fixed_after_writer);
    application.add_system(Stage::FrameUpdate, read_frame_events);

    let mut runner = application.build_headless(world)?;
    let startup = runner
        .resource::<EventObservation>()
        .ok_or("event observation should exist")?;
    assert_eq!(startup.startup_first, [3, 4]);
    assert_eq!(startup.startup_second, [3, 4]);
    assert_eq!(startup.secondary, [9]);

    let outcome = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(20),
        &[],
        viewport()?,
    ));
    let FrameOutcome::Advanced(report) = outcome else {
        panic!("bounded frame should be accepted");
    };
    assert!(report.failure().is_none());
    assert_eq!(report.fixed_ticks_attempted(), 2);

    let observation = runner
        .resource::<EventObservation>()
        .ok_or("event observation should remain available")?;
    assert_eq!(observation.fixed_before_writer, [0, 0]);
    assert_eq!(observation.fixed_after_writer, [vec![0], vec![1]]);
    assert_eq!(observation.frame_counts, [0]);
    Ok(())
}

#[test]
fn ignored_event_limit_error_stops_stage_and_discards_commands() -> Result<(), Box<dyn Error>> {
    let mut config = AppConfig::default();
    config.set_time(TimeConfig::new(Duration::from_millis(10), 8)?);
    config.set_event_limit(1)?;
    let mut application = Application::<TestAction>::new(config)?;
    application.approve_component::<Ephemeral>()?;
    let camera = camera()?;
    let world = application.register_world("event-limit", move |world| {
        world.spawn(camera)?;
        world.insert_resource(EventFailureObservation::default())?;
        Ok(())
    })?;

    application.add_system(
        Stage::FixedUpdate,
        |mut observation: ResMut<EventFailureObservation>,
         mut events: EventWriter<PrimaryEvent>,
         mut secondary: EventWriter<SecondaryEvent>,
         mut commands: LogicCommands| {
            if observation.attempts > 0 {
                return;
            }
            observation.attempts += 1;
            assert!(commands.request_exit().is_ok());
            assert!(commands.spawn(Ephemeral).is_ok());
            assert!(events.send(PrimaryEvent(1)).is_ok());
            let first = events
                .send(PrimaryEvent(2))
                .expect_err("second primary event should exceed the limit");
            let repeated = secondary
                .send(SecondaryEvent(8))
                .expect_err("poisoned stage should reject another event type");
            assert!(matches!(
                first,
                EventSendError::LimitExceeded { event, limit: 1 }
                    if event == type_name::<PrimaryEvent>()
            ));
            assert_eq!(repeated, EventSendError::StageAlreadyFailed);
        },
    );
    application.add_system(
        Stage::FixedUpdate,
        |mut observation: ResMut<EventFailureObservation>| {
            observation.later_runs += 1;
        },
    );

    let mut runner = application.build_headless(world)?;
    let failed = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(10),
        &[],
        viewport()?,
    ));
    let FrameOutcome::Advanced(failed_report) = failed else {
        panic!("bounded frame should be accepted");
    };
    let Some(FrameFailure::System { stage, error }) = failed_report.failure() else {
        panic!("ignored event overflow should poison the fixed stage");
    };
    assert_eq!(*stage, Stage::FixedUpdate);
    assert!(error.reason().contains("exceeded its limit of 1"));
    assert!(error.reason().contains(type_name::<PrimaryEvent>()));
    assert!(!failed_report.exit_requested());
    assert_eq!(failed_report.spawned(), 0);
    assert_eq!(runner.components::<Ephemeral>().count(), 0);
    let observation = runner
        .resource::<EventFailureObservation>()
        .ok_or("failure observation should exist")?;
    assert_eq!(observation.attempts, 1);
    assert_eq!(observation.later_runs, 0);

    let recovered = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(10),
        &[],
        viewport()?,
    ));
    let FrameOutcome::Advanced(recovered_report) = recovered else {
        panic!("bounded recovery frame should be accepted");
    };
    assert!(recovered_report.failure().is_none());
    assert_eq!(
        runner
            .resource::<EventFailureObservation>()
            .ok_or("failure observation should remain available")?
            .later_runs,
        1
    );
    Ok(())
}

#[test]
fn startup_event_failure_discards_the_isolated_candidate() -> Result<(), Box<dyn Error>> {
    let mut config = AppConfig::default();
    config.set_event_limit(1)?;
    let mut application = Application::<TestAction>::new(config)?;
    let camera = camera()?;
    let world = application.register_world("startup-event-limit", move |world| {
        world.spawn(camera)?;
        Ok(())
    })?;
    application.add_system(Stage::Startup, |mut events: EventWriter<PrimaryEvent>| {
        assert!(events.send(PrimaryEvent(1)).is_ok());
        let _ = events.send(PrimaryEvent(2));
    });

    let error = match application.build_headless(world) {
        Ok(_) => panic!("poisoned Startup should reject the candidate"),
        Err(error) => error,
    };
    let RunnerBuildError::InitialWorld(CandidateFailure::StartupSystem(error)) = error else {
        panic!("event failure should be reported as a Startup System failure");
    };
    assert!(error.reason().contains("exceeded its limit of 1"));
    Ok(())
}

#[test]
fn explicit_system_error_takes_priority_over_event_poison() -> Result<(), Box<dyn Error>> {
    let mut config = AppConfig::default();
    config.set_time(TimeConfig::new(Duration::from_millis(10), 8)?);
    config.set_event_limit(1)?;
    let mut application = Application::<TestAction>::new(config)?;
    let camera = camera()?;
    let world = application.register_world("event-error-priority", move |world| {
        world.spawn(camera)?;
        Ok(())
    })?;
    application.add_fallible_system(
        Stage::FixedUpdate,
        |mut events: EventWriter<PrimaryEvent>| -> Result<(), &'static str> {
            events
                .send(PrimaryEvent(1))
                .expect("first event should fit");
            let _ = events.send(PrimaryEvent(2));
            Err("explicit event system failure")
        },
    );

    let mut runner = application.build_headless(world)?;
    let outcome = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(10),
        &[],
        viewport()?,
    ));
    let FrameOutcome::Advanced(report) = outcome else {
        panic!("bounded frame should be accepted");
    };
    let Some(FrameFailure::System { error, .. }) = report.failure() else {
        panic!("fallible System should stop the fixed stage");
    };
    assert_eq!(
        error.reason(),
        "returned an error: explicit event system failure"
    );
    Ok(())
}

#[test]
fn frame_event_poison_stops_later_systems_and_discards_commands() -> Result<(), Box<dyn Error>> {
    let mut config = AppConfig::default();
    config.set_event_limit(1)?;
    let mut application = Application::<TestAction>::new(config)?;
    application.approve_component::<Ephemeral>()?;
    let camera = camera()?;
    let world = application.register_world("frame-event-limit", move |world| {
        world.spawn(camera)?;
        world.insert_resource(EventFailureObservation::default())?;
        Ok(())
    })?;
    application.add_system(
        Stage::FrameUpdate,
        |mut observation: ResMut<EventFailureObservation>,
         mut events: EventWriter<PrimaryEvent>,
         mut commands: LogicCommands| {
            if observation.attempts > 0 {
                return;
            }
            observation.attempts += 1;
            assert!(commands.spawn(Ephemeral).is_ok());
            assert!(events.send(PrimaryEvent(1)).is_ok());
            let _ = events.send(PrimaryEvent(2));
        },
    );
    application.add_system(
        Stage::FrameUpdate,
        |mut observation: ResMut<EventFailureObservation>| {
            observation.later_runs += 1;
        },
    );

    let mut runner = application.build_headless(world)?;
    let failed = runner.advance_frame(FrameRequest::new(Duration::ZERO, &[], viewport()?));
    let FrameOutcome::Advanced(failed_report) = failed else {
        panic!("bounded frame should be accepted");
    };
    assert!(matches!(
        failed_report.failure(),
        Some(FrameFailure::System {
            stage: Stage::FrameUpdate,
            ..
        })
    ));
    assert!(!failed_report.exit_requested());
    assert_eq!(failed_report.spawned(), 0);
    assert_eq!(runner.components::<Ephemeral>().count(), 0);
    assert_eq!(
        runner
            .resource::<EventFailureObservation>()
            .ok_or("frame failure observation should exist")?
            .later_runs,
        0
    );

    let recovered = runner.advance_frame(FrameRequest::new(Duration::ZERO, &[], viewport()?));
    let FrameOutcome::Advanced(recovered_report) = recovered else {
        panic!("bounded recovery frame should be accepted");
    };
    assert!(recovered_report.failure().is_none());
    assert_eq!(
        runner
            .resource::<EventFailureObservation>()
            .ok_or("frame failure observation should remain available")?
            .later_runs,
        1
    );
    Ok(())
}

#[test]
fn event_poison_discards_transition_and_consumes_its_input_intent() -> Result<(), Box<dyn Error>> {
    let mut config = AppConfig::default();
    config.set_time(TimeConfig::new(Duration::from_millis(10), 8)?);
    config.set_event_limit(1)?;
    let mut application = Application::<TestAction>::new(config)?;
    application.bind_key(PhysicalKeyCode::Enter, TestAction::Enter)?;

    let target_camera = camera()?;
    let target = application.register_world("event-poison-target", move |world| {
        world.spawn(target_camera)?;
        Ok(())
    })?;
    let source_camera = camera()?;
    let source = application.register_world("event-poison-source", move |world| {
        world.spawn(source_camera)?;
        world.insert_resource(NextWorld(target))?;
        Ok(())
    })?;
    application.add_system(
        Stage::FixedUpdate,
        |input: FixedInput<TestAction>,
         next: Option<Res<NextWorld>>,
         mut events: EventWriter<PrimaryEvent>,
         mut commands: LogicCommands| {
            let Some(next) = next else {
                return;
            };
            for edge in input.pressed(TestAction::Enter) {
                assert!(commands.replace_world(edge.intent(), next.0).is_ok());
                assert!(events.send(PrimaryEvent(1)).is_ok());
                let _ = events.send(PrimaryEvent(2));
            }
        },
    );

    let mut runner = application.build_headless(source)?;
    let source_generation = runner.world_generation();
    let pressed = [InputEvent::key(
        PhysicalKeyCode::Enter,
        ButtonState::Pressed,
    )];
    let failed = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(10),
        &pressed,
        viewport()?,
    ));
    let FrameOutcome::Advanced(failed_report) = failed else {
        panic!("bounded frame should be accepted");
    };
    assert!(matches!(
        failed_report.failure(),
        Some(FrameFailure::System {
            stage: Stage::FixedUpdate,
            ..
        })
    ));
    assert!(matches!(failed_report.transition(), FrameTransition::None));
    assert_eq!(runner.world_generation(), source_generation);

    let recovered = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(10),
        &[],
        viewport()?,
    ));
    let FrameOutcome::Advanced(recovered_report) = recovered else {
        panic!("bounded recovery frame should be accepted");
    };
    assert!(recovered_report.failure().is_none());
    assert!(matches!(
        recovered_report.transition(),
        FrameTransition::None
    ));
    assert_eq!(runner.world_generation(), source_generation);
    Ok(())
}

#[test]
fn committed_world_starts_with_fresh_event_channels() -> Result<(), Box<dyn Error>> {
    let mut config = AppConfig::default();
    config.set_time(TimeConfig::new(Duration::from_millis(10), 8)?);
    let mut application = Application::<TestAction>::new(config)?;
    application.bind_key(PhysicalKeyCode::Enter, TestAction::Enter)?;

    let target_camera = camera()?;
    let target = application.register_world("fresh-event-target", move |world| {
        world.spawn(target_camera)?;
        world.insert_resource(StartupEventCount::default())?;
        Ok(())
    })?;
    let source_camera = camera()?;
    let source = application.register_world("fresh-event-source", move |world| {
        world.spawn(source_camera)?;
        world.insert_resource(NextWorld(target))?;
        Ok(())
    })?;
    application.add_system(
        Stage::Startup,
        |events: EventReader<PrimaryEvent>, count: Option<ResMut<StartupEventCount>>| {
            if let Some(mut count) = count {
                count.0 = events.len();
            }
        },
    );
    application.add_system(
        Stage::FixedUpdate,
        |input: FixedInput<TestAction>,
         next: Option<Res<NextWorld>>,
         mut events: EventWriter<PrimaryEvent>,
         mut commands: LogicCommands| {
            let Some(next) = next else {
                return;
            };
            for edge in input.pressed(TestAction::Enter) {
                assert!(events.send(PrimaryEvent(7)).is_ok());
                assert!(commands.replace_world(edge.intent(), next.0).is_ok());
            }
        },
    );

    let mut runner = application.build_headless(source)?;
    let pressed = [InputEvent::key(
        PhysicalKeyCode::Enter,
        ButtonState::Pressed,
    )];
    let outcome = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(10),
        &pressed,
        viewport()?,
    ));
    let FrameOutcome::Advanced(report) = outcome else {
        panic!("bounded frame should be accepted");
    };
    assert!(report.failure().is_none());
    assert!(matches!(
        report.transition(),
        FrameTransition::Committed { .. }
    ));
    assert_eq!(runner.active_world_name(), "fresh-event-target");
    assert_eq!(
        runner
            .resource::<StartupEventCount>()
            .ok_or("target startup observation should exist")?
            .0,
        0
    );
    Ok(())
}