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 "context"
5 "net/http"
6 "net/url"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/wire"
12)
13
14// TestContractModels (connectors spec rev 3.3 §16.1, slice S5b): the
15// models card lists every model the key listed, the ones that can be
16// allowed ticked and the rest greyed with Price or tier unavailable; the set goes to
17// the runner as a durable models frame (revision 1 while connecting);
18// the row says "Models allowed: 1 of 2" and opens the same list; a change is
19// revision-checked, pending until the runner's ack, and sent again when
20// a hello shows the runner behind; the Usage page names the tier
21// table's date beside the price date.
22func TestContractModels(t *testing.T) {
23 r := newRig(t)
24 keys := sealKeys(t)
25 runner := dialAt(t, r, wire.ProtocolModels)
26 hello := primaryAt(held(), wire.ProtocolModels)
27 hello.Generation, hello.SealKey = runner.Welcome.Generation, keys.PublicString()
28 if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil {
29 t.Fatal(err)
30 }
31 r.wait("runner online", func(c *Conversation) bool { return c.Runner.Online })
32 pages := &toolRig{rig: r}
33 owner := r.inst.Cookie()
34 attempt, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, LimitConfirmed: true, Budget: &wire.Budget{Day: wire.DefaultBudgetDay, Month: wire.DefaultBudgetMonth}})
35 if err != nil {
36 t.Fatal(err)
37 }
38 f, _, err := runner.Expect(r.ctx, wire.FrameConnect)
39 if err != nil {
40 t.Fatal(err)
41 }
42 runner.Send(r.ctx, "a-connect", wire.FrameAck, wire.Ack{ID: f.ID})
43 choices := []wire.ModelChoice{{ID: "moonshotai/kimi-k3", Eligible: true, Family: "kimi", Tier: "strong"}, {ID: "vendor/unpriced"}, {ID: "z-ai/glm-5.3", Eligible: true, Family: "glm", Tier: "strong"}}
44 runner.Send(r.ctx, "r-models", wire.FrameConnection, wire.ConnectionReport{Connection: attempt.ID, Person: Owner, Provider: wire.ProviderTensorX, State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptModels, Choices: choices}})
45 r.wait("the models card", func(c *Conversation) bool {
46 p := c.Connections[attempt.ID].Prompt
47 return p != nil && p.Kind == wire.PromptModels && len(p.Choices) == 3
48 })
49 got, _ := r.client.Connection(r.ctx, attempt.ID)
50 shape(t, "api/connection-choose-models.json", got)
51 card := pages.get(owner, "/conversation/settings/connections/add", 200)
52 for _, want := range []string{`action="/conversation/connections/` + attempt.ID + `/models"`, `name="model" value="z-ai/glm-5.3" checked`, `name="model" value="moonshotai/kimi-k3" checked`, `name="model" value="vendor/unpriced" disabled`, "Price or tier unavailable", `name="revision" value="0"`} {
53 if !strings.Contains(card, want) {
54 t.Fatalf("the models card lacks %s", want)
55 }
56 }
57 if _, err := r.client.SetModels(r.ctx, attempt.ID, wire.SetModels{Allowed: []string{"vendor/unpriced"}, Revision: 0}); Status(err) != 422 {
58 t.Fatalf("an unpriced model allowed: %v", err)
59 }
60 if _, err := r.client.SetModels(r.ctx, attempt.ID, wire.SetModels{Allowed: []string{}, Revision: 0}); Status(err) != 422 {
61 t.Fatalf("no model allowed: %v", err)
62 }
63 // From the page: GLM only.
64 if status, loc := pages.post(owner, "/conversation/connections/"+attempt.ID+"/models", url.Values{"from": {"add"}, "revision": {"0"}, "model": {"z-ai/glm-5.3"}}); status != http.StatusSeeOther || loc != "/conversation/settings/connections/add" {
65 t.Fatalf("the card's post: %d %q", status, loc)
66 }
67 f, body, err := runner.Expect(r.ctx, wire.FrameModels)
68 if err != nil {
69 t.Fatal(err)
70 }
71 if cmd := body.(*wire.ModelsCommand); cmd.Connection != attempt.ID || cmd.LocalID != "" || cmd.Revision != 1 || strings.Join(cmd.Allowed, ",") != "z-ai/glm-5.3" {
72 t.Fatalf("connect set %+v", cmd)
73 }
74 runner.Send(r.ctx, "a-models-1", wire.FrameAck, wire.Ack{ID: f.ID})
75 runner.Send(r.ctx, "r-healthy", wire.FrameConnection, wire.ConnectionReport{Connection: attempt.ID, Person: Owner, Provider: wire.ProviderTensorX, LocalID: "tensorx-1", State: wire.ConnHealthy})
76 metered := wire.HeldConnection{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: wire.DefaultBudgetDay, Month: wire.DefaultBudgetMonth, Currency: "USD", Revision: 1}, Spend: &wire.Spend{Currency: "USD"},
77 Models: []wire.ModelState{{ID: "z-ai/glm-5.3", State: wire.ModelHealthy, Family: "glm", Tier: "strong"}}, ModelsRevision: 1, Choices: choices}
78 hello.Connections = []wire.HeldConnection{metered}
79 runner.Send(r.ctx, "h2", wire.FrameHello, hello)
80 r.wait("healthy with its set", func(c *Conversation) bool {
81 v := c.Connections[attempt.ID]
82 return v.State == wire.ConnHealthy && v.ModelsRevision == 1 && !v.ModelsPending && len(v.Models) == 3
83 })
84 list := pages.get(owner, "/conversation/settings/connections", 200)
85 for _, want := range []string{"Models allowed: 1 of 2", `data-models="` + attempt.ID + `"`, `name="model" value="z-ai/glm-5.3" checked`, `name="model" value="moonshotai/kimi-k3">`, `name="model" value="vendor/unpriced" disabled`, `name="revision" value="1"`} {
86 if !strings.Contains(list, want) {
87 t.Fatalf("the row lacks %s", want)
88 }
89 }
90 // A change: revision-checked, pending until the ack.
91 if _, err := r.client.SetModels(r.ctx, attempt.ID, wire.SetModels{Allowed: []string{"z-ai/glm-5.3", "moonshotai/kimi-k3"}, Revision: 0}); Status(err) != 409 {
92 t.Fatalf("a stale revision: %v", err)
93 }
94 changed, err := r.client.SetModels(r.ctx, attempt.ID, wire.SetModels{Allowed: []string{"z-ai/glm-5.3", "moonshotai/kimi-k3"}, Revision: 1})
95 if err != nil || !changed.ModelsPending || changed.ModelsRevision != 2 {
96 t.Fatalf("changed %+v %v", changed, err)
97 }
98 shape(t, "api/connection-models.json", changed)
99 if page := pages.get(owner, "/conversation/settings/connections", 200); !strings.Contains(page, "Applies when Headless runner confirms") || !strings.Contains(page, "Models allowed: 2 of 2") {
100 t.Fatal("the pending set is not said")
101 }
102 f, body, err = runner.Expect(r.ctx, wire.FrameModels)
103 if err != nil {
104 t.Fatal(err)
105 }
106 shape(t, "frames/models.json", runner.Received[len(runner.Received)-1])
107 if cmd := body.(*wire.ModelsCommand); cmd.LocalID != "tensorx-1" || cmd.Revision != 2 || len(cmd.Allowed) != 2 {
108 t.Fatalf("models frame %+v", cmd)
109 }
110 // The runner went away before applying it: its hello is behind, so
111 // the set is sent again.
112 runner.Send(r.ctx, "h3", wire.FrameHello, hello)
113 f, body, err = runner.Expect(r.ctx, wire.FrameModels)
114 if err != nil || body.(*wire.ModelsCommand).Revision != 2 {
115 t.Fatalf("not sent again: %+v %v", body, err)
116 }
117 runner.Send(r.ctx, "a-models-2", wire.FrameAck, wire.Ack{ID: f.ID})
118 // The ack is receipt only (security round #4): still pending until
119 // the runner's hello reports the set applied.
120 time.Sleep(200 * time.Millisecond)
121 if v, _ := r.client.Connection(r.ctx, attempt.ID); !v.ModelsPending {
122 t.Fatalf("an ack showed the set applied: %+v", v)
123 }
124 metered.ModelsRevision = 2
125 metered.Models = []wire.ModelState{{ID: "z-ai/glm-5.3", State: wire.ModelHealthy}, {ID: "moonshotai/kimi-k3", State: wire.ModelUnprobed}}
126 hello.Connections = []wire.HeldConnection{metered}
127 runner.Send(r.ctx, "h3b", wire.FrameHello, hello)
128 r.wait("the set applied", func(c *Conversation) bool {
129 v := c.Connections[attempt.ID]
130 return !v.ModelsPending && v.ModelsRevision == 2
131 })
132
133 // Check again read the key's list anew: a model it lists now is
134 // offered on the row.
135 metered.Choices = append(choices, wire.ModelChoice{ID: "qwen/qwen3.8-27b", Eligible: true, Family: "qwen", Tier: "standard"})
136 hello.Connections = []wire.HeldConnection{metered}
137 runner.Send(r.ctx, "h4", wire.FrameHello, hello)
138 waitFor(t, "the new listed model", func() bool {
139 return strings.Contains(pages.get(owner, "/conversation/settings/connections", 200), "Models allowed: 2 of 3")
140 })
141
142 // The Usage page: the tier table's date beside the price date.
143 n, cost := int64(10), int64(30)
144 runner.Send(r.ctx, "r-usage", wire.FrameUsage, wire.UsageReport{ProbeID: "probe:st_1:1:moonshotai/kimi-k3", Rows: []wire.Usage{{Provider: wire.ProviderTensorX, Connection: "tensorx-1", Model: "moonshotai/kimi-k3", Attempt: 1, Outcome: wire.OutcomeReplied, Complete: true, InputTokens: &n, OutputTokens: &n, CostMicros: &cost, Currency: "USD", PriceVersion: "tensorx-2026-11-12", Family: "kimi", Tier: "strong", TierVersion: "tiers-2026-11-12"}}})
145 deadline := time.Now().Add(10 * time.Second)
146 for {
147 page := pages.get(owner, "/usage", 200)
148 if strings.Contains(page, "TensorX prices taken from the public page on 12 Nov 2026. Model tiers reflect Equipo’s judgement as of 12 Nov 2026.") {
149 break
150 }
151 if time.Now().After(deadline) {
152 t.Fatal("the Usage page does not name the tier table")
153 }
154 time.Sleep(50 * time.Millisecond)
155 }
156 var tier, version string
157 if err := r.inst.DB.Writer().QueryRow("SELECT tier, tier_version FROM chat_usage WHERE step_id = ?", "probe:st_1:1:moonshotai/kimi-k3").Scan(&tier, &version); err != nil || tier != "strong" || version != "tiers-2026-11-12" {
158 t.Fatalf("usage row %q %q %v", tier, version, err)
159 }
160 if page := pages.get(owner, "/usage?period=month&by=model", 200); !strings.Contains(page, "moonshotai/kimi-k3") {
161 t.Fatal("no row by model")
162 }
163}
164
165// §16.1: a runner below ProtocolModels keeps its connections' one model,
166// exactly as before; the page says it needs an update to allow more
167// models, and a change is refused (409 runner_too_old) and never sent.
168func TestContractModelsOlderRunner(t *testing.T) {
169 r := newRig(t)
170 runner := dialAt(t, r, wire.ProtocolOpenCode)
171 hello := primaryAt(held(wire.HeldConnection{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: wire.DefaultBudgetDay, Month: wire.DefaultBudgetMonth, Currency: "USD", Revision: 1}, Spend: &wire.Spend{Currency: "USD"}}), wire.ProtocolOpenCode)
172 hello.Generation = runner.Welcome.Generation
173 if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil {
174 t.Fatal(err)
175 }
176 var id string
177 waitFor(t, "the metered connection", func() bool {
178 list, err := r.client.Connections(r.ctx)
179 for _, c := range list.Items {
180 if c.Kind == wire.KindAPIKey {
181 id = c.ID
182 }
183 }
184 return err == nil && id != ""
185 })
186 // The S1 connection, migrated: its set is its one model.
187 if _, err := r.inst.DB.Writer().Exec("UPDATE chat_connections SET models = json_array(model), models_revision = 1, models_applied = 1 WHERE id = ?", id); err != nil {
188 t.Fatal(err)
189 }
190 pages := &toolRig{rig: r}
191 page := pages.get(r.inst.Cookie(), "/conversation/settings/connections", 200)
192 if !strings.Contains(page, "Your runner needs an update before you can change the allowed models.") || !strings.Contains(page, "<code equipo-conn-model-id>z-ai/glm-5.3</code>") || strings.Contains(page, "data-models-change") {
193 i := strings.Index(page, "data-spend")
194 t.Fatalf("the older runner's row is not as before with its note: %s", page[max(0, i-200):min(len(page), i+800)])
195 }
196 if _, err := r.client.SetModels(r.ctx, id, wire.SetModels{Allowed: []string{"z-ai/glm-5.3"}, Revision: 1}); Status(err) != 409 {
197 t.Fatalf("a protocol 17 runner: %v", err)
198 }
199 ctx, cancel := context.WithTimeout(r.ctx, 300*time.Millisecond)
200 defer cancel()
201 if f, _, _ := runner.Expect(ctx, wire.FrameModels); f.Type == wire.FrameModels {
202 t.Fatal("a protocol 17 runner was sent a models frame")
203 }
204}
205
206// §16.5: Review's dispatch names the family Engineering's attempt
207// recorded, read from that step, never from the connection; an older
208// runner is sent none (Encode drops it below 18).
209func TestContractReviewNamesEngineeringFamily(t *testing.T) {
210 for _, c := range []struct {
211 protocol int
212 want string
213 }{{wire.ProtocolModels, "claude"}, {wire.ProtocolOpenCode, ""}} {
214 r := newToolRigAt(t, c.protocol)
215 base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40)
216 r.pushed("a", "Fix the README typo", base, cand)
217 r.expect(wire.FrameWatchMerge)
218 c1 := r.dispatched(wire.SpecialistChecks, base, cand, 0)
219 r.checks("a-c1", c1, wire.ChecksPassed, exit(0), "", "ok\n")
220 rv := r.dispatched(wire.SpecialistReview, base, cand, 0)
221 if rv.AvoidAccount != "claude-a" || rv.AvoidFamily != c.want {
222 t.Fatalf("protocol %d: review dispatch %+v", c.protocol, rv)
223 }
224 }
225}
226