tokmd-analysis-types 1.10.0

Analysis receipt contracts for tokmd.
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
//! Deep tests for `analysis types util module` shared utilities.
//!
//! Covers corner cases, boundary values, invariant checks, and interaction
//! scenarios across normalize_path, path_depth, is_test_path, is_infra_lang,
//! empty_file_row, normalize_root, AnalysisLimits, and re-exported math helpers.

use std::path::{Path, PathBuf};

use crate::{
    AnalysisLimits, empty_file_row, gini_coefficient, is_infra_lang, is_test_path, normalize_path,
    normalize_root, now_ms, path_depth, percentile, round_f64, safe_ratio,
};

// ── normalize_path: root-stripping interactions ─────────────────────────────

#[test]
fn normalize_path_root_match_leaves_nested_relative() {
    let root = PathBuf::from("project");
    assert_eq!(
        normalize_path("project/src/deep/mod.rs", &root),
        "src/deep/mod.rs"
    );
}

#[test]
fn normalize_path_root_match_with_backslash_input() {
    let root = PathBuf::from("project");
    // backslash converted first, then root stripped
    assert_eq!(normalize_path(r"project\src\lib.rs", &root), "src/lib.rs");
}

#[test]
fn normalize_path_dot_slash_combined_with_root_no_double_strip() {
    // "./" is stripped, but root won't match the remaining path
    let root = PathBuf::from("other");
    assert_eq!(normalize_path("./src/main.rs", &root), "src/main.rs");
}

#[test]
fn normalize_path_multiple_dot_slash_only_strips_leading() {
    let root = PathBuf::from("r");
    let result = normalize_path("./a/./b/./c.rs", &root);
    assert!(!result.starts_with("./"));
    // Only the leading "./" is stripped; inner "./" remain
    assert!(result.contains("./b/"));
}

#[test]
fn normalize_path_single_filename_no_change() {
    let root = PathBuf::from("repo");
    assert_eq!(normalize_path("file.txt", &root), "file.txt");
}

#[test]
fn normalize_path_whitespace_preserved() {
    let root = PathBuf::from("repo");
    assert_eq!(normalize_path("src/my file.rs", &root), "src/my file.rs");
}

#[test]
fn normalize_path_double_dot_prefix_not_stripped() {
    let root = PathBuf::from("repo");
    let result = normalize_path("../sibling/lib.rs", &root);
    assert!(result.starts_with(".."));
}

// ── path_depth: boundary and semantic checks ────────────────────────────────

#[test]
fn path_depth_single_dot_is_one_segment() {
    assert_eq!(path_depth("."), 1);
}

#[test]
fn path_depth_dot_dot_segments() {
    // ".." is a non-empty segment
    assert_eq!(path_depth("../src/lib.rs"), 3);
}

#[test]
fn path_depth_matches_forward_slash_count_plus_one() {
    // For a clean path without empty segments, depth == slash_count + 1
    let path = "a/b/c/d/e";
    let slash_count = path.chars().filter(|&c| c == '/').count();
    assert_eq!(path_depth(path), slash_count + 1);
}

#[test]
fn path_depth_backslash_not_treated_as_separator() {
    // path_depth only splits on '/', so backslashes are literal characters
    assert_eq!(path_depth(r"a\b\c"), 1);
}

#[test]
fn path_depth_unicode_segments() {
    assert_eq!(path_depth("日本/中国/한국"), 3);
}

// ── is_test_path: subtle detection patterns ─────────────────────────────────

#[test]
fn is_test_path_test_at_end_of_path_as_dir() {
    // "/test/" must be a directory component, not at end without trailing /
    assert!(is_test_path("project/test/unit.rs"));
}

#[test]
fn is_test_path_rejects_contest_in_dirname() {
    // "contest" contains "test" substring but not as "/test/" directory
    assert!(!is_test_path("src/contest/solution.py"));
}

#[test]
fn is_test_path_rejects_latest_in_dirname() {
    // "latest" contains "test" substring
    assert!(!is_test_path("dist/latest/bundle.js"));
}

#[test]
fn is_test_path_rejects_attest_in_dirname() {
    assert!(!is_test_path("src/attest/verify.rs"));
}

#[test]
fn is_test_path_detects_test_suffix_various_extensions() {
    assert!(is_test_path("src/parser_test.py"));
    assert!(is_test_path("src/handler_test.go"));
    assert!(is_test_path("src/utils_test.ts"));
}

