package changelog_test import ( "os" "path/filepath" "strings" "testing" "time" "github.com/thokra/stamp/internal/changelog" ) var testDate = time.Date(2026, 3, 8, 0, 0, 0, 0, time.UTC) func TestAppend_NewFile(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "CHANGELOG.md") err := changelog.Append(path, changelog.Entry{ Version: "1.0.0", Date: testDate, Description: "Initial release", }) if err != nil { t.Fatalf("unexpected error: %v", err) } data, _ := os.ReadFile(path) content := string(data) if !strings.Contains(content, "## [1.0.0] - 2026-03-08") { t.Errorf("expected version header, got:\n%s", content) } if !strings.Contains(content, "Initial release") { t.Errorf("expected description in changelog") } } func TestAppend_ExistingFile(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "CHANGELOG.md") // Write first version. _ = changelog.Append(path, changelog.Entry{ Version: "1.0.0", Date: testDate, Description: "First release", }) // Append second version. _ = changelog.Append(path, changelog.Entry{ Version: "1.1.0", Date: testDate.Add(24 * time.Hour), Description: "Second release", }) data, _ := os.ReadFile(path) content := string(data) if !strings.Contains(content, "## [1.0.0]") { t.Error("expected 1.0.0 section") } if !strings.Contains(content, "## [1.1.0]") { t.Error("expected 1.1.0 section") } // New version should appear before old. idx110 := strings.Index(content, "## [1.1.0]") idx100 := strings.Index(content, "## [1.0.0]") if idx110 > idx100 { t.Error("expected 1.1.0 to appear before 1.0.0 (newest first)") } } func TestAppend_EmptyDescription(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "CHANGELOG.md") err := changelog.Append(path, changelog.Entry{ Version: "2.0.0", Date: testDate, }) if err != nil { t.Fatalf("unexpected error: %v", err) } data, _ := os.ReadFile(path) if !strings.Contains(string(data), "## [2.0.0]") { t.Error("expected version header even with empty description") } } func TestAppend_CreatesDirectory(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "sub", "dir", "CHANGELOG.md") err := changelog.Append(path, changelog.Entry{ Version: "1.0.0", Date: testDate, }) if err != nil { t.Fatalf("unexpected error: %v", err) } if _, err := os.Stat(path); err != nil { t.Errorf("expected file to be created: %v", err) } }