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
// Tests for work handlers
// Extracted for file health compliance (CB-040)

use super::*;

mod tests {
    use super::*;

    #[test]
    fn test_parse_github_url_https() {
        let url = "https://github.com/paiml/pmat.git";
        assert_eq!(parse_github_url(url), Some("paiml/pmat".to_string()));
    }

    #[test]
    fn test_parse_github_url_ssh() {
        let url = "git@github.com:paiml/pmat.git";
        assert_eq!(parse_github_url(url), Some("paiml/pmat".to_string()));
    }

    #[test]
    fn test_parse_github_url_invalid() {
        let url = "https://gitlab.com/owner/repo.git";
        assert_eq!(parse_github_url(url), None);
    }
}


mod coverage_tests {
    use super::*;
    use proptest::prelude::*;
    use tempfile::TempDir;

    // ========== Test Fixtures ==========

    /// Create a test project directory with roadmap structure
    fn create_test_project() -> TempDir {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");

        // Create docs/roadmaps directory
        let roadmaps_dir = temp_dir.path().join("docs").join("roadmaps");
        std::fs::create_dir_all(&roadmaps_dir).expect("Failed to create roadmaps dir");

        temp_dir
    }

    /// Create a test project with initialized roadmap
    fn create_initialized_project() -> TempDir {
        let temp_dir = create_test_project();

        let roadmap_path = temp_dir
            .path()
            .join("docs")
            .join("roadmaps")
            .join("roadmap.yaml");
        let roadmap_content = r#"
roadmap_version: '1.0'
github_enabled: true
github_repo: paiml/pmat
roadmap:
  - id: TEST-001
    title: Test Item 1
    status: planned
    priority: medium
  - id: GH-42
    github_issue: 42
    title: GitHub Issue
    status: inprogress
    priority: high
    labels:
      - enhancement
      - feature
  - id: EPIC-001
    title: Epic Item
    status: planned
    priority: high
    item_type: epic
    subtasks:
      - id: EPIC-001-A
        title: Subtask A
        status: completed
        completion: 100
      - id: EPIC-001-B
        title: Subtask B
        status: inprogress
        completion: 50
"#;
        std::fs::write(&roadmap_path, roadmap_content).expect("Failed to write roadmap");

        temp_dir
    }

    /// Create a test roadmap item
    fn make_test_item(id: &str, title: &str, status: ItemStatus) -> RoadmapItem {
        let mut item = RoadmapItem::new(id.to_string(), title.to_string());
        item.status = status;
        item
    }

    // ========== parse_github_url Tests ==========

    mod parse_github_url_tests {
        use super::*;

        #[test]
        fn test_https_url_with_git_extension() {
            let url = "https://github.com/owner/repo.git";
            assert_eq!(parse_github_url(url), Some("owner/repo".to_string()));
        }

        #[test]
        fn test_https_url_without_git_extension() {
            let url = "https://github.com/owner/repo";
            assert_eq!(parse_github_url(url), Some("owner/repo".to_string()));
        }

        #[test]
        fn test_ssh_url_with_git_extension() {
            let url = "git@github.com:owner/repo.git";
            assert_eq!(parse_github_url(url), Some("owner/repo".to_string()));
        }

        #[test]
        fn test_ssh_url_without_git_extension() {
            let url = "git@github.com:owner/repo";
            assert_eq!(parse_github_url(url), Some("owner/repo".to_string()));
        }

        #[test]
        fn test_https_url_with_org_and_nested_repo() {
            let url = "https://github.com/paiml/paiml-mcp-agent-toolkit.git";
            assert_eq!(
                parse_github_url(url),
                Some("paiml/paiml-mcp-agent-toolkit".to_string())
            );
        }

        #[test]
        fn test_gitlab_url_returns_none() {
            let url = "https://gitlab.com/owner/repo.git";
            assert_eq!(parse_github_url(url), None);
        }

        #[test]
        fn test_bitbucket_url_returns_none() {
            let url = "https://bitbucket.org/owner/repo.git";
            assert_eq!(parse_github_url(url), None);
        }

        #[test]
        fn test_empty_url() {
            assert_eq!(parse_github_url(""), None);
        }

        #[test]
        fn test_random_string() {
            assert_eq!(parse_github_url("not-a-url"), None);
        }
    }

    // ========== parse_acceptance_criteria Tests ==========

    mod parse_acceptance_criteria_tests {
        use super::*;

        #[test]
        fn test_empty_body() {
            let body = "";
            let criteria = parse_acceptance_criteria(body);
            assert!(criteria.is_empty());
        }

        #[test]
        fn test_body_with_unchecked_checkboxes() {
            let body = r#"
## Acceptance Criteria
- [ ] First criterion
- [ ] Second criterion
- [ ] Third criterion
"#;
            let criteria = parse_acceptance_criteria(body);
            assert_eq!(criteria.len(), 3);
            assert_eq!(criteria[0], "First criterion");
            assert_eq!(criteria[1], "Second criterion");
            assert_eq!(criteria[2], "Third criterion");
        }