#[test]
fn is_test_path_detects_test_prefix_various_extensions() {
    assert!(is_test_path("test_main.py"));
    assert!(is_test_path("test_handler.go"));
}

#[test]
fn is_test_path_detects_dot_test_pattern_in_nested_path() {
    assert!(is_test_path("packages/ui/src/Button.test.tsx"));
}

#[test]
fn is_test_path_uppercase_spec_dir() {
    assert!(is_test_path("app/SPEC/model_spec.rb"));
}

#[test]
fn is_test_path_dunder_tests_case_insensitive() {
    assert!(is_test_path("src/__TESTS__/component.js"));
}

#[test]
fn is_test_path_no_extension() {
    // "test_runner" starts with "test_"
    assert!(is_test_path("bin/test_runner"));
}

#[test]
fn is_test_path_rejects_testimony() {
    // "testimony" in filename doesn't match exact patterns
    assert!(!is_test_path("src/testimony.rs"));
}

// ── is_infra_lang: boundary detection ───────────────────────────────────────

#[test]
fn is_infra_lang_rejects_near_matches() {
    assert!(!is_infra_lang("json5"));
    assert!(!is_infra_lang("yaml2"));
    assert!(!is_infra_lang("toml2"));
    assert!(!is_infra_lang("xml2"));
}

#[test]
fn is_infra_lang_rejects_prefixed_langs() {
    assert!(!is_infra_lang("ejson"));
    assert!(!is_infra_lang("xhtml")); // only "html" matches
    assert!(!is_infra_lang("postcss"));
}

#[test]
fn is_infra_lang_all_case_variants_of_every_infra_lang() {
    // Exhaustive upper/lower/title case for every infra language
    let all_infra = [
        "json",
        "yaml",
        "toml",
        "markdown",
        "xml",
        "html",
        "css",
        "scss",
        "less",
        "makefile",
        "dockerfile",
        "hcl",
        "terraform",
        "nix",
        "cmake",
        "ini",
        "properties",
        "gitignore",
        "gitconfig",
        "editorconfig",
        "csv",
        "tsv",
        "svg",
    ];
    for lang in &all_infra {
        let upper = lang.to_uppercase();
        let mut title = String::new();
        for (i, c) in lang.chars().enumerate() {
            if i == 0 {
                title.extend(c.to_uppercase());
            } else {
                title.push(c);
            }
        }
        assert!(is_infra_lang(lang), "lower: {}", lang);
        assert!(is_infra_lang(&upper), "upper: {}", upper);
        assert!(is_infra_lang(&title), "title: {}", title);
    }
}

#[test]
fn is_infra_lang_rejects_numeric_and_special_strings() {
    assert!(!is_infra_lang("123"));
    assert!(!is_infra_lang("!!!"));
    assert!(!is_infra_lang("json\n"));
    assert!(!is_infra_lang("json\t"));
}

// ── empty_file_row: mutation independence ───────────────────────────────────

#[test]
fn empty_file_row_multiple_calls_independent() {
    let mut a = empty_file_row();
    let b = empty_file_row();
    a.code = 42;
    a.path = "modified".to_string();
    assert_eq!(b.code, 0);
    assert!(b.path.is_empty());
}

#[test]
fn empty_file_row_clone_is_independent() {
    let original = empty_file_row();
    let mut cloned = original.clone();
    cloned.code = 100;
    cloned.lang = "Rust".to_string();
    assert_eq!(original.code, 0);
    assert!(original.lang.is_empty());
}

#[test]
fn empty_file_row_lines_equal_code_plus_comments_plus_blanks() {
    let row = empty_file_row();
    // For the empty row, all are zero, so the invariant holds trivially
    assert_eq!(row.lines, row.code + row.comments + row.blanks);
}

// ── normalize_root: behavior on various path types ──────────────────────────

#[test]
fn normalize_root_relative_dot_resolves_to_cwd() {
    let result = normalize_root(Path::new("."));
    // "." should canonicalize to the current working directory
    assert!(result.is_absolute());
}

#[test]
fn normalize_root_empty_path_fallback() {
    let result = normalize_root(Path::new(""));
    // Empty path cannot be canonicalized; returns the empty PathBuf
    assert_eq!(result, PathBuf::from(""));
}

// ── AnalysisLimits: trait behavior ──────────────────────────────────────────

#[test]
fn analysis_limits_clone_is_equal_to_original() {
    let original = AnalysisLimits {
        max_files: Some(100),
        max_bytes: Some(50_000),
        max_file_bytes: Some(10_000),
        max_commits: Some(200),
        max_commit_files: Some(15),
    };
    let cloned = original.clone();
    assert_eq!(cloned.max_files, original.max_files);
    assert_eq!(cloned.max_bytes, original.max_bytes);
    assert_eq!(cloned.max_file_bytes, original.max_file_bytes);
    assert_eq!(cloned.max_commits, original.max_commits);
    assert_eq!(cloned.max_commit_files, original.max_commit_files);
}

#[test]
fn analysis_limits_debug_format_contains_field_names() {
    let limits = AnalysisLimits {
        max_files: Some(5),
        ..Default::default()
    };
    let debug = format!("{:?}", limits);
    assert!(debug.contains("max_files"));
    assert!(debug.contains("max_bytes"));
    assert!(debug.contains("max_file_bytes"));
    assert!(debug.contains("max_commits"));
    assert!(debug.contains("max_commit_files"));
}

#[test]
fn analysis_limits_default_then_override_single_field() {
    let limits = AnalysisLimits {
        max_commits: Some(999),
        ..Default::default()
    };
    assert_eq!(limits.max_commits, Some(999));
    assert!(limits.max_files.is_none());
    assert!(limits.max_bytes.is_none());
}

#[test]
fn analysis_limits_max_values() {
    let limits = AnalysisLimits {
        max_files: Some(usize::MAX),
        max_bytes: Some(u64::MAX),
        max_file_bytes: Some(u64::MAX),
        max_commits: Some(usize::MAX),
        max_commit_files: Some(usize::MAX),
    };
    assert_eq!(limits.max_files, Some(usize::MAX));
    assert_eq!(limits.max_bytes, Some(u64::MAX));
}

// ── now_ms: timing invariants ───────────────────────────────────────────────

#[test]
fn now_ms_monotonic_across_loop() {
    let mut prev = now_ms();
    for _ in 0..100 {
        let curr = now_ms();
        assert!(curr >= prev, "now_ms went backward: {} < {}", curr, prev);
        prev = curr;
    }
}

#[test]
fn now_ms_within_reasonable_range() {
    let ts = now_ms();
    // Should be between 2020 and 2100 in epoch millis
    let min_2020: u128 = 1_577_836_800_000;
    let max_2100: u128 = 4_102_444_800_000;
    assert!(
        ts > min_2020 && ts < max_2100,
        "timestamp out of range: {}",
        ts
    );
}

// ── round_f64: precision and special values ─────────────────────────────────

#[test]
fn round_f64_half_rounds_to_even_behavior() {
    // f64 .round() uses "round half away from zero"
    assert_eq!(round_f64(0.5, 0), 1.0);
    assert_eq!(round_f64(1.5, 0), 2.0);
    assert_eq!(round_f64(-0.5, 0), -1.0);
}

#[test]
fn round_f64_preserves_exact_values() {
    assert_eq!(round_f64(1.0, 5), 1.0);
    assert_eq!(round_f64(0.0, 0), 0.0);
    assert_eq!(round_f64(-1.0, 3), -1.0);
}

#[test]
#[allow(clippy::approx_constant)]
fn round_f64_high_precision() {
    let val = round_f64(std::f64::consts::PI, 10);
    assert!((val - 3.1415926536).abs() < 1e-10);
}

#[test]
fn round_f64_nan_stays_nan() {
    let val = round_f64(f64::NAN, 2);
    assert!(val.is_nan());
}

#[test]
fn round_f64_infinity_stays_infinity() {
    assert_eq!(round_f64(f64::INFINITY, 2), f64::INFINITY);
    assert_eq!(round_f64(f64::NEG_INFINITY, 2), f64::NEG_INFINITY);
}

// ── safe_ratio: edge cases ──────────────────────────────────────────────────

#[test]
fn safe_ratio_very_large_numerator() {
    let r = safe_ratio(usize::MAX, 1);
    assert!(r > 0.0);
    assert!(r.is_finite());
}

#[test]
fn safe_ratio_one_to_large_denominator() {
    let r = safe_ratio(1, usize::MAX);
    assert_eq!(r, 0.0); // rounds to 0.0000 at 4 decimals
}

