scrape-le 0.1.4

Check whether a page is scrapeable before the scraper is written, and say when it cannot tell
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
//! `check()` — evidence in, report out. Both surfaces call this and
//! nothing else, so neither can grow its own copy of a rule. Pure:
//! `fetch.rs` and `render.rs` gather the evidence, this decides.

use std::collections::HashMap;
use std::sync::Arc;

use serde_json::json;

use super::antibot::match_headers;
use super::auth::detect_authentication;
use super::ratelimit::detect_rate_limit;
use super::report::{
    CheckStatus, Checks, Finding, Report, RobotsReport, Severity, Timing, Verdict,
};
use super::robots::RobotsDocument;
use super::signatures::signatures;

/// What a fetch produced. `robots` is `None` when robots.txt did not
/// exist or was unreachable — distinct from an empty file.
///
/// It arrives parsed, and shared: one origin's document answers for
/// every URL on it in a run, so the rules are compiled once rather than
/// once per path. Which document is a question for `fetch.rs`; what it
/// says about a path is decided here.
pub(crate) struct Evidence {
    pub(crate) url: String,
    pub(crate) final_url: String,
    pub(crate) status: Option<u16>,
    /// lowercased header names, as delivered
    pub(crate) headers: HashMap<String, String>,
    pub(crate) body_html: String,
    pub(crate) robots: Option<Arc<RobotsDocument>>,
    pub(crate) render: Option<RenderEvidence>,
    pub(crate) fetch_ms: u64,
    pub(crate) total_ms: u64,
}

/// What only a rendered page can show. Present ⇒ the antibot and auth
/// checks ran completely and `clear` becomes reachable. `status` and
/// `headers` come from the document's own CDP response, so a rendered
/// check needs no second request to the site.
pub(crate) struct RenderEvidence {
    pub(crate) final_url: Option<String>,
    pub(crate) title: Option<String>,
    pub(crate) body_html: String,
    pub(crate) status: Option<u16>,
    pub(crate) headers: Option<HashMap<String, String>>,
    /// per vendor key, the extension's single-evaluate probe result
    pub(crate) probes: HashMap<String, ProbeResult>,
    pub(crate) auth: AuthPageEvidence,
    pub(crate) console_errors: Vec<String>,
    pub(crate) screenshot: Option<String>,
    pub(crate) render_ms: u64,
}

impl Evidence {
    /// Evidence from a render alone: no HTTP fetch of the page ever
    /// happened, so everything but robots.txt comes from the browser.
    pub(crate) fn from_render(
        url: &str,
        render: RenderEvidence,
        robots: Option<Arc<RobotsDocument>>,
    ) -> Self {
        Self {
            url: url.to_string(),
            final_url: render.final_url.clone().unwrap_or_else(|| url.to_string()),
            status: render.status,
            headers: render.headers.clone().unwrap_or_default(),
            body_html: render.body_html.clone(),
            robots,
            render: Some(render),
            fetch_ms: 0,
            total_ms: 0,
        }
    }
}

pub(crate) struct ProbeResult {
    pub(crate) script: bool,
    pub(crate) selector: bool,
    pub(crate) global: bool,
}

pub(crate) struct AuthPageEvidence {
    pub(crate) has_password_input: bool,
    pub(crate) has_form: bool,
    pub(crate) has_username_input: bool,
    pub(crate) form_action: Option<String>,
    pub(crate) keyword: Option<String>,
}

/// Caller-supplied knobs the decision layer needs. Defaults are the
/// extension's behaviour: generic robots groups, no batch index.
#[derive(Debug, Default, Clone)]
pub(crate) struct CheckOptions {
    /// RFC 9309 product token to evaluate robots.txt against; `None`
    /// evaluates the generic (`User-agent: *`) rules only
    pub(crate) agent: Option<String>,
    /// the URL's position in a batch input
    pub(crate) index: Option<usize>,
}

