cursus 0.5.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
use super::*;

// --- extract_pr_number ---

#[test]
fn extract_pr_number_squash_merge_format() {
	assert_eq!(extract_pr_number("feat: add widget (#42)"), Some(42));
}

#[test]
fn extract_pr_number_squash_merge_at_start() {
	assert_eq!(extract_pr_number("fix: thing (#1)"), Some(1));
}

#[test]
fn extract_pr_number_merge_commit_format() {
	assert_eq!(
		extract_pr_number("Merge pull request #123 from owner/branch"),
		Some(123)
	);
}

#[test]
fn extract_pr_number_no_match_rebase() {
	assert_eq!(extract_pr_number("feat: add widget"), None);
}

#[test]
fn extract_pr_number_no_match_hash_without_parens() {
	assert_eq!(extract_pr_number("fix: issue #99 workaround"), None);
}

#[test]
fn extract_pr_number_empty_subject() {
	assert_eq!(extract_pr_number(""), None);
}

// --- CommitReference::format_suffix ---

#[test]
fn commit_reference_format_suffix_with_pr() {
	let r = CommitReference {
		short_hash: "abc1234".to_string(),
		pr_number: Some(42),
	};
	assert_eq!(r.format_suffix(), " [abc1234] via #42");
}

#[test]
fn commit_reference_format_suffix_without_pr() {
	let r = CommitReference {
		short_hash: "abc1234".to_string(),
		pr_number: None,
	};
	assert_eq!(r.format_suffix(), " [abc1234]");
}

#[test]
fn commit_reference_new_truncates_sha_to_7_chars() {
	let r = CommitReference::new("abcdef1234567890", "feat: stuff (#5)");
	assert_eq!(r.short_hash, "abcdef1");
	assert_eq!(r.pr_number, Some(5));
}

// --- format_sections with commit references ---

#[test]
fn format_sections_with_commit_reference_renders_suffix() {
	let commit_ref = CommitReference {
		short_hash: "abc1234".to_string(),
		pr_number: Some(42),
	};
	let changes = vec![(
		ChangeType::Minor,
		Some("Added widget".to_string()),
		Some(commit_ref),
	)];
	let changelog = Changelog::new(
		"1.0.0".parse().unwrap(),
		"2024-01-01".to_string(),
		changes,
		AbsolutePath::new("/nonexistent").unwrap(),
	);
	let sections = changelog.format_sections();
	assert!(
		sections.contains("- Added widget [abc1234] via #42"),
		"Expected suffix in output, got: {sections}"
	);
}

#[test]
fn format_sections_multiline_message_suffix_on_first_line() {
	let commit_ref = CommitReference {
		short_hash: "abc1234".to_string(),
		pr_number: None,
	};
	let changes = vec![(
		ChangeType::Minor,
		Some("Added widget\nwith extra details".to_string()),
		Some(commit_ref),
	)];
	let changelog = Changelog::new(
		"1.0.0".parse().unwrap(),
		"2024-01-01".to_string(),
		changes,
		AbsolutePath::new("/nonexistent").unwrap(),
	);
	let sections = changelog.format_sections();
	assert!(
		sections.contains("- Added widget [abc1234]\n  with extra details"),
		"Expected suffix on first line with indented continuation, got: {sections}"
	);
}

// --- split_at_first_h2 ---

#[test]
fn split_at_first_h2_with_preamble() {
	let content = "# Changelog\n\nIntro paragraph.\n\n## 1.0.0\n\nOld\n";
	let (preamble, rest) = split_at_first_h2(content);
	assert_eq!(preamble, "# Changelog\n\nIntro paragraph.\n\n");
	assert_eq!(rest, "## 1.0.0\n\nOld\n");
}

#[test]
fn split_at_first_h2_starts_with_h2() {
	let content = "## 1.0.0\n\nOld\n";
	let (preamble, rest) = split_at_first_h2(content);
	assert_eq!(preamble, "");
	assert_eq!(rest, "## 1.0.0\n\nOld\n");
}

