oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "bytes"
5 "regexp"
6 "slices"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/equipo/chat"
12 "amadan.net/oficina/equipo/internal/wire"
13)
14
15// labelConns are Paul's connections as he reported them (2026-11-12): a
16// Claude account the provider named, a token he typed an email for, two
17// tokens signed in by link with no email (made in that order), two
18// Codex accounts and a TensorX key.
19func labelConns() ([]wire.Connection, map[string]string) {
20 at := usageNow.Add(-48 * time.Hour)
21 return []wire.Connection{
22 {ID: "cn_tok_b", LocalID: "claude-4", RunnerID: "r1", Provider: wire.ProviderClaude, Kind: wire.KindToken, State: wire.ConnHealthy, CreatedAt: at.Add(4 * time.Hour)},
23 {ID: "cn_mail", LocalID: "claude-1", RunnerID: "r1", Provider: wire.ProviderClaude, Kind: wire.KindSubscription, Label: "paul@tito.io", State: wire.ConnHealthy, CreatedAt: at},
24 {ID: "cn_said", LocalID: "claude-2", RunnerID: "r1", Provider: wire.ProviderClaude, Kind: wire.KindToken, State: wire.ConnHealthy, CreatedAt: at.Add(time.Hour)},
25 {ID: "cn_tok_a", LocalID: "claude-3", RunnerID: "r1", Provider: wire.ProviderClaude, Kind: wire.KindToken, State: wire.ConnHealthy, CreatedAt: at.Add(2 * time.Hour)},
26 {ID: "cn_cx", LocalID: "codex-1", RunnerID: "r1", Provider: wire.ProviderCodex, Kind: wire.KindSubscription, Label: "paul@keymail.dev", State: wire.ConnHealthy, CreatedAt: at},
27 {ID: "cn_cx2", LocalID: "codex-2", RunnerID: "r1", Provider: wire.ProviderCodex, Kind: wire.KindSubscription, State: wire.ConnHealthy, CreatedAt: at.Add(time.Hour)},
28 {ID: "cn_tx", LocalID: "tensorx-1", RunnerID: "r1", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, Billing: wire.BillingMetered, State: wire.ConnHealthy, Budget: &wire.Budget{Day: 1_000_000, Month: 10_000_000, Currency: "USD", Revision: 1}, Spend: &wire.Spend{Day: 840_000, Month: 6_200_000, Currency: "USD"}, CreatedAt: at},
29 }, map[string]string{"cn_said": "me@tito.io"}
30}
31
32// labelPages renders the Connections and Usage pages from the same
33// connections.
34func labelPages(t *testing.T) (connections, usage string) {
35 t.Helper()
36 conns, stated := labelConns()
37 snap := wire.Snapshot{Runner: wire.Runner{ID: "r1", Online: true}, Connections: conns}
38 cv := connectionsPage(snap, nil, wire.Preferences{}, "", stated, usageNow)
39 rowFacts(&cv, snap, chat.RowFacts{Stated: stated, Served: map[string]bool{}})
40 var buf bytes.Buffer
41 if err := pages["models"].ExecuteTemplate(&buf, "content", pageData{Models: cv}); err != nil {
42 t.Fatal(err)
43 }
44 connections = buf.String()
45 month := wire.UsageTotals{Period: wire.UsageMonth, By: wire.UsageByConnection}
46 for _, c := range conns {
47 month.Rows = append(month.Rows, wire.UsageRow{ID: c.ID, Label: c.Provider, Input: 10, Output: 1})
48 }
49 uv := usagePage(conns, connLabels(conns, stated), month, month, nil, usageNow)
50 buf.Reset()
51 if err := pages["usage"].ExecuteTemplate(&buf, "content", pageData{Usage: uv}); err != nil {
52 t.Fatal(err)
53 }
54 return connections, buf.String()
55}
56
57var connLabelAttr = regexp.MustCompile(`data-conn-label="([^"]*)"`)
58
59// labelsIn are the connection labels a page shows, in order, each once.
60func labelsIn(html string) []string {
61 var out []string
62 for _, m := range connLabelAttr.FindAllStringSubmatch(html, -1) {
63 if l := strings.ReplaceAll(m[1], "+", "+"); !slices.Contains(out, l) {
64 out = append(out, l)
65 }
66 }
67 return out
68}
69
70// usageItem is the Usage page's "available now" item for id.
71func usageItem(t *testing.T, html, id string) string {
72 t.Helper()
73 i := strings.Index(html, `data-available="`+id+`"`)
74 if i < 0 {
75 t.Fatalf("no item for %s", id)
76 }
77 end := strings.Index(html[i:], "</li>")
78 return html[i : i+end]
79}
80
81// A connection the provider named an email for reads the same on the
82// Usage page as on the Connections page: "Claude (paul@tito.io)".
83func TestLabelProviderEmailSameOnUsageAndConnections(t *testing.T) {
84 conns, usage := labelPages(t)
85 want := "Claude (paul@tito.io)"
86 if !slices.Contains(labelsIn(conns), want) || !slices.Contains(labelsIn(usage), want) {
87 t.Fatalf("connections %q, usage %q", labelsIn(conns), labelsIn(usage))
88 }
89 if !strings.Contains(conns, `<span equipo-conn-name>Claude</span><span equipo-conn-account data-conn-label="Claude (paul@tito.io)">paul@tito.io</span>`) {
90 t.Fatal("the Connections row no longer renders the account as it did")
91 }
92 if item := usageItem(t, usage, "cn_mail"); !strings.Contains(item, "<span>Claude (paul@tito.io)</span>") || strings.Contains(item, "data-add-email") {
93 t.Fatalf("usage item %s", item)
94 }
95}
96
97// An email the person typed shows "(added by you)" on the Usage page,
98// just as on the Connections row.
99func TestLabelStatedEmailOnUsage(t *testing.T) {
100 conns, usage := labelPages(t)
101 said := `<span equipo-conn-said>(added by you)</span>`
102 if !strings.Contains(conns, `<span data-stated-email>me@tito.io</span> `+said) {
103 t.Fatal("the Connections row lost the typed email")
104 }
105 if item := usageItem(t, usage, "cn_said"); !strings.Contains(item, `<span>Claude (me@tito.io) `+said+`</span>`) {
106 t.Fatalf("usage item %s", item)
107 }
108 if !slices.Contains(labelsIn(usage), "Claude (me@tito.io) (added by you)") {
109 t.Fatalf("usage labels %q", labelsIn(usage))
110 }
111}
112
113// A token connection with no email reads "Claude (signed in by link)"
114// and links to its own Connections row, where the email field is.
115func TestLabelTokenWithNoEmail(t *testing.T) {
116 conns, usage := labelPages(t)
117 item := usageItem(t, usage, "cn_tok_a")
118 if !strings.Contains(item, "<span>Claude (signed in by link)</span>") {
119 t.Fatalf("usage item %s", item)
120 }
121 if !strings.Contains(item, `<a equipo-link href="/conversation/settings/connections#connection-cn_tok_a" data-add-email="cn_tok_a">Add the account email</a>`) {
122 t.Fatalf("no add-email link: %s", item)
123 }
124 if !strings.Contains(conns, `<div equipo-conn-body id="connection-cn_tok_a">`) || !strings.Contains(conns, `data-account-email="cn_tok_a"`) {
125 t.Fatal("the link's row or its email field is missing on Connections")
126 }
127 if !strings.Contains(conns, `data-conn-label="Claude (signed in by link)">signed in by link</span>`) {
128 t.Fatal("the Connections row does not say signed in by link")
129 }
130}
131
132// Two token connections with no email get distinct numbered labels, the
133// first made keeping the plain form, in the same order on both pages;
134// no two of the person's connections read alike anywhere.
135func TestLabelTwoTokensNumberedAlike(t *testing.T) {
136 conns, usage := labelPages(t)
137 want := []string{"Claude (signed in by link, 2)", "Claude (paul@tito.io)", "Claude (me@tito.io) (added by you)", "Claude (signed in by link)", "Codex (paul@keymail.dev)", "Codex", "TensorX"}
138 if got := labelsIn(conns); !slices.Equal(got, want) {
139 t.Fatalf("connections %q", got)
140 }
141 // The Usage page lists the same connections in the same order.
142 var avail []string
143 for _, id := range []string{"cn_tok_b", "cn_mail", "cn_said", "cn_tok_a", "cn_cx", "cn_cx2", "cn_tx"} {
144 m := connLabelAttr.FindStringSubmatch(usageItem(t, usage, id))
145 avail = append(avail, m[1])
146 }
147 if !slices.Equal(avail, want) {
148 t.Fatalf("usage %q", avail)
149 }
150 if strings.Count(usage, `data-conn-label="Claude (signed in by link)"`) != strings.Count(usage, `data-conn-label="Claude (signed in by link, 2)"`) {
151 t.Fatal("the two tokens are not named alike on every Usage part")
152 }
153}
154
155// Every connection the Usage page names carries its provider's mark,
156// the Connections page's monogram tile, hidden from screen readers.
157func TestLabelUsageRowsCarryTheProviderMark(t *testing.T) {
158 _, usage := labelPages(t)
159 for id, mark := range map[string]string{"cn_mail": "Cl", "cn_tok_b": "Cl", "cn_cx2": "Cx", "cn_tx": "Tx"} {
160 item := usageItem(t, usage, id)
161 provider := map[string]string{"Cl": "claude", "Cx": "codex", "Tx": "tensorx"}[mark]
162 if !strings.Contains(item, `<span equipo-pmark data-provider="`+provider+`" aria-hidden="true">`+mark+`</span>`) {
163 t.Fatalf("%s lacks its %s mark: %s", id, mark, item)
164 }
165 }
166 // The spend, plan and token-table rows carry it too.
167 if n := strings.Count(usage, `<span equipo-conn-label `); n != strings.Count(usage, `<span equipo-pmark `) || n < 7*3 {
168 t.Fatalf("%d labels, %d marks", n, strings.Count(usage, `<span equipo-pmark `))
169 }
170}
171
172// Advice and the scheduler's hold lines name a connection by the shared
173// label, with its mark: the advice card on Usage, and a hold on the
174// task creator's own pages (another person sees the provider).
175func TestLabelAdviceAndHoldLines(t *testing.T) {
176 conns, stated := labelConns()
177 labels := connLabels(conns, stated)
178 adv := &wire.Advice{ID: "ad_1", Kind: wire.AdviceBudgetNear, Subject: wire.AdviceSubject{Connection: "cn_tx"}, Text: "TensorX: $0.84 of today’s $1 budget used.", Act: "Raise", Dismiss: "Leave it", Revision: 1}
179 uv := usagePage(conns, labels, wire.UsageTotals{}, wire.UsageTotals{}, adv, usageNow)
180 if uv.AdviceTag == nil || uv.AdviceTag.Text != "TensorX" {
181 t.Fatalf("advice tag %+v", uv.AdviceTag)
182 }
183 var buf bytes.Buffer
184 if err := pages["usage"].ExecuteTemplate(&buf, "content", pageData{Usage: uv}); err != nil {
185 t.Fatal(err)
186 }
187 if !strings.Contains(buf.String(), `<span equipo-pmark data-provider="tensorx" aria-hidden="true">Tx</span><span data-advice-text>`) {
188 t.Fatal("the advice card lacks its connection's mark")
189 }
190 reset := usageNow.Add(3 * time.Hour)
191 step := wire.Step{ID: "s1", RunnerID: "r1", Hold: &wire.StepHold{Reason: wire.HoldHeadroom, Connection: "claude-4", Provider: wire.ProviderClaude, ResetsAt: &reset, StepRevision: 1}}
192 task := wire.Task{ID: "k1", Creator: "me@example.com", Stage: wire.StageInProgress, Steps: []wire.Step{step}}
193 v := taskLineOf(task, "me@example.com")
194 v.richOf(task, "me@example.com", nil, usageNow)
195 if v.Hold == nil || !strings.HasPrefix(v.Hold.Line, "Waiting for your Claude allowance") {
196 t.Fatalf("before naming %+v", v.Hold)
197 }
198 holdNamed(&v, conns, labels, defaultZone())
199 if want := "Waiting for your Claude (signed in by link, 2) allowance to reset " + resetText(reset, defaultZone()); v.Hold.Line != want || v.StageLine != want || v.Hold.Tag == nil || v.Hold.Tag.ID != "cn_tok_b" {
200 t.Fatalf("hold %+v, stage %q", v.Hold, v.StageLine)
201 }
202 for reason, want := range map[string]string{
203 wire.HoldBudget: "Waiting: Claude (signed in by link, 2) has reached its budget",
204 wire.HoldSpendUnknown: "Waiting: spend unknown. Check your spending at Claude (signed in by link, 2), then choose Resume",
205 wire.HoldBusy: "Waiting: Claude (signed in by link, 2) is running another step",
206 } {
207 s := step
208 h := *step.Hold
209 h.Reason, s.Hold = reason, &h
210 if got := holdOf(s, true, chat.HoldLabel(conns, labels, s.RunnerID, h.Connection, h.Provider), defaultZone()).Line; got != want {
211 t.Fatalf("%s: %q", reason, got)
212 }
213 }
214 // The stage line renders with the connection's mark.
215 v.StageTag = v.Hold.Tag
216 v.Say = v.StageLine
217 buf.Reset()
218 if err := pages["tasks"].ExecuteTemplate(&buf, "work-task-row", v); err != nil {
219 t.Fatal(err)
220 }
221 if !strings.Contains(buf.String(), `<span equipo-conn-label><span equipo-pmark data-provider="claude" aria-hidden="true">Cl</span><span>Waiting for your Claude (signed in by link, 2)`) {
222 t.Fatalf("hold row %s", buf.String())
223 }
224}
225