openlogi-core 0.6.17

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
678
679
680
681
//! Privacy-filtered diagnostics report for support tickets — model-level only, no unique identifiers by construction.

use std::fmt::Write as _;

use serde::{Deserialize, Serialize};

use crate::device::{BatteryInfo, BatteryStatus, Capabilities, DeviceKind, DeviceTransports};

/// Where the resolver found the bundled device renders.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AssetSource {
    /// Read-only assets shipped inside the macOS `.app` bundle (release builds).
    Bundle,
    /// The per-user cache populated by the background asset sync.
    UserCache,
    /// Neither tier was found — devices fall back to the synthetic silhouette.
    None,
}

/// How a device reaches the host, refined past the raw HID++ route via announced transports.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConnectionKind {
    BoltReceiver,
    UnifyingReceiver,
    BluetoothDirect,
    Wired,
    Unknown,
}

/// Whether a curated render resolved, or the device fell back to the silhouette.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "state", content = "depot")]
pub enum RenderState {
    Resolved(String),
    Silhouette,
}

/// Agent-side device-enumeration health — explains an empty or stale device
/// section (e.g. a report copied while the agent is still scanning).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InventoryState {
    /// The first enumeration hasn't completed yet — the device set is unknown.
    Scanning,
    /// Enumeration completed; the device section is authoritative.
    Ready,
    /// Enumeration failed and is no longer retried; details in the agent log.
    Unavailable,
}

/// A receiver, by model only — never its `unique_id`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReceiverDiag {
    pub name: String,
    pub vendor_id: u16,
    pub product_id: u16,
}

/// One paired device, model-level only.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceDiag {
    pub display_name: String,
    pub kind: DeviceKind,
    /// Firmware codename (e.g. `"MX Master 3S"`), when known.
    pub codename: Option<String>,
    pub connection: ConnectionKind,
    pub online: bool,
    pub battery: Option<BatteryInfo>,
    /// Measured HID++ capabilities, or `None` if never probed since the agent started.
    pub capabilities: Option<Capabilities>,
    /// Human DPI summary (current + supported range), or `None` when not queried.
    pub dpi: Option<String>,
    /// Model identifier (e.g. `"2b35a"`) — a per-model key, not user-identifying.
    pub config_key: String,
    pub wpid: Option<u16>,
    /// Per-transport PID array from HID++ DeviceInformation (0x0003).
    pub model_ids: Option<[u16; 3]>,
    pub extended_model_id: Option<u8>,
    pub transports: Option<DeviceTransports>,
    pub render: RenderState,
    pub slot: u8,
}

/// App, agent, and host environment.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AppInfo {
    pub gui_version: String,
    /// `"debug"` or `"release"`.
    pub build_profile: String,
    /// `None` when the agent is unreachable (not yet connected / restarting).
    pub agent_version: Option<String>,
    pub protocol_gui: u32,
    pub protocol_agent: Option<u32>,
    /// Enumeration health behind the device section, `None` when the agent
    /// status is unavailable.
    pub inventory: Option<InventoryState>,
    /// Raw `std::env::consts::OS` (`"macos"` / `"linux"` / `"windows"`).
    pub os: String,
    pub os_version: Option<String>,
    pub arch: String,
    pub system_locale: Option<String>,
    /// Explicit UI-language override, or `None` for "follow system".
    pub ui_language: Option<String>,
    pub accessibility_granted: bool,
    /// `None` when the agent status is unavailable.
    pub hook_installed: Option<bool>,
    pub launch_at_login: Option<bool>,
    pub show_in_menu_bar: Option<bool>,
    pub check_for_updates: Option<bool>,
    pub thumbwheel_sensitivity: Option<i32>,
    pub config_schema_version: Option<u32>,
    pub configured_device_count: Option<usize>,
    pub running_from_bundle: bool,
}

/// Asset-cache state behind device renders.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AssetInfo {
    pub source: AssetSource,
    pub index_loaded: bool,
    /// Number of device models in the loaded index, when known.
    pub index_entries: Option<usize>,
    pub user_cache_present: bool,
    /// Cache directory with the home prefix redacted to `~`.
    pub cache_path: String,
    pub bundle_present: bool,
}

/// The whole report. Render with [`Self::to_markdown`] for the clipboard.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiagnosticsReport {
    pub app: AppInfo,
    pub assets: AssetInfo,
    pub receivers: Vec<ReceiverDiag>,
    pub devices: Vec<DeviceDiag>,
}

