openlogi-core 0.8.14

Core types, config, and paths for OpenLogi. No I/O specifics.
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Schema migrations, v1 through the current version: bindings, thumb-wheel pairs, gesture owners, direct-route keys and host-switch targets.

use super::*;

#[test]
fn migrates_v1_button_and_gesture_bindings() {
    // A pre-v2 file: split button_bindings + a flat gesture_bindings map.
    let v1 = "\
schema_version = 1

[devices.2b042.button_bindings]
Back = \"BrowserBack\"

[devices.2b042.gesture_bindings]
Up = \"Copy\"
Click = \"Paste\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, v1).expect("write");

    // v1 still loads (version <= current) and folds into the merged map.
    let cfg = Config::load_from_path(&path).expect("load v1");
    let bindings = cfg.stored_bindings("2b042");
    assert_eq!(
        bindings.get(&ButtonId::Back),
        Some(&Binding::Single(Action::BrowserBack))
    );
    let mut gesture = BTreeMap::new();
    gesture.insert(GestureDirection::Up, Action::Copy);
    gesture.insert(GestureDirection::Click, Action::Paste);
    assert_eq!(
        bindings.get(&ButtonId::GestureButton),
        Some(&Binding::Gesture(gesture))
    );

    // Saving self-heals to the current shape: stamped version + merged table,
    // legacy field names gone.
    let body = toml::to_string_pretty(&cfg).expect("serialize");
    assert!(
        body.contains(&format!("schema_version = {SCHEMA_VERSION}")),
        "got: {body}"
    );
    assert!(body.contains("[devices.2b042.bindings]"), "got: {body}");
    assert!(!body.contains("button_bindings"), "got: {body}");
    assert!(!body.contains("gesture_bindings"), "got: {body}");
}

