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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! Runtime regressions for transitions.

use super::*;

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

    let target_camera = camera()?;
    let target = application.register_world("command-poison-target", move |world| {
        world.spawn(target_camera)?;
        Ok(())
    })?;
    let source_camera = camera()?;
    let source = application.register_world("command-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 commands: LogicCommands| {
            let mut requested = false;
            if let Some(next) = next {
                for edge in input.pressed(TestAction::Enter) {
                    requested = true;
                    assert!(commands.replace_world(edge.intent(), next.0).is_ok());
                    assert!(matches!(
                        commands.spawn(Ephemeral),
                        Err(CommandEnqueueError::LimitExceeded { limit: 1 })
                    ));
                }
            }
            if !requested {
                assert!(commands.spawn(Ephemeral).is_ok());
            }
        },
    );

    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 command-poison frame should be accepted");
    };
    assert!(matches!(
        failed_report.failure(),
        Some(FrameFailure::Commands {
            stage: Stage::FixedUpdate,
            error: crate::commands::CommandBatchError::LimitExceeded { limit: 1 },
        })
    ));
    assert!(matches!(failed_report.transition(), FrameTransition::None));
    assert_eq!(runner.world_generation(), source_generation);
    assert_eq!(runner.components::<Ephemeral>().count(), 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!(matches!(
        recovered_report.transition(),
        FrameTransition::None
    ));
    assert_eq!(recovered_report.spawned(), 1);
    assert_eq!(runner.world_generation(), source_generation);
    assert_eq!(runner.components::<Ephemeral>().count(), 1);
    Ok(())
}