#[test]
fn split_at_first_h2_no_h2() {
	let content = "# Changelog\n\nNo versions yet.\n";
	let (preamble, rest) = split_at_first_h2(content);
	assert_eq!(preamble, "# Changelog\n\nNo versions yet.\n");
	assert_eq!(rest, "");
}

#[test]
fn split_at_first_h2_empty() {
	let (preamble, rest) = split_at_first_h2("");
	assert_eq!(preamble, "");
	assert_eq!(rest, "");
}

#[tokio::test]
async fn update_changelog_preserves_custom_preamble() {
	let dir = tempfile::tempdir().unwrap();
	std::fs::write(
		dir.path().join("CHANGELOG.md"),
		"# My Custom Title\n\nAn intro paragraph.\n\n## 0.1.0\n\nOld entry\n",
	)
	.unwrap();
	let changes = vec![(ChangeType::Minor, Some("New thing".to_string()), None)];
	let changelog = Changelog::new(
		"0.2.0".parse().unwrap(),
		"2024-06-01".to_string(),
		changes,
		AbsolutePath::new(dir.path()).unwrap(),
	);
	changelog
		.update(false, &crate::filesystem::LocalFilesystem)
		.await
		.unwrap();

	let content = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();
	insta::assert_snapshot!(content);
}

#[test]
fn format_sections_returns_sections_without_heading() {
	let changes = vec![
		(ChangeType::Minor, Some("Added feature X".to_string()), None),
		(ChangeType::Patch, Some("Fixed bug Y".to_string()), None),
	];
	let changelog = Changelog::new(
		"1.1.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new("/nonexistent").unwrap(),
	);
	let sections = changelog.format_sections();
	assert!(!sections.contains("## 1.1.0"));
	assert!(sections.contains("### Features"));
	assert!(sections.contains("- Added feature X"));
	assert!(sections.contains("### Bug Fixes"));
	assert!(sections.contains("- Fixed bug Y"));
}

#[test]
fn format_sections_dependency_section_separated_by_blank_line() {
	let changes = vec![(ChangeType::Minor, Some("Feature X".to_string()), None)];
	let changelog = Changelog::new(
		"1.0.0".parse().unwrap(),
		"2024-01-01".to_string(),
		changes,
		AbsolutePath::new("/nonexistent").unwrap(),
	)
	.with_dependency_entries(vec!["`pkg-a` bumped to 1.0.0".to_string()]);
	let sections = changelog.format_sections();
	assert!(
		sections.contains("\n\n### Dependencies"),
		"Expected blank line before Dependencies section, got: {sections}"
	);
	assert!(sections.contains("### Features"));
	assert!(sections.contains("### Dependencies"));
}

#[test]
fn format_sections_returns_empty_when_no_messages() {
	let changes: Vec<(ChangeType, Option<String>, Option<CommitReference>)> =
		vec![(ChangeType::Minor, None, None)];
	let changelog = Changelog::new(
		"1.1.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new("/nonexistent").unwrap(),
	);
	assert!(changelog.format_sections().is_empty());
}

#[test]
fn format_changelog_entry_with_messages() {
	let changes = vec![
		(ChangeType::Minor, Some("Added feature X".to_string()), None),
		(ChangeType::Patch, Some("Fixed bug Y".to_string()), None),
	];
	let changelog = Changelog::new(
		"1.1.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new("/nonexistent").unwrap(),
	);
	let entry = changelog.format_entry();
	assert!(entry.contains("## 1.1.0 - 2024-01-15"));
	assert!(entry.contains("### Features"));
	assert!(entry.contains("- Added feature X"));
	assert!(entry.contains("### Bug Fixes"));
	assert!(entry.contains("- Fixed bug Y"));
}

