windows-topology-sys 0.2.0

Safe enumeration of Windows processor, cache, and memory topology, with a JSON-describable schema for discovered or synthetic topologies.
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// Copyright (c) 2026 Mike Grier
use std::collections::BTreeMap;

use super::*;

#[test]
fn processor_id_orders_by_group_then_number() {
    let a = ProcessorId {
        group: 0,
        number: 5,
    };
    let b = ProcessorId {
        group: 1,
        number: 0,
    };
    assert!(a < b);
}

#[test]
fn a_memory_domain_may_have_no_processors() {
    // A CXL memory expander: a memory domain that is nonetheless real and
    // must not be treated as degenerate just because it has no processors
    // (D-5). This shape is unreachable via GetLogicalProcessorInformationEx
    // on real hardware, so it is exercised here rather than through
    // discovery.
    let domain = Domain {
        kind: DomainKind::Memory {
            memory_bytes: Observed::Known(64 * 1024 * 1024 * 1024),
        },
        processors: ProcessorSet::empty(),
        observations: Vec::new(),
    };
    assert!(domain.processors.is_empty());
    let DomainKind::Memory { memory_bytes } = domain.kind else {
        panic!("expected Memory")
    };
    assert_eq!(memory_bytes, Observed::Known(64 * 1024 * 1024 * 1024));
}

#[test]
fn a_discovered_memory_domain_has_no_known_size() {
    // Contrast with the test above: Windows's own enumeration cannot report
    // node memory capacity at all, so that arm must stay `None` rather than
    // guessing `Some(0)`, which would be indistinguishable from "no memory".
    let domain = Domain {
        kind: DomainKind::Memory {
            memory_bytes: Observed::NotObserved,
        },
        processors: ProcessorSet::empty(),
        observations: Vec::new(),
    };
    let DomainKind::Memory { memory_bytes } = domain.kind else {
        panic!("expected Memory")
    };
    assert_eq!(memory_bytes, Observed::NotObserved);
}

#[test]
fn an_unrecognised_domain_kind_carries_its_attributes() {
    let mut attributes = BTreeMap::new();
    attributes.insert("watts".to_string(), AttributeValue::Float(15.5));
    let domain = Domain {
        kind: DomainKind::Other {
            name: "power".to_string(),
            attributes: attributes.clone(),
        },
        processors: ProcessorSet::empty(),
        observations: Vec::new(),
    };
    let DomainKind::Other {
        name,
        attributes: got,
    } = &domain.kind
    else {
        panic!("expected Other")
    };
    assert_eq!(name, "power");
    assert_eq!(got, &attributes);
}

#[test]
fn attribute_value_supports_nested_structures() {
    let mut inner = BTreeMap::new();
    inner.insert("a".to_string(), AttributeValue::Bool(true));
    let value = AttributeValue::Array(vec![AttributeValue::Null, AttributeValue::Object(inner)]);
    assert_eq!(
        value.clone(),
        value,
        "AttributeValue must support equality for round-trip tests later"
    );
}

// --- serde (M3.3) ---

#[cfg(feature = "serde")]
mod serde_tests {
    use std::collections::BTreeMap;

    use super::super::*;
    use crate::observed::Observed;
    use crate::processor_set::ProcessorSet;

    fn round_trip(domain: &Domain) -> Domain {
        let json = serde_json::to_string(domain).expect("serialize");
        serde_json::from_str(&json).expect("deserialize")
    }

    #[test]
    fn a_group_domain_round_trips() {
        let domain = Domain {
            kind: DomainKind::Group,
            processors: ProcessorSet::from_group_mask(0, 0b11),
            observations: Vec::new(),
        };
        assert_eq!(round_trip(&domain), domain);
    }

    #[test]
    fn a_core_domain_round_trips_with_its_fields() {
        let domain = Domain {
            kind: DomainKind::Core {
                simultaneous_multithreading: true,
                efficiency_class: 7,
            },
            processors: ProcessorSet::from_group_mask(0, 0b1),
            observations: Vec::new(),
        };
        assert_eq!(round_trip(&domain), domain);
    }

