pmat 3.15.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

#[test]
fn test_fuzzy_matching_not_found() {
    let mut roadmap = Roadmap::new(None);
    roadmap.upsert_item(RoadmapItem::new("PMAT-001".to_string(), "Test".to_string()));

    let found = roadmap.find_item("NONEXISTENT");
    assert!(found.is_none());
}

// ============================================================================
// Tests for ItemStatus parsing (used in handle_work_edit)
// ============================================================================

#[test]
fn test_item_status_from_string_completed() {
    assert_eq!(
        ItemStatus::from_string("completed").unwrap(),
        ItemStatus::Completed
    );
    assert_eq!(
        ItemStatus::from_string("done").unwrap(),
        ItemStatus::Completed
    );
    assert_eq!(
        ItemStatus::from_string("DONE").unwrap(),
        ItemStatus::Completed
    );
}

#[test]
fn test_item_status_from_string_inprogress() {
    assert_eq!(
        ItemStatus::from_string("inprogress").unwrap(),
        ItemStatus::InProgress
    );
    assert_eq!(
        ItemStatus::from_string("wip").unwrap(),
        ItemStatus::InProgress
    );
    assert_eq!(
        ItemStatus::from_string("in-progress").unwrap(),
        ItemStatus::InProgress
    );
}

#[test]
fn test_item_status_from_string_planned() {
    assert_eq!(
        ItemStatus::from_string("planned").unwrap(),
        ItemStatus::Planned
    );
    assert_eq!(
        ItemStatus::from_string("todo").unwrap(),
        ItemStatus::Planned
    );
    assert_eq!(
        ItemStatus::from_string("open").unwrap(),
        ItemStatus::Planned
    );
}

#[test]
fn test_item_status_from_string_blocked() {
    assert_eq!(
        ItemStatus::from_string("blocked").unwrap(),
        ItemStatus::Blocked
    );
    assert_eq!(
        ItemStatus::from_string("stuck").unwrap(),
        ItemStatus::Blocked
    );
    assert_eq!(
        ItemStatus::from_string("on-hold").unwrap(),
        ItemStatus::Blocked
    );
}

#[test]
fn test_item_status_from_string_review() {
    assert_eq!(
        ItemStatus::from_string("review").unwrap(),
        ItemStatus::Review
    );
    assert_eq!(ItemStatus::from_string("pr").unwrap(), ItemStatus::Review);
    assert_eq!(
        ItemStatus::from_string("pending-review").unwrap(),
        ItemStatus::Review
    );
}

#[test]
fn test_item_status_from_string_cancelled() {
    assert_eq!(
        ItemStatus::from_string("cancelled").unwrap(),
        ItemStatus::Cancelled
    );
    assert_eq!(
        ItemStatus::from_string("canceled").unwrap(),
        ItemStatus::Cancelled
    );
    assert_eq!(
        ItemStatus::from_string("wontfix").unwrap(),
        ItemStatus::Cancelled
    );
}

#[test]
fn test_item_status_from_string_invalid() {
    let result = ItemStatus::from_string("invalid_status");
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.contains("unknown status"));
    assert!(err.contains("did you mean"));
}

#[test]
fn test_item_status_valid_values() {
    let values = ItemStatus::valid_values();
    assert!(values.contains(&"completed"));
    assert!(values.contains(&"inprogress"));
    assert!(values.contains(&"planned"));
    assert!(values.contains(&"blocked"));
    assert!(values.contains(&"review"));
    assert!(values.contains(&"cancelled"));
}

// ============================================================================
// Tests for ItemStatus::can_transition_to (DBC §work_lifecycle)
// ============================================================================

#[test]
fn test_lifecycle_valid_transitions() {
    // Planned -> InProgress, Cancelled
    assert!(ItemStatus::Planned.can_transition_to(ItemStatus::InProgress));
    assert!(ItemStatus::Planned.can_transition_to(ItemStatus::Cancelled));
    // InProgress -> Blocked, Review, Completed
    assert!(ItemStatus::InProgress.can_transition_to(ItemStatus::Blocked));
    assert!(ItemStatus::InProgress.can_transition_to(ItemStatus::Review));
    assert!(ItemStatus::InProgress.can_transition_to(ItemStatus::Completed));
    // Blocked -> InProgress
    assert!(ItemStatus::Blocked.can_transition_to(ItemStatus::InProgress));
    // Review -> InProgress, Completed
    assert!(ItemStatus::Review.can_transition_to(ItemStatus::InProgress));
    assert!(ItemStatus::Review.can_transition_to(ItemStatus::Completed));
}

#[test]
fn test_lifecycle_invalid_planned_to_completed() {
    // FALSIFY-WDB-001: Planned->Completed is INVALID (no skip)
    assert!(!ItemStatus::Planned.can_transition_to(ItemStatus::Completed));
}