#[test]
fn one_input_intent_commits_world_a_to_b_during_catch_up() -> Result<(), Box<dyn Error>> {
    let mut application = Application::<TestAction>::new(AppConfig::default())?;
    application.approve_component::<Ball>()?;
    application.bind_key(PhysicalKeyCode::Enter, TestAction::Enter)?;

    let mut camera_b = camera()?;
    camera_b.set_center(Vec2::new(7.0, 3.0))?;
    let circle_b = circle(Color::rgb8(32, 96, 240))?;
    let transform_b = Transform2d::new(Vec2::new(7.0, 3.0))?;
    let world_b = application.register_world("world-b", move |world| {
        world.spawn(camera_b)?;
        world.spawn((Ball, transform_b, circle_b))?;
        world.insert_resource(RetainedEdgeObservation::default())?;
        Ok(())
    })?;

    let camera_a = camera()?;
    let circle_a = circle(Color::rgb8(240, 96, 32))?;
    let world_a = application.register_world("world-a", move |world| {
        world.spawn(camera_a)?;
        world.spawn((Ball, Transform2d::default(), circle_a))?;
        world.insert_resource(NextWorld(world_b))?;
        Ok(())
    })?;

    // Two systems react to the same causal edge. Arbitration must coalesce
    // their identical requests instead of treating the catch-up frame as a
    // conflict or allowing a second fixed tick to repeat the request.
    application.add_system(Stage::FixedUpdate, request_next_world);
    application.add_system(Stage::FixedUpdate, request_next_world);
    application.add_system(
        Stage::FixedUpdate,
        |input: FixedInput<TestAction>, observation: Option<ResMut<RetainedEdgeObservation>>| {
            let Some(mut observation) = observation else {
                return;
            };
            observation.pressed = input.pressed(TestAction::Enter).count();
            observation.released = input.released(TestAction::Enter).count();
            observation.held = input.held(TestAction::Enter);
        },
    );

    let mut runner = application.build_headless(world_a)?;
    assert_eq!(runner.factory_name(world_a), Some("world-a"));
    assert_eq!(runner.factory_name(world_b), Some("world-b"));
    assert_eq!(runner.active_world_name(), "world-a");
    let old_generation = runner.world_generation();
    let events = [InputEvent::key(
        PhysicalKeyCode::Enter,
        ButtonState::Pressed,
    )];
    let outcome = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(50),
        &events,
        viewport()?,
    ));
    let FrameOutcome::Advanced(report) = outcome else {
        panic!("bounded frame should be accepted");
    };

    let FrameTransition::Committed {
        old,
        new,
        target,
        warning,
    } = report.transition()
    else {
        panic!("input edge should commit World B");
    };
    assert_eq!(*old, old_generation);
    assert_eq!(*new, runner.world_generation());
    assert_eq!(*target, world_b);
    assert_eq!(runner.active_world_name(), "world-b");
    assert!(warning.is_none());
    assert_eq!(report.fixed_ticks_attempted(), 1);
    assert!(report.failure().is_none());
    assert_eq!(report.extracted_generation(), Some(*new));

    let lifecycle = runner.lifecycle();
    assert_eq!(lifecycle.len(), 3);
    assert_eq!(lifecycle[0].event(), LifecycleEvent::WorldEnter);
    assert_eq!(lifecycle[0].generation(), *old);
    assert_eq!(lifecycle[1].event(), LifecycleEvent::WorldExit);
    assert_eq!(lifecycle[1].generation(), *old);
    assert_eq!(lifecycle[2].event(), LifecycleEvent::WorldEnter);
    assert_eq!(lifecycle[2].generation(), *new);

    let extracted = runner
        .extracted_frame()
        .ok_or("committed World should be extracted in the same frame")?;
    assert_eq!(extracted.world_generation(), *new);
    assert_eq!(extracted.camera().center(), Vec2::new(7.0, 3.0));
    let [resolved] = extracted.resolved_circles() else {
        panic!("World B should extract exactly one circle");
    };
    assert_eq!(resolved.position(), Vec2::new(7.0, 3.0));

    let frame_snapshot = runner
        .active
        .world
        .get_resource::<FrameInputState<TestAction>>()
        .ok_or("committed World should own a frame snapshot")?;
    let fixed_snapshot = runner
        .active
        .world
        .get_resource::<FixedInputState<TestAction>>()
        .ok_or("committed World should own a fixed snapshot")?;
    assert!(!frame_snapshot.held(TestAction::Enter));
    assert_eq!(frame_snapshot.pressed(TestAction::Enter).count(), 0);
    assert!(!fixed_snapshot.held(TestAction::Enter));
    assert_eq!(fixed_snapshot.pressed(TestAction::Enter).count(), 0);

    let next = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(17),
        &[],
        viewport()?,
    ));
    assert!(matches!(
        next,
        FrameOutcome::Advanced(ref report)
            if report.failure().is_none() && report.fixed_ticks_attempted() == 1
    ));
    let observation = runner
        .resource::<RetainedEdgeObservation>()
        .ok_or("target input observation should remain available")?;
    assert!(observation.held);
    assert_eq!(observation.pressed, 0);
    assert_eq!(observation.released, 0);
    Ok(())
}