    #[test]
    fn a_cache_domain_round_trips_including_its_cache_type() {
        let domain = Domain {
            kind: DomainKind::Cache {
                level: 3,
                associativity: 16,
                line_size: 64,
                size_bytes: 32 * 1024 * 1024,
                cache_type: CacheKind::Unified,
            },
            processors: ProcessorSet::from_group_mask(0, 0b1111),
            observations: Vec::new(),
        };
        assert_eq!(round_trip(&domain), domain);
    }

    #[test]
    fn a_cache_domain_with_an_unrecognised_cache_type_round_trips() {
        let domain = Domain {
            kind: DomainKind::Cache {
                level: 1,
                associativity: 8,
                line_size: 64,
                size_bytes: 32 * 1024,
                cache_type: CacheKind::Other(99),
            },
            processors: ProcessorSet::from_group_mask(0, 0b1),
            observations: Vec::new(),
        };
        let json = serde_json::to_string(&domain).expect("serialize");
        assert!(
            json.contains(r#""cache_type":{"other":99}"#),
            "unexpected JSON: {json}"
        );
        assert_eq!(round_trip(&domain), domain);
    }

    #[test]
    fn a_cache_domain_with_a_negative_unrecognised_cache_type_round_trips() {
        // PROCESSOR_CACHE_TYPE is a raw i32 enum; a value Windows reports
        // that happens to be negative must round-trip, not just the
        // non-negative range a u32-shaped decode would accept.
        let domain = Domain {
            kind: DomainKind::Cache {
                level: 1,
                associativity: 8,
                line_size: 64,
                size_bytes: 32 * 1024,
                cache_type: CacheKind::Other(-7),
            },
            processors: ProcessorSet::from_group_mask(0, 0b1),
            observations: Vec::new(),
        };
        let json = serde_json::to_string(&domain).expect("serialize");
        assert!(
            json.contains(r#""cache_type":{"other":-7}"#),
            "unexpected JSON: {json}"
        );
        assert_eq!(round_trip(&domain), domain);
    }

    #[test]
    fn a_memory_only_domain_round_trips_with_no_processors() {
        // The CXL-expander case (D-5): a memory domain with a known size and
        // no processors at all.
        let domain = Domain {
            kind: DomainKind::Memory {
                memory_bytes: Observed::Known(64 * 1024 * 1024 * 1024),
            },
            processors: ProcessorSet::empty(),
            observations: Vec::new(),
        };
        let json = serde_json::to_string(&domain).expect("serialize");
        assert!(
            json.contains(r#""processors":[]"#),
            "unexpected JSON: {json}"
        );
        assert_eq!(round_trip(&domain), domain);
    }

    #[test]
    fn a_memory_domain_with_unknown_size_omits_memory_bytes_rather_than_writing_null() {
        let domain = Domain {
            kind: DomainKind::Memory {
                memory_bytes: Observed::NotObserved,
            },
            processors: ProcessorSet::empty(),
            observations: Vec::new(),
        };
        let json = serde_json::to_string(&domain).expect("serialize");
        assert!(!json.contains("memory_bytes"), "unexpected JSON: {json}");
        assert_eq!(round_trip(&domain), domain);
    }

    #[test]
    fn an_unrecognised_domain_kind_round_trips_its_attributes_losslessly() {
        let mut attributes = BTreeMap::new();
        attributes.insert("watts".to_string(), AttributeValue::Float(15.5));
        attributes.insert("throttled".to_string(), AttributeValue::Bool(false));
        let domain = Domain {
            kind: DomainKind::Other {
                name: "power".to_string(),
                attributes,
            },
            processors: ProcessorSet::empty(),
            observations: Vec::new(),
        };
        assert_eq!(round_trip(&domain), domain);
    }

    #[test]
    fn an_integer_attribute_beyond_f64s_precision_round_trips_exactly() {
        // PR #20 review response: 9,007,199,254,740,993 (2^53 + 1) is the
        // smallest positive integer an f64 cannot represent exactly -- it
        // would decode as 9,007,199,254,740,992 if this ever routed through
        // `f64` again. Also covers a large negative integer, which only the
        // signed variant can hold.
        let large_unsigned: u64 = (1u64 << 53) + 1;
        let large_signed: i64 = -((1i64 << 53) + 1);
        let mut attributes = BTreeMap::new();
        attributes.insert(
            "large_unsigned".to_string(),
            AttributeValue::UnsignedInteger(large_unsigned),
        );
        attributes.insert(
            "large_signed".to_string(),
            AttributeValue::SignedInteger(large_signed),
        );
        let domain = Domain {
            kind: DomainKind::Other {
                name: "precision".to_string(),
                attributes,
            },
            processors: ProcessorSet::empty(),
            observations: Vec::new(),
        };
        let restored = round_trip(&domain);
        assert_eq!(restored, domain);
        let DomainKind::Other { attributes, .. } = &restored.kind else {
            panic!("expected Other")
        };
        assert_eq!(
            attributes.get("large_unsigned"),
            Some(&AttributeValue::UnsignedInteger(large_unsigned))
        );
        assert_eq!(
            attributes.get("large_signed"),
            Some(&AttributeValue::SignedInteger(large_signed))
        );
    }

    #[test]
    fn a_memory_bytes_value_beyond_f64s_precision_decodes_exactly() {
        // PR #20 review response: `memory_bytes` is decoded through the same
        // `as_u64` helper as any other unsigned attribute, so it must
        // preserve the same precision beyond 2^53.
        let precise: u64 = (1u64 << 53) + 1;
        let domain = Domain {
            kind: DomainKind::Memory {
                memory_bytes: Observed::Known(precise),
            },
            processors: ProcessorSet::empty(),
            observations: Vec::new(),
        };
        let restored = round_trip(&domain);
        let DomainKind::Memory { memory_bytes } = restored.kind else {
            panic!("expected Memory")
        };
        assert_eq!(memory_bytes, Observed::Known(precise));
    }

    #[test]
    fn an_unrecognised_domain_kinds_attribute_colliding_with_a_reserved_field_name_is_refused() {
        for reserved in ["kind", "id", "processors"] {
            let mut attributes = BTreeMap::new();
            attributes.insert(reserved.to_string(), AttributeValue::UnsignedInteger(1));
            let domain = Domain {
                kind: DomainKind::Other {
                    name: "power".to_string(),
                    attributes,
                },
                processors: ProcessorSet::empty(),
                observations: Vec::new(),
            };
            serde_json::to_string(&domain).expect_err(&format!(
                "an attribute named {reserved:?} must not silently overwrite the reserved field"
            ));
        }
    }

    #[test]
    fn an_unrecognised_domain_kind_named_after_a_known_one_is_refused() {
        // The sibling of the attribute-name check above, and the same hazard
        // one level up: `Other` promises that a description this crate cannot
        // interpret round-trips losslessly, and a name this crate *does*
        // interpret breaks that promise outright. Written as `"kind": "group"`,
        // the document reads back as `DomainKind::Group` -- a different kind,
        // with the attributes silently gone. Raised in the PR #56 review.
        for known in [
            "group", "package", "die", "module", "core", "cache", "memory",
        ] {
            let mut attributes = BTreeMap::new();
            attributes.insert("watts".to_string(), AttributeValue::UnsignedInteger(15));
            let domain = Domain {
                kind: DomainKind::Other {
                    name: known.to_string(),
                    attributes,
                },
                processors: ProcessorSet::empty(),
                observations: Vec::new(),
            };
            serde_json::to_string(&domain).expect_err(&format!(
                "an unrecognised kind named {known:?} must not be written as that known kind"
            ));
        }
    }

    #[test]
    fn an_unrecognised_domain_kind_with_its_own_name_still_round_trips() {
        // The complement, so the refusal above cannot be satisfied by refusing
        // every `Other`: a genuinely unknown name is exactly what the variant
        // is for and must still survive the round trip intact.
        let mut attributes = BTreeMap::new();
        attributes.insert("watts".to_string(), AttributeValue::UnsignedInteger(15));
        let domain = Domain {
            kind: DomainKind::Other {
                name: "power".to_string(),
                attributes,
            },
            processors: ProcessorSet::empty(),
            observations: Vec::new(),
        };
        let restored = round_trip(&domain);
        assert_eq!(restored, domain);
    }

    #[test]
    fn every_well_known_name_decodes_to_a_named_kind() {
        // Binds the constant the serializer refuses on to the arms the
        // deserializer actually matches, so the two cannot drift apart. A name
        // listed but no longer decoded would make the refusal above spurious;
        // a name decoded but not listed would let an `Other` claim it, which is
        // the defect this pair of tests exists to prevent. Derived from the
        // constant rather than restating it, so adding a kind to one place and
        // not the other fails here.
        for name in crate::domain::serde_impl::WELL_KNOWN_KIND_NAMES {
            let json = format!(
                r#"{{"kind":"{name}","id":0,"processors":[],
                     "simultaneous_multithreading":false,"efficiency_class":0,
                     "level":1,"associativity":8,"line_size":64,"size_bytes":32768,
                     "cache_type":"unified","memory_bytes":0}}"#
            );
            let domain: Domain = serde_json::from_str(&json)
                .unwrap_or_else(|e| panic!("{name:?} must decode as a named kind: {e}"));
            assert!(
                !matches!(domain.kind, DomainKind::Other { .. }),
                "{name:?} is listed as well known but decoded as `Other`, so the \
                 serializer refuses a name nothing actually reserves"
            );
        }
    }

    #[test]
    fn a_hand_written_synthetic_description_parses() {
        // The whole point of an open, hand-writable schema: no discovery, no
        // ProcessorSet builder API, just JSON text a human could type.
        let json = r#"{
            "kind": "memory",
            "id": 5,
            "processors": [],
            "memory_bytes": 549755813888
        }"#;
        let domain: Domain = serde_json::from_str(json).expect("parse");
        // The wire "id" is read as the relationship walk's label, since that is
        // what serialization writes there. It is not evidence the walk observed
        // this -- deserialization records no observation at all (D-12).
        assert!(domain.observations.is_empty());
        assert!(domain.processors.is_empty());
        assert_eq!(
            domain.kind,
            DomainKind::Memory {
                memory_bytes: Observed::Known(549_755_813_888)
            }
        );
    }

