prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
Documentation
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! Tests for commit tracking functionality

#[cfg(test)]
mod tests {
    use crate::abstractions::MockGitOperations;
    use crate::cook::commit_tracker::{CommitConfig, CommitTracker, TrackedCommit};
    use chrono::Utc;
    use std::collections::HashMap;
    use std::path::PathBuf;
    use std::sync::Arc;

    #[tokio::test]
    async fn test_commit_tracker_initialization() {
        let mock_git = Arc::new(MockGitOperations::new());
        mock_git.add_success_response("abc123def456\n").await;

        let mut tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        tracker.initialize().await.unwrap();

        assert_eq!(tracker.initial_head, Some("abc123def456".to_string()));
    }

    #[tokio::test]
    async fn test_has_changes_detection() {
        let mock_git = Arc::new(MockGitOperations::new());

        // First check - has changes
        mock_git
            .add_success_response("M  src/main.rs\nA  src/new.rs\n")
            .await;
        let tracker = CommitTracker::new(mock_git.clone(), PathBuf::from("/test"));
        assert!(tracker.has_changes().await.unwrap());

        // Second check - no changes
        mock_git.add_success_response("").await;
        assert!(!tracker.has_changes().await.unwrap());
    }

    #[tokio::test]
    async fn test_get_commits_between() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock the git log output
        let log_output = "hash1|feat: add feature|John Doe|2024-01-01T12:00:00Z\nfile1.rs\nfile2.rs\n\nhash2|fix: bug fix|Jane Smith|2024-01-02T12:00:00Z\nfile3.rs\n";
        mock_git.add_success_response(log_output).await;

        // Mock diff stats for first commit
        mock_git
            .add_success_response(" 2 files changed, 10 insertions(+), 3 deletions(-)\n")
            .await;
        // Mock diff stats for second commit
        mock_git
            .add_success_response(" 1 file changed, 5 insertions(+), 2 deletions(-)\n")
            .await;

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let commits = tracker.get_commits_between("HEAD~2", "HEAD").await.unwrap();

        assert_eq!(commits.len(), 2);
        assert_eq!(commits[0].hash, "hash1");
        assert_eq!(commits[0].message, "feat: add feature");
        assert_eq!(commits[0].author, "John Doe");
        assert_eq!(commits[0].files_changed.len(), 2);
        assert_eq!(commits[0].insertions, 10);
        assert_eq!(commits[0].deletions, 3);

