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
//! BDD-style scenario tests for topic-cloud extraction.

use crate::topics::build_topic_clouds;
use tokmd_types::{ChildIncludeMode, ExportData, FileKind, FileRow};

// ── helpers ──────────────────────────────────────────────────────────

fn make_row(path: &str, module: &str, code: usize, tokens: usize) -> FileRow {
    FileRow {
        path: path.to_string(),
        module: module.to_string(),
        lang: "Rust".to_string(),
        kind: FileKind::Parent,
        code,
        comments: 0,
        blanks: 0,
        lines: code,
        bytes: code * 10,
        tokens,
    }
}

fn make_export(rows: Vec<FileRow>, module_roots: Vec<&str>) -> ExportData {
    ExportData {
        rows,
        module_roots: module_roots.into_iter().map(String::from).collect(),
        module_depth: 2,
        children: ChildIncludeMode::Separate,
    }
}

// ── Scenario: empty input ────────────────────────────────────────────

#[test]
fn given_no_rows_then_overall_is_empty() {
    let export = make_export(vec![], vec![]);
    let clouds = build_topic_clouds(&export);
    assert!(clouds.overall.is_empty());
    assert!(clouds.per_module.is_empty());
}

// ── Scenario: only child rows (no parents) ───────────────────────────

#[test]
fn given_only_child_rows_then_topics_are_empty() {
    let mut row = make_row("src/lib.rs", "root", 10, 50);
    row.kind = FileKind::Child;
    let export = make_export(vec![row], vec![]);
    let clouds = build_topic_clouds(&export);
    assert!(clouds.overall.is_empty());
}

// ── Scenario: single file ────────────────────────────────────────────

#[test]
fn given_single_file_then_path_segments_appear_as_topics() {
    let rows = vec![make_row("crates/auth/src/login.rs", "crates/auth", 10, 50)];
    let export = make_export(rows, vec!["crates"]);
    let clouds = build_topic_clouds(&export);

    assert_eq!(clouds.per_module.len(), 1);
    let auth_terms = clouds.per_module.get("crates/auth").unwrap();
    let term_names: Vec<&str> = auth_terms.iter().map(|t| t.term.as_str()).collect();
    // "auth" is a module root → stopword, "src"/"rs" are stopwords
    assert!(
        term_names.contains(&"login"),
        "expected 'login' in {term_names:?}"
    );
}

#[test]
fn given_single_file_then_overall_matches_module() {
    let rows = vec![make_row("crates/auth/src/login.rs", "crates/auth", 10, 50)];
    let export = make_export(rows, vec!["crates"]);
    let clouds = build_topic_clouds(&export);

    let module_terms = clouds.per_module.get("crates/auth").unwrap();
    // Overall should contain the same terms (single module)
    for mt in module_terms {
        assert!(
            clouds.overall.iter().any(|ov| ov.term == mt.term),
            "overall missing term '{}'",
            mt.term
        );
    }
}

// ── Scenario: stopwords are filtered ─────────────────────────────────

#[test]
fn given_path_with_stopwords_then_they_are_excluded() {
    let rows = vec![make_row("src/lib/mod/index.rs", "root", 10, 50)];
    let export = make_export(rows, vec![]);
    let clouds = build_topic_clouds(&export);

    // All segments are stopwords → nothing extracted
    assert!(clouds.overall.is_empty());
}

#[test]
fn given_extension_stopwords_then_they_are_excluded() {
    let rows = vec![make_row("auth/handler.py", "auth", 10, 50)];
    let export = make_export(rows, vec![]);
    let clouds = build_topic_clouds(&export);

    let terms: Vec<&str> = clouds.overall.iter().map(|t| t.term.as_str()).collect();
    assert!(!terms.contains(&"py"), "'py' should be a stopword");
    assert!(terms.contains(&"handler"), "expected 'handler'");
}

#[test]
fn given_module_roots_then_they_become_stopwords() {
    let rows = vec![make_row("packages/core/util.ts", "packages/core", 10, 50)];
    let export = make_export(rows, vec!["packages"]);
    let clouds = build_topic_clouds(&export);

    let terms: Vec<&str> = clouds.overall.iter().map(|t| t.term.as_str()).collect();
    assert!(
        !terms.contains(&"packages"),
        "'packages' is a module root stopword"
    );
}

// ── Scenario: TF-IDF scoring ─────────────────────────────────────────

#[test]
fn given_common_term_across_all_modules_then_lower_score_than_unique_term() {
    let rows = vec![
        make_row("app/shared/utils.rs", "app/shared", 10, 50),
        make_row("app/api/utils.rs", "app/api", 10, 50),
        make_row("app/api/controller.rs", "app/api", 10, 50),
    ];
    let export = make_export(rows, vec!["app"]);
    let clouds = build_topic_clouds(&export);

    let find = |term: &str| clouds.overall.iter().find(|t| t.term == term);
    let _utils_score = find("utils").map(|t| t.score).unwrap_or(0.0);
    let controller_score = find("controller").map(|t| t.score).unwrap_or(0.0);

    // "controller" appears in only 1 file → lower df → higher IDF per-occurrence
    assert!(
        controller_score > 0.0,
        "controller should have a positive score"
    );
}

#[test]
fn given_term_in_single_module_then_high_idf() {
    let rows = vec![
        make_row("mod_a/unique_term.rs", "mod_a", 10, 50),
        make_row("mod_b/common.rs", "mod_b", 10, 50),
    ];
    let export = make_export(rows, vec![]);
    let clouds = build_topic_clouds(&export);

    let find_mod = |module: &str, term: &str| {
        clouds
            .per_module
            .get(module)
            .and_then(|v| v.iter().find(|t| t.term == term))
    };

    let unique = find_mod("mod_a", "unique");
    assert!(unique.is_some(), "expected 'unique' in mod_a");
    assert_eq!(unique.unwrap().df, 1);
}

// ── Scenario: weight by tokens ───────────────────────────────────────

#[test]
fn given_file_with_more_tokens_then_higher_tf() {
    let rows = vec![
        make_row("app/heavy.rs", "app", 100, 5000),
        make_row("app/light.rs", "app", 10, 10),
    ];
    let export = make_export(rows, vec!["app"]);
    let clouds = build_topic_clouds(&export);

    let find = |term: &str| clouds.overall.iter().find(|t| t.term == term);
    let heavy_tf = find("heavy").map(|t| t.tf).unwrap_or(0);
    let light_tf = find("light").map(|t| t.tf).unwrap_or(0);

    assert!(
        heavy_tf > light_tf,
        "heavy ({heavy_tf}) should have higher tf than light ({light_tf})"
    );
}

#[test]
fn given_zero_tokens_then_weight_is_at_least_one() {
    let rows = vec![make_row("app/empty.rs", "app", 0, 0)];
    let export = make_export(rows, vec!["app"]);
    let clouds = build_topic_clouds(&export);

    let term = clouds.overall.iter().find(|t| t.term == "empty");
    assert!(term.is_some(), "term should exist even with 0 tokens");
    assert!(term.unwrap().tf >= 1, "tf should be at least 1");
}

// ── Scenario: path normalization ─────────────────────────────────────

#[test]
fn given_backslash_paths_then_segments_are_split_correctly() {
    let rows = vec![make_row(r"crates\auth\src\login.rs", "crates/auth", 10, 50)];
    let export = make_export(rows, vec!["crates"]);
    let clouds = build_topic_clouds(&export);

    let terms: Vec<&str> = clouds.overall.iter().map(|t| t.term.as_str()).collect();
    assert!(
        terms.contains(&"login"),
        "backslash path should yield 'login'"
    );
}

// ── Scenario: tokenizer splits on underscore, hyphen, dot ────────────

#[test]
fn given_compound_filename_then_split_into_tokens() {
    let rows = vec![make_row("app/my_api-client.v2.rs", "app", 10, 50)];
    let export = make_export(rows, vec!["app"]);
    let clouds = build_topic_clouds(&export);

    let terms: Vec<&str> = clouds.overall.iter().map(|t| t.term.as_str()).collect();
    assert!(terms.contains(&"my"), "expected 'my' from underscore split");
    assert!(
        terms.contains(&"api"),
        "expected 'api' from underscore split"
    );
    assert!(
        terms.contains(&"client"),
        "expected 'client' from hyphen split"
    );
    assert!(terms.contains(&"v2"), "expected 'v2' from dot split");
}

// ── Scenario: TOP_K truncation ───────────────────────────────────────

#[test]
fn given_many_unique_terms_then_per_module_truncated_to_at_most_8() {
    let rows: Vec<FileRow> = (0..20)
        .map(|i| make_row(&format!("app/term{i}.rs", i = i), "app", 10, 50))
        .collect();
    let export = make_export(rows, vec!["app"]);
    let clouds = build_topic_clouds(&export);

    let app_terms = clouds.per_module.get("app").unwrap();
    assert!(
        app_terms.len() <= 8,
        "per-module should be truncated to TOP_K=8, got {}",
        app_terms.len()
    );
}

#[test]
fn given_many_unique_terms_then_overall_truncated_to_at_most_8() {
    let rows: Vec<FileRow> = (0..20)
        .map(|i| make_row(&format!("app/term{i}.rs", i = i), "app", 10, 50))
        .collect();
    let export = make_export(rows, vec!["app"]);
    let clouds = build_topic_clouds(&export);

    assert!(
        clouds.overall.len() <= 8,
        "overall should be truncated to TOP_K=8, got {}",
        clouds.overall.len()
    );
}

// ── Scenario: deterministic ordering ─────────────────────────────────