pub(crate) fn check(evidence: &Evidence, options: &CheckOptions) -> Report {
    let mut findings: Vec<Finding> = Vec::new();

    check_antibot(evidence, &mut findings);
    check_rate_limit(evidence, &mut findings);
    let (robots_status, robots) = check_robots(evidence, options, &mut findings);
    check_auth(evidence, &mut findings);

    let rendered = evidence.render.is_some();
    let dom_status = if rendered {
        CheckStatus::Ran
    } else {
        CheckStatus::Partial
    };
    let checks = Checks {
        antibot: dom_status,
        rate_limit: CheckStatus::Ran,
        robots: robots_status,
        auth: dom_status,
    };
    let checks_skipped = skipped_names(&checks);
    let verdict = decide(&findings, &checks_skipped);

    let render = evidence.render.as_ref();
    let final_url = render
        .and_then(|r| r.final_url.clone())
        .unwrap_or_else(|| evidence.final_url.clone());
    let title = render
        .and_then(|r| r.title.clone())
        .or_else(|| extract_title(&evidence.body_html));

    Report {
        schema: 1,
        index: options.index,
        url: evidence.url.clone(),
        final_url,
        status: evidence.status,
        title,
        verdict,
        findings,
        checks,
        checks_skipped,
        robots,
        console_errors: render.map(|r| r.console_errors.clone()).unwrap_or_default(),
        screenshot: render.and_then(|r| r.screenshot.clone()),
        timing_ms: Timing {
            fetch: evidence.fetch_ms,
            render: render.map(|r| r.render_ms),
            total: evidence.total_ms,
        },
    }
}

/// One header pass, then the page-probe pass for vendors the headers
/// did not already name — the extension's order, so the reported
/// evidence label is the same one it would print.
fn check_antibot(evidence: &Evidence, findings: &mut Vec<Finding>) {
    let mut detected: Vec<&str> = Vec::new();
    for signature in signatures() {
        let Some(matched) = match_headers(&evidence.headers, signature) else {
            continue;
        };
        detected.push(&signature.key);
        findings.push(Finding {
            kind: "antibot".to_string(),
            severity: Severity::Warns,
            detail: matched.detail,
            evidence: json!({ "signal": matched.header, "source": "response-header" }),
        });
    }
    let Some(render) = &evidence.render else {
        return;
    };
    for signature in signatures() {
        if detected.contains(&signature.key.as_str()) {
            continue;
        }
        let Some(probe) = render.probes.get(&signature.key) else {
            continue;
        };
        // First matching evidence wins, so the label names how it was
        // found — same precedence as the extension.
        let source = if probe.script {
            "script src"
        } else if probe.selector {
            "DOM element"
        } else if probe.global {
            "window global"
        } else {
            continue;
        };
        findings.push(Finding {
            kind: "antibot".to_string(),
            severity: Severity::Warns,
            detail: format!("{} ({source})", signature.label),
            evidence: json!({ "source": source }),
        });
    }
}

fn check_rate_limit(evidence: &Evidence, findings: &mut Vec<Finding>) {
    let rate_limit = detect_rate_limit(&evidence.headers, evidence.status);
    if !rate_limit.detected {
        return;
    }
    let actively_limited = evidence.status == Some(429);
    let severity = if actively_limited {
        Severity::Blocks
    } else {
        Severity::Warns
    };
    let detail = if actively_limited {
        "HTTP 429: rate limited right now".to_string()
    } else {
        "Rate-limit headers advertised".to_string()
    };
    findings.push(Finding {
        kind: "rate_limit".to_string(),
        severity,
        detail,
        evidence: json!({
            "limit": rate_limit.limit,
            "remaining": rate_limit.remaining,
            "reset": rate_limit.reset,
            "retry_after": rate_limit.retry_after,
            "status": evidence.status,
        }),
    });
}

