repo-trust 0.1.1

A command-line tool that tells you whether an open-source repository deserves your trust — beyond the star count.
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
//! Adoption Signals scorer — pure function from features → `(ModuleResult, evidence)`.
//!
//! Per `docs/methodology.md` §Module 4 and `specs/adoption-signals-module.md`,
//! the scorer aggregates four sub-scores: `weekly_downloads` (logarithmic
//! banding), `documentation_maturity`, `package_systems_count`, and
//! `awesome_list_mentions`. The final score is the arithmetic mean of the
//! present sub-scores.
//!
//! Conservative posture: a repo that publishes no package is a Neutral
//! caveat, NEVER a Concerning verdict ("we don't penalize this — we simply
//! lower the module's confidence to Medium" — methodology §Module 4).

use std::collections::BTreeMap;

use serde_json::json;

use super::thresholds::AdoptionThresholds;
use crate::features::adoption::AdoptionFeatures;
use crate::models::{Confidence, EvidenceItem, ModuleResult, Verdict};

const MODULE_NAME: &str = "adoption";

/// Score the adoption module.
#[must_use]
pub fn score(
    features: &AdoptionFeatures,
    thresholds: &AdoptionThresholds,
) -> (ModuleResult, Vec<EvidenceItem>) {
    let mut sub_scores: BTreeMap<String, u8> = BTreeMap::new();
    let mut evidence: Vec<EvidenceItem> = Vec::new();
    let mut missing: Vec<String> = Vec::new();

    // ─── 1. Weekly downloads (logarithmic banding) ─────────────────────────
    // deps.dev v3 dropped this field in mid-2026 (see scoring-model 1.1.0
    // change-log). The block is kept for the day downloads come back from
    // some other source — when `weekly_downloads` is `Some`, the sub-score
    // and evidence row are emitted additively. The scorer no longer uses
    // download absence as a "no_packages" proxy; that gate is now
    // anchored on `package_systems_count` directly (§3 below).
    if let Some(dl) = features.weekly_downloads {
        let s = downloads_to_score(dl, thresholds);
        sub_scores.insert("weekly_downloads".into(), s);
        evidence.push(EvidenceItem {
            module: MODULE_NAME.into(),
            code: "weekly_downloads".into(),
            label: "Aggregated weekly downloads across published packages".into(),
            value: json!(dl),
            threshold: Some(json!({
                "band_25": thresholds.downloads_band_25,
                "band_50": thresholds.downloads_band_50,
                "band_75": thresholds.downloads_band_75,
                "band_100": thresholds.downloads_band_100,
            })),
            verdict: verdict_from_score(s),
            rationale: format!(
                "{dl} weekly downloads. Logarithmic banding: 0→0, {}→25, {}→50, {}→75, {}+→100.",
                thresholds.downloads_band_25,
                thresholds.downloads_band_50,
                thresholds.downloads_band_75,
                thresholds.downloads_band_100,
            ),
        });
    }

    // ─── 2. Documentation maturity ─────────────────────────────────────────
    let doc_score = doc_maturity_to_score(features.documentation_maturity_score);
    sub_scores.insert("documentation_maturity".into(), doc_score);
    evidence.push(EvidenceItem {
        module: MODULE_NAME.into(),
        code: "documentation_maturity".into(),
        label: "Documentation maturity (README + docs/ + examples/)".into(),
        value: json!({
            "score": crate::utils::time::round6(features.documentation_maturity_score),
            "has_readme": features.has_readme,
            "readme_word_count": features.readme_word_count,
            "has_docs_dir": features.has_docs_dir,
            "has_examples_dir": features.has_examples_dir,
        }),
        threshold: Some(json!({
            "readme_words_full_credit": thresholds.readme_words_full_credit,
            "readme_words_half_credit": thresholds.readme_words_half_credit,
        })),
        verdict: verdict_from_score(doc_score),
        rationale: format!(
            "Doc-maturity {:.2}/1.0. README {}; docs/ {}; examples/ {}.",
            features.documentation_maturity_score,
            if features.has_readme {
                "present"
            } else {
                "absent"
            },
            if features.has_docs_dir {
                "present"
            } else {
                "absent"
            },
            if features.has_examples_dir {
                "present"
            } else {
                "absent"
            },
        ),
    });

    // ─── 2a. Missing README (Concerning per scenario S-201) ────────────────
    if !features.has_readme {
        evidence.push(EvidenceItem {
            module: MODULE_NAME.into(),
            code: "no_readme".into(),
            label: "Repository has no detected README".into(),
            value: json!(false),
            threshold: None,
            verdict: Verdict::Concerning,
            rationale: "GET /readme returned 404 — the default branch has no README at the conventional paths.".into(),
        });
    }

    // ─── 3. Package systems count ──────────────────────────────────────────
    // Sub-score is always present (drives the score arithmetic). The
    // evidence row is split: `ecosystem_coverage` when ≥1 system is
    // detected (positive for ≥2, neutral for 1); `no_packages` Neutral
    // caveat when count == 0.
    let systems_score = systems_count_to_score(features.package_systems_count);
    sub_scores.insert("package_systems_count".into(), systems_score);
    if features.package_systems_count == 0 {
        missing.push("no_packages".into());
        evidence.push(EvidenceItem {
            module: MODULE_NAME.into(),
            code: "no_packages".into(),
            label: "Repository publishes no package".into(),
            value: json!(0),
            threshold: None,
            verdict: Verdict::Neutral,
            rationale: "deps.dev returned no published packages mapped to this repository. Many healthy repos legitimately publish no package (research, dotfiles, manifests, example collections); the documentation-maturity signal carries the adoption score in that case.".into(),
        });
    } else {
        evidence.push(EvidenceItem {
            module: MODULE_NAME.into(),
            code: "ecosystem_coverage".into(),
            label: "Package ecosystem coverage".into(),
            value: json!(features.package_systems),
            threshold: None,
            verdict: if features.package_systems_count >= 2 {
                Verdict::Positive
            } else {
                Verdict::Neutral
            },
            rationale: format!(
                "{} distinct package system(s) detected: {}. Cross-ecosystem packaging is a strong adoption signal.",
                features.package_systems_count,
                features.package_systems.join(", "),
            ),
        });
    }

    // ─── 4. Awesome-list mentions (neutral baseline) ───────────────────────
    let awesome_score = awesome_to_score(features.awesome_list_mentions);
    sub_scores.insert("awesome_list_mentions".into(), awesome_score);
    evidence.push(EvidenceItem {
        module: MODULE_NAME.into(),
        code: "awesome_list_mentions".into(),
        label: "Awesome-list mentions".into(),
        value: json!(features.awesome_list_mentions),
        threshold: None,
        verdict: verdict_from_score(awesome_score),
        rationale: format!(
            "{} awesome-list mention(s). 0 is the neutral baseline (most repos are not on awesome lists); ≥1 is a Positive boost.",
            features.awesome_list_mentions,
        ),
    });

    // ─── deps.dev outage caveat ────────────────────────────────────────────
    if features.deps_dev_error {
        missing.push("deps_dev_unavailable".into());
        evidence.push(EvidenceItem {
            module: MODULE_NAME.into(),
            code: "deps_dev_unavailable".into(),
            label: "deps.dev API unavailable".into(),
            value: json!(true),
            threshold: None,
            verdict: Verdict::Neutral,
            rationale: "deps.dev returned an error during this scan. Downloads + ecosystem-count signals are skipped; this scan reflects documentation signals only.".into(),
        });
    }

    // ─── Archived ──────────────────────────────────────────────────────────
    if features.archived {
        missing.push("archived".into());
        evidence.push(EvidenceItem {
            module: MODULE_NAME.into(),
            code: "archived".into(),
            label: "Repository is archived".into(),
            value: json!(true),
            threshold: None,
            verdict: Verdict::Neutral,
            rationale: "Owner has archived this repository; adoption signals are frozen.".into(),
        });
    }

    // ─── Final score = arithmetic mean of present sub-scores ──────────────
    let final_score = if sub_scores.is_empty() {
        0
    } else {
        let sum: u32 = sub_scores.values().map(|s| u32::from(*s)).sum();
        let n = u32::try_from(sub_scores.len()).unwrap_or(1).max(1);
        u8::try_from((sum + n / 2) / n).unwrap_or(0)
    };

    let confidence = compute_confidence(features, thresholds);

    (
        ModuleResult {
            module: MODULE_NAME.into(),
            score: final_score,
            confidence,
            sub_scores,
            sample_size: None,
            missing_data: missing,
        },
        evidence,
    )
}