#[test]
fn test_lifecycle_terminal_states() {
    // FALSIFY-WDB-006: Completed and Cancelled are terminal (no outgoing edges)
    for target in [
        ItemStatus::Planned,
        ItemStatus::InProgress,
        ItemStatus::Blocked,
        ItemStatus::Review,
        ItemStatus::Completed,
        ItemStatus::Cancelled,
    ] {
        assert!(
            !ItemStatus::Completed.can_transition_to(target),
            "Completed should not transition to {:?}",
            target
        );
        assert!(
            !ItemStatus::Cancelled.can_transition_to(target),
            "Cancelled should not transition to {:?}",
            target
        );
    }
}

#[test]
fn test_lifecycle_display_name() {
    assert_eq!(ItemStatus::Planned.display_name(), "Planned");
    assert_eq!(ItemStatus::InProgress.display_name(), "InProgress");
    assert_eq!(ItemStatus::Completed.display_name(), "Completed");
    assert_eq!(ItemStatus::Cancelled.display_name(), "Cancelled");
    assert_eq!(ItemStatus::Blocked.display_name(), "Blocked");
    assert_eq!(ItemStatus::Review.display_name(), "Review");
}

// ============================================================================
// Tests for RoadmapItem methods
// ============================================================================

#[test]
fn test_roadmap_item_new() {
    let item = RoadmapItem::new("TEST-001".to_string(), "Test title".to_string());
    assert_eq!(item.id, "TEST-001");
    assert_eq!(item.title, "Test title");
    assert_eq!(item.status, ItemStatus::Planned);
    assert_eq!(item.priority, Priority::Medium);
    assert!(item.github_issue.is_none());
}

#[test]
fn test_roadmap_item_from_github_issue() {
    let item = RoadmapItem::from_github_issue(42, "GitHub issue title".to_string());
    assert_eq!(item.id, "GH-42");
    assert_eq!(item.github_issue, Some(42));
    assert_eq!(item.title, "GitHub issue title");
}

#[test]
fn test_roadmap_item_completion_percentage_planned() {
    let item = RoadmapItem::new("TEST".to_string(), "Test".to_string());
    assert_eq!(item.completion_percentage(), 0);
}

#[test]
fn test_roadmap_item_completion_percentage_inprogress() {
    let mut item = RoadmapItem::new("TEST".to_string(), "Test".to_string());
    item.status = ItemStatus::InProgress;
    assert_eq!(item.completion_percentage(), 50);
}

#[test]
fn test_roadmap_item_completion_percentage_review() {
    let mut item = RoadmapItem::new("TEST".to_string(), "Test".to_string());
    item.status = ItemStatus::Review;
    assert_eq!(item.completion_percentage(), 90);
}

#[test]
fn test_roadmap_item_completion_percentage_completed() {
    let mut item = RoadmapItem::new("TEST".to_string(), "Test".to_string());
    item.status = ItemStatus::Completed;
    assert_eq!(item.completion_percentage(), 100);
}

#[test]
fn test_roadmap_item_is_github_synced() {
    let mut item = RoadmapItem::new("TEST".to_string(), "Test".to_string());
    assert!(!item.is_github_synced());

    item.github_issue = Some(123);
    assert!(item.is_github_synced());
}

// ============================================================================
// Tests for Roadmap methods
// ============================================================================

#[test]
fn test_roadmap_new() {
    let roadmap = Roadmap::new(Some("owner/repo".to_string()));
    assert_eq!(roadmap.roadmap_version, "1.0");
    assert!(roadmap.github_enabled);
    assert_eq!(roadmap.github_repo, Some("owner/repo".to_string()));
    assert!(roadmap.roadmap.is_empty());
}

#[test]
fn test_roadmap_default() {
    let roadmap = Roadmap::default();
    assert_eq!(roadmap.roadmap_version, "1.0");
    assert!(roadmap.github_enabled);
    assert!(roadmap.github_repo.is_none());
    assert!(roadmap.roadmap.is_empty());
}

#[test]
fn test_roadmap_upsert_item() {
    let mut roadmap = Roadmap::new(None);
    let item = RoadmapItem::new("TEST-001".to_string(), "Test".to_string());

    roadmap.upsert_item(item.clone());
    assert_eq!(roadmap.roadmap.len(), 1);

    // Upsert same ID should update, not add
    let mut updated = item.clone();
    updated.title = "Updated title".to_string();
    roadmap.upsert_item(updated);
    assert_eq!(roadmap.roadmap.len(), 1);
    assert_eq!(roadmap.roadmap[0].title, "Updated title");
}

#[test]
fn test_roadmap_remove_item() {
    let mut roadmap = Roadmap::new(None);
    roadmap.upsert_item(RoadmapItem::new("TEST-001".to_string(), "Test".to_string()));

    let removed = roadmap.remove_item("TEST-001");
    assert!(removed.is_some());
    assert!(roadmap.roadmap.is_empty());

    // Remove nonexistent
    let not_found = roadmap.remove_item("NONEXISTENT");
    assert!(not_found.is_none());
}