#[test]
fn format_changelog_entry_no_messages() {
	let changes: Vec<(ChangeType, Option<String>, Option<CommitReference>)> =
		vec![(ChangeType::Minor, None, None)];
	let changelog = Changelog::new(
		"1.1.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new("/nonexistent").unwrap(),
	);
	let entry = changelog.format_entry();
	assert!(entry.contains("## 1.1.0 - 2024-01-15"));
	assert!(!entry.contains("###"));
}

#[test]
fn format_changelog_entry_multiline_message() {
	let changes = vec![(
		ChangeType::Minor,
		Some("First line\nSecond line\nThird line".to_string()),
		None,
	)];
	let changelog = Changelog::new(
		"1.1.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new("/nonexistent").unwrap(),
	);
	let entry = changelog.format_entry();
	// Continuation lines must be indented so the list item renders correctly
	assert!(entry.contains("- First line\n  Second line\n  Third line"));
}

#[test]
fn format_changelog_entry_multiline_message_blank_lines_not_indented() {
	let changes = vec![(
		ChangeType::Minor,
		Some("First line\n\nSecond paragraph".to_string()),
		None,
	)];
	let changelog = Changelog::new(
		"1.1.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new("/nonexistent").unwrap(),
	);
	let entry = changelog.format_entry();
	// Blank lines must not be indented
	assert!(entry.contains("- First line\n\n  Second paragraph"));
}

#[test]
fn format_changelog_entry_major_section() {
	let changes = vec![(
		ChangeType::Major,
		Some("Breaking API change".to_string()),
		None,
	)];
	let changelog = Changelog::new(
		"2.0.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new("/nonexistent").unwrap(),
	);
	let entry = changelog.format_entry();
	assert!(entry.contains("### Breaking Changes"));
	assert!(entry.contains("- Breaking API change"));
}

#[tokio::test]
async fn update_changelog_creates_new_file() {
	let dir = tempfile::tempdir().unwrap();
	let changes = vec![(ChangeType::Minor, Some("Something new".to_string()), None)];
	let changelog = Changelog::new(
		"1.0.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new(dir.path()).unwrap(),
	);
	changelog
		.update(false, &crate::filesystem::LocalFilesystem)
		.await
		.unwrap();

	let content = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();
	assert!(content.contains("# Changelog"));
	assert!(content.contains("## 1.0.0 - 2024-01-15"));
}

#[tokio::test]
async fn update_changelog_prepends_to_existing() {
	let dir = tempfile::tempdir().unwrap();
	std::fs::write(
		dir.path().join("CHANGELOG.md"),
		"# Changelog\n\n## 0.1.0\n\nOld entry\n",
	)
	.unwrap();
	let changes = vec![(ChangeType::Minor, Some("New thing".to_string()), None)];
	let changelog = Changelog::new(
		"0.2.0".parse().unwrap(),
		"2024-06-01".to_string(),
		changes,
		AbsolutePath::new(dir.path()).unwrap(),
	);
	changelog
		.update(false, &crate::filesystem::LocalFilesystem)
		.await
		.unwrap();

	let content = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();
	assert!(content.contains("## 0.2.0 - 2024-06-01"));
	assert!(content.contains("## 0.1.0"));
	// New entry should come first
	let pos_new = content.find("## 0.2.0").unwrap();
	let pos_old = content.find("## 0.1.0").unwrap();
	assert!(pos_new < pos_old);
	// Header must appear exactly once
	assert_eq!(content.matches("# Changelog").count(), 1);
}

