tokmd-analysis 1.10.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
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
//! Deep tests for `tokmd-analysis content module`.
//!
//! Exercises build_todo_report, build_duplicate_report, and build_import_report
//! with edge cases, serialization roundtrips, and realistic inputs.

use std::path::PathBuf;

use crate::content::{
    ContentLimits, ImportGranularity, build_duplicate_report, build_import_report,
    build_todo_report,
};
use tempfile::TempDir;
use tokmd_types::{ChildIncludeMode, ExportData, FileKind, FileRow};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn file_row(path: &str, module: &str, lang: &str, bytes: usize) -> FileRow {
    FileRow {
        path: path.to_string(),
        module: module.to_string(),
        lang: lang.to_string(),
        kind: FileKind::Parent,
        code: 10,
        comments: 2,
        blanks: 1,
        lines: 13,
        bytes,
        tokens: 80,
    }
}

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

fn write_file(dir: &std::path::Path, rel: &str, content: &[u8]) -> PathBuf {
    let full = dir.join(rel);
    if let Some(parent) = full.parent() {
        std::fs::create_dir_all(parent).unwrap();
    }
    std::fs::write(&full, content).unwrap();
    PathBuf::from(rel)
}

// ===========================================================================
// build_todo_report
// ===========================================================================

// 1. Empty file list → zero total
#[test]
fn todo_empty_files() {
    let tmp = TempDir::new().unwrap();
    let report = build_todo_report(tmp.path(), &[], &ContentLimits::default(), 1000).unwrap();
    assert_eq!(report.total, 0);
    assert!(report.tags.is_empty());
}

// 2. File with no tags → zero total
#[test]
fn todo_no_tags_in_file() {
    let tmp = TempDir::new().unwrap();
    let rel = write_file(
        tmp.path(),
        "clean.rs",
        b"fn main() { println!(\"hello\"); }\n",
    );
    let report = build_todo_report(tmp.path(), &[rel], &ContentLimits::default(), 1000).unwrap();
    assert_eq!(report.total, 0);
}

// 3. All four tag types counted
#[test]
fn todo_all_four_tags() {
    let tmp = TempDir::new().unwrap();
    let content = "// TODO: a\n// FIXME: b\n// HACK: c\n// XXX: d\n";
    let rel = write_file(tmp.path(), "tags.rs", content.as_bytes());
    let report = build_todo_report(tmp.path(), &[rel], &ContentLimits::default(), 1000).unwrap();
    assert_eq!(report.total, 4);
    let tags: std::collections::BTreeMap<String, usize> = report
        .tags
        .iter()
        .map(|t| (t.tag.clone(), t.count))
        .collect();
    assert_eq!(tags["TODO"], 1);
    assert_eq!(tags["FIXME"], 1);
    assert_eq!(tags["HACK"], 1);
    assert_eq!(tags["XXX"], 1);
}

// 4. Multiple TODOs in one file
#[test]
fn todo_multiple_same_tag() {
    let tmp = TempDir::new().unwrap();
    let content = "// TODO: first\n// TODO: second\n// TODO: third\nfn f() {}\n";
    let rel = write_file(tmp.path(), "multi.rs", content.as_bytes());
    let report = build_todo_report(tmp.path(), &[rel], &ContentLimits::default(), 1000).unwrap();
    assert_eq!(report.total, 3);
}

// 5. Density per KLOC calculation
#[test]
fn todo_density_per_kloc() {
    let tmp = TempDir::new().unwrap();
    let content = "// TODO: one\n// TODO: two\n";
    let rel = write_file(tmp.path(), "d.rs", content.as_bytes());
    let report = build_todo_report(tmp.path(), &[rel], &ContentLimits::default(), 2000).unwrap();
    // 2 TODOs / 2.0 KLOC = 1.0
    assert_eq!(report.density_per_kloc, 1.0);
}

// 6. Density with zero code lines
#[test]
fn todo_density_zero_code() {
    let tmp = TempDir::new().unwrap();
    let rel = write_file(tmp.path(), "empty.rs", b"// TODO: x\n");
    let report = build_todo_report(tmp.path(), &[rel], &ContentLimits::default(), 0).unwrap();
    assert_eq!(report.density_per_kloc, 0.0);
}

// 7. Multiple files aggregated
#[test]
fn todo_multiple_files() {
    let tmp = TempDir::new().unwrap();
    let f1 = write_file(tmp.path(), "a.rs", b"// TODO: a\n");
    let f2 = write_file(tmp.path(), "b.rs", b"// FIXME: b\n// TODO: c\n");
    let report = build_todo_report(tmp.path(), &[f1, f2], &ContentLimits::default(), 1000).unwrap();
    assert_eq!(report.total, 3);
}

