tokmd-analysis 1.14.0

Analysis logic and enrichers for tokmd receipts.
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
//! Deep round-2 tests for `analysis derived module` (w51).
//!
//! Focuses on COCOMO estimation across project sizes, extreme density ratios,
//! distribution metrics with uniform vs skewed distributions, Gini coefficient
//! verification, and polyglot behavior with 1 vs 100 languages.

use crate::derived::derive_report;
use tokmd_types::{ChildIncludeMode, ExportData, FileKind, FileRow};

// ── Helpers ─────────────────────────────────────────────────────

#[expect(
    clippy::too_many_arguments,
    reason = "policy:clippy-0016 derived test fixture builds FileRow in one call"
)]
fn row(
    path: &str,
    module: &str,
    lang: &str,
    code: usize,
    comments: usize,
    blanks: usize,
    bytes: usize,
    tokens: usize,
) -> FileRow {
    FileRow {
        path: path.to_string(),
        module: module.to_string(),
        lang: lang.to_string(),
        kind: FileKind::Parent,
        code,
        comments,
        blanks,
        lines: code + comments + blanks,
        bytes,
        tokens,
    }
}

fn simple_row(path: &str, lang: &str, code: usize) -> FileRow {
    row(path, "src", lang, code, 0, 0, code * 40, code * 8)
}

fn export(rows: Vec<FileRow>) -> ExportData {
    ExportData {
        rows,
        module_roots: vec![],
        module_depth: 1,
        children: ChildIncludeMode::ParentsOnly,
    }
}

// ═══════════════════════════════════════════════════════════════════
// § COCOMO with various project sizes
// ═══════════════════════════════════════════════════════════════════

mod cocomo_sizes {
    use super::*;

    #[test]
    fn cocomo_small_project_100_lines() {
        let r = derive_report(&export(vec![simple_row("a.rs", "Rust", 100)]), None);
        let c = r.cocomo.as_ref().expect("cocomo present for code > 0");
        assert!((c.kloc - 0.1).abs() < 0.001);
        // effort = 2.4 * 0.1^1.05 ≈ 0.214
        assert!(c.effort_pm > 0.0 && c.effort_pm < 1.0);
        assert!(c.duration_months > 0.0);
        assert!(c.staff > 0.0);
    }

    #[test]
    fn cocomo_medium_project_5000_lines() {
        let r = derive_report(&export(vec![simple_row("a.rs", "Rust", 5_000)]), None);
        let c = r.cocomo.as_ref().unwrap();
        assert!((c.kloc - 5.0).abs() < 0.001);
        let expected = 2.4 * 5.0_f64.powf(1.05);
        assert!(
            (c.effort_pm - expected).abs() < 0.5,
            "5 KLOC effort ≈ {expected:.2}, got {}",
            c.effort_pm
        );
    }

    #[test]
    fn cocomo_large_project_50000_lines() {
        let r = derive_report(&export(vec![simple_row("a.rs", "Rust", 50_000)]), None);
        let c = r.cocomo.as_ref().unwrap();
        assert!((c.kloc - 50.0).abs() < 0.01);
        let expected = 2.4 * 50.0_f64.powf(1.05);
        assert!(
            (c.effort_pm - expected).abs() < 1.0,
            "50 KLOC effort ≈ {expected:.2}, got {}",
            c.effort_pm
        );
    }

    #[test]
    fn cocomo_huge_project_500000_lines() {
        let r = derive_report(&export(vec![simple_row("a.rs", "Rust", 500_000)]), None);
        let c = r.cocomo.as_ref().unwrap();
        assert!((c.kloc - 500.0).abs() < 0.1);
        let expected = 2.4 * 500.0_f64.powf(1.05);
        assert!(
            (c.effort_pm - expected).abs() < 5.0,
            "500 KLOC effort ≈ {expected:.2}, got {}",
            c.effort_pm
        );
        // Huge projects require many person-months
        assert!(c.effort_pm > 100.0);
        assert!(c.duration_months > 10.0);
    }

    #[test]
    fn cocomo_effort_increases_superlinearly() {
        let r1 = derive_report(&export(vec![simple_row("a.rs", "Rust", 1_000)]), None);
        let r10 = derive_report(&export(vec![simple_row("a.rs", "Rust", 10_000)]), None);
        let r100 = derive_report(&export(vec![simple_row("a.rs", "Rust", 100_000)]), None);

        let e1 = r1.cocomo.as_ref().unwrap().effort_pm;
        let e10 = r10.cocomo.as_ref().unwrap().effort_pm;
        let e100 = r100.cocomo.as_ref().unwrap().effort_pm;

        // 10x code should yield > 10x effort (superlinear, exponent 1.05)
        assert!(e10 / e1 > 10.0, "10x code → >10x effort");
        assert!(e100 / e10 > 10.0, "10x code → >10x effort");
    }