#[tokio::test]
async fn update_changelog_successive_releases_snapshot() {
	let dir = tempfile::tempdir().unwrap();

	let make = |version: &str, msg: &str| {
		Changelog::new(
			version.parse().unwrap(),
			"2024-01-01".to_string(),
			vec![(ChangeType::Patch, Some(msg.to_string()), None)],
			AbsolutePath::new(dir.path()).unwrap(),
		)
	};

	make("1.0.0", "Initial release")
		.update(false, &crate::filesystem::LocalFilesystem)
		.await
		.unwrap();
	make("1.0.1", "Second release")
		.update(false, &crate::filesystem::LocalFilesystem)
		.await
		.unwrap();
	make("1.0.2", "Third release")
		.update(false, &crate::filesystem::LocalFilesystem)
		.await
		.unwrap();

	let content = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();
	insta::assert_snapshot!(content);
}

#[tokio::test]
async fn update_changelog_no_duplicate_header_on_successive_releases() {
	let dir = tempfile::tempdir().unwrap();

	let make = |version: &str, msg: &str| {
		Changelog::new(
			version.parse().unwrap(),
			"2024-01-01".to_string(),
			vec![(ChangeType::Patch, Some(msg.to_string()), None)],
			AbsolutePath::new(dir.path()).unwrap(),
		)
	};

	make("1.0.0", "Initial release")
		.update(false, &crate::filesystem::LocalFilesystem)
		.await
		.unwrap();
	make("1.0.1", "Second release")
		.update(false, &crate::filesystem::LocalFilesystem)
		.await
		.unwrap();
	make("1.0.2", "Third release")
		.update(false, &crate::filesystem::LocalFilesystem)
		.await
		.unwrap();

	let content = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();
	assert_eq!(content.matches("# Changelog").count(), 1);
	// All three versions present
	assert!(content.contains("## 1.0.2"));
	assert!(content.contains("## 1.0.1"));
	assert!(content.contains("## 1.0.0"));
	// Newest first
	let p2 = content.find("## 1.0.2").unwrap();
	let p1 = content.find("## 1.0.1").unwrap();
	let p0 = content.find("## 1.0.0").unwrap();
	assert!(p2 < p1 && p1 < p0);
}

#[tokio::test]
async fn update_changelog_in_subdir() {
	let dir = tempfile::tempdir().unwrap();
	let sub = dir.path().join("packages/my-pkg");
	std::fs::create_dir_all(&sub).unwrap();
	let changes = vec![(ChangeType::Patch, Some("Release".to_string()), None)];
	let changelog = Changelog::new(
		"1.0.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new(sub.clone()).unwrap(),
	);
	changelog
		.update(false, &crate::filesystem::LocalFilesystem)
		.await
		.unwrap();

	let content = std::fs::read_to_string(sub.join("CHANGELOG.md")).unwrap();
	assert!(content.contains("## 1.0.0 - 2024-01-15"));
}

#[tokio::test]
async fn update_changelog_fails_when_cannot_read_existing() {
	let dir = tempfile::tempdir().unwrap();
	let changelog_path = dir.path().join("CHANGELOG.md");
	// Create a directory with the same name as the file we want to read
	std::fs::create_dir(&changelog_path).unwrap();

	let changes = vec![(ChangeType::Minor, Some("New".to_string()), None)];
	let changelog = Changelog::new(
		"1.0.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new(dir.path()).unwrap(),
	);
	let result = changelog
		.update(false, &crate::filesystem::LocalFilesystem)
		.await;

	// Should fail because CHANGELOG.md is a directory, not a file
	assert!(result.is_err());
}