        #[test]
        fn test_body_with_checked_checkboxes() {
            let body = r#"
## Done
- [x] Completed task
- [x] Another completed task
"#;
            let criteria = parse_acceptance_criteria(body);
            assert_eq!(criteria.len(), 2);
            assert_eq!(criteria[0], "Completed task");
            assert_eq!(criteria[1], "Another completed task");
        }

        #[test]
        fn test_body_with_mixed_checkboxes() {
            let body = r#"
## Acceptance Criteria
- [x] Already done
- [ ] Still pending
- [x] Also done
"#;
            let criteria = parse_acceptance_criteria(body);
            assert_eq!(criteria.len(), 3);
        }

        #[test]
        fn test_body_with_no_checkboxes() {
            let body = r#"
This is a description without checkboxes.
Just regular text.
"#;
            let criteria = parse_acceptance_criteria(body);
            assert!(criteria.is_empty());
        }

        #[test]
        fn test_body_with_empty_checkbox() {
            let body = "- [ ] ";
            let criteria = parse_acceptance_criteria(body);
            assert!(criteria.is_empty());
        }

        #[test]
        fn test_body_with_whitespace_only_checkbox() {
            let body = "- [ ]    ";
            let criteria = parse_acceptance_criteria(body);
            assert!(criteria.is_empty());
        }
    }

    // ========== extract_line_from_yaml_error Tests ==========

    mod extract_line_from_yaml_error_tests {
        use super::*;

        #[test]
        fn test_error_with_line_number() {
            let error = "invalid type: string, expected sequence at line 42 column 5";
            let line = extract_line_from_yaml_error(error);
            assert_eq!(line, Some(42));
        }

        #[test]
        fn test_error_without_line_number() {
            let error = "invalid type: string, expected sequence";
            let line = extract_line_from_yaml_error(error);
            assert_eq!(line, None);
        }

        #[test]
        fn test_error_with_single_digit_line() {
            let error = "error at line 5 column 1";
            let line = extract_line_from_yaml_error(error);
            assert_eq!(line, Some(5));
        }

        #[test]
        fn test_error_with_large_line_number() {
            let error = "parsing failed at line 1234 column 10";
            let line = extract_line_from_yaml_error(error);
            assert_eq!(line, Some(1234));
        }

        #[test]
        fn test_empty_error_string() {
            let error = "";
            let line = extract_line_from_yaml_error(error);
            assert_eq!(line, None);
        }
    }

    // ========== CommitMetadata Tests ==========

    mod commit_metadata_tests {
        use super::*;

        #[test]
        fn test_commit_metadata_serialization() {
            let metadata = CommitMetadata {
                commit_sha: Some("abc123".to_string()),
                work_item_id: "TEST-001".to_string(),
                prompt: "Test task".to_string(),
                tdg_score: 85.0,
                repo_score: 75.0,
                rust_project_score: Some(90.0),
                timestamp: chrono::Utc::now(),
            };

            let json = serde_json::to_string(&metadata).unwrap();
            assert!(json.contains("abc123"));
            assert!(json.contains("TEST-001"));
            assert!(json.contains("85"));
        }

        #[test]
        fn test_commit_metadata_deserialization() {
            let json = r#"{
                "commit_sha": "def456",
                "work_item_id": "GH-42",
                "prompt": "Fix bug",
                "tdg_score": 90.0,
                "repo_score": 80.0,
                "rust_project_score": null,
                "timestamp": "2024-01-01T00:00:00Z"
            }"#;