impl DiagnosticsReport {
    /// Render the report as the Markdown blob copied to the clipboard.
    #[must_use]
    pub fn to_markdown(&self) -> String {
        let mut out = String::new();
        let _ = writeln!(out, "### OpenLogi Diagnostics\n");
        self.write_app(&mut out);
        self.write_assets(&mut out);
        self.write_devices(&mut out);
        out.truncate(out.trim_end().len());
        out
    }

    fn write_app(&self, out: &mut String) {
        let a = &self.app;
        let _ = writeln!(out, "**App**");
        let _ = writeln!(
            out,
            "- OpenLogi (GUI): v{} ({})",
            a.gui_version, a.build_profile
        );
        let agent = match &a.agent_version {
            Some(v) if *v == a.gui_version => format!("v{v} (connected)"),
            Some(v) => format!("v{v} (connected) ⚠️ version mismatch with GUI"),
            None => "not connected".to_string(),
        };
        let _ = writeln!(out, "- Agent: {agent}");
        let proto = match a.protocol_agent {
            Some(p) if p == a.protocol_gui => format!("GUI {} / agent {p}", a.protocol_gui),
            Some(p) => format!("GUI {} / agent {p} ⚠️ mismatch", a.protocol_gui),
            None => format!("GUI {} / agent —", a.protocol_gui),
        };
        let _ = writeln!(out, "- IPC protocol: {proto}");
        let inventory = match a.inventory {
            Some(InventoryState::Ready) => "ready",
            Some(InventoryState::Scanning) => "scanning (first enumeration in progress)",
            Some(InventoryState::Unavailable) => {
                "⚠️ unavailable (enumeration failed — see agent log)"
            }
            None => "",
        };
        let _ = writeln!(out, "- Inventory: {inventory}");
        let os = match &a.os_version {
            Some(v) => format!("{} {} ({})", os_label(&a.os), v, a.arch),
            None => format!("{} ({})", os_label(&a.os), a.arch),
        };
        let _ = writeln!(out, "- OS: {os}");
        let locale = a.system_locale.as_deref().unwrap_or("unknown");
        let ui = a.ui_language.as_deref().unwrap_or("follow system");
        let _ = writeln!(out, "- Locale: {locale} (UI: {ui})");
        let _ = writeln!(
            out,
            "- Accessibility: {} · Input hook: {}",
            granted(a.accessibility_granted),
            opt_state(a.hook_installed, "installed", "not installed"),
        );
        let _ = writeln!(
            out,
            "- Launch at login: {} · Menu bar: {} · Update check: {}",
            opt_state(a.launch_at_login, "yes", "no"),
            opt_state(a.show_in_menu_bar, "yes", "no"),
            opt_state(a.check_for_updates, "on", "off"),
        );
        let source = if a.running_from_bundle {
            "app bundle (release)"
        } else {
            "source build (dev)"
        };
        let _ = writeln!(out, "- Running from: {source}");
        let _ = writeln!(
            out,
            "- Config: schema {} · {} configured device(s) · thumbwheel {}\n",
            opt_num(a.config_schema_version),
            opt_num(a.configured_device_count),
            opt_num(a.thumbwheel_sensitivity),
        );
    }

    fn write_assets(&self, out: &mut String) {
        let s = &self.assets;
        let _ = writeln!(out, "**Assets**");
        let index = match (s.index_loaded, s.index_entries) {
            (true, Some(n)) => format!("loaded ({n} models)"),
            (true, None) => "loaded".to_string(),
            (false, _) => "not loaded".to_string(),
        };
        let _ = writeln!(
            out,
            "- Source: {} · Index: {index} · User cache: {}",
            asset_source_label(s.source),
            if s.user_cache_present {
                "present"
            } else {
                "absent"
            },
        );
        let _ = writeln!(
            out,
            "- Cache path: {} · Bundle assets: {}\n",
            s.cache_path,
            if s.bundle_present {
                "present"
            } else {
                "absent"
            },
        );
    }