#[tokio::test]
async fn update_changelog_fails_when_cannot_write() {
	use crate::path::AbsolutePath;

	#[derive(Debug)]
	struct FailingWriteFilesystem;

	#[async_trait::async_trait]
	impl crate::filesystem::Filesystem for FailingWriteFilesystem {
		async fn read_to_string(&self, _: &AbsolutePath) -> anyhow::Result<String> {
			anyhow::bail!("not implemented")
		}
		async fn read(&self, _: &AbsolutePath) -> anyhow::Result<Vec<u8>> {
			anyhow::bail!("not implemented")
		}
		async fn write(&self, _: &AbsolutePath, _: &[u8]) -> anyhow::Result<()> {
			anyhow::bail!("simulated write failure")
		}
		async fn create_dir_all(&self, _: &AbsolutePath) -> anyhow::Result<()> {
			anyhow::bail!("not implemented")
		}
		async fn remove_file(&self, _: &AbsolutePath) -> anyhow::Result<()> {
			anyhow::bail!("not implemented")
		}
		async fn exists(&self, _: &AbsolutePath) -> anyhow::Result<bool> {
			Ok(false)
		}
		async fn is_dir(&self, _: &AbsolutePath) -> anyhow::Result<bool> {
			anyhow::bail!("not implemented")
		}
		async fn canonicalize(&self, _: &AbsolutePath) -> anyhow::Result<std::path::PathBuf> {
			anyhow::bail!("not implemented")
		}
		async fn glob(&self, _: &str) -> anyhow::Result<Vec<std::path::PathBuf>> {
			anyhow::bail!("not implemented")
		}
		async fn file_size(&self, _: &AbsolutePath) -> anyhow::Result<u64> {
			anyhow::bail!("not implemented")
		}
	}

	let dir = tempfile::tempdir().unwrap();
	let changes = vec![(ChangeType::Patch, Some("Fix".to_string()), None)];
	let changelog = Changelog::new(
		"1.0.0".parse().unwrap(),
		"2024-01-15".to_string(),
		changes,
		AbsolutePath::new(dir.path()).unwrap(),
	);
	let result = changelog.update(false, &FailingWriteFilesystem).await;

	// Should fail because the filesystem returns an error on write.
	assert!(result.is_err());
}

// --- extract_version_body ---

const MULTI_VERSION_CHANGELOG: &str = "\
# Changelog

## 1.2.0 - 2024-06-01

### Features

- Added widget

## 1.1.0 - 2024-03-01

### Bug Fixes

- Fixed thing

## 1.0.0

Initial release
";

#[tokio::test]
async fn extract_version_body_finds_middle_version() {
	let dir = tempfile::tempdir().unwrap();
	let path = dir.path().join("CHANGELOG.md");
	std::fs::write(&path, MULTI_VERSION_CHANGELOG).unwrap();

	let body = extract_version_body(
		&path,
		&"1.1.0".parse().unwrap(),
		&crate::filesystem::LocalFilesystem,
	)
	.await
	.unwrap();
	assert!(body.contains("### Bug Fixes"));
	assert!(body.contains("- Fixed thing"));
	assert!(!body.contains("### Features"));
}

#[tokio::test]
async fn extract_version_body_finds_first_version() {
	let dir = tempfile::tempdir().unwrap();
	let path = dir.path().join("CHANGELOG.md");
	std::fs::write(&path, MULTI_VERSION_CHANGELOG).unwrap();

	let body = extract_version_body(
		&path,
		&"1.2.0".parse().unwrap(),
		&crate::filesystem::LocalFilesystem,
	)
	.await
	.unwrap();
	assert!(body.contains("### Features"));
	assert!(body.contains("- Added widget"));
}

#[tokio::test]
async fn extract_version_body_finds_version_at_eof() {
	let dir = tempfile::tempdir().unwrap();
	let path = dir.path().join("CHANGELOG.md");
	std::fs::write(&path, MULTI_VERSION_CHANGELOG).unwrap();

	let body = extract_version_body(
		&path,
		&"1.0.0".parse().unwrap(),
		&crate::filesystem::LocalFilesystem,
	)
	.await
	.unwrap();
	assert_eq!(body.trim(), "Initial release");
}

#[tokio::test]
async fn extract_version_body_returns_empty_for_missing_version() {
	let dir = tempfile::tempdir().unwrap();
	let path = dir.path().join("CHANGELOG.md");
	std::fs::write(&path, MULTI_VERSION_CHANGELOG).unwrap();

	let body = extract_version_body(
		&path,
		&"9.9.9".parse().unwrap(),
		&crate::filesystem::LocalFilesystem,
	)
	.await
	.unwrap();
	assert!(body.is_empty());
}

