package thintest
import (
"net/http"
"net/url"
"strings"
"testing"
"amadan.net/oficina/equipo/internal/wire"
)
// TestPagesConnectionsAdd: Paul, 2026-11-10: "Can 'Add a connection' go
// to a new screen?" Connections links to its own Add page, a Settings
// page with Connections current and, on a phone, Connections in its bar;
// the page is the chooser (Claude, Codex and TensorX, two greyed) and then the
// attempt's card. Without scripts the forms still work: an Add posts and
// comes back to the Add page, its Check again leads back to Connections
// once the attempt connected, with the new row marked.
func TestPagesConnectionsAdd(t *testing.T) {
r := &toolRig{rig: newRig(t)}
owner := r.inst.Cookie()
runner := r.dial(0)
r.wait("runner online", func(c *Conversation) bool { return c.Runner.Online })
list := r.get(owner, "/conversation/settings/connections", 200)
if !strings.Contains(list, `href="/conversation/settings/connections/add" equipo-conn-add data-add-link`) || strings.Contains(list, "equipo-conn-chooser") {
t.Fatal("Connections does not link to the Add page, or still holds the chooser")
}
add := r.get(owner, "/conversation/settings/connections/add", 200)
for _, want := range []string{`data-settings-link="connections" aria-current="page"`, `href="/conversation/settings/connections" rel="up" aria-label="Connections"`, "
Add a connection", `data-add="claude"`, `data-add="codex"`, "data-paste-instead", "data-sign-in-how", "data-adding"} {
if !strings.Contains(add, want) {
t.Fatalf("the Add page lacks %s", want)
}
}
// TensorX is offered now (connectors spec S1); two stay greyed.
if n := strings.Count(add, "equipo-conn-opt disabled"); n != 2 || !strings.Contains(add, `data-add-metered="tensorx"`) {
t.Fatalf("%d greyed choices, want 2, and TensorX", n)
}
// Scriptless: Add Codex posts and comes back to the Add page.
if status, loc := r.post(owner, "/conversation/connections", url.Values{"provider": {"codex"}}); status != http.StatusSeeOther || loc != "/conversation/settings/connections/add" {
t.Fatalf("Add posted: %d to %q", status, loc)
}
f, body, err := runner.Expect(r.ctx, wire.FrameConnect)
if err != nil {
t.Fatal(err)
}
id := body.(*wire.ConnectCommand).Connection
if err = runner.Send(r.ctx, "a-c", wire.FrameAck, wire.Ack{ID: f.ID}); err != nil {
t.Fatal(err)
}
if err = runner.Send(r.ctx, "r-1", wire.FrameConnection, wire.ConnectionReport{Connection: id, Person: Owner, Provider: "codex", State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptDeviceCode, URL: "https://auth.openai.com/codex/device", Code: "ABCD-1234"}}); err != nil {
t.Fatal(err)
}
r.wait("device code", func(c *Conversation) bool { p := c.Connections[id].Prompt; return p != nil && p.Code == "ABCD-1234" })
add = r.get(owner, "/conversation/settings/connections/add", 200)
if !strings.Contains(add, "data-device-code") || !strings.Contains(add, `href="/conversation/settings/connections/add?attempt=`+id+`"`) || strings.Contains(add, "equipo-conn-chooser") {
t.Fatal("the Add page does not show the attempt's card with its scriptless Check again")
}
if strings.Contains(r.get(owner, "/conversation/settings/connections", 200), "data-device-code") {
t.Fatal("the attempt's card is on Connections too")
}
// Connecting: Check again stays; connected: back to Connections.
noFollow := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
check := func() (int, string) {
req, _ := http.NewRequest("GET", r.inst.URL+"/conversation/settings/connections/add?attempt="+id, nil)
req.AddCookie(owner)
res, err := noFollow.Do(req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
return res.StatusCode, res.Header.Get("Location")
}
if status, _ := check(); status != 200 {
t.Fatalf("Check again while connecting: %d", status)
}
if err = runner.Send(r.ctx, "r-2", wire.FrameConnection, wire.ConnectionReport{Connection: id, Person: Owner, LocalID: "codex-1", Provider: "codex", State: wire.ConnHealthy, Label: Owner, Plan: "pro"}); err != nil {
t.Fatal(err)
}
r.wait("connected", func(c *Conversation) bool { return c.Connections[id].State == wire.ConnHealthy })
if status, loc := check(); status != http.StatusSeeOther || loc != "/conversation/settings/connections?added="+id {
t.Fatalf("Check again once connected: %d to %q", status, loc)
}
page := r.get(owner, "/conversation/settings/connections?added="+id, 200)
if !strings.Contains(page, `data-connection="`+id+`" data-provider="codex" data-state="healthy" data-new>`) {
t.Fatal("the new row is not marked")
}
if strings.Contains(r.get(owner, "/conversation/settings/connections", 200), "data-new") {
t.Fatal("a row is marked without ?added")
}
}