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
    // Helper function to create a basic CommitInfo for testing
    fn make_commit_info(
        message: &str,
        timestamp: i64,
        files: Vec<&str>,
        branch: &str,
    ) -> CommitInfo {
        CommitInfo {
            message: message.to_string(),
            timestamp_seconds: timestamp,
            modified_files: files.into_iter().map(|s| s.to_string()).collect(),
            issue_number: None,
            issue_created_timestamp: None,
            branch: branch.to_string(),
            test_changes: TestChanges {
                added_tests: 0,
                fixed_tests: 0,
                modified_test_files: vec![],
            },
        }
    }

    // ===== IntentClassifier::new() tests =====

    #[test]
    fn test_intent_classifier_new_default_values() {
        let classifier = IntentClassifier::new();
        assert_eq!(classifier.grace_period_hours, 48);
        assert!((classifier.code_overlap_threshold - 0.8).abs() < f64::EPSILON);
        assert!(!classifier.hallucination_keywords.is_empty());
        assert!(!classifier.iteration_keywords.is_empty());
    }

    #[test]
    fn test_intent_classifier_default_trait() {
        let classifier = IntentClassifier::default();
        assert_eq!(classifier.grace_period_hours, 48);
    }

    #[test]
    fn test_hallucination_keywords_present() {
        let classifier = IntentClassifier::new();
        assert!(classifier
            .hallucination_keywords
            .contains(&"fix".to_string()));
        assert!(classifier
            .hallucination_keywords
            .contains(&"bug".to_string()));
        assert!(classifier
            .hallucination_keywords
            .contains(&"broken".to_string()));
        assert!(classifier
            .hallucination_keywords
            .contains(&"error".to_string()));
        assert!(classifier
            .hallucination_keywords
            .contains(&"regress".to_string()));
        assert!(classifier
            .hallucination_keywords
            .contains(&"fail".to_string()));
        assert!(classifier
            .hallucination_keywords
            .contains(&"incorrect".to_string()));
        assert!(classifier
            .hallucination_keywords
            .contains(&"wrong".to_string()));
    }

    #[test]
    fn test_iteration_keywords_present() {
        let classifier = IntentClassifier::new();
        assert!(classifier
            .iteration_keywords
            .contains(&"refactor".to_string()));
        assert!(classifier
            .iteration_keywords
            .contains(&"improve".to_string()));
        assert!(classifier
            .iteration_keywords
            .contains(&"enhance".to_string()));
        assert!(classifier
            .iteration_keywords
            .contains(&"optimize".to_string()));
        assert!(classifier
            .iteration_keywords
            .contains(&"cleanup".to_string()));
        assert!(classifier.iteration_keywords.contains(&"add".to_string()));
        assert!(classifier
            .iteration_keywords
            .contains(&"extend".to_string()));
    }

    // ===== analyze_commit_message tests =====

    #[test]
    fn test_commit_message_hallucination_keywords() {
        let classifier = IntentClassifier::new();
        let result = classifier.analyze_commit_message("Fix broken error in parser");
        assert_eq!(result.vote, CommitIntent::HallucinationFix);
        assert!((result.confidence - 0.7).abs() < f64::EPSILON);
        assert_eq!(result.signal_name, "commit_message");
    }

    #[test]
    fn test_commit_message_iteration_keywords() {
        let classifier = IntentClassifier::new();
        let result = classifier.analyze_commit_message("Refactor and improve the optimizer");
        assert_eq!(result.vote, CommitIntent::PlannedIteration);
        assert!((result.confidence - 0.7).abs() < f64::EPSILON);
    }

    #[test]
    fn test_commit_message_uncertain_no_keywords() {
        let classifier = IntentClassifier::new();
        let result = classifier.analyze_commit_message("Update documentation");
        assert_eq!(result.vote, CommitIntent::Uncertain);
        assert!((result.confidence - 0.3).abs() < f64::EPSILON);
        assert!(result.evidence.contains("No clear keyword pattern"));
    }

    #[test]
    fn test_commit_message_equal_keywords() {
        let classifier = IntentClassifier::new();
        // "fix" = hallucination, "add" = iteration -> equal count
        let result = classifier.analyze_commit_message("fix add");
        assert_eq!(result.vote, CommitIntent::Uncertain);
    }

    #[test]
    fn test_commit_message_case_insensitive() {
        let classifier = IntentClassifier::new();
        let result = classifier.analyze_commit_message("FIX BROKEN ERROR");
        assert_eq!(result.vote, CommitIntent::HallucinationFix);
    }

    #[test]
    fn test_commit_message_empty() {
        let classifier = IntentClassifier::new();
        let result = classifier.analyze_commit_message("");
        assert_eq!(result.vote, CommitIntent::Uncertain);
    }

    // ===== analyze_issue_linkage tests =====

    #[test]
    fn test_issue_linkage_created_after_original() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 1000, vec![], "main");
        let mut followup = make_commit_info("followup", 2000, vec![], "main");
        followup.issue_number = Some(42);
        followup.issue_created_timestamp = Some(1500); // After original commit

        let result = classifier.analyze_issue_linkage(&original, &followup);
        assert_eq!(result.vote, CommitIntent::HallucinationFix);
        assert!((result.confidence - 0.9).abs() < f64::EPSILON);
        assert!(result
            .evidence
            .contains("Issue #42 created after original commit"));
    }

    #[test]
    fn test_issue_linkage_created_before_original() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 1000, vec![], "main");
        let mut followup = make_commit_info("followup", 2000, vec![], "main");
        followup.issue_number = Some(123);
        followup.issue_created_timestamp = Some(500); // Before original commit

        let result = classifier.analyze_issue_linkage(&original, &followup);
        assert_eq!(result.vote, CommitIntent::PlannedIteration);
        assert!((result.confidence - 0.8).abs() < f64::EPSILON);
        assert!(result
            .evidence
            .contains("Issue #123 existed before original commit"));
    }

    #[test]
    fn test_issue_linkage_no_issue() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 1000, vec![], "main");
        let followup = make_commit_info("followup", 2000, vec![], "main");

        let result = classifier.analyze_issue_linkage(&original, &followup);
        assert_eq!(result.vote, CommitIntent::Uncertain);
        assert!((result.confidence - 0.2).abs() < f64::EPSILON);
        assert!(result.evidence.contains("No issue reference"));
    }

    #[test]
    fn test_issue_linkage_issue_number_but_no_timestamp() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 1000, vec![], "main");
        let mut followup = make_commit_info("followup", 2000, vec![], "main");
        followup.issue_number = Some(42);
        // No issue_created_timestamp

        let result = classifier.analyze_issue_linkage(&original, &followup);
        assert_eq!(result.vote, CommitIntent::Uncertain);
    }

    #[test]
    fn test_issue_linkage_issue_created_at_exact_same_time() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 1000, vec![], "main");
        let mut followup = make_commit_info("followup", 2000, vec![], "main");
        followup.issue_number = Some(42);
        followup.issue_created_timestamp = Some(1000); // Same as original

        let result = classifier.analyze_issue_linkage(&original, &followup);
        // Not greater than, so it's PlannedIteration
        assert_eq!(result.vote, CommitIntent::PlannedIteration);
    }

    // ===== analyze_code_churn tests =====

    #[test]
    fn test_code_churn_high_overlap() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 1000, vec!["a.rs", "b.rs", "c.rs"], "main");
        let followup = make_commit_info("followup", 2000, vec!["a.rs", "b.rs", "c.rs"], "main");

        let result = classifier.analyze_code_churn(&original, &followup);
        assert_eq!(result.vote, CommitIntent::HallucinationFix);
        assert!((result.confidence - 0.8).abs() < f64::EPSILON);
        assert!(result.evidence.contains("100% file overlap"));
    }

    #[test]
    fn test_code_churn_low_overlap() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 1000, vec!["a.rs"], "main");
        let followup = make_commit_info(
            "followup",
            2000,
            vec!["x.rs", "y.rs", "z.rs", "w.rs", "v.rs"],
            "main",
        );

        let result = classifier.analyze_code_churn(&original, &followup);
        assert_eq!(result.vote, CommitIntent::PlannedIteration);
        assert!((result.confidence - 0.7).abs() < f64::EPSILON);
        assert!(result.evidence.contains("overlap suggests new work"));
    }

    #[test]
    fn test_code_churn_moderate_overlap() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 1000, vec!["a.rs", "b.rs"], "main");
        let followup = make_commit_info("followup", 2000, vec!["a.rs", "c.rs", "d.rs"], "main");
        // Overlap: 1 (a.rs), total: 3, ratio = 0.33

        let result = classifier.analyze_code_churn(&original, &followup);
        assert_eq!(result.vote, CommitIntent::Uncertain);
        assert!((result.confidence - 0.4).abs() < f64::EPSILON);
        assert!(result.evidence.contains("ambiguous"));
    }

    #[test]
    fn test_code_churn_no_modified_files() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 1000, vec!["a.rs"], "main");
        let followup = make_commit_info("followup", 2000, vec![], "main");

        let result = classifier.analyze_code_churn(&original, &followup);
        assert_eq!(result.vote, CommitIntent::Uncertain);
        assert!((result.confidence - 0.1).abs() < f64::EPSILON);
        assert!(result.evidence.contains("No modified files"));
    }

    #[test]
    fn test_code_churn_exactly_at_threshold() {
        let classifier = IntentClassifier::new();
        // threshold is 0.8, so 80% overlap should still be HallucinationFix
        let original = make_commit_info(
            "original",
            1000,
            vec!["a.rs", "b.rs", "c.rs", "d.rs"],
            "main",
        );
        let followup = make_commit_info(
            "followup",
            2000,
            vec!["a.rs", "b.rs", "c.rs", "d.rs", "e.rs"],
            "main",
        );
        // Overlap: 4, total: 5, ratio = 0.8

        let result = classifier.analyze_code_churn(&original, &followup);
        // > 0.8 is false for 0.8, so it should be Uncertain
        assert_eq!(result.vote, CommitIntent::Uncertain);
    }

    #[test]
    fn test_code_churn_just_above_threshold() {
        let classifier = IntentClassifier::new();
        // 0.81 > 0.8
        let original = make_commit_info(
            "original",
            1000,
            vec![
                "a.rs", "b.rs", "c.rs", "d.rs", "e.rs", "f.rs", "g.rs", "h.rs", "i.rs",
            ],
            "main",
        );
        let followup = make_commit_info(
            "followup",
            2000,
            vec![
                "a.rs", "b.rs", "c.rs", "d.rs", "e.rs", "f.rs", "g.rs", "h.rs", "i.rs", "j.rs",
                "k.rs",
            ],
            "main",
        );
        // Overlap: 9, total: 11, ratio = 9/11 = 0.818

        let result = classifier.analyze_code_churn(&original, &followup);
        assert_eq!(result.vote, CommitIntent::HallucinationFix);
    }

    // ===== analyze_test_changes tests =====

    #[test]
    fn test_test_changes_more_added_than_fixed() {
        let classifier = IntentClassifier::new();
        let test_changes = TestChanges {
            added_tests: 5,
            fixed_tests: 2,
            modified_test_files: vec!["tests/test_a.rs".to_string()],
        };

        let result = classifier.analyze_test_changes(&test_changes);
        assert_eq!(result.vote, CommitIntent::PlannedIteration);
        assert!((result.confidence - 0.7).abs() < f64::EPSILON);
        assert!(result.evidence.contains("expanding coverage"));
    }

    #[test]
    fn test_test_changes_more_fixed_than_added() {
        let classifier = IntentClassifier::new();
        let test_changes = TestChanges {
            added_tests: 1,
            fixed_tests: 4,
            modified_test_files: vec![],
        };

        let result = classifier.analyze_test_changes(&test_changes);
        assert_eq!(result.vote, CommitIntent::HallucinationFix);
        assert!((result.confidence - 0.8).abs() < f64::EPSILON);
        assert!(result.evidence.contains("fixing broken tests"));
    }

    #[test]
    fn test_test_changes_equal() {
        let classifier = IntentClassifier::new();
        let test_changes = TestChanges {
            added_tests: 3,
            fixed_tests: 3,
            modified_test_files: vec![],
        };

        let result = classifier.analyze_test_changes(&test_changes);
        assert_eq!(result.vote, CommitIntent::Uncertain);
        assert!((result.confidence - 0.3).abs() < f64::EPSILON);
        assert!(result.evidence.contains("Equal test additions and fixes"));
    }

    #[test]
    fn test_test_changes_both_zero() {
        let classifier = IntentClassifier::new();
        let test_changes = TestChanges {
            added_tests: 0,
            fixed_tests: 0,
            modified_test_files: vec![],
        };

        let result = classifier.analyze_test_changes(&test_changes);
        assert_eq!(result.vote, CommitIntent::Uncertain);
    }

    // ===== analyze_temporal_context tests =====

    #[test]
    fn test_temporal_within_grace_period() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 0, vec![], "main");
        // 24 hours = 24 * 3600 = 86400 seconds
        let followup = make_commit_info("followup", 86400, vec![], "feature");

        let result = classifier.analyze_temporal_context(&original, &followup);
        assert_eq!(result.vote, CommitIntent::PlannedIteration);
        assert!((result.confidence - 0.8).abs() < f64::EPSILON);
        assert!(result.evidence.contains("Within 48-hour grace period"));
    }

    #[test]
    fn test_temporal_after_grace_same_branch() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 0, vec![], "main");
        // 72 hours = 72 * 3600 = 259200 seconds
        let followup = make_commit_info("followup", 259200, vec![], "main");

        let result = classifier.analyze_temporal_context(&original, &followup);
        assert_eq!(result.vote, CommitIntent::PlannedIteration);
        assert!((result.confidence - 0.6).abs() < f64::EPSILON);
        assert!(result.evidence.contains("Same branch"));
    }

    #[test]
    fn test_temporal_after_grace_different_branch() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 0, vec![], "main");
        // 100 hours = 100 * 3600 = 360000 seconds
        let followup = make_commit_info("followup", 360000, vec![], "hotfix");

        let result = classifier.analyze_temporal_context(&original, &followup);
        assert_eq!(result.vote, CommitIntent::HallucinationFix);
        assert!((result.confidence - 0.4).abs() < f64::EPSILON);
        assert!(result.evidence.contains("after grace period"));
        assert!(result.evidence.contains("different branch"));
    }

    #[test]
    fn test_temporal_exactly_at_grace_period() {
        let classifier = IntentClassifier::new();
        let original = make_commit_info("original", 0, vec![], "main");
        // 48 hours exactly
        let followup = make_commit_info("followup", 48 * 3600, vec![], "feature");

        let result = classifier.analyze_temporal_context(&original, &followup);
        // Not < 48, so it checks branch
        assert_eq!(result.vote, CommitIntent::HallucinationFix);
    }