/// Logarithmic banding per `specs/adoption-signals-module.md` §9 v1.
///
/// Pure step function rather than smooth interpolation — adoption is a
/// power-law signal and a few crisp bands beat a noisy linear curve.
#[must_use]
pub fn downloads_to_score(downloads: u64, t: &AdoptionThresholds) -> u8 {
    if downloads >= t.downloads_band_100 {
        100
    } else if downloads >= t.downloads_band_75 {
        75
    } else if downloads >= t.downloads_band_50 {
        50
    } else if downloads >= t.downloads_band_25 {
        25
    } else {
        0
    }
}

/// Map a `[0.0, 1.0]` documentation-maturity score to a 0-100 sub-score.
#[must_use]
pub fn doc_maturity_to_score(maturity: f64) -> u8 {
    let s = (maturity.clamp(0.0, 1.0) * 100.0).round();
    u8::try_from(s as i64).unwrap_or(0)
}

/// Cross-ecosystem packaging is a strong adoption signal. 0 → 0 (no
/// published package), 1 → 75 (the common single-ecosystem case), 2+ → 100.
#[must_use]
pub fn systems_count_to_score(count: u64) -> u8 {
    match count {
        0 => 0,
        1 => 75,
        _ => 100,
    }
}

/// Awesome-list mentions. 0 is the *neutral baseline* — most repos are not
/// on awesome lists, so absence is not a negative.
#[must_use]
pub fn awesome_to_score(count: u64) -> u8 {
    match count {
        0 => 50,
        1 => 75,
        _ => 100,
    }
}