    fn write_devices(&self, out: &mut String) {
        let _ = writeln!(out, "**Devices ({})**", self.devices.len());
        if self.devices.is_empty() {
            let _ = writeln!(out, "- No devices detected.");
        }
        for d in &self.devices {
            let codename = d
                .codename
                .as_deref()
                .map(|c| format!(" (codename: {c})"))
                .unwrap_or_default();
            let _ = writeln!(
                out,
                "- {}{}{codename}",
                d.display_name,
                kind_label(d.kind)
            );
            let _ = writeln!(
                out,
                "  - Connection: {} · Online: {} · Battery: {}",
                connection_label(d.connection),
                yes_no(d.online),
                battery_label(d.battery.as_ref()),
            );
            let caps = match d.capabilities {
                Some(c) => format!(
                    "buttons={}, pointer={}, lighting={}",
                    yes_no(c.buttons),
                    yes_no(c.pointer),
                    yes_no(c.lighting),
                ),
                None => "not probed".to_string(),
            };
            let _ = writeln!(out, "  - Capabilities: {caps}");
            if let Some(dpi) = &d.dpi {
                let _ = writeln!(out, "  - DPI: {dpi}");
            }
            let _ = writeln!(out, "  - Model: {}{}", d.config_key, model_detail(d));
            if let Some(t) = d.transports {
                let _ = writeln!(out, "  - Transports: {}", transports_label(t));
            }
            let render = match &d.render {
                RenderState::Resolved(depot) => depot.clone(),
                RenderState::Silhouette => "⚠️ none (silhouette)".to_string(),
            };
            let _ = writeln!(out, "  - Render: {render} · {}", slot_label(d.slot));
        }
        if !self.receivers.is_empty() {
            let _ = writeln!(out, "\n**Receivers ({})**", self.receivers.len());
            for r in &self.receivers {
                let _ = writeln!(
                    out,
                    "- {} (VID {:04x} / PID {:04x})",
                    r.name, r.vendor_id, r.product_id
                );
            }
        }
    }
}

fn model_detail(d: &DeviceDiag) -> String {
    let mut parts = Vec::new();
    if let Some(wpid) = d.wpid {
        parts.push(format!("wpid: {wpid:04x}"));
    }
    if let Some([a, b, c]) = d.model_ids {
        parts.push(format!("model-ids: {a:04x}/{b:04x}/{c:04x}"));
    }
    if let Some(ext) = d.extended_model_id {
        parts.push(format!("ext-model: {ext:02x}"));
    }
    if parts.is_empty() {
        String::new()
    } else {
        format!(" ({})", parts.join(", "))
    }
}

fn slot_label(slot: u8) -> String {
    // 0xFF is the HID++ direct-device index (USB cable / Bluetooth, no receiver).
    if slot == 0xFF {
        "direct".to_string()
    } else {
        format!("Slot {slot}")
    }
}

fn os_label(os: &str) -> &str {
    match os {
        "macos" => "macOS",
        "linux" => "Linux",
        "windows" => "Windows",
        other => other,
    }
}

fn asset_source_label(source: AssetSource) -> &'static str {
    match source {
        AssetSource::Bundle => "app bundle",
        AssetSource::UserCache => "user cache",
        AssetSource::None => "none",
    }
}

fn kind_label(kind: DeviceKind) -> &'static str {
    match kind {
        DeviceKind::Mouse => "mouse",
        DeviceKind::Keyboard => "keyboard",
        DeviceKind::Numpad => "numpad",
        DeviceKind::Presenter => "presenter",
        DeviceKind::Remote => "remote",
        DeviceKind::Trackball => "trackball",
        DeviceKind::Touchpad => "touchpad",
        DeviceKind::Tablet => "tablet",
        DeviceKind::Gamepad => "gamepad",
        DeviceKind::Joystick => "joystick",
        DeviceKind::Headset => "headset",
        DeviceKind::Unknown => "unknown",
    }
}

fn connection_label(connection: ConnectionKind) -> &'static str {
    match connection {
        ConnectionKind::BoltReceiver => "Logi Bolt receiver",
        ConnectionKind::UnifyingReceiver => "Logi Unifying receiver",
        ConnectionKind::BluetoothDirect => "Bluetooth (direct)",
        ConnectionKind::Wired => "Wired (USB)",
        ConnectionKind::Unknown => "unknown",
    }
}

fn battery_label(battery: Option<&BatteryInfo>) -> String {
    match battery {
        Some(b) => format!(
            "{}% ({}, {})",
            b.percentage,
            battery_status_label(b.status),
            battery_level_label(b.level),
        ),
        None => "n/a".to_string(),
    }
}

fn battery_status_label(status: BatteryStatus) -> &'static str {
    match status {
        BatteryStatus::Discharging => "discharging",
        BatteryStatus::Charging => "charging",
        BatteryStatus::ChargingSlow => "charging (slow)",
        BatteryStatus::Full => "full",
        BatteryStatus::Error => "error",
        BatteryStatus::Unknown => "unknown",
    }
}

fn battery_level_label(level: crate::device::BatteryLevel) -> &'static str {
    use crate::device::BatteryLevel;
    match level {
        BatteryLevel::Critical => "critical",
        BatteryLevel::Low => "low",
        BatteryLevel::Good => "good",
        BatteryLevel::Full => "full",
        BatteryLevel::Unknown => "unknown",
    }
}

