medius 3.4.0

Host control library for the medius transparent mouse passthrough box
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
use crate::link::reconcile::DesiredState;
use crate::types::{Action, Button, Key, MediaKey, Usage};

#[test]
fn default_is_idle() {
    let d = DesiredState::default();
    assert!(d.is_idle());
    assert_eq!(d.held().count(), 0);
}

#[test]
fn press_is_held_and_non_idle() {
    let mut d = DesiredState::default();
    d.apply(Button::LEFT.into(), Action::Press);
    assert!(!d.is_idle());
    assert_eq!(
        d.held().collect::<Vec<_>>(),
        vec![(Usage::from(Button::LEFT), Action::Press)]
    );
}

#[test]
fn force_release_is_held() {
    let mut d = DesiredState::default();
    d.apply(Button::RIGHT.into(), Action::ForceRelease);
    assert!(!d.is_idle());
    assert_eq!(
        d.held().collect::<Vec<_>>(),
        vec![(Usage::from(Button::RIGHT), Action::ForceRelease)]
    );
}

#[test]
fn soft_release_clears_the_override() {
    let mut d = DesiredState::default();
    d.apply(Button::MIDDLE.into(), Action::Press);
    d.apply(Button::MIDDLE.into(), Action::SoftRelease);
    assert!(d.is_idle());
}

#[test]
fn clear_resets_all() {
    let mut d = DesiredState::default();
    d.apply(Button::LEFT.into(), Action::Press);
    d.apply(Button::SIDE2.into(), Action::ForceRelease);
    assert!(!d.is_idle());
    d.clear();
    assert!(d.is_idle());
    assert_eq!(d.held().count(), 0);
}

#[test]
fn held_preserves_identity_in_class_then_id_order() {
    let mut d = DesiredState::default();
    d.apply(Button::LEFT.into(), Action::Press);
    d.apply(Button::SIDE1.into(), Action::ForceRelease);
    assert_eq!(
        d.held().collect::<Vec<_>>(),
        vec![
            (Usage::from(Button::LEFT), Action::Press),
            (Usage::from(Button::SIDE1), Action::ForceRelease),
        ]
    );
}

#[test]
fn key_press_is_held_and_non_idle() {
    let mut d = DesiredState::default();
    d.apply(Key::A.into(), Action::Press);
    assert!(!d.is_idle());
    assert_eq!(
        d.held().collect::<Vec<_>>(),
        vec![(Usage::from(Key::A), Action::Press)]
    );
}

#[test]
fn key_soft_release_clears_the_override() {
    let mut d = DesiredState::default();
    d.apply(Key::LEFT_SHIFT.into(), Action::Press);
    d.apply(Key::LEFT_SHIFT.into(), Action::SoftRelease);
    assert!(d.is_idle());
    assert_eq!(d.held().count(), 0);
}

#[test]
fn media_press_is_held() {
    let mut d = DesiredState::default();
    d.apply(MediaKey::VOLUME_UP.into(), Action::Press);
    assert!(!d.is_idle());
    assert_eq!(
        d.held().collect::<Vec<_>>(),
        vec![(Usage::from(MediaKey::VOLUME_UP), Action::Press)]
    );
}

#[test]
fn media_soft_release_clears_the_override() {
    let mut d = DesiredState::default();
    d.apply(MediaKey::MUTE.into(), Action::Press);
    d.apply(MediaKey::MUTE.into(), Action::SoftRelease);
    assert!(d.is_idle());
}

#[test]
fn one_store_holds_every_class_and_orders_by_class_then_id() {
    let mut d = DesiredState::default();
    d.apply(MediaKey::VOLUME_UP.into(), Action::Press);
    d.apply(Button::LEFT.into(), Action::Press);
    d.apply(Key::A.into(), Action::Press);
    assert_eq!(
        d.held().map(|(u, _)| u).collect::<Vec<_>>(),
        vec![
            Usage::from(Button::LEFT),
            Usage::from(Key::A),
            Usage::from(MediaKey::VOLUME_UP),
        ]
    );
    d.clear();
    assert!(d.is_idle());
    assert_eq!(d.held().count(), 0);
}

#[test]
fn idle_requires_every_class_empty() {
    let mut d = DesiredState::default();
    d.apply(Key::ESCAPE.into(), Action::Press);
    assert!(!d.is_idle());
    d.apply(Key::ESCAPE.into(), Action::SoftRelease);
    assert!(d.is_idle());
    d.apply(MediaKey::PLAY_PAUSE.into(), Action::ForceRelease);
    assert!(!d.is_idle());
}

