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
//! Wave-38 deep tests for `analysis Halstead module`.
//!
//! Focuses on areas not yet covered by the existing deep/bdd/edge/unit suites:
//! - Ruby-specific tokenization (spaceship <=>, regex =~, splat ...)
//! - C-family specific tokenization (sizeof, typeof, ::, ->)
//! - round_f64 edge cases (negative values, large values, zero decimals)
//! - is_halstead_lang coverage for all supported + unsupported languages
//! - operators_for_lang returns empty for unsupported
//! - HalsteadMetrics serde round-trip
//! - HalsteadMetrics JSON shape validation
//! - time_seconds and estimated_bugs formulas
//! - Comment-only file produces all-zero metrics
//! - Multi-file aggregation merges operator/operand counts
//! - Unsupported language files are skipped in build_halstead_report
//! - FileTokenCounts fields consistency
//! - Tokenizer handles CRLF line endings
//! - Logical operators (&&, ||) counted correctly
//! - Nested expressions with many distinct operands

use std::path::PathBuf;

use crate::halstead::{
    build_halstead_report, is_halstead_lang, operators_for_lang, round_f64, tokenize_for_halstead,
};
use tokmd_analysis_types::AnalysisLimits;
use tokmd_analysis_types::HalsteadMetrics;
use tokmd_types::{ChildIncludeMode, ExportData, FileKind, FileRow};

fn no_limits() -> AnalysisLimits {
    AnalysisLimits {
        max_files: None,
        max_bytes: None,
        max_file_bytes: None,
        max_commits: None,
        max_commit_files: None,
    }
}

fn make_row(path: &str, lang: &str) -> FileRow {
    FileRow {
        path: path.to_string(),
        module: String::new(),
        lang: lang.to_string(),
        kind: FileKind::Parent,
        code: 10,
        comments: 0,
        blanks: 0,
        lines: 10,
        bytes: 100,
        tokens: 50,
    }
}

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

fn build_report_for_code(code: &str, lang: &str, filename: &str) -> HalsteadMetrics {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join(filename), code).unwrap();
    let export = make_export(vec![make_row(filename, lang)]);
    let files = vec![PathBuf::from(filename)];
    build_halstead_report(dir.path(), &files, &export, &no_limits()).unwrap()
}

// ═══════════════════════════════════════════════════════════════════
// § Ruby-specific tokenization
// ═══════════════════════════════════════════════════════════════════

#[test]
fn ruby_spaceship_operator() {
    let counts = tokenize_for_halstead("a <=> b", "ruby");
    assert!(
        counts.operators.contains_key("<=>"),
        "Ruby should detect spaceship operator <=>"
    );
}

#[test]
fn ruby_regex_match_operator() {
    let counts = tokenize_for_halstead("x =~ pattern", "ruby");
    assert!(
        counts.operators.contains_key("=~"),
        "Ruby should detect regex match operator =~"
    );
}

#[test]
fn ruby_not_match_operator() {
    let counts = tokenize_for_halstead("x !~ pattern", "ruby");
    assert!(
        counts.operators.contains_key("!~"),
        "Ruby should detect negated match operator !~"
    );
}

#[test]
fn ruby_range_operators() {
    let counts = tokenize_for_halstead("a = 1..10\nb = 1...10", "ruby");
    assert!(counts.operators.contains_key(".."));
    assert!(counts.operators.contains_key("..."));
}

#[test]
fn ruby_keywords_as_operators() {
    let code = "def foo\n  yield\n  rescue\n  ensure\nend";
    let counts = tokenize_for_halstead(code, "ruby");
    assert!(counts.operators.contains_key("def"));
    assert!(counts.operators.contains_key("yield"));
    assert!(counts.operators.contains_key("rescue"));
    assert!(counts.operators.contains_key("ensure"));
    assert!(counts.operators.contains_key("end"));
}

// ═══════════════════════════════════════════════════════════════════
// § C-family tokenization
// ═══════════════════════════════════════════════════════════════════

#[test]
fn c_sizeof_operator() {
    let counts = tokenize_for_halstead("int x = sizeof(int);", "c");
    assert!(counts.operators.contains_key("sizeof"));
}