#[test]
fn test_roadmap_find_item_by_github_issue() {
    let mut roadmap = Roadmap::new(None);
    roadmap.upsert_item(RoadmapItem::from_github_issue(42, "Test".to_string()));

    let found = roadmap.find_item_by_github_issue(42);
    assert!(found.is_some());
    assert_eq!(found.unwrap().id, "GH-42");

    let not_found = roadmap.find_item_by_github_issue(999);
    assert!(not_found.is_none());
}

#[test]
fn test_roadmap_yaml_only_items() {
    let mut roadmap = Roadmap::new(None);
    roadmap.upsert_item(RoadmapItem::from_github_issue(42, "GH".to_string()));
    roadmap.upsert_item(RoadmapItem::new("YAML-001".to_string(), "YAML".to_string()));

    let yaml_only = roadmap.yaml_only_items();
    assert_eq!(yaml_only.len(), 1);
    assert_eq!(yaml_only[0].id, "YAML-001");
}

#[test]
fn test_roadmap_epic_items() {
    use crate::models::roadmap::ItemType;

    let mut roadmap = Roadmap::new(None);
    let task = RoadmapItem::new("TASK-001".to_string(), "Task".to_string());
    let mut epic = RoadmapItem::new("EPIC-001".to_string(), "Epic".to_string());
    epic.item_type = ItemType::Epic;

    roadmap.upsert_item(task);
    roadmap.upsert_item(epic);

    let epics = roadmap.epic_items();
    assert_eq!(epics.len(), 1);
    assert_eq!(epics[0].id, "EPIC-001");
}

// ============================================================================
// Tests for ticket title truncation in handle_work_list
// ============================================================================

#[test]
fn test_title_truncation_short() {
    let title = "Short title";
    let truncated = truncate_title_test(title);
    assert_eq!(truncated, "Short title");
}

#[test]
fn test_title_truncation_exactly_40() {
    let title = "This is exactly forty characters long!!!";
    assert_eq!(title.len(), 40);
    let truncated = truncate_title_test(title);
    assert_eq!(truncated, title);
}

#[test]
fn test_title_truncation_over_40() {
    let title = "This is a very long title that exceeds forty characters";
    let truncated = truncate_title_test(title);
    assert_eq!(truncated.len(), 40); // 37 chars + "..."
    assert!(truncated.ends_with("..."));
}

/// Test helper for title truncation
fn truncate_title_test(title: &str) -> String {
    if title.len() > 40 {
        format!("{}...", &title[..37])
    } else {
        title.to_string()
    }
}

// ============================================================================
// Tests for special character handling in titles
// ============================================================================

#[test]
fn test_title_with_special_chars() {
    let (temp_dir, roadmap_path) = setup_temp_roadmap();

    let service = RoadmapService::new(&roadmap_path);
    let item = RoadmapItem::new("TEST-001".to_string(), "Title with: colons".to_string());
    service.upsert_item(item.clone()).expect("Failed to upsert");

    let loaded = service.find_item("TEST-001").unwrap().unwrap();
    assert_eq!(loaded.title, "Title with: colons");

    drop(temp_dir);
}

#[test]
fn test_title_with_unicode() {
    let (temp_dir, roadmap_path) = setup_temp_roadmap();

    let service = RoadmapService::new(&roadmap_path);
    let item = RoadmapItem::new(
        "TEST-001".to_string(),
        "Unicode: \u{2713} \u{2717}".to_string(),
    );
    service.upsert_item(item).expect("Failed to upsert");

    let loaded = service.find_item("TEST-001").unwrap().unwrap();
    assert!(loaded.title.contains("\u{2713}"));

    drop(temp_dir);
}

// ============================================================================
// Tests for long ID warning logic (chars().count() > 50)
// ============================================================================

#[test]
fn test_long_id_warning_threshold() {
    let short_id = "a".repeat(50);
    let long_id = "b".repeat(51);

    assert!(short_id.chars().count() <= 50);
    assert!(long_id.chars().count() > 50);
}

#[test]
fn test_long_id_unicode_chars() {
    // Unicode characters count as 1 char each
    let unicode_id = "\u{1F600}".repeat(51); // 51 emoji
    assert!(unicode_id.chars().count() > 50);

    // But bytes are different
    assert!(unicode_id.len() > 51 * 3); // Each emoji is 4 bytes
}

// ============================================================================
// Tests for status normalization patterns in handle_work_migrate
// ============================================================================

#[test]
fn test_status_normalization_patterns() {
    let patterns = [
        ("status: done", "status: completed"),
        ("status: Done", "status: completed"),
        ("status: DONE", "status: completed"),
        ("status: finished", "status: completed"),
        ("status: in progress", "status: inprogress"),
        ("status: WIP", "status: inprogress"),
        ("status: wip", "status: inprogress"),
        ("status: stuck", "status: blocked"),
        ("status: on-hold", "status: blocked"),
        ("status: todo", "status: planned"),
        ("status: TODO", "status: planned"),
        ("status: open", "status: planned"),
    ];

    for (old, new) in patterns {
        let mut content = format!("test content with {}", old);
        content = content.replace(old, new);
        assert!(
            content.contains(new),
            "Failed to replace {} with {}",
            old,
            new
        );
    }
}