// The lock half. The keys are wire triples (class, id, direction), the shape a reapply re-sends.
use crate::protocol::opcode::{
    LOCK_AXIS_X, LOCK_CLS_AXIS, LOCK_CLS_BTN, LOCK_CLS_KEY, LOCK_DIR_AGAINST, LOCK_DIR_BOTH,
    LOCK_DIR_NEG, LOCK_DIR_POS, LOCK_DIR_WITH, LOCK_ID_ALL, LOCK_SCALE_BLOCK, LOCK_SCALE_PASS,
};

const X: (u8, u16) = (LOCK_CLS_AXIS, LOCK_AXIS_X);

#[test]
fn releasing_one_sign_of_a_both_lock_leaves_the_other() {
    let mut d = DesiredState::default();
    d.apply_lock((X.0, X.1, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    d.apply_lock((X.0, X.1, LOCK_DIR_NEG), LOCK_SCALE_PASS);
    assert_eq!(
        d.held_locks(),
        vec![((X.0, X.1, LOCK_DIR_POS), LOCK_SCALE_BLOCK)]
    );
    d.apply_lock((X.0, X.1, LOCK_DIR_POS), LOCK_SCALE_PASS);
    assert!(d.is_idle());
}

#[test]
fn a_both_write_replaces_the_single_directions_before_it() {
    let mut d = DesiredState::default();
    d.apply_lock((X.0, X.1, LOCK_DIR_NEG), 40);
    d.apply_lock((X.0, X.1, LOCK_DIR_AGAINST), 30);
    d.apply_lock((X.0, X.1, LOCK_DIR_BOTH), 50);
    // Both is the whole row: the fixed pair takes the scale and the relative pair goes back to
    // passing, so one command rebuilds it.
    assert_eq!(d.held_locks(), vec![((X.0, X.1, LOCK_DIR_BOTH), 50)]);
}

#[test]
fn a_relative_scale_survives_an_absolute_one() {
    let mut d = DesiredState::default();
    d.apply_lock((X.0, X.1, LOCK_DIR_AGAINST), 40);
    d.apply_lock((X.0, X.1, LOCK_DIR_POS), 60);
    assert_eq!(
        d.held_locks(),
        vec![
            ((X.0, X.1, LOCK_DIR_POS), 60),
            ((X.0, X.1, LOCK_DIR_AGAINST), 40),
        ]
    );
}

#[test]
fn a_full_unlock_clears_every_slot() {
    let mut d = DesiredState::default();
    d.apply_lock((X.0, X.1, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    d.apply_lock((X.0, X.1, LOCK_DIR_WITH), 30);
    assert!(!d.is_idle());
    d.apply_lock((X.0, X.1, LOCK_DIR_BOTH), LOCK_SCALE_PASS);
    assert!(d.is_idle());
    assert_eq!(d.held_locks(), vec![]);
}

#[test]
fn a_one_bit_class_holds_what_the_box_will_hold() {
    let mut d = DesiredState::default();
    // 150% on a button is an unlock on the box: it truncates to a pass. Held as 150 the keepalive
    // would stay open for a lock that does not exist.
    d.apply_lock((LOCK_CLS_BTN, 0, LOCK_DIR_POS), 150);
    assert!(d.is_idle());
    d.apply_lock((LOCK_CLS_BTN, 0, LOCK_DIR_POS), 50);
    assert_eq!(
        d.held_locks(),
        vec![((LOCK_CLS_BTN, 0, LOCK_DIR_POS), LOCK_SCALE_BLOCK)]
    );
    // An axis keeps the number itself.
    d.apply_lock((X.0, X.1, LOCK_DIR_POS), 150);
    assert!(d.held_locks().contains(&((X.0, X.1, LOCK_DIR_POS), 150)));
}

#[test]
fn a_button_blanket_widens_to_the_declared_count_when_caps_arrives() {
    let mut d = DesiredState::default();
    d.apply_lock((LOCK_CLS_BTN, LOCK_ID_ALL, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    // Before any CAPS read the blanket expands onto the five named buttons.
    assert_eq!(d.held_locks().len(), 5);
    // The blanket is held unexpanded, so once CAPS reports a wider device the SAME blanket re-expands
    // onto every declared button: a reconnect that re-read CAPS re-asserts the wide buttons instead of
    // the frozen five. Materialising at apply time (the old behaviour) could not do this.
    d.note_declared_buttons(8);
    let ids: Vec<u16> = d.held_locks().iter().map(|&((_, id, _), _)| id).collect();
    assert_eq!(ids, (0..8).collect::<Vec<u16>>());
    // Releasing one button afterwards must not be undone by a replay of the blanket.
    d.apply_lock((LOCK_CLS_BTN, 0, LOCK_DIR_BOTH), LOCK_SCALE_PASS);
    let held = d.held_locks();
    assert_eq!(held.len(), 7);
    assert!(!held.iter().any(|&((_, id, _), _)| id == 0));
}

#[test]
fn a_button_blanket_re_expands_when_the_declared_count_changes() {
    // A device swapped in during a reconnect blip re-reads CAPS, and the held blanket then re-asserts
    // onto the new count, wider or narrower, because it was never materialised at the old one.
    let mut d = DesiredState::default();
    d.note_declared_buttons(8);
    d.apply_lock((LOCK_CLS_BTN, LOCK_ID_ALL, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    assert_eq!(d.held_locks().len(), 8);
    d.note_declared_buttons(12);
    assert_eq!(d.held_locks().len(), 12);
    d.note_declared_buttons(4);
    assert_eq!(d.held_locks().len(), 4);
}

#[test]
fn an_undone_button_release_restores_the_blanket() {
    // The single-button write that bursts the blanket rolls back cleanly when its frame never went out,
    // leaving the blanket as it was rather than the materialised rows the burst created.
    let mut d = DesiredState::default();
    d.note_declared_buttons(8);
    d.apply_lock((LOCK_CLS_BTN, LOCK_ID_ALL, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    let before = d.held_locks();
    let undo = d.apply_lock((LOCK_CLS_BTN, 0, LOCK_DIR_BOTH), LOCK_SCALE_PASS);
    assert_eq!(d.held_locks().len(), 7); // burst to eight, minus the released button
    d.restore_lock(undo);
    assert_eq!(d.held_locks(), before);
}

#[test]
fn a_button_blanket_expands_onto_the_declared_count() {
    // Once CAPS reports a wide button count, the blanket expands onto every declared button, so a
    // reconnect re-asserts a lock on a button past the five named ones.
    let mut d = DesiredState::default();
    d.note_declared_buttons(16);
    d.apply_lock((LOCK_CLS_BTN, LOCK_ID_ALL, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    let ids: Vec<u16> = d.held_locks().iter().map(|&((_, id, _), _)| id).collect();
    assert_eq!(ids, (0..16).collect::<Vec<u16>>());
    assert!(
        d.held_locks()
            .iter()
            .any(|&((cls, id, _), _)| cls == LOCK_CLS_BTN && id == 8)
    );
}

#[test]
fn a_button_blanket_caps_at_the_box_ceiling() {
    // A device declaring more buttons than the box can drive still expands onto only the ceiling.
    let mut d = DesiredState::default();
    d.note_declared_buttons(40);
    d.apply_lock((LOCK_CLS_BTN, LOCK_ID_ALL, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    assert_eq!(d.held_locks().len(), 16);
}

#[test]
fn a_key_blanket_is_its_own_row() {
    // The box holds a key blanket as its own flag rather than expanding it over 256 usages, so it
    // stays one row here and reapplies as one command.
    let mut d = DesiredState::default();
    d.apply_lock((LOCK_CLS_KEY, LOCK_ID_ALL, LOCK_DIR_POS), LOCK_SCALE_BLOCK);
    assert_eq!(
        d.held_locks(),
        vec![((LOCK_CLS_KEY, LOCK_ID_ALL, LOCK_DIR_POS), LOCK_SCALE_BLOCK)]
    );
}

// The box keeps granular media locks in a fixed 8-slot array filled first-free-slot-first
// (input_core.c media_set, INPUT_MEDIA_MAX = 8), so what a replay has to reproduce is the order they
// were taken in, not their ids.
use crate::protocol::opcode::LOCK_CLS_MEDIA;

fn media_ids(d: &DesiredState) -> Vec<u16> {
    d.held_locks()
        .iter()
        .filter(|&&((class, _, _), _)| class == LOCK_CLS_MEDIA)
        .map(|&((_, id, _), _)| id)
        .collect()
}

#[test]
fn media_locks_replay_in_the_order_they_were_taken() {
    let mut d = DesiredState::default();
    // Nine usages in an order id-sorting would not produce: MUTE, VOL_UP, VOL_DOWN come back in that
    // order only if the take order is what is remembered.
    let taken = [0x223u16, 0x30, 0xB5, 0xE9, 0xB6, 0xCD, 0xE2, 0xB7, 0xEA];
    for id in taken {
        d.apply_lock((LOCK_CLS_MEDIA, id, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    }
    // Past the box's eight slots the ninth taken is what falls off, here and on the box alike; by id
    // the ninth would be 0x223 and the box would still be dropping 0xEA.
    assert_eq!(media_ids(&d), taken);
}

#[test]
fn a_released_media_lock_leaves_the_order_of_the_rest() {
    let mut d = DesiredState::default();
    for id in [0xEAu16, 0xE9, 0x30] {
        d.apply_lock((LOCK_CLS_MEDIA, id, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    }
    d.apply_lock((LOCK_CLS_MEDIA, 0xE9, LOCK_DIR_BOTH), LOCK_SCALE_PASS);
    d.apply_lock((LOCK_CLS_MEDIA, 0xB5, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    assert_eq!(media_ids(&d), vec![0xEA, 0x30, 0xB5]);
    // Re-taking one already held keeps its place rather than moving it to the end.
    d.apply_lock((LOCK_CLS_MEDIA, 0xEA, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    assert_eq!(media_ids(&d), vec![0xEA, 0x30, 0xB5]);
}

#[test]
fn a_media_blanket_does_not_take_a_slot() {
    // The blanket is its own flag on the box (lock_all_media), not an entry in the slot array.
    let mut d = DesiredState::default();
    d.apply_lock((LOCK_CLS_MEDIA, 0xEA, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    d.apply_lock(
        (LOCK_CLS_MEDIA, LOCK_ID_ALL, LOCK_DIR_BOTH),
        LOCK_SCALE_BLOCK,
    );
    d.apply_lock((LOCK_CLS_MEDIA, 0x30, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    assert_eq!(media_ids(&d), vec![0xEA, 0x30, LOCK_ID_ALL]);
}

#[test]
fn every_other_class_still_replays_in_id_order() {
    let mut d = DesiredState::default();
    for id in [3u16, 0, 1] {
        d.apply_lock((LOCK_CLS_BTN, id, LOCK_DIR_POS), LOCK_SCALE_BLOCK);
    }
    assert_eq!(
        d.held_locks()
            .iter()
            .map(|&((_, id, _), _)| id)
            .collect::<Vec<_>>(),
        vec![0, 1, 3]
    );
}

#[test]
fn an_undone_apply_leaves_the_state_exactly_as_it_was() {
    let mut d = DesiredState::default();
    d.apply_lock((X.0, X.1, LOCK_DIR_BOTH), 40);
    d.apply_lock((LOCK_CLS_MEDIA, 0xEA, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    let before = (d.held_locks(), d.is_idle());

    let undo = d.apply_lock((X.0, X.1, LOCK_DIR_AGAINST), LOCK_SCALE_BLOCK);
    d.restore_lock(undo);
    assert_eq!((d.held_locks(), d.is_idle()), before);

    // Including the case that took a fresh row: undoing it must leave nothing behind, blanket
    // expansion and media order alike.
    let undo = d.apply_lock((LOCK_CLS_BTN, LOCK_ID_ALL, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    d.restore_lock(undo);
    let undo = d.apply_lock((LOCK_CLS_MEDIA, 0x30, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    d.restore_lock(undo);
    assert_eq!((d.held_locks(), d.is_idle()), before);

    // And the case that cleared a row: undoing an unlock puts the lock back.
    let undo = d.apply_lock((X.0, X.1, LOCK_DIR_BOTH), LOCK_SCALE_PASS);
    d.restore_lock(undo);
    assert_eq!((d.held_locks(), d.is_idle()), before);
}

#[test]
fn a_row_of_another_class_never_disturbs_the_media_order() {
    // The classes share an id space: media usage 3 and button 3 are different rows, and releasing
    // one must not move the other in the replay.
    let mut d = DesiredState::default();
    d.apply_lock((LOCK_CLS_MEDIA, 3, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    d.apply_lock((LOCK_CLS_MEDIA, 0xEA, LOCK_DIR_BOTH), LOCK_SCALE_BLOCK);
    d.apply_lock((LOCK_CLS_BTN, 3, LOCK_DIR_POS), LOCK_SCALE_BLOCK);
    d.apply_lock((LOCK_CLS_BTN, 3, LOCK_DIR_POS), LOCK_SCALE_PASS);
    assert_eq!(media_ids(&d), vec![3, 0xEA]);
}

// --- rewrite rules (ยง3.14): session state re-asserted like locks/catch ---

use crate::link::reconcile::StoredRewrite;

fn stored(class: u8, id: u16) -> StoredRewrite {
    StoredRewrite {
        class,
        id,
        direction: 0,
        action: 1,
        offset: 0,
        match_bytes: vec![],
        mask: vec![],
        payload: vec![],
    }
}

#[test]
fn rewrite_rule_is_held_and_non_idle() {
    let mut d = DesiredState::default();
    d.apply_rewrite(stored(9, 1));
    assert!(!d.is_idle());
    assert_eq!(d.held_rewrites().len(), 1);
}

#[test]
fn rewrite_overwrite_keeps_one_row() {
    let mut d = DesiredState::default();
    d.apply_rewrite(stored(9, 1));
    let mut r = stored(9, 1);
    r.action = 3; // same key, new action
    d.apply_rewrite(r);
    let held = d.held_rewrites();
    assert_eq!(held.len(), 1);
    assert_eq!(held[0].action, 3);
}

#[test]
fn rewrite_remove_and_restore() {
    let mut d = DesiredState::default();
    d.apply_rewrite(stored(9, 1));
    let key = stored(9, 1).key();
    let undo = d.remove_rewrite(key);
    assert!(d.held_rewrites().is_empty());
    d.restore_rewrite(undo); // a send that never went out is rolled back
    assert_eq!(d.held_rewrites().len(), 1);
}

#[test]
fn rewrite_apply_undo_puts_it_back() {
    let mut d = DesiredState::default();
    let undo = d.apply_rewrite(stored(9, 1));
    d.restore_rewrite(undo);
    assert!(d.held_rewrites().is_empty());
    assert!(d.is_idle());
}

#[test]
fn clear_rewrites_empties_the_table() {
    let mut d = DesiredState::default();
    d.apply_rewrite(stored(9, 1));
    d.apply_rewrite(stored(4, 0));
    d.clear_rewrites();
    assert!(d.held_rewrites().is_empty());
}

#[test]
fn reset_clears_rewrites_too() {
    let mut d = DesiredState::default();
    d.apply_rewrite(stored(9, 1));
    d.clear(); // the RESET path
    assert!(d.held_rewrites().is_empty());
    assert!(d.is_idle());
}

// --- field transforms (ยง3.15): session state re-asserted like locks/rewrites ---

use crate::link::reconcile::StoredTransform;

fn stored_xf(sclass: u8, sid: u16, dclass: u8, did: u16) -> StoredTransform {
    StoredTransform {
        op: 0, // Remap
        sclass,
        sid,
        dclass,
        did,
    }
}

#[test]
fn transform_is_held_and_non_idle() {
    let mut d = DesiredState::default();
    d.apply_transform(stored_xf(3, 1, 3, 1));
    assert!(!d.is_idle());
    assert_eq!(d.held_transforms().len(), 1);
}

#[test]
fn transform_overwrite_keeps_one_row() {
    let mut d = DesiredState::default();
    d.apply_transform(stored_xf(3, 1, 3, 1));
    let mut r = stored_xf(3, 1, 3, 1);
    r.op = 1; // same key (source, dest), new op
    d.apply_transform(r);
    let held = d.held_transforms();
    assert_eq!(held.len(), 1);
    assert_eq!(held[0].op, 1);
}

#[test]
fn transform_remove_and_restore() {
    let mut d = DesiredState::default();
    d.apply_transform(stored_xf(3, 1, 3, 1));
    let key = stored_xf(3, 1, 3, 1).key();
    let undo = d.remove_transform(key);
    assert!(d.held_transforms().is_empty());
    d.restore_transform(undo); // a send that never went out is rolled back
    assert_eq!(d.held_transforms().len(), 1);
}

#[test]
fn transform_apply_undo_puts_it_back() {
    let mut d = DesiredState::default();
    let undo = d.apply_transform(stored_xf(3, 1, 3, 1));
    d.restore_transform(undo);
    assert!(d.held_transforms().is_empty());
    assert!(d.is_idle());
}

#[test]
fn clear_transforms_empties_the_table() {
    let mut d = DesiredState::default();
    d.apply_transform(stored_xf(3, 0, 3, 0));
    d.apply_transform(stored_xf(3, 1, 3, 1));
    d.clear_transforms();
    assert!(d.held_transforms().is_empty());
}

#[test]
fn reset_clears_transforms_too() {
    let mut d = DesiredState::default();
    d.apply_transform(stored_xf(3, 1, 3, 1));
    d.clear(); // the RESET path
    assert!(d.held_transforms().is_empty());
    assert!(d.is_idle());
}