| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // Paul's screenshot, Tue 2026-11-10 22:30 UTC: Claude paul@tito.io Ready |
| 13 | // (its week window reported full, resetting Tue 21:00, which has passed); |
| 14 | // Codex paul@tito.io promax limited until Thu 09:36; Codex |
| 15 | // paul@keymail.dev prolite Ready, the coordinator's last turn on it. |
| 16 | var screenshotNow = time.Date(2026, 11, 10, 22, 30, 0, 0, time.UTC) |
| 17 | |
| 18 | func screenshotSnapshot() wire.Snapshot { |
| 19 | weekReset := time.Date(2026, 11, 10, 21, 0, 0, 0, time.UTC) |
| 20 | codexReset := time.Date(2026, 11, 12, 9, 36, 0, 0, time.UTC) |
| 21 | return wire.Snapshot{ |
| 22 | Runner: wire.Runner{ID: "r1", Online: true}, |
| 23 | Connections: []wire.Connection{ |
| 24 | {ID: "c-claude", LocalID: "claude-1", Provider: wire.ProviderClaude, Label: "paul@tito.io", State: wire.ConnHealthy, Headroom: &wire.Headroom{Windows: []wire.Window{{Name: "seven_day", UsedPercent: 100, Minutes: 7 * 24 * 60, ResetsAt: &weekReset}}}}, |
| 25 | {ID: "c-promax", LocalID: "codex-1", Provider: wire.ProviderCodex, Label: "paul@tito.io", Plan: "promax", State: wire.ConnLimited, Headroom: &wire.Headroom{ResetsAt: &codexReset, Windows: []wire.Window{{Name: "secondary", UsedPercent: 100, Minutes: 7 * 24 * 60, ResetsAt: &codexReset}}}}, |
| 26 | {ID: "c-prolite", LocalID: "codex-2", Provider: wire.ProviderCodex, Label: "paul@keymail.dev", Plan: "prolite", State: wire.ConnHealthy}, |
| 27 | }, |
| 28 | Turns: []wire.Turn{ |
| 29 | {ID: "t1", State: wire.TurnReplied, Connection: "codex-1", UpdatedAt: screenshotNow.Add(-3 * time.Hour)}, |
| 30 | {ID: "t2", State: wire.TurnReplied, Connection: "codex-2", UpdatedAt: screenshotNow.Add(-10 * time.Minute)}, |
| 31 | }, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func rowOf(t *testing.T, v modelsView, id string) connRow { |
| 36 | t.Helper() |
| 37 | for _, r := range v.Rows { |
| 38 | if r.ID == id { |
| 39 | return r |
| 40 | } |
| 41 | } |
| 42 | t.Fatalf("no row %s", id) |
| 43 | return connRow{} |
| 44 | } |
| 45 | |
| 46 | func lead(a connAnswering) string { return a.LeadStart + a.LeadName + a.LeadEnd } |
| 47 | |
| 48 | // Bug 1: with two Codex connections, the summary names the account, and |
| 49 | // the resting one is the other Codex account, not "Codex". |
| 50 | func TestConnectionsSummaryNamesTheAccount(t *testing.T) { |
| 51 | v := connectionsPage(screenshotSnapshot(), nil, wire.Preferences{}, "Tito Studio", nil, screenshotNow) |
| 52 | want := "Your coordinator is on Codex (paul@keymail.dev). Your other Codex account is back in 1 day 11 hours." |
| 53 | if got := lead(v.Answering); got != want { |
| 54 | t.Fatalf("lead %q, want %q", got, want) |
| 55 | } |
| 56 | if v.Answering.LeadName != "Codex (paul@keymail.dev)" { |
| 57 | t.Fatalf("bold name %q", v.Answering.LeadName) |
| 58 | } |
| 59 | // One connection of a provider: the provider alone. |
| 60 | snap := screenshotSnapshot() |
| 61 | snap.Connections = snap.Connections[:2] |
| 62 | snap.Turns = snap.Turns[:0] |
| 63 | one := connectionsPage(snap, nil, wire.Preferences{}, "Tito Studio", nil, screenshotNow) |
| 64 | if got := lead(one.Answering); got != "Your coordinator is on Claude. Codex is back in 1 day 11 hours." { |
| 65 | t.Fatalf("one of each: %q", got) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Bug 2: a window whose reset has passed shows no stale percentage or |
| 70 | // reset; nor does a full window on a connection that answers. |
| 71 | func TestConnectionsStaleMeterAfterReset(t *testing.T) { |
| 72 | v := connectionsPage(screenshotSnapshot(), nil, wire.Preferences{}, "Tito Studio", nil, screenshotNow) |
| 73 | if m := rowOf(t, v, "c-claude").Meters; len(m) != 0 { |
| 74 | t.Fatalf("Claude shows a stale meter after its reset: %+v", m) |
| 75 | } |
| 76 | // The resting Codex keeps its reported window: its reset is ahead. |
| 77 | if m := rowOf(t, v, "c-promax").Meters; len(m) != 1 || m[0].Used != 100 || m[0].Resets != "resets in 1 day 11 hours" || m[0].At != "Thu 09:36" { |
| 78 | t.Fatalf("promax meters %+v", m) |
| 79 | } |
| 80 | snap := screenshotSnapshot() |
| 81 | ahead := screenshotNow.Add(30 * time.Hour) |
| 82 | snap.Connections[0].Headroom = &wire.Headroom{Windows: []wire.Window{ |
| 83 | {Name: "five_hour", UsedPercent: 40, Minutes: 300, ResetsAt: &ahead}, |
| 84 | {Name: "seven_day", UsedPercent: 100, Minutes: 7 * 24 * 60, ResetsAt: &ahead}, |
| 85 | }} |
| 86 | m := rowOf(t, connectionsPage(snap, nil, wire.Preferences{}, "", nil, screenshotNow), "c-claude").Meters |
| 87 | if len(m) != 1 || m[0].Used != 40 { |
| 88 | t.Fatalf("a Ready connection shows a full window, or lost its fresh one: %+v", m) |
| 89 | } |
| 90 | // A limited connection whose reset has passed is not "until" then. |
| 91 | past := screenshotNow.Add(-time.Hour) |
| 92 | snap.Connections[1].Headroom = &wire.Headroom{ResetsAt: &past} |
| 93 | if w := rowOf(t, connectionsPage(snap, nil, wire.Preferences{}, "", nil, screenshotNow), "c-promax").Words; w != copyText("chat.connections.limited") { |
| 94 | t.Fatalf("limited words %q", w) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Bug 3: the resting row says where its work went: the connection the |
| 99 | // coordinator last answered on, named by account, not the first healthy. |
| 100 | func TestConnectionsRestingDestination(t *testing.T) { |
| 101 | v := connectionsPage(screenshotSnapshot(), nil, wire.Preferences{}, "Tito Studio", nil, screenshotNow) |
| 102 | if got := rowOf(t, v, "c-promax").RolesNote; got != "Resting. Its work moved to Codex (paul@keymail.dev)." { |
| 103 | t.Fatalf("resting note %q", got) |
| 104 | } |
| 105 | if r := rowOf(t, v, "c-prolite"); len(r.Roles) != 1 || r.Roles[0].Name != copyText("conversation.coordinator") { |
| 106 | t.Fatalf("keymail roles %+v", r.Roles) |
| 107 | } |
| 108 | if r := rowOf(t, v, "c-claude"); len(r.Roles) != 0 { |
| 109 | t.Fatalf("Claude claims a role %+v", r.Roles) |
| 110 | } |
| 111 | // A step running on Claude: the work went there too. |
| 112 | snap := screenshotSnapshot() |
| 113 | snap.Tasks = []wire.Task{{ID: "k1", Steps: []wire.Step{{ID: "s1", Specialist: wire.SpecialistEngineering, State: wire.StepRunning, Account: "claude-1"}}}} |
| 114 | v = connectionsPage(snap, nil, wire.Preferences{}, "", nil, screenshotNow) |
| 115 | if got := rowOf(t, v, "c-promax").RolesNote; got != "Resting. Its work moved to Codex (paul@keymail.dev) and Claude." { |
| 116 | t.Fatalf("resting note with a step %q", got) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Bug 4: under Automatic there is no "moves back": work stays until its |
| 121 | // next message or step, then the healthiest with headroom is picked. |
| 122 | // With a preference, the summary may say work moves to it. |
| 123 | func TestConnectionsSummaryNoInventedPreference(t *testing.T) { |
| 124 | v := connectionsPage(screenshotSnapshot(), nil, wire.Preferences{}, "Tito Studio", nil, screenshotNow) |
| 125 | if strings.Contains(v.Answering.Sub, "moves back") || !strings.Contains(v.Answering.Sub, copyText("chat.connections.answering.stays")) { |
| 126 | t.Fatalf("automatic sub %q", v.Answering.Sub) |
| 127 | } |
| 128 | v = connectionsPage(screenshotSnapshot(), nil, wire.Preferences{Provider: wire.ProviderClaude, Revision: 1}, "Tito Studio", nil, screenshotNow) |
| 129 | if !strings.Contains(v.Answering.Sub, "Work moves to Claude, your preference,") || strings.Contains(v.Answering.Sub, copyText("chat.connections.answering.stays")) { |
| 130 | t.Fatalf("preferred sub %q", v.Answering.Sub) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // An ended attempt's note (Paul, 2026-11-10: "The sign-in was not |
| 135 | // finished in time." stayed under the list) gives way to a newer |
| 136 | // attempt: one connecting, or one that connected. |
| 137 | func TestConnectionsEndedNoteClears(t *testing.T) { |
| 138 | snap := screenshotSnapshot() |
| 139 | ended := wire.Connection{ID: "c-ended", Provider: wire.ProviderClaude, State: wire.ConnError, Reason: "timed_out", CreatedAt: screenshotNow.Add(-20 * time.Minute), UpdatedAt: screenshotNow.Add(-5 * time.Minute)} |
| 140 | snap.Connections = append(snap.Connections, ended) |
| 141 | v := connectionsPage(snap, nil, wire.Preferences{}, "", nil, screenshotNow) |
| 142 | if len(v.Ended) != 1 || v.Ended[0].Note != "The sign-in was not finished in time." { |
| 143 | t.Fatalf("ended %+v", v.Ended) |
| 144 | } |
| 145 | for _, state := range []string{wire.ConnConnecting, wire.ConnHealthy} { |
| 146 | later := append([]wire.Connection{}, snap.Connections...) |
| 147 | later = append(later, wire.Connection{ID: "c-new", LocalID: map[string]string{wire.ConnHealthy: "claude-2"}[state], Provider: wire.ProviderClaude, State: state, CreatedAt: screenshotNow.Add(-time.Minute), UpdatedAt: screenshotNow}) |
| 148 | s := snap |
| 149 | s.Connections = later |
| 150 | if v := connectionsPage(s, nil, wire.Preferences{}, "", nil, screenshotNow); len(v.Ended) != 0 { |
| 151 | t.Fatalf("a %s attempt after it leaves the note: %+v", state, v.Ended) |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Paul, 2026-11-11: "Can we get Resets in Xdays Yhours". Days and hours |
| 157 | // from a day, hours and minutes under one, minutes under an hour; a zero |
| 158 | // part left out, singular and plural. |
| 159 | func TestInText(t *testing.T) { |
| 160 | for _, c := range []struct { |
| 161 | d time.Duration |
| 162 | want string |
| 163 | }{ |
| 164 | {0, "under a minute"}, |
| 165 | {59 * time.Second, "under a minute"}, |
| 166 | {time.Minute, "1 minute"}, |
| 167 | {12 * time.Minute, "12 minutes"}, |
| 168 | {59*time.Minute + 59*time.Second, "59 minutes"}, |
| 169 | {time.Hour, "1 hour"}, |
| 170 | {time.Hour + time.Minute, "1 hour 1 minute"}, |
| 171 | {5*time.Hour + 20*time.Minute, "5 hours 20 minutes"}, |
| 172 | {23*time.Hour + 59*time.Minute, "23 hours 59 minutes"}, |
| 173 | {24 * time.Hour, "1 day"}, |
| 174 | {24*time.Hour + 59*time.Minute, "1 day"}, |
| 175 | {25 * time.Hour, "1 day 1 hour"}, |
| 176 | {49 * time.Hour, "2 days 1 hour"}, |
| 177 | {51*time.Hour + 30*time.Minute, "2 days 3 hours"}, |
| 178 | {-time.Minute, "under a minute"}, |
| 179 | } { |
| 180 | if got := inText(c.d); got != c.want { |
| 181 | t.Errorf("inText(%v) = %q, want %q", c.d, got, c.want) |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // A limited row says when it is back once: the meter counts down and the |
| 187 | // status says "Limited"; without a meter the status counts down. Each |
| 188 | // keeps the time as its hover title. |
| 189 | func TestConnectionsLimitedRowCountsDown(t *testing.T) { |
| 190 | now := time.Date(2026, 11, 11, 11, 31, 0, 0, time.UTC) |
| 191 | reset := time.Date(2026, 11, 12, 9, 36, 0, 0, time.UTC) |
| 192 | snap := screenshotSnapshot() |
| 193 | snap.Connections[1].Headroom = &wire.Headroom{ResetsAt: &reset, Windows: []wire.Window{{Name: "secondary", UsedPercent: 100, Minutes: 7 * 24 * 60, ResetsAt: &reset}}} |
| 194 | r := rowOf(t, connectionsPage(snap, nil, wire.Preferences{}, "", nil, now), "c-promax") |
| 195 | if r.Words != "Limited" || r.WordsAt != "" || len(r.Meters) != 1 || r.Meters[0].Resets != "resets in 22 hours 5 minutes" || r.Meters[0].At != "Thu 09:36" { |
| 196 | t.Fatalf("metered limited row: %q (%q) %+v", r.Words, r.WordsAt, r.Meters) |
| 197 | } |
| 198 | snap.Connections[1].Headroom = &wire.Headroom{ResetsAt: &reset} |
| 199 | r = rowOf(t, connectionsPage(snap, nil, wire.Preferences{}, "", nil, now), "c-promax") |
| 200 | if r.Words != "Limited, resets in 22 hours 5 minutes" || r.WordsAt != "Thu 09:36" || len(r.Meters) != 0 { |
| 201 | t.Fatalf("limited row without a meter: %q (%q) %+v", r.Words, r.WordsAt, r.Meters) |
| 202 | } |
| 203 | // A meter without a reset time leaves the countdown to the status. |
| 204 | snap.Connections[1].Headroom = &wire.Headroom{ResetsAt: &reset, Windows: []wire.Window{{Name: "secondary", UsedPercent: 100}}} |
| 205 | if r = rowOf(t, connectionsPage(snap, nil, wire.Preferences{}, "", nil, now), "c-promax"); r.Words != "Limited, resets in 22 hours 5 minutes" { |
| 206 | t.Fatalf("limited row with an untimed meter: %q", r.Words) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // The answering card counts down too: "is back in", the time in its |
| 211 | // title; and when nothing can answer, "resets in". |
| 212 | func TestConnectionsAnsweringCountsDown(t *testing.T) { |
| 213 | now := time.Date(2026, 11, 11, 11, 31, 0, 0, time.UTC) |
| 214 | v := connectionsPage(screenshotSnapshot(), nil, wire.Preferences{}, "", nil, now) |
| 215 | if got := lead(v.Answering); got != "Your coordinator is on Codex (paul@keymail.dev). Your other Codex account is back in 22 hours 5 minutes." { |
| 216 | t.Fatalf("lead %q", got) |
| 217 | } |
| 218 | if v.Answering.LeadAt != "Your other Codex account resets Thu 09:36" { |
| 219 | t.Fatalf("lead title %q", v.Answering.LeadAt) |
| 220 | } |
| 221 | snap := screenshotSnapshot() |
| 222 | snap.Connections = snap.Connections[1:2] |
| 223 | snap.Turns = nil |
| 224 | v = connectionsPage(snap, nil, wire.Preferences{}, "", nil, now) |
| 225 | if got := lead(v.Answering); got != "Nothing can answer right now. Codex resets in 22 hours 5 minutes." || v.Answering.LeadAt != "Codex resets Thu 09:36" { |
| 226 | t.Fatalf("nothing answers: %q (%q)", got, v.Answering.LeadAt) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // The conversation's lines are history under old messages: they keep the |
| 231 | // time, not a countdown, in the person's zone with no UTC (paused work, |
| 232 | // decision 4). |
| 233 | func TestConnectionsChatLinesStayAbsolute(t *testing.T) { |
| 234 | reset := time.Date(2026, 11, 12, 9, 36, 0, 0, time.UTC) |
| 235 | if got := switchedText(&wire.Switched{From: wire.ProviderCodex, To: wire.ProviderClaude, ResetsAt: &reset}, defaultZone()); got != "Switched to Claude; Codex resets Thu 09:36" { |
| 236 | t.Fatalf("switched %q", got) |
| 237 | } |
| 238 | if got := noConnectionText([]chat.NoConnectionLine{{Provider: wire.ProviderCodex, State: wire.ConnLimited, ResetsAt: &reset}}, defaultZone()); got != "Nothing can answer right now. Codex resets Thu 09:36." { |
| 239 | t.Fatalf("no connection %q", got) |
| 240 | } |
| 241 | madrid, _ := time.LoadLocation("Europe/Madrid") |
| 242 | if got := switchedText(&wire.Switched{From: wire.ProviderCodex, To: wire.ProviderClaude, ResetsAt: &reset}, madrid); got != "Switched to Claude; Codex resets Thu 10:36" { |
| 243 | t.Fatalf("switched in Madrid %q", got) |
| 244 | } |
| 245 | } |
| 246 | |