pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
//! BUG-004: Dead Code Multi-Language Tests (RED Phase)
//!
//! These tests define expected behavior for dead code analysis
//! across multiple programming languages without requiring Cargo.toml.
//!
//! Current Status: 🔴 RED - These tests will FAIL until implementation complete
//!
//! Test Strategy (Extreme TDD):
//! 1. RED: Write failing tests that define expected behavior
//! 2. GREEN: Implement minimum code to make tests pass
//! 3. REFACTOR: Clean up implementation
//! 4. COMMIT: Single atomic commit with fix

use tempfile::TempDir;

// Import the actual implementation
use pmat::services::dead_code_multi_language::analyze_dead_code_multi_language;

// =============================================================================
// RED TEST 1: C Project Dead Code Analysis
// =============================================================================

#[test]
#[ignore = "BUG-004: RED test - will fail until multi-language dead code implemented"]
fn test_c_project_dead_code_without_cargo_toml() {
    // Arrange: Create C project (no Cargo.toml)
    let project = create_c_project_with_dead_code();

    // Act: Analyze dead code
    let result = analyze_dead_code_multi_language(project.path());

    // Assert: Should succeed and detect C language
    assert!(
        result.is_ok(),
        "Should succeed on C project without Cargo.toml"
    );
    let result = result.unwrap();

    assert_eq!(result.language, "c", "Should detect C language");
    assert_eq!(result.total_functions, 2, "Should find 2 functions total");
    assert_eq!(
        result.dead_functions.len(),
        1,
        "Should find 1 dead function"
    );

    let dead_fn = &result.dead_functions[0];
    assert_eq!(dead_fn.name, "unused_function");
    assert!(dead_fn.file.contains("utils.c"));
}

#[test]
#[ignore = "BUG-004: RED test - will fail until C++ support implemented"]
fn test_cpp_project_dead_code_with_cmake() {
    // Arrange: Create C++ project with CMakeLists.txt
    let project = create_cpp_project_with_dead_code();

    // Act: Analyze dead code
    let result = analyze_dead_code_multi_language(project.path());

    // Assert: Should detect C++ and find dead code
    assert!(result.is_ok());
    let result = result.unwrap();

    assert_eq!(result.language, "cpp", "Should detect C++ language");
    assert!(
        !result.dead_functions.is_empty(),
        "Should find dead C++ functions"
    );
}

// =============================================================================
// RED TEST 2: Python Project Dead Code Analysis
// =============================================================================

#[test]
#[ignore = "BUG-004: RED test - will fail until Python support implemented"]
fn test_python_project_dead_code_without_cargo_toml() {
    // Arrange: Create Python project (no Cargo.toml)
    let project = create_python_project_with_dead_code();

    // Act: Analyze dead code
    let result = analyze_dead_code_multi_language(project.path());

    // Assert: Should succeed and detect Python language
    assert!(
        result.is_ok(),
        "Should succeed on Python project without Cargo.toml"
    );
    let result = result.unwrap();

    assert_eq!(result.language, "python", "Should detect Python language");
    assert!(
        result.total_functions >= 2,
        "Should find at least 2 functions"
    );
    assert!(
        !result.dead_functions.is_empty(),
        "Should find at least 1 dead function"
    );

    // Check that unused_function is detected as dead
    assert!(
        result
            .dead_functions
            .iter()
            .any(|f| f.name.contains("unused")),
        "Should detect unused_function as dead code"
    );
}

// =============================================================================
// RED TEST 3: Rust Project (Should Still Work)
// =============================================================================

#[test]
#[ignore = "BUG-004: RED test - ensure Rust still works after refactor"]
fn test_rust_project_dead_code_still_works() {
    // Arrange: Create Rust project with Cargo.toml
    let project = create_rust_project_with_dead_code();

    // Act: Analyze dead code
    let result = analyze_dead_code_multi_language(project.path());

    // Assert: Should still work for Rust projects
    assert!(result.is_ok(), "Should still work for Rust projects");
    let result = result.unwrap();

    assert_eq!(result.language, "rust", "Should detect Rust language");
    assert!(
        !result.dead_functions.is_empty(),
        "Should find dead Rust functions"
    );
}