fn check_robots(
    evidence: &Evidence,
    options: &CheckOptions,
    findings: &mut Vec<Finding>,
) -> (CheckStatus, Option<RobotsReport>) {
    let Some(document) = &evidence.robots else {
        // no robots.txt is an answer, not a failure: nothing forbids
        return (
            CheckStatus::Ran,
            Some(RobotsReport {
                exists: false,
                allows_crawling: true,
                agent: options.agent.clone().unwrap_or_else(|| "*".to_string()),
                crawl_delay: None,
                disallowed_paths: Vec::new(),
                sitemaps: Vec::new(),
            }),
        );
    };
    let Ok(parsed_url) = url::Url::parse(&evidence.url) else {
        return (CheckStatus::Skipped, None);
    };
    let info = document.evaluate(parsed_url.path(), options.agent.as_deref());
    let report = RobotsReport {
        exists: info.exists,
        allows_crawling: info.allows_crawling,
        agent: info.agent.clone(),
        crawl_delay: info.crawl_delay,
        disallowed_paths: info.disallowed_paths.clone(),
        sitemaps: info.sitemaps.clone(),
    };
    if info.allows_crawling {
        return (CheckStatus::Ran, Some(report));
    }
    let rule = info.matched_rule.clone().unwrap_or_default();
    findings.push(Finding {
        kind: "robots".to_string(),
        severity: Severity::Blocks,
        detail: format!("Disallow: {rule} for User-agent: {}", info.agent),
        evidence: json!({ "rule": format!("Disallow: {rule}"), "agent": info.agent }),
    });
    (CheckStatus::Ran, Some(report))
}

fn check_auth(evidence: &Evidence, findings: &mut Vec<Finding>) {
    let final_url = evidence
        .render
        .as_ref()
        .and_then(|r| r.final_url.as_deref())
        .unwrap_or(&evidence.final_url);
    let page = evidence.render.as_ref().map(|r| &r.auth);
    let auth = detect_authentication(evidence.status, final_url, page);
    if !auth.required {
        return;
    }
    findings.push(Finding {
        kind: "auth".to_string(),
        severity: Severity::Blocks,
        detail: "Authentication required".to_string(),
        evidence: json!({
            "indicators": auth.indicators,
            "type": auth.auth_type,
            "login_url": auth.login_url,
            "paired_username": auth.paired_username,
        }),
    });
}

/// `clear` requires completeness; `restricted` does not. A positive
/// finding does not need every check to have run — a negative one does.
fn decide(findings: &[Finding], checks_skipped: &[String]) -> Verdict {
    if !findings.is_empty() {
        return Verdict::Restricted;
    }
    if checks_skipped.is_empty() {
        return Verdict::Clear;
    }
    Verdict::Inconclusive
}

fn skipped_names(checks: &Checks) -> Vec<String> {
    let mut names = Vec::new();
    for (name, status) in [
        ("antibot", checks.antibot),
        ("rate_limit", checks.rate_limit),
        ("robots", checks.robots),
        ("auth", checks.auth),
    ] {
        if status != CheckStatus::Ran {
            names.push(name.to_string());
        }
    }
    names
}