    #[test]
    fn cocomo_duration_constants_match() {
        let r = derive_report(&export(vec![simple_row("a.rs", "Rust", 10_000)]), None);
        let c = r.cocomo.as_ref().unwrap();
        assert_eq!(c.a, 2.4);
        assert_eq!(c.b, 1.05);
        assert_eq!(c.c, 2.5);
        assert_eq!(c.d, 0.38);
        assert_eq!(c.mode, "organic");
    }
}

// ═══════════════════════════════════════════════════════════════════
// § Extreme density ratios
// ═══════════════════════════════════════════════════════════════════

mod density_extremes {
    use super::*;

    #[test]
    fn density_99_percent_comments() {
        // 1 code line, 99 comment lines → density ≈ 0.99
        let r = derive_report(
            &export(vec![row("a.rs", "src", "Rust", 1, 99, 0, 4000, 80)]),
            None,
        );
        assert!(
            r.doc_density.total.ratio > 0.98,
            "99% comments → density > 0.98, got {}",
            r.doc_density.total.ratio
        );
        assert!(r.doc_density.total.ratio <= 1.0);
    }

    #[test]
    fn density_zero_percent_comments() {
        // 100 code lines, 0 comment lines → density = 0
        let r = derive_report(
            &export(vec![row("a.rs", "src", "Rust", 100, 0, 0, 4000, 800)]),
            None,
        );
        assert_eq!(r.doc_density.total.ratio, 0.0);
    }

    #[test]
    fn density_all_comments_no_code() {
        // 0 code lines, 100 comment lines → density = 1.0
        let r = derive_report(
            &export(vec![row("a.rs", "src", "Rust", 0, 100, 0, 4000, 0)]),
            None,
        );
        assert_eq!(r.doc_density.total.ratio, 1.0);
    }

    #[test]
    fn density_by_lang_sorted_descending() {
        let r = derive_report(
            &export(vec![
                row("a.rs", "src", "Rust", 90, 10, 0, 4000, 800),
                row("b.py", "src", "Python", 50, 50, 0, 4000, 800),
                row("c.js", "src", "JavaScript", 20, 80, 0, 4000, 800),
            ]),
            None,
        );
        // by_lang rows should be sorted descending by ratio
        for w in r.doc_density.by_lang.windows(2) {
            assert!(
                w[0].ratio >= w[1].ratio,
                "by_lang not sorted descending: {} < {}",
                w[0].ratio,
                w[1].ratio
            );
        }
    }
}

// ═══════════════════════════════════════════════════════════════════
// § Distribution with uniform vs skewed
// ═══════════════════════════════════════════════════════════════════

mod distribution_shape {
    use super::*;

    #[test]
    fn uniform_distribution_stats() {
        let rows: Vec<FileRow> = (0..10)
            .map(|i| simple_row(&format!("f{i}.rs"), "Rust", 100))
            .collect();
        let r = derive_report(&export(rows), None);
        let d = &r.distribution;
        assert_eq!(d.count, 10);
        assert_eq!(d.min, 100);
        assert_eq!(d.max, 100);
        assert!((d.mean - 100.0).abs() < 0.01);
        assert!((d.median - 100.0).abs() < 0.01);
        assert!(d.gini < 0.01, "uniform → gini ≈ 0, got {}", d.gini);
    }

    #[test]
    fn heavily_skewed_distribution() {
        let mut rows = vec![simple_row("giant.rs", "Rust", 100_000)];
        for i in 0..99 {
            rows.push(simple_row(&format!("tiny{i}.rs"), "Rust", 1));
        }
        let r = derive_report(&export(rows), None);
        let d = &r.distribution;
        assert_eq!(d.count, 100);
        assert_eq!(d.min, 1);
        assert_eq!(d.max, 100_000);
        assert!(d.gini > 0.8, "heavily skewed → gini > 0.8, got {}", d.gini);
        // p90 should be low since 90% of files are tiny
        assert!(d.p90 < 100.0);
        // p99 should be the giant file
        assert!(d.p99 >= 100_000.0);
    }

    #[test]
    fn gini_zero_for_perfectly_equal() {
        let rows: Vec<FileRow> = (0..20)
            .map(|i| simple_row(&format!("f{i}.rs"), "Rust", 50))
            .collect();
        let r = derive_report(&export(rows), None);
        assert_eq!(r.distribution.gini, 0.0);
    }

