lurq 0.48.2

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//! Paced animation frames, run through a model of the winit loop: each turn ticks timers, futures and scheduled
//! redraws, asks [`FramePacing`] for the window's next frame, renders when the tree needs a redraw and otherwise
//! sleeps as [`LoopWait`] says. The render engine models a 60 Hz FIFO swapchain with a frame latency of one: a
//! frame waits for the vblank after the previous present, as `get_current_texture` does, then takes its render
//! cost. These tests run on the wall clock, so their bounds leave room for a busy machine; the cadence rule itself
//! is also checked on a virtual clock, which no scheduling noise reaches.

use std::{
  collections::VecDeque,
  f32::consts::TAU,
  num::NonZeroIsize,
  sync::{Arc, Mutex},
  time::{Duration, Instant},
};

use raw_window_handle::{
  DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, Win32WindowHandle, WindowHandle, WindowsDisplayHandle,
};
use winit::{dpi::PhysicalPosition, event::WindowEvent};

use super::{
  frame_pacing::{FramePacing, PresentedFrame, frame_due},
  loop_wait::{LoopNeeds, LoopWait},
};
use crate::{
  animation::{AnimatableProperty, Animation, Easing, Keyframes, KeyframesId, StepPosition},
  app::{
    App, Tree,
    component::Component,
    ctx::{Ctx, Interval},
    events::MouseButton,
    render_engine::RenderEngine,
  },
  components::{Rect, Row},
  layout::render_list::RenderList,
  node::{Element, transform::Decomposed},
};

const REFRESH: Duration = Duration::from_nanos(16_666_667);
const TURN: KeyframesId = KeyframesId::new(41);
const SPINNER: (f32, f32) = (20.0, 20.0);

struct TestSurface;

impl HasWindowHandle for TestSurface {
  fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
    let handle = Win32WindowHandle::new(NonZeroIsize::new(1).expect("1 is not zero"));
    // SAFETY: the handle is never dereferenced: the test render engine ignores it.
    Ok(unsafe { WindowHandle::borrow_raw(handle.into()) })
  }
}

impl HasDisplayHandle for TestSurface {
  fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
    // SAFETY: as above, the handle is never used.
    Ok(unsafe { DisplayHandle::borrow_raw(WindowsDisplayHandle::new().into()) })
  }
}

#[derive(Clone, Copy, Debug)]
struct Frame {
  /// When the pass that drew the frame started, which is the time the animation was sampled at.
  pass_at: Instant,
  /// The spinner's rotation, in radians.
  angle: f32,
}

struct Display {
  epoch: Instant,
  last_present: Option<Instant>,
  pass_at: Instant,
  /// The CPU time a frame takes once the swapchain hands it a buffer.
  render_cost: Duration,
  frames: Vec<Frame>,
  /// The vblank that shows each presented frame.
  vblanks: Vec<u128>,
}

impl Display {
  fn vblank_index(&self, at: Instant) -> u128 {
    vblank_index(self.epoch, REFRESH, at)
  }

  fn vblank_after(&self, at: Instant) -> Instant {
    let refreshes = self.vblank_index(at);
    self.epoch + REFRESH * u32::try_from(refreshes).expect("a test runs for less than two years")
  }
}

/// Which vblank shows a frame presented at `at`: the first after it, counted from the vblank at `epoch`.
fn vblank_index(epoch: Instant, refresh: Duration, at: Instant) -> u128 {
  (at - epoch).as_nanos() / refresh.as_nanos() + 1
}

/// How many vblanks between the first and the last present showed no new frame.
fn skipped_vblanks(vblanks: &[u128]) -> u128 {
  vblanks.windows(2).map(|pair| pair[1] - pair[0] - 1).sum()
}

struct VsyncEngine(Arc<Mutex<Display>>);

impl RenderEngine for VsyncEngine {
  fn resize(&mut self, _width: u32, _height: u32) {}

