elevator-core 18.0.0

Engine-agnostic elevator simulation library with pluggable dispatch strategies
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
//! Tests for the per-elevator `DestinationQueue` component and its
//! imperative push/clear API.

use crate::builder::SimulationBuilder;
use crate::components::ElevatorPhase;
use crate::dispatch::scan::ScanDispatch;
use crate::entity::ElevatorId;
use crate::error::SimError;
use crate::events::Event;
use crate::stop::StopId;

use super::helpers::default_config;

fn build_sim() -> crate::sim::Simulation {
    SimulationBuilder::from_config(default_config())
        .dispatch(ScanDispatch::new())
        .build()
        .unwrap()
}

fn first_elevator(sim: &crate::sim::Simulation) -> crate::entity::ElevatorId {
    crate::entity::ElevatorId::from(sim.world().elevator_ids()[0])
}

// 1
#[test]
fn fresh_queue_is_empty() {
    let sim = build_sim();
    let elev = first_elevator(&sim);
    assert_eq!(sim.destination_queue(elev), Some(&[][..]));
}

// 2
#[test]
fn dispatch_populates_queue() {
    let mut sim = build_sim();
    // Spawn rider traveling from stop 1 (not elevator's start) to stop 2,
    // so dispatch has to send the car somewhere — triggering a queue push.
    sim.spawn_rider(StopId(1), StopId(2), 75.0).unwrap();
    sim.step();
    let elev = first_elevator(&sim);
    let queue = sim.destination_queue(elev).unwrap();
    assert!(
        !queue.is_empty(),
        "queue should contain the dispatched target (got {queue:?})"
    );
}

// 3
#[test]
fn queue_pops_on_arrival() {
    let mut sim = build_sim();
    sim.spawn_rider(StopId(0), StopId(2), 75.0).unwrap();
    // Run enough ticks for the elevator to reach stop 2.
    for _ in 0..2000 {
        sim.step();
        let elev = first_elevator(&sim);
        let car = sim.world().elevator(elev.entity()).unwrap();
        if !matches!(car.phase(), ElevatorPhase::MovingToStop(_))
            && sim.destination_queue(elev).is_some_and(<[_]>::is_empty)
        {
            break;
        }
    }
    let elev = first_elevator(&sim);
    // After arrival, the queue should be empty.
    assert!(sim.destination_queue(elev).unwrap().is_empty());
}

// 4
#[test]
fn push_destination_adds_to_back() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    let s1 = sim.stop_entity(StopId(1)).unwrap();
    sim.push_destination(elev, s1).unwrap();
    assert_eq!(sim.destination_queue(elev).unwrap(), &[s1]);
}

// 5
#[test]
fn push_destination_adjacent_dedup_back() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    let s1 = sim.stop_entity(StopId(1)).unwrap();
    sim.push_destination(elev, s1).unwrap();
    sim.push_destination(elev, s1).unwrap();
    assert_eq!(sim.destination_queue(elev).unwrap(), &[s1]);
}

// 6
#[test]
fn push_destination_front_inserts_at_index_0() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    let s1 = sim.stop_entity(StopId(1)).unwrap();
    let s2 = sim.stop_entity(StopId(2)).unwrap();
    sim.push_destination(elev, s1).unwrap();
    sim.push_destination_front(elev, s2).unwrap();
    assert_eq!(sim.destination_queue(elev).unwrap(), &[s2, s1]);
}

// 7
#[test]
fn push_destination_front_adjacent_dedup() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    let s1 = sim.stop_entity(StopId(1)).unwrap();
    sim.push_destination_front(elev, s1).unwrap();
    sim.push_destination_front(elev, s1).unwrap();
    assert_eq!(sim.destination_queue(elev).unwrap(), &[s1]);
}

// 8
#[test]
fn clear_destinations_empties_queue() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    let s0 = sim.stop_entity(StopId(0)).unwrap();
    let s1 = sim.stop_entity(StopId(1)).unwrap();
    let s2 = sim.stop_entity(StopId(2)).unwrap();
    sim.push_destination(elev, s1).unwrap();
    sim.push_destination(elev, s2).unwrap();
    sim.push_destination(elev, s0).unwrap();
    sim.clear_destinations(elev).unwrap();
    assert!(sim.destination_queue(elev).unwrap().is_empty());
}