    #[test]
    fn gini_approaches_1_for_maximum_inequality() {
        // One file with all lines, rest with 1 line each
        let mut rows = vec![simple_row("big.rs", "Rust", 1_000_000)];
        for i in 0..999 {
            rows.push(simple_row(&format!("s{i}.rs"), "Rust", 1));
        }
        let r = derive_report(&export(rows), None);
        assert!(
            r.distribution.gini > 0.95,
            "extreme inequality → gini > 0.95, got {}",
            r.distribution.gini
        );
    }

    #[test]
    fn mean_between_min_and_max() {
        let rows = vec![
            simple_row("a.rs", "Rust", 10),
            simple_row("b.rs", "Rust", 100),
            simple_row("c.rs", "Rust", 1000),
        ];
        let r = derive_report(&export(rows), None);
        let d = &r.distribution;
        assert!(d.mean >= d.min as f64);
        assert!(d.mean <= d.max as f64);
    }
}

// ═══════════════════════════════════════════════════════════════════
// § Polyglot: 1 language vs 100 languages
// ═══════════════════════════════════════════════════════════════════

mod polyglot_extremes {
    use super::*;

    #[test]
    fn single_language_entropy_zero() {
        let rows: Vec<FileRow> = (0..50)
            .map(|i| simple_row(&format!("f{i}.rs"), "Rust", 100))
            .collect();
        let r = derive_report(&export(rows), None);
        assert_eq!(r.polyglot.lang_count, 1);
        assert_eq!(r.polyglot.entropy, 0.0);
        assert_eq!(r.polyglot.dominant_lang, "Rust");
        assert!((r.polyglot.dominant_pct - 1.0).abs() < 0.001);
    }

    #[test]
    fn many_languages_high_entropy() {
        let langs = [
            "Rust",
            "Python",
            "JavaScript",
            "TypeScript",
            "Go",
            "Java",
            "C",
            "C++",
            "Ruby",
            "PHP",
            "Swift",
            "Kotlin",
            "Scala",
            "Haskell",
            "OCaml",
            "Lua",
            "Perl",
            "R",
            "Julia",
            "Dart",
        ];
        let rows: Vec<FileRow> = langs
            .iter()
            .enumerate()
            .map(|(i, &lang)| simple_row(&format!("f{i}.x"), lang, 100))
            .collect();
        let r = derive_report(&export(rows), None);
        assert_eq!(r.polyglot.lang_count, 20);
        // Shannon entropy for 20 uniform languages ≈ log2(20) ≈ 4.32
        assert!(
            r.polyglot.entropy > 4.0,
            "20 uniform langs → entropy > 4.0, got {}",
            r.polyglot.entropy
        );
    }

    #[test]
    fn dominant_language_identified_correctly() {
        let r = derive_report(
            &export(vec![
                simple_row("a.rs", "Rust", 900),
                simple_row("b.py", "Python", 50),
                simple_row("c.js", "JavaScript", 50),
            ]),
            None,
        );
        assert_eq!(r.polyglot.dominant_lang, "Rust");
        assert!(r.polyglot.dominant_pct > 0.85);
        assert_eq!(r.polyglot.dominant_lines, 900);
    }

    #[test]
    fn two_equal_languages_entropy_one() {
        let r = derive_report(
            &export(vec![
                simple_row("a.rs", "Rust", 100),
                simple_row("b.py", "Python", 100),
            ]),
            None,
        );
        assert_eq!(r.polyglot.lang_count, 2);
        // Shannon entropy for 2 uniform = log2(2) = 1.0
        assert!(
            (r.polyglot.entropy - 1.0).abs() < 0.01,
            "2 equal langs → entropy ≈ 1.0, got {}",
            r.polyglot.entropy
        );
    }
}

// ═══════════════════════════════════════════════════════════════════
// § Histogram bucket assignment
// ═══════════════════════════════════════════════════════════════════

mod histogram_buckets {
    use super::*;

    #[test]
    fn histogram_sums_to_file_count() {
        let rows: Vec<FileRow> = (0..25)
            .map(|i| simple_row(&format!("f{i}.rs"), "Rust", (i + 1) * 50))
            .collect();
        let r = derive_report(&export(rows), None);
        let total: usize = r.histogram.iter().map(|b| b.files).sum();
        assert_eq!(total, 25);
    }

    #[test]
    fn histogram_pct_sums_to_approximately_one() {
        let rows: Vec<FileRow> = (0..10)
            .map(|i| simple_row(&format!("f{i}.rs"), "Rust", (i + 1) * 100))
            .collect();
        let r = derive_report(&export(rows), None);
        let pct_sum: f64 = r.histogram.iter().map(|b| b.pct).sum();
        assert!(
            (pct_sum - 1.0).abs() < 0.01,
            "histogram pct sum ≈ 1.0, got {pct_sum}"
        );
    }
}