| 1 | package pause |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // words is the reviewed copy these tests need (internal/equipo/copy.go |
| 11 | // holds the real entries; a test elsewhere checks they agree). |
| 12 | var words = map[string]string{ |
| 13 | "time.tonight": "%s tonight", |
| 14 | "time.today_at": "today at %s", |
| 15 | "time.tomorrow_at": "tomorrow at %s", |
| 16 | "time.midnight": "midnight", |
| 17 | "time.in_hours": "in %d hours", |
| 18 | "time.in_hours.one": "in 1 hour", |
| 19 | "time.in_minutes": "in %d minutes", |
| 20 | "time.in_minutes.one": "in 1 minute", |
| 21 | "time.in_days": "in %d days", |
| 22 | "time.in_days.one": "in 1 day", |
| 23 | "time.when_in": "%s (%s)", |
| 24 | "work.pause.until": "Paused until %s (%s): %s.", |
| 25 | "work.pause.since": "Paused since %s: %s.", |
| 26 | "work.pause.moment": "Paused for a moment: %s.", |
| 27 | "work.pause.lead.stopped": "Stopped at %s: %s", |
| 28 | "work.pause.lead.spend_unknown": "Paused: Equipo can’t tell how much %s has spent.", |
| 29 | "work.pause.reason.headroom_out": "your %s allowance has run out", |
| 30 | } |
| 31 | |
| 32 | func copyOf(k string) string { return words[k] } |
| 33 | |
| 34 | func dublin(t *testing.T) *time.Location { |
| 35 | t.Helper() |
| 36 | loc, err := time.LoadLocation("Europe/Dublin") |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | return loc |
| 41 | } |
| 42 | |
| 43 | // Paul's case, 2026-11-24: the Claude allowance ran out and resets at |
| 44 | // 21:00 UTC; it is noon in Dublin (UTC+0 in winter). |
| 45 | func TestPaulsCaseInDublin(t *testing.T) { |
| 46 | loc := dublin(t) |
| 47 | now := time.Date(2026, 11, 24, 12, 0, 0, 0, time.UTC) |
| 48 | reset := time.Date(2026, 11, 24, 21, 0, 0, 0, time.UTC) |
| 49 | when, in := When(copyOf, now, reset, loc), In(copyOf, reset.Sub(now)) |
| 50 | if when != "21:00 tonight" || in != "in 9 hours" { |
| 51 | t.Fatalf("when %q in %q", when, in) |
| 52 | } |
| 53 | strong, rest := Lead(copyOf, "work.pause.until", when, in, fmt.Sprintf(copyOf("work.pause.reason.headroom_out"), "Claude")) |
| 54 | if strong != "Paused until 21:00 tonight" || rest != " (in 9 hours): your Claude allowance has run out." { |
| 55 | t.Fatalf("lead %q %q", strong, rest) |
| 56 | } |
| 57 | if strings.Contains(strong+rest, "UTC") { |
| 58 | t.Fatal("UTC in the lead") |
| 59 | } |
| 60 | if got := Short(copyOf, now, reset, loc); got != "21:00" { |
| 61 | t.Fatalf("short %q", got) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // The same reset in summer is 22:00 in Dublin, and in Madrid an hour on |
| 66 | // again: the zone, not UTC, decides the words. |
| 67 | func TestWhenFollowsTheZone(t *testing.T) { |
| 68 | now := time.Date(2026, 7, 14, 11, 0, 0, 0, time.UTC) |
| 69 | reset := time.Date(2026, 7, 14, 21, 0, 0, 0, time.UTC) |
| 70 | if got := When(copyOf, now, reset, dublin(t)); got != "22:00 tonight" { |
| 71 | t.Fatalf("Dublin in summer %q", got) |
| 72 | } |
| 73 | madrid, _ := time.LoadLocation("Europe/Madrid") |
| 74 | if got := When(copyOf, now, reset, madrid); got != "23:00 tonight" { |
| 75 | t.Fatalf("Madrid %q", got) |
| 76 | } |
| 77 | tokyo, _ := time.LoadLocation("Asia/Tokyo") |
| 78 | // 21:00 UTC is 06:00 the next day in Tokyo, where it is 20:00 now. |
| 79 | if got := When(copyOf, now, reset, tokyo); got != "tomorrow at 06:00" { |
| 80 | t.Fatalf("Tokyo %q", got) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestWhenWords(t *testing.T) { |
| 85 | loc := dublin(t) |
| 86 | now := time.Date(2026, 11, 24, 12, 0, 0, 0, time.UTC) // Tuesday |
| 87 | for _, c := range []struct { |
| 88 | at time.Time |
| 89 | when, short string |
| 90 | }{ |
| 91 | {time.Date(2026, 11, 24, 15, 0, 0, 0, time.UTC), "today at 15:00", "15:00"}, |
| 92 | {time.Date(2026, 11, 24, 18, 0, 0, 0, time.UTC), "18:00 tonight", "18:00"}, |
| 93 | {time.Date(2026, 11, 25, 0, 0, 0, 0, time.UTC), "midnight", "midnight"}, |
| 94 | {time.Date(2026, 11, 25, 9, 0, 0, 0, time.UTC), "tomorrow at 09:00", "09:00"}, |
| 95 | {time.Date(2026, 11, 26, 3, 0, 0, 0, time.UTC), "Thu 03:00", "Thursday"}, |
| 96 | {time.Date(2026, 11, 26, 0, 0, 0, 0, time.UTC), "Thu 00:00", "Thursday"}, |
| 97 | } { |
| 98 | if got := When(copyOf, now, c.at, loc); got != c.when { |
| 99 | t.Errorf("When(%v) = %q, want %q", c.at, got, c.when) |
| 100 | } |
| 101 | if got := Short(copyOf, now, c.at, loc); got != c.short { |
| 102 | t.Errorf("Short(%v) = %q, want %q", c.at, got, c.short) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestInWords(t *testing.T) { |
| 108 | for _, c := range []struct { |
| 109 | d time.Duration |
| 110 | want string |
| 111 | }{ |
| 112 | {0, "in 1 minute"}, |
| 113 | {20 * time.Second, "in 1 minute"}, |
| 114 | {time.Minute, "in 1 minute"}, |
| 115 | {40 * time.Minute, "in 40 minutes"}, |
| 116 | {59*time.Minute + 50*time.Second, "in 1 hour"}, |
| 117 | {time.Hour, "in 1 hour"}, |
| 118 | {90 * time.Minute, "in 2 hours"}, |
| 119 | {9 * time.Hour, "in 9 hours"}, |
| 120 | {23*time.Hour + 10*time.Minute, "in 23 hours"}, |
| 121 | {25 * time.Hour, "in 1 day"}, |
| 122 | {39 * time.Hour, "in 2 days"}, |
| 123 | {72 * time.Hour, "in 3 days"}, |
| 124 | } { |
| 125 | if got := In(copyOf, c.d); got != c.want { |
| 126 | t.Errorf("In(%v) = %q, want %q", c.d, got, c.want) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // The bold part of a lead ends at its first colon (kept) or at its first |
| 132 | // " (" (not kept); the full stop is never bold. |
| 133 | func TestLeadSplits(t *testing.T) { |
| 134 | for _, c := range []struct { |
| 135 | key string |
| 136 | args []any |
| 137 | strong, rest string |
| 138 | }{ |
| 139 | {"work.pause.since", []any{"11:20", "Paul’s Mac mini, which runs this work, is offline"}, "Paused since 11:20:", " Paul’s Mac mini, which runs this work, is offline."}, |
| 140 | {"work.pause.moment", []any{"TensorX is busy with another step for you"}, "Paused for a moment:", " TensorX is busy with another step for you."}, |
| 141 | {"work.pause.lead.stopped", []any{"10:42", "the push to Amadan failed"}, "Stopped at 10:42:", " the push to Amadan failed"}, |
| 142 | {"work.pause.lead.spend_unknown", []any{"TensorX"}, "Paused:", " Equipo can’t tell how much TensorX has spent."}, |
| 143 | } { |
| 144 | s, r := Lead(copyOf, c.key, c.args...) |
| 145 | if s != c.strong || r != c.rest { |
| 146 | t.Errorf("%s: %q %q", c.key, s, r) |
| 147 | } |
| 148 | } |
| 149 | // A template that cannot be split is all rest, never lost. |
| 150 | words["x.plain"] = "No colon here %s" |
| 151 | if s, r := Lead(copyOf, "x.plain", "at all"); s != "" || r != "No colon here at all" { |
| 152 | t.Errorf("plain: %q %q", s, r) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func TestWhenIn(t *testing.T) { |
| 157 | loc := dublin(t) |
| 158 | now := time.Date(2026, 11, 24, 12, 0, 0, 0, time.UTC) |
| 159 | at := time.Date(2026, 11, 26, 3, 0, 0, 0, time.UTC) |
| 160 | if got := WhenIn(copyOf, now, at, loc); got != "Thu 03:00 (in 2 days)" { |
| 161 | t.Fatalf("%q", got) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // At is the time Base's sentence names: the clock today or tomorrow, |
| 166 | // midnight, else the day and the clock. |
| 167 | func TestAtWords(t *testing.T) { |
| 168 | loc := dublin(t) |
| 169 | now := time.Date(2026, 11, 24, 12, 0, 0, 0, time.UTC) |
| 170 | for at, want := range map[time.Time]string{ |
| 171 | time.Date(2026, 11, 24, 21, 0, 0, 0, time.UTC): "21:00", |
| 172 | time.Date(2026, 11, 25, 0, 0, 0, 0, time.UTC): "midnight", |
| 173 | time.Date(2026, 11, 25, 9, 30, 0, 0, time.UTC): "09:30", |
| 174 | time.Date(2026, 11, 26, 3, 0, 0, 0, time.UTC): "Thu 03:00", |
| 175 | } { |
| 176 | if got := At(copyOf, now, at, loc); got != want { |
| 177 | t.Errorf("At(%v) = %q, want %q", at, got, want) |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |