cursus 0.3.1

Library crate for the cursus release management CLI
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
//! Integration tests for the `prepare` command.

mod common;

use std::process::ExitCode;

use common::{
	temp_git_repo, temp_git_repo_with_cargo_workspace, temp_git_repo_with_project, write_changeset,
};
use cursus::model::config::PackageManager;
use cursus::test_logging::{init_test_logger, take_logs};

#[tokio::test]
async fn prepare_fails_when_no_config() {
	let dir = temp_git_repo();
	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;

	assert!(result.is_err());
	let err = result.unwrap_err();
	assert!(
		err.to_string().contains("No configuration found"),
		"Expected 'No configuration found' error, got: {err}"
	);
}

#[tokio::test]
async fn prepare_with_no_changesets_is_noop() {
	init_test_logger();
	let _ = take_logs();
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;

	assert!(result.is_ok());
	assert_eq!(result.unwrap(), ExitCode::SUCCESS);

	let logs = take_logs();
	assert!(
		logs.iter()
			.any(|(level, m)| *level == log::Level::Info
				&& m.contains("No pending changesets found")),
		"Expected info 'No pending changesets found' log, got: {logs:?}"
	);
}

#[tokio::test]
async fn prepare_with_single_changeset_cargo() {
	init_test_logger();
	let _ = take_logs();
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	write_changeset(
		dir.path(),
		"test-change.md",
		"+++\ntest-project = \"minor\"\n+++\n\nAdded a feature\n",
	);

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok());
	assert_eq!(result.unwrap(), ExitCode::SUCCESS);

	let logs = take_logs();
	assert!(
		logs.iter().any(|(level, m)| *level == log::Level::Info
			&& m.contains("test-project: 0.1.0 -> 0.2.0 (minor)")),
		"Expected info version bump summary log, got: {logs:?}"
	);

	// Verify version was bumped
	let cargo_toml = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
	assert!(
		cargo_toml.contains("version = \"0.2.0\""),
		"Expected version 0.2.0, got: {cargo_toml}"
	);

	// Verify changeset was deleted
	assert!(
		!dir.path().join(".cursus/test-change.md").exists(),
		"Changeset file should be deleted"
	);

	// Verify changelog was created
	let today = cursus::utils::today_iso_date();
	let changelog = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();
	assert!(
		changelog.contains(&format!("## 0.2.0 - {today}")),
		"Changelog should contain version header with date, got: {changelog}"
	);
	assert!(
		changelog.contains("Added a feature"),
		"Changelog should contain the message, got: {changelog}"
	);
}

#[tokio::test]
async fn prepare_with_single_changeset_npm() {
	init_test_logger();
	let _ = take_logs();
	let dir = temp_git_repo_with_project(PackageManager::Npm).await;
	write_changeset(
		dir.path(),
		"test-change.md",
		"+++\ntest-project = \"patch\"\n+++\n\nFixed a bug\n",
	);

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok());
	assert_eq!(result.unwrap(), ExitCode::SUCCESS);

	let logs = take_logs();
	assert!(
		logs.iter().any(|(level, m)| *level == log::Level::Info
			&& m.contains("test-project: 0.1.0 -> 0.1.1 (patch)")),
		"Expected info version bump summary log, got: {logs:?}"
	);

	// Verify version was bumped
	let pkg_json = std::fs::read_to_string(dir.path().join("package.json")).unwrap();
	assert!(
		pkg_json.contains("\"0.1.1\""),
		"Expected version 0.1.1, got: {pkg_json}"
	);

	// Verify changeset was deleted
	assert!(!dir.path().join(".cursus/test-change.md").exists());
}

#[tokio::test]
async fn prepare_aggregates_to_highest_change_type() {
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	write_changeset(
		dir.path(),
		"change-1.md",
		"+++\ntest-project = \"patch\"\n+++\n\nBug fix\n",
	);
	write_changeset(
		dir.path(),
		"change-2.md",
		"+++\ntest-project = \"minor\"\n+++\n\nNew feature\n",
	);

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok());
	assert_eq!(result.unwrap(), ExitCode::SUCCESS);

	// Minor wins over patch
	let cargo_toml = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
	assert!(
		cargo_toml.contains("version = \"0.2.0\""),
		"Expected version 0.2.0 (minor bump), got: {cargo_toml}"
	);

	// Both changesets should be deleted
	assert!(!dir.path().join(".cursus/change-1.md").exists());
	assert!(!dir.path().join(".cursus/change-2.md").exists());
}

#[tokio::test]
async fn prepare_dry_run_does_not_modify_files() {
	init_test_logger();
	let _ = take_logs();
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	write_changeset(
		dir.path(),
		"test-change.md",
		"+++\ntest-project = \"major\"\n+++\n\nBreaking change\n",
	);

	let original_cargo = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();

	let result = common::run_cursus(
		["cursus", "--no-interactive", "prepare", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok());
	assert_eq!(result.unwrap(), ExitCode::SUCCESS);

	let logs = take_logs();
	assert!(
		logs.iter()
			.any(|(level, m)| *level == log::Level::Info && m.contains("Would consume changeset")),
		"Expected info 'Would consume changeset' log in dry-run, got: {logs:?}"
	);

	// Cargo.toml should not be modified
	let cargo_toml = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
	assert_eq!(
		cargo_toml, original_cargo,
		"Cargo.toml should not change in dry-run"
	);

	// Changeset should still exist
	assert!(
		dir.path().join(".cursus/test-change.md").exists(),
		"Changeset should not be deleted in dry-run"
	);

	// Changelog should not be created
	assert!(
		!dir.path().join("CHANGELOG.md").exists(),
		"Changelog should not be created in dry-run"
	);
}

#[tokio::test]
async fn prepare_major_bump_resets_minor_and_patch() {
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	// First set the version to something non-trivial
	std::fs::write(
		dir.path().join("Cargo.toml"),
		"[package]\nname = \"test-project\"\nversion = \"1.5.9\"\nedition = \"2024\"\n",
	)
	.unwrap();
	write_changeset(
		dir.path(),
		"test-change.md",
		"+++\ntest-project = \"major\"\n+++\n\nBreaking\n",
	);

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok());

	let cargo_toml = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
	assert!(
		cargo_toml.contains("version = \"2.0.0\""),
		"Expected version 2.0.0, got: {cargo_toml}"
	);
}

#[tokio::test]
async fn prepare_idempotent_no_changesets_after_release() {
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	write_changeset(
		dir.path(),
		"test-change.md",
		"+++\ntest-project = \"minor\"\n+++\n\nFeature\n",
	);

	// First release
	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok());

	// Second release (no changesets)
	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok());
	assert_eq!(result.unwrap(), ExitCode::SUCCESS);
}

#[tokio::test]
async fn prepare_changelog_has_proper_sections() {
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	write_changeset(
		dir.path(),
		"change-1.md",
		"+++\ntest-project = \"major\"\n+++\n\nBreaking API change\n",
	);
	write_changeset(
		dir.path(),
		"change-2.md",
		"+++\ntest-project = \"minor\"\n+++\n\nNew feature\n",
	);
	write_changeset(
		dir.path(),
		"change-3.md",
		"+++\ntest-project = \"patch\"\n+++\n\nBug fix\n",
	);

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok());

	let today = cursus::utils::today_iso_date();
	let changelog = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();

	// Major bump aggregates all, version should be 1.0.0
	assert!(
		changelog.contains(&format!("## 1.0.0 - {today}")),
		"Should have major bumped version with date, got: {changelog}"
	);
	assert!(
		changelog.contains("### Breaking Changes"),
		"Should have breaking changes section, got: {changelog}"
	);
	assert!(
		changelog.contains("### Features"),
		"Should have features section, got: {changelog}"
	);
	assert!(
		changelog.contains("### Bug Fixes"),
		"Should have bug fixes section, got: {changelog}"
	);
}

