parlov-analysis 0.7.0

Analysis engine trait and signal detection for parlov.
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
//! Unit and property tests for `surface_relevance` and helpers.
//!
//! No mocks — real `DifferentialSet` values are constructed via the public `parlov-core`
//! constructors and exercise the full classification path.

use bytes::Bytes;
use http::{HeaderMap, HeaderName, HeaderValue, StatusCode};
use parlov_core::{
    always_applicable, DifferentialSet, NormativeStrength, OracleClass, ProbeDefinition,
    ProbeExchange, ResponseSurface, SignalSurface, Technique, Vector,
};
use proptest::prelude::*;

use super::{body_diff_ratio, header_diff_ratio, surface_relevance, SurfaceDecision};

// --- fixtures --------------------------------------------------------------

fn technique_with_surface(surface: SignalSurface) -> Technique {
    Technique {
        id: "test-surface",
        name: "Test surface technique",
        oracle_class: OracleClass::Existence,
        vector: Vector::StatusCodeDiff,
        strength: NormativeStrength::Must,
        normalization_weight: Some(0.2),
        inverted_signal_weight: None,
        method_relevant: false,
        parser_relevant: false,
        applicability: always_applicable,
        contradiction_surface: surface,
    }
}

fn make_exchange(status: u16, headers: HeaderMap, body: Bytes) -> ProbeExchange {
    ProbeExchange {
        request: ProbeDefinition {
            url: "https://example.com/r/1".into(),
            method: http::Method::GET,
            headers: HeaderMap::new(),
            body: None,
        },
        response: ResponseSurface {
            status: StatusCode::from_u16(status).expect("valid status"),
            headers,
            body,
            timing_ns: 0,
        },
    }
}

fn diff_set(
    technique: Technique,
    baseline: ProbeExchange,
    probe: ProbeExchange,
) -> DifferentialSet {
    DifferentialSet {
        baseline: vec![baseline],
        probe: vec![probe],
        canonical: None,
        technique,
    }
}

fn header(name: &'static str, value: &'static str) -> (HeaderName, HeaderValue) {
    (
        HeaderName::from_static(name),
        HeaderValue::from_static(value),
    )
}

fn headers_from(pairs: &[(HeaderName, HeaderValue)]) -> HeaderMap {
    let mut m = HeaderMap::new();
    for (n, v) in pairs {
        m.insert(n.clone(), v.clone());
    }
    m
}

// --- surface_relevance: edge cases ----------------------------------------

#[test]
fn empty_differential_set_returns_reached_one() {
    let t = technique_with_surface(SignalSurface::Status);
    let ds = DifferentialSet {
        baseline: vec![],
        probe: vec![],
        canonical: None,
        technique: t,
    };
    let decision = surface_relevance(&t, &ds);
    assert_eq!(decision, SurfaceDecision::Reached(1.0));
}

#[test]
fn empty_baseline_returns_reached_one() {
    let t = technique_with_surface(SignalSurface::Status);
    let ds = DifferentialSet {
        baseline: vec![],
        probe: vec![make_exchange(200, HeaderMap::new(), Bytes::new())],
        canonical: None,
        technique: t,
    };
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
}

// --- Status-surface: byte-identical body and headers ----------------------

#[test]
fn status_surface_identical_body_and_headers_reaches_one() {
    let t = technique_with_surface(SignalSurface::Status);
    let body = Bytes::from_static(b"{\"ok\":true}");
    let ds = diff_set(
        t,
        make_exchange(200, HeaderMap::new(), body.clone()),
        make_exchange(200, HeaderMap::new(), body),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
}

// --- Status-surface: body length differs significantly --------------------

#[test]
fn status_surface_body_length_diff_above_threshold_blocks() {
    let t = technique_with_surface(SignalSurface::Status);
    let small = Bytes::from_static(b"{\"ok\":true}");
    let large = Bytes::from_static(
        b"{\"id\":1,\"name\":\"Alice\",\"email\":\"alice@example.com\",\"role\":\"admin\"}",
    );
    let ds = diff_set(
        t,
        make_exchange(200, HeaderMap::new(), large),
        make_exchange(200, HeaderMap::new(), small),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Blocked);
}

#[test]
fn status_surface_body_length_diff_below_threshold_reaches() {
    let t = technique_with_surface(SignalSurface::Status);
    // 100 'x' vs 95 'x': overlap differing=0, length_diff=5, max=100 → 0.05 < 0.10.
    let a = Bytes::from(vec![b'x'; 100]);
    let b = Bytes::from(vec![b'x'; 95]);
    let ds = diff_set(
        t,
        make_exchange(200, HeaderMap::new(), a),
        make_exchange(200, HeaderMap::new(), b),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
}

#[test]
fn status_surface_body_length_diff_at_threshold_does_not_block() {
    // Body lengths exactly at the 10% threshold do NOT block (`>` not `>=`).
    // 100 'x' vs 90 'x': overlap differing=0, length_diff=10, max=100 → 0.10, not > 0.10.
    let t = technique_with_surface(SignalSurface::Status);
    let a = Bytes::from(vec![b'x'; 100]);
    let b = Bytes::from(vec![b'x'; 90]);
    let ds = diff_set(
        t,
        make_exchange(200, HeaderMap::new(), a),
        make_exchange(200, HeaderMap::new(), b),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
}

// --- Status-surface: equal-length divergent body content (regression) ------

#[test]
fn status_surface_equal_length_divergent_body_blocks() {
    // Regression for the byte-length-only bug: equal-length bodies with completely different
    // content used to return ratio 0.0 and reach. The corrected metric counts byte-position
    // divergence and blocks.
    let t = technique_with_surface(SignalSurface::Status);
    let a = Bytes::from_static(br#"{"id":1,"data":"abc"}"#); // 21 bytes
    let b = Bytes::from_static(br#"{"id":2,"data":"xyz"}"#); // 21 bytes
    assert_eq!(a.len(), b.len(), "fixture invariant: equal-length bodies");
    let ds = diff_set(
        t,
        make_exchange(200, HeaderMap::new(), a),
        make_exchange(200, HeaderMap::new(), b),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Blocked);
}

// --- Status-surface: header-key set divergence ----------------------------

#[test]
fn status_surface_majority_header_keys_differ_blocks() {
    let t = technique_with_surface(SignalSurface::Status);
    // Baseline {a,b}; Probe {c,d,e}. Union={a,b,c,d,e}=5; diverged=5 (none shared) → 1.0 > 0.5.
    let baseline_h = headers_from(&[header("a", "1"), header("b", "1")]);
    let probe_h = headers_from(&[header("c", "1"), header("d", "1"), header("e", "1")]);
    let ds = diff_set(
        t,
        make_exchange(200, baseline_h, Bytes::new()),
        make_exchange(200, probe_h, Bytes::new()),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Blocked);
}

#[test]
fn status_surface_minor_header_diff_reaches() {
    let t = technique_with_surface(SignalSurface::Status);
    // Baseline {a,b,c,d} all "1"; Probe {a,b,c,e} all "1". Shared {a,b,c} same values → 0
    // diverged. {d}, {e} disjoint → 2 diverged. Union=5. Ratio=2/5=0.40 < 0.50.
    let baseline_h = headers_from(&[
        header("a", "1"),
        header("b", "1"),
        header("c", "1"),
        header("d", "1"),
    ]);
    let probe_h = headers_from(&[
        header("a", "1"),
        header("b", "1"),
        header("c", "1"),
        header("e", "1"),
    ]);
    let ds = diff_set(
        t,
        make_exchange(200, baseline_h, Bytes::new()),
        make_exchange(200, probe_h, Bytes::new()),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
}

#[test]
fn status_surface_header_diff_at_threshold_does_not_block() {
    // Baseline {a,b,c} all "1"; Probe {a,b,d} all "1". Shared {a,b} same values → 0; {c},{d}
    // diverged → 2. Union=4. Ratio=2/4=0.50 (not strictly >). No block.
    let t = technique_with_surface(SignalSurface::Status);
    let baseline_h = headers_from(&[header("a", "1"), header("b", "1"), header("c", "1")]);
    let probe_h = headers_from(&[header("a", "1"), header("b", "1"), header("d", "1")]);
    let ds = diff_set(
        t,
        make_exchange(200, baseline_h, Bytes::new()),
        make_exchange(200, probe_h, Bytes::new()),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
}

// --- non-Status surfaces: gate is inert -----------------------------------

#[test]
fn body_surface_returns_reached_one_even_with_body_diff() {
    let t = technique_with_surface(SignalSurface::Body);
    let large = Bytes::from(vec![b'x'; 1000]);
    let small = Bytes::from_static(b"x");
    let ds = diff_set(
        t,
        make_exchange(200, HeaderMap::new(), large),
        make_exchange(200, HeaderMap::new(), small),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
}

#[test]
fn headers_surface_returns_reached_one_even_with_header_diff() {
    let t = technique_with_surface(SignalSurface::Headers);
    let baseline_h = headers_from(&[header("a", "1"), header("b", "1")]);
    let probe_h = headers_from(&[header("c", "1"), header("d", "1"), header("e", "1")]);
    let ds = diff_set(
        t,
        make_exchange(200, baseline_h, Bytes::new()),
        make_exchange(200, probe_h, Bytes::new()),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
}

#[test]
fn timing_surface_returns_reached_one() {
    let t = technique_with_surface(SignalSurface::Timing);
    let ds = diff_set(
        t,
        make_exchange(200, HeaderMap::new(), Bytes::from_static(b"a")),
        make_exchange(200, HeaderMap::new(), Bytes::from_static(b"b")),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
}

#[test]
fn composite_surface_returns_reached_one() {
    let t = technique_with_surface(SignalSurface::Composite);
    let large = Bytes::from(vec![b'x'; 1000]);
    let small = Bytes::from_static(b"x");
    let ds = diff_set(
        t,
        make_exchange(200, HeaderMap::new(), large),
        make_exchange(200, HeaderMap::new(), small),
    );
    assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
}

// --- body_diff_ratio -------------------------------------------------------

#[test]
fn body_diff_ratio_identical_bytes_is_zero() {
    let a = Bytes::from_static(b"hello world");
    assert!(body_diff_ratio(&a, &a).abs() < f64::EPSILON);
}

#[test]
fn body_diff_ratio_identical_long_random_bytes_is_zero() {
    let a = Bytes::from(
        (0u16..256)
            .map(|i| u8::try_from(i & 0xff).unwrap())
            .collect::<Vec<u8>>(),
    );
    assert!(body_diff_ratio(&a, &a).abs() < f64::EPSILON);
}

#[test]
fn body_diff_ratio_one_empty_is_one() {
    let empty = Bytes::new();
    let big = Bytes::from(vec![b'x'; 100]);
    assert!((body_diff_ratio(&empty, &big) - 1.0).abs() < f64::EPSILON);
    assert!((body_diff_ratio(&big, &empty) - 1.0).abs() < f64::EPSILON);
}

#[test]
fn body_diff_ratio_both_empty_is_zero() {
    let empty = Bytes::new();
    assert!(body_diff_ratio(&empty, &empty).abs() < f64::EPSILON);
}

#[test]
fn body_diff_ratio_same_length_all_bytes_differ_is_one() {
    let a = Bytes::from(vec![b'a'; 50]);
    let b = Bytes::from(vec![b'b'; 50]);
    assert!((body_diff_ratio(&a, &b) - 1.0).abs() < f64::EPSILON);
}

#[test]
fn body_diff_ratio_same_length_half_bytes_differ_is_half() {
    // First 50 bytes equal, last 50 differ.
    let mut a = vec![b'x'; 100];
    let mut b = vec![b'x'; 100];
    for byte in b.iter_mut().take(100).skip(50) {
        *byte = b'y';
    }
    a[..50].fill(b'x');
    let ar = body_diff_ratio(&Bytes::from(a), &Bytes::from(b));
    assert!((ar - 0.5).abs() < f64::EPSILON, "expected 0.5, got {ar}");
}

#[test]
fn body_diff_ratio_different_lengths_identical_overlap() {
    // 100 'x' vs 75 'x': overlap differing=0, length_diff=25, max=100 → 0.25.
    let a = Bytes::from(vec![b'x'; 100]);
    let b = Bytes::from(vec![b'x'; 75]);
    assert!((body_diff_ratio(&a, &b) - 0.25).abs() < f64::EPSILON);
}

#[test]
fn body_diff_ratio_equal_length_divergent_content_is_above_threshold() {
    // Regression test for the byte-length-only bug. Both bodies are 21 bytes; the old metric
    // returned 0.0; the new metric must return a ratio > BODY_SURFACE_MISMATCH_THRESHOLD (0.10).
    let a = Bytes::from_static(br#"{"id":1,"data":"abc"}"#);
    let b = Bytes::from_static(br#"{"id":2,"data":"xyz"}"#);
    let r = body_diff_ratio(&a, &b);
    assert!(r > 0.10, "expected > 0.10, got {r}");
}

// --- header_diff_ratio -----------------------------------------------------

#[test]
fn header_diff_ratio_identical_keys_and_values_is_zero() {
    let h = headers_from(&[header("a", "1"), header("b", "1")]);
    assert!(header_diff_ratio(&h, &h).abs() < f64::EPSILON);
}

#[test]
fn header_diff_ratio_completely_disjoint_keys_is_one() {
    let a = headers_from(&[header("a", "1"), header("b", "1")]);
    let b = headers_from(&[header("c", "1"), header("d", "1")]);
    assert!((header_diff_ratio(&a, &b) - 1.0).abs() < f64::EPSILON);
}

#[test]
fn header_diff_ratio_both_empty_is_zero() {
    let a = HeaderMap::new();
    let b = HeaderMap::new();
    assert!(header_diff_ratio(&a, &b).abs() < f64::EPSILON);
}

#[test]
fn header_diff_ratio_same_key_different_value_counts_as_diverged() {
    // Regression test for the key-only bug. Both maps have `cache-control`; under the old
    // metric the symmetric-difference was 0 (key sets match). The corrected metric must
    // count value divergence and return > 0.0.
    let a = headers_from(&[header("cache-control", "public")]);
    let b = headers_from(&[header("cache-control", "private")]);
    let r = header_diff_ratio(&a, &b);
    assert!(r > 0.0, "expected > 0.0 for differing values, got {r}");
    assert!(
        (r - 1.0).abs() < f64::EPSILON,
        "1 diverged of 1 union → 1.0"
    );
}

#[test]
fn header_diff_ratio_key_only_in_one_side_counts_as_diverged() {
    let a = headers_from(&[header("a", "1"), header("b", "1")]);
    let b = headers_from(&[header("a", "1")]);
    // Shared {a} same value → 0 diverged; {b} only in baseline → 1 diverged. Union=2. Ratio=0.5.
    let r = header_diff_ratio(&a, &b);
    assert!((r - 0.5).abs() < f64::EPSILON, "expected 0.5, got {r}");
}

#[test]
fn header_diff_ratio_multi_value_headers_full_sequence_compared() {
    // `set-cookie` with two values per side: identical sequences → 0.0.
    let mut a = HeaderMap::new();
    a.append(
        HeaderName::from_static("set-cookie"),
        HeaderValue::from_static("session=abc"),
    );
    a.append(
        HeaderName::from_static("set-cookie"),
        HeaderValue::from_static("csrf=xyz"),
    );
    let mut b = HeaderMap::new();
    b.append(
        HeaderName::from_static("set-cookie"),
        HeaderValue::from_static("session=abc"),
    );
    b.append(
        HeaderName::from_static("set-cookie"),
        HeaderValue::from_static("csrf=xyz"),
    );
    assert!(header_diff_ratio(&a, &b).abs() < f64::EPSILON);

    // Same key, different second value → diverged.
    let mut c = HeaderMap::new();
    c.append(
        HeaderName::from_static("set-cookie"),
        HeaderValue::from_static("session=abc"),
    );
    c.append(
        HeaderName::from_static("set-cookie"),
        HeaderValue::from_static("csrf=DIFFERENT"),
    );
    let r = header_diff_ratio(&a, &c);
    assert!(r > 0.0, "expected > 0.0 for differing multi-value, got {r}");
}

// --- SurfaceDecision accessors --------------------------------------------

#[test]
fn confidence_of_reached_returns_inner() {
    assert!((SurfaceDecision::Reached(0.7).confidence() - 0.7).abs() < f64::EPSILON);
}

#[test]
fn confidence_of_blocked_is_zero() {
    assert!(SurfaceDecision::Blocked.confidence().abs() < f64::EPSILON);
}

// --- property tests --------------------------------------------------------

proptest! {
    /// `surface_relevance` is referentially transparent.
    #[test]
    fn surface_relevance_referentially_transparent(
        body_a_len in 0usize..=200,
        body_b_len in 0usize..=200,
    ) {
        let t = technique_with_surface(SignalSurface::Status);
        let a = Bytes::from(vec![b'x'; body_a_len]);
        let b = Bytes::from(vec![b'x'; body_b_len]);
        let ds = diff_set(
            t,
            make_exchange(200, HeaderMap::new(), a),
            make_exchange(200, HeaderMap::new(), b),
        );
        let d1 = surface_relevance(&t, &ds);
        let d2 = surface_relevance(&t, &ds);
        prop_assert_eq!(d1, d2);
    }

    /// For any `Status`-surface technique with byte-identical body and headers, returns `Reached(1.0)`.
    #[test]
    fn status_surface_identical_always_reaches(body_len in 0usize..=500) {
        let t = technique_with_surface(SignalSurface::Status);
        let body = Bytes::from(vec![b'x'; body_len]);
        let ds = diff_set(
            t,
            make_exchange(200, HeaderMap::new(), body.clone()),
            make_exchange(200, HeaderMap::new(), body),
        );
        prop_assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
    }

    /// For any non-Status surface, gate is inert regardless of body/header content.
    #[test]
    fn non_status_surface_always_reaches(
        surface_idx in 0usize..4,
        body_a_len in 0usize..=500,
        body_b_len in 0usize..=500,
    ) {
        let surface = match surface_idx {
            0 => SignalSurface::Body,
            1 => SignalSurface::Headers,
            2 => SignalSurface::Timing,
            _ => SignalSurface::Composite,
        };
        let t = technique_with_surface(surface);
        let a = Bytes::from(vec![b'x'; body_a_len]);
        let b = Bytes::from(vec![b'y'; body_b_len]);
        let ds = diff_set(
            t,
            make_exchange(200, HeaderMap::new(), a),
            make_exchange(200, HeaderMap::new(), b),
        );
        prop_assert_eq!(surface_relevance(&t, &ds), SurfaceDecision::Reached(1.0));
    }

    /// `body_diff_ratio` is bounded in `[0.0, 1.0]` for any byte content.
    #[test]
    fn body_diff_ratio_bounded(
        a in proptest::collection::vec(any::<u8>(), 0..=500),
        b in proptest::collection::vec(any::<u8>(), 0..=500),
    ) {
        let r = body_diff_ratio(&Bytes::from(a), &Bytes::from(b));
        prop_assert!(r >= 0.0);
        prop_assert!(r <= 1.0);
    }

    /// `body_diff_ratio` is symmetric for any byte content.
    #[test]
    fn body_diff_ratio_symmetric(
        a in proptest::collection::vec(any::<u8>(), 0..=500),
        b in proptest::collection::vec(any::<u8>(), 0..=500),
    ) {
        let ab = body_diff_ratio(&Bytes::from(a.clone()), &Bytes::from(b.clone()));
        let ba = body_diff_ratio(&Bytes::from(b), &Bytes::from(a));
        prop_assert!((ab - ba).abs() < f64::EPSILON);
    }

    /// `body_diff_ratio(a, a) == 0.0` for any input.
    #[test]
    fn body_diff_ratio_self_is_zero(a in proptest::collection::vec(any::<u8>(), 0..=500)) {
        let bytes = Bytes::from(a);
        prop_assert!(body_diff_ratio(&bytes, &bytes).abs() < f64::EPSILON);
    }

    /// `header_diff_ratio` is bounded in `[0.0, 1.0]`.
    #[test]
    fn header_diff_ratio_bounded(seed in 0u32..=10) {
        let mut a = HeaderMap::new();
        let mut b = HeaderMap::new();
        for i in 0..(seed % 5) {
            let name = HeaderName::from_lowercase(format!("a-{i}").as_bytes()).unwrap();
            a.insert(name, HeaderValue::from_static("1"));
        }
        for i in 0..((seed * 3) % 7) {
            let name = HeaderName::from_lowercase(format!("b-{i}").as_bytes()).unwrap();
            b.insert(name, HeaderValue::from_static("1"));
        }
        let r = header_diff_ratio(&a, &b);
        prop_assert!(r >= 0.0);
        prop_assert!(r <= 1.0);
    }

    /// `header_diff_ratio` is symmetric.
    #[test]
    fn header_diff_ratio_symmetric(seed in 0u32..=10) {
        let mut a = HeaderMap::new();
        let mut b = HeaderMap::new();
        for i in 0..(seed % 5) {
            let name = HeaderName::from_lowercase(format!("h-{i}").as_bytes()).unwrap();
            let val = if i % 2 == 0 { "x" } else { "y" };
            a.insert(name, HeaderValue::from_str(val).unwrap());
        }
        for i in 0..((seed * 3) % 7) {
            let name = HeaderName::from_lowercase(format!("h-{i}").as_bytes()).unwrap();
            let val = if i % 3 == 0 { "x" } else { "z" };
            b.insert(name, HeaderValue::from_str(val).unwrap());
        }
        let ab = header_diff_ratio(&a, &b);
        let ba = header_diff_ratio(&b, &a);
        prop_assert!((ab - ba).abs() < f64::EPSILON);
    }

    /// `header_diff_ratio(map, map) == 0.0` for any input.
    #[test]
    fn header_diff_ratio_self_is_zero(seed in 0u32..=10) {
        let mut m = HeaderMap::new();
        for i in 0..(seed % 6) {
            let name = HeaderName::from_lowercase(format!("k-{i}").as_bytes()).unwrap();
            let val = format!("v-{i}");
            m.insert(name, HeaderValue::from_str(&val).unwrap());
        }
        prop_assert!(header_diff_ratio(&m, &m).abs() < f64::EPSILON);
    }
}