#[test]
fn c_arrow_and_scope_operators() {
    let counts = tokenize_for_halstead("ptr->field\nClass::method", "c++");
    assert!(counts.operators.contains_key("->"));
    assert!(counts.operators.contains_key("::"));
}

// ═══════════════════════════════════════════════════════════════════
// § round_f64 edge cases
// ═══════════════════════════════════════════════════════════════════

#[test]
fn round_f64_zero_decimals() {
    assert_eq!(round_f64(3.7, 0), 4.0);
    assert_eq!(round_f64(3.2, 0), 3.0);
}

#[test]
fn round_f64_negative_values() {
    assert_eq!(round_f64(-1.555, 2), -1.56);
    assert_eq!(round_f64(-0.005, 2), -0.01);
}

#[test]
fn round_f64_large_values() {
    let result = round_f64(123456.789, 2);
    assert!((result - 123456.79).abs() < 0.001);
}

#[test]
fn round_f64_zero() {
    assert_eq!(round_f64(0.0, 5), 0.0);
}

// ═══════════════════════════════════════════════════════════════════
// § is_halstead_lang coverage
// ═══════════════════════════════════════════════════════════════════

#[test]
fn all_supported_languages() {
    let supported = [
        "rust",
        "Rust",
        "RUST",
        "javascript",
        "JavaScript",
        "typescript",
        "TypeScript",
        "python",
        "Python",
        "go",
        "Go",
        "c",
        "C",
        "c++",
        "C++",
        "java",
        "Java",
        "c#",
        "C#",
        "php",
        "PHP",
        "ruby",
        "Ruby",
    ];
    for lang in &supported {
        assert!(is_halstead_lang(lang), "{lang} should be supported");
    }
}

#[test]
fn unsupported_languages() {
    let unsupported = ["haskell", "erlang", "fortran", "cobol", "lua", ""];
    for lang in &unsupported {
        assert!(!is_halstead_lang(lang), "{lang} should not be supported");
    }
}

// ═══════════════════════════════════════════════════════════════════
// § operators_for_lang returns empty for unsupported
// ═══════════════════════════════════════════════════════════════════

#[test]
fn operators_for_unsupported_lang_empty() {
    assert!(operators_for_lang("haskell").is_empty());
    assert!(operators_for_lang("").is_empty());
    assert!(operators_for_lang("brainfuck").is_empty());
}

// ═══════════════════════════════════════════════════════════════════
// § HalsteadMetrics serde round-trip
// ═══════════════════════════════════════════════════════════════════

#[test]
fn halstead_metrics_serde_round_trip() {
    let m = build_report_for_code("let x = 1\nlet y = x + 2", "Rust", "test.rs");
    let json = serde_json::to_string(&m).unwrap();
    let deserialized: HalsteadMetrics = serde_json::from_str(&json).unwrap();

    assert_eq!(m.distinct_operators, deserialized.distinct_operators);
    assert_eq!(m.distinct_operands, deserialized.distinct_operands);
    assert_eq!(m.total_operators, deserialized.total_operators);
    assert_eq!(m.total_operands, deserialized.total_operands);
    assert_eq!(m.vocabulary, deserialized.vocabulary);
    assert_eq!(m.length, deserialized.length);
    assert!((m.volume - deserialized.volume).abs() < 0.01);
    assert!((m.difficulty - deserialized.difficulty).abs() < 0.01);
    assert!((m.effort - deserialized.effort).abs() < 0.01);
    assert!((m.time_seconds - deserialized.time_seconds).abs() < 0.01);
    assert!((m.estimated_bugs - deserialized.estimated_bugs).abs() < 0.0001);
}

// ═══════════════════════════════════════════════════════════════════
// § HalsteadMetrics JSON shape
// ═══════════════════════════════════════════════════════════════════

