grokkit/cmd/changelog_test.go

107 lines
2.8 KiB
Go
Raw Permalink Normal View History

package cmd
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBuildChangelogMessages(t *testing.T) {
t.Parallel()
log := `feat: add changelog command
Implements #1
---
fix: typo in docs
---`
version := "v0.2.0"
messages := buildChangelogMessages(log, version)
require.Len(t, messages, 2)
assert.Equal(t, "system", messages[0]["role"])
assert.Contains(t, messages[0]["content"], "Generate a changelog section")
assert.Contains(t, messages[0]["content"], "### Added")
assert.Contains(t, messages[0]["content"], "### Changed")
assert.Contains(t, messages[0]["content"], "### Fixed")
assert.Equal(t, "user", messages[1]["role"])
assert.Contains(t, messages[1]["content"], "Version: v0.2.0")
assert.Contains(t, messages[1]["content"], log)
}
func TestBuildFullChangelog(t *testing.T) {
t.Parallel()
newSection := "## [v0.2.0] - 2026-03-03\n\n### Added\n- changelog command\n"
t.Run("creates new file with header", func(t *testing.T) {
tmpDir := t.TempDir()
originalWd, err := os.Getwd()
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.Chdir(originalWd))
})
require.NoError(t, os.Chdir(tmpDir))
result := buildFullChangelog(newSection)
assert.Contains(t, result, "# Changelog")
assert.Contains(t, result, "All notable changes to this project will be documented in this file.")
assert.Contains(t, result, newSection)
})
t.Run("prepends to existing file", func(t *testing.T) {
tmpDir := t.TempDir()
originalWd, err := os.Getwd()
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.Chdir(originalWd))
})
require.NoError(t, os.Chdir(tmpDir))
// Simulate existing changelog
existing := `# Changelog
All notable changes to this project will be documented in this file.
## [v0.1.0] - 2025-01-01
### Fixed
- old bug
`
require.NoError(t, os.WriteFile("CHANGELOG.md", []byte(existing), 0644))
result := buildFullChangelog(newSection)
assert.True(t, strings.HasPrefix(result, newSection), "new version section must be prepended at the top")
assert.Contains(t, result, "old bug")
})
}
func TestChangelogCmd_Flags(t *testing.T) {
t.Parallel()
require.NotNil(t, changelogCmd)
stdoutFlag := changelogCmd.Flags().Lookup("stdout")
require.NotNil(t, stdoutFlag)
assert.Equal(t, "Print ONLY the new section (ideal for Gitea release notes)", stdoutFlag.Usage)
versionFlag := changelogCmd.Flags().Lookup("version")
require.NotNil(t, versionFlag)
assert.Equal(t, "V", versionFlag.Shorthand)
commitFlag := changelogCmd.Flags().Lookup("commit")
require.NotNil(t, commitFlag)
assert.Equal(t, "After writing, remind to run grokkit commit", commitFlag.Usage)
sinceFlag := changelogCmd.Flags().Lookup("since")
require.NotNil(t, sinceFlag)
}