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
            .await;

            assert!(result.is_ok());

            // Verify item was created
            let roadmap_path = temp_dir
                .path()
                .join("docs/roadmaps/roadmap.yaml");
            let service = RoadmapService::new(&roadmap_path);
            let item = service.find_item("NEW-TICKET").unwrap();
            assert!(item.is_some());
            assert_eq!(item.unwrap().status, ItemStatus::InProgress);
        }

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

            // Start work on existing TEST-001
            let result = handle_work_start(
                "TEST-001".to_string(),
                false,
                false,
                Some(temp_dir.path().to_path_buf()),
                false,
            )
            .await;

            assert!(result.is_ok());

            // Verify status changed to InProgress
            let roadmap_path = temp_dir.path().join("docs/roadmaps/roadmap.yaml");
            let service = RoadmapService::new(&roadmap_path);
            let item = service.find_item("TEST-001").unwrap().unwrap();
            assert_eq!(item.status, ItemStatus::InProgress);
        }

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

            let result = handle_work_start(
                "EPIC-NEW".to_string(),
                false,
                true, // epic flag
                Some(temp_dir.path().to_path_buf()),
                false,
            )
            .await;

            assert!(result.is_ok());

            let roadmap_path = temp_dir.path().join("docs/roadmaps/roadmap.yaml");
            let service = RoadmapService::new(&roadmap_path);
            let item = service.find_item("EPIC-NEW").unwrap().unwrap();
            assert_eq!(item.item_type, crate::models::roadmap::ItemType::Epic);
        }

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

            let result = handle_work_start(
                "SPEC-TEST".to_string(),
                true, // with_spec
                false,
                Some(temp_dir.path().to_path_buf()),
                false,
            )
            .await;

            assert!(result.is_ok());

            // Verify spec file was created
            let spec_path = temp_dir.path().join("docs/specifications/spec-test-spec.md");
            assert!(spec_path.exists());
        }

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

            // Start work on issue number (no GitHub API available, should create placeholder)
            let result = handle_work_start(
                "999".to_string(),
                false,
                false,
                Some(temp_dir.path().to_path_buf()),
                false,
            )
            .await;

            assert!(result.is_ok());

            let roadmap_path = temp_dir.path().join("docs/roadmaps/roadmap.yaml");
            let service = RoadmapService::new(&roadmap_path);
            let item = service.find_item("GH-999").unwrap();
            assert!(item.is_some());
            assert_eq!(item.unwrap().github_issue, Some(999));
        }
    }

    // ========== Work Complete Handler Tests ==========

    mod work_complete_tests {
        use super::*;

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

            // First start the work
            let roadmap_path = temp_dir.path().join("docs/roadmaps/roadmap.yaml");
            let service = RoadmapService::new(&roadmap_path);
            let mut item = service.find_item("TEST-001").unwrap().unwrap();
            item.status = ItemStatus::InProgress;
            service.upsert_item(item).unwrap();

            // Initialize git for metadata capture
            std::process::Command::new("git")
                .args(["init"])
                .current_dir(temp_dir.path())
                .status()
                .ok();
            std::process::Command::new("git")
                .args(["config", "user.email", "test@test.com"])
                .current_dir(temp_dir.path())
                .status()
                .ok();
            std::process::Command::new("git")
                .args(["config", "user.name", "Test"])
                .current_dir(temp_dir.path())
                .status()
                .ok();
            std::fs::write(temp_dir.path().join("test.txt"), "test").unwrap();
            std::process::Command::new("git")
                .args(["add", "."])
                .current_dir(temp_dir.path())
                .status()
                .ok();
            std::process::Command::new("git")
                .args(["commit", "-m", "init"])
                .current_dir(temp_dir.path())
                .status()
                .ok();

            let result = handle_work_complete(
                "TEST-001".to_string(),
                true, // skip_quality
                Some(temp_dir.path().to_path_buf()),
            )
            .await;

            assert!(result.is_ok());

            // Verify status changed to Completed
            let item = service.find_item("TEST-001").unwrap().unwrap();
            assert_eq!(item.status, ItemStatus::Completed);
        }

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

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

            assert!(result.is_err());
        }

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

            // Set up item with labels
            let roadmap_path = temp_dir.path().join("docs/roadmaps/roadmap.yaml");
            let service = RoadmapService::new(&roadmap_path);
            let item = service.find_item("GH-42").unwrap().unwrap();
            // GH-42 already has labels from test fixture
            assert!(!item.labels.is_empty());

            // Initialize git
            std::process::Command::new("git")
                .args(["init"])
                .current_dir(temp_dir.path())
                .status()
                .ok();
            std::process::Command::new("git")
                .args(["config", "user.email", "test@test.com"])
                .current_dir(temp_dir.path())
                .status()
                .ok();
            std::process::Command::new("git")
                .args(["config", "user.name", "Test"])
                .current_dir(temp_dir.path())
                .status()
                .ok();
            std::fs::write(temp_dir.path().join("test.txt"), "test").unwrap();
            std::process::Command::new("git")
                .args(["add", "."])
                .current_dir(temp_dir.path())
                .status()
                .ok();
            std::process::Command::new("git")
                .args(["commit", "-m", "init"])
                .current_dir(temp_dir.path())
                .status()
                .ok();

            let result = handle_work_complete(
                "GH-42".to_string(),
                true, // skip_quality
                Some(temp_dir.path().to_path_buf()),
            )
            .await;

            assert!(result.is_ok());
        }
    }

    // ========== Git Detection Tests ==========

    mod git_detection_tests {
        use super::*;

        #[test]
        fn test_detect_github_repo_no_git() {
            let temp_dir = TempDir::new().unwrap();
            let result = detect_github_repo(&temp_dir.path().to_path_buf());
            assert!(result.is_ok());
            assert!(result.unwrap().is_none());
        }

        #[test]
        fn test_detect_github_repo_with_remote() {
            let temp_dir = TempDir::new().unwrap();

            // Initialize git repo
            std::process::Command::new("git")
                .args(["init"])
                .current_dir(temp_dir.path())
                .status()
                .ok();

            // Add remote
            std::process::Command::new("git")
                .args(["remote", "add", "origin", "https://github.com/test/repo.git"])
                .current_dir(temp_dir.path())
                .status()
                .ok();

            let result = detect_github_repo(&temp_dir.path().to_path_buf());
            assert!(result.is_ok());
            assert_eq!(result.unwrap(), Some("test/repo".to_string()));
        }

        #[test]
        fn test_detect_github_repo_ssh_remote() {
            let temp_dir = TempDir::new().unwrap();

            std::process::Command::new("git")
                .args(["init"])
                .current_dir(temp_dir.path())
                .status()
                .ok();

            std::process::Command::new("git")
                .args(["remote", "add", "origin", "git@github.com:owner/project.git"])
                .current_dir(temp_dir.path())
                .status()
                .ok();

            let result = detect_github_repo(&temp_dir.path().to_path_buf());
            assert!(result.is_ok());
            assert_eq!(result.unwrap(), Some("owner/project".to_string()));
        }
    }

    // ========== Migration Tests ==========

    mod migration_tests {
        use super::*;

        #[tokio::test]
        async fn test_migrate_normalizes_done_status() {
            let temp_dir = TempDir::new().unwrap();
            let roadmap_dir = temp_dir.path().join("docs/roadmaps");
            std::fs::create_dir_all(&roadmap_dir).unwrap();

            let roadmap_path = roadmap_dir.join("roadmap.yaml");
            let content = r#"
roadmap_version: '1.0'
github_enabled: true
roadmap:
  - id: TEST-001
    title: Test Item
    status: done
    priority: medium
"#;
            std::fs::write(&roadmap_path, content).unwrap();

            let result = handle_work_migrate(
                Some(temp_dir.path().to_path_buf()),
                false, // not dry_run
                false, // no backup
            )
            .await;

            assert!(result.is_ok());

            // Verify status was normalized
            let new_content = std::fs::read_to_string(&roadmap_path).unwrap();
            assert!(new_content.contains("status: completed"));
            assert!(!new_content.contains("status: done"));
        }

        #[tokio::test]
        async fn test_migrate_normalizes_wip_status() {
            let temp_dir = TempDir::new().unwrap();
            let roadmap_dir = temp_dir.path().join("docs/roadmaps");
            std::fs::create_dir_all(&roadmap_dir).unwrap();

            let roadmap_path = roadmap_dir.join("roadmap.yaml");
            let content = r#"
roadmap_version: '1.0'
github_enabled: true
roadmap:
  - id: TEST-001
    title: Test Item
    status: wip
    priority: medium
"#;
            std::fs::write(&roadmap_path, content).unwrap();

            handle_work_migrate(
                Some(temp_dir.path().to_path_buf()),
                false,
                false,
            )
            .await
            .unwrap();

            let new_content = std::fs::read_to_string(&roadmap_path).unwrap();
            assert!(new_content.contains("status: inprogress"));
        }

        #[tokio::test]
        async fn test_migrate_dry_run_no_changes() {
            let temp_dir = TempDir::new().unwrap();
            let roadmap_dir = temp_dir.path().join("docs/roadmaps");
            std::fs::create_dir_all(&roadmap_dir).unwrap();

            let roadmap_path = roadmap_dir.join("roadmap.yaml");
            let content = r#"
roadmap_version: '1.0'
github_enabled: true
roadmap:
  - id: TEST-001
    title: Test Item
    status: done
    priority: medium
"#;
            std::fs::write(&roadmap_path, content).unwrap();

            handle_work_migrate(
                Some(temp_dir.path().to_path_buf()),
                true, // dry_run
                false,
            )
            .await
            .unwrap();

            // Content should be unchanged
            let new_content = std::fs::read_to_string(&roadmap_path).unwrap();
            assert!(new_content.contains("status: done"));
        }

        #[tokio::test]
        async fn test_migrate_multiple_status_normalizations() {
            let temp_dir = TempDir::new().unwrap();
            let roadmap_dir = temp_dir.path().join("docs/roadmaps");
            std::fs::create_dir_all(&roadmap_dir).unwrap();

            let roadmap_path = roadmap_dir.join("roadmap.yaml");
            let content = r#"
roadmap_version: '1.0'
github_enabled: true
roadmap:
  - id: TEST-001
    title: Item 1
    status: Done
    priority: medium
  - id: TEST-002
    title: Item 2
    status: WIP
    priority: high
  - id: TEST-003
    title: Item 3
    status: stuck
    priority: low
  - id: TEST-004
    title: Item 4
    status: todo
    priority: medium
"#;
            std::fs::write(&roadmap_path, content).unwrap();

            handle_work_migrate(
                Some(temp_dir.path().to_path_buf()),
                false,
                false,
            )
            .await
            .unwrap();

            let new_content = std::fs::read_to_string(&roadmap_path).unwrap();
            assert!(new_content.contains("status: completed"));
            assert!(new_content.contains("status: inprogress"));
            assert!(new_content.contains("status: blocked"));
            assert!(new_content.contains("status: planned"));
        }
    }

    // ========== Sync Direction Tests ==========

    mod sync_direction_tests {
        use super::*;

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

            let result = handle_work_sync(
                SyncDirection::YamlToGithub,
                Some(temp_dir.path().to_path_buf()),
                true,
            )
            .await;

            assert!(result.is_ok());
        }

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

            // Test all sync directions
            for direction in [
                SyncDirection::YamlToGithub,
                SyncDirection::GithubToYaml,
                SyncDirection::Full,
            ] {
                let result = handle_work_sync(
                    direction,
                    Some(temp_dir.path().to_path_buf()),
                    true,
                )
                .await;
                assert!(result.is_ok());
            }
        }
    }

    // ========== Roadmap Item Properties Tests ==========

    mod roadmap_item_properties {
        use super::*;

        #[test]
        fn test_completion_percentage_with_subtasks() {
            let mut item = RoadmapItem::new("EPIC-001".to_string(), "Epic".to_string());
            item.subtasks = vec![
                crate::models::roadmap::Subtask {
                    id: "SUB-1".to_string(),
                    github_issue: None,
                    title: "Sub 1".to_string(),
                    status: ItemStatus::Completed,
                    completion: 100,
                },
                crate::models::roadmap::Subtask {
                    id: "SUB-2".to_string(),
                    github_issue: None,
                    title: "Sub 2".to_string(),
                    status: ItemStatus::InProgress,
                    completion: 50,
                },
            ];
            // Average of 100 and 50 = 75
            assert_eq!(item.completion_percentage(), 75);
        }

        #[test]
        fn test_completion_percentage_with_phases() {
            let mut item = RoadmapItem::new("TASK-001".to_string(), "Task".to_string());
            item.phases = vec![
                crate::models::roadmap::Phase {