#[tokio::test]
async fn prepare_successive_releases_prepend_to_changelog() {
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;

	// First release: minor bump 0.1.0 -> 0.2.0
	write_changeset(
		dir.path(),
		"change-1.md",
		"+++\ntest-project = \"minor\"\n+++\n\nFirst feature\n",
	);
	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok());

	// Second release: patch bump 0.2.0 -> 0.2.1
	write_changeset(
		dir.path(),
		"change-2.md",
		"+++\ntest-project = \"patch\"\n+++\n\nA bug fix\n",
	);
	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok());

	let today = cursus::utils::today_iso_date();
	let changelog = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();

	// Both versions should be present
	assert!(
		changelog.contains(&format!("## 0.2.1 - {today}")),
		"Should contain newer version with date, got: {changelog}"
	);
	assert!(
		changelog.contains(&format!("## 0.2.0 - {today}")),
		"Should contain older version with date, got: {changelog}"
	);

	// Newer entry must appear before older entry
	let pos_new = changelog.find(&format!("## 0.2.1 - {today}")).unwrap();
	let pos_old = changelog.find(&format!("## 0.2.0 - {today}")).unwrap();
	assert!(
		pos_new < pos_old,
		"Newer version should appear before older version in changelog, got: {changelog}"
	);
}

#[tokio::test]
async fn prepare_unknown_package_in_changeset_fails() {
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	write_changeset(
		dir.path(),
		"test-change.md",
		"+++\nnonexistent-package = \"minor\"\n+++\n\nSome change\n",
	);

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_err());
	let err = result.unwrap_err();
	assert!(
		err.to_string().contains("not found in projects"),
		"Expected 'not found in projects' error, got: {err}"
	);
}

#[tokio::test]
async fn prepare_package_flag_filters_packages() {
	let dir = temp_git_repo_with_cargo_workspace(&[("pkg-a", "0.1.0"), ("pkg-b", "0.2.0")]).await;

	write_changeset(
		dir.path(),
		"test-change.md",
		"+++\npkg-a = \"patch\"\npkg-b = \"minor\"\n+++\n\nSome change\n",
	);

	let result = common::run_cursus(
		[
			"cursus",
			"--no-interactive",
			"prepare",
			"--package",
			"pkg-a",
		],
		dir.path(),
	)
	.await;
	assert!(result.is_ok());
	assert_eq!(result.unwrap(), ExitCode::SUCCESS);

	// pkg-a should be bumped
	let pkg_a_toml = std::fs::read_to_string(dir.path().join("pkg-a/Cargo.toml")).unwrap();
	assert!(
		pkg_a_toml.contains("version = \"0.1.1\""),
		"Expected pkg-a version 0.1.1, got: {pkg_a_toml}"
	);

	// pkg-b should NOT be bumped
	let pkg_b_toml = std::fs::read_to_string(dir.path().join("pkg-b/Cargo.toml")).unwrap();
	assert!(
		pkg_b_toml.contains("version = \"0.2.0\""),
		"Expected pkg-b version 0.2.0 (unchanged), got: {pkg_b_toml}"
	);

	// Changeset should be partially consumed — rewritten with only pkg-b remaining
	assert!(
		dir.path().join(".cursus/test-change.md").exists(),
		"Changeset should still exist (partially consumed)"
	);
	let rewritten = std::fs::read_to_string(dir.path().join(".cursus/test-change.md")).unwrap();
	insta::assert_snapshot!(rewritten);
}

#[tokio::test]
async fn prepare_scoped_fully_consumed_changeset_is_deleted() {
	let dir = temp_git_repo_with_cargo_workspace(&[("pkg-a", "0.1.0")]).await;

	// Changeset only references pkg-a
	write_changeset(
		dir.path(),
		"only-a.md",
		"+++\npkg-a = \"patch\"\n+++\n\nFix in pkg-a\n",
	);

	let result = common::run_cursus(
		[
			"cursus",
			"--no-interactive",
			"prepare",
			"--package",
			"pkg-a",
		],
		dir.path(),
	)
	.await;
	assert!(result.is_ok());

	// Fully consumed changeset must be deleted
	assert!(
		!dir.path().join(".cursus/only-a.md").exists(),
		"Fully consumed changeset should be deleted"
	);
}

#[tokio::test]
async fn prepare_scoped_sequential_releases() {
	let dir = temp_git_repo_with_cargo_workspace(&[("pkg-a", "0.1.0"), ("pkg-b", "0.2.0")]).await;

	// Single changeset covering both packages
	write_changeset(
		dir.path(),
		"shared.md",
		"+++\npkg-a = \"patch\"\npkg-b = \"minor\"\n+++\n\nShared change\n",
	);

	// First release: only pkg-a
	let result = common::run_cursus(
		[
			"cursus",
			"--no-interactive",
			"prepare",
			"--package",
			"pkg-a",
		],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "First release failed: {result:?}");

	// Changeset should be rewritten with only pkg-b
	assert!(
		dir.path().join(".cursus/shared.md").exists(),
		"Changeset should still exist after first release"
	);
	let intermediate = std::fs::read_to_string(dir.path().join(".cursus/shared.md")).unwrap();
	insta::assert_snapshot!(intermediate);

	let pkg_a_toml = std::fs::read_to_string(dir.path().join("pkg-a/Cargo.toml")).unwrap();
	assert!(
		pkg_a_toml.contains("version = \"0.1.1\""),
		"pkg-a should be at 0.1.1, got: {pkg_a_toml}"
	);

	// Second release: only pkg-b
	let result = common::run_cursus(
		[
			"cursus",
			"--no-interactive",
			"prepare",
			"--package",
			"pkg-b",
		],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Second release failed: {result:?}");

	// Changeset now fully consumed — should be deleted
	assert!(
		!dir.path().join(".cursus/shared.md").exists(),
		"Changeset should be deleted after second release"
	);
	let pkg_b_toml = std::fs::read_to_string(dir.path().join("pkg-b/Cargo.toml")).unwrap();
	assert!(
		pkg_b_toml.contains("version = \"0.3.0\""),
		"pkg-b should be at 0.3.0, got: {pkg_b_toml}"
	);
}

#[tokio::test]
async fn prepare_scoped_unrelated_changeset_untouched() {
	let dir = temp_git_repo_with_cargo_workspace(&[("pkg-a", "0.1.0"), ("pkg-b", "0.2.0")]).await;

	// One changeset for pkg-a, one for pkg-b
	let pkg_b_content = "+++\npkg-b = \"minor\"\n+++\n\nPkg-b only change\n";
	write_changeset(
		dir.path(),
		"only-a.md",
		"+++\npkg-a = \"patch\"\n+++\n\nPkg-a only change\n",
	);
	write_changeset(dir.path(), "only-b.md", pkg_b_content);

	// Release only pkg-a
	let result = common::run_cursus(
		[
			"cursus",
			"--no-interactive",
			"prepare",
			"--package",
			"pkg-a",
		],
		dir.path(),
	)
	.await;
	assert!(result.is_ok());

	// pkg-a changeset deleted
	assert!(
		!dir.path().join(".cursus/only-a.md").exists(),
		"pkg-a changeset should be deleted"
	);

	// pkg-b changeset untouched
	assert!(
		dir.path().join(".cursus/only-b.md").exists(),
		"pkg-b changeset should be untouched"
	);
	let b_content = std::fs::read_to_string(dir.path().join(".cursus/only-b.md")).unwrap();
	assert_eq!(
		b_content, pkg_b_content,
		"pkg-b changeset should be unchanged"
	);
}

#[tokio::test]
async fn prepare_unknown_package_flag_fails() {
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	write_changeset(
		dir.path(),
		"test-change.md",
		"+++\ntest-project = \"minor\"\n+++\n\nSome change\n",
	);

	let result = common::run_cursus(
		[
			"cursus",
			"--no-interactive",
			"prepare",
			"--package",
			"nonexistent",
		],
		dir.path(),
	)
	.await;
	assert!(result.is_err());
	let err = result.unwrap_err();
	assert!(
		err.to_string().contains("Unknown package: nonexistent"),
		"Expected 'Unknown package: nonexistent' error, got: {err}"
	);
}

#[tokio::test]
async fn prepare_updates_cargo_lock_file() {
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	write_changeset(
		dir.path(),
		"test-change.md",
		"+++\ntest-project = \"minor\"\n+++\n\nAdded a feature\n",
	);

	// Remove Cargo.lock if it exists
	let lock_file = dir.path().join("Cargo.lock");
	if lock_file.exists() {
		std::fs::remove_file(&lock_file).unwrap();
	}

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	if let Err(ref err) = result {
		eprintln!("Release failed: {:#}", err);
	}
	assert!(result.is_ok());
	assert_eq!(result.unwrap(), std::process::ExitCode::SUCCESS);

	// Verify Cargo.lock was created/updated
	assert!(
		lock_file.exists(),
		"Cargo.lock should be created after release"
	);

	// Verify the lock file contains the new version
	let lock_content = std::fs::read_to_string(&lock_file).unwrap();
	assert!(
		lock_content.contains("0.2.0"),
		"Cargo.lock should contain the new version, got: {lock_content}"
	);
}

#[tokio::test]
async fn prepare_updates_cargo_intra_workspace_dep_version() {
	// pkg-a depends on pkg-b via a path dep; when pkg-b is bumped, pkg-a's Cargo.toml should be updated
	let dir = temp_git_repo_with_cargo_workspace(&[("pkg-a", "0.1.0"), ("pkg-b", "0.2.0")]).await;

	// Write pkg-a with a path + version dep on pkg-b (path lets Cargo resolve it locally)
	std::fs::write(
		dir.path().join("pkg-a/Cargo.toml"),
		"[package]\nname = \"pkg-a\"\nversion = \"0.1.0\"\nedition = \"2024\"\n\n[dependencies]\npkg-b = { path = \"../pkg-b\", version = \"0.2.0\" }\n",
	)
	.unwrap();

	write_changeset(
		dir.path(),
		"bump-b.md",
		"+++\npkg-b = \"minor\"\n+++\n\nAdded feature to pkg-b\n",
	);

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok(), "release failed: {:?}", result.unwrap_err());

	// Verify pkg-b version was bumped
	let pkg_b_toml = std::fs::read_to_string(dir.path().join("pkg-b/Cargo.toml")).unwrap();
	assert!(
		pkg_b_toml.contains("version = \"0.3.0\""),
		"Expected pkg-b version 0.3.0, got: {pkg_b_toml}"
	);

	// Verify pkg-a's dependency on pkg-b was updated (version field in table entry)
	let pkg_a_toml = std::fs::read_to_string(dir.path().join("pkg-a/Cargo.toml")).unwrap();
	assert!(
		pkg_a_toml.contains("version = \"0.3.0\""),
		"Expected pkg-a to reference pkg-b 0.3.0, got: {pkg_a_toml}"
	);
	assert!(
		pkg_a_toml.contains("path = \"../pkg-b\""),
		"Expected path dep to be preserved, got: {pkg_a_toml}"
	);
}

#[tokio::test]
async fn prepare_updates_cargo_workspace_dep_in_root() {
	// Root Cargo.toml has [workspace.dependencies]; pkg-a uses it via workspace = true.
	// When pkg-b is bumped, the root [workspace.dependencies] entry should be updated.
	let dir = temp_git_repo_with_cargo_workspace(&[("pkg-a", "0.1.0"), ("pkg-b", "0.2.0")]).await;

	// Root Cargo.toml with workspace.dependencies (path dep so Cargo finds it locally)
	std::fs::write(
		dir.path().join("Cargo.toml"),
		"[workspace]\nmembers = [\"pkg-a\", \"pkg-b\"]\n\n[workspace.dependencies]\npkg-b = { path = \"pkg-b\", version = \"0.2.0\" }\n",
	)
	.unwrap();

	// pkg-a inherits pkg-b from workspace, which puts "pkg-b" in its dependency_names
	std::fs::write(
		dir.path().join("pkg-a/Cargo.toml"),
		"[package]\nname = \"pkg-a\"\nversion = \"0.1.0\"\nedition = \"2024\"\n\n[dependencies]\npkg-b = { workspace = true }\n",
	)
	.unwrap();

	write_changeset(
		dir.path(),
		"bump-b.md",
		"+++\npkg-b = \"minor\"\n+++\n\nAdded feature\n",
	);

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok(), "release failed: {:?}", result.unwrap_err());

	let root_toml = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
	assert!(
		root_toml.contains("version = \"0.3.0\""),
		"Expected root [workspace.dependencies] pkg-b version 0.3.0, got: {root_toml}"
	);
	assert!(
		root_toml.contains("path = \"pkg-b\""),
		"Expected path to be preserved in workspace.dependencies, got: {root_toml}"
	);
}

