cresca 0.3.0

A tool to partially review the pull requests.
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
mod common;

use common::TempGitRepo;

/// Test that `cresca review` creates a review branch with the correct name.
#[test]
fn test_review_creates_branch() {
    let repo = TempGitRepo::new();

    // Create a develop branch with some changes
    repo.create_branch("develop");
    repo.write_file("feature.txt", "new feature");
    repo.git(&["add", "."]);
    repo.commit("Add feature");
    repo.git(&["push", "-u", "origin", "develop"]);

    // Switch back to main
    repo.switch_branch("main");

    // Run cresca review
    let output = repo.run_cresca(&["review", "main", "develop"]);
    assert!(
        output.status.success(),
        "cresca review should succeed\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify we're now on the review branch
    let current = repo.current_branch();
    assert_eq!(current, "review-main-develop");
}

/// Test that `cresca review` shows the diff as unstaged changes.
#[test]
fn test_review_shows_diff() {
    let repo = TempGitRepo::new();

    // Create a develop branch with some changes
    repo.create_branch("develop");
    repo.write_file("feature.txt", "new feature content");
    repo.git(&["add", "."]);
    repo.commit("Add feature");
    repo.git(&["push", "-u", "origin", "develop"]);

    // Switch back to main
    repo.switch_branch("main");

    // Run cresca review
    repo.run_cresca(&["review", "main", "develop"]);

    // Verify the changes are shown as unstaged
    assert!(
        repo.has_uncommitted_changes(),
        "Should have uncommitted changes"
    );

    // Check status for new files
    let status = repo.git(&["status", "--porcelain"]);
    let status_str = String::from_utf8_lossy(&status.stdout);
    assert!(
        status_str.contains("feature.txt"),
        "feature.txt should appear in status"
    );
}

/// Test that `cresca approve` commits staged changes and discards unstaged ones.
#[test]
fn test_approve_commits_staged() {
    let repo = TempGitRepo::new();

    // Setup: create develop with two files
    repo.create_branch("develop");
    repo.write_file("reviewed.txt", "reviewed content");
    repo.write_file("not_reviewed.txt", "not reviewed content");
    repo.git(&["add", "."]);
    repo.commit("Add features");
    repo.git(&["push", "-u", "origin", "develop"]);

    // Switch back to main and run review
    repo.switch_branch("main");
    repo.run_cresca(&["review", "main", "develop"]);

    // Stage only one file (simulating partial review)
    repo.git(&["add", "reviewed.txt"]);

    // Run approve
    let output = repo.run_cresca(&["approve"]);
    assert!(
        output.status.success(),
        "cresca approve should succeed\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify: reviewed.txt should be committed
    let files_in_head = repo.git(&["ls-tree", "--name-only", "HEAD"]);
    let files_str = String::from_utf8_lossy(&files_in_head.stdout);
    assert!(
        files_str.contains("reviewed.txt"),
        "reviewed.txt should be committed"
    );

    // Verify: not_reviewed.txt should NOT exist (discarded)
    let not_reviewed_path = repo.path().join("not_reviewed.txt");
    assert!(
        !not_reviewed_path.exists(),
        "not_reviewed.txt should be discarded"
    );

    // Verify: working directory is clean
    assert!(
        !repo.has_uncommitted_changes(),
        "Working directory should be clean after approve"
    );
}

/// Test that `cresca approve` fails on a non-review branch.
#[test]
fn test_approve_on_non_review_branch() {
    let repo = TempGitRepo::new();

    // Try to approve on main (not a review branch)
    let output = repo.run_cresca(&["approve"]);

    assert!(
        !output.status.success(),
        "cresca approve should fail on non-review branch"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("error") || stderr.contains("Not on a review branch"),
        "Should show error message about not being on review branch"
    );
}

/// Test that `cresca review` fails with uncommitted changes.
#[test]
fn test_review_with_uncommitted_changes() {
    let repo = TempGitRepo::new();

    // Create develop branch and push it
    repo.create_branch("develop");
    repo.git(&["push", "-u", "origin", "develop"]);
    repo.switch_branch("main");

    // Create uncommitted changes
    repo.write_file("uncommitted.txt", "uncommitted content");

    // Try to run review
    let output = repo.run_cresca(&["review", "main", "develop"]);

    assert!(
        !output.status.success(),
        "cresca review should fail with uncommitted changes"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("error") || stderr.contains("Uncommitted"),
        "Should show error about uncommitted changes"
    );
}

/// Test that running review twice updates the review branch correctly.
#[test]
fn test_review_updates_existing_branch() {
    let repo = TempGitRepo::new();

    // Create develop branch with initial change
    repo.create_branch("develop");
    repo.write_file("file1.txt", "content 1");
    repo.git(&["add", "."]);
    repo.commit("Add file1");
    repo.git(&["push", "-u", "origin", "develop"]);

    // First review
    repo.switch_branch("main");
    repo.run_cresca(&["review", "main", "develop"]);

    // Approve all changes
    repo.git(&["add", "."]);
    repo.run_cresca(&["approve"]);

    // Add more changes to develop
    repo.switch_branch("develop");
    repo.write_file("file2.txt", "content 2");
    repo.git(&["add", "."]);
    repo.commit("Add file2");
    repo.git(&["push", "origin", "develop"]);

    // Second review (from the review branch)
    repo.switch_branch("review-main-develop");
    repo.run_cresca(&["review", "main", "develop"]);

    // Verify: file1.txt should still be present (previously approved)
    assert!(
        repo.path().join("file1.txt").exists(),
        "file1.txt should exist from previous approval"
    );

    // Verify: file2.txt should appear as new change
    let status = repo.git(&["status", "--porcelain"]);
    let status_str = String::from_utf8_lossy(&status.stdout);
    assert!(
        status_str.contains("file2.txt"),
        "file2.txt should appear as new unreviewed change"
    );
}

/// Test that `cresca review --skip-to` auto-approves earlier commits.
#[test]
fn test_review_with_skip_to_option() {
    let repo = TempGitRepo::new();

    // Create develop branch with multiple commits
    repo.create_branch("develop");
    repo.write_file("file1.txt", "content 1");
    repo.git(&["add", "."]);
    repo.commit("Add file1");

    repo.write_file("file2.txt", "content 2");
    repo.git(&["add", "."]);
    repo.commit("Add file2");

    repo.write_file("file3.txt", "content 3");
    repo.git(&["add", "."]);
    repo.commit("Add file3");

    repo.git(&["push", "-u", "origin", "develop"]);

    // Get the hash of second commit (file2)
    let log_output = repo.git(&["log", "--oneline", "main..develop"]);
    let log_str = String::from_utf8_lossy(&log_output.stdout);
    let commits: Vec<&str> = log_str.lines().collect();
    // commits[0] = file3, commits[1] = file2, commits[2] = file1
    let file2_hash = commits[1].split_whitespace().next().unwrap();

    // Switch back to main
    repo.switch_branch("main");

    // Run cresca review with --skip-to option (skip to file2 commit)
    let output = repo.run_cresca(&["review", "main", "develop", "--skip-to", file2_hash]);
    assert!(
        output.status.success(),
        "cresca review --skip-to should succeed\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify: file1.txt should be auto-approved (committed)
    let files_in_head = repo.git(&["ls-tree", "--name-only", "HEAD"]);
    let files_str = String::from_utf8_lossy(&files_in_head.stdout);
    assert!(
        files_str.contains("file1.txt"),
        "file1.txt should be auto-approved and committed"
    );

    // Verify: file2.txt and file3.txt should be unstaged changes
    let status = repo.git(&["status", "--porcelain"]);
    let status_str = String::from_utf8_lossy(&status.stdout);
    assert!(
        status_str.contains("file2.txt"),
        "file2.txt should be an unstaged change"
    );
    assert!(
        status_str.contains("file3.txt"),
        "file3.txt should be an unstaged change"
    );
}

/// Test that `cresca review --skip-to` with already approved commits works correctly.
#[test]
fn test_review_with_skip_to_already_approved() {
    let repo = TempGitRepo::new();

    // Create develop branch with multiple commits
    repo.create_branch("develop");
    repo.write_file("file1.txt", "content 1");
    repo.git(&["add", "."]);
    repo.commit("Add file1");

    repo.write_file("file2.txt", "content 2");
    repo.git(&["add", "."]);
    repo.commit("Add file2");

    repo.git(&["push", "-u", "origin", "develop"]);

    // Get hashes
    let log_output = repo.git(&["log", "--oneline", "main..develop"]);
    let log_str = String::from_utf8_lossy(&log_output.stdout);
    let commits: Vec<&str> = log_str.lines().collect();
    let file2_hash = commits[0].split_whitespace().next().unwrap();
    let file1_hash = commits[1].split_whitespace().next().unwrap();

    // Switch back to main and do first review with --skip-to file2 (file1 auto-approved)
    repo.switch_branch("main");
    repo.run_cresca(&["review", "main", "develop", "--skip-to", file2_hash]);

    // Approve file2
    repo.git(&["add", "."]);
    repo.run_cresca(&["approve"]);

    // Now try to run review again with --skip-to file1 (file1 already committed)
    let output = repo.run_cresca(&["review", "main", "develop", "--skip-to", file1_hash]);
    assert!(
        output.status.success(),
        "cresca review --skip-to with already approved commits should succeed\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

/// Test that `cresca status` shows remaining diff statistics on a review branch.
#[test]
fn test_status_shows_diff_stats() {
    let repo = TempGitRepo::new();

    // Create a develop branch with some changes
    repo.create_branch("develop");
    repo.write_file("feature1.txt", "new feature 1");
    repo.write_file("feature2.txt", "new feature 2");
    repo.git(&["add", "."]);
    repo.commit("Add features");
    repo.git(&["push", "-u", "origin", "develop"]);

    // Switch back to main and run review
    repo.switch_branch("main");
    repo.run_cresca(&["review", "main", "develop"]);

    // Run status
    let output = repo.run_cresca(&["status"]);
    assert!(
        output.status.success(),
        "cresca status should succeed\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("Review status"),
        "Should show review status header"
    );
    assert!(
        stdout.contains("Remaining diff to develop"),
        "Should mention develop branch"
    );
    assert!(stdout.contains("2 file(s)"), "Should show 2 files changed");
    assert!(stdout.contains("feature1.txt"), "Should list feature1.txt");
    assert!(stdout.contains("feature2.txt"), "Should list feature2.txt");
}

/// Test that `cresca status` fails on a non-review branch.
#[test]
fn test_status_on_non_review_branch() {
    let repo = TempGitRepo::new();

    // Try to run status on main (not a review branch)
    let output = repo.run_cresca(&["status"]);

    assert!(
        !output.status.success(),
        "cresca status should fail on non-review branch"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("error") || stderr.contains("Not on a review branch"),
        "Should show error message about not being on review branch"
    );
}

/// Test that `cresca status` updates after partial approval.
#[test]
fn test_status_after_partial_approval() {
    let repo = TempGitRepo::new();

    // Create develop branch with multiple files
    repo.create_branch("develop");
    repo.write_file("file1.txt", "content 1");
    repo.write_file("file2.txt", "content 2");
    repo.write_file("file3.txt", "content 3");
    repo.git(&["add", "."]);
    repo.commit("Add three files");
    repo.git(&["push", "-u", "origin", "develop"]);

    // Switch back to main and run review
    repo.switch_branch("main");
    repo.run_cresca(&["review", "main", "develop"]);

    // Initial status should show 3 files
    let output = repo.run_cresca(&["status"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("3 file(s)"),
        "Should initially show 3 files, got: {}",
        stdout
    );

    // Approve only one file
    repo.git(&["add", "file1.txt"]);
    repo.run_cresca(&["approve"]);

    // Run review again to see remaining changes
    repo.run_cresca(&["review", "main", "develop"]);

    // Status should show remaining files (file2.txt and file3.txt)
    let output = repo.run_cresca(&["status"]);
    let stdout = String::from_utf8_lossy(&output.stdout);

    // After partial approval, approved file should not appear in unstaged diff
    assert!(
        stdout.contains("file2.txt"),
        "file2.txt should be in remaining files, got: {}",
        stdout
    );
    assert!(
        stdout.contains("file3.txt"),
        "file3.txt should be in remaining files, got: {}",
        stdout
    );
}

/// Test that `cresca review --stop-at` excludes later commits from review.
#[test]
fn test_review_with_stop_at_option() {
    let repo = TempGitRepo::new();

    // Create develop branch with multiple commits
    repo.create_branch("develop");
    repo.write_file("file1.txt", "content 1");
    repo.git(&["add", "."]);
    repo.commit("Add file1");

    repo.write_file("file2.txt", "content 2");
    repo.git(&["add", "."]);
    repo.commit("Add file2");

    repo.write_file("file3.txt", "content 3");
    repo.git(&["add", "."]);
    repo.commit("Add file3");

    repo.git(&["push", "-u", "origin", "develop"]);

    // Get the hash of second commit (file2)
    let log_output = repo.git(&["log", "--oneline", "main..develop"]);
    let log_str = String::from_utf8_lossy(&log_output.stdout);
    let commits: Vec<&str> = log_str.lines().collect();
    // commits[0] = file3, commits[1] = file2, commits[2] = file1
    let file2_hash = commits[1].split_whitespace().next().unwrap();

    // Switch back to main
    repo.switch_branch("main");

    // Run cresca review with --stop-at option (stop at file2 commit)
    let output = repo.run_cresca(&["review", "main", "develop", "--stop-at", file2_hash]);
    assert!(
        output.status.success(),
        "cresca review --stop-at should succeed\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify: file1.txt and file2.txt should be unstaged changes (in review)
    let status = repo.git(&["status", "--porcelain"]);
    let status_str = String::from_utf8_lossy(&status.stdout);
    assert!(
        status_str.contains("file1.txt"),
        "file1.txt should be an unstaged change"
    );
    assert!(
        status_str.contains("file2.txt"),
        "file2.txt should be an unstaged change"
    );

    // Verify: file3.txt should NOT appear (excluded from review)
    assert!(
        !status_str.contains("file3.txt"),
        "file3.txt should NOT appear (excluded by --stop-at)"
    );
}

/// Test that `cresca review --skip-to --stop-at` limits review to specific range.
#[test]
fn test_review_with_skip_to_and_stop_at() {
    let repo = TempGitRepo::new();

    // Create develop branch with multiple commits: A -> B -> C -> D
    repo.create_branch("develop");
    repo.write_file("fileA.txt", "content A");
    repo.git(&["add", "."]);
    repo.commit("Add fileA");

    repo.write_file("fileB.txt", "content B");
    repo.git(&["add", "."]);
    repo.commit("Add fileB");

    repo.write_file("fileC.txt", "content C");
    repo.git(&["add", "."]);
    repo.commit("Add fileC");

    repo.write_file("fileD.txt", "content D");
    repo.git(&["add", "."]);
    repo.commit("Add fileD");

    repo.git(&["push", "-u", "origin", "develop"]);

    // Get commit hashes
    // Log order: D (newest) -> C -> B -> A (oldest)
    let log_output = repo.git(&["log", "--oneline", "main..develop"]);
    let log_str = String::from_utf8_lossy(&log_output.stdout);
    let commits: Vec<&str> = log_str.lines().collect();
    let _file_d_hash = commits[0].split_whitespace().next().unwrap();
    let file_c_hash = commits[1].split_whitespace().next().unwrap();
    let file_b_hash = commits[2].split_whitespace().next().unwrap();
    let _file_a_hash = commits[3].split_whitespace().next().unwrap();

    // Switch back to main
    repo.switch_branch("main");

    // Run cresca review: skip A, review B and C, exclude D
    let output = repo.run_cresca(&[
        "review",
        "main",
        "develop",
        "--skip-to",
        file_b_hash,
        "--stop-at",
        file_c_hash,
    ]);
    assert!(
        output.status.success(),
        "cresca review --skip-to --stop-at should succeed\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify: fileA.txt should be auto-approved (committed)
    let files_in_head = repo.git(&["ls-tree", "--name-only", "HEAD"]);
    let files_str = String::from_utf8_lossy(&files_in_head.stdout);
    assert!(
        files_str.contains("fileA.txt"),
        "fileA.txt should be auto-approved and committed"
    );

    // Verify: fileB.txt and fileC.txt should be unstaged changes (in review)
    let status = repo.git(&["status", "--porcelain"]);
    let status_str = String::from_utf8_lossy(&status.stdout);
    assert!(
        status_str.contains("fileB.txt"),
        "fileB.txt should be an unstaged change"
    );
    assert!(
        status_str.contains("fileC.txt"),
        "fileC.txt should be an unstaged change"
    );

    // Verify: fileD.txt should NOT appear (excluded by --stop-at)
    assert!(
        !status_str.contains("fileD.txt"),
        "fileD.txt should NOT appear (excluded by --stop-at), got: {}",
        status_str
    );

    // Additional check: fileD.txt should not be committed either
    assert!(
        !files_str.contains("fileD.txt"),
        "fileD.txt should not be in HEAD"
    );
}

/// Test that `cresca review --stop-at` fails with invalid commit hash.
#[test]
fn test_review_with_invalid_stop_at() {
    let repo = TempGitRepo::new();

    // Create develop branch with a commit
    repo.create_branch("develop");
    repo.write_file("file1.txt", "content 1");
    repo.git(&["add", "."]);
    repo.commit("Add file1");
    repo.git(&["push", "-u", "origin", "develop"]);

    // Switch back to main
    repo.switch_branch("main");

    // Run cresca review with invalid --stop-at hash
    let output = repo.run_cresca(&["review", "main", "develop", "--stop-at", "invalidhash"]);

    assert!(
        !output.status.success(),
        "cresca review --stop-at with invalid hash should fail"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("error") && stderr.contains("invalidhash"),
        "Should show error about invalid commit hash, got: {}",
        stderr
    );
}

/// Test that `cresca review` fails when --stop-at is before --skip-to.
#[test]
fn test_review_with_stop_at_before_skip_to() {
    let repo = TempGitRepo::new();

    // Create develop branch with multiple commits
    repo.create_branch("develop");
    repo.write_file("file1.txt", "content 1");
    repo.git(&["add", "."]);
    repo.commit("Add file1");

    repo.write_file("file2.txt", "content 2");
    repo.git(&["add", "."]);
    repo.commit("Add file2");

    repo.write_file("file3.txt", "content 3");
    repo.git(&["add", "."]);
    repo.commit("Add file3");

    repo.git(&["push", "-u", "origin", "develop"]);

    // Get commit hashes: file3 (newest), file2, file1 (oldest)
    let log_output = repo.git(&["log", "--oneline", "main..develop"]);
    let log_str = String::from_utf8_lossy(&log_output.stdout);
    let commits: Vec<&str> = log_str.lines().collect();
    let file3_hash = commits[0].split_whitespace().next().unwrap();
    let file1_hash = commits[2].split_whitespace().next().unwrap();

    // Switch back to main
    repo.switch_branch("main");

    // Run cresca review with --stop-at BEFORE --skip-to (invalid)
    let output = repo.run_cresca(&[
        "review",
        "main",
        "develop",
        "--skip-to",
        file3_hash,
        "--stop-at",
        file1_hash,
    ]);

    assert!(
        !output.status.success(),
        "cresca review should fail when --stop-at is before --skip-to"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("error") && stderr.contains("--stop-at"),
        "Should show error about --stop-at being before --skip-to, got: {}",
        stderr
    );
}