Skip to content

Commit 2da04b8

Browse files
authored
Fix TestFormatDueDate year-boundary flake (#514)
* Fix TestFormatDueDate year-boundary flake The same-year assertion used now.AddDate(0, 6, 0), which crosses into the next calendar year for any run after June — so from July onward the date carried a year and the 'should not contain year' assertion failed. Pick a date in the opposite half of the current year instead, keeping it same-year and clear of the today/tomorrow/yesterday window regardless of when in the year the suite runs. * Make TestFormatDueDate fully deterministic Address review: the test still read time.Now() while formatDueDate computed its own time.Now() internally, leaving a residual midnight/New-Year race where the two calls could observe different years. Extract formatDueDateAt(iso, now) as a testable seam (formatDueDate stays a thin wrapper passing time.Now()), and drive the test from a fixed reference now so none of the relative-label cases depend on the wall clock.
1 parent f4bf171 commit 2da04b8

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

‎internal/tui/workspace/views/detail.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1549,11 +1549,17 @@ func fetchSubscriptionState(sub *basecamp.Subscription, err error) bool {
15491549

15501550
// formatDueDate converts an ISO date string to a human-friendly label.
15511551
func formatDueDate(iso string) string {
1552+
return formatDueDateAt(iso, time.Now())
1553+
}
1554+
1555+
// formatDueDateAt formats a due date relative to a caller-supplied now, so the
1556+
// relative labels ("Today"/"Tomorrow"/same-year) are deterministic and testable
1557+
// without depending on the wall clock. formatDueDate passes time.Now().
1558+
func formatDueDateAt(iso string, now time.Time) string {
15521559
t, err := time.ParseInLocation("2006-01-02", iso, time.Local)
15531560
if err != nil {
15541561
return iso
15551562
}
1556-
now := time.Now()
15571563
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
15581564
switch {
15591565
case t.Equal(today):

‎internal/tui/workspace/views/detail_test.go‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -800,29 +800,34 @@ func TestDetail_EditingCallsRelayout(t *testing.T) {
800800
}
801801

802802
func TestFormatDueDate(t *testing.T) {
803-
now := time.Now()
803+
// Fixed reference time so the test is fully deterministic — it never reads
804+
// the wall clock, so there's no midnight/New-Year race between the test's
805+
// now and the one formatDueDate would compute internally.
806+
now := time.Date(2026, time.July, 2, 12, 0, 0, 0, time.Local)
807+
format := func(iso string) string { return formatDueDateAt(iso, now) }
808+
804809
today := now.Format("2006-01-02")
805810
tomorrow := now.AddDate(0, 0, 1).Format("2006-01-02")
806811
yesterday := now.AddDate(0, 0, -1).Format("2006-01-02")
807812

808-
assert.Equal(t, "Today", formatDueDate(today))
809-
assert.Equal(t, "Tomorrow", formatDueDate(tomorrow))
810-
assert.Equal(t, "Yesterday", formatDueDate(yesterday))
813+
assert.Equal(t, "Today", format(today))
814+
assert.Equal(t, "Tomorrow", format(tomorrow))
815+
assert.Equal(t, "Yesterday", format(yesterday))
811816

812-
// Same year, far enough from today to avoid collisions
813-
farDate := now.AddDate(0, 6, 0) // 6 months ahead
814-
farISO := farDate.Format("2006-01-02")
815-
result := formatDueDate(farISO)
816-
assert.Contains(t, result, farDate.Format("Jan 2"))
817-
assert.NotContains(t, result, farDate.Format("2006"), "same-year dates should omit year")
817+
// Same calendar year as now, comfortably away from the today/tomorrow/
818+
// yesterday window.
819+
sameYear := time.Date(now.Year(), time.February, 15, 0, 0, 0, 0, now.Location())
820+
result := format(sameYear.Format("2006-01-02"))
821+
assert.Contains(t, result, sameYear.Format("Jan 2"))
822+
assert.NotContains(t, result, sameYear.Format("2006"), "same-year dates should omit year")
818823

819-
// Different year: includes year
820-
otherYear := now.AddDate(-2, 0, 0).Format("2006-01-02")
821-
result = formatDueDate(otherYear)
822-
assert.Contains(t, result, now.AddDate(-2, 0, 0).Format("2006"))
824+
// Different year: includes year.
825+
otherYear := now.AddDate(-2, 0, 0)
826+
result = format(otherYear.Format("2006-01-02"))
827+
assert.Contains(t, result, otherYear.Format("2006"))
823828

824-
// Invalid input: pass through
825-
assert.Equal(t, "not-a-date", formatDueDate("not-a-date"))
829+
// Invalid input: pass through.
830+
assert.Equal(t, "not-a-date", format("not-a-date"))
826831
}
827832

828833
func TestDetail_SyncPreview_DueDateFormatted(t *testing.T) {

0 commit comments

Comments
 (0)