package thintest import ( "context" "net/http" "net/url" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // TestContractOpenCodeGoConnection (connectors spec §4.2, slice S5): an // OpenCode Go connection is refused to a runner before protocol 17, a // publishing protocol 16 one included (a TensorX one still goes to it); // on a current runner the Add page offers it with its required limit // confirmation, how Go's limit works and where Go says so, and no pay-as-you-go confirmation (Go is always // metered, §0 decision 7); it goes to the runner as a metered api_key // connect run by OpenCode, and its key card is sealed for opencode.ai. func TestContractOpenCodeGoConnection(t *testing.T) { budget := &wire.Budget{Day: wire.DefaultBudgetDay, Month: wire.DefaultBudgetMonth} good := wire.Connect{Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, LimitConfirmed: true, Budget: budget} // A protocol 16 runner (publishing, no OpenCode) and a protocol 15 // one are refused OpenCode Go and sent only the TensorX connect. for _, proto := range []int{wire.ProtocolPublish, wire.ProtocolReadings} { old := newRig(t) runner := dialAt(t, old, proto) hello := primaryAt(held(), proto) hello.Generation, hello.SealKey = runner.Welcome.Generation, sealKeys(t).PublicString() if err := runner.Send(old.ctx, "h1", wire.FrameHello, hello); err != nil { t.Fatal(err) } old.wait("runner online", func(c *Conversation) bool { return c.Runner.Online }) if _, err := old.client.Connect(old.ctx, good); Status(err) != 409 { t.Fatalf("a protocol %d runner: %v", proto, err) } // TensorX still goes to an older runner, and is the one connect // it is sent. if _, err := old.client.Connect(old.ctx, wire.Connect{Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, LimitConfirmed: true, Budget: budget}); err != nil { t.Fatalf("TensorX on a protocol %d runner: %v", proto, err) } _, body, err := runner.Expect(old.ctx, wire.FrameConnect) if err != nil || body.(*wire.ConnectCommand).Provider != wire.ProviderTensorX { t.Fatalf("a protocol %d runner was sent %+v: %v", proto, body, err) } ctx, cancel := context.WithTimeout(old.ctx, 300*time.Millisecond) if f, _, _ := runner.Expect(ctx, wire.FrameConnect); f.Type == wire.FrameConnect { t.Fatalf("a protocol %d runner was sent an OpenCode Go connect", proto) } cancel() } r := newRig(t) keys := sealKeys(t) runner := dialAt(t, r, wire.ProtocolOpenCode) hello := primaryAt(held(), wire.ProtocolOpenCode) hello.Generation, hello.SealKey = runner.Welcome.Generation, keys.PublicString() if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil { t.Fatal(err) } r.wait("runner online", func(c *Conversation) bool { return c.Runner.Online }) for name, bad := range map[string]wire.Connect{ "no limit confirmation": {Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, Budget: budget}, "pay-as-you-go off confirmed": {Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, LimitConfirmed: true, PaygOffConfirmed: true, Budget: budget}, "no budget": {Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, LimitConfirmed: true}, "pay-as-you-go off by itself": {Provider: wire.ProviderOpenCodeGo, Kind: wire.KindAPIKey, PaygOffConfirmed: true, Budget: budget}, } { if _, err := r.client.Connect(r.ctx, bad); Status(err) != 422 { t.Fatalf("%s: %v", name, err) } } pages := &toolRig{rig: r} owner := r.inst.Cookie() add := pages.get(owner, "/conversation/settings/connections/add", 200) start := strings.Index(add, `data-add-metered="opencode-go"`) if start < 0 { t.Fatal("the Add page does not offer OpenCode Go") } form := add[start:] form = form[:strings.Index(form, "")] 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"`} { if !strings.Contains(form, want) { t.Fatalf("the OpenCode Go form lacks %s", want) } } if strings.Contains(form, "payg_off") { t.Fatal("OpenCode Go is offered a pay-as-you-go confirmation") } 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" { t.Fatalf("unconfirmed add: %d %q", status, loc) } attempt, err := r.client.Connect(r.ctx, good) 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 { t.Fatalf("attempt %+v %v", attempt, err) } f, body, err := runner.Expect(r.ctx, wire.FrameConnect) if err != nil { t.Fatal(err) } 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" { t.Fatalf("connect %+v", cmd) } runner.Send(r.ctx, "a-connect", wire.FrameAck, wire.Ack{ID: f.ID}) 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") { t.Fatal("the key card is not sealed for opencode.ai") } }