// 8. TodoReport JSON serialization roundtrip
#[test]
fn todo_serialization_roundtrip() {
    let tmp = TempDir::new().unwrap();
    let rel = write_file(tmp.path(), "t.rs", b"// TODO: ser\n// FIXME: rt\n");
    let report = build_todo_report(tmp.path(), &[rel], &ContentLimits::default(), 5000).unwrap();
    let json = serde_json::to_string(&report).unwrap();
    let deser: tokmd_analysis_types::TodoReport = serde_json::from_str(&json).unwrap();
    assert_eq!(deser.total, report.total);
    assert_eq!(deser.tags.len(), report.tags.len());
    assert_eq!(deser.density_per_kloc, report.density_per_kloc);
}

// ===========================================================================
// build_duplicate_report
// ===========================================================================

// 9. Empty file list → no duplicates
#[test]
fn dup_empty_files() {
    let tmp = TempDir::new().unwrap();
    let exp = export(vec![]);
    let report = build_duplicate_report(tmp.path(), &[], &exp, &ContentLimits::default()).unwrap();
    assert_eq!(report.wasted_bytes, 0);
    assert!(report.groups.is_empty());
    assert_eq!(report.strategy, "exact-blake3");
}

// 10. Two identical files → one duplicate group
#[test]
fn dup_two_identical_files() {
    let tmp = TempDir::new().unwrap();
    let content = b"identical content here\n";
    let f1 = write_file(tmp.path(), "a.rs", content);
    let f2 = write_file(tmp.path(), "b.rs", content);
    let exp = export(vec![
        file_row("a.rs", "src", "Rust", content.len()),
        file_row("b.rs", "src", "Rust", content.len()),
    ]);
    let report =
        build_duplicate_report(tmp.path(), &[f1, f2], &exp, &ContentLimits::default()).unwrap();
    assert_eq!(report.groups.len(), 1);
    assert_eq!(report.groups[0].files.len(), 2);
    assert_eq!(report.wasted_bytes, content.len() as u64);
}

// 11. Two different files → no duplicates
#[test]
fn dup_different_files() {
    let tmp = TempDir::new().unwrap();
    let f1 = write_file(tmp.path(), "a.rs", b"content a\n");
    let f2 = write_file(tmp.path(), "b.rs", b"content b\n");
    let exp = export(vec![
        file_row("a.rs", "src", "Rust", 10),
        file_row("b.rs", "src", "Rust", 10),
    ]);
    let report =
        build_duplicate_report(tmp.path(), &[f1, f2], &exp, &ContentLimits::default()).unwrap();
    assert!(report.groups.is_empty());
}

// 12. Three identical files → wasted = 2 * size
#[test]
fn dup_three_identical_files() {
    let tmp = TempDir::new().unwrap();
    let content = b"same content\n";
    let f1 = write_file(tmp.path(), "x.rs", content);
    let f2 = write_file(tmp.path(), "y.rs", content);
    let f3 = write_file(tmp.path(), "z.rs", content);
    let exp = export(vec![
        file_row("x.rs", "m", "Rust", content.len()),
        file_row("y.rs", "m", "Rust", content.len()),
        file_row("z.rs", "m", "Rust", content.len()),
    ]);
    let report =
        build_duplicate_report(tmp.path(), &[f1, f2, f3], &exp, &ContentLimits::default()).unwrap();
    assert_eq!(report.groups.len(), 1);
    assert_eq!(report.groups[0].files.len(), 3);
    assert_eq!(report.wasted_bytes, 2 * content.len() as u64);
}

// 13. Empty files not counted as duplicates
#[test]
fn dup_empty_files_skipped() {
    let tmp = TempDir::new().unwrap();
    let f1 = write_file(tmp.path(), "empty1.rs", b"");
    let f2 = write_file(tmp.path(), "empty2.rs", b"");
    let exp = export(vec![
        file_row("empty1.rs", "m", "Rust", 0),
        file_row("empty2.rs", "m", "Rust", 0),
    ]);
    let report =
        build_duplicate_report(tmp.path(), &[f1, f2], &exp, &ContentLimits::default()).unwrap();
    // Empty files (size 0) are skipped
    assert!(report.groups.is_empty());
}

