oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "encoding/json"
5 "net/url"
6 "strings"
7 "testing"
8 "time"
9
10 "amadan.net/oficina/equipo/internal/wire"
11 "amadan.net/oficina/equipo/internal/wire/wiretest"
12)
13
14// usageHello is the runner's hello with Claude's reading two hours old
15// (its window near its limit) and TensorX at 84% of today's budget.
16func usageHello(r *toolRig, protocol int, read *time.Time) wire.Hello {
17 reset := time.Now().Add(70 * time.Hour).UTC().Truncate(time.Second)
18 h := primaryAt(toolHello(r.runner.Welcome.Generation), protocol)
19 h.Connections = []wire.HeldConnection{
20 {LocalID: "claude-a", Person: Owner, Provider: wire.ProviderClaude, Kind: wire.KindSubscription, Label: Owner, Plan: "max", State: wire.ConnHealthy, Headroom: &wire.Headroom{Windows: []wire.Window{{Name: "seven_day", UsedPercent: 85, Minutes: 10080, ResetsAt: &reset}}, CheckedAt: read}},
21 {LocalID: "tensorx-1", Person: Owner, Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, State: wire.ConnHealthy, Harness: wire.HarnessPi, Upstream: "https://api.tensorx.ai", Billing: wire.BillingMetered, Model: "z-ai/glm-5.3", 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"}},
22 }
23 return h
24}
25
26// sayHello sends h and waits for both connections to show.
27func (r *toolRig) sayHello(key string, h wire.Hello) {
28 r.t.Helper()
29 if err := r.runner.Send(r.ctx, key, wire.FrameHello, h); err != nil {
30 r.t.Fatal(err)
31 }
32 deadline := time.Now().Add(10 * time.Second)
33 for {
34 list, err := r.client.Connections(r.ctx)
35 if err == nil && len(list.Items) >= 2 {
36 return
37 }
38 if time.Now().After(deadline) {
39 r.t.Fatalf("connections %+v %v", list, err)
40 }
41 time.Sleep(50 * time.Millisecond)
42 }
43}
44
45// TestContractUsage (connectors spec rev 3.2 S4, §9, §10; Connections
46// rev 3 §6; the controller's ruling #7): the Usage page shows what is
47// available with each reading's age and the spend this month; advice
48// comes as one card at the top of Usage and one quiet card in the
49// conversation, never Needs you; GET /api/v1/advice lists it; its action
50// from the page is the ordinary budget call, reaching the runner; a step
51// held for headroom says how old its reading is; the sidebar has Usage,
52// with a dot once a connection needs a look.
53func TestContractUsage(t *testing.T) {
54 r := newToolRigAt(t, wire.ProtocolCurrent)
55 read := time.Now().Add(-2 * time.Hour).UTC().Truncate(time.Second)
56 r.sayHello("h-usage", usageHello(r, wire.ProtocolCurrent, &read))
57 owner := r.inst.Cookie()
58
59 page := r.get(owner, "/usage", 200)
60 for _, want := range []string{`id="nav-usage"`, `data-available=`, `data-reading-age>last reported 2 h ago`, `data-spend-line>$0.84 of $1 today · $6.20 of $10 this month`, `data-usage-table`} {
61 if !strings.Contains(page, want) {
62 t.Fatalf("usage page lacks %s", want)
63 }
64 }
65 if strings.Contains(page, "data-usage-dot") || strings.Contains(page, "data-advice=") {
66 t.Fatal("a dot or advice before anything needs it")
67 }
68
69 // The loop's minute: today's one card.
70 if err := r.inst.Store.Chat().HousekeepingTick(r.ctx); err != nil {
71 t.Fatal(err)
72 }
73 raw, _ := r.fetch(r.client.Token, "/api/v1/advice", 200)
74 var list wire.AdviceList
75 if err := wire.Strict(raw, &list); err != nil || len(list.Items) != 1 {
76 t.Fatalf("advice %s %v", raw, err)
77 }
78 if err := wiretest.SameShape(wiretest.Fixture("api/advice.json"), raw); err != nil {
79 t.Fatal(err)
80 }
81 a := list.Items[0]
82 if a.Kind != wire.AdviceBudgetNear || a.Act != "Raise today’s budget to $2" || a.Dismiss != "Not now" || a.Text != "TensorX: $0.84 of today’s $1 budget used." {
83 t.Fatalf("advice %+v", a)
84 }
85 page = r.get(owner, "/usage", 200)
86 if !strings.Contains(page, `data-advice="`+a.ID+`"`) || strings.Index(page, "data-advice=") > strings.Index(page, `id="usage-now"`) {
87 t.Fatal("the advice is not at the top of Usage")
88 }
89 if conv := r.get(owner, "/conversation", 200); !strings.Contains(conv, `data-advice-choice="act"`) || !strings.Contains(conv, "TensorX: $0.84 of today’s $1 budget used.") {
90 t.Fatal("no quiet advice card in the conversation")
91 }
92 if needs, err := r.client.NeedsYou(r.ctx); err != nil || len(needs.Items) != 0 {
93 t.Fatalf("advice was Needs you: %+v %v", needs, err)
94 }
95
96 // A stale form acts on nothing; the current one raises today's budget
97 // through the ordinary call.
98 if code, _ := r.post(owner, "/usage/advice/"+a.ID+"/act", url.Values{"revision": {"9"}}); code != 303 || r.frames(wire.FrameBudget) != 0 {
99 t.Fatalf("a stale act: %d, %d budget frames", code, r.frames(wire.FrameBudget))
100 }
101 if code, at := r.post(owner, "/usage/advice/"+a.ID+"/act", url.Values{"revision": {"1"}}); code != 303 || at != "/usage" {
102 t.Fatalf("act: %d %s", code, at)
103 }
104 _, body := r.expect(wire.FrameBudget)
105 if b := body.(*wire.BudgetCommand); b.Budget.Day != 2_000_000 || b.Budget.Month != 10_000_000 || b.Budget.Revision != 2 {
106 t.Fatalf("budget %+v", b)
107 }
108 if raw, _ = r.fetch(r.client.Token, "/api/v1/advice", 200); !strings.Contains(string(raw), `"items":[]`) {
109 t.Fatalf("advice after acting: %s", raw)
110 }
111
112 // A step held for headroom on Claude says how old the reading is.
113 id := taskID(t, r.call("u1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Tidy the README", "brief": "b"}))
114 if res := r.call("u2", wire.ToolStartTask, map[string]string{"task": id, "brief": "go"}); res.IsError {
115 t.Fatalf("start: %+v", res)
116 }
117 _, d := r.expect(wire.FrameDispatch)
118 reset := time.Now().Add(70 * time.Hour).UTC().Truncate(time.Second)
119 r.hold("uh", d.(*wire.DispatchCommand), wire.StepHold{Reason: wire.HoldHeadroom, Connection: "claude-a", Provider: wire.ProviderClaude, ResetsAt: &reset, WakeAt: &reset, StepRevision: 1})
120 if page := r.get(owner, "/tasks/"+id, 200); !strings.Contains(page, "your Claude allowance is 85% used, so Equipo is keeping the rest for urgent work.") || !strings.Contains(page, "It carries on by itself then. · last reported 2 h ago") {
121 t.Fatal("the hold line does not say the reading's age")
122 }
123
124 // The usage resource, strictly decoded.
125 raw, _ = r.fetch(r.client.Token, "/api/v1/usage?period=week&by=role", 200)
126 var totals wire.UsageTotals
127 if err := wire.Strict(raw, &totals); err != nil || totals.Period != wire.UsageWeek || totals.By != wire.UsageByRole {
128 t.Fatalf("usage %s %v", raw, err)
129 }
130 r.fetch(r.client.Token, "/api/v1/usage?period=year", 422)
131
132 // Claude limited: Usage has its dot.
133 h := usageHello(r, wire.ProtocolCurrent, &read)
134 h.Connections[0].State = wire.ConnLimited
135 if err := r.runner.Send(r.ctx, "h-limited", wire.FrameHello, h); err != nil {
136 t.Fatal(err)
137 }
138 deadline := time.Now().Add(10 * time.Second)
139 for !strings.Contains(r.get(owner, "/usage", 200), "data-usage-dot") {
140 if time.Now().After(deadline) {
141 t.Fatal("no dot on Usage for a limited connection")
142 }
143 time.Sleep(50 * time.Millisecond)
144 }
145}
146
147// TestContractUsageOlderRunner: a protocol 14 runner says no reading
148// time; the page shows its readings without an age, and nothing breaks.
149func TestContractUsageOlderRunner(t *testing.T) {
150 r := newToolRigAt(t, wire.ProtocolScheduler)
151 r.sayHello("h-usage", usageHello(r, wire.ProtocolScheduler, nil))
152 page := r.get(r.inst.Cookie(), "/usage", 200)
153 if !strings.Contains(page, `data-available=`) || strings.Contains(page, "data-reading-age") {
154 t.Fatal("an older runner's reading showed an age, or nothing showed")
155 }
156 raw, _ := json.Marshal(usageHello(r, wire.ProtocolScheduler, nil))
157 if strings.Contains(string(raw), "checked_at") {
158 t.Fatal("the older hello carried a reading time")
159 }
160}
161