/// Raw-HTML `<title>` extraction — the fallback when no render ran.
///
/// **`to_ascii_lowercase`, not `to_lowercase`.** The offsets below are
/// found in the lowered copy and used to slice the original, and
/// `to_lowercase` is not length-preserving: `\u{130}` grows a byte and
/// `\u{212a}` shrinks one, so a page carrying either before its title
/// slid every offset and the slice landed inside a character. Tag names
/// are ASCII, so the ASCII fold is both correct and the same length.
fn extract_title(html: &str) -> Option<String> {
    let lower = html.to_ascii_lowercase();
    let open = lower.find("<title")?;
    let after_open = open + lower[open..].find('>')? + 1;
    let close = after_open + lower[after_open..].find("</title>")?;
    let title = html.get(after_open..close)?.trim();
    if title.is_empty() {
        return None;
    }
    Some(title.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn evidence(status: u16, headers: &[(&str, &str)], robots: Option<&str>) -> Evidence {
        Evidence {
            url: "https://example.com/search?q=x".to_string(),
            final_url: "https://example.com/search?q=x".to_string(),
            status: Some(status),
            headers: headers
                .iter()
                .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
                .collect(),
            body_html: "<html><title>Search — Example</title></html>".to_string(),
            robots: robots.map(|body| Arc::new(RobotsDocument::parse(body))),
            render: None,
            fetch_ms: 10,
            total_ms: 12,
        }
    }

    fn clean_render() -> RenderEvidence {
        RenderEvidence {
            final_url: Some("https://example.com/search?q=x".to_string()),
            title: Some("Search — Example".to_string()),
            body_html: "<html><title>Search — Example</title></html>".to_string(),
            status: Some(200),
            headers: Some(HashMap::new()),
            probes: HashMap::new(),
            auth: AuthPageEvidence {
                has_password_input: false,
                has_form: false,
                has_username_input: false,
                form_action: None,
                keyword: None,
            },
            console_errors: Vec::new(),
            screenshot: Some("./scrape-le-example-com-2026-08-06.png".to_string()),
            render_ms: 1500,
        }
    }

    #[test]
    fn clean_fetch_without_render_is_inconclusive_never_clear() {
        let report = check(&evidence(200, &[], None), &CheckOptions::default());
        assert_eq!(report.verdict, Verdict::Inconclusive);
        assert!(report.findings.is_empty());
        assert_eq!(report.checks_skipped, ["antibot", "auth"]);
    }

    #[test]
    fn clean_rendered_run_is_clear() {
        let mut e = evidence(200, &[], None);
        e.render = Some(clean_render());
        let report = check(&e, &CheckOptions::default());
        assert_eq!(report.verdict, Verdict::Clear);
        assert!(report.checks_skipped.is_empty());
        assert_eq!(report.timing_ms.render, Some(1500));
        assert!(report.screenshot.is_some());
    }

    #[test]
    fn page_probe_warns_with_the_extension_precedence() {
        let mut e = evidence(200, &[], None);
        let mut render = clean_render();
        render.probes.insert(
            "recaptcha".to_string(),
            ProbeResult {
                script: true,
                selector: true,
                global: true,
            },
        );
        e.render = Some(render);
        let report = check(&e, &CheckOptions::default());
        assert_eq!(report.verdict, Verdict::Restricted);
        let finding = &report.findings[0];
        assert_eq!(finding.detail, "reCAPTCHA (script src)");
    }

    #[test]
    fn header_match_wins_over_probe_for_the_same_vendor() {
        let mut e = evidence(200, &[("cf-ray", "8abc-EWR")], None);
        let mut render = clean_render();
        render.probes.insert(
            "cloudflare".to_string(),
            ProbeResult {
                script: false,
                selector: false,
                global: true,
            },
        );
        e.render = Some(render);
        let report = check(&e, &CheckOptions::default());
        let antibot: Vec<&Finding> = report
            .findings
            .iter()
            .filter(|f| f.kind == "antibot")
            .collect();
        assert_eq!(antibot.len(), 1);
        assert_eq!(antibot[0].detail, "Cloudflare (cf-ray header)");
    }

    #[test]
    fn login_form_blocks_on_a_rendered_page() {
        let mut e = evidence(200, &[], None);
        let mut render = clean_render();
        render.auth.has_password_input = true;
        render.auth.has_form = true;
        render.auth.form_action = Some("https://example.com/session".to_string());
        e.render = Some(render);
        let report = check(&e, &CheckOptions::default());
        let finding = report
            .findings
            .iter()
            .find(|f| f.kind == "auth")
            .expect("auth finding");
        assert_eq!(finding.severity, Severity::Blocks);
        assert_eq!(finding.evidence["login_url"], "https://example.com/session");
    }

    #[test]
    fn robots_disallow_blocks_and_names_the_rule() {
        let robots = "User-agent: *\nDisallow: /search";
        let report = check(&evidence(200, &[], Some(robots)), &CheckOptions::default());
        assert_eq!(report.verdict, Verdict::Restricted);
        let finding = &report.findings[0];
        assert_eq!(finding.kind, "robots");
        assert_eq!(finding.severity, Severity::Blocks);
        assert_eq!(finding.detail, "Disallow: /search for User-agent: *");
    }

    #[test]
    fn cloudflare_header_warns_with_evidence() {
        let report = check(
            &evidence(200, &[("cf-ray", "8abc-EWR")], None),
            &CheckOptions::default(),
        );
        assert_eq!(report.verdict, Verdict::Restricted);
        let finding = &report.findings[0];
        assert_eq!(finding.kind, "antibot");
        assert_eq!(finding.detail, "Cloudflare (cf-ray header)");
        assert_eq!(finding.evidence["signal"], "cf-ray");
    }

    #[test]
    fn status_429_blocks() {
        let report = check(&evidence(429, &[], None), &CheckOptions::default());
        let finding = report
            .findings
            .iter()
            .find(|f| f.kind == "rate_limit")
            .expect("rate_limit finding");
        assert_eq!(finding.severity, Severity::Blocks);
    }

    #[test]
    fn status_401_blocks_via_auth() {
        let report = check(&evidence(401, &[], None), &CheckOptions::default());
        let finding = report
            .findings
            .iter()
            .find(|f| f.kind == "auth")
            .expect("auth finding");
        assert_eq!(finding.severity, Severity::Blocks);
        assert_eq!(report.verdict, Verdict::Restricted);
    }

    #[test]
    fn title_extracted_from_raw_html() {
        let report = check(&evidence(200, &[], None), &CheckOptions::default());
        assert_eq!(report.title.as_deref(), Some("Search — Example"));
    }

    /// **Regression.** The offsets are found in a lowered copy and used
    /// against the original, so the fold has to preserve length.
    /// `to_lowercase` does not: U+0130 grows a byte and U+212A shrinks
    /// one, and a page carrying either before its title slid every
    /// offset until the slice landed inside a character.
    #[test]
    fn a_title_survives_characters_that_change_length_when_lowered() {
        for marker in ['\u{130}', '\u{212a}', '\u{1e9e}'] {
            let mut e = evidence(200, &[], None);
            e.body_html =
                format!("<html><body>{marker}</body><title>Search — Example</title></html>");
            let report = check(&e, &CheckOptions::default());
            assert_eq!(
                report.title.as_deref(),
                Some("Search — Example"),
                "a {marker:?} before the title moved the slice"
            );
        }
    }

    /// Tag names are ASCII, so the fold only has to reach them.
    #[test]
    fn an_upper_case_title_tag_is_still_found() {
        let mut e = evidence(200, &[], None);
        e.body_html = "<HTML><TITLE>Search — Example</TITLE></HTML>".to_string();
        let report = check(&e, &CheckOptions::default());
        assert_eq!(report.title.as_deref(), Some("Search — Example"));
    }

    /// The verdict rules, held over every combination rather than the
    /// cases someone thought of:
    ///
    /// - `clear` requires zero findings **and** zero skipped checks;
    /// - no set of `warns` findings ever produces `blocked` — that
    ///   verdict belongs to a page that could not be reached at all;
    /// - a positive finding does not need completeness, so a partial
    ///   run can still say `restricted` honestly.
    #[test]
    fn the_verdict_rules_hold_over_every_combination() {
        let statuses = [CheckStatus::Ran, CheckStatus::Partial, CheckStatus::Skipped];
        for antibot in statuses {
            for auth in statuses {
                for robots in statuses {
                    for finding_count in 0..3 {
                        for severity in [Severity::Warns, Severity::Blocks] {
                            let findings: Vec<Finding> = (0..finding_count)
                                .map(|i| Finding {
                                    kind: "antibot".to_string(),
                                    severity,
                                    detail: format!("probe {i}"),
                                    evidence: json!({}),
                                })
                                .collect();
                            let checks = Checks {
                                antibot,
                                rate_limit: CheckStatus::Ran,
                                robots,
                                auth,
                            };
                            let skipped = skipped_names(&checks);
                            let verdict = decide(&findings, &skipped);

                            assert_ne!(
                                verdict,
                                Verdict::Blocked,
                                "findings alone must never produce blocked"
                            );
                            if verdict == Verdict::Clear {
                                assert!(findings.is_empty(), "clear with findings");
                                assert!(skipped.is_empty(), "clear with skipped checks");
                            }
                            if !findings.is_empty() {
                                assert_eq!(
                                    verdict,
                                    Verdict::Restricted,
                                    "a positive finding stands without completeness"
                                );
                            }
                            if findings.is_empty() && !skipped.is_empty() {
                                assert_eq!(verdict, Verdict::Inconclusive);
                            }
                        }
                    }
                }
            }
        }
    }

    /// **The coverage matrix.** The extractor crates in this family
    /// answer "does it open what it claims?" with one file per extension
    /// in their alias table. This crate has no format table; what it
    /// claims is **five anti-bot vendors, four checks and four
    /// verdicts**, so that is the table.
    ///
    /// Every vendor must be reachable through every kind of signal it
    /// declares. A vendor listed in the corpus that no evidence can
    /// surface is the same failure as a format in the table with no
    /// corpus document: it inflates what the tool says it covers.
    #[test]
    fn coverage_matrix_every_vendor_is_reachable_through_every_signal_it_declares() {
        let mut reached = 0;
        for signature in signatures() {
            // Headers, where the vendor declares any.
            for header in &signature.headers {
                let value = header
                    .contains
                    .clone()
                    .unwrap_or_else(|| "probe-value".to_string());
                let report = check(
                    &evidence(200, &[(&header.name, &value)], None),
                    &CheckOptions::default(),
                );
                let finding = report
                    .findings
                    .iter()
                    .find(|f| f.kind == "antibot" && f.detail.starts_with(&signature.label))
                    .unwrap_or_else(|| {
                        panic!(
                            "{}: the {} header names no vendor",
                            signature.key, header.name
                        )
                    });
                assert_eq!(finding.evidence["source"], "response-header");
                reached += 1;
            }

            // The page probe, once per source the vendor declares.
            for (source, declared) in [
                ("script src", !signature.script_substrings.is_empty()),
                ("DOM element", !signature.selectors.is_empty()),
                ("window global", !signature.globals.is_empty()),
            ] {
                if !declared {
                    continue;
                }
                let mut e = evidence(200, &[], None);
                let mut render = clean_render();
                render.probes.insert(
                    signature.key.clone(),
                    ProbeResult {
                        script: source == "script src",
                        selector: source == "DOM element",
                        global: source == "window global",
                    },
                );
                e.render = Some(render);
                let report = check(&e, &CheckOptions::default());
                assert!(
                    report
                        .findings
                        .iter()
                        .any(|f| f.detail == format!("{} ({source})", signature.label)),
                    "{}: a {source} signal names no vendor",
                    signature.key
                );
                reached += 1;
            }
        }
        // Not vacuous: an empty corpus would satisfy every loop above.
        assert!(
            reached >= 5,
            "only {reached} vendor signals were reachable, which is too few to be a matrix"
        );
        eprintln!("coverage-matrix: {reached} vendor signals reachable through check()");
    }

    /// Every check the report names, and every verdict it can carry,
    /// reachable from evidence. A verdict nothing can produce is a
    /// documented state that never happens.
    #[test]
    fn coverage_matrix_every_check_and_every_verdict_is_reachable() {
        let mut rendered = evidence(200, &[], None);
        rendered.render = Some(clean_render());
        assert_eq!(
            check(&rendered, &CheckOptions::default()).verdict,
            Verdict::Clear
        );
        assert_eq!(
            check(&evidence(200, &[], None), &CheckOptions::default()).verdict,
            Verdict::Inconclusive
        );
        assert_eq!(
            check(&evidence(429, &[], None), &CheckOptions::default()).verdict,
            Verdict::Restricted
        );
        // `blocked` belongs to a page that could not be reached at all,
        // so it is built by the surfaces rather than by `check`.
        assert_eq!(
            crate::cli::blocked_report("https://example.com", "dns failure", None).verdict,
            Verdict::Blocked
        );

        let kinds = [
            (
                "antibot",
                check(
                    &evidence(200, &[("cf-ray", "x")], None),
                    &CheckOptions::default(),
                ),
            ),
            (
                "rate_limit",
                check(&evidence(429, &[], None), &CheckOptions::default()),
            ),
            (
                "robots",
                check(
                    &evidence(200, &[], Some("User-agent: *\nDisallow: /search")),
                    &CheckOptions::default(),
                ),
            ),
            (
                "auth",
                check(&evidence(401, &[], None), &CheckOptions::default()),
            ),
        ];
        for (kind, report) in kinds {
            assert!(
                report.findings.iter().any(|f| f.kind == kind),
                "the {kind} check produces no finding anything can trigger"
            );
        }
        eprintln!("coverage-matrix: 4 checks and 4 verdicts reachable");
    }

    #[test]
    fn no_single_warns_finding_produces_blocked() {
        let report = check(
            &evidence(200, &[("cf-ray", "x")], None),
            &CheckOptions::default(),
        );
        assert_ne!(report.verdict, Verdict::Blocked);
    }
}