// =============================================================================
// RED TEST 4: Unsupported Language Error
// =============================================================================

#[test]
#[ignore = "BUG-004: RED test - will fail until error handling implemented"]
fn test_unsupported_language_returns_error() {
    // Arrange: Create project with unsupported language (e.g., Fortran)
    let project = create_fortran_project();

    // Act: Analyze dead code
    let result = analyze_dead_code_multi_language(project.path());

    // Assert: Should return error with helpful message
    assert!(
        result.is_err(),
        "Should return error for unsupported language"
    );
    let error = result.unwrap_err().to_string();
    assert!(
        error.contains("not supported") || error.contains("unsupported"),
        "Error should mention unsupported language: {}",
        error
    );
}

// =============================================================================
// RED TEST 5: Language Detection Integration
// =============================================================================

#[test]
#[ignore = "BUG-004: RED test - integration with enhanced_language_detection"]
fn test_uses_enhanced_language_detection() {
    // Arrange: Create polyglot project (C++ primary, Python secondary)
    let project = create_polyglot_project();

    // Act: Analyze dead code
    let result = analyze_dead_code_multi_language(project.path());

    // Assert: Should use enhanced language detection from BUG-011 fix
    assert!(result.is_ok());
    let result = result.unwrap();

    // Should detect C++ as primary (has CMakeLists.txt)
    assert_eq!(
        result.language, "cpp",
        "Should use enhanced_language_detection to identify C++ as primary"
    );
}

// =============================================================================
// RED TEST 6: Dead Code Percentage Calculation
// =============================================================================

#[test]
#[ignore = "BUG-004: RED test - percentage calculation"]
fn test_dead_code_percentage_calculation() {
    // Arrange: Project with known dead code ratio (1 dead out of 3 functions)
    let project = create_project_with_known_ratio();

    // Act: Analyze dead code
    let result = analyze_dead_code_multi_language(project.path()).unwrap();

    // Assert: Should calculate percentage correctly
    assert_eq!(result.total_functions, 3, "Should find 3 total functions");
    assert_eq!(
        result.dead_functions.len(),
        1,
        "Should find 1 dead function"
    );

    let expected_percentage = (1.0 / 3.0) * 100.0;
    assert!(
        (result.dead_code_percentage - expected_percentage).abs() < 0.1,
        "Dead code percentage should be ~33.3%, got {}",
        result.dead_code_percentage
    );
}

// =============================================================================
// Mock Project Creators
// =============================================================================

fn create_c_project_with_dead_code() -> TempDir {
    use std::fs;

    let temp = TempDir::new().unwrap();
    let base = temp.path();

    fs::create_dir_all(base.join("src")).unwrap();
    fs::create_dir_all(base.join("include")).unwrap();

    // main.c
    fs::write(
        base.join("src/main.c"),
        r#"
#include <stdio.h>
#include "utils.h"

int main() {
    used_function();
    return 0;
}
"#,
    )
    .unwrap();

    // utils.c with dead code
    fs::write(
        base.join("src/utils.c"),
        r#"
#include <stdio.h>
#include "utils.h"

void used_function() {
    printf("Used\n");
}

void unused_function() {
    printf("DEAD CODE!\n");
}
"#,
    )
    .unwrap();

    // utils.h
    fs::write(
        base.join("include/utils.h"),
        r#"
#ifndef UTILS_H
#define UTILS_H

void used_function();
void unused_function();

#endif
"#,
    )
    .unwrap();

    // Makefile (C projects often use Make instead of CMake)
    fs::write(base.join("Makefile"), "CC=gcc\nCFLAGS=-Wall\n\nall: main\n").unwrap();

    temp
}

