-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers_test.go
More file actions
131 lines (120 loc) · 3.2 KB
/
Copy pathhelpers_test.go
File metadata and controls
131 lines (120 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"strings"
"testing"
"time"
)
func TestTruncateSessionID(t *testing.T) {
tests := []struct {
input string
want string
}{
{"abcdefghijklmnop", "abcdefgh"},
{"abcdefgh", "abcdefgh"},
{"short", "short"},
{"", ""},
}
for _, tt := range tests {
if got := truncateSessionID(tt.input); got != tt.want {
t.Errorf("truncateSessionID(%q) = %q, want %q", tt.input, got, tt.want)
}
}
}
func TestParseTimestamp(t *testing.T) {
tests := []struct {
name string
input string
wantOK bool
}{
{"RFC3339", "2026-03-28T10:00:00Z", true},
{"RFC3339Nano", "2026-03-28T10:00:00.123456789Z", true},
{"RFC3339 with offset", "2026-03-28T10:00:00+02:00", true},
{"empty", "", false},
{"invalid", "not-a-timestamp", false},
{"date only", "2026-03-28", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts, ok := parseTimestamp(tt.input)
if ok != tt.wantOK {
t.Errorf("parseTimestamp(%q) ok = %v, want %v", tt.input, ok, tt.wantOK)
}
if ok && ts.IsZero() {
t.Error("expected non-zero time on success")
}
})
}
}
func TestParseTimestampOrNow(t *testing.T) {
ts := parseTimestampOrNow("2026-03-28T10:00:00Z")
if ts.Year() != 2026 {
t.Errorf("expected 2026, got %d", ts.Year())
}
before := time.Now()
ts = parseTimestampOrNow("invalid")
after := time.Now()
if ts.Before(before) || ts.After(after) {
t.Error("expected parseTimestampOrNow with invalid input to return ~now")
}
}
func TestCoalesce(t *testing.T) {
t.Run("overwrites with non-empty", func(t *testing.T) {
s := "original"
coalesce(&s, "replacement")
if s != "replacement" {
t.Errorf("expected replacement, got %q", s)
}
})
t.Run("keeps original on empty alt", func(t *testing.T) {
s := "original"
coalesce(&s, "")
if s != "original" {
t.Errorf("expected original, got %q", s)
}
})
t.Run("fills empty target", func(t *testing.T) {
s := ""
coalesce(&s, "filled")
if s != "filled" {
t.Errorf("expected filled, got %q", s)
}
})
}
func TestFormatTimeAgo(t *testing.T) {
now := time.Now()
tests := []struct {
name string
input time.Time
want string
}{
{"just now", now.Add(-30 * time.Second), "just now"},
{"1m ago", now.Add(-90 * time.Second), "1m ago"},
{"5m ago", now.Add(-5 * time.Minute), "5m ago"},
{"1h ago", now.Add(-90 * time.Minute), "1h ago"},
{"3h ago", now.Add(-3 * time.Hour), "3h ago"},
{"yesterday", now.Add(-36 * time.Hour), "yesterday"},
{"old date", time.Date(2025, 1, 15, 12, 0, 0, 0, time.UTC), "Jan 15"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := formatTimeAgo(tt.input); got != tt.want {
t.Errorf("formatTimeAgo() = %q, want %q", got, tt.want)
}
})
}
}
func TestFormatTimeRange(t *testing.T) {
now := time.Now()
t.Run("same label collapses", func(t *testing.T) {
got := formatTimeRange(now.Add(-10*time.Second), now.Add(-20*time.Second))
if got != "just now" {
t.Errorf("expected collapsed label, got %q", got)
}
})
t.Run("different labels show range", func(t *testing.T) {
got := formatTimeRange(now.Add(-2*time.Minute), now.Add(-30*time.Minute))
if !strings.Contains(got, " - ") {
t.Errorf("expected range with separator, got %q", got)
}
})
}