fn transports_label(t: DeviceTransports) -> String {
    let mut parts = Vec::new();
    if t.usb {
        parts.push("USB");
    }
    if t.equad {
        parts.push("eQuad");
    }
    if t.btle {
        parts.push("BTLE");
    }
    if t.bluetooth {
        parts.push("Bluetooth");
    }
    if parts.is_empty() {
        "none".to_string()
    } else {
        parts.join(", ")
    }
}

fn yes_no(value: bool) -> &'static str {
    if value { "yes" } else { "no" }
}

fn granted(value: bool) -> &'static str {
    if value { "granted" } else { "denied" }
}

fn opt_state(value: Option<bool>, yes: &'static str, no: &'static str) -> &'static str {
    match value {
        Some(true) => yes,
        Some(false) => no,
        None => "unknown",
    }
}

fn opt_num<T: std::fmt::Display>(value: Option<T>) -> String {
    value.map_or_else(|| "".to_string(), |v| v.to_string())
}

#[cfg(test)]
#[allow(clippy::expect_used, reason = "expect/unwrap are idiomatic in tests")]
mod tests {
    use super::{
        AppInfo, AssetInfo, AssetSource, ConnectionKind, DeviceDiag, DiagnosticsReport,
        InventoryState, ReceiverDiag, RenderState,
    };
    use crate::device::{
        BatteryInfo, BatteryLevel, BatteryStatus, Capabilities, DeviceKind, DeviceTransports,
    };

    fn app() -> AppInfo {
        AppInfo {
            gui_version: "0.6.6".to_string(),
            build_profile: "release".to_string(),
            agent_version: Some("0.6.6".to_string()),
            protocol_gui: 1,
            protocol_agent: Some(1),
            inventory: Some(InventoryState::Ready),
            os: "macos".to_string(),
            os_version: Some("15.5".to_string()),
            arch: "arm64".to_string(),
            system_locale: Some("en-US".to_string()),
            ui_language: None,
            accessibility_granted: true,
            hook_installed: Some(true),
            launch_at_login: Some(true),
            show_in_menu_bar: Some(true),
            check_for_updates: Some(false),
            thumbwheel_sensitivity: Some(0),
            config_schema_version: Some(2),
            configured_device_count: Some(3),
            running_from_bundle: true,
        }
    }

    fn assets() -> AssetInfo {
        AssetInfo {
            source: AssetSource::Bundle,
            index_loaded: true,
            index_entries: Some(142),
            user_cache_present: true,
            cache_path: "~/.local/share/openlogi/assets".to_string(),
            bundle_present: true,
        }
    }

    fn sample() -> DiagnosticsReport {
        DiagnosticsReport {
            app: app(),
            assets: assets(),
            receivers: vec![ReceiverDiag {
                name: "Logi Bolt".to_string(),
                vendor_id: 0x046d,
                product_id: 0xc548,
            }],
            devices: vec![
                DeviceDiag {
                    display_name: "MX Keys".to_string(),
                    kind: DeviceKind::Keyboard,
                    codename: Some("MX Keys".to_string()),
                    connection: ConnectionKind::BoltReceiver,
                    online: true,
                    battery: Some(BatteryInfo {
                        percentage: 80,
                        level: BatteryLevel::Good,
                        status: BatteryStatus::Discharging,
                    }),
                    capabilities: Some(Capabilities::default()),
                    dpi: None,
                    config_key: "2b35a".to_string(),
                    wpid: Some(0x4093),
                    model_ids: Some([0xb35a, 0, 0]),
                    extended_model_id: Some(0x02),
                    transports: Some(DeviceTransports {
                        equad: true,
                        ..DeviceTransports::default()
                    }),
                    render: RenderState::Silhouette,
                    slot: 2,
                },
                DeviceDiag {
                    display_name: "MX Master 3S".to_string(),
                    kind: DeviceKind::Mouse,
                    codename: Some("MX Master 3S".to_string()),
                    connection: ConnectionKind::Wired,
                    online: false,
                    battery: None,
                    capabilities: Some(Capabilities {
                        buttons: true,
                        pointer: true,
                        lighting: false,
                        scroll_inversion: false,
                    }),
                    dpi: Some("1600 dpi (range 200–8000, 5 steps)".to_string()),
                    config_key: "4082d".to_string(),
                    wpid: Some(0x4082),
                    model_ids: Some([0x082d, 0, 0]),
                    extended_model_id: Some(0x04),
                    transports: Some(DeviceTransports {
                        usb: true,
                        ..DeviceTransports::default()
                    }),
                    render: RenderState::Resolved("mx_master_3s".to_string()),
                    slot: 1,
                },
            ],
        }
    }