// 14. Duplicate groups sorted by bytes desc
#[test]
fn dup_groups_sorted_by_bytes_desc() {
    let tmp = TempDir::new().unwrap();
    let small = b"sm\n";
    let large = b"this is a much larger content block!\n";
    let f1 = write_file(tmp.path(), "s1.rs", small);
    let f2 = write_file(tmp.path(), "s2.rs", small);
    let f3 = write_file(tmp.path(), "l1.rs", large);
    let f4 = write_file(tmp.path(), "l2.rs", large);
    let exp = export(vec![
        file_row("s1.rs", "m", "Rust", small.len()),
        file_row("s2.rs", "m", "Rust", small.len()),
        file_row("l1.rs", "m", "Rust", large.len()),
        file_row("l2.rs", "m", "Rust", large.len()),
    ]);
    let report = build_duplicate_report(
        tmp.path(),
        &[f1, f2, f3, f4],
        &exp,
        &ContentLimits::default(),
    )
    .unwrap();
    assert_eq!(report.groups.len(), 2);
    assert!(report.groups[0].bytes >= report.groups[1].bytes);
}

// 15. DuplicateReport JSON serialization roundtrip
#[test]
fn dup_serialization_roundtrip() {
    let tmp = TempDir::new().unwrap();
    let content = b"dup content\n";
    let f1 = write_file(tmp.path(), "a.rs", content);
    let f2 = write_file(tmp.path(), "b.rs", content);
    let exp = export(vec![
        file_row("a.rs", "m", "Rust", content.len()),
        file_row("b.rs", "m", "Rust", content.len()),
    ]);
    let report =
        build_duplicate_report(tmp.path(), &[f1, f2], &exp, &ContentLimits::default()).unwrap();
    let json = serde_json::to_string(&report).unwrap();
    let deser: tokmd_analysis_types::DuplicateReport = serde_json::from_str(&json).unwrap();
    assert_eq!(deser.groups.len(), report.groups.len());
    assert_eq!(deser.wasted_bytes, report.wasted_bytes);
    assert_eq!(deser.strategy, "exact-blake3");
}

// 16. Density report present and correct
#[test]
fn dup_density_report() {
    let tmp = TempDir::new().unwrap();
    let content = b"duplicate block\n";
    let f1 = write_file(tmp.path(), "a.rs", content);
    let f2 = write_file(tmp.path(), "b.rs", content);
    let exp = export(vec![
        file_row("a.rs", "mod", "Rust", content.len()),
        file_row("b.rs", "mod", "Rust", content.len()),
    ]);
    let report =
        build_duplicate_report(tmp.path(), &[f1, f2], &exp, &ContentLimits::default()).unwrap();
    let density = report.density.as_ref().expect("density present");
    assert_eq!(density.duplicate_groups, 1);
    assert_eq!(density.duplicate_files, 2);
    assert!(density.wasted_pct_of_codebase > 0.0);
    assert!(density.wasted_pct_of_codebase <= 1.0);
}

// ===========================================================================
// build_import_report
// ===========================================================================

// 17. Empty files → empty edges
#[test]
fn import_empty_files() {
    let tmp = TempDir::new().unwrap();
    let exp = export(vec![]);
    let report = build_import_report(
        tmp.path(),
        &[],
        &exp,
        ImportGranularity::Module,
        &ContentLimits::default(),
    )
    .unwrap();
    assert!(report.edges.is_empty());
    assert_eq!(report.granularity, "module");
}

// 18. Rust file with use statement → import edge
#[test]
fn import_rust_use_statement() {
    let tmp = TempDir::new().unwrap();
    let content = b"use std::collections::HashMap;\nfn main() {}\n";
    let rel = write_file(tmp.path(), "main.rs", content);
    let exp = export(vec![file_row("main.rs", "root", "Rust", content.len())]);
    let report = build_import_report(
        tmp.path(),
        &[rel],
        &exp,
        ImportGranularity::Module,
        &ContentLimits::default(),
    )
    .unwrap();
    assert!(!report.edges.is_empty());
}

// 19. File granularity sets granularity field
#[test]
fn import_file_granularity() {
    let tmp = TempDir::new().unwrap();
    let exp = export(vec![]);
    let report = build_import_report(
        tmp.path(),
        &[],
        &exp,
        ImportGranularity::File,
        &ContentLimits::default(),
    )
    .unwrap();
    assert_eq!(report.granularity, "file");
}

// 20. ImportReport JSON serialization roundtrip
#[test]
fn import_serialization_roundtrip() {
    let tmp = TempDir::new().unwrap();
    let content = b"use serde::Serialize;\nuse anyhow::Result;\n";
    let rel = write_file(tmp.path(), "lib.rs", content);
    let exp = export(vec![file_row("lib.rs", "core", "Rust", content.len())]);
    let report = build_import_report(
        tmp.path(),
        &[rel],
        &exp,
        ImportGranularity::Module,
        &ContentLimits::default(),
    )
    .unwrap();
    let json = serde_json::to_string(&report).unwrap();
    let deser: tokmd_analysis_types::ImportReport = serde_json::from_str(&json).unwrap();
    assert_eq!(deser.granularity, report.granularity);
    assert_eq!(deser.edges.len(), report.edges.len());
}

