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// TestContractOpenCodeGoConnection (connectors spec §4.2, slice S5): an
15// OpenCode Go connection is refused to a runner before protocol 17, a
16// publishing protocol 16 one included (a TensorX one still goes to it);
17// on a current runner the Add page offers it with its required limit
18// confirmation, how Go's limit works and where Go says so, and no pay-as-you-go confirmation (Go is always
19// metered, §0 decision 7); it goes to the runner as a metered api_key
20// connect run by OpenCode, and its key card is sealed for opencode.ai.
21func TestContractOpenCodeGoConnection(t *testing.T) {
22 budget := &wire.Budget{Day: wire.DefaultBudgetDay, Month: wire.DefaultBudgetMonth}
23 good := wire.Connect{Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, LimitConfirmed: true, Budget: budget}
24 // A protocol 16 runner (publishing, no OpenCode) and a protocol 15
25 // one are refused OpenCode Go and sent only the TensorX connect.
26 for _, proto := range []int{wire.ProtocolPublish, wire.ProtocolReadings} {
27 old := newRig(t)
28 runner := dialAt(t, old, proto)
29 hello := primaryAt(held(), proto)
30 hello.Generation, hello.SealKey = runner.Welcome.Generation, sealKeys(t).PublicString()
31 if err := runner.Send(old.ctx, "h1", wire.FrameHello, hello); err != nil {
32 t.Fatal(err)
33 }
34 old.wait("runner online", func(c *Conversation) bool { return c.Runner.Online })
35 if _, err := old.client.Connect(old.ctx, good); Status(err) != 409 {
36 t.Fatalf("a protocol %d runner: %v", proto, err)
37 }
38 // TensorX still goes to an older runner, and is the one connect
39 // it is sent.
40 if _, err := old.client.Connect(old.ctx, wire.Connect{Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, LimitConfirmed: true, Budget: budget}); err != nil {
41 t.Fatalf("TensorX on a protocol %d runner: %v", proto, err)
42 }
43 _, body, err := runner.Expect(old.ctx, wire.FrameConnect)
44 if err != nil || body.(*wire.ConnectCommand).Provider != wire.ProviderTensorX {
45 t.Fatalf("a protocol %d runner was sent %+v: %v", proto, body, err)
46 }
47 ctx, cancel := context.WithTimeout(old.ctx, 300*time.Millisecond)
48 if f, _, _ := runner.Expect(ctx, wire.FrameConnect); f.Type == wire.FrameConnect {
49 t.Fatalf("a protocol %d runner was sent an OpenCode Go connect", proto)
50 }
51 cancel()
52 }
53 r := newRig(t)
54 keys := sealKeys(t)
55 runner := dialAt(t, r, wire.ProtocolOpenCode)
56 hello := primaryAt(held(), wire.ProtocolOpenCode)
57 hello.Generation, hello.SealKey = runner.Welcome.Generation, keys.PublicString()
58 if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil {
59 t.Fatal(err)
60 }
61 r.wait("runner online", func(c *Conversation) bool { return c.Runner.Online })
62 for name, bad := range map[string]wire.Connect{
63 "no limit confirmation": {Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, Budget: budget},
64 "pay-as-you-go off confirmed": {Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, LimitConfirmed: true, PaygOffConfirmed: true, Budget: budget},
65 "no budget": {Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, LimitConfirmed: true},
66 "pay-as-you-go off by itself": {Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, PaygOffConfirmed: true, Budget: budget},
67 } {
68 if _, err := r.client.Connect(r.ctx, bad); Status(err) != 422 {
69 t.Fatalf("%s: %v", name, err)
70 }
71 }
72 pages := &toolRig{rig: r}
73 owner := r.inst.Cookie()
74 add := pages.get(owner, "/conversation/settings/connections/add", 200)
75 start := strings.Index(add, `data-add-metered="opencode-go"`)
76 if start < 0 {
77 t.Fatal("the Add page does not offer OpenCode Go")
78 }
79 form := add[start:]
80 form = form[:strings.Index(form, "</details>")]
81 for _, want := range []string{"OpenCode Go", `name="provider" value="opencode-go"`, `name="limit_confirmed" value="1" required`, "This key has a spending limit at OpenCode Go", `href="https://opencode.ai/docs/console/go/"`, `name="budget_day" inputmode="decimal" value="1"`} {
82 if !strings.Contains(form, want) {
83 t.Fatalf("the OpenCode Go form lacks %s", want)
84 }
85 }
86 if strings.Contains(form, "payg_off") {
87 t.Fatal("OpenCode Go is offered a pay-as-you-go confirmation")
88 }
89 if status, loc := pages.post(owner, "/conversation/connections", url.Values{"provider": {"opencode-go"}, "kind": {"api_key"}, "budget_day": {"1"}, "budget_month": {"10"}}); status != http.StatusSeeOther || loc != "/conversation/settings/connections/add?refused=confirm" {
90 t.Fatalf("unconfirmed add: %d %q", status, loc)
91 }
92 attempt, err := r.client.Connect(r.ctx, good)
93 if err != nil || attempt.State != wire.ConnConnecting || attempt.Kind != wire.KindAPIKey || attempt.Billing != wire.BillingMetered || attempt.Harness != wire.HarnessOpenCode || attempt.Upstream != "https://opencode.ai" || attempt.LimitConfirmedAt == nil || attempt.PaygOffConfirmedAt != nil {
94 t.Fatalf("attempt %+v %v", attempt, err)
95 }
96 f, body, err := runner.Expect(r.ctx, wire.FrameConnect)
97 if err != nil {
98 t.Fatal(err)
99 }
100 if cmd := body.(*wire.ConnectCommand); cmd.Connection != attempt.ID || cmd.Provider != wire.ProviderOpenCodeGo || cmd.Kind != wire.KindAPIKey || cmd.Billing != wire.BillingMetered || cmd.Budget == nil || cmd.Budget.Currency != "USD" {
101 t.Fatalf("connect %+v", cmd)
102 }
103 runner.Send(r.ctx, "a-connect", wire.FrameAck, wire.Ack{ID: f.ID})
104 if card := pages.get(owner, "/conversation/settings/connections/add", 200); !strings.Contains(card, `data-endpoint="https://opencode.ai:443"`) || !strings.Contains(card, "Paste your OpenCode Go API key") {
105 t.Fatal("the key card is not sealed for opencode.ai")
106 }
107}
108