  fn render(&mut self, list: &RenderList, _window: WindowHandle<'_>, _display: DisplayHandle<'_>) -> bool {
    let mut display = self.0.lock().expect("the display is used on one thread");
    if let Some(previous) = display.last_present {
      sleep_until(display.vblank_after(previous));
    }
    std::thread::sleep(display.render_cost);
    let presented = Instant::now();
    display.last_present = Some(presented);
    let vblank = display.vblank_index(presented);
    display.vblanks.push(vblank);
    let [a, b, _, _] = list.rects.first().map_or([1.0, 0.0, 0.0, 1.0], |rect| rect.transform);
    let frame = Frame {
      pass_at: display.pass_at,
      angle: b.atan2(a).rem_euclid(TAU),
    };
    display.frames.push(frame);
    true
  }
}

fn sleep_until(at: Instant) {
  let now = Instant::now();
  if at > now {
    std::thread::sleep(at - now);
  }
}

/// The winit loop of one window, without winit.
struct Shell {
  app: App,
  tree: Tree,
  pacing: FramePacing,
  display: Arc<Mutex<Display>>,
  /// Clicks on the spinner the "OS" delivers at these times.
  clicks: VecDeque<Instant>,
  /// How late each due timer fired.
  timer_lateness: Vec<Duration>,
  /// Turns that ended with the loop asleep.
  sleeps: usize,
}

impl Shell {
  fn new(tree: Tree) -> Self {
    let display = Arc::new(Mutex::new(Display {
      epoch: Instant::now(),
      last_present: None,
      pass_at: Instant::now(),
      render_cost: Duration::ZERO,
      frames: Vec::new(),
      vblanks: Vec::new(),
    }));
    let mut shell = Self {
      app: App::new(),
      tree,
      pacing: FramePacing::default(),
      display: display.clone(),
      clicks: VecDeque::new(),
      timer_lateness: Vec::new(),
      sleeps: 0,
    };
    shell.tree.resize(200, 100);
    shell
      .tree
      .set_render_engine_factory(move || Box::new(VsyncEngine(display.clone())));
    shell.tree.request_redraw();
    shell
  }

  fn run_for(&mut self, duration: Duration) {
    let end = Instant::now() + duration;
    loop {
      let now = Instant::now();
      if now >= end {
        return;
      }
      self.turn(now, end);
    }
  }

  fn turn(&mut self, now: Instant, end: Instant) {
    while self.clicks.front().is_some_and(|at| *at <= now) {
      self.clicks.pop_front();
      self.tree.mouse_down(SPINNER.0, SPINNER.1, MouseButton::Left);
      self.tree.mouse_up(SPINNER.0, SPINNER.1, MouseButton::Left);
    }
    let timer_due = self.tree.next_timer_at().filter(|due| *due <= now);
    self.tree.tick_timers();
    if let Some(due) = timer_due {
      self.timer_lateness.push(Instant::now() - due);
    }
    self.tree.tick_futures();
    self.pacing.request_frame(&mut self.tree, || REFRESH, now);
    self.tree.tick_scheduled_redraw(now);
    if self.tree.needs_redraw() {
      let started = Instant::now();
      self.display.lock().expect("one thread").pass_at = started;
      if self.tree.pass(&mut self.app, &TestSurface).rendered {
        self.pacing.presented(PresentedFrame {
          started,
          presented: Instant::now(),
        });
      }
      return;
    }
    let needs = LoopNeeds {
      polling: self.tree.needs_polling(),
      next_redraw: self.tree.next_scheduled_redraw(),
      next_timer: self.tree.next_timer_at(),
      ..LoopNeeds::default()
    };
    let wake = match LoopWait::of(needs, now) {
      LoopWait::Poll => return,
      LoopWait::WaitUntil(at) => at.min(end),
      LoopWait::Wait => end,
    };
    let wake = self.clicks.front().map_or(wake, |click| wake.min(*click));
    self.sleeps += 1;
    sleep_until(wake);
  }