// 21. Import edges sorted by count desc
#[test]
fn import_edges_sorted_by_count() {
    let tmp = TempDir::new().unwrap();
    // Two Rust files importing different things, one importing the same thing twice
    let content = b"use std::io;\nuse std::io;\nuse std::collections::HashMap;\n";
    let rel = write_file(tmp.path(), "multi.rs", content);
    let exp = export(vec![file_row("multi.rs", "root", "Rust", content.len())]);
    let report = build_import_report(
        tmp.path(),
        &[rel],
        &exp,
        ImportGranularity::Module,
        &ContentLimits::default(),
    )
    .unwrap();
    if report.edges.len() >= 2 {
        assert!(report.edges[0].count >= report.edges[1].count);
    }
}

// 22. Unsupported language produces no edges
#[test]
fn import_unsupported_language() {
    let tmp = TempDir::new().unwrap();
    let content = b"some assembly code\n";
    let rel = write_file(tmp.path(), "code.asm", content);
    let exp = export(vec![file_row(
        "code.asm",
        "root",
        "Assembly",
        content.len(),
    )]);
    let report = build_import_report(
        tmp.path(),
        &[rel],
        &exp,
        ImportGranularity::Module,
        &ContentLimits::default(),
    )
    .unwrap();
    assert!(report.edges.is_empty());
}

// 23. ContentLimits max_file_bytes respected for TODO scanning
#[test]
fn todo_respects_per_file_limit() {
    let tmp = TempDir::new().unwrap();
    // Write a file with a TODO far past the limit
    let mut content = String::new();
    for _ in 0..200 {
        content.push_str("// normal line\n");
    }
    content.push_str("// TODO: should not be found\n");
    let rel = write_file(tmp.path(), "big.rs", content.as_bytes());
    let limits = ContentLimits {
        max_bytes: None,
        max_file_bytes: Some(100), // Only read first 100 bytes
    };
    let report = build_todo_report(tmp.path(), &[rel], &limits, 1000).unwrap();
    // The TODO is past byte 100, so it should not be found
    assert_eq!(report.total, 0);
}

// 24. ContentLimits max_bytes limits total scanning
#[test]
fn todo_respects_total_byte_limit() {
    let tmp = TempDir::new().unwrap();
    // First file is large enough to exceed the limit by itself
    let big_content = "// TODO: first file\n".repeat(10); // ~200 bytes
    let f1 = write_file(tmp.path(), "a.rs", big_content.as_bytes());
    let f2 = write_file(tmp.path(), "b.rs", b"// TODO: second file\n");
    let limits = ContentLimits {
        max_bytes: Some(big_content.len() as u64), // Limit reached after first file
        max_file_bytes: None,
    };
    let report = build_todo_report(tmp.path(), &[f1, f2], &limits, 1000).unwrap();
    // Only first file should be scanned (10 TODOs), second file skipped
    assert_eq!(report.total, 10);
}

// 25. Duplicate density by_module attribution
#[test]
fn dup_density_by_module() {
    let tmp = TempDir::new().unwrap();
    let content = b"same content in both\n";
    let f1 = write_file(tmp.path(), "mod_a/x.rs", content);
    let f2 = write_file(tmp.path(), "mod_b/y.rs", content);
    let exp = export(vec![
        file_row("mod_a/x.rs", "mod_a", "Rust", content.len()),
        file_row("mod_b/y.rs", "mod_b", "Rust", content.len()),
    ]);
    let report =
        build_duplicate_report(tmp.path(), &[f1, f2], &exp, &ContentLimits::default()).unwrap();
    let density = report.density.as_ref().unwrap();
    assert!(!density.by_module.is_empty());
}

// 26. Child file kind rows excluded from duplicate analysis
#[test]
fn dup_excludes_child_rows() {
    let tmp = TempDir::new().unwrap();
    let content = b"child content\n";
    let f1 = write_file(tmp.path(), "a.rs", content);
    let f2 = write_file(tmp.path(), "b.rs", content);
    let mut rows = vec![file_row("a.rs", "m", "Rust", content.len())];
    rows.push(FileRow {
        path: "b.rs".to_string(),
        module: "m".to_string(),
        lang: "Rust".to_string(),
        kind: FileKind::Child,
        code: 10,
        comments: 0,
        blanks: 0,
        lines: 10,
        bytes: content.len(),
        tokens: 50,
    });
    let exp = export(rows);
    let report =
        build_duplicate_report(tmp.path(), &[f1, f2], &exp, &ContentLimits::default()).unwrap();
    // b.rs is a Child, so path_to_module won't have it; duplicate still detected
    // but module attribution uses "(unknown)" for b.rs
    assert_eq!(report.groups.len(), 1);
}