#[test]
fn migrates_pre_v7_thumbwheel_defaults_to_normalised_native_direction() {
    let v6 = "\
schema_version = 6

[devices.\"unit:6be9d300\".bindings]
ThumbwheelScrollUp = \"HorizontalScrollRight\"
ThumbwheelScrollDown = \"HorizontalScrollLeft\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, v6).expect("write");

    let cfg = Config::load_from_path(&path).expect("load v6");
    let bindings = cfg.stored_bindings("unit:6be9d300");
    assert_eq!(
        bindings.get(&ButtonId::ThumbwheelScrollUp),
        Some(&Binding::Single(Action::HorizontalScrollLeft))
    );
    assert_eq!(
        bindings.get(&ButtonId::ThumbwheelScrollDown),
        Some(&Binding::Single(Action::HorizontalScrollRight))
    );
}

#[test]
fn migrates_a_pre_v7_per_app_pair_that_was_effectively_default() {
    // The app overrides only one half; the other old default comes from a
    // custom global pair. The migration must materialise both new defaults in
    // the app so it remains native without changing the custom global profile.
    let v6 = "\
schema_version = 6

[devices.\"unit:6be9d300\".bindings]
ThumbwheelScrollUp = \"NextTab\"
ThumbwheelScrollDown = \"HorizontalScrollLeft\"

[devices.\"unit:6be9d300\".per_app_bindings.\"com.example.Editor\"]
ThumbwheelScrollUp = \"HorizontalScrollRight\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, v6).expect("write");

    let cfg = Config::load_from_path(&path).expect("load v6");
    let app = cfg.effective_bindings("unit:6be9d300", Some("com.example.Editor"));
    assert_eq!(
        app.get(&ButtonId::ThumbwheelScrollUp),
        Some(&Binding::Single(Action::HorizontalScrollLeft))
    );
    assert_eq!(
        app.get(&ButtonId::ThumbwheelScrollDown),
        Some(&Binding::Single(Action::HorizontalScrollRight))
    );
    assert_eq!(
        cfg.stored_bindings("unit:6be9d300")
            .get(&ButtonId::ThumbwheelScrollUp),
        Some(&Binding::Single(Action::NextTab)),
        "the custom global pair must stay literal"
    );
}

#[test]
fn pre_v7_thumbwheel_migration_leaves_a_custom_pair_unchanged() {
    let v6 = "\
schema_version = 6

[devices.\"unit:6be9d300\".bindings]
ThumbwheelScrollUp = \"HorizontalScrollRight\"
ThumbwheelScrollDown = \"NextTab\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, v6).expect("write");

    let cfg = Config::load_from_path(&path).expect("load v6");
    let bindings = cfg.stored_bindings("unit:6be9d300");
    assert_eq!(
        bindings.get(&ButtonId::ThumbwheelScrollUp),
        Some(&Binding::Single(Action::HorizontalScrollRight))
    );
    assert_eq!(
        bindings.get(&ButtonId::ThumbwheelScrollDown),
        Some(&Binding::Single(Action::NextTab))
    );
}

#[test]
fn pre_v7_thumbwheel_migration_preserves_non_single_binding_shapes() {
    let v6 = "\
schema_version = 6

[devices.\"unit:6be9d300\".bindings]
ThumbwheelScrollUp = { short = \"HorizontalScrollRight\", long = \"NextTab\" }
ThumbwheelScrollDown = \"HorizontalScrollLeft\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, v6).expect("write");

    let cfg = Config::load_from_path(&path).expect("load v6");
    let bindings = cfg.stored_bindings("unit:6be9d300");
    let Some(Binding::LongPress(up)) = bindings.get(&ButtonId::ThumbwheelScrollUp) else {
        panic!("custom long-press binding must keep its shape");
    };
    assert_eq!(up.short(), &Action::HorizontalScrollRight);
    assert_eq!(up.long(), &Action::NextTab);
    assert_eq!(
        bindings.get(&ButtonId::ThumbwheelScrollDown),
        Some(&Binding::Single(Action::HorizontalScrollLeft))
    );
}

#[test]
fn v7_reversed_thumbwheel_pair_survives_reload() {
    let v7 = "\
schema_version = 7

[devices.\"unit:6be9d300\".bindings]
ThumbwheelScrollUp = \"HorizontalScrollRight\"
ThumbwheelScrollDown = \"HorizontalScrollLeft\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, v7).expect("write");

    let cfg = Config::load_from_path(&path).expect("load v7");
    let bindings = cfg.stored_bindings("unit:6be9d300");
    assert_eq!(
        bindings.get(&ButtonId::ThumbwheelScrollUp),
        Some(&Binding::Single(Action::HorizontalScrollRight))
    );
    assert_eq!(
        bindings.get(&ButtonId::ThumbwheelScrollDown),
        Some(&Binding::Single(Action::HorizontalScrollLeft))
    );
}

#[test]
fn migration_gesture_map_wins_over_legacy_single_gesture_button_entry() {
    // The data-loss guard: when a legacy single button_bindings[GestureButton]
    // entry coexists with a gesture_bindings map (reachable via hand-edited
    // or very old configs), the gesture map must survive — not be shadowed by
    // the single entry. Mirrors the pre-v2 "gesture entries win" rule.
    let v1 = "\
schema_version = 1

[devices.2b042.button_bindings]
GestureButton = \"MissionControl\"

[devices.2b042.gesture_bindings]
Up = \"Copy\"
Down = \"Paste\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, v1).expect("write");

    let cfg = Config::load_from_path(&path).expect("load v1");
    let mut gesture = BTreeMap::new();
    gesture.insert(GestureDirection::Up, Action::Copy);
    gesture.insert(GestureDirection::Down, Action::Paste);
    assert_eq!(
        cfg.stored_bindings("2b042").get(&ButtonId::GestureButton),
        Some(&Binding::Gesture(gesture)),
        "gesture map must win over the legacy single GestureButton entry"
    );
}

#[test]
fn migration_drops_vestigial_lone_gesture_button_single() {
    // A v1 file with only `button_bindings[GestureButton]` and no
    // `gesture_bindings` (the pre-gesture-picker shape). That entry never
    // dispatched in v1 — the gesture button's plain press routes through the
    // gesture `Click` slot, not the per-button map — so migrating it to a
    // `Binding::Single` would leave an unreachable entry the GUI hides and the
    // runtime ignores. It must be dropped, not shadow the gesture path.
    let v1 = "\
schema_version = 1

[devices.2b042.button_bindings]
GestureButton = \"MissionControl\"
Back = \"BrowserBack\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, v1).expect("write");

    let bindings = Config::load_from_path(&path)
        .expect("load v1")
        .stored_bindings("2b042");
    // An ordinary button still migrates to a `Single`...
    assert_eq!(
        bindings.get(&ButtonId::Back),
        Some(&Binding::Single(Action::BrowserBack))
    );
    // ...but the vestigial gesture-button single is gone, leaving the button
    // to fall back to its canonical default rather than an unreachable entry.
    assert_eq!(bindings.get(&ButtonId::GestureButton), None);
}

#[test]
fn migration_demotes_the_dormant_non_owner_gesture_maps() {
    // An owner-locked config: Middle owns gestures; the dedicated button
    // keeps a dormant map from an earlier reign. Shape-driven load keeps the
    // owner gesturing and flattens the dormant map to its Click, so a stored
    // map's presence always MEANS gesture mode.
    let toml = "\
schema_version = 3

[devices.2b042]
gesture_owner = \"MiddleClick\"

[devices.2b042.bindings]
GestureButton = { Up = \"Copy\", Click = \"Paste\" }
MiddleClick = { Up = \"MissionControl\", Click = \"MiddleClick\" }
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, toml).expect("write");

    let cfg = Config::load_from_path(&path).expect("load");
    assert!(cfg.is_gesture_mode("2b042", ButtonId::MiddleClick));
    assert!(!cfg.is_gesture_mode("2b042", ButtonId::GestureButton));
    assert_eq!(
        cfg.stored_bindings("2b042").get(&ButtonId::GestureButton),
        Some(&Binding::Single(Action::Paste)),
        "the dormant map demotes to its Click choice"
    );
    // The legacy field is gone on save — the binding shape is the whole truth.
    let body = toml::to_string_pretty(&cfg).expect("serialize");
    assert!(!body.contains("gesture_owner"), "got: {body}");
}

#[test]
fn migration_off_pins_the_dedicated_button_out_of_gesture_mode() {
    // gesture_owner = "Off" with no stored bindings: absence would re-enter
    // default gesture mode under shape rules, so the load pins the dedicated
    // button with an explicit Single at its canonical default (which the
    // capture layer treats as native/undiverted).
    let toml = "\
schema_version = 3

[devices.2b042]
gesture_owner = \"Off\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, toml).expect("write");

    let cfg = Config::load_from_path(&path).expect("load");
    assert!(!cfg.is_gesture_mode("2b042", ButtonId::GestureButton));
    assert!(cfg.gesture_mode_buttons("2b042").is_empty());
    assert_eq!(
        cfg.stored_bindings("2b042").get(&ButtonId::GestureButton),
        Some(&Binding::Single(default_binding(ButtonId::GestureButton)))
    );
}

#[test]
fn migration_materializes_a_hidpp_owners_missing_map() {
    // A v3 HID++ owner dispatched the seeded default direction map
    // regardless of its stored shape (the runtime seeded at projection
    // time), so an owner with no stored map must not lose gestures when
    // the file is rewritten to the current schema.
    let toml = "\
schema_version = 3

[devices.2b042]
gesture_owner = \"HapticPanel\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, toml).expect("write");

    let cfg = Config::load_from_path(&path).expect("load");
    assert!(cfg.is_gesture_mode("2b042", ButtonId::HapticPanel));
    match cfg.stored_bindings("2b042").get(&ButtonId::HapticPanel) {
        Some(Binding::Gesture(map)) => {
            for dir in GestureDirection::ALL {
                assert_eq!(map.get(&dir), Some(&default_gesture_binding(dir)));
            }
        }
        other => panic!("expected the owner's materialized default map, got {other:?}"),
    }
    // The non-owner dedicated button is still pinned off.
    assert!(!cfg.is_gesture_mode("2b042", ButtonId::GestureButton));
}

#[test]
fn migration_replaces_a_hidpp_owners_single_with_the_default_map() {
    // A hand-edited v3 file: the owner field says GestureButton but its
    // stored binding is a Single. The v3 runtime still dispatched the full
    // default direction map (seeded at projection), so the load must
    // materialize that map rather than keep the never-dispatched Single.
    let toml = "\
schema_version = 3

[devices.2b042]
gesture_owner = \"GestureButton\"

[devices.2b042.bindings]
GestureButton = \"CycleDpiPresets\"
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, toml).expect("write");

    let cfg = Config::load_from_path(&path).expect("load");
    assert!(cfg.is_gesture_mode("2b042", ButtonId::GestureButton));
    match cfg.stored_bindings("2b042").get(&ButtonId::GestureButton) {
        Some(Binding::Gesture(map)) => {
            assert_eq!(
                map.get(&GestureDirection::Click),
                Some(&default_gesture_binding(GestureDirection::Click)),
                "v3 dispatched the seeded default Click, not the stored Single"
            );
        }
        other => panic!("expected the owner's materialized default map, got {other:?}"),
    }
}

#[test]
fn migration_stashes_dormant_maps_for_re_enabling() {
    // The owner-lock model preserved every dormant non-owner map for
    // restore-on-reselection. The migration keeps that promise: the demoted
    // map is stashed, and turning the button back on restores it.
    let toml = "\
schema_version = 3

[devices.2b042]
gesture_owner = \"MiddleClick\"

[devices.2b042.bindings]
GestureButton = { Up = \"Copy\", Click = \"Paste\" }
MiddleClick = { Up = \"MissionControl\", Click = \"MiddleClick\" }
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, toml).expect("write");

    let mut cfg = Config::load_from_path(&path).expect("load");
    assert!(!cfg.is_gesture_mode("2b042", ButtonId::GestureButton));

    cfg.set_gesture_mode("2b042", ButtonId::GestureButton, true);
    match cfg.stored_bindings("2b042").get(&ButtonId::GestureButton) {
        Some(Binding::Gesture(map)) => {
            assert_eq!(
                map.get(&GestureDirection::Up),
                Some(&Action::Copy),
                "the dormant map's arms come back on re-enable"
            );
        }
        other => panic!("expected the stashed dormant map restored, got {other:?}"),
    }
}

#[test]
fn migration_infers_the_owner_for_pre_field_configs() {
    // Pre-owner-field file: a gesture-shaped OS-hook button was the owner by
    // inference, silencing the dedicated button. The load preserves exactly
    // that: Back keeps gesturing, the dedicated button is pinned off.
    let toml = "\
schema_version = 2

[devices.2b042.bindings]
Back = { Up = \"Copy\" }
";
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, toml).expect("write");

    let cfg = Config::load_from_path(&path).expect("load");
    assert!(cfg.is_gesture_mode("2b042", ButtonId::Back));
    assert!(!cfg.is_gesture_mode("2b042", ButtonId::GestureButton));
    assert_eq!(
        cfg.stored_bindings("2b042").get(&ButtonId::GestureButton),
        Some(&Binding::Single(default_binding(ButtonId::GestureButton)))
    );
}

#[test]
fn migrating_v4_drops_the_transport_prefix_from_direct_keys() {
    let source = r#"
schema_version = 4
selected_device = "direct:046d:c08d:unit:6be9d300"

[devices."direct:046d:c08d:unit:6be9d300"]
invert_scroll = true

[devices."receiver:82839805:slot:1"]
dpi = 1600
"#;
    let mut config: Config = toml::from_str(source).expect("parses");
    config.migrate_transport_scoped_keys();

    assert!(config.devices.contains_key("unit:6be9d300"));
    assert!(
        !config
            .devices
            .contains_key("direct:046d:c08d:unit:6be9d300")
    );
    assert_eq!(
        config.selected_device.as_deref(),
        Some("unit:6be9d300"),
        "the selection follows the key"
    );
    assert!(
        config.devices["unit:6be9d300"]
            .links
            .contains_key("direct:046d:c08d"),
        "the route it came from is remembered, not discarded"
    );
    assert!(
        config.devices.contains_key("receiver:82839805:slot:1"),
        "receiver entries are left for runtime adoption"
    );
}

#[test]
fn migrating_two_direct_routes_of_one_device_folds_instead_of_dropping_one() {
    // An MX Master 3S reached over USB *and* over Bluetooth-direct has two v4
    // entries whose keys both rename to `unit:6be9d300`. Inserting would let
    // whichever came later in `BTreeMap` order silently delete the other's
    // bindings, DPI and lighting — the one case where phase A of the
    // migration is neither mechanical nor lossless.
    let source = r#"
schema_version = 4

[devices."direct:046d:b034:unit:6be9d300"]
dpi = 1600
invert_scroll = true

[devices."direct:046d:b034:unit:6be9d300".bindings]
Back = "BrowserBack"

[devices."direct:046d:c08d:unit:6be9d300"]
dpi = 800

[devices."direct:046d:c08d:unit:6be9d300".bindings]
Forward = "BrowserForward"
"#;
    let mut config: Config = toml::from_str(source).expect("parses");
    config.migrate_transport_scoped_keys();

    assert_eq!(config.devices.len(), 1, "one device, one entry");
    let device = &config.devices["unit:6be9d300"];
    assert_eq!(
        device.bindings.len(),
        2,
        "both routes' bindings survive: {:?}",
        device.bindings
    );
    assert_eq!(
        device.dpi,
        Some(Dpi::new(1600)),
        "one value stays canonical"
    );
    assert_eq!(
        device.links["direct:046d:c08d"].overrides.dpi,
        Some(Dpi::new(800)),
        "the other survives as an override on the route it was set for"
    );
    assert!(
        device.links.contains_key("direct:046d:b034"),
        "both routes are indexed: {:?}",
        device.links
    );
}

#[test]
fn loading_hand_edited_duplicate_routes_keeps_the_first_device_key() {
    let source = r#"
schema_version = 6
selected_device = "unit:ffffffff"

[devices.keyboard]
host_switch_targets = ["unit:ffffffff"]

[devices."unit:11111111".links."receiver:82839805:slot:1".overrides]
dpi = 800

[devices."unit:ffffffff".links."receiver:82839805:slot:1".overrides]
dpi = 1600
"#;
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, source).expect("write hand-edited config");

    let config = Config::load_from_path(&path).expect("load");

    assert_eq!(
        config.devices["unit:11111111"].links["receiver:82839805:slot:1"]
            .overrides
            .dpi,
        Some(Dpi::new(800)),
        "the lexicographically first device key keeps its complete link"
    );
    assert!(
        !config.devices["unit:ffffffff"]
            .links
            .contains_key("receiver:82839805:slot:1"),
        "the later duplicate no longer indexes the route"
    );
    assert_eq!(config.selected_device.as_deref(), Some("unit:ffffffff"));
    assert_eq!(
        config.devices["keyboard"].host_switch_targets,
        vec!["unit:ffffffff".to_string()],
        "repairing an index does not rename the referenced device entry"
    );
}

#[test]
fn loading_v4_repairs_duplicate_routes_created_by_key_migration() {
    let source = r#"
schema_version = 4
selected_device = "direct:046d:c08d:unit:11111111"

[devices.keyboard]
host_switch_targets = ["direct:046d:c08d:unit:11111111"]

[devices."direct:046d:c08d:unit:11111111"]
dpi = 1600

[devices."unit:ffffffff".links."direct:046d:c08d".overrides]
dpi = 800
"#;
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.toml");
    fs::write(&path, source).expect("write v4 config");

    let config = Config::load_from_path(&path).expect("load and migrate");

    assert!(
        config.devices["unit:11111111"]
            .links
            .contains_key("direct:046d:c08d"),
        "the first post-migration device key owns the route"
    );
    assert!(
        !config.devices["unit:ffffffff"]
            .links
            .contains_key("direct:046d:c08d"),
        "the pre-existing duplicate is removed"
    );
    assert_eq!(
        config.selected_device.as_deref(),
        Some("unit:11111111"),
        "selection follows the migrated device key"
    );
    assert_eq!(
        config.devices["keyboard"].host_switch_targets,
        vec!["unit:11111111".to_string()],
        "host-switch references follow the migrated device key"
    );
}

#[test]
fn migrating_rewrites_host_switch_targets() {
    let source = r#"
schema_version = 4

[devices."receiver:82839805:slot:2"]
host_switch_targets = ["direct:046d:c08d:unit:6be9d300"]
"#;
    let mut config: Config = toml::from_str(source).expect("parses");
    config.migrate_transport_scoped_keys();
    assert_eq!(
        config.devices["receiver:82839805:slot:2"].host_switch_targets,
        vec!["unit:6be9d300".to_string()],
    );
}

#[test]
fn a_v4_file_with_no_direct_entries_is_untouched() {
    let source = "schema_version = 4\n\n[devices.\"receiver:82839805:slot:1\"]\ndpi = 1600\n";
    let mut config: Config = toml::from_str(source).expect("parses");
    let before = config.devices.clone();
    config.migrate_transport_scoped_keys();
    assert_eq!(config.devices.len(), before.len());
    assert!(config.devices.contains_key("receiver:82839805:slot:1"));
}

#[test]
fn migrating_v4_drops_the_transport_prefix_from_serial_keyed_direct_keys() {
    // The identity fragment is not always `unit:<hex>` — a device that
    // reports a serial keys as `serial:<s>` instead, and the brief names
    // both halves of the mapping equally.
    let source = r#"
schema_version = 4

[devices."direct:046d:c08d:serial:abc123"]
invert_scroll = true
"#;
    let mut config: Config = toml::from_str(source).expect("parses");
    config.migrate_transport_scoped_keys();

    assert!(config.devices.contains_key("serial:abc123"));
    assert!(
        !config
            .devices
            .contains_key("direct:046d:c08d:serial:abc123")
    );
    assert!(
        config.devices["serial:abc123"]
            .links
            .contains_key("direct:046d:c08d"),
        "the route it came from is remembered, not discarded"
    );
}

#[test]
fn migrating_leaves_a_direct_key_with_no_physical_identity_untouched() {
    // An all-zero unit id is not a physical identity (see
    // `PhysicalDeviceKey::parse`), so this key must not be rewritten — doing
    // so would collide every never-identified direct device onto one
    // `unit:00000000` entry.
    let source = "schema_version = 4\n\n[devices.\"direct:046d:c08d:unit:00000000\"]\ninvert_scroll = true\n";
    let mut config: Config = toml::from_str(source).expect("parses");
    config.migrate_transport_scoped_keys();

    assert!(
        config
            .devices
            .contains_key("direct:046d:c08d:unit:00000000"),
        "a non-physical identity is left keyed exactly as it was loaded"
    );
    assert!(!config.devices.contains_key("unit:00000000"));
}