● Zion Boggan
repos/Agent Time Ledger/internal/clock/clock_test.go
zionboggan.com ↗
55 lines · go
History for this file →
1
package clock
2
 
3
import (
4
	"testing"
5
	"time"
6
)
7
 
8
func TestParseDuration(t *testing.T) {
9
	tests := map[string]time.Duration{
10
		"15s": 15 * time.Second,
11
		"30m": 30 * time.Minute,
12
		"2h":  2 * time.Hour,
13
		"1d":  24 * time.Hour,
14
	}
15
	for input, want := range tests {
16
		got, err := ParseDuration(input)
17
		if err != nil {
18
			t.Fatalf("ParseDuration(%q) returned error: %v", input, err)
19
		}
20
		if got != want {
21
			t.Fatalf("ParseDuration(%q) = %s, want %s", input, got, want)
22
		}
23
	}
24
}
25
 
26
func TestFormatDuration(t *testing.T) {
27
	got := FormatDuration(26*time.Hour + 3*time.Minute + 4*time.Second)
28
	want := "1d 2h 3m 4s"
29
	if got != want {
30
		t.Fatalf("FormatDuration returned %q, want %q", got, want)
31
	}
32
}
33
 
34
func TestRFC3339RoundTrip(t *testing.T) {
35
	now := time.Date(2026, 6, 19, 6, 30, 0, 123, time.UTC)
36
	text := FormatRFC3339(now)
37
	got, err := ParseRFC3339(text)
38
	if err != nil {
39
		t.Fatalf("ParseRFC3339 returned error: %v", err)
40
	}
41
	if !got.Equal(now) {
42
		t.Fatalf("round trip = %s, want %s", got, now)
43
	}
44
}
45
 
46
func TestLoadLocationAndOffset(t *testing.T) {
47
	location, err := LoadLocation("America/Chicago")
48
	if err != nil {
49
		t.Fatalf("LoadLocation returned error: %v", err)
50
	}
51
	now := time.Date(2026, 6, 19, 6, 30, 0, 0, time.UTC).In(location)
52
	if got := UTCOffset(now); got != "-05:00" {
53
		t.Fatalf("UTCOffset = %q, want -05:00", got)
54
	}
55
}