#[test]
fn failed_candidate_preserves_old_generation_and_snaps_interpolation() -> Result<(), Box<dyn Error>>
{
    let mut application = Application::<TestAction>::new(AppConfig::default())?;
    application.approve_component::<Ball>()?;
    application.bind_key(PhysicalKeyCode::KeyD, TestAction::MoveRight)?;
    application.bind_key(PhysicalKeyCode::Enter, TestAction::Enter)?;

    let failing_world = application.register_world("failing-world", |_world| {
        Err(WorldBuildError::user("deliberate candidate failure"))
    })?;
    let camera = camera()?;
    let ball = circle(Color::WHITE)?;
    let world_a = application.register_world("world-a", move |world| {
        world.spawn(camera)?;
        world.spawn((Ball, Transform2d::default(), ball))?;
        world.insert_resource(NextWorld(failing_world))?;
        Ok(())
    })?;
    application.add_system(Stage::FixedUpdate, move_ball);
    application.add_fallible_system(Stage::FixedUpdate, follow_ball);
    application.add_system(Stage::FixedUpdate, request_next_world);

    let mut runner = application.build_headless(world_a)?;
    let movement = [InputEvent::key(PhysicalKeyCode::KeyD, ButtonState::Pressed)];
    let first = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(25),
        &movement,
        viewport()?,
    ));
    let FrameOutcome::Advanced(first_report) = first else {
        panic!("bounded movement frame should be accepted");
    };
    assert!(first_report.failure().is_none());
    let first_extracted = runner
        .extracted_frame()
        .ok_or("movement frame should publish an extraction")?;
    let [first_circle] = first_extracted.resolved_circles() else {
        panic!("test World should extract exactly one circle");
    };
    assert!(first_circle.position().x() > 0.0);
    assert!(first_circle.position().x() < 1.0);

    let old_generation = runner.world_generation();
    let transition = [InputEvent::key(
        PhysicalKeyCode::Enter,
        ButtonState::Pressed,
    )];
    let second = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(9),
        &transition,
        viewport()?,
    ));
    let FrameOutcome::Advanced(second_report) = second else {
        panic!("bounded transition frame should be accepted");
    };

    assert!(matches!(
        second_report.transition(),
        FrameTransition::PreparationFailed { target, .. } if *target == failing_world
    ));
    assert!(second_report.failure().is_none());
    assert_eq!(second_report.fixed_ticks_attempted(), 1);
    assert_eq!(runner.world_generation(), old_generation);
    assert_eq!(runner.lifecycle().len(), 1);
    assert!(runner.active.world.contains_resource::<CommandQueue>());

    let Some((_, transform)) = runner.components::<Transform2d>().next() else {
        panic!("old World should retain its ball");
    };
    assert_eq!(transform.previous_translation(), transform.translation());
    assert!(transform.translation().x() > 1.9);
    let Some((_, camera)) = runner.components::<ActiveCamera2d>().next() else {
        panic!("old World should retain its camera");
    };
    assert_eq!(camera.previous_center(), camera.center());
    assert!(camera.center().x() > 1.9);

    let extracted = runner
        .extracted_frame()
        .ok_or("old World should be extracted after failed preparation")?;
    assert_eq!(extracted.world_generation(), old_generation);
    let [resolved] = extracted.resolved_circles() else {
        panic!("old World should still extract exactly one circle");
    };
    assert_eq!(resolved.position(), transform.translation());
    assert_eq!(extracted.camera().center(), camera.center());
    Ok(())
}

#[test]
fn old_frame_token_is_invalid_while_waiting_for_fixed_delivery() -> Result<(), Box<dyn Error>> {
    let mut application = Application::<TestAction>::new(AppConfig::default())?;
    application.bind_key(PhysicalKeyCode::Enter, TestAction::Enter)?;

    let target_camera = camera()?;
    let target = application.register_world("target", move |world| {
        world.spawn(target_camera)?;
        Ok(())
    })?;
    let source_camera = camera()?;
    let source = application.register_world("source", move |world| {
        world.spawn(source_camera)?;
        world.insert_resource(StoredFrameIntent(None))?;
        Ok(())
    })?;
    application.add_system(
        Stage::FrameUpdate,
        move |input: FrameInput<TestAction>,
              mut stored: ResMut<StoredFrameIntent>,
              mut commands: LogicCommands| {
            if let Some(edge) = input.pressed(TestAction::Enter).next() {
                stored.0 = Some(edge.intent());
                return;
            }
            if let Some(intent) = stored.0.take() {
                assert!(commands.replace_world(intent, target).is_ok());
            }
        },
    );

    let mut runner = application.build_headless(source)?;
    let old_generation = runner.world_generation();
    let events = [InputEvent::key(
        PhysicalKeyCode::Enter,
        ButtonState::Pressed,
    )];
    let first = runner.advance_frame(FrameRequest::new(Duration::ZERO, &events, viewport()?));
    let FrameOutcome::Advanced(first_report) = first else {
        panic!("bounded frame should be accepted");
    };
    assert_eq!(first_report.fixed_ticks_attempted(), 0);
    assert!(matches!(first_report.transition(), FrameTransition::None));

    let second = runner.advance_frame(FrameRequest::new(Duration::ZERO, &[], viewport()?));
    let FrameOutcome::Advanced(second_report) = second else {
        panic!("bounded frame should be accepted");
    };
    assert_eq!(second_report.fixed_ticks_attempted(), 0);
    assert!(matches!(
        second_report.transition(),
        FrameTransition::Invalid(TransitionRequestFailure::InvalidIntent)
    ));
    assert!(second_report.failure().is_none());
    assert_eq!(runner.world_generation(), old_generation);
    assert_eq!(runner.lifecycle().len(), 1);
    Ok(())
}

