package thintest import ( "net/http" "net/url" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // tokenRowRig is a runner whose hello holds the owner's Claude token // connection (no account named, no usage reported) and a Claude login // the provider named, with its week window. It returns the token row's // id and the login's. func tokenRowRig(t *testing.T) (r *toolRig, runner *FakeRunner, token, login string) { t.Helper() r = &toolRig{rig: newRig(t)} runner = r.dial(0) reset := time.Now().Add(30 * time.Hour).UTC().Truncate(time.Minute) hello := held(wire.HeldConnection{LocalID: "claude-9", Person: Owner, Provider: "claude", Kind: wire.KindToken, State: wire.ConnHealthy}, wire.HeldConnection{LocalID: "claude-1", Person: Owner, Provider: "claude", Kind: "subscription", Label: Owner, State: wire.ConnHealthy, Headroom: &wire.Headroom{Windows: []wire.Window{{Name: "seven_day", UsedPercent: 40, Minutes: 10080, ResetsAt: &reset}}}}) hello.Generation = runner.Welcome.Generation if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil { t.Fatal(err) } r.wait("both rows", func(c *Conversation) bool { for _, v := range c.Connections { switch v.LocalID { case "claude-9": token = v.ID case "claude-1": login = v.ID } } return token != "" && login != "" }) return r, runner, token, login } // rowHTML is one connection's row on the page. func rowHTML(t *testing.T, page, id string) string { t.Helper() _, row, ok := strings.Cut(page, `
") return row } // TestPagesTokenRowFacts (Paul, 2026-11-11: "Why does this Claude // account not show its email address or its usage?"): a token row the // provider named no account for and reported no usage on says so // honestly. It offers the account email field (open once, after the // sign-in), says usage shows once Claude reports it, and, once turns // were answered on it with still no window, that Claude has not // reported usage for it. An email the owner types shows as theirs, // "(added by you)", and goes nowhere else: not in the API, not to the // runner. A teammate, a cross-site post and a bad shape are refused. func TestPagesTokenRowFacts(t *testing.T) { r, runner, token, login := tokenRowRig(t) owner := r.inst.Cookie() const page = "/conversation/settings/connections" row := rowHTML(t, r.get(owner, page, 200), token) for _, want := range []string{`data-account-email="` + token + `"`, "Add the account email", "Claude account email", `data-usage-note>Usage shows after Claude reports it<`} { if !strings.Contains(row, want) { t.Fatalf("the token row lacks %q: %s", want, row) } } if strings.Contains(row, "(added by you)") || strings.Contains(row, `data-account-email="`+token+`" open`) { t.Fatal("the token row claims an email, or opens the field unasked") } if other := rowHTML(t, r.get(owner, page, 200), login); strings.Contains(other, "data-account-email") || strings.Contains(other, "data-usage-note") || !strings.Contains(other, Owner) { t.Fatalf("the named, metered row changed: %s", other) } // Just signed in: the question is open, once. if row = rowHTML(t, r.get(owner, page+"?added="+token, 200), token); !strings.Contains(row, `data-account-email="`+token+`" open`) { t.Fatal("the question is not asked after the sign-in") } // Refused: a teammate, a cross-site post, a bad shape. email := func(c *http.Cookie, id, v string) int { status, _ := r.post(c, "/conversation/connections/"+id+"/email", url.Values{"email": {v}}) return status } if status := email(r.memberCookie(), token, "mallory@example.com"); status != http.StatusNotFound { t.Fatalf("a teammate set the email: %d", status) } cross, _ := http.NewRequest("POST", r.inst.URL+"/conversation/connections/"+token+"/email", strings.NewReader(url.Values{"email": {"cross@example.com"}}.Encode())) cross.AddCookie(owner) cross.Header.Set("Content-Type", "application/x-www-form-urlencoded") cross.Header.Set("Origin", "https://elsewhere.example") cross.Header.Set("Sec-Fetch-Site", "cross-site") if res, err := http.DefaultClient.Do(cross); err != nil || res.StatusCode != http.StatusForbidden { t.Fatalf("a cross-site post: %v %v", res.StatusCode, err) } else { res.Body.Close() } for _, bad := range []string{"not an email", "me@example", "me@@example.com", "@example.com", "me@example.com\u202e", "me\x07@example.com", strings.Repeat("a", 243) + "@example.com"} { if status := email(owner, token, bad); status != http.StatusUnprocessableEntity { t.Fatalf("%q: %d", bad, status) } } if status := email(owner, login, "me@example.com"); status != http.StatusConflict { t.Fatalf("an email on a login the provider names: %d", status) } if row = rowHTML(t, r.get(owner, page, 200), token); strings.Contains(row, "(added by you)") { t.Fatal("a refused email was kept") } // The owner's: theirs on the row, nowhere else. if status, loc := r.post(owner, "/conversation/connections/"+token+"/email", url.Values{"email": {" me@example.com "}}); status != http.StatusSeeOther || loc != page { t.Fatalf("the owner's email: %d to %q", status, loc) } row = rowHTML(t, r.get(owner, page, 200), token) for _, want := range []string{"me@example.com (added by you)", "Change the account email", `value="me@example.com"`} { if !strings.Contains(row, want) { t.Fatalf("the stated email row lacks %q: %s", want, row) } } list, err := r.client.Connections(r.ctx) if err != nil { t.Fatal(err) } for _, c := range list.Items { if c.ID == token && c.Label != "" { t.Fatalf("the stated email became the provider's label: %+v", c) } } var sent int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE body LIKE '%me@example.com%'").Scan(&sent); err != nil || sent != 0 { t.Fatalf("the stated email went to the runner: %d %v", sent, err) } if raw := rawEvents(t, r.rig); strings.Contains(raw, "me@example.com") { t.Fatal("the stated email is in the event stream") } // The next hello keeps it; the provider's own name would win. hello := held(wire.HeldConnection{LocalID: "claude-9", Person: Owner, Provider: "claude", Kind: wire.KindToken, State: wire.ConnHealthy}) hello.Generation = runner.Welcome.Generation if err := runner.Send(r.ctx, "h2", wire.FrameHello, hello); err != nil { t.Fatal(err) } r.wait("the login signed out", func(c *Conversation) bool { return c.Connections[login].State == wire.ConnSignedOut }) if row = rowHTML(t, r.get(owner, page, 200), token); !strings.Contains(row, "me@example.com ") { t.Fatal("the next hello dropped the stated email") } // Turns answered on it a while ago and still no window: Claude has // not reported usage for it. A recent one, or a check, proves nothing. usage := func(role string, ago time.Duration) { if _, err := r.inst.DB.Writer().Exec("INSERT INTO chat_usage(person, connection, provider, model, role, turn_id, step_id, attempt, outcome, recorded_at) VALUES(?,?,?,?,?,?,?,?,?,?)", Owner, "claude-9", "claude", "claude-opus-5-5", role, "t-"+role+ago.String(), "", 1, wire.OutcomeReplied, time.Now().UTC().Add(-ago)); err != nil { t.Fatal(err) } } usage("check", time.Hour) usage("coordinator", time.Minute) if row = rowHTML(t, r.get(owner, page, 200), token); !strings.Contains(row, "Usage shows after Claude reports it") { t.Fatal("a check or a fresh turn was taken as usage never reported") } usage("coordinator", time.Hour) if row = rowHTML(t, r.get(owner, page, 200), token); !strings.Contains(row, `data-usage-note>Claude has not reported usage for this connection<`) { t.Fatalf("the served row does not say usage is not reported: %s", row) } }