            let metadata: CommitMetadata = serde_json::from_str(json).unwrap();
            assert_eq!(metadata.commit_sha, Some("def456".to_string()));
            assert_eq!(metadata.work_item_id, "GH-42");
            assert_eq!(metadata.tdg_score, 90.0);
            assert!(metadata.rust_project_score.is_none());
        }
    }

    // ========== Score Capture Tests ==========

    mod score_capture_tests {
        use super::*;

        #[tokio::test]
        async fn test_capture_tdg_score_no_cache() {
            let temp_dir = TempDir::new().unwrap();
            let score = capture_tdg_score(&temp_dir.path().to_path_buf()).await;
            // Should return default when no cache exists
            assert!(score.is_ok());
            assert_eq!(score.unwrap(), 0.0);
        }

        #[tokio::test]
        async fn test_capture_tdg_score_with_cache() {
            let temp_dir = TempDir::new().unwrap();
            let metrics_dir = temp_dir.path().join(".pmat-metrics");
            std::fs::create_dir_all(&metrics_dir).unwrap();

            let tdg_file = metrics_dir.join("tdg-score.json");
            std::fs::write(&tdg_file, r#"{"score": 85.5}"#).unwrap();

            let score = capture_tdg_score(&temp_dir.path().to_path_buf()).await;
            assert!(score.is_ok());
            assert_eq!(score.unwrap(), 85.5);
        }

        #[tokio::test]
        async fn test_capture_repo_score_no_cache() {
            let temp_dir = TempDir::new().unwrap();
            let score = capture_repo_score(&temp_dir.path().to_path_buf()).await;
            assert!(score.is_ok());
            assert_eq!(score.unwrap(), 0.0);
        }

        #[tokio::test]
        async fn test_capture_repo_score_with_cache() {
            let temp_dir = TempDir::new().unwrap();
            let metrics_dir = temp_dir.path().join(".pmat-metrics");
            std::fs::create_dir_all(&metrics_dir).unwrap();

            let repo_file = metrics_dir.join("repo-score.json");
            std::fs::write(&repo_file, r#"{"score": 72.0}"#).unwrap();

            let score = capture_repo_score(&temp_dir.path().to_path_buf()).await;
            assert!(score.is_ok());
            assert_eq!(score.unwrap(), 72.0);
        }

        #[tokio::test]
        async fn test_capture_rust_project_score_no_cache() {
            let temp_dir = TempDir::new().unwrap();
            let score = capture_rust_project_score(&temp_dir.path().to_path_buf()).await;
            assert!(score.is_ok());
            assert_eq!(score.unwrap(), 0.0);
        }

        #[tokio::test]
        async fn test_capture_rust_project_score_with_cache() {
            let temp_dir = TempDir::new().unwrap();
            let metrics_dir = temp_dir.path().join(".pmat-metrics");
            std::fs::create_dir_all(&metrics_dir).unwrap();

            let rust_file = metrics_dir.join("rust-project-score.json");
            std::fs::write(&rust_file, r#"{"total_earned": 95.0}"#).unwrap();

            let score = capture_rust_project_score(&temp_dir.path().to_path_buf()).await;
            assert!(score.is_ok());
            assert_eq!(score.unwrap(), 95.0);
        }

        #[tokio::test]
        async fn test_capture_score_with_invalid_json() {
            let temp_dir = TempDir::new().unwrap();
            let metrics_dir = temp_dir.path().join(".pmat-metrics");
            std::fs::create_dir_all(&metrics_dir).unwrap();

            let tdg_file = metrics_dir.join("tdg-score.json");
            std::fs::write(&tdg_file, "not valid json").unwrap();

            let score = capture_tdg_score(&temp_dir.path().to_path_buf()).await;
            assert!(score.is_err());
        }
    }

    // ========== Handler Integration Tests ==========

    mod handler_integration_tests {
        use super::*;

        #[tokio::test]
        async fn test_handle_work_init_creates_roadmap() {
            let temp_dir = create_test_project();

            let result = handle_work_init(
                Some("paiml/test".to_string()),
                false,
                Some(temp_dir.path().to_path_buf()),
            )
            .await;

            assert!(result.is_ok());

            let roadmap_path = temp_dir
                .path()
                .join("docs")
                .join("roadmaps")
                .join("roadmap.yaml");
            assert!(roadmap_path.exists());
        }

        #[tokio::test]
        async fn test_handle_work_init_no_github() {
            let temp_dir = create_test_project();

            let result = handle_work_init(None, true, Some(temp_dir.path().to_path_buf())).await;

            assert!(result.is_ok());
        }

        #[tokio::test]
        async fn test_handle_work_init_already_exists() {
            let temp_dir = create_initialized_project();

            let result = handle_work_init(
                Some("paiml/test".to_string()),
                false,
                Some(temp_dir.path().to_path_buf()),
            )
            .await;

            // Should succeed but indicate already exists
            assert!(result.is_ok());
        }

        #[tokio::test]
        async fn test_handle_work_status_all_items() {
            let temp_dir = create_initialized_project();

            let result = handle_work_status(None, Some(temp_dir.path().to_path_buf()), false).await;

            assert!(result.is_ok());
        }

        #[tokio::test]
        async fn test_handle_work_status_active_only() {
            let temp_dir = create_initialized_project();

            let result = handle_work_status(None, Some(temp_dir.path().to_path_buf()), true).await;

            assert!(result.is_ok());
        }

        #[tokio::test]
        async fn test_handle_work_status_specific_item() {
            let temp_dir = create_initialized_project();

            let result = handle_work_status(
                Some("TEST-001".to_string()),
                Some(temp_dir.path().to_path_buf()),
                false,
            )
            .await;

            assert!(result.is_ok());
        }

        #[tokio::test]
        async fn test_handle_work_status_nonexistent_item() {
            let temp_dir = create_initialized_project();

            let result = handle_work_status(
                Some("NONEXISTENT-999".to_string()),
                Some(temp_dir.path().to_path_buf()),
                false,
            )
            .await;