#[test]
fn frame_update_does_not_run_after_fixed_transition_request() -> Result<(), Box<dyn Error>> {
    let mut application = Application::<TestAction>::new(AppConfig::default())?;
    application.bind_key(PhysicalKeyCode::Enter, TestAction::Enter)?;

    let target_camera = camera()?;
    let target = application.register_world("target", move |world| {
        world.spawn(target_camera)?;
        Ok(())
    })?;
    let source_camera = camera()?;
    let source = application.register_world("source", move |world| {
        world.spawn(source_camera)?;
        world.insert_resource(NextWorld(target))?;
        Ok(())
    })?;
    application.add_system(Stage::FixedUpdate, request_next_world);
    let frame_runs = Arc::new(AtomicUsize::new(0));
    let observed_frame_runs = Arc::clone(&frame_runs);
    application.add_system(Stage::FrameUpdate, move || {
        observed_frame_runs.fetch_add(1, Ordering::SeqCst);
    });

    let mut runner = application.build_headless(source)?;
    let events = [InputEvent::key(
        PhysicalKeyCode::Enter,
        ButtonState::Pressed,
    )];
    let outcome = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(50),
        &events,
        viewport()?,
    ));
    let FrameOutcome::Advanced(report) = outcome else {
        panic!("bounded frame should be accepted");
    };

    assert!(matches!(
        report.transition(),
        FrameTransition::Committed { .. }
    ));
    assert_eq!(report.fixed_ticks_attempted(), 1);
    assert_eq!(frame_runs.load(Ordering::SeqCst), 0);
    Ok(())
}

#[test]
fn same_intent_with_different_targets_is_malformed_at_runtime() -> Result<(), Box<dyn Error>> {
    let mut application = Application::<TestAction>::new(AppConfig::default())?;
    application.bind_key(PhysicalKeyCode::Enter, TestAction::Enter)?;

    let first_camera = camera()?;
    let first_target = application.register_world("first-target", move |world| {
        world.spawn(first_camera)?;
        Ok(())
    })?;
    let second_camera = camera()?;
    let second_target = application.register_world("second-target", move |world| {
        world.spawn(second_camera)?;
        Ok(())
    })?;
    let source_camera = camera()?;
    let source = application.register_world("source", move |world| {
        world.spawn(source_camera)?;
        Ok(())
    })?;
    application.add_system(
        Stage::FixedUpdate,
        move |input: FixedInput<TestAction>, mut commands: LogicCommands| {
            for edge in input.pressed(TestAction::Enter) {
                assert!(commands.replace_world(edge.intent(), first_target).is_ok());
                assert!(commands.replace_world(edge.intent(), second_target).is_ok());
            }
        },
    );

    let mut runner = application.build_headless(source)?;
    let old_generation = runner.world_generation();
    let events = [InputEvent::key(
        PhysicalKeyCode::Enter,
        ButtonState::Pressed,
    )];
    let outcome = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(50),
        &events,
        viewport()?,
    ));
    let FrameOutcome::Advanced(report) = outcome else {
        panic!("bounded frame should be accepted");
    };

    assert!(matches!(
        report.transition(),
        FrameTransition::Rejected(TransitionRejection::MalformedIntent)
    ));
    assert!(report.failure().is_none());
    assert_eq!(report.fixed_ticks_attempted(), 1);
    assert!(report.dropped_for_transition().is_some());
    assert_eq!(report.extracted_generation(), Some(old_generation));
    assert_eq!(runner.world_generation(), old_generation);
    assert_eq!(runner.lifecycle().len(), 1);
    Ok(())
}