    #[test]
    fn a_hand_written_description_can_place_kind_in_any_field_order() {
        // Buffering to a map before interpreting `kind` (see the module doc
        // on `serde_impl`) is what makes this safe rather than fragile.
        let json = r#"{
            "processors": [{"group": 0, "number": 0}],
            "id": 1,
            "kind": "package"
        }"#;
        let domain: Domain = serde_json::from_str(json).expect("parse");
        assert_eq!(domain.kind, DomainKind::Package);
    }

    #[test]
    fn an_unrecognised_kind_from_hand_written_json_parses_as_other() {
        let json = r#"{
            "kind": "die",
            "id": 0,
            "processors": [{"group": 0, "number": 0}]
        }"#;
        // "die" IS recognised -- this proves the well-known path, contrasted
        // with a genuinely unknown kind below.
        let domain: Domain = serde_json::from_str(json).expect("parse");
        assert_eq!(domain.kind, DomainKind::Die);

        let json = r#"{
            "kind": "quantum-cache",
            "id": 0,
            "processors": [],
            "coherence": "eventual"
        }"#;
        let domain: Domain = serde_json::from_str(json).expect("parse");
        let DomainKind::Other { name, attributes } = domain.kind else {
            panic!("expected Other")
        };
        assert_eq!(name, "quantum-cache");
        assert_eq!(
            attributes.get("coherence"),
            Some(&AttributeValue::String("eventual".to_string()))
        );
    }

    #[test]
    fn a_missing_required_field_is_a_clean_error_not_a_panic() {
        let json = r#"{"kind": "group", "id": 0}"#;
        let error = serde_json::from_str::<Domain>(json).expect_err("processors is required");
        assert!(
            error.to_string().contains("processors"),
            "error should name the missing field: {error}"
        );
    }

    // --- float-to-integer coercion (mutation-testing gap) ---
    //
    // `as_u64`/`as_i64` accept a JSON float only when it is a whole number
    // inside the target's range. That guard was added as a PR #20 review
    // response, specifically to stop the silent precision loss the older
    // f64-for-everything encoding had -- and a `cargo mutants` run found that
    // *every* operator in it survives mutation. Replacing the whole guard with
    // `true` or `false`, flipping `==` to `!=`, or `&&` to `||`, all left the
    // suite green, because no test ever fed a float into an integer field.
    //
    // A correction made in response to review, never verified, is the worst
    // case of this: the reasoning is on record and the behaviour is not.
    //
    // These matter for a hand-written or fed-in description, which is the whole
    // reason the schema is open: JSON has one number type, so a generator that
    // emits 4.0 for a count is entirely ordinary, and one that emits 4.5 is a
    // defect that must be refused rather than truncated.

    /// A memory domain whose `memory_bytes` is written as the given JSON number
    /// literal, which is the shortest path to `as_u64`.
    fn memory_domain_with(bytes_literal: &str) -> Result<Domain, serde_json::Error> {
        let json = format!(
            r#"{{"kind": "memory", "id": 0, "processors": [], "memory_bytes": {bytes_literal}}}"#
        );
        serde_json::from_str(&json)
    }

    #[test]
    fn a_whole_number_float_is_accepted_as_an_unsigned_field() {
        let domain = memory_domain_with("1024.0").expect("4.0 is a whole number");
        assert_eq!(
            domain.kind,
            DomainKind::Memory {
                memory_bytes: Observed::Known(1024)
            },
            "a generator emitting a whole number as a float is ordinary JSON"
        );
    }

    #[test]
    fn a_fractional_float_is_refused_rather_than_truncated() {
        // The precision-loss case the guard exists for. Truncating to 1024
        // would be silent data corruption in a field describing a machine.
        assert!(
            memory_domain_with("1024.5").is_err(),
            "a fractional byte count must be refused, not rounded"
        );
    }

    #[test]
    fn a_negative_float_is_refused_for_an_unsigned_field() {
        assert!(
            memory_domain_with("-1.0").is_err(),
            "a negative count is out of range for an unsigned field"
        );
    }

    #[test]
    fn a_float_beyond_the_unsigned_range_is_refused() {
        // Above u64::MAX. `n as u64` would saturate silently, which is exactly
        // the conversion the range half of the guard prevents.
        assert!(
            memory_domain_with("1e300").is_err(),
            "a float larger than u64::MAX must be refused, not saturated"
        );
    }

    #[test]
    fn zero_is_accepted_at_the_bottom_of_the_unsigned_range() {
        // The boundary the range check includes. A guard written with an
        // exclusive bound would wrongly refuse this.
        let domain = memory_domain_with("0.0").expect("zero is in range");
        assert_eq!(
            domain.kind,
            DomainKind::Memory {
                memory_bytes: Observed::Known(0)
            }
        );
    }

    #[test]
    fn an_integer_literal_still_parses_unchanged() {
        // The common path, asserted beside the float ones so a guard that
        // refused everything could not pass this group.
        let domain = memory_domain_with("4096").expect("plain integers parse");
        assert_eq!(
            domain.kind,
            DomainKind::Memory {
                memory_bytes: Observed::Known(4096)
            }
        );
    }

    /// A cache domain whose `cache_type` carries a raw signed code, which is
    /// the path to `as_i64`.
    fn cache_domain_with_other_type(code_literal: &str) -> Result<Domain, serde_json::Error> {
        let json = format!(
            r#"{{"kind": "cache", "id": 0, "processors": [],
                 "level": 2, "associativity": 8, "line_size": 64,
                 "size_bytes": 1024, "cache_type": {{"other": {code_literal}}}}}"#
        );
        serde_json::from_str(&json)
    }

    #[test]
    fn a_whole_number_float_is_accepted_as_a_signed_field() {
        let domain = cache_domain_with_other_type("9.0").expect("9.0 is a whole number");
        let DomainKind::Cache { cache_type, .. } = domain.kind else {
            panic!("expected a cache domain");
        };
        assert_eq!(cache_type, CacheKind::Other(9));
    }

    #[test]
    fn a_negative_whole_float_is_accepted_as_a_signed_field() {
        // The signed range genuinely extends below zero -- a raw
        // PROCESSOR_CACHE_TYPE is an i32 and is not guaranteed non-negative --
        // so this is the case that distinguishes `as_i64`'s guard from
        // `as_u64`'s rather than duplicating it.
        let domain = cache_domain_with_other_type("-3.0").expect("-3.0 is a whole number");
        let DomainKind::Cache { cache_type, .. } = domain.kind else {
            panic!("expected a cache domain");
        };
        assert_eq!(cache_type, CacheKind::Other(-3));
    }

    #[test]
    fn a_fractional_float_is_refused_for_a_signed_field() {
        assert!(
            cache_domain_with_other_type("-3.5").is_err(),
            "a fractional cache-type code must be refused, not truncated"
        );
    }

    #[test]
    fn a_float_beyond_the_signed_range_is_refused() {
        assert!(
            cache_domain_with_other_type("-1e300").is_err(),
            "a float below i64::MIN must be refused, not saturated"
        );
    }
    // -----------------------------------------------------------------------
    // Rejection.
    //
    // Every test above round-trips a value this crate serialized, so the
    // deserializer only ever sees well-formed input and its refusal paths are
    // never taken. A mutation run replaced `as_bool` with a constant `true` and
    // loosened the cache-object guard, and neither could fail against input
    // that was already valid.
    //
    // These start from hand-written JSON instead.
    // -----------------------------------------------------------------------

    #[test]
    fn a_false_flag_survives_the_round_trip_as_false() {
        // `as_bool -> Ok(true)` survived because every serde test above used
        // `simultaneous_multithreading: true`. With only that value in the
        // suite, a deserializer that ignored its input and always answered
        // `true` was indistinguishable from one that read it.
        let domain = Domain {
            kind: DomainKind::Core {
                simultaneous_multithreading: false,
                efficiency_class: 0,
            },
            processors: ProcessorSet::from_group_mask(0, 0b1),
            observations: Vec::new(),
        };

        let restored = round_trip(&domain);
        assert_eq!(restored, domain);
        let DomainKind::Core {
            simultaneous_multithreading,
            ..
        } = restored.kind
        else {
            panic!("a core domain must deserialize as one");
        };
        assert!(
            !simultaneous_multithreading,
            "a machine without SMT must not be reported as having it"
        );
    }

    #[test]
    fn a_non_boolean_smt_flag_is_refused_rather_than_read_as_true() {
        // The other half: a constant `true` accepts input that is not a boolean
        // at all. Anything that decodes to a different `AttributeValue` reaches
        // the refusal, so a string and a number are both tried.
        // The well-formed shape is asserted first, so a later failure is
        // attributable to the flag rather than to the rest of the description.
        // Written after exactly that mistake: an ad-hoc `processors` literal
        // made the refusals happen for an unrelated reason.
        let well_formed = r#"{"kind": "core", "id": 1, "processors": [],
                 "simultaneous_multithreading": false, "efficiency_class": 0}"#;
        serde_json::from_str::<Domain>(well_formed)
            .expect("the fixture must parse when only the flag is changed");

        for bad in [r#""yes""#, "1", "null"] {
            let json = well_formed.replace("false", bad);
            let error = serde_json::from_str::<Domain>(&json)
                .expect_err("{bad} is not a boolean and must not be read as one");
            assert!(
                error.to_string().contains("boolean"),
                "the refusal must say what was expected: {error}"
            );
        }
    }

    #[test]
    fn a_cache_type_object_carrying_more_than_the_other_key_is_refused() {
        // The `map.len() == 1` guard survived being replaced with `true`, which
        // would accept an object with extra keys and silently ignore them. That
        // matters more than it looks: the object form exists to carry a raw
        // `PROCESSOR_CACHE_TYPE` this crate does not recognise, so a reader
        // sending a *newer* shape must be told it was not understood rather
        // than have the parts we recognise quietly taken.
        //
        // Built from the same helper as the accepted case below, which is what
        // makes the refusal attributable: an ad-hoc JSON literal here was
        // rejected for a malformed `processors` field instead, and would have
        // passed this assertion while testing nothing about the guard.
        let json = r#"{"kind": "cache", "id": 0, "processors": [],
                 "level": 2, "associativity": 8, "line_size": 64,
                 "size_bytes": 1024, "cache_type": {"other": 99, "extra": 1}}"#;

        let outcome: Result<Domain, _> = serde_json::from_str(json);
        let error = outcome.expect_err("an object with a second key must be refused");
        assert!(
            error.to_string().contains("cache_type"),
            "the refusal must name the field that was not understood, or a \
             reader cannot tell which part of their description was rejected: {error}"
        );
    }

    #[test]
    fn a_cache_type_object_with_exactly_the_other_key_is_accepted() {
        // The positive case, so the test above cannot be satisfied by a guard
        // that refuses every object -- or, as it first was, by a fixture that
        // never reached the guard at all.
        let domain =
            cache_domain_with_other_type("99").expect("the single-key form is the one we emit");
        let DomainKind::Cache { cache_type, .. } = domain.kind else {
            panic!("a cache domain must deserialize as one");
        };
        assert_eq!(cache_type, CacheKind::Other(99));
    }
}

