|
| 1 | +import { test, expect } from "bun:test" |
| 2 | +import { extractChangelogFromContent } from "../../src/cli/changelog" |
| 3 | + |
| 4 | +const sampleChangelog = `# Changelog |
| 5 | +
|
| 6 | +## [0.3.0] - 2026-04-01 |
| 7 | +
|
| 8 | +### Added |
| 9 | +
|
| 10 | +- New feature A |
| 11 | +
|
| 12 | +## [0.2.2] - 2026-03-05 |
| 13 | +
|
| 14 | +### Fixed |
| 15 | +
|
| 16 | +- Bug fix B |
| 17 | +
|
| 18 | +## [0.2.1] - 2026-03-05 |
| 19 | +
|
| 20 | +### Added |
| 21 | +
|
| 22 | +- Feature C |
| 23 | +
|
| 24 | +## [0.2.0] - 2026-03-04 |
| 25 | +
|
| 26 | +### Added |
| 27 | +
|
| 28 | +- Feature D |
| 29 | +
|
| 30 | +## [0.1.0] - 2025-06-01 |
| 31 | +
|
| 32 | +### Added |
| 33 | +
|
| 34 | +- Initial release |
| 35 | +` |
| 36 | + |
| 37 | +test("extracts changelog between two versions", () => { |
| 38 | + const result = extractChangelogFromContent(sampleChangelog, "0.2.0", "0.2.2") |
| 39 | + expect(result).toContain("## [0.2.2]") |
| 40 | + expect(result).toContain("Bug fix B") |
| 41 | + expect(result).toContain("## [0.2.1]") |
| 42 | + expect(result).toContain("Feature C") |
| 43 | + expect(result).not.toContain("## [0.2.0]") |
| 44 | + expect(result).not.toContain("## [0.3.0]") |
| 45 | + expect(result).not.toContain("## [0.1.0]") |
| 46 | +}) |
| 47 | + |
| 48 | +test("extracts single version", () => { |
| 49 | + const result = extractChangelogFromContent(sampleChangelog, "0.2.1", "0.2.2") |
| 50 | + expect(result).toContain("## [0.2.2]") |
| 51 | + expect(result).toContain("Bug fix B") |
| 52 | + expect(result).not.toContain("## [0.2.1]") |
| 53 | +}) |
| 54 | + |
| 55 | +test("returns empty string when no versions in range", () => { |
| 56 | + const result = extractChangelogFromContent(sampleChangelog, "0.3.0", "0.4.0") |
| 57 | + expect(result).toBe("") |
| 58 | +}) |
| 59 | + |
| 60 | +test("returns empty string for empty content", () => { |
| 61 | + expect(extractChangelogFromContent("", "0.1.0", "0.2.0")).toBe("") |
| 62 | +}) |
| 63 | + |
| 64 | +test("handles v-prefixed versions", () => { |
| 65 | + const result = extractChangelogFromContent(sampleChangelog, "v0.2.0", "v0.2.2") |
| 66 | + expect(result).toContain("## [0.2.2]") |
| 67 | + expect(result).toContain("## [0.2.1]") |
| 68 | +}) |
| 69 | + |
| 70 | +test("returns empty string for invalid version strings", () => { |
| 71 | + expect(extractChangelogFromContent(sampleChangelog, "not-a-version", "0.2.0")).toBe("") |
| 72 | + expect(extractChangelogFromContent(sampleChangelog, "0.1.0", "bad")).toBe("") |
| 73 | +}) |
| 74 | + |
| 75 | +test("works with the real CHANGELOG.md", async () => { |
| 76 | + const fs = await import("fs") |
| 77 | + const path = await import("path") |
| 78 | + const changelogPath = path.resolve(import.meta.dir, "../../../../CHANGELOG.md") |
| 79 | + if (!fs.existsSync(changelogPath)) return // skip if not available |
| 80 | + |
| 81 | + const content = fs.readFileSync(changelogPath, "utf-8") |
| 82 | + const result = extractChangelogFromContent(content, "0.1.0", "0.2.0") |
| 83 | + expect(result).toContain("## [0.2.0]") |
| 84 | + expect(result).toContain("Context management") |
| 85 | + expect(result).not.toContain("## [0.1.0]") |
| 86 | +}) |
0 commit comments