#[tokio::test]
async fn prepare_dry_run_shows_dep_updates_without_modifying_files() {
	let dir = temp_git_repo_with_cargo_workspace(&[("pkg-a", "0.1.0"), ("pkg-b", "0.2.0")]).await;

	std::fs::write(
		dir.path().join("pkg-a/Cargo.toml"),
		"[package]\nname = \"pkg-a\"\nversion = \"0.1.0\"\nedition = \"2024\"\n\n[dependencies]\npkg-b = { path = \"../pkg-b\", version = \"0.2.0\" }\n",
	)
	.unwrap();

	write_changeset(
		dir.path(),
		"bump-b.md",
		"+++\npkg-b = \"patch\"\n+++\n\nBug fix\n",
	);

	let original_a = std::fs::read_to_string(dir.path().join("pkg-a/Cargo.toml")).unwrap();

	let result = common::run_cursus(
		["cursus", "--no-interactive", "prepare", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok());

	// pkg-a/Cargo.toml should not be modified in dry-run
	let after_a = std::fs::read_to_string(dir.path().join("pkg-a/Cargo.toml")).unwrap();
	assert_eq!(original_a, after_a, "dry-run must not modify dep files");
}

/// `prepare` issues a warning when `--branch` is given with the push strategy.
#[tokio::test]
async fn prepare_branch_arg_with_push_strategy_warns() {
	init_test_logger();
	let _ = take_logs();
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	write_changeset(
		dir.path(),
		"change.md",
		"+++\ntest-project = \"patch\"\n+++\n\nFix\n",
	);

	// Push strategy is the default (no [git] section). Passing --branch should warn.
	let result = common::run_cursus(
		[
			"cursus",
			"--no-interactive",
			"prepare",
			"--branch",
			"custom-branch",
		],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	assert!(
		logs.iter()
			.any(|(level, m)| *level == log::Level::Warn && m.contains("no effect")),
		"Expected a 'no effect' warning when --branch is given with push strategy, got: {logs:?}"
	);
}

/// `prepare` without `--branch` and push strategy must NOT issue the "no effect" warning.
///
/// This guards against mutations that weaken `&&` to `||` in the branch-warning predicate,
/// which would incorrectly warn whenever push strategy is active even without a `--branch` arg.
#[tokio::test]
async fn prepare_no_branch_arg_with_push_strategy_no_warning() {
	init_test_logger();
	let _ = take_logs();
	let dir = temp_git_repo_with_project(PackageManager::Cargo).await;
	write_changeset(
		dir.path(),
		"change.md",
		"+++\ntest-project = \"patch\"\n+++\n\nFix\n",
	);

	// No --branch arg, push strategy (default) → warning must NOT appear.
	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	assert!(
		!logs
			.iter()
			.any(|(_, m)| m.contains("no effect") && m.contains("branch")),
		"Should not warn about --branch when it was not passed, got: {logs:?}"
	);
}

#[tokio::test]
async fn prepare_updates_npm_intra_workspace_dep_version() {
	// pkg-a depends on pkg-b; when pkg-b is bumped, pkg-a's package.json should be updated
	let dir = temp_git_repo();
	let env = common::test_env(dir.path());
	let config =
		cursus::model::config::Config::new().with_npm(cursus::model::config::NpmConfig::enabled());
	config.save(env.fs(), env.git().path()).await.unwrap();

	// Root package.json with workspace config
	std::fs::write(
		dir.path().join("package.json"),
		r#"{"name": "root", "version": "0.0.0", "private": true, "workspaces": ["pkg-a", "pkg-b"]}"#,
	)
	.unwrap();

	std::fs::create_dir_all(dir.path().join("pkg-a")).unwrap();
	std::fs::write(
		dir.path().join("pkg-a/package.json"),
		r#"{"name": "pkg-a", "version": "0.1.0", "dependencies": {"pkg-b": "^0.2.0"}}"#,
	)
	.unwrap();

	std::fs::create_dir_all(dir.path().join("pkg-b")).unwrap();
	std::fs::write(
		dir.path().join("pkg-b/package.json"),
		r#"{"name": "pkg-b", "version": "0.2.0"}"#,
	)
	.unwrap();

	write_changeset(
		dir.path(),
		"bump-b.md",
		"+++\npkg-b = \"minor\"\n+++\n\nAdded feature to pkg-b\n",
	);

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok(), "release failed: {:?}", result.unwrap_err());

	// Verify pkg-b version was bumped
	let pkg_b_json = std::fs::read_to_string(dir.path().join("pkg-b/package.json")).unwrap();
	assert!(
		pkg_b_json.contains("\"0.3.0\""),
		"Expected pkg-b version 0.3.0, got: {pkg_b_json}"
	);

	// Verify pkg-a's dependency on pkg-b was updated (preserving ^ prefix)
	let pkg_a_json = std::fs::read_to_string(dir.path().join("pkg-a/package.json")).unwrap();
	assert!(
		pkg_a_json.contains("\"pkg-b\": \"^0.3.0\""),
		"Expected pkg-a to reference pkg-b ^0.3.0, got: {pkg_a_json}"
	);
}

#[tokio::test]
async fn prepare_skips_workspace_protocol_dep_version() {
	// pkg-a depends on pkg-b via workspace: protocol; that dep must NOT be rewritten
	// (ADR-012: workspace: entries auto-resolve at publish time via the package manager)
	let dir = temp_git_repo();
	let env = common::test_env(dir.path());
	let config =
		cursus::model::config::Config::new().with_npm(cursus::model::config::NpmConfig::enabled());
	config.save(env.fs(), env.git().path()).await.unwrap();

	std::fs::write(
		dir.path().join("package.json"),
		r#"{"name": "root", "version": "0.0.0", "private": true, "workspaces": ["pkg-a", "pkg-b"]}"#,
	)
	.unwrap();

	std::fs::create_dir_all(dir.path().join("pkg-a")).unwrap();
	std::fs::write(
		dir.path().join("pkg-a/package.json"),
		r#"{"name": "pkg-a", "version": "0.1.0", "dependencies": {"pkg-b": "workspace:*"}}"#,
	)
	.unwrap();

	std::fs::create_dir_all(dir.path().join("pkg-b")).unwrap();
	std::fs::write(
		dir.path().join("pkg-b/package.json"),
		r#"{"name": "pkg-b", "version": "0.2.0"}"#,
	)
	.unwrap();

	write_changeset(
		dir.path(),
		"bump-b.md",
		"+++\npkg-b = \"minor\"\n+++\n\nAdded feature to pkg-b\n",
	);

	let result = common::run_cursus(["cursus", "--no-interactive", "prepare"], dir.path()).await;
	assert!(result.is_ok(), "prepare failed: {:?}", result.unwrap_err());

	// pkg-b version should be bumped normally
	let pkg_b_json = std::fs::read_to_string(dir.path().join("pkg-b/package.json")).unwrap();
	assert!(
		pkg_b_json.contains("\"0.3.0\""),
		"Expected pkg-b version 0.3.0, got: {pkg_b_json}"
	);

	// pkg-a's workspace: dep must remain untouched
	let pkg_a_json = std::fs::read_to_string(dir.path().join("pkg-a/package.json")).unwrap();
	assert!(
		pkg_a_json.contains("\"pkg-b\": \"workspace:*\""),
		"Expected workspace: dep to be preserved unchanged, got: {pkg_a_json}"
	);
}