Skip to content

Commit 65b8af2

Browse files
compscidrclaude
andcommitted
Add test for archive sort order and zero-padded months
Create posts in 2023 and 2025, verify the archives page renders 2025 before 2023 (newest first) and months are zero-padded (2023/03). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0058aa4 commit 65b8af2

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

blog/blog_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strconv"
1616
"strings"
1717
"testing"
18+
"time"
1819

1920
"github.com/gin-contrib/sessions"
2021
"github.com/gin-contrib/sessions/cookie"
@@ -216,6 +217,14 @@ func TestBlogWorkflow(t *testing.T) {
216217
t.Fatalf("Expected to get status %d for /tags but instead got %d\n", http.StatusOK, w.Code)
217218
}
218219

220+
// Create posts in different years/months so we can verify archive sort order
221+
oldPost := blog.Post{Title: "Old Post", Content: "old", Slug: "old-post", PostTypeID: defaultType.ID}
222+
oldPost.CreatedAt = time.Date(2023, 3, 15, 0, 0, 0, 0, time.UTC)
223+
db.Create(&oldPost)
224+
newPost := blog.Post{Title: "New Post", Content: "new", Slug: "new-post", PostTypeID: defaultType.ID}
225+
newPost.CreatedAt = time.Date(2025, 11, 1, 0, 0, 0, 0, time.UTC)
226+
db.Create(&newPost)
227+
219228
// Dynamic page: /archives resolves via NoRoute
220229
a.On("IsAdmin", mock.Anything).Return(false).Once()
221230
a.On("IsLoggedIn", mock.Anything).Return(false).Once()
@@ -225,6 +234,23 @@ func TestBlogWorkflow(t *testing.T) {
225234
if w.Code != http.StatusOK {
226235
t.Fatalf("Expected to get status %d for /archives but instead got %d\n", http.StatusOK, w.Code)
227236
}
237+
// Verify archives are sorted newest first
238+
body := w.Body.String()
239+
idx2025 := strings.Index(body, "2025")
240+
idx2023 := strings.Index(body, "2023")
241+
if idx2025 < 0 || idx2023 < 0 {
242+
t.Fatal("Expected archives page to contain both 2025 and 2023")
243+
}
244+
if idx2025 > idx2023 {
245+
t.Fatal("Expected 2025 to appear before 2023 in archives (newest first)")
246+
}
247+
// Verify months are zero-padded
248+
if !strings.Contains(body, "2023/03") {
249+
t.Fatal("Expected zero-padded month 2023/03 in archives")
250+
}
251+
if !strings.Contains(body, "2025/11") {
252+
t.Fatal("Expected 2025/11 in archives")
253+
}
228254

229255
//get home
230256
a.On("IsAdmin", mock.Anything).Return(false).Once()

0 commit comments

Comments
 (0)