|
| 1 | +// @vitest-environment node |
| 2 | +/** |
| 3 | + * CHANGELOG.md consistency checks. |
| 4 | + * |
| 5 | + * Validates that version headers have matching compare links, |
| 6 | + * that [Unreleased] points to the latest version, and that |
| 7 | + * package.json version matches the latest CHANGELOG entry. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect } from 'vitest'; |
| 11 | +import { readFileSync } from 'node:fs'; |
| 12 | +import { resolve } from 'node:path'; |
| 13 | +import { fileURLToPath } from 'node:url'; |
| 14 | + |
| 15 | +const __dirname = fileURLToPath(new URL('.', import.meta.url)); |
| 16 | +const ROOT = resolve(__dirname, '../..'); |
| 17 | + |
| 18 | +const changelog = readFileSync(resolve(ROOT, 'CHANGELOG.md'), 'utf-8'); |
| 19 | +const pkg = JSON.parse(readFileSync(resolve(ROOT, 'package.json'), 'utf-8')); |
| 20 | + |
| 21 | +/** Extract all `## [x.y.z]` version headers (excludes [Unreleased]). */ |
| 22 | +function getVersionHeaders(text: string): string[] { |
| 23 | + return [...text.matchAll(/^## \[(\d+\.\d+\.\d+)\]/gm)].map((m) => m[1]); |
| 24 | +} |
| 25 | + |
| 26 | +/** Extract all `[x.y.z]: <url>` reference links at the bottom. */ |
| 27 | +function getCompareLinks(text: string): Map<string, string> { |
| 28 | + const links = new Map<string, string>(); |
| 29 | + for (const m of text.matchAll(/^\[([^\]]+)\]:\s*(.+)$/gm)) { |
| 30 | + links.set(m[1], m[2].trim()); |
| 31 | + } |
| 32 | + return links; |
| 33 | +} |
| 34 | + |
| 35 | +describe('CHANGELOG.md', () => { |
| 36 | + const versions = getVersionHeaders(changelog); |
| 37 | + const links = getCompareLinks(changelog); |
| 38 | + |
| 39 | + it('has at least one version entry', () => { |
| 40 | + expect(versions.length).toBeGreaterThan(0); |
| 41 | + }); |
| 42 | + |
| 43 | + it('latest version matches package.json', () => { |
| 44 | + expect(versions[0]).toBe(pkg.version); |
| 45 | + }); |
| 46 | + |
| 47 | + it('every version header has a matching compare link', () => { |
| 48 | + const missing = versions.filter((v) => !links.has(v)); |
| 49 | + expect(missing).toEqual([]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('[Unreleased] link points to latest version tag', () => { |
| 53 | + const unreleased = links.get('Unreleased'); |
| 54 | + expect(unreleased).toBeDefined(); |
| 55 | + expect(unreleased).toContain(`v${versions[0]}...HEAD`); |
| 56 | + }); |
| 57 | + |
| 58 | + it('compare links are in descending version order', () => { |
| 59 | + const linkVersions = [...links.keys()].filter((k) => k !== 'Unreleased' && /^\d+\.\d+\.\d+$/.test(k)); |
| 60 | + expect(linkVersions).toEqual(versions); |
| 61 | + }); |
| 62 | +}); |
0 commit comments