        assert_eq!(commits[1].hash, "hash2");
        assert_eq!(commits[1].message, "fix: bug fix");
        assert_eq!(commits[1].files_changed.len(), 1);
    }

    #[tokio::test]
    async fn test_create_auto_commit() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock has_changes
        mock_git.add_success_response("M  src/main.rs\n").await;
        // Mock git add
        mock_git.add_success_response("").await;
        // Mock git commit
        mock_git.add_success_response("").await;
        // Mock get HEAD
        mock_git.add_success_response("new_hash\n").await;
        // Mock get commits between
        let log_output =
            "new_hash|Auto-commit: test-step|Test User|2024-01-01T12:00:00Z\nsrc/main.rs\n";
        mock_git.add_success_response(log_output).await;
        // Mock diff stats
        mock_git
            .add_success_response(" 1 file changed, 5 insertions(+), 2 deletions(-)\n")
            .await;

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let variables = HashMap::new();
        let commit = tracker
            .create_auto_commit("test-step", None, &variables, None)
            .await
            .unwrap();

        assert_eq!(commit.hash, "new_hash");
        assert_eq!(commit.message, "Auto-commit: test-step");
        assert_eq!(commit.step_name, "test-step");
    }

    #[tokio::test]
    async fn test_message_template_interpolation() {
        let mock_git = Arc::new(MockGitOperations::new());
        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));

        let mut variables = HashMap::new();
        variables.insert("item".to_string(), "user.py".to_string());
        variables.insert("feature".to_string(), "authentication".to_string());

        let message = tracker
            .interpolate_template(
                "feat: modernize ${item} for ${feature} in ${step.name}",
                "refactor-step",
                &variables,
            )
            .unwrap();

        assert_eq!(
            message,
            "feat: modernize user.py for authentication in refactor-step"
        );
    }

    #[tokio::test]
    async fn test_commit_message_validation() {
        let mock_git = Arc::new(MockGitOperations::new());
        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));

        let pattern = r"^(feat|fix|docs|style|refactor|test|chore)(\([a-z]+\))?: .+$";

        // Valid messages
        assert!(tracker
            .validate_message("feat: add new feature", pattern)
            .is_ok());
        assert!(tracker
            .validate_message("fix(auth): resolve login issue", pattern)
            .is_ok());
        assert!(tracker
            .validate_message("docs: update README", pattern)
            .is_ok());

        // Invalid messages
        assert!(tracker.validate_message("bad message", pattern).is_err());
        assert!(tracker
            .validate_message("Feature: wrong case", pattern)
            .is_err());
    }

    #[tokio::test]
    async fn test_track_step_commits() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock get commits between
        let log_output = "hash1|feat: step change|Dev|2024-01-01T12:00:00Z\nfile1.rs\n";
        mock_git.add_success_response(log_output).await;
        // Mock diff stats
        mock_git
            .add_success_response(" 1 file changed, 10 insertions(+), 0 deletions(-)\n")
            .await;

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let commits = tracker
            .track_step_commits("test-step", "old_hash", "new_hash")
            .await
            .unwrap();

        assert_eq!(commits.len(), 1);
        assert_eq!(commits[0].step_name, "test-step");

        // Check that commits were added to tracked commits
        let all_commits = tracker.get_all_commits().await;
        assert_eq!(all_commits.len(), 1);
        assert_eq!(all_commits[0].step_name, "test-step");
    }

    #[tokio::test]
    async fn test_squash_commits() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Create test commits
        let commits = vec![
            TrackedCommit {
                hash: "hash1".to_string(),
                message: "commit 1".to_string(),
                author: "test".to_string(),
                timestamp: Utc::now(),
                files_changed: vec![PathBuf::from("file1.rs")],
                insertions: 10,
                deletions: 5,
                step_name: "step1".to_string(),
                agent_id: None,
            },
            TrackedCommit {
                hash: "hash2".to_string(),
                message: "commit 2".to_string(),
                author: "test".to_string(),
                timestamp: Utc::now(),
                files_changed: vec![PathBuf::from("file2.rs")],
                insertions: 20,
                deletions: 3,
                step_name: "step2".to_string(),
                agent_id: None,
            },
        ];

        // Mock get parent
        mock_git.add_success_response("parent_hash\n").await;
        // Mock reset
        mock_git.add_success_response("").await;
        // Mock commit
        mock_git.add_success_response("").await;
        // Mock get HEAD
        mock_git.add_success_response("squashed_hash\n").await;

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let squashed = tracker
            .squash_commits(&commits, "feat: squashed changes")
            .await
            .unwrap();

        assert_eq!(squashed, "squashed_hash");
    }

    #[tokio::test]
    async fn test_commit_config_serialization() {
        let config = CommitConfig {
            message_template: Some("feat: ${item}".to_string()),
            message_pattern: Some(r"^feat:".to_string()),
            sign: true,
            author: Some("Test Author".to_string()),
            include_files: Some(vec!["*.rs".to_string()]),
            exclude_files: Some(vec!["*.tmp".to_string()]),
            squash: false,
        };

        let json = serde_json::to_string(&config).unwrap();
        let deserialized: CommitConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.message_template, config.message_template);
        assert_eq!(deserialized.sign, config.sign);
        assert_eq!(deserialized.author, config.author);
    }

    #[tokio::test]
    async fn test_commit_tracking_result() {
        use crate::cook::commit_tracker::CommitTrackingResult;

        let commits = vec![
            TrackedCommit {
                hash: "hash1".to_string(),
                message: "commit 1".to_string(),
                author: "test".to_string(),
                timestamp: Utc::now(),
                files_changed: vec![PathBuf::from("file1.rs"), PathBuf::from("file2.rs")],
                insertions: 10,
                deletions: 5,
                step_name: "step1".to_string(),
                agent_id: None,
            },
            TrackedCommit {
                hash: "hash2".to_string(),
                message: "commit 2".to_string(),
                author: "test".to_string(),
                timestamp: Utc::now(),
                files_changed: vec![PathBuf::from("file2.rs"), PathBuf::from("file3.rs")],
                insertions: 20,
                deletions: 3,
                step_name: "step2".to_string(),
                agent_id: None,
            },
        ];

        let result = CommitTrackingResult::from_commits(commits);

        assert_eq!(result.commits.len(), 2);
        assert_eq!(result.total_files_changed, 3); // file1, file2, file3 (deduplicated)
        assert_eq!(result.total_insertions, 30);
        assert_eq!(result.total_deletions, 8);
    }

    // Tests for parse_git_status_line
    #[test]
    fn test_parse_git_status_line_valid() {
        let result = CommitTracker::parse_git_status_line("M  src/file.rs");
        assert_eq!(result, Some("src/file.rs".to_string()));
    }

    #[test]
    fn test_parse_git_status_line_short() {
        let result = CommitTracker::parse_git_status_line("M ");
        assert_eq!(result, None);
    }

    #[test]
    fn test_parse_git_status_line_empty() {
        let result = CommitTracker::parse_git_status_line("");
        assert_eq!(result, None);
    }

    #[test]
    fn test_parse_git_status_line_with_spaces() {
        let result = CommitTracker::parse_git_status_line("A  path with spaces/file.rs");
        assert_eq!(result, Some("path with spaces/file.rs".to_string()));
    }

    // Tests for should_include_file
    #[test]
    fn test_should_include_file_no_patterns() {
        let result = CommitTracker::should_include_file("file.rs", &[]);
        assert!(!result);
    }

    #[test]
    fn test_should_include_file_single_match() {
        let result = CommitTracker::should_include_file("file.rs", &["*.rs".to_string()]);
        assert!(result);
    }

    #[test]
    fn test_should_include_file_single_no_match() {
        let result = CommitTracker::should_include_file("file.txt", &["*.rs".to_string()]);
        assert!(!result);
    }

    #[test]
    fn test_should_include_file_multiple_first_matches() {
        let result = CommitTracker::should_include_file(
            "file.rs",
            &["*.rs".to_string(), "*.md".to_string()],
        );
        assert!(result);
    }

    #[test]
    fn test_should_include_file_multiple_second_matches() {
        let result = CommitTracker::should_include_file(
            "file.md",
            &["*.rs".to_string(), "*.md".to_string()],
        );
        assert!(result);
    }

    #[test]
    fn test_should_include_file_invalid_pattern() {
        // Invalid pattern should be skipped gracefully
        let result = CommitTracker::should_include_file("file.rs", &["[invalid".to_string()]);
        assert!(!result);
    }

    // Tests for should_exclude_file
    #[test]
    fn test_should_exclude_file_no_patterns() {
        let result = CommitTracker::should_exclude_file("file.tmp", &[]);
        assert!(!result);
    }

    #[test]
    fn test_should_exclude_file_single_match() {
        let result = CommitTracker::should_exclude_file("file.tmp", &["*.tmp".to_string()]);
        assert!(result);
    }

    #[test]
    fn test_should_exclude_file_single_no_match() {
        let result = CommitTracker::should_exclude_file("file.rs", &["*.tmp".to_string()]);
        assert!(!result);
    }

    #[test]
    fn test_should_exclude_file_multiple_with_match() {
        let result = CommitTracker::should_exclude_file(
            "file.log",
            &["*.tmp".to_string(), "*.log".to_string()],
        );
        assert!(result);
    }

    #[test]
    fn test_should_exclude_file_invalid_pattern() {
        // Invalid pattern should be skipped gracefully
        let result = CommitTracker::should_exclude_file("file.tmp", &["[invalid".to_string()]);
        assert!(!result);
    }

    // Tests for should_stage_file
    #[test]
    fn test_should_stage_file_no_config() {
        let result = CommitTracker::should_stage_file("file.rs", None);
        assert!(result);
    }

    #[test]
    fn test_should_stage_file_only_include_matching() {
        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: Some(vec!["*.rs".to_string()]),
            exclude_files: None,
            squash: false,
        };
        let result = CommitTracker::should_stage_file("file.rs", Some(&config));
        assert!(result);
    }

    #[test]
    fn test_should_stage_file_only_include_not_matching() {
        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: Some(vec!["*.rs".to_string()]),
            exclude_files: None,
            squash: false,
        };
        let result = CommitTracker::should_stage_file("file.md", Some(&config));
        assert!(!result);
    }

    #[test]
    fn test_should_stage_file_only_exclude_matching() {
        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: None,
            exclude_files: Some(vec!["*.tmp".to_string()]),
            squash: false,
        };
        let result = CommitTracker::should_stage_file("file.tmp", Some(&config));
        assert!(!result);
    }

    #[test]
    fn test_should_stage_file_only_exclude_not_matching() {
        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: None,
            exclude_files: Some(vec!["*.tmp".to_string()]),
            squash: false,
        };
        let result = CommitTracker::should_stage_file("file.rs", Some(&config));
        assert!(result);
    }

    #[test]
    fn test_should_stage_file_include_and_exclude_passes() {
        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: Some(vec!["*.rs".to_string()]),
            exclude_files: Some(vec!["*_test.rs".to_string()]),
            squash: false,
        };
        let result = CommitTracker::should_stage_file("main.rs", Some(&config));
        assert!(result);
    }

    #[test]
    fn test_should_stage_file_include_and_exclude_blocked() {
        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: Some(vec!["*.rs".to_string()]),
            exclude_files: Some(vec!["*_test.rs".to_string()]),
            squash: false,
        };
        let result = CommitTracker::should_stage_file("foo_test.rs", Some(&config));
        assert!(!result);
    }

    // Tests for get_files_to_stage
    #[tokio::test]
    async fn test_get_files_to_stage_no_config() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock git status output
        mock_git
            .add_success_response("M  src/main.rs\nA  src/lib.rs\nD  old.rs\n")
            .await;

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let files = tracker.get_files_to_stage(None).await.unwrap();

        assert_eq!(files.len(), 3);
        assert!(files.contains(&"src/main.rs".to_string()));
        assert!(files.contains(&"src/lib.rs".to_string()));
        assert!(files.contains(&"old.rs".to_string()));
    }

    #[tokio::test]
    async fn test_get_files_to_stage_include_patterns_match() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock git status output
        mock_git
            .add_success_response("M  src/main.rs\nA  src/lib.rs\nM  README.md\n")
            .await;

        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: Some(vec!["*.rs".to_string()]),
            exclude_files: None,
            squash: false,
        };

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let files = tracker.get_files_to_stage(Some(&config)).await.unwrap();

        assert_eq!(files.len(), 2);
        assert!(files.contains(&"src/main.rs".to_string()));
        assert!(files.contains(&"src/lib.rs".to_string()));
        assert!(!files.contains(&"README.md".to_string()));
    }

    #[tokio::test]
    async fn test_get_files_to_stage_include_patterns_no_match() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock git status output
        mock_git
            .add_success_response("M  README.md\nA  docs/guide.md\n")
            .await;

        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: Some(vec!["*.rs".to_string()]),
            exclude_files: None,
            squash: false,
        };

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let files = tracker.get_files_to_stage(Some(&config)).await.unwrap();

        assert_eq!(files.len(), 0);
    }

    #[tokio::test]
    async fn test_get_files_to_stage_exclude_patterns_block() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock git status output
        mock_git
            .add_success_response("M  src/main.rs\nA  test.tmp\nM  cache.log\n")
            .await;

        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: None,
            exclude_files: Some(vec!["*.tmp".to_string(), "*.log".to_string()]),
            squash: false,
        };

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let files = tracker.get_files_to_stage(Some(&config)).await.unwrap();

        assert_eq!(files.len(), 1);
        assert!(files.contains(&"src/main.rs".to_string()));
        assert!(!files.contains(&"test.tmp".to_string()));
        assert!(!files.contains(&"cache.log".to_string()));
    }

    #[tokio::test]
    async fn test_get_files_to_stage_include_and_exclude_interaction() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock git status output
        mock_git
            .add_success_response("M  src/main.rs\nA  src/test.rs\nM  tests/helper.rs\n")
            .await;

        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: Some(vec!["src/**/*.rs".to_string()]),
            exclude_files: Some(vec!["**/test*.rs".to_string()]),
            squash: false,
        };

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let files = tracker.get_files_to_stage(Some(&config)).await.unwrap();

        assert_eq!(files.len(), 1);
        assert!(files.contains(&"src/main.rs".to_string()));
        assert!(!files.contains(&"src/test.rs".to_string())); // excluded
        assert!(!files.contains(&"tests/helper.rs".to_string())); // not included
    }

    #[tokio::test]
    async fn test_get_files_to_stage_empty_status() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock empty git status output
        mock_git.add_success_response("").await;

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let files = tracker.get_files_to_stage(None).await.unwrap();

        assert_eq!(files.len(), 0);
    }

    #[tokio::test]
    async fn test_get_files_to_stage_malformed_lines() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock git status with malformed lines (< 3 chars)
        mock_git
            .add_success_response("M  src/main.rs\nM \nA  src/lib.rs\n\n")
            .await;

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let files = tracker.get_files_to_stage(None).await.unwrap();

        // Should skip malformed lines
        assert_eq!(files.len(), 2);
        assert!(files.contains(&"src/main.rs".to_string()));
        assert!(files.contains(&"src/lib.rs".to_string()));
    }

    #[tokio::test]
    async fn test_get_files_to_stage_invalid_glob_pattern() {
        let mock_git = Arc::new(MockGitOperations::new());

        // Mock git status output
        mock_git
            .add_success_response("M  src/main.rs\nA  src/lib.rs\n")
            .await;

        // Invalid glob pattern with unbalanced brackets
        let config = CommitConfig {
            message_template: None,
            message_pattern: None,
            sign: false,
            author: None,
            include_files: Some(vec!["[invalid".to_string()]),
            exclude_files: None,
            squash: false,
        };

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let files = tracker.get_files_to_stage(Some(&config)).await.unwrap();

        // Invalid patterns should be skipped, resulting in no matches
        assert_eq!(files.len(), 0);
    }
}