#[tokio::test]
async fn extract_version_body_returns_error_for_missing_file() {
	let dir = tempfile::tempdir().unwrap();
	let path = dir.path().join("CHANGELOG.md");

	let result = extract_version_body(
		&path,
		&"1.0.0".parse().unwrap(),
		&crate::filesystem::LocalFilesystem,
	)
	.await;
	assert!(result.is_err());
}

#[tokio::test]
async fn extract_version_body_does_not_match_version_prefix() {
	let changelog = "# Changelog\n\n## 1.2.0-beta - 2024-01-01\n\nbeta content\n\n## 1.2.0 - 2024-02-01\n\nstable content\n";
	let dir = tempfile::tempdir().unwrap();
	let path = dir.path().join("CHANGELOG.md");
	std::fs::write(&path, changelog).unwrap();

	let body = extract_version_body(
		&path,
		&"1.2.0".parse().unwrap(),
		&crate::filesystem::LocalFilesystem,
	)
	.await
	.unwrap();
	assert!(body.contains("stable content"));
	assert!(!body.contains("beta content"));
}

#[tokio::test]
async fn extract_version_body_with_date_suffix() {
	let changelog = "# Changelog\n\n## 2.0.0 - 2025-01-01\n\nMajor release\n";
	let dir = tempfile::tempdir().unwrap();
	let path = dir.path().join("CHANGELOG.md");
	std::fs::write(&path, changelog).unwrap();

	let body = extract_version_body(
		&path,
		&"2.0.0".parse().unwrap(),
		&crate::filesystem::LocalFilesystem,
	)
	.await
	.unwrap();
	assert!(body.contains("Major release"));
}

#[tokio::test]
async fn extract_version_body_empty_body() {
	let changelog = "# Changelog\n\n## 1.0.0\n\n## 0.9.0\n\nPrevious\n";
	let dir = tempfile::tempdir().unwrap();
	let path = dir.path().join("CHANGELOG.md");
	std::fs::write(&path, changelog).unwrap();

	let body = extract_version_body(
		&path,
		&"1.0.0".parse().unwrap(),
		&crate::filesystem::LocalFilesystem,
	)
	.await
	.unwrap();
	assert!(body.is_empty());
}

#[tokio::test]
async fn extract_version_body_strips_leading_blank_lines() {
	// Multiple blank lines before content — the result must not start with a blank line.
	let changelog = "# Changelog\n\n## 1.0.0\n\n\n\nContent here\n";
	let dir = tempfile::tempdir().unwrap();
	let path = dir.path().join("CHANGELOG.md");
	std::fs::write(&path, changelog).unwrap();

	let body = extract_version_body(
		&path,
		&"1.0.0".parse().unwrap(),
		&crate::filesystem::LocalFilesystem,
	)
	.await
	.unwrap();
	assert!(
		!body.starts_with('\n'),
		"body should not start with blank line, got: {body:?}"
	);
	assert!(body.contains("Content here"));
}

#[tokio::test]
async fn extract_version_body_strips_trailing_blank_lines() {
	// Trailing blank lines between sections — the result must not end with a blank line.
	let changelog = "# Changelog\n\n## 1.0.0\n\nContent here\n\n\n## 0.9.0\n\nPrevious\n";
	let dir = tempfile::tempdir().unwrap();
	let path = dir.path().join("CHANGELOG.md");
	std::fs::write(&path, changelog).unwrap();

	let body = extract_version_body(
		&path,
		&"1.0.0".parse().unwrap(),
		&crate::filesystem::LocalFilesystem,
	)
	.await
	.unwrap();
	assert!(
		!body.ends_with('\n'),
		"body should not end with blank line, got: {body:?}"
	);
	assert!(body.contains("Content here"));
}