fn verdict_from_score(s: u8) -> Verdict {
    match s {
        80..=100 => Verdict::Positive,
        50..=79 => Verdict::Neutral,
        20..=49 => Verdict::Concerning,
        _ => Verdict::HighRisk,
    }
}

/// Documentation-maturity threshold above which a repository is "well
/// documented" for the purposes of Adoption confidence (scoring 1.1.0).
///
/// 0.50 corresponds to a substantial README (≥500 words) and nothing
/// else, OR README ≥100 words plus a `docs/` directory. Either signal
/// shows the maintainer cared enough to write user-facing docs.
const MEDIUM_DOC_THRESHOLD: f64 = 0.50;

/// Widened "documented" predicate for the confidence rule (scoring
/// 1.1.1). A repository qualifies if EITHER:
///
/// 1. `documentation_maturity_score >= MEDIUM_DOC_THRESHOLD` — the
///    original 1.1.0 floor (covers heavy-doc repos with long READMEs
///    and `docs/` trees); OR
/// 2. The repository has a README **and** at least one of `docs/` or
///    `examples/` — the idiomatic library-project layout where the
///    root README intentionally points at deeper material rather
///    than reproducing it inline (clap-rs/clap, serde-rs/serde,
///    tower-rs/tower follow this pattern).
///
/// (1) covers heavy-doc projects; (2) covers library projects whose
/// raw doc-maturity score under-rates because the doc weight is
/// concentrated in `examples/` (0.20) or a short root README (0.20).
/// The doc-maturity *score* itself is unchanged — only the boolean
/// used by the confidence rule widens.
fn is_well_documented(features: &AdoptionFeatures) -> bool {
    if features.documentation_maturity_score >= MEDIUM_DOC_THRESHOLD {
        return true;
    }
    features.has_readme && (features.has_docs_dir || features.has_examples_dir)
}