// --- M5+.2: omitted and explicit-null stop being the same value ---

#[cfg(feature = "serde")]
#[test]
fn an_omitted_memory_bytes_and_an_explicit_null_are_different_values() {
    // The defect: both used to parse to `None`, so a description that never
    // addressed the field and one that addressed it and had no answer were
    // indistinguishable.
    let omitted: Domain =
        serde_json::from_str(r#"{"kind": "memory", "id": 0, "processors": []}"#).expect("parse");
    let explicit_null: Domain = serde_json::from_str(
        r#"{"kind": "memory", "id": 0, "processors": [], "memory_bytes": null}"#,
    )
    .expect("parse");

    let DomainKind::Memory {
        memory_bytes: from_omission,
    } = omitted.kind
    else {
        panic!("memory");
    };
    let DomainKind::Memory {
        memory_bytes: from_null,
    } = explicit_null.kind
    else {
        panic!("memory");
    };

    assert_eq!(from_omission, Observed::NotObserved, "nobody addressed it");
    assert_eq!(from_null, Observed::Absent, "addressed, and no value");
    assert_ne!(from_omission, from_null);
}

#[cfg(feature = "serde")]
#[test]
fn the_three_memory_bytes_states_round_trip_distinctly() {
    // A wire format that collapsed any two would put the ambiguity back where
    // the type just removed it.
    for state in [
        Observed::Known(4096_u64),
        Observed::Absent,
        Observed::NotObserved,
    ] {
        let domain = Domain {
            kind: DomainKind::Memory {
                memory_bytes: state,
            },
            processors: ProcessorSet::empty(),
            observations: Vec::new(),
        };
        let json = serde_json::to_string(&domain).expect("serialize");
        let back: Domain = serde_json::from_str(&json).expect("deserialize");
        let DomainKind::Memory { memory_bytes } = back.kind else {
            panic!("memory");
        };
        assert_eq!(memory_bytes, state, "round trip changed {json}");
    }
}

#[test]
fn a_node_with_genuinely_no_memory_is_known_zero_not_an_absence() {
    // D-11's point, preserved: `Known(0)` is the CXL-shaped node that really
    // has no memory, and it must not be confusable with an unknown capacity.
    // This was already true of `Some(0)`; the change must not lose it.
    let zero = Observed::Known(0_u64);
    assert!(zero.was_observed());
    assert_ne!(zero, Observed::Absent);
    assert_ne!(zero, Observed::NotObserved);
}