// 9
#[test]
fn imperative_push_drives_elevator() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    let s2 = sim.stop_entity(StopId(2)).unwrap();
    sim.push_destination(elev, s2).unwrap();

    let mut arrived = false;
    for _ in 0..2000 {
        sim.step();
        for ev in sim.drain_events() {
            if let Event::ElevatorArrived {
                elevator, at_stop, ..
            } = ev
                && elevator == elev.entity()
                && at_stop == s2
            {
                arrived = true;
            }
        }
        if arrived {
            break;
        }
    }
    assert!(
        arrived,
        "elevator should have arrived at stop 2 via imperative queue"
    );
}

// 10
#[test]
fn push_front_overrides_current_target() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    let s1 = sim.stop_entity(StopId(1)).unwrap();
    let s2 = sim.stop_entity(StopId(2)).unwrap();

    // First put s2 on the queue and get the elevator moving toward it.
    sim.push_destination(elev, s2).unwrap();
    sim.step(); // AdvanceQueue sets target to s2
    sim.step();

    // Now override: put s1 at the front.
    sim.push_destination_front(elev, s1).unwrap();

    let mut arrived_at = None;
    for _ in 0..2000 {
        sim.step();
        for ev in sim.drain_events() {
            if let Event::ElevatorArrived {
                elevator, at_stop, ..
            } = ev
                && elevator == elev.entity()
            {
                arrived_at = Some(at_stop);
                break;
            }
        }
        if arrived_at.is_some() {
            break;
        }
    }
    assert_eq!(
        arrived_at,
        Some(s1),
        "elevator should arrive at s1 first after push_front"
    );
}

// 11
#[test]
fn destination_queued_event_fires_on_push() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    let s2 = sim.stop_entity(StopId(2)).unwrap();

    // One push via sim helper (top stop — elevator is at stop 0).
    sim.push_destination(elev, s2).unwrap();
    // Second push via dispatch: rider at stop 1 triggers a GoToStop(s1) push.
    sim.spawn_rider(StopId(1), StopId(2), 75.0).unwrap();
    sim.step();

    let events = sim.drain_events();
    let count = events
        .iter()
        .filter(|e| matches!(e, Event::DestinationQueued { .. }))
        .count();
    // One from direct push (s2) + one from dispatch (s1).
    assert!(
        count >= 2,
        "expected at least 2 DestinationQueued events, got {count}"
    );
}

// 12
#[test]
fn destination_queued_event_suppressed_on_dedup() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    let s1 = sim.stop_entity(StopId(1)).unwrap();

    sim.push_destination(elev, s1).unwrap();
    sim.push_destination(elev, s1).unwrap(); // dedup

    let events = sim.drain_events();
    let count = events
        .iter()
        .filter(|e| matches!(e, Event::DestinationQueued { .. }))
        .count();
    assert_eq!(count, 1);
}

// 13
#[test]
fn snapshot_roundtrip_preserves_queue() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    let s0 = sim.stop_entity(StopId(0)).unwrap();
    let s1 = sim.stop_entity(StopId(1)).unwrap();
    let s2 = sim.stop_entity(StopId(2)).unwrap();

    sim.push_destination(elev, s1).unwrap();
    sim.push_destination(elev, s2).unwrap();
    sim.push_destination(elev, s0).unwrap();

    let snapshot = sim.snapshot();
    let restored = snapshot.restore(None).unwrap();
    let new_elev = ElevatorId::from(restored.world().elevator_ids()[0]);

    let restored_queue = restored.destination_queue(new_elev).unwrap();
    assert_eq!(restored_queue.len(), 3);
}

// 14
#[test]
fn push_destination_errors_on_non_elevator() {
    let mut sim = build_sim();
    let s1 = sim.stop_entity(StopId(1)).unwrap();
    // Spawn a rider entity — not an elevator.
    let rider = sim.spawn_rider(StopId(0), StopId(2), 75.0).unwrap();
    let result = sim.push_destination(ElevatorId::from(rider.entity()), s1);
    assert!(matches!(result, Err(SimError::NotAnElevator(_))));
}

// 15
#[test]
fn push_destination_errors_on_non_stop() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);
    // Use the elevator entity as the target — not a stop.
    let result = sim.push_destination(elev, elev.entity());
    assert!(matches!(result, Err(SimError::NotAStop(_))));
}