    #[test]
    fn renders_header_and_sections() {
        let md = sample().to_markdown();
        assert!(md.starts_with("### OpenLogi Diagnostics"));
        assert!(md.contains("**App**"));
        assert!(md.contains("**Assets**"));
        assert!(md.contains("**Devices (2)**"));
        assert!(md.contains("**Receivers (1)**"));
        assert!(md.contains("- Logi Bolt (VID 046d / PID c548)"));
        assert!(md.contains("- OpenLogi (GUI): v0.6.6 (release)"));
        assert!(md.contains("- Agent: v0.6.6 (connected)"));
        assert!(md.contains("- IPC protocol: GUI 1 / agent 1"));
        assert!(md.contains("- Inventory: ready"));
        assert!(md.contains("- OS: macOS 15.5 (arm64)"));
        assert!(
            md.contains("- Source: app bundle · Index: loaded (142 models) · User cache: present")
        );
        assert!(md.contains("- Config: schema 2 · 3 configured device(s) · thumbwheel 0"));
    }

    #[test]
    fn renders_device_detail() {
        let md = sample().to_markdown();
        assert!(md.contains("- MX Keys — keyboard (codename: MX Keys)"));
        assert!(md.contains(
            "Connection: Logi Bolt receiver · Online: yes · Battery: 80% (discharging, good)"
        ));
        assert!(md.contains("Capabilities: buttons=no, pointer=no, lighting=no"));
        assert!(md.contains("Model: 2b35a (wpid: 4093, model-ids: b35a/0000/0000, ext-model: 02)"));
        assert!(md.contains("Transports: eQuad"));
        assert!(md.contains("Render: ⚠️ none (silhouette) · Slot 2"));
        assert!(md.contains("- MX Master 3S — mouse"));
        assert!(md.contains("DPI: 1600 dpi (range 200–8000, 5 steps)"));
        assert!(md.contains("Transports: USB"));
        assert!(md.contains("Render: mx_master_3s · Slot 1"));
        assert!(md.contains("Battery: n/a"));
    }

    #[test]
    fn flags_version_and_protocol_mismatch() {
        let mut report = sample();
        report.app.agent_version = Some("0.6.5".to_string());
        report.app.protocol_agent = Some(2);
        let md = report.to_markdown();
        assert!(md.contains("v0.6.5 (connected) ⚠️ version mismatch with GUI"));
        assert!(md.contains("GUI 1 / agent 2 ⚠️ mismatch"));
    }

    #[test]
    fn omits_unique_identifiers_and_footer() {
        let md = sample().to_markdown();
        assert!(!md.contains("Serial"));
        assert!(!md.to_lowercase().contains("unit id"));
        assert!(!md.contains("omitted by design"));
    }

    #[test]
    fn direct_slot_renders_as_direct() {
        let mut report = sample();
        report.devices[0].slot = 0xFF;
        let md = report.to_markdown();
        assert!(md.contains("· direct"));
        assert!(!md.contains("Slot 255"));
    }

    #[test]
    fn unprobed_capabilities_render_not_probed() {
        let mut report = sample();
        report.devices[0].capabilities = None;
        let md = report.to_markdown();
        assert!(md.contains("  - Capabilities: not probed"));
    }

    #[test]
    fn empty_inventory_still_renders() {
        let report = DiagnosticsReport {
            app: app(),
            assets: assets(),
            receivers: Vec::new(),
            devices: Vec::new(),
        };
        let md = report.to_markdown();
        assert!(md.contains("**Devices (0)**"));
        assert!(md.contains("- No devices detected."));
    }

    #[test]
    fn unreachable_agent_renders_unknowns() {
        let mut report = sample();
        report.app.agent_version = None;
        report.app.protocol_agent = None;
        report.app.inventory = None;
        report.app.hook_installed = None;
        report.app.launch_at_login = None;
        let md = report.to_markdown();
        assert!(md.contains("- Agent: not connected"));
        assert!(md.contains("GUI 1 / agent —"));
        assert!(md.contains("- Inventory: —"));
        assert!(md.contains("Input hook: unknown"));
    }

    #[test]
    fn incomplete_enumeration_is_flagged() {
        let mut report = sample();
        report.app.inventory = Some(InventoryState::Scanning);
        assert!(
            report
                .to_markdown()
                .contains("- Inventory: scanning (first enumeration in progress)")
        );
        report.app.inventory = Some(InventoryState::Unavailable);
        assert!(
            report
                .to_markdown()
                .contains("- Inventory: ⚠️ unavailable (enumeration failed — see agent log)")
        );
    }
}