fn create_cpp_project_with_dead_code() -> TempDir {
    use std::fs;

    let temp = TempDir::new().unwrap();
    let base = temp.path();

    fs::create_dir_all(base.join("src")).unwrap();

    // main.cpp
    fs::write(
        base.join("src/main.cpp"),
        r#"
#include <iostream>

void used_function() {
    std::cout << "Used" << std::endl;
}

void unused_function() {
    std::cout << "DEAD CODE!" << std::endl;
}

int main() {
    used_function();
    return 0;
}
"#,
    )
    .unwrap();

    // CMakeLists.txt
    fs::write(
        base.join("CMakeLists.txt"),
        "cmake_minimum_required(VERSION 3.10)\nproject(Test CXX)\n",
    )
    .unwrap();

    temp
}

fn create_python_project_with_dead_code() -> TempDir {
    use std::fs;

    let temp = TempDir::new().unwrap();
    let base = temp.path();

    // main.py
    fs::write(
        base.join("main.py"),
        r#"
from utils import used_function

def main():
    used_function()

if __name__ == "__main__":
    main()
"#,
    )
    .unwrap();

    // utils.py with dead code
    fs::write(
        base.join("utils.py"),
        r#"
def used_function():
    print("Used")

def unused_function():
    print("DEAD CODE!")
"#,
    )
    .unwrap();

    // pyproject.toml
    fs::write(base.join("pyproject.toml"), "[project]\nname = \"test\"\n").unwrap();

    temp
}

fn create_rust_project_with_dead_code() -> TempDir {
    use std::fs;

    let temp = TempDir::new().unwrap();
    let base = temp.path();

    fs::create_dir_all(base.join("src")).unwrap();

    // main.rs
    fs::write(
        base.join("src/main.rs"),
        r#"
fn used_function() {
    println!("Used");
}

fn unused_function() {
    println!("DEAD CODE!");
}

fn main() {
    used_function();
}
"#,
    )
    .unwrap();

    // Cargo.toml
    fs::write(
        base.join("Cargo.toml"),
        "[package]\nname = \"test\"\nversion = \"0.1.0\"\n",
    )
    .unwrap();

    temp
}

fn create_fortran_project() -> TempDir {
    use std::fs;

    let temp = TempDir::new().unwrap();
    let base = temp.path();

    // main.f90
    fs::write(
        base.join("main.f90"),
        r#"
program main
    print *, "Hello"
end program main
"#,
    )
    .unwrap();

    temp
}

fn create_polyglot_project() -> TempDir {
    use std::fs;

    let temp = TempDir::new().unwrap();
    let base = temp.path();

    fs::create_dir_all(base.join("src")).unwrap();

    // C++ files (primary)
    for i in 0..70 {
        fs::write(
            base.join(format!("src/file_{}.cpp", i)),
            "int main() { return 0; }",
        )
        .unwrap();
    }

    // Python files (secondary)
    for i in 0..30 {
        fs::write(base.join(format!("src/tool_{}.py", i)), "print('hello')").unwrap();
    }

    // CMakeLists.txt (indicates C++ primary)
    fs::write(
        base.join("CMakeLists.txt"),
        "cmake_minimum_required(VERSION 3.10)\nproject(Test CXX)\n",
    )
    .unwrap();

    temp
}

fn create_project_with_known_ratio() -> TempDir {
    use std::fs;

    let temp = TempDir::new().unwrap();
    let base = temp.path();

    fs::write(
        base.join("main.py"),
        r#"
def used1():
    print("Used 1")

def used2():
    print("Used 2")

def unused():
    print("DEAD CODE!")

def main():
    used1()
    used2()

if __name__ == "__main__":
    main()
"#,
    )
    .unwrap();

    fs::write(base.join("pyproject.toml"), "[project]\nname = \"test\"\n").unwrap();

    temp
}