  fn frames(&self) -> Vec<Frame> {
    self.display.lock().expect("one thread").frames.clone()
  }

  fn set_render_cost(&mut self, cost: Duration) {
    self.display.lock().expect("one thread").render_cost = cost;
  }

  fn vblanks(&self) -> Vec<u128> {
    self.display.lock().expect("one thread").vblanks.clone()
  }
}

fn turn_keyframes() -> Keyframes {
  Keyframes::new(TURN)
    .frame(0.0, |f| {
      f.set(AnimatableProperty::Transform, Decomposed::IDENTITY.with_rotate(0.0));
    })
    .frame(1.0, |f| {
      f.set(AnimatableProperty::Transform, Decomposed::IDENTITY.with_rotate(TAU));
    })
}

fn steps(count: u32) -> Easing {
  Easing::Steps {
    count,
    position: StepPosition::JumpEnd,
  }
}

fn spinner(easing: Easing) -> Tree {
  let mut tree = Tree::new();
  tree.register_keyframes(turn_keyframes());
  tree.set_root(
    Row::new().child(
      Rect::new(40.0, 40.0)
        .background("#2563eb")
        .animation(Animation::new(TURN).duration_ms(1000).easing(easing).infinite()),
    ),
  );
  tree
}

/// Which of the `count` poses of a turn the angle is.
fn pose(angle: f32, count: u32) -> u32 {
  ((angle / (TAU / count as f32)).round() as u32) % count
}

#[test]
fn a_spinner_of_twelve_steps_a_second_presents_about_twelve_frames_a_second() {
  let mut shell = Shell::new(spinner(steps(12)));
  shell.run_for(Duration::from_secs(1));

  let frames = shell.frames();
  assert!(
    (11..=14).contains(&frames.len()),
    "a 12-step turn per second draws a frame per step, not one per refresh: {} frames in 1 s",
    frames.len()
  );
  for pair in frames.windows(2) {
    assert_ne!(
      pose(pair[0].angle, 12),
      pose(pair[1].angle, 12),
      "every frame shows the next step: {frames:?}"
    );
  }
}

#[test]
fn a_continuous_spinner_advances_every_refresh_through_paced_frames() {
  let mut shell = Shell::new(spinner(Easing::Linear));
  shell.run_for(Duration::from_millis(500));

  let frames = shell.frames();
  assert!(
    (24..=32).contains(&frames.len()),
    "a continuous animation runs at the display's rate: {} frames in 0.5 s",
    frames.len()
  );
  let advanced = frames.windows(2).filter(|pair| pair[0].angle != pair[1].angle).count();
  assert_eq!(advanced, frames.len() - 1, "every frame turns the spinner: {frames:?}");
  assert!(
    shell.sleeps >= frames.len() - 1,
    "the loop sleeps until each frame is due instead of waiting for the swapchain: {} sleeps for {} frames",
    shell.sleeps,
    frames.len()
  );
}

#[test]
fn the_loop_waits_for_an_event_once_an_animation_ends() {
  let mut tree = Tree::new();
  tree.register_keyframes(turn_keyframes());
  tree.set_root(
    Row::new().child(Rect::new(40.0, 40.0).animation(Animation::new(TURN).duration_ms(300).easing(steps(3)))),
  );
  let mut shell = Shell::new(tree);
  shell.run_for(Duration::from_millis(500));

  let frames = shell.frames().len();
  assert!((3..=5).contains(&frames), "three steps and the end: {frames} frames");
  assert!(!shell.tree.has_active_timeline());
  assert!(!shell.tree.needs_redraw());
  let needs = LoopNeeds {
    polling: shell.tree.needs_polling(),
    next_redraw: shell.tree.next_scheduled_redraw(),
    next_timer: shell.tree.next_timer_at(),
    ..LoopNeeds::default()
  };
  assert_eq!(
    LoopWait::of(needs, Instant::now()),
    LoopWait::Wait,
    "nothing is left to wake for"
  );
}

