aboutsummaryrefslogtreecommitdiff
path: root/internal/cli/duration_test.go
diff options
context:
space:
mode:
authortsne <tsne.dev@outlook.com>2026-07-06 16:52:20 +0200
committertsne <tsne.dev@outlook.com>2026-07-06 21:37:42 +0200
commit84da43998f7cc42cc72652333ac6e3d293abb23e (patch)
tree6610f82df854ae86f7f34152bb05fc712ef6f314 /internal/cli/duration_test.go
downloadhopper-84da43998f7cc42cc72652333ac6e3d293abb23e.tar.gz
initialHEADmain
Diffstat (limited to 'internal/cli/duration_test.go')
-rw-r--r--internal/cli/duration_test.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/internal/cli/duration_test.go b/internal/cli/duration_test.go
new file mode 100644
index 0000000..60d446a
--- /dev/null
+++ b/internal/cli/duration_test.go
@@ -0,0 +1,51 @@
+package cli
+
+import (
+ "testing"
+ "time"
+)
+
+func TestDurationValueParses(t *testing.T) {
+ cases := map[string]duration{
+ "30d": {days: 30},
+ "7d": {days: 7},
+ "1y2m10d": {years: 1, months: 2, days: 10},
+ "2m": {months: 2},
+ "1y": {years: 1},
+ "0d": {days: 0},
+ }
+ for in, want := range cases {
+ var v duration
+ if err := v.Set(in); err != nil {
+ t.Fatalf("Set(%q): %v", in, err)
+ }
+ if v != want {
+ t.Fatalf("Set(%q) = %+v, want %+v", in, v, want)
+ }
+ }
+}
+
+func TestDurationValueRejectsBadInput(t *testing.T) {
+ for _, in := range []string{"", "d", "5", "5x", "1h", "1m2m", "-3d", "y", "5 d", "99999999999999999999d"} {
+ var v duration
+ if err := v.Set(in); err == nil {
+ t.Fatalf("Set(%q): expected error", in)
+ }
+ }
+}
+
+func TestDurationValueMonthIsCalendarMonth(t *testing.T) {
+ var v duration
+ if err := v.Set("1m"); err != nil {
+ t.Fatal(err)
+ }
+ now := time.Date(2024, time.March, 31, 0, 0, 0, 0, time.UTC)
+ // A calendar-aware month back from March 31 lands in February, not "31 days".
+ got := v.cutoff(now)
+ if got.Month() != time.February && got.Month() != time.March {
+ t.Fatalf("cutoff month = %v, want calendar-aware", got.Month())
+ }
+ if got.Equal(now.AddDate(0, 0, -30)) {
+ t.Fatalf("cutoff must not use a fixed 30-day month")
+ }
+}