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 "strings"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/equipo/chat"
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13var usageNow = time.Date(2026, 11, 12, 12, 0, 0, 0, time.UTC)
14
15func usageConns() []wire.Connection {
16 week, five := usageNow.Add(70*time.Hour), usageNow.Add(3*time.Hour)
17 codexRead, claudeRead := usageNow.Add(-2*time.Hour), usageNow.Add(-5*time.Hour)
18 return []wire.Connection{
19 {ID: "cn_cx", LocalID: "codex-1", RunnerID: "r1", Provider: wire.ProviderCodex, Kind: wire.KindSubscription, Plan: "pro", State: wire.ConnHealthy, Headroom: &wire.Headroom{Windows: []wire.Window{{Name: "primary", UsedPercent: 42, Minutes: 300, ResetsAt: &five}, {Name: "secondary", UsedPercent: 18, Minutes: 10080, ResetsAt: &week}}, CheckedAt: &codexRead}},
20 {ID: "cn_cl", LocalID: "claude-1", RunnerID: "r1", Provider: wire.ProviderClaude, Kind: wire.KindSubscription, State: wire.ConnHealthy, Headroom: &wire.Headroom{Windows: []wire.Window{{Name: "seven_day", UsedPercent: 81, Minutes: 10080, ResetsAt: &week}}, CheckedAt: &claudeRead}},
21 {ID: "cn_old", LocalID: "codex-2", RunnerID: "r1", Provider: wire.ProviderCodex, Kind: wire.KindSubscription, Label: "second@example.com", State: wire.ConnHealthy, Headroom: &wire.Headroom{Windows: []wire.Window{{Name: "primary", UsedPercent: 10, Minutes: 300, ResetsAt: &five}}}},
22 {ID: "cn_tx", LocalID: "tensorx-1", RunnerID: "r1", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, Harness: wire.HarnessPi, 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"}},
23 {ID: "cn_new", Provider: wire.ProviderCodex, State: wire.ConnConnecting},
24 }
25}
26
27func usageTotals() (wire.UsageTotals, wire.UsageTotals) {
28 cost := int64(6_200_000)
29 month := wire.UsageTotals{Period: wire.UsageMonth, By: wire.UsageByConnection, Rows: []wire.UsageRow{
30 {ID: "cn_cx", Label: "codex", Input: 1_200_000, CachedInput: 800_000, Output: 40_000, Attempts: 20},
31 {ID: "cn_tx", Label: "tensorx", Input: 120_000, Output: 9_000, Cost: &cost, Currency: "USD", Attempts: 7},
32 }, Prices: []wire.PriceSource{{Connection: "cn_tx", PriceVersion: "tensorx-2026-11-12"}}}
33 byRole := wire.UsageTotals{Period: wire.UsageWeek, By: wire.UsageByRole, Rows: []wire.UsageRow{
34 {ID: wire.SpecialistEngineering, Label: wire.SpecialistEngineering, Input: 900_000, Output: 30_000, Partial: true},
35 {ID: "coordinator", Label: "coordinator", Input: 50_000, Output: 2_000},
36 {ID: "probe", Label: "probe", Input: 1, Output: 1, Cost: &cost, Currency: "USD"},
37 }}
38 return byRole, month
39}
40
41// The Usage page from the person's connections and usage: availability
42// with each reading's age (Codex refreshed, Claude as last reported,
43// none from an older runner), spend this month with where the prices
44// come from read from the price table's version, subscriptions as their
45// plan and tokens, and token use in tabs over one dataset.
46func TestUsagePage(t *testing.T) {
47 byRole, month := usageTotals()
48 adv := &wire.Advice{ID: "ad_1", Kind: wire.AdviceBudgetNear, Text: "TensorX: $0.84 of today’s $1.", Act: "Raise today’s budget to $2", Dismiss: "Leave it", Revision: 1}
49 v := usagePage(usageConns(), connLabels(usageConns(), nil), byRole, month, adv, usageNow)
50 if v.Advice != adv || len(v.Available) != 4 || v.NoConnections {
51 t.Fatalf("view %+v", v)
52 }
53 got := map[string]usageConn{}
54 for _, c := range v.Available {
55 got[c.ID] = c
56 }
57 if c := got["cn_cx"]; c.Age != "as of "+ago(usageNow, usageNow.Add(-2*time.Hour)) || len(c.Meters) != 2 {
58 t.Fatalf("codex %+v", c)
59 }
60 if c := got["cn_cl"]; c.Age != "last reported "+ago(usageNow, usageNow.Add(-5*time.Hour)) || len(c.Meters) != 1 {
61 t.Fatalf("claude %+v", c)
62 }
63 if c := got["cn_old"]; c.Age != "" || !strings.Contains(c.Name, "second@example.com") {
64 t.Fatalf("a reading from an older runner says no age: %+v", c)
65 }
66 if c := got["cn_tx"]; c.Spend == "" || c.Age != "" || len(c.Meters) != 0 {
67 t.Fatalf("tensorx %+v", c)
68 }
69 if len(v.Spend) != 1 || v.Spend[0].Source != "TensorX prices taken from the public page on 12 Nov 2026." || !strings.Contains(v.Spend[0].Line, "$0.84") {
70 t.Fatalf("spend %+v", v.Spend)
71 }
72 if len(v.Plans) != 3 || v.Plans[0].Plan != "Pro plan" || !strings.HasPrefix(v.Plans[0].Tokens, "1.2M") {
73 t.Fatalf("plans %+v", v.Plans)
74 }
75 month.Prices[0].PriceVersion = "tensorx-2027-01-03"
76 if again := usagePage(usageConns(), connLabels(usageConns(), nil), byRole, month, adv, usageNow); again.Spend[0].Source != "TensorX prices taken from the public page on 3 Jan 2027." {
77 t.Fatalf("the source does not follow the price table: %q", again.Spend[0].Source)
78 }
79 if v.Column != "Role" || len(v.Rows) != 3 || v.Rows[0].Label != "Engineering" || v.Rows[0].Input != "at least 900k" || v.Rows[1].Label != "Coordinator" || v.Rows[2].Label != "Connection checks" || v.Rows[2].Estimate != "$6.20" || v.Rows[0].Estimate != "" {
80 t.Fatalf("rows %+v", v.Rows)
81 }
82 for _, tab := range append(v.Periods, v.By...) {
83 if tab.On != (tab.Key == wire.UsageWeek || tab.Key == wire.UsageByRole) {
84 t.Fatalf("tab %+v", tab)
85 }
86 }
87 if v.Periods[0].Href != "/usage?by=role&period=day" || v.By[0].Href != "/usage?by=connection&period=week" {
88 t.Fatalf("tabs %+v %+v", v.Periods[0], v.By[0])
89 }
90 // The page renders: the advice first, each part once.
91 data := pageData{Usage: v}
92 var buf bytes.Buffer
93 if err := pages["usage"].ExecuteTemplate(&buf, "content", data); err != nil {
94 t.Fatal(err)
95 }
96 html := buf.String()
97 for _, want := range []string{`data-advice="ad_1"`, `data-advice-act>Raise today’s budget to $2`, `data-reading-age>as of `, `data-price-source>TensorX prices taken from the public page on 12 Nov 2026.`, `data-usage-row="engineering"`, `aria-current="true">This week`} {
98 if !strings.Contains(html, want) {
99 t.Fatalf("page lacks %s", want)
100 }
101 }
102 if strings.Index(html, "data-advice=") > strings.Index(html, "usage-now") {
103 t.Fatal("advice is not at the top")
104 }
105}
106
107// Where prices come from is read from the table's version, never written
108// per provider; a version that names no day says nothing.
109func TestPriceSource(t *testing.T) {
110 for version, want := range map[string]string{
111 "tensorx-2026-11-12": "TensorX prices taken from the public page on 12 Nov 2026.",
112 "tensorx-2027-01-03": "TensorX prices taken from the public page on 3 Jan 2027.",
113 "tensorx": "",
114 "tensorx2026-11-12": "",
115 "": "",
116 } {
117 if got := priceSource(wire.ProviderTensorX, version); got != want {
118 t.Fatalf("%q: %q", version, got)
119 }
120 }
121}
122
123// The sidebar's dot: a connection limited or needing a sign-in, or a
124// metered one paused by a budget or unknown spend; quiet otherwise.
125func TestUsageAttention(t *testing.T) {
126 conns := usageConns()
127 if usageAttention(conns) {
128 t.Fatal("a dot with nothing to look at")
129 }
130 for what, change := range map[string]func([]wire.Connection){
131 "limited": func(cs []wire.Connection) { cs[0].State = wire.ConnLimited },
132 "sign in again": func(cs []wire.Connection) { cs[1].State = wire.ConnSignInAgain },
133 "day's budget": func(cs []wire.Connection) { cs[3].Spend = &wire.Spend{Day: 1_000_000, Month: 6_000_000} },
134 "month's budget": func(cs []wire.Connection) { cs[3].Spend = &wire.Spend{Day: 0, Month: 10_000_000} },
135 "spend unknown": func(cs []wire.Connection) { cs[3].Spend = &wire.Spend{Unknown: true} },
136 } {
137 cs := usageConns()
138 change(cs)
139 if !usageAttention(cs) {
140 t.Fatalf("no dot: %s", what)
141 }
142 }
143}
144
145// The routing line: a step held for headroom says how old the reading it
146// waits on is; a reading with no time says nothing more.
147func TestHoldLineSaysTheReadingsAge(t *testing.T) {
148 reset := usageNow.Add(70 * time.Hour)
149 step := wire.Step{ID: "s1", Hold: &wire.StepHold{Reason: wire.HoldHeadroom, Connection: "claude-1", Provider: wire.ProviderClaude, ResetsAt: &reset, StepRevision: 1}}
150 v := taskLine{Step: &step}
151 v.Hold = holdOf(step, true, chat.HoldLabel(usageConns(), connLabels(usageConns(), nil), "", step.Hold.Connection, step.Hold.Provider), defaultZone())
152 v.StageLine = v.Hold.Line
153 holdReading(&v, usageConns(), usageNow)
154 if !strings.HasPrefix(v.Hold.Line, "Waiting for your Claude allowance to reset") || !strings.HasSuffix(v.Hold.Line, " · last reported "+ago(usageNow, usageNow.Add(-5*time.Hour))) || v.StageLine != v.Hold.Line {
155 t.Fatalf("hold line %q / %q", v.Hold.Line, v.StageLine)
156 }
157 step.Hold.Connection, step.Hold.Provider = "codex-2", wire.ProviderCodex
158 v = taskLine{Step: &step}
159 v.Hold = holdOf(step, true, chat.HoldLabel(usageConns(), connLabels(usageConns(), nil), "", step.Hold.Connection, step.Hold.Provider), defaultZone())
160 before := v.Hold.Line
161 holdReading(&v, usageConns(), usageNow)
162 if v.Hold.Line != before {
163 t.Fatalf("a reading with no time: %q", v.Hold.Line)
164 }
165}
166
167// Spend can be under-counted on a harness that reports a request without
168// usage as zero inside a turn's sum (OpenCode, security round finding 3,
169// accepted as a stated gap): the spend source line says so for such a
170// harness's connections, from the harness table, never per provider.
171// Pi's missing usage already makes spend unknown, so TensorX says
172// nothing.
173func TestUsageSpendUnderCountLine(t *testing.T) {
174 conns := append(usageConns(), wire.Connection{ID: "cn_go", LocalID: "opencode-go-1", RunnerID: "r1", Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, Harness: wire.HarnessOpenCode, 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: 10_000, Month: 20_000, Currency: "USD"}})
175 byRole, month := usageTotals()
176 month.Prices = append(month.Prices, wire.PriceSource{Connection: "cn_go", PriceVersion: "opencode-go-2026-11-12"})
177 v := usagePage(conns, connLabels(conns, nil), byRole, month, nil, usageNow)
178 spend := map[string]usageSpend{}
179 for _, s := range v.Spend {
180 spend[s.ID] = s
181 }
182 if spend["cn_go"].UnderCount != "Spend may be higher than shown when usage is not reported for a request." || spend["cn_tx"].UnderCount != "" {
183 t.Fatalf("spend %+v", v.Spend)
184 }
185 if !wire.Harnesses[wire.HarnessOpenCode].PartialUsageUnderCounts || wire.Harnesses[wire.HarnessPi].PartialUsageUnderCounts {
186 t.Fatal("the harness table")
187 }
188 var buf bytes.Buffer
189 if err := pages["usage"].ExecuteTemplate(&buf, "content", pageData{Usage: v}); err != nil {
190 t.Fatal(err)
191 }
192 if n := strings.Count(buf.String(), `data-spend-undercount>Spend may be higher than shown`); n != 1 {
193 t.Fatalf("the line shows %d times", n)
194 }
195 // From the harness's facts, whatever the provider: a TensorX
196 // connection on a harness with the gap would say it too.
197 conns[len(conns)-1].Provider, conns[len(conns)-1].Harness = wire.ProviderTensorX, wire.HarnessOpenCode
198 again := usagePage(conns, connLabels(conns, nil), byRole, month, nil, usageNow)
199 for _, s := range again.Spend {
200 if s.ID == "cn_go" && s.UnderCount == "" {
201 t.Fatal("the line follows the provider, not the harness")
202 }
203 }
204}
205