#[test]
fn safe_ratio_rounds_to_four_decimals() {
    // 1/3 = 0.33333... rounds to 0.3333
    assert_eq!(safe_ratio(1, 3), 0.3333);
    // 2/3 = 0.66666... rounds to 0.6667
    assert_eq!(safe_ratio(2, 3), 0.6667);
}

// ── percentile: corner cases ────────────────────────────────────────────────

#[test]
fn percentile_all_same_values() {
    let vals = [7, 7, 7, 7, 7];
    for pct in [0.0, 0.25, 0.5, 0.75, 1.0] {
        assert_eq!(percentile(&vals, pct), 7.0, "pct={}", pct);
    }
}

#[test]
fn percentile_two_elements_boundaries() {
    let vals = [10, 20];
    assert_eq!(percentile(&vals, 0.0), 10.0);
    assert_eq!(percentile(&vals, 1.0), 20.0);
}

#[test]
fn percentile_large_sorted_input() {
    let vals: Vec<usize> = (1..=1000).collect();
    let p50 = percentile(&vals, 0.5);
    // Median of 1..=1000 should be around 500
    assert!((p50 - 500.0).abs() < 2.0, "p50={}", p50);
}

#[test]
fn percentile_p0_is_minimum_p100_is_maximum() {
    let vals = [5, 15, 25, 35, 45, 55];
    assert_eq!(percentile(&vals, 0.0), 5.0);
    assert_eq!(percentile(&vals, 1.0), 55.0);
}

// ── gini_coefficient: distribution shape tests ──────────────────────────────

#[test]
fn gini_coefficient_linear_increase() {
    // [1, 2, 3, 4, 5] — moderate inequality
    let g = gini_coefficient(&[1, 2, 3, 4, 5]);
    assert!(g > 0.1 && g < 0.5, "Expected moderate gini, got {}", g);
}

#[test]
fn gini_coefficient_single_large_with_many_small() {
    // High inequality: one large value among many small
    let mut vals = vec![1usize; 99];
    vals.push(10_000);
    vals.sort();
    let g = gini_coefficient(&vals);
    assert!(g > 0.9, "Expected very high gini, got {}", g);
}

#[test]
fn gini_coefficient_two_values_half() {
    // [0, X] should give gini = 0.5 for any positive X
    let g = gini_coefficient(&[0, 1000]);
    assert!((g - 0.5).abs() < 1e-10, "Expected 0.5, got {}", g);
}

#[test]
fn gini_coefficient_symmetric_around_middle() {
    // [1, 2, 3, 2, 1] sorted = [1, 1, 2, 2, 3]
    let g = gini_coefficient(&[1, 1, 2, 2, 3]);
    assert!(g > 0.0 && g < 0.5);
}

// ── Cross-function interaction tests ────────────────────────────────────────

#[test]
fn normalize_path_then_path_depth_consistent() {
    let root = PathBuf::from("project");
    let raw = r"project\src\deep\mod.rs";
    let normalized = normalize_path(raw, &root);
    // After normalization and root-stripping: "src/deep/mod.rs" → depth 3
    assert_eq!(path_depth(&normalized), 3);
}

#[test]
fn normalize_path_then_is_test_path_detects_test_dir() {
    let root = PathBuf::from("proj");
    let raw = r"proj\src\tests\unit.rs";
    let normalized = normalize_path(raw, &root);
    assert!(is_test_path(&normalized));
}

#[test]
fn normalize_path_then_is_test_path_rejects_non_test() {
    let root = PathBuf::from("proj");
    let raw = r"proj\src\lib.rs";
    let normalized = normalize_path(raw, &root);
    assert!(!is_test_path(&normalized));
}

#[test]
fn empty_file_row_depth_matches_path_depth_for_empty() {
    let row = empty_file_row();
    // Empty path → path_depth("") = 1 (max(0,1)), but row.depth is 0
    // This documents the intentional difference: empty_file_row sets depth=0
    assert_eq!(row.depth, 0);
    assert_eq!(path_depth(&row.path), 1);
}

#[test]
fn safe_ratio_result_matches_manual_round() {
    // safe_ratio(a, b) == round_f64(a as f64 / b as f64, 4) when b != 0
    let a = 7usize;
    let b = 13usize;
    let expected = round_f64(a as f64 / b as f64, 4);
    assert_eq!(safe_ratio(a, b), expected);
}