import * as fs from 'node:fs' import * as cp from 'node:child_process' import * as os from 'node:os' import * as path from 'node:path' import assert from '@remix-run/assert' import { describe, it } from '@remix-run/test' import type { PackageRelease } from './changes.ts' import { generateChangelogContent, getNextVersion, parseChangelog, parsePackageChanges, readChangesConfig, } from './changes.ts' function makeRelease(overrides: Partial = {}): PackageRelease { return { packageDirName: 'remix', packageName: 'remix', currentVersion: '3.0.0-alpha.3', nextVersion: '3.0.0-alpha.4', bump: 'patch', changes: [], dependencyBumps: [], ...overrides, } } function withTemporaryPackage( version: string, callback: (packageDirName: string, packagePath: string) => void, ) { let packagePath = fs.mkdtempSync(path.join(os.tmpdir(), 'remix-changes-test-')) let packageDirName = path.basename(packagePath) let packageJsonPath = path.join(packagePath, 'package.json') fs.writeFileSync( packageJsonPath, JSON.stringify( { name: `@remix-run/${packageDirName}`, version, type: 'module', }, null, 2, ) + '\n', ) try { callback(packageDirName, packagePath) } finally { fs.rmSync(packagePath, { recursive: true, force: true }) } } describe('parsePackageChanges', () => { it('validates change files without a git checkout', () => { withTemporaryPackage('1.0.0', (packageDirName, packagePath) => { fs.mkdirSync(path.join(packagePath, '.changes')) fs.writeFileSync(path.join(packagePath, '.changes/patch.fix.md'), 'Fix the bug') assert.deepEqual(parsePackageChanges(packageDirName, packagePath), { valid: true, changes: [{ file: 'patch.fix.md', bump: 'patch', content: 'Fix the bug' }], changesConfig: null, }) }) }) it('resolves introduction commits only when generating the changelog', () => { withTemporaryPackage('1.0.0', (packageDirName, packagePath) => { function git(args: string[]) { return cp .execFileSync('git', ['-c', 'core.hooksPath=/dev/null', ...args], { cwd: packagePath, stdio: 'pipe', encoding: 'utf-8', }) .trim() } git(['init']) git(['config', 'user.name', 'Test Bot']) git(['config', 'user.email', 'test@example.com']) let changesPath = path.join(packagePath, '.changes') fs.mkdirSync(changesPath) let filePath = path.join(changesPath, 'patch.fix.md') fs.writeFileSync(filePath, 'Fix the bug') git(['add', '.']) git(['commit', '-m', 'Fix the bug (#123)']) let introducingCommit = git(['rev-parse', 'HEAD']) fs.writeFileSync(filePath, 'Clarify the bug fix') git(['add', '.']) git(['commit', '-m', 'Edit the release note (#456)']) git(['mv', '.changes/patch.fix.md', '.changes/patch.renamed.md']) fs.writeFileSync(path.join(changesPath, 'patch.direct.md'), 'Direct commit change') git(['add', '.']) git(['commit', '-m', 'Commit a direct fix']) let directCommit = git(['rev-parse', 'HEAD']) fs.writeFileSync(path.join(changesPath, 'patch.untracked.md'), 'Uncommitted change') fs.writeFileSync(path.join(changesPath, 'patch.staged.md'), 'Staged change') git(['add', '.changes/patch.staged.md']) let result = parsePackageChanges(packageDirName, packagePath) if (!result.valid) assert.fail('Expected valid changes') assert.deepEqual(result.changes, [ { file: 'patch.direct.md', bump: 'patch', content: 'Direct commit change', }, { file: 'patch.renamed.md', bump: 'patch', content: 'Clarify the bug fix', }, { file: 'patch.staged.md', bump: 'patch', content: 'Staged change' }, { file: 'patch.untracked.md', bump: 'patch', content: 'Uncommitted change' }, ]) let content = generateChangelogContent( makeRelease({ packageDirName: packagePath, changes: result.changes }), ) assert.match( content, /Clarify the bug fix \(\[#123\]\(https:\/\/github.com\/remix-run\/remix\/pull\/123\)\)/, ) assert.ok(content.includes(`/commit/${directCommit}`)) assert.ok(!content.includes(`/commit/${introducingCommit}`)) assert.ok(!content.includes('/pull/456')) assert.match(content, /- Staged change\n/) assert.match(content, /- Uncommitted change\n/) }) }) it('allows stable packages without .changes directories', () => { withTemporaryPackage('1.0.0', (packageDirName, packagePath) => { let result = parsePackageChanges(packageDirName, packagePath) if (!result.valid) { assert.fail('Expected a stable package without a .changes directory to validate') } assert.equal(result.changes.length, 0) assert.equal(result.changesConfig, null) }) }) }) describe('readChangesConfig', () => { it('reads a custom prerelease start', () => { withTemporaryPackage('3.0.0-beta.10', (packageDirName, packagePath) => { let changesPath = path.join(packagePath, '.changes') fs.mkdirSync(changesPath) fs.writeFileSync( path.join(changesPath, 'config.json'), JSON.stringify({ prereleaseChannel: 'rc', prereleaseStart: 1 }), ) assert.deepEqual(readChangesConfig(packageDirName, packagePath), { exists: true, valid: true, config: { prereleaseChannel: 'rc', prereleaseStart: 1 }, }) }) }) it('rejects an invalid prerelease start', () => { withTemporaryPackage('3.0.0-beta.10', (packageDirName, packagePath) => { let changesPath = path.join(packagePath, '.changes') fs.mkdirSync(changesPath) fs.writeFileSync( path.join(changesPath, 'config.json'), JSON.stringify({ prereleaseChannel: 'rc', prereleaseStart: -1 }), ) assert.deepEqual(readChangesConfig(packageDirName, packagePath), { exists: true, valid: false, error: '.changes/config.json "prereleaseStart" must be a non-negative integer', }) }) }) }) describe('parseChangelog', () => { it('returns version entries in document order', () => { let entries = parseChangelog(`# package changelog ## v3.0.0-beta.9 ### Pre-release Changes - New behavior ## v3.0.0-beta.8 (2026-08-17) ### Pre-release Changes - Earlier behavior ## Unreleased `) assert.deepEqual(entries, [ { version: '3.0.0-beta.9', date: undefined, body: '### Pre-release Changes\n\n- New behavior', }, { version: '3.0.0-beta.8', date: new Date('2026-08-17'), body: '### Pre-release Changes\n\n- Earlier behavior', }, ]) }) }) describe('generateChangelogContent', () => { it('adds PR links to stable entries and commit links to multiline entries', () => { let content = generateChangelogContent( makeRelease({ nextVersion: '3.0.1', changes: [ { file: 'patch.a.md', bump: 'patch', content: 'Fix a bug', gitSha: 'abcdef0123456789', prNumber: 123, }, { file: 'patch.b.md', bump: 'patch', content: 'Fix another bug\n\nMore details\n\n```ts\nexample()\n```', gitSha: '123456789abcdef0', }, ], }), ) assert.equal( content, '## v3.0.1\n\n### Patch Changes\n\n' + '- Fix a bug ([#123](https://github.com/remix-run/remix/pull/123))\n\n' + '- Fix another bug ([1234567](https://github.com/remix-run/remix/commit/123456789abcdef0))\n\n' + ' More details\n\n ```ts\n example()\n ```\n', ) }) it('adds source links to prerelease entries', () => { let content = generateChangelogContent( makeRelease({ changes: [{ file: 'patch.fix.md', bump: 'patch', content: 'Fix a bug', prNumber: 123 }], }), ) assert.equal( content, '## v3.0.0-alpha.4\n\n### Pre-release Changes\n\n' + '- Fix a bug ([#123](https://github.com/remix-run/remix/pull/123))\n', ) }) it('groups prerelease changes into a single section', () => { let content = generateChangelogContent( makeRelease({ changes: [ { file: 'minor.alpha.md', bump: 'minor', content: 'Alpha change', }, { file: 'major.breaking.md', bump: 'major', content: 'BREAKING CHANGE: Breaking change', }, { file: 'patch.fix.md', bump: 'patch', content: 'Patch change', }, ], dependencyBumps: [ { packageName: '@remix-run/router', version: '1.2.3', releaseUrl: 'https://example.com/router', }, ], }), ) assert.match(content, /^## v3\.0\.0-alpha\.4/m) assert.match(content, /^### Pre-release Changes$/m) assert.doesNotMatch(content, /^### Major Changes$/m) assert.doesNotMatch(content, /^### Minor Changes$/m) assert.doesNotMatch(content, /^### Patch Changes$/m) assert.match(content, /- BREAKING CHANGE: Breaking change/) assert.match(content, /- Alpha change/) assert.match(content, /- Patch change/) assert.match(content, /- Bumped `@remix-run\/\*` dependencies:/) assert.match(content, /\[`router@1\.2\.3`\]\(https:\/\/example\.com\/router\)/) }) it('keeps stable releases grouped by bump type', () => { let content = generateChangelogContent( makeRelease({ currentVersion: '3.0.0', nextVersion: '3.0.1', changes: [ { file: 'minor.feature.md', bump: 'minor', content: 'Feature change', }, ], dependencyBumps: [ { packageName: '@remix-run/router', version: '1.2.3', releaseUrl: 'https://example.com/router', }, ], }), ) assert.match(content, /^## v3\.0\.1/m) assert.match(content, /^### Minor Changes$/m) assert.match(content, /^### Patch Changes$/m) assert.doesNotMatch(content, /^### Pre-release Changes$/m) }) }) describe('getNextVersion', () => { it('starts prereleases from the next major version', () => { assert.equal(getNextVersion('2.17.4', 'major', { prereleaseChannel: 'alpha' }), '3.0.0-alpha.0') }) it('increments prereleases within the same channel', () => { assert.equal( getNextVersion('3.0.0-alpha.0', 'patch', { prereleaseChannel: 'alpha' }), '3.0.0-alpha.1', ) }) it('starts a new prerelease channel at a configured number', () => { assert.equal( getNextVersion('3.0.0-beta.10', 'patch', { prereleaseChannel: 'rc', prereleaseStart: 1, }), '3.0.0-rc.1', ) }) it('increments the configured channel after it starts', () => { assert.equal( getNextVersion('3.0.0-rc.1', 'patch', { prereleaseChannel: 'rc', prereleaseStart: 1, }), '3.0.0-rc.2', ) }) })