#[test]
fn different_intents_with_same_target_commit_with_runtime_warning() -> Result<(), Box<dyn Error>> {
    let mut config = AppConfig::default();
    config.set_lifecycle_trace_limit(2)?;
    let mut application = Application::<TestAction>::new(config)?;

    let target_camera = camera()?;
    let target = application.register_world("target", move |world| {
        world.spawn(target_camera)?;
        Ok(())
    })?;
    let source_camera = camera()?;
    let source = application.register_world("source", move |world| {
        world.spawn(source_camera)?;
        Ok(())
    })?;
    application.add_system(Stage::FixedUpdate, move |mut commands: LogicCommands| {
        let first = commands.new_transition_intent();
        let second = commands.new_transition_intent();
        let (Ok(first), Ok(second)) = (first, second) else {
            panic!("fixed stage should issue two bounded direct intents");
        };
        assert!(commands.replace_world(first, target).is_ok());
        assert!(commands.replace_world(second, target).is_ok());
    });

    let mut runner = application.build_headless(source)?;
    let old_generation = runner.world_generation();
    let outcome = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(17),
        &[],
        viewport()?,
    ));
    let FrameOutcome::Advanced(report) = outcome else {
        panic!("bounded frame should be accepted");
    };

    let FrameTransition::Committed {
        old,
        new,
        target: committed_target,
        warning: Some(warning),
    } = report.transition()
    else {
        panic!("convergent direct intents should commit with a warning");
    };
    assert_eq!(*old, old_generation);
    assert_eq!(*new, runner.world_generation());
    assert_eq!(*committed_target, target);
    assert_eq!(warning.distinct_intents(), 2);
    assert!(report.failure().is_none());
    assert_eq!(runner.lifecycle().len(), 2);
    assert_eq!(runner.lifecycle()[0].event(), LifecycleEvent::WorldExit);
    assert_eq!(runner.lifecycle()[1].event(), LifecycleEvent::WorldEnter);
    assert_eq!(runner.dropped_lifecycle_records(), 1);
    Ok(())
}

#[test]
fn different_intents_with_different_targets_conflict_at_runtime() -> Result<(), Box<dyn Error>> {
    let mut application = Application::<TestAction>::new(AppConfig::default())?;

    let first_camera = camera()?;
    let first_target = application.register_world("first-target", move |world| {
        world.spawn(first_camera)?;
        Ok(())
    })?;
    let second_camera = camera()?;
    let second_target = application.register_world("second-target", move |world| {
        world.spawn(second_camera)?;
        Ok(())
    })?;
    let source_camera = camera()?;
    let source = application.register_world("source", move |world| {
        world.spawn(source_camera)?;
        Ok(())
    })?;
    application.add_system(Stage::FixedUpdate, move |mut commands: LogicCommands| {
        let first = commands.new_transition_intent();
        let second = commands.new_transition_intent();
        let (Ok(first), Ok(second)) = (first, second) else {
            panic!("fixed stage should issue two bounded direct intents");
        };
        assert!(commands.replace_world(first, first_target).is_ok());
        assert!(commands.replace_world(second, second_target).is_ok());
    });

    let mut runner = application.build_headless(source)?;
    let old_generation = runner.world_generation();
    let outcome = runner.advance_frame(FrameRequest::new(
        Duration::from_millis(50),
        &[],
        viewport()?,
    ));
    let FrameOutcome::Advanced(report) = outcome else {
        panic!("bounded frame should be accepted");
    };

    assert!(matches!(
        report.transition(),
        FrameTransition::Rejected(TransitionRejection::Conflict)
    ));
    assert!(report.failure().is_none());
    assert_eq!(report.fixed_ticks_attempted(), 1);
    assert!(report.dropped_for_transition().is_some());
    assert_eq!(report.extracted_generation(), Some(old_generation));
    assert_eq!(runner.world_generation(), old_generation);
    assert_eq!(runner.lifecycle().len(), 1);
    Ok(())
}