#[test]
fn halstead_metrics_json_shape() {
    let m = build_report_for_code("let x = 1;", "Rust", "test.rs");
    let v: serde_json::Value = serde_json::to_value(m).unwrap();

    assert!(v.is_object());
    let expected_keys = [
        "distinct_operators",
        "distinct_operands",
        "total_operators",
        "total_operands",
        "vocabulary",
        "length",
        "volume",
        "difficulty",
        "effort",
        "time_seconds",
        "estimated_bugs",
    ];
    for key in &expected_keys {
        assert!(v.get(key).is_some(), "JSON should have key '{key}'");
    }
}

// ═══════════════════════════════════════════════════════════════════
// § time_seconds and estimated_bugs formulas
// ═══════════════════════════════════════════════════════════════════

#[test]
fn time_and_bugs_formulas() {
    let m = build_report_for_code(
        "fn compute(a: i32, b: i32) -> i32 { let x = a + b; let y = a * b; x - y }",
        "Rust",
        "f.rs",
    );

    // time = effort / 18
    let expected_time = round_f64(m.effort / 18.0, 2);
    assert!(
        (m.time_seconds - expected_time).abs() < 0.01,
        "time_seconds ({}) should be effort/18 ({})",
        m.time_seconds,
        expected_time
    );

    // bugs = volume / 3000
    let expected_bugs = round_f64(m.volume / 3000.0, 4);
    assert!(
        (m.estimated_bugs - expected_bugs).abs() < 0.001,
        "estimated_bugs ({}) should be volume/3000 ({})",
        m.estimated_bugs,
        expected_bugs
    );
}

// ═══════════════════════════════════════════════════════════════════
// § Comment-only file produces all-zero metrics
// ═══════════════════════════════════════════════════════════════════

#[test]
fn comment_only_file_all_zeros() {
    let code = "// this is a comment\n// another comment\n// yet another";
    let m = build_report_for_code(code, "Rust", "comments.rs");

    assert_eq!(m.distinct_operators, 0);
    assert_eq!(m.distinct_operands, 0);
    assert_eq!(m.total_operators, 0);
    assert_eq!(m.total_operands, 0);
    assert_eq!(m.volume, 0.0);
    assert_eq!(m.difficulty, 0.0);
    assert_eq!(m.effort, 0.0);
}

// ═══════════════════════════════════════════════════════════════════
// § Multi-file aggregation
// ═══════════════════════════════════════════════════════════════════

#[test]
fn multi_file_aggregation_merges_counts() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("a.rs"), "let x = 1;").unwrap();
    std::fs::write(dir.path().join("b.rs"), "let y = 2;").unwrap();

    let export = make_export(vec![make_row("a.rs", "Rust"), make_row("b.rs", "Rust")]);
    let files = vec![PathBuf::from("a.rs"), PathBuf::from("b.rs")];
    let m = build_halstead_report(dir.path(), &files, &export, &no_limits()).unwrap();

    // Each file: let(1) =(1) → 2 operators, operand(2)
    // Merged: let(2) =(2) → total_operators=4, operands: {x,1,y,2}
    assert_eq!(m.total_operators, 4);
    assert_eq!(m.distinct_operators, 2); // let, =
    assert_eq!(m.distinct_operands, 4); // x, 1, y, 2
}

// ═══════════════════════════════════════════════════════════════════
// § Unsupported language files are skipped
// ═══════════════════════════════════════════════════════════════════

#[test]
fn unsupported_language_files_skipped() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(
        dir.path().join("main.hs"),
        "module Main where\nmain = putStrLn \"hello\"",
    )
    .unwrap();

    let export = make_export(vec![make_row("main.hs", "Haskell")]);
    let files = vec![PathBuf::from("main.hs")];
    let m = build_halstead_report(dir.path(), &files, &export, &no_limits()).unwrap();

    assert_eq!(m.distinct_operators, 0);
    assert_eq!(m.distinct_operands, 0);
    assert_eq!(m.volume, 0.0);
}

// ═══════════════════════════════════════════════════════════════════
// § FileTokenCounts consistency
// ═══════════════════════════════════════════════════════════════════

#[test]
fn file_token_counts_consistency() {
    let counts = tokenize_for_halstead("let a = 1\nlet b = a + 2", "rust");

    // total_operators should equal sum of all operator counts
    let sum_ops: usize = counts.operators.values().sum();
    assert_eq!(
        counts.total_operators, sum_ops,
        "total_operators should equal sum of individual operator counts"
    );

    // distinct_operators = operators.len()
    assert_eq!(counts.operators.len(), 3); // let, =, +

    // total_operands >= operands.len() (distinct)
    assert!(
        counts.total_operands >= counts.operands.len(),
        "total_operands ({}) should be >= distinct operands ({})",
        counts.total_operands,
        counts.operands.len()
    );
}

// ═══════════════════════════════════════════════════════════════════
// § CRLF line endings handled
// ═══════════════════════════════════════════════════════════════════

#[test]
fn crlf_line_endings_handled() {
    let code = "let x = 1;\r\nlet y = 2;\r\n";
    let counts = tokenize_for_halstead(code, "rust");
    assert_eq!(counts.total_operators, 4); // let, =, let, =
    assert!(counts.operands.contains("x"));
    assert!(counts.operands.contains("y"));
}

// ═══════════════════════════════════════════════════════════════════
// § Logical operators counted correctly
// ═══════════════════════════════════════════════════════════════════

#[test]
fn logical_operators_counted() {
    let code = "if a && b || c { return true; }";
    let counts = tokenize_for_halstead(code, "rust");
    assert!(counts.operators.contains_key("&&"));
    assert!(counts.operators.contains_key("||"));
    assert_eq!(*counts.operators.get("&&").unwrap(), 1);
    assert_eq!(*counts.operators.get("||").unwrap(), 1);
}

// ═══════════════════════════════════════════════════════════════════
// § Many distinct operands increase vocabulary
// ═══════════════════════════════════════════════════════════════════

#[test]
fn many_distinct_operands_increase_vocabulary() {
    let code =
        "let a = 1\nlet b = 2\nlet c = 3\nlet d = 4\nlet e = 5\nlet f = 6\nlet g = 7\nlet h = 8";
    let m = build_report_for_code(code, "Rust", "many.rs");
    // 2 distinct operators (let, =), 16 distinct operands (a-h, 1-8)
    assert_eq!(m.distinct_operators, 2);
    assert_eq!(m.distinct_operands, 16);
    assert_eq!(m.vocabulary, 18);
}

// ═══════════════════════════════════════════════════════════════════
// § HalsteadMetrics deserialization from known JSON
// ═══════════════════════════════════════════════════════════════════

#[test]
fn halstead_metrics_deserializes_from_known_json() {
    let json = r#"{
        "distinct_operators": 3,
        "distinct_operands": 4,
        "total_operators": 5,
        "total_operands": 5,
        "vocabulary": 7,
        "length": 10,
        "volume": 28.07,
        "difficulty": 1.88,
        "effort": 52.77,
        "time_seconds": 2.93,
        "estimated_bugs": 0.0094
    }"#;
    let m: HalsteadMetrics = serde_json::from_str(json).unwrap();
    assert_eq!(m.distinct_operators, 3);
    assert_eq!(m.distinct_operands, 4);
    assert_eq!(m.vocabulary, 7);
    assert_eq!(m.length, 10);
    assert!((m.volume - 28.07).abs() < 0.01);
}

// ═══════════════════════════════════════════════════════════════════
// § Python hash comment lines are skipped
// ═══════════════════════════════════════════════════════════════════

#[test]
fn python_hash_comments_skipped() {
    let code = "# this is a comment\nx = 1\n# another comment\ny = 2";
    let counts = tokenize_for_halstead(code, "python");
    // Only lines 2 and 4 are parsed
    assert_eq!(counts.total_operators, 2); // =, =
    assert_eq!(counts.total_operands, 4); // x, 1, y, 2
}

// ═══════════════════════════════════════════════════════════════════
// § Difficulty zero when no operands
// ═══════════════════════════════════════════════════════════════════

#[test]
fn difficulty_zero_when_no_operands() {
    let m = build_report_for_code("return", "Rust", "ret.rs");
    assert_eq!(m.difficulty, 0.0);
}