/// Adoption Signals confidence (scoring v1.1.0+).
///
/// deps.dev v3 no longer exposes `weeklyDownloads` (verified empty
/// across CARGO / NPM / GO / PYPI / MAVEN as of 2026-05-04, see
/// `docs/scoring-model.md` 1.1.0 entry). The previous v1.0.0 rule
/// gated `Confidence::High` on a downloads floor and is no longer
/// satisfiable. The new rule grades confidence by the breadth of
/// ecosystem evidence we actually receive:
///
/// - `High`: package(s) present in ≥1 ecosystem AND repository is
///   not archived AND documentation maturity ≥ [`MEDIUM_DOC_THRESHOLD`].
///   We have two independent signals that the project is published,
///   maintained, and in active use.
/// - `Medium`: packages present but repository archived; OR packages
///   present but under-documented; OR no packages but docs are mature
///   (suggests adoption in non-package form, e.g. a manifest or
///   examples repository).
/// - `Low`: no packages in any ecosystem AND no documentation depth.
///
/// `deps_dev_error == true` (the source itself was unavailable for
/// this scan) short-circuits to `Low` regardless of the other
/// signals — we genuinely don't know what we're missing.
fn compute_confidence(features: &AdoptionFeatures, _t: &AdoptionThresholds) -> Confidence {
    if features.deps_dev_error {
        return Confidence::Low;
    }
    let has_packages = features.package_systems_count > 0;
    let well_documented = is_well_documented(features);
    let archived = features.archived;

    match (has_packages, archived, well_documented) {
        (true, false, true) => Confidence::High,
        (true, false, false) | (true, true, _) | (false, _, true) => Confidence::Medium,
        (false, _, false) => Confidence::Low,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::features::adoption::AdoptionFeatures;

    fn baseline() -> AdoptionFeatures {
        AdoptionFeatures::default()
    }

    fn popular() -> AdoptionFeatures {
        AdoptionFeatures {
            weekly_downloads: Some(500_000),
            package_systems: vec!["GO".into()],
            package_systems_count: 1,
            has_readme: true,
            readme_word_count: Some(1200),
            has_docs_dir: true,
            has_examples_dir: true,
            documentation_maturity_score: 1.0,
            awesome_list_mentions: 0,
            archived: false,
            deps_dev_error: false,
        }
    }

    // S-001: popular package + complete docs scores high.
    #[test]
    fn s001_popular_package_scores_high() {
        let f = popular();
        let (r, ev) = score(&f, &AdoptionThresholds::v1());
        assert!(r.score >= 75, "expected ≥75, got {}", r.score);
        assert_eq!(r.confidence, Confidence::High);
        let dl = ev
            .iter()
            .find(|e| e.code == "weekly_downloads")
            .expect("weekly_downloads evidence");
        assert!(matches!(dl.verdict, Verdict::Neutral | Verdict::Positive));
        let dm = ev
            .iter()
            .find(|e| e.code == "documentation_maturity")
            .expect("documentation_maturity evidence");
        assert!(matches!(dm.verdict, Verdict::Positive));
    }

    // S-002: minimal but well-documented package lands mid.
    #[test]
    fn s002_minimal_but_documented_lands_mid() {
        let mut f = baseline();
        f.weekly_downloads = Some(1_500);
        f.package_systems = vec!["NPM".into()];
        f.package_systems_count = 1;
        f.has_readme = true;
        f.readme_word_count = Some(600);
        f.has_docs_dir = true;
        f.documentation_maturity_score = 0.80; // README + docs, no examples.
        let (r, _) = score(&f, &AdoptionThresholds::v1());
        assert!(
            (40..=70).contains(&r.score),
            "expected 40..=70, got {}",
            r.score
        );
    }

    // S-101: no published package + mature docs → Medium confidence
    // (we still have the documentation signal even without packages).
    // Per scoring 1.1.0 confidence rule: (false, _, true) → Medium.
    #[test]
    fn s101_no_packages_with_docs_falls_back_to_medium() {
        let mut f = baseline();
        f.has_readme = true;
        f.readme_word_count = Some(800);
        f.has_docs_dir = true;
        // README ≥500 words = 0.50; +docs/ = 0.80; well above the
        // MEDIUM_DOC_THRESHOLD of 0.50.
        f.documentation_maturity_score = 0.80;
        let (r, ev) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.confidence, Confidence::Medium);
        assert!(r.missing_data.iter().any(|m| m == "no_packages"));
        let np = ev
            .iter()
            .find(|e| e.code == "no_packages")
            .expect("no_packages evidence");
        assert!(matches!(np.verdict, Verdict::Neutral));
        // Module score is computed from the present sub-scores only —
        // weekly_downloads is omitted (deps.dev v3 no longer exposes it).
        assert!(!r.sub_scores.contains_key("weekly_downloads"));
    }

    // S-102: deps.dev outage drops to Low confidence.
    #[test]
    fn s102_deps_dev_error_low_confidence_with_caveat() {
        let mut f = baseline();
        f.has_readme = true;
        f.readme_word_count = Some(300);
        f.documentation_maturity_score = 0.35;
        f.deps_dev_error = true;
        let (r, ev) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.confidence, Confidence::Low);
        assert!(r.missing_data.iter().any(|m| m == "deps_dev_unavailable"));
        let cav = ev
            .iter()
            .find(|e| e.code == "deps_dev_unavailable")
            .expect("deps_dev_unavailable evidence");
        assert!(matches!(cav.verdict, Verdict::Neutral));
    }

    // S-103: archived repo demotes to Medium + emits the archived caveat.
    // Per scoring 1.1.0: archived-but-published is Medium (rather than
    // the previous Low) because the package is still distributed and
    // resolvable; the archive flag means "no new versions", not "broken".
    #[test]
    fn s103_archived_demotes_to_medium_confidence() {
        let mut f = popular();
        f.archived = true;
        let (r, ev) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.confidence, Confidence::Medium);
        assert!(r.missing_data.iter().any(|m| m == "archived"));
        assert!(ev.iter().any(|e| e.code == "archived"));
    }

    // S-201: missing README → Concerning evidence.
    #[test]
    fn s201_missing_readme_emits_concerning() {
        let mut f = baseline();
        f.weekly_downloads = Some(50_000);
        f.package_systems = vec!["GO".into()];
        f.package_systems_count = 1;
        // README absent, docs absent, examples absent → maturity 0.
        let (_, ev) = score(&f, &AdoptionThresholds::v1());
        let nr = ev
            .iter()
            .find(|e| e.code == "no_readme")
            .expect("no_readme evidence");
        assert!(matches!(nr.verdict, Verdict::Concerning));
    }

    #[test]
    fn download_band_boundaries_are_correct() {
        let t = AdoptionThresholds::v1();
        assert_eq!(downloads_to_score(0, &t), 0);
        assert_eq!(downloads_to_score(999, &t), 0);
        assert_eq!(downloads_to_score(1_000, &t), 25);
        assert_eq!(downloads_to_score(9_999, &t), 25);
        assert_eq!(downloads_to_score(10_000, &t), 50);
        assert_eq!(downloads_to_score(99_999, &t), 50);
        assert_eq!(downloads_to_score(100_000, &t), 75);
        assert_eq!(downloads_to_score(999_999, &t), 75);
        assert_eq!(downloads_to_score(1_000_000, &t), 100);
        assert_eq!(downloads_to_score(50_000_000, &t), 100);
    }

    #[test]
    fn multi_system_packaging_boost() {
        // A single-ecosystem repo scores 75 on the systems sub-score; a
        // cross-ecosystem repo scores 100. Both score the same on every
        // other sub-score → final score ordering preserved.
        let mut single = popular();
        single.package_systems = vec!["NPM".into()];
        single.package_systems_count = 1;
        let mut multi = popular();
        multi.package_systems = vec!["NPM".into(), "PYPI".into()];
        multi.package_systems_count = 2;
        let (rs, _) = score(&single, &AdoptionThresholds::v1());
        let (rm, _) = score(&multi, &AdoptionThresholds::v1());
        assert!(
            rm.score >= rs.score,
            "multi-system ({}) should ≥ single-system ({})",
            rm.score,
            rs.score
        );
        assert_eq!(
            rm.sub_scores.get("package_systems_count").copied(),
            Some(100)
        );
        assert_eq!(
            rs.sub_scores.get("package_systems_count").copied(),
            Some(75)
        );
    }

    #[test]
    fn no_packages_drops_downloads_subscore() {
        let mut f = baseline();
        f.has_readme = true;
        f.readme_word_count = Some(50);
        f.documentation_maturity_score = 0.20;
        let (r, _) = score(&f, &AdoptionThresholds::v1());
        assert!(!r.sub_scores.contains_key("weekly_downloads"));
        // The other three sub-scores are still present.
        assert!(r.sub_scores.contains_key("documentation_maturity"));
        assert!(r.sub_scores.contains_key("package_systems_count"));
        assert!(r.sub_scores.contains_key("awesome_list_mentions"));
    }

    #[test]
    fn awesome_list_mentions_neutral_baseline() {
        // 0 mentions → 50 (neutral); 1 → 75; 2+ → 100.
        assert_eq!(awesome_to_score(0), 50);
        assert_eq!(awesome_to_score(1), 75);
        assert_eq!(awesome_to_score(2), 100);
        assert_eq!(awesome_to_score(20), 100);
    }

    #[test]
    fn evidence_codes_are_unique() {
        let f = popular();
        let (_, ev) = score(&f, &AdoptionThresholds::v1());
        let mut codes: Vec<&str> = ev.iter().map(|e| e.code.as_str()).collect();
        codes.sort_unstable();
        codes.dedup();
        assert_eq!(codes.len(), ev.len(), "codes must be unique");
    }

    #[test]
    fn module_result_carries_module_name_and_emits_at_least_three_evidence() {
        let f = popular();
        let (r, ev) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.module, "adoption");
        assert!(
            ev.len() >= 3,
            "expected ≥3 evidence items, got {}",
            ev.len()
        );
    }

    #[test]
    fn no_packages_is_neutral_never_concerning() {
        // Conservative posture — no published package is a Neutral caveat,
        // never a Concerning verdict (per spec §2 + methodology §Module 4).
        let mut f = baseline();
        f.has_readme = true;
        f.readme_word_count = Some(500);
        f.documentation_maturity_score = 0.50;
        let (_, ev) = score(&f, &AdoptionThresholds::v1());
        let np = ev
            .iter()
            .find(|e| e.code == "no_packages")
            .expect("no_packages evidence");
        assert!(matches!(np.verdict, Verdict::Neutral));
        assert!(!matches!(
            np.verdict,
            Verdict::Concerning | Verdict::HighRisk
        ));
    }

    // ─── 1.1.0 ecosystem-coverage confidence rule ──────────────────────────
    //
    // Replaces the v1.0.0 downloads-floor rule. See the doc comment on
    // `compute_confidence` for the truth table.

    #[test]
    fn confidence_high_when_packages_and_documented_and_not_archived() {
        let mut f = baseline();
        f.package_systems = vec!["CARGO".into(), "GO".into()];
        f.package_systems_count = 2;
        f.archived = false;
        f.documentation_maturity_score = 0.70;
        f.weekly_downloads = None; // v3 reality
        let (r, _) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.confidence, Confidence::High);
    }

    #[test]
    fn confidence_medium_when_packages_but_underdocumented() {
        let mut f = baseline();
        f.package_systems = vec!["NPM".into()];
        f.package_systems_count = 1;
        f.archived = false;
        f.documentation_maturity_score = 0.20;
        f.weekly_downloads = None;
        let (r, _) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.confidence, Confidence::Medium);
    }

    #[test]
    fn confidence_medium_when_archived_even_with_packages() {
        let mut f = baseline();
        f.package_systems = vec!["CARGO".into(), "NPM".into(), "GO".into()];
        f.package_systems_count = 3;
        f.archived = true;
        f.documentation_maturity_score = 0.90;
        let (r, _) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.confidence, Confidence::Medium);
    }

    #[test]
    fn confidence_medium_when_no_packages_but_documented() {
        let mut f = baseline();
        f.package_systems_count = 0;
        f.archived = false;
        f.documentation_maturity_score = 0.70;
        let (r, _) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.confidence, Confidence::Medium);
    }

    #[test]
    fn confidence_low_when_no_packages_and_underdocumented() {
        let mut f = baseline();
        f.package_systems_count = 0;
        f.archived = false;
        f.documentation_maturity_score = 0.10;
        let (r, _) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.confidence, Confidence::Low);
    }

    #[test]
    fn no_packages_evidence_only_fires_when_count_is_zero() {
        // Repos that DO publish must NOT emit no_packages — they get
        // ecosystem_coverage instead.
        let mut f = baseline();
        f.package_systems = vec!["CARGO".into(), "GO".into()];
        f.package_systems_count = 2;
        let (_r, ev) = score(&f, &AdoptionThresholds::v1());
        assert!(
            !ev.iter().any(|e| e.code == "no_packages"),
            "no_packages must not fire when package_systems_count > 0; got: {:?}",
            ev.iter().map(|e| &e.code).collect::<Vec<_>>(),
        );
        assert!(
            ev.iter().any(|e| e.code == "ecosystem_coverage"),
            "expected ecosystem_coverage evidence row; got: {:?}",
            ev.iter().map(|e| &e.code).collect::<Vec<_>>(),
        );
    }

    #[test]
    fn ecosystem_coverage_evidence_positive_when_two_or_more_systems() {
        let mut f = baseline();
        f.package_systems = vec!["CARGO".into(), "GO".into()];
        f.package_systems_count = 2;
        let (_r, ev) = score(&f, &AdoptionThresholds::v1());
        let item = ev
            .iter()
            .find(|e| e.code == "ecosystem_coverage")
            .expect("ecosystem_coverage evidence");
        assert!(matches!(item.verdict, Verdict::Positive));
    }

    #[test]
    fn ecosystem_coverage_evidence_neutral_when_single_system() {
        let mut f = baseline();
        f.package_systems = vec!["NPM".into()];
        f.package_systems_count = 1;
        let (_r, ev) = score(&f, &AdoptionThresholds::v1());
        let item = ev
            .iter()
            .find(|e| e.code == "ecosystem_coverage")
            .expect("ecosystem_coverage evidence");
        assert!(matches!(item.verdict, Verdict::Neutral));
    }

    // ─── 1.1.1 widened "documented" predicate ──────────────────────────────

    #[test]
    fn well_documented_when_doc_maturity_above_threshold() {
        let mut f = baseline();
        f.documentation_maturity_score = 0.50;
        assert!(is_well_documented(&f));
    }

    #[test]
    fn well_documented_when_readme_and_examples_dir() {
        // The clap-rs/clap pattern: short README, no docs/, but
        // examples/. doc_maturity_score 0.40 (README 0.20 + examples
        // 0.20) is below the floor; widened predicate still accepts.
        let mut f = baseline();
        f.documentation_maturity_score = 0.40;
        f.has_readme = true;
        f.has_docs_dir = false;
        f.has_examples_dir = true;
        assert!(is_well_documented(&f));
    }

    #[test]
    fn well_documented_when_readme_and_docs_dir() {
        let mut f = baseline();
        f.documentation_maturity_score = 0.45;
        f.has_readme = true;
        f.has_docs_dir = true;
        f.has_examples_dir = false;
        assert!(is_well_documented(&f));
    }

    #[test]
    fn not_well_documented_when_only_short_readme() {
        let mut f = baseline();
        f.documentation_maturity_score = 0.20;
        f.has_readme = true;
        f.has_docs_dir = false;
        f.has_examples_dir = false;
        assert!(!is_well_documented(&f));
    }

    #[test]
    fn not_well_documented_when_no_readme() {
        // docs/ + examples/ without a README is implausible in
        // practice (you can't navigate the project from the root) —
        // the widened predicate requires has_readme to anchor either
        // sub-directory branch.
        let mut f = baseline();
        f.documentation_maturity_score = 0.30;
        f.has_readme = false;
        f.has_docs_dir = true;
        f.has_examples_dir = true;
        assert!(!is_well_documented(&f));
    }

    #[test]
    fn confidence_high_for_clap_like_pattern() {
        // The actual reason this calibration tweak exists — verify
        // end-to-end that compute_confidence promotes the clap-rs/clap
        // shape (short README + examples/ but no docs/, doc-maturity
        // 0.40, one ecosystem, not archived) to High.
        let mut f = baseline();
        f.package_systems = vec!["CARGO".into()];
        f.package_systems_count = 1;
        f.archived = false;
        f.documentation_maturity_score = 0.40;
        f.has_readme = true;
        f.has_docs_dir = false;
        f.has_examples_dir = true;
        let (r, _) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.confidence, Confidence::High);
    }

    #[test]
    fn deps_dev_error_short_circuits_confidence_to_low() {
        // Even with all the right ecosystem-coverage signals, an
        // outage on the source itself drops confidence to Low — we
        // genuinely don't know what we're missing.
        let mut f = popular();
        f.deps_dev_error = true;
        let (r, _) = score(&f, &AdoptionThresholds::v1());
        assert_eq!(r.confidence, Confidence::Low);
    }
}