// Regression for greptile P1: when `advance_queue` redirects a moving
// elevator via `push_destination_front`, the direction indicators used
// by loading.rs to gate boarding must be updated — otherwise downward
// riders get silently rejected by a physically-descending car still
// flagged as going up.
#[test]
fn redirect_via_push_front_updates_direction_indicators() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);

    // Dispatch sends the elevator upward toward stop 2.
    sim.spawn_rider(StopId(1), StopId(2), 75.0).unwrap();
    // Let dispatch push to the queue and the elevator begin moving up.
    for _ in 0..20 {
        sim.step();
        if matches!(
            sim.world()
                .elevator(elev.entity())
                .map(crate::components::Elevator::phase),
            Some(ElevatorPhase::MovingToStop(_))
        ) {
            break;
        }
    }
    assert_eq!(sim.elevator_going_up(elev), Some(true));
    assert_eq!(sim.elevator_going_down(elev), Some(false));

    // Game imperatively redirects to a stop below the current position.
    let stop_0 = sim.stop_entity(StopId(0)).unwrap();
    sim.push_destination_front(elev, stop_0).unwrap();
    // One step of advance_queue flips the target and must refresh the lamps.
    sim.step();

    assert_eq!(
        sim.elevator_going_up(elev),
        Some(false),
        "push_destination_front to a lower stop must clear going_up",
    );
    assert_eq!(sim.elevator_going_down(elev), Some(true));
}

// ── recall_to ──────────────────────────────────────────────────────

/// `recall_to` clears the queue and sets a single target.
#[test]
fn recall_to_clears_queue_and_sets_target() {
    let mut sim = SimulationBuilder::demo().build().unwrap();
    let elev = ElevatorId::from(sim.world().elevator_ids()[0]);

    // Queue a destination, then recall to the other stop.
    sim.push_destination(elev, StopId(1)).unwrap();
    sim.recall_to(elev, StopId(0)).unwrap();

    let q = sim.destination_queue(elev).unwrap();
    assert_eq!(q.len(), 1, "queue should contain only the recall target");
    assert_eq!(q[0], sim.stop_entity(StopId(0)).unwrap());
}

/// `recall_to` emits an `ElevatorRecalled` event.
#[test]
fn recall_to_emits_event() {
    let mut sim = SimulationBuilder::demo().build().unwrap();
    let elev = ElevatorId::from(sim.world().elevator_ids()[0]);
    sim.drain_events();

    sim.recall_to(elev, StopId(1)).unwrap();

    let recall_events: Vec<_> = sim
        .drain_events()
        .into_iter()
        .filter(|e| matches!(e, Event::ElevatorRecalled { .. }))
        .collect();
    assert_eq!(recall_events.len(), 1);
    if let Event::ElevatorRecalled {
        elevator, to_stop, ..
    } = &recall_events[0]
    {
        assert_eq!(*elevator, elev.entity());
        assert_eq!(*to_stop, sim.stop_entity(StopId(1)).unwrap());
    }
}

/// `recall_to` on an idle car at a different stop causes it to depart.
#[test]
fn recall_idle_car_to_distant_stop() {
    let mut sim = SimulationBuilder::demo().build().unwrap();
    let elev = ElevatorId::from(sim.world().elevator_ids()[0]);

    sim.recall_to(elev, StopId(1)).unwrap();

    let target_pos = sim
        .world()
        .stop(sim.stop_entity(StopId(1)).unwrap())
        .unwrap()
        .position();
    let mut arrived = false;
    for _ in 0..2000 {
        sim.step();
        let pos = sim.world().position(elev.entity()).unwrap().value;
        if (pos - target_pos).abs() < 0.01 {
            arrived = true;
            break;
        }
    }
    assert!(arrived, "car should have arrived at the recall stop");
}

/// `recall_to` on a car already at the recall stop triggers a door cycle.
#[test]
fn recall_to_current_stop_opens_doors() {
    let mut sim = SimulationBuilder::demo().build().unwrap();
    let elev = ElevatorId::from(sim.world().elevator_ids()[0]);

    // Car starts at stop 0 (default). Recall to stop 0.
    sim.recall_to(elev, StopId(0)).unwrap();

    // Track whether doors opened during the cycle.
    let mut saw_open = false;
    for _ in 0..30 {
        sim.step();
        let car = sim.world().elevator(elev.entity()).unwrap();
        if car.door().is_open() {
            saw_open = true;
            break;
        }
    }
    assert!(saw_open, "doors should open when recalled to current stop");
}

