pmat 3.16.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
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Tests for DependencyGraphBuilder
// Extracted from builder.rs

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_source_file() {
        assert!(DependencyGraphBuilder::is_source_file(Path::new("test.rs")));
        assert!(DependencyGraphBuilder::is_source_file(Path::new("test.py")));
        assert!(DependencyGraphBuilder::is_source_file(Path::new("test.ts")));
        assert!(!DependencyGraphBuilder::is_source_file(Path::new(
            "test.txt"
        )));
        assert!(!DependencyGraphBuilder::is_source_file(Path::new(
            "README.md"
        )));
    }

    #[test]
    fn test_extract_function_names() {
        assert_eq!(
            DependencyGraphBuilder::extract_function_name("pub fn test_func() {"),
            Some("test_func")
        );
        assert_eq!(
            DependencyGraphBuilder::extract_function_name("fn private_func(arg: i32) {"),
            Some("private_func")
        );
    }

    /// extract_type_name strips `pub`/keyword prefix, trailing generics `<...>`
    /// and braces `{...}`.
    #[test]
    fn test_extract_type_name_covers_generics_and_body() {
        assert_eq!(
            DependencyGraphBuilder::extract_type_name("pub struct Foo {", "struct"),
            Some("Foo")
        );
        assert_eq!(
            DependencyGraphBuilder::extract_type_name("struct Bar<T> { x: T }", "struct"),
            Some("Bar")
        );
        assert_eq!(
            DependencyGraphBuilder::extract_type_name("enum Status", "enum"),
            Some("Status")
        );
        assert_eq!(
            DependencyGraphBuilder::extract_type_name("   ", "struct"),
            None
        );
    }

    /// extract_ts_name strips `export`/`const`/`function`, `(...)` args, and `= ...`.
    #[test]
    fn test_extract_ts_name_covers_all_prefixes() {
        assert_eq!(
            DependencyGraphBuilder::extract_ts_name("export function foo() {}"),
            Some("foo")
        );
        assert_eq!(
            DependencyGraphBuilder::extract_ts_name("function bar(arg: number) {}"),
            Some("bar")
        );
        assert_eq!(
            DependencyGraphBuilder::extract_ts_name("export const baz = 42"),
            Some("baz")
        );
        assert_eq!(
            DependencyGraphBuilder::extract_ts_name("const qux = () => 1"),
            Some("qux")
        );
        assert_eq!(DependencyGraphBuilder::extract_ts_name(""), None);
    }

    /// extract_ts_class_name strips `export`/`class` and trailing `{...}`.
    #[test]
    fn test_extract_ts_class_name_covers_branches() {
        assert_eq!(
            DependencyGraphBuilder::extract_ts_class_name("export class PubClass {}"),
            Some("PubClass")
        );
        assert_eq!(
            DependencyGraphBuilder::extract_ts_class_name("class Bare"),
            Some("Bare")
        );
        assert_eq!(
            DependencyGraphBuilder::extract_ts_class_name("class Foo{body}"),
            Some("Foo")
        );
        assert_eq!(DependencyGraphBuilder::extract_ts_class_name(""), None);
    }

    #[test]
    fn test_extract_python_names() {
        assert_eq!(
            DependencyGraphBuilder::extract_python_function_name("def test_func():"),
            Some("test_func")
        );
        assert_eq!(
            DependencyGraphBuilder::extract_python_class_name("class TestClass(BaseClass):"),
            Some("TestClass")
        );
    }

    #[test]
    fn test_builder_creation() {
        let builder = DependencyGraphBuilder::new();
        assert!(builder.symbol_table.is_empty());
        assert_eq!(builder.graph.node_count(), 0);
        assert_eq!(builder.graph.edge_count(), 0);
    }

    /// Test that analyze_file cache hit returns correct node_id
    /// Validates unwrap at line 159-162
    #[test]
    fn test_analyze_file_cache_hit() {
        use std::fs;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        fs::write(&test_file, "fn main() {}\n").unwrap();

        let mut builder = DependencyGraphBuilder::new();

        // First analysis - should create new node
        let node_id_1 = builder.analyze_file(&test_file).unwrap();
        assert_eq!(builder.graph.node_count(), 1);
        assert!(builder.node_map.contains_key(&test_file));
        assert!(builder.processed_hashes.contains_key(&test_file));

        // Second analysis with same content - should return cached node_id
        let node_id_2 = builder.analyze_file(&test_file).unwrap();
        assert_eq!(node_id_1, node_id_2, "Cache hit should return same node_id");
        assert_eq!(
            builder.graph.node_count(),
            1,
            "Should not create duplicate node"
        );
    }

    /// Test that analyze_file updates node when content changes
    /// Validates unwrap at line 184-187
    #[test]
    fn test_analyze_file_content_change() {
        use std::fs;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        fs::write(&test_file, "fn main() {}\n").unwrap();

        let mut builder = DependencyGraphBuilder::new();

        // First analysis
        let node_id_1 = builder.analyze_file(&test_file).unwrap();
        let original_loc = builder.graph.node_weight(node_id_1).unwrap().loc;

        // Modify file (different content = different hash)
        fs::write(&test_file, "fn main() {}\nfn helper() {}\n").unwrap();

        // Second analysis - should update existing node
        let node_id_2 = builder.analyze_file(&test_file).unwrap();
        assert_eq!(
            node_id_1, node_id_2,
            "Should reuse same node_id for same path"
        );
        assert_eq!(
            builder.graph.node_count(),
            1,
            "Should still have only 1 node"
        );

        // Verify node was updated (LOC should increase)
        let updated_loc = builder.graph.node_weight(node_id_2).unwrap().loc;
        assert!(
            updated_loc > original_loc,
            "Node should be updated with new LOC"
        );
    }

    /// Test that node_map and processed_hashes stay synchronized
    /// Validates invariant that both maps are updated together (lines 191-195)
    #[test]
    fn test_node_map_hash_map_synchronization() {
        use std::fs;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        fs::write(&test_file, "fn test() {}\n").unwrap();

        let mut builder = DependencyGraphBuilder::new();

        // Analyze file
        builder.analyze_file(&test_file).unwrap();

        // Both maps should contain the file
        assert!(
            builder.node_map.contains_key(&test_file),
            "node_map should contain analyzed file"
        );
        assert!(
            builder.processed_hashes.contains_key(&test_file),
            "processed_hashes should contain analyzed file"
        );

        // Both maps should have same size
        assert_eq!(
            builder.node_map.len(),
            builder.processed_hashes.len(),
            "node_map and processed_hashes must stay synchronized"
        );
    }

    /// Parse Rust: `pub fn` (public), `fn` (private), `pub struct`, and skipped lines.
    #[test]
    fn test_parse_rust_symbols_covers_all_branches() {
        let builder = DependencyGraphBuilder::new();
        let content = "\
pub fn public_fn() {}
fn private_fn(x: i32) {}
pub struct MyStruct {
    field: u32,
}
use std::collections::HashMap;
// comment
";
        let symbols = builder.parse_rust_symbols(content).unwrap();
        assert_eq!(symbols.len(), 3);

        assert_eq!(symbols[0].name, "public_fn");
        assert_eq!(symbols[0].kind, SymbolKind::Function);
        assert_eq!(symbols[0].visibility, Visibility::Public);
        assert_eq!(symbols[0].line, 0);

        assert_eq!(symbols[1].name, "private_fn");
        assert_eq!(symbols[1].kind, SymbolKind::Function);
        assert_eq!(symbols[1].visibility, Visibility::Private);
        assert_eq!(symbols[1].line, 1);

        assert_eq!(symbols[2].name, "MyStruct");
        assert_eq!(symbols[2].kind, SymbolKind::Struct);
        assert_eq!(symbols[2].visibility, Visibility::Public);
        assert_eq!(symbols[2].line, 2);
    }

    #[test]
    fn test_parse_rust_symbols_empty_content() {
        let builder = DependencyGraphBuilder::new();
        assert!(builder.parse_rust_symbols("").unwrap().is_empty());
    }

    /// builder_symbol_parsing.rs:47/56/65 — the three `if let Some(name) = ...`
    /// None arms in parse_rust_symbols. Lines starting with `pub fn `/`fn `/
    /// `pub struct ` where only whitespace follows make extract_function_name
    /// or extract_type_name return None, so the symbol is NOT pushed and the
    /// branch falls through. Existing tests only exercise the Some path.
    #[test]
    fn test_parse_rust_symbols_extract_none_fallthrough() {
        let builder = DependencyGraphBuilder::new();
        // Each line trips its starts_with guard but extract_* returns None,
        // so no symbols are produced.
        let content = "pub fn \nfn \npub struct \n";
        let symbols = builder.parse_rust_symbols(content).unwrap();
        assert!(
            symbols.is_empty(),
            "keyword-only lines must hit the None arms of extract_function_name / \
             extract_type_name and produce no symbols, got: {symbols:?}"
        );
    }

    /// Parse Python: `def` (public), `def _` (private), `class`, and skipped lines.
    #[test]
    fn test_parse_python_symbols_covers_all_branches() {
        let builder = DependencyGraphBuilder::new();
        let content = "\
def public_func():
    pass
def _private_func():
    pass
class MyClass(Base):
    pass
# a comment
import os
";
        let symbols = builder.parse_python_symbols(content).unwrap();
        assert_eq!(symbols.len(), 3);

        assert_eq!(symbols[0].name, "public_func");
        assert_eq!(symbols[0].kind, SymbolKind::Function);
        assert_eq!(symbols[0].visibility, Visibility::Public);
        assert_eq!(symbols[0].line, 0);

        assert_eq!(symbols[1].name, "_private_func");
        assert_eq!(symbols[1].visibility, Visibility::Private);
        assert_eq!(symbols[1].line, 2);

        assert_eq!(symbols[2].name, "MyClass");
        assert_eq!(symbols[2].kind, SymbolKind::Struct);
        assert_eq!(symbols[2].visibility, Visibility::Public);
        assert_eq!(symbols[2].line, 4);
    }

    #[test]
    fn test_parse_python_symbols_empty_content() {
        let builder = DependencyGraphBuilder::new();
        assert!(builder.parse_python_symbols("").unwrap().is_empty());
    }

    /// Parse TS/JS: `export function`, `export const`, `function`, `const`, `export class`.
    #[test]
    fn test_parse_typescript_symbols_covers_all_branches() {
        let builder = DependencyGraphBuilder::new();
        let content = "\
export function pubFn() {}
export const pubConst = 1;
function privFn() {}
const privConst = 2;
export class PubClass {}
// skip
let other = 5;
";
        let symbols = builder.parse_typescript_symbols(content).unwrap();
        assert_eq!(symbols.len(), 5);

        assert_eq!(symbols[0].name, "pubFn");
        assert_eq!(symbols[0].kind, SymbolKind::Function);
        assert_eq!(symbols[0].visibility, Visibility::Public);

        assert_eq!(symbols[1].name, "pubConst");
        assert_eq!(symbols[1].visibility, Visibility::Public);

        assert_eq!(symbols[2].name, "privFn");
        assert_eq!(symbols[2].visibility, Visibility::Private);

        assert_eq!(symbols[3].name, "privConst");
        assert_eq!(symbols[3].visibility, Visibility::Private);

        assert_eq!(symbols[4].name, "PubClass");
        assert_eq!(symbols[4].kind, SymbolKind::Struct);
        assert_eq!(symbols[4].visibility, Visibility::Public);
    }

    #[test]
    fn test_parse_typescript_symbols_empty_content() {
        let builder = DependencyGraphBuilder::new();
        assert!(builder.parse_typescript_symbols("").unwrap().is_empty());
    }

    /// Parse Rust: `use x;` (picked), `use y` (no semicolon, skipped),
    /// `fn` (skipped). Covers strip_prefix/strip_suffix path.
    #[test]
    fn test_parse_rust_imports_covers_branches() {
        let builder = DependencyGraphBuilder::new();
        let content = "\
use std::path::Path;
use crate::graph::Node;
  use indented::Import;
use missing_semicolon
fn main() {}
// comment
";
        let imports = builder.parse_rust_imports(content).unwrap();
        assert_eq!(imports.len(), 3);
        assert_eq!(imports[0], "std::path::Path");
        assert_eq!(imports[1], "crate::graph::Node");
        assert_eq!(imports[2], "indented::Import");
    }

    #[test]
    fn test_parse_rust_imports_empty_content() {
        let builder = DependencyGraphBuilder::new();
        assert!(builder.parse_rust_imports("").unwrap().is_empty());
    }

    /// Parse Python: `import` and `from` both picked;
    /// other lines (def, comment, code) skipped.
    #[test]
    fn test_parse_python_imports_covers_branches() {
        let builder = DependencyGraphBuilder::new();
        let content = "\
import os
from pathlib import Path
  from indented import Foo
def main():
    pass
# a comment
x = 1
";
        let imports = builder.parse_python_imports(content).unwrap();
        assert_eq!(imports.len(), 3);
        assert_eq!(imports[0], "import os");
        assert_eq!(imports[1], "from pathlib import Path");
        assert_eq!(imports[2], "from indented import Foo");
    }

    #[test]
    fn test_parse_python_imports_empty_content() {
        let builder = DependencyGraphBuilder::new();
        assert!(builder.parse_python_imports("").unwrap().is_empty());
    }

    /// Parse TS/JS: `import ... from '...'` (picked, quote-stripped),
    /// `import` with no `from` (skipped), `const X = require('...')` (picked),
    /// `const` without require (skipped), other lines skipped.
    #[test]
    fn test_parse_typescript_imports_covers_branches() {
        let builder = DependencyGraphBuilder::new();
        let content = "\
import { x } from 'lodash';
import \"side-effect-only\";
const fs = require('fs');
const unused = 42;
let other = 5;
// comment
";
        let imports = builder.parse_typescript_imports(content).unwrap();
        assert_eq!(imports.len(), 2);
        assert_eq!(imports[0], "lodash");
        assert_eq!(imports[1], "fs");
    }

    #[test]
    fn test_parse_typescript_imports_empty_content() {
        let builder = DependencyGraphBuilder::new();
        assert!(builder.parse_typescript_imports("").unwrap().is_empty());
    }

    /// Test first-time file analysis creates both node and hash entry
    /// Validates initialization path (lines 189-195)
    #[test]
    fn test_first_time_analysis() {
        use std::fs;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("fresh.rs");
        fs::write(&test_file, "pub fn new_function() {}\n").unwrap();

        let mut builder = DependencyGraphBuilder::new();

        // Fresh builder should have empty maps
        assert_eq!(builder.node_map.len(), 0);
        assert_eq!(builder.processed_hashes.len(), 0);

        // First analysis
        let node_id = builder.analyze_file(&test_file).unwrap();

        // Should create entries in both maps
        assert_eq!(builder.node_map.len(), 1);
        assert_eq!(builder.processed_hashes.len(), 1);
        assert!(builder.node_map.contains_key(&test_file));
        assert!(builder.processed_hashes.contains_key(&test_file));

        // Should create node in graph
        assert_eq!(builder.graph.node_count(), 1);
        assert!(builder.graph.node_weight(node_id).is_some());
    }

    /// builder_file_collection.rs:16-17 — `if !dir.is_dir() { return Ok(()) }`.
    /// Reachable by passing a file path as the root to from_workspace, which
    /// forwards to collect_source_files -> collect_files_recursive. The
    /// non-directory early-return must leave the collected files vec empty.
    #[test]
    fn test_from_workspace_non_directory_path_is_noop() {
        use std::fs;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let file_as_root = temp_dir.path().join("lone_file.rs");
        fs::write(&file_as_root, "pub fn solo() {}\n").unwrap();

        // Pass the file (not its parent) as the workspace root.
        let builder = DependencyGraphBuilder::from_workspace(&file_as_root)
            .expect("non-directory root must not error — must early-return Ok");
        let graph = builder.build().expect("build must succeed on empty graph");
        assert_eq!(
            graph.node_count(),
            0,
            "non-directory root must produce zero nodes (nothing collected)"
        );
    }

    /// builder_file_collection.rs:26-30 — skip arm for dirs starting with '.'
    /// or named `target`/`node_modules`, plus the recursive call for normal
    /// subdirs. Build a tree with a hidden dir, a `target` dir, a
    /// `node_modules` dir (each containing a .rs file that MUST be skipped),
    /// and a plain subdir (containing a .rs file that MUST be picked up).
    #[test]
    fn test_collect_files_recursive_skips_hidden_target_and_node_modules() {
        use std::fs;
        use tempfile::TempDir;

        let root = TempDir::new().unwrap();

        // Plain subdirectory — file here MUST be picked up via recursion.
        let plain = root.path().join("subdir");
        fs::create_dir(&plain).unwrap();
        fs::write(plain.join("good.rs"), "pub fn good() {}\n").unwrap();

        // Hidden subdirectory — must be skipped.
        let hidden = root.path().join(".hidden");
        fs::create_dir(&hidden).unwrap();
        fs::write(hidden.join("skip.rs"), "pub fn hidden_skip() {}\n").unwrap();

        // target/ — must be skipped.
        let target = root.path().join("target");
        fs::create_dir(&target).unwrap();
        fs::write(target.join("skip.rs"), "pub fn target_skip() {}\n").unwrap();

        // node_modules/ — must be skipped.
        let nm = root.path().join("node_modules");
        fs::create_dir(&nm).unwrap();
        fs::write(nm.join("skip.rs"), "pub fn nm_skip() {}\n").unwrap();

        // Also a .rs file at the root level — picked up directly.
        fs::write(root.path().join("top.rs"), "pub fn at_root() {}\n").unwrap();

        let builder = DependencyGraphBuilder::from_workspace(root.path())
            .expect("from_workspace must succeed");
        let graph = builder.build().unwrap();

        // Exactly two files should have been collected: top.rs + subdir/good.rs.
        // The three skip-dirs each contain a .rs that MUST NOT appear.
        assert_eq!(
            graph.node_count(),
            2,
            "expected 2 nodes (top.rs + subdir/good.rs); skipped dirs \
             (.hidden, target, node_modules) must not contribute"
        );
    }

    /// builder_symbol_parsing.rs:14 — `Some("py") => self.parse_python_symbols(...)`.
    /// Reachable by putting a .py file in the workspace root. is_source_file
    /// accepts "py", so from_workspace forwards to build_file_symbols which
    /// must dispatch to the Python parser. Verifies the Python arm populates
    /// the symbol table.
    #[test]
    fn test_build_file_symbols_python_extension_hits_python_parser() {
        use std::fs;
        use tempfile::TempDir;

        let root = TempDir::new().unwrap();
        fs::write(
            root.path().join("script.py"),
            "def my_public_fn():\n    pass\n",
        )
        .unwrap();

        let builder = DependencyGraphBuilder::from_workspace(root.path())
            .expect("from_workspace with .py file must succeed");
        assert!(
            !builder.symbol_table().is_empty(),
            "Python arm must parse `def my_public_fn` into the symbol table \
             — empty table means the Some(\"py\") arm never ran"
        );
    }

    /// builder_symbol_parsing.rs:15-17 — TypeScript/JavaScript arm
    /// `Some("ts") | Some("tsx") | Some("js") | Some("jsx")`. Reachable via a
    /// .ts file in the workspace; from_workspace -> build_file_symbols must
    /// dispatch to parse_typescript_symbols.
    #[test]
    fn test_build_file_symbols_typescript_extension_hits_typescript_parser() {
        use std::fs;
        use tempfile::TempDir;

        let root = TempDir::new().unwrap();
        fs::write(
            root.path().join("module.ts"),
            "export function exported_fn() {}\n",
        )
        .unwrap();

        let builder = DependencyGraphBuilder::from_workspace(root.path())
            .expect("from_workspace with .ts file must succeed");
        assert!(
            !builder.symbol_table().is_empty(),
            "TypeScript arm must parse `export function exported_fn` into \
             the symbol table — empty table means the .ts arm never ran"
        );
    }

    /// builder_symbol_parsing.rs:18 — `_ => vec![]` unsupported-extension
    /// fallback. is_source_file accepts "go"/"java"/"c"/"cpp"/"h"/"hpp" but
    /// build_file_symbols has no arm for them, so they fall through to the
    /// empty-Vec default. No symbols should be produced for a .go-only workspace.
    #[test]
    fn test_build_file_symbols_unsupported_extension_falls_through_to_empty() {
        use std::fs;
        use tempfile::TempDir;

        let root = TempDir::new().unwrap();
        fs::write(
            root.path().join("only.go"),
            "package main\nfunc Hello() {}\n",
        )
        .unwrap();

        let builder = DependencyGraphBuilder::from_workspace(root.path())
            .expect("from_workspace with .go file must succeed even when \
                 build_file_symbols has no parser arm for it");
        assert!(
            builder.symbol_table().is_empty(),
            "Go files must hit the `_ => vec![]` fallback arm — symbol \
             table should remain empty for a .go-only workspace"
        );
    }
}