#[test]
fn given_same_input_twice_then_identical_output() {
    let make = || {
        let rows = vec![
            make_row("app/auth/login.rs", "app/auth", 10, 50),
            make_row("app/auth/logout.rs", "app/auth", 10, 50),
            make_row("app/db/pool.rs", "app/db", 10, 50),
            make_row("app/db/migrate.rs", "app/db", 10, 50),
        ];
        make_export(rows, vec!["app"])
    };

    let a = build_topic_clouds(&make());
    let b = build_topic_clouds(&make());

    // Compare overall
    assert_eq!(a.overall.len(), b.overall.len());
    for (ta, tb) in a.overall.iter().zip(b.overall.iter()) {
        assert_eq!(ta.term, tb.term);
        assert_eq!(ta.tf, tb.tf);
        assert_eq!(ta.df, tb.df);
        assert!((ta.score - tb.score).abs() < f64::EPSILON);
    }

    // Compare per_module keys and terms
    assert_eq!(
        a.per_module.keys().collect::<Vec<_>>(),
        b.per_module.keys().collect::<Vec<_>>()
    );
    for key in a.per_module.keys() {
        let va = &a.per_module[key];
        let vb = &b.per_module[key];
        assert_eq!(va.len(), vb.len());
        for (ta, tb) in va.iter().zip(vb.iter()) {
            assert_eq!(ta.term, tb.term);
        }
    }
}

// ── Scenario: multiple modules with shared and unique terms ──────────

#[test]
fn given_multiple_modules_then_per_module_maps_are_separate() {
    let rows = vec![
        make_row("svc/auth/handler.rs", "svc/auth", 10, 50),
        make_row("svc/billing/handler.rs", "svc/billing", 10, 50),
        make_row("svc/billing/invoice.rs", "svc/billing", 10, 50),
    ];
    let export = make_export(rows, vec!["svc"]);
    let clouds = build_topic_clouds(&export);

    assert!(clouds.per_module.contains_key("svc/auth"));
    assert!(clouds.per_module.contains_key("svc/billing"));
    assert_eq!(clouds.per_module.len(), 2);

    let billing = clouds.per_module.get("svc/billing").unwrap();
    let billing_terms: Vec<&str> = billing.iter().map(|t| t.term.as_str()).collect();
    assert!(billing_terms.contains(&"invoice"));
}

// ── Scenario: scores are non-negative ────────────────────────────────

#[test]
fn given_any_input_then_all_scores_are_non_negative() {
    let rows = vec![
        make_row("a/b/c.rs", "a/b", 10, 50),
        make_row("x/y/z.rs", "x/y", 5, 25),
    ];
    let export = make_export(rows, vec![]);
    let clouds = build_topic_clouds(&export);

    for term in &clouds.overall {
        assert!(
            term.score >= 0.0,
            "score should be >= 0, got {}",
            term.score
        );
    }
    for terms in clouds.per_module.values() {
        for term in terms {
            assert!(
                term.score >= 0.0,
                "score should be >= 0, got {}",
                term.score
            );
        }
    }
}

// ── Scenario: overall sorting is descending by score ─────────────────

#[test]
fn given_multiple_terms_then_overall_sorted_descending_by_score() {
    let rows = vec![
        make_row("a/alpha.rs", "a", 100, 500),
        make_row("a/beta.rs", "a", 10, 50),
        make_row("b/gamma.rs", "b", 10, 50),
    ];
    let export = make_export(rows, vec![]);
    let clouds = build_topic_clouds(&export);

    for window in clouds.overall.windows(2) {
        assert!(
            window[0].score >= window[1].score,
            "overall not sorted: {} ({}) should >= {} ({})",
            window[0].term,
            window[0].score,
            window[1].term,
            window[1].score,
        );
    }
}

// ── Scenario: case normalization ─────────────────────────────────────

#[test]
fn given_mixed_case_path_then_terms_are_lowercased() {
    let rows = vec![make_row("App/MyController.rs", "App", 10, 50)];
    let export = make_export(rows, vec![]);
    let clouds = build_topic_clouds(&export);

    for term in &clouds.overall {
        assert_eq!(
            term.term,
            term.term.to_lowercase(),
            "term '{}' should be lowercase",
            term.term
        );
    }
}

// ── Scenario: df counts ──────────────────────────────────────────────

#[test]
fn given_term_in_two_modules_then_df_is_two() {
    let rows = vec![
        make_row("mod_a/shared.rs", "mod_a", 10, 50),
        make_row("mod_b/shared.rs", "mod_b", 10, 50),
    ];
    let export = make_export(rows, vec![]);
    let clouds = build_topic_clouds(&export);

    let shared = clouds.overall.iter().find(|t| t.term == "shared");
    assert!(shared.is_some());
    assert_eq!(shared.unwrap().df, 2);
}

#[test]
fn given_term_repeated_in_same_module_then_df_counts_files() {
    // df counts per-file (document) occurrences, not per-module
    let rows = vec![
        make_row("mod_a/widget_one.rs", "mod_a", 10, 50),
        make_row("mod_a/widget_two.rs", "mod_a", 10, 50),
    ];
    let export = make_export(rows, vec![]);
    let clouds = build_topic_clouds(&export);

    let widget = clouds
        .per_module
        .get("mod_a")
        .and_then(|v| v.iter().find(|t| t.term == "widget"));
    assert!(widget.is_some());
    assert_eq!(widget.unwrap().df, 2, "df counts files containing the term");
}