/// `recall_to` works on dispatch-excluded (`Independent`) cars.
#[test]
fn recall_works_on_independent_car() {
    let mut sim = SimulationBuilder::demo().build().unwrap();
    let elev = ElevatorId::from(sim.world().elevator_ids()[0]);

    sim.set_service_mode(elev.entity(), crate::components::ServiceMode::Independent)
        .unwrap();

    sim.recall_to(elev, StopId(1)).unwrap();

    let target_pos = sim
        .world()
        .stop(sim.stop_entity(StopId(1)).unwrap())
        .unwrap()
        .position();
    let mut arrived = false;
    for _ in 0..2000 {
        sim.step();
        let pos = sim.world().position(elev.entity()).unwrap().value;
        if (pos - target_pos).abs() < 0.01 {
            arrived = true;
            break;
        }
    }
    assert!(arrived, "Independent car should still respond to recall_to");
}

/// `recall_to` errors on invalid entities.
#[test]
fn recall_to_validates_entities() {
    let mut sim = SimulationBuilder::demo().build().unwrap();
    let elev = ElevatorId::from(sim.world().elevator_ids()[0]);
    let stop_entity = sim.stop_entity(StopId(0)).unwrap();

    // Not an elevator.
    assert!(matches!(
        sim.recall_to(ElevatorId::from(stop_entity), StopId(0)),
        Err(SimError::NotAnElevator(_))
    ));

    // Not a stop.
    assert!(sim.recall_to(elev, StopId(99)).is_err());
}

/// `recall_to` mid-flight redirects the car to the recall stop.
#[test]
fn recall_mid_flight_redirects() {
    let mut sim = SimulationBuilder::demo().build().unwrap();
    let elev = ElevatorId::from(sim.world().elevator_ids()[0]);

    // Send car toward stop 1.
    sim.push_destination(elev, StopId(1)).unwrap();

    // Wait until car is actually moving.
    for _ in 0..10 {
        sim.step();
        if sim
            .world()
            .elevator(elev.entity())
            .unwrap()
            .phase()
            .is_moving()
        {
            break;
        }
    }
    assert!(
        sim.world()
            .elevator(elev.entity())
            .unwrap()
            .phase()
            .is_moving(),
        "car should be in flight"
    );

    // Recall back to stop 0 mid-flight.
    sim.recall_to(elev, StopId(0)).unwrap();

    // Car should eventually return to stop 0.
    let stop0_pos = sim
        .world()
        .stop(sim.stop_entity(StopId(0)).unwrap())
        .unwrap()
        .position();
    let mut returned = false;
    for _ in 0..2000 {
        sim.step();
        let pos = sim.world().position(elev.entity()).unwrap().value;
        let phase = sim.world().elevator(elev.entity()).unwrap().phase();
        if (pos - stop0_pos).abs() < 0.01 && !phase.is_moving() {
            returned = true;
            break;
        }
    }
    assert!(
        returned,
        "car should return to stop 0 after mid-flight recall"
    );
}

/// Disabling a stop scrubs it from all elevator destination queues.
#[test]
fn disable_stop_scrubs_destination_queues() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);

    sim.push_destination(elev, StopId(1)).unwrap();
    sim.push_destination(elev, StopId(2)).unwrap();

    let stop1_entity = sim.stop_entity(StopId(1)).unwrap();
    sim.disable(stop1_entity).unwrap();

    let q = sim.destination_queue(elev).unwrap();
    assert!(
        !q.contains(&stop1_entity),
        "disabled stop should be scrubbed from destination queue"
    );
    assert_eq!(q.len(), 1, "only the non-disabled stop should remain");
}

/// Disabling a stop that a car is actively targeting resets the car to `Idle`.
#[test]
fn disable_stop_resets_inflight_car() {
    let mut sim = build_sim();
    let elev = first_elevator(&sim);

    // Send car toward stop 2.
    sim.push_destination(elev, StopId(2)).unwrap();

    // Advance until the car is in flight (target popped from queue).
    for _ in 0..10 {
        sim.step();
        if sim
            .world()
            .elevator(elev.entity())
            .unwrap()
            .phase()
            .is_moving()
        {
            break;
        }
    }
    assert!(
        sim.world()
            .elevator(elev.entity())
            .unwrap()
            .phase()
            .is_moving(),
        "car should be in flight"
    );

    // Disable the target stop while car is en route.
    let stop2_entity = sim.stop_entity(StopId(2)).unwrap();
    sim.disable(stop2_entity).unwrap();

    // Car should be reset to Idle — no longer targeting the disabled stop.
    let car = sim.world().elevator(elev.entity()).unwrap();
    assert_eq!(
        car.phase(),
        ElevatorPhase::Idle,
        "car targeting a disabled stop should be reset to Idle"
    );
    assert_eq!(
        car.target_stop(),
        None,
        "target_stop should be cleared for the disabled stop"
    );
}