#[test]
fn a_minimized_window_draws_no_animation_frames_and_resumes_on_time_when_restored() {
  let mut shell = Shell::new(spinner(steps(12)));
  shell.run_for(Duration::from_millis(300));
  let started_at = shell.frames()[0].pass_at;

  shell.tree.window().set_minimized(true);
  let frames_before = shell.frames().len();
  shell.run_for(Duration::from_millis(500));
  assert_eq!(
    shell.frames().len(),
    frames_before,
    "a minimized window draws no animation frames"
  );

  shell.tree.window().set_minimized(false);
  let restored_at = Instant::now();
  shell.run_for(Duration::from_millis(400));

  let resumed = &shell.frames()[frames_before..];
  assert!(
    (4..=7).contains(&resumed.len()),
    "the spinner turns again at its pace after the restore: {} frames in 0.4 s",
    resumed.len()
  );
  assert!(
    resumed[0].pass_at - restored_at < REFRESH,
    "the first frame comes at once"
  );
  for frame in resumed {
    let elapsed_steps = ((frame.pass_at - started_at).as_secs_f64() * 12.0).floor() as u32 % 12;
    let drawn = pose(frame.angle, 12);
    let off = (drawn + 12 - elapsed_steps) % 12;
    assert!(
      off == 0 || off == 11,
      "the animation kept its time while minimized: step {drawn} drawn at step {elapsed_steps} of the clock"
    );
  }
}

/// When each click reached the spinner's handler.
#[derive(Clone, Default)]
struct ClickLog(Arc<Mutex<Vec<Instant>>>);

impl PartialEq for ClickLog {
  fn eq(&self, other: &Self) -> bool {
    Arc::ptr_eq(&self.0, &other.0)
  }
}

#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for ClickLog {
  fn write_info(&self, _buffer: &mut Vec<crate::app::component::ComponentInfo>) {}
}

/// A turning spinner that logs its clicks, beside a 25 ms interval.
struct ClickableSpinner {
  clicks: ClickLog,
  _interval: Interval,
}

impl Component for ClickableSpinner {
  type Props = ClickLog;

  fn create(ctx: &mut Ctx) -> Self {
    let interval = ctx.create_interval(Duration::from_millis(25), || {});
    interval.start();
    Self {
      clicks: ctx.props::<ClickLog>().clone(),
      _interval: interval,
    }
  }

  fn render(&self, _ctx: &mut Ctx) -> impl Into<Element> {
    let clicks = self.clicks.clone();
    Row::new().child(
      Rect::new(40.0, 40.0)
        .background("#2563eb")
        .animation(Animation::new(TURN).duration_ms(1000).easing(steps(12)).infinite())
        .on_click(move |_| clicks.0.lock().expect("one thread").push(Instant::now())),
    )
  }
}

#[test]
fn input_and_timers_are_handled_at_once_while_a_spinner_turns() {
  let mut app = App::new();
  let mut tree = Tree::new();
  tree.register_keyframes(turn_keyframes());
  let log = ClickLog::default();
  tree.mount_root::<ClickableSpinner>(&mut app, log.clone());
  let mut shell = Shell::new(tree);
  shell.run_for(Duration::from_millis(100));

  let first_click = Instant::now() + Duration::from_millis(7);
  let clicks: Vec<Instant> = (0..12).map(|i| first_click + Duration::from_millis(37) * i).collect();
  shell.clicks = clicks.iter().copied().collect();
  shell.timer_lateness.clear();
  shell.run_for(Duration::from_millis(600));

  let clicked_at = log.0.lock().expect("one thread").clone();
  assert_eq!(clicked_at.len(), clicks.len(), "every click is handled");
  let mut click_latency: Vec<Duration> = clicks
    .iter()
    .zip(&clicked_at)
    .map(|(sent, handled)| *handled - *sent)
    .collect();
  click_latency.sort();
  let mut timer_lateness = shell.timer_lateness.clone();
  timer_lateness.sort();
  assert!(
    timer_lateness.len() >= 15,
    "the 25 ms interval keeps firing: {timer_lateness:?}"
  );
  // A present within a refresh of the previous one still waits for the vblank, as on a real FIFO swapchain, so an
  // event that comes during it waits too: the medians tell a loop that sleeps between frames from one that waits
  // in the swapchain.
  let median = |sorted: &[Duration]| sorted[sorted.len() / 2];
  let on_time = Duration::from_millis(3);
  assert!(
    median(&click_latency) < on_time && median(&timer_lateness) < on_time,
    "clicks are handled and timers fire when they are due, not after the next frame:\n  click latency {click_latency:?}\n  timer lateness {timer_lateness:?}"
  );
}

#[test]
fn a_continuous_spinner_presents_at_every_vblank_whatever_its_render_cost() {
  for cost_ms in 0..=3 {
    let mut shell = Shell::new(spinner(Easing::Linear));
    shell.set_render_cost(Duration::from_millis(cost_ms));
    shell.run_for(Duration::from_millis(600));

    // The first frame comes when the run starts, off the vblank grid; the cadence counts from the second.
    let vblanks = shell.vblanks();
    let skipped = skipped_vblanks(&vblanks[1..]);
    assert_eq!(
      skipped,
      0,
      "with {cost_ms} ms of render work, every vblank shows a new frame: {} presents, {skipped} vblanks without one",
      vblanks.len()
    );
    assert!(
      (34..=39).contains(&vblanks.len()),
      "60 presents a second: {} in 0.6 s with {cost_ms} ms of work",
      vblanks.len()
    );
  }
}

#[test]
fn a_twelve_step_spinner_draws_only_its_steps_whatever_its_render_cost() {
  let mut shell = Shell::new(spinner(steps(12)));
  shell.set_render_cost(Duration::from_millis(3));
  shell.run_for(Duration::from_secs(1));

  let frames = shell.frames().len();
  assert!(
    (11..=14).contains(&frames),
    "12 steps a second draw 12 frames a second: {frames} in 1 s"
  );
}

#[test]
fn a_minimized_window_draws_no_perf_overlay_frames() {
  let mut tree = Tree::new();
  tree.set_root(Row::new().child(Rect::new(40.0, 40.0).background("#2563eb")));
  tree.draw_perf_overlay();
  let mut shell = Shell::new(tree);
  shell.run_for(Duration::from_millis(100));

  shell.tree.window().set_minimized(true);
  let frames_before = shell.frames().len();
  // Wake the loop every 50 ms, as events reach a minimized window (an MCP call, a wake from another thread),
  // across the overlay's 1 s sample.
  for _ in 0..24 {
    shell.run_for(Duration::from_millis(50));
  }
  assert_eq!(
    shell.frames().len(),
    frames_before,
    "the overlay's samples draw nothing while the window is minimized"
  );
}

#[test]
fn moving_the_window_reads_its_refresh_rate_again() {
  let mut app = App::new();
  let mut tree = spinner(Easing::Linear);
  tree.pass_headless(&mut app);
  let mut pacing = FramePacing::default();
  let mut reads = 0;
  let mut request = |pacing: &mut FramePacing, tree: &mut Tree| {
    pacing.request_frame(
      tree,
      || {
        reads += 1;
        REFRESH
      },
      Instant::now(),
    );
  };

  request(&mut pacing, &mut tree);
  request(&mut pacing, &mut tree);
  // Another display may refresh at another rate.
  pacing.handle_event(&WindowEvent::Moved(PhysicalPosition::new(1920, 0)));
  request(&mut pacing, &mut tree);
  assert_eq!(reads, 2, "the rate is read once, then again when the window moves");
}

/// A FIFO swapchain of frame latency one on a virtual clock that starts at `epoch` on a vblank: when each of
/// `count` frames is presented, for a pass that works `cost` before it asks the swapchain for a buffer and as long
/// again after, and a loop that wakes up to 1 ms late. `changes` gives the timeline's next change after a pass that
/// sampled it at a time.
fn virtual_presents(
  epoch: Instant,
  refresh: Duration,
  cost: Duration,
  count: usize,
  changes: impl Fn(Instant) -> Instant,
) -> Vec<Instant> {
  let vblank_after = |at: Instant| {
    let refreshes = vblank_index(epoch, refresh, at);
    epoch + refresh * u32::try_from(refreshes).expect("a short simulation")
  };
  let mut seed = 0x2545_f491_4f6c_dd1d_u64;
  let mut wake_latency = || {
    seed = seed
      .wrapping_mul(6_364_136_223_846_793_005)
      .wrapping_add(1_442_695_040_888_963_407);
    Duration::from_micros((seed >> 33) % 1000)
  };
  let mut now = epoch;
  let mut last: Option<PresentedFrame> = None;
  let mut presents: Vec<Instant> = Vec::with_capacity(count);
  while presents.len() < count {
    if let Some(frame) = last {
      now = now.max(frame_due(changes(frame.started), last, refresh)) + wake_latency();
    }
    let started = now;
    now += cost;
    if let Some(previous) = presents.last() {
      now = now.max(vblank_after(*previous));
    }
    now += cost;
    presents.push(now);
    last = Some(PresentedFrame {
      started,
      presented: now,
    });
  }
  presents
}

#[test]
fn on_a_virtual_clock_continuous_frames_take_every_vblank() {
  let cases = [(REFRESH, [0, 1000, 2000, 3000]), (REFRESH / 2, [0, 500, 1000, 1500])];
  for (refresh, costs_us) in cases {
    for cost in costs_us.map(Duration::from_micros) {
      // A continuous animation changes at once: its next change is the time its last pass sampled it.
      let epoch = Instant::now();
      let presents = virtual_presents(epoch, refresh, cost / 2, 600, |sampled| sampled);
      let vblanks: Vec<u128> = presents
        .iter()
        .map(|present| vblank_index(epoch, refresh, *present))
        .collect();
      assert_eq!(
        skipped_vblanks(&vblanks[1..]),
        0,
        "at {refresh:?} a refresh and {cost:?} of work per frame, every vblank shows a new frame"
      );
    }
  }
}

#[test]
fn on_a_virtual_clock_continuous_frames_take_every_vblank_at_144_hz() {
  let refresh = Duration::from_nanos(6_944_444);
  for cost_us in [2000, 2250, 2500] {
    let cost = Duration::from_micros(cost_us);
    let epoch = Instant::now();
    let presents = virtual_presents(epoch, refresh, cost / 2, 1200, |sampled| sampled);
    let vblanks: Vec<u128> = presents
      .iter()
      .map(|present| vblank_index(epoch, refresh, *present))
      .collect();
    assert_eq!(
      skipped_vblanks(&vblanks[1..]),
      0,
      "at 144 Hz and {cost:?} of work per frame, every vblank shows a new frame"
    );
  }
}

#[test]
fn on_a_virtual_clock_twelve_steps_draw_twelve_frames_a_second() {
  let step = Duration::from_secs(1) / 12;
  let epoch = Instant::now();
  let next_step = |sampled: Instant| {
    let steps = (sampled - epoch).as_nanos() / step.as_nanos() + 1;
    epoch + step * u32::try_from(steps).expect("a short simulation") + Duration::from_micros(100)
  };
  let presents = virtual_presents(epoch, REFRESH, Duration::from_millis(1), 40, next_step);
  let in_first_second = presents
    .iter()
    .filter(|present| **present - epoch < Duration::from_secs(1))
    .count();
  assert!(
    (12..=13).contains(&in_first_second),
    "a frame per step: {in_first_second} frames in the first second"
  );
}