| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // tokenRowRig is a runner whose hello holds the owner's Claude token |
| 14 | // connection (no account named, no usage reported) and a Claude login |
| 15 | // the provider named, with its week window. It returns the token row's |
| 16 | // id and the login's. |
| 17 | func tokenRowRig(t *testing.T) (r *toolRig, runner *FakeRunner, token, login string) { |
| 18 | t.Helper() |
| 19 | r = &toolRig{rig: newRig(t)} |
| 20 | runner = r.dial(0) |
| 21 | reset := time.Now().Add(30 * time.Hour).UTC().Truncate(time.Minute) |
| 22 | hello := held(wire.HeldConnection{LocalID: "claude-9", Person: Owner, Provider: "claude", Kind: wire.KindToken, State: wire.ConnHealthy}, |
| 23 | 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}}}}) |
| 24 | hello.Generation = runner.Welcome.Generation |
| 25 | if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | r.wait("both rows", func(c *Conversation) bool { |
| 29 | for _, v := range c.Connections { |
| 30 | switch v.LocalID { |
| 31 | case "claude-9": |
| 32 | token = v.ID |
| 33 | case "claude-1": |
| 34 | login = v.ID |
| 35 | } |
| 36 | } |
| 37 | return token != "" && login != "" |
| 38 | }) |
| 39 | return r, runner, token, login |
| 40 | } |
| 41 | |
| 42 | // rowHTML is one connection's row on the page. |
| 43 | func rowHTML(t *testing.T, page, id string) string { |
| 44 | t.Helper() |
| 45 | _, row, ok := strings.Cut(page, `<article equipo-conn-row data-connection="`+id+`"`) |
| 46 | if !ok { |
| 47 | t.Fatalf("no row %s", id) |
| 48 | } |
| 49 | row, _, _ = strings.Cut(row, "</article>") |
| 50 | return row |
| 51 | } |
| 52 | |
| 53 | // TestPagesTokenRowFacts (Paul, 2026-11-11: "Why does this Claude |
| 54 | // account not show its email address or its usage?"): a token row the |
| 55 | // provider named no account for and reported no usage on says so |
| 56 | // honestly. It offers the account email field (open once, after the |
| 57 | // sign-in), says usage shows once Claude reports it, and, once turns |
| 58 | // were answered on it with still no window, that Claude has not |
| 59 | // reported usage for it. An email the owner types shows as theirs, |
| 60 | // "(added by you)", and goes nowhere else: not in the API, not to the |
| 61 | // runner. A teammate, a cross-site post and a bad shape are refused. |
| 62 | func TestPagesTokenRowFacts(t *testing.T) { |
| 63 | r, runner, token, login := tokenRowRig(t) |
| 64 | owner := r.inst.Cookie() |
| 65 | const page = "/conversation/settings/connections" |
| 66 | |
| 67 | row := rowHTML(t, r.get(owner, page, 200), token) |
| 68 | for _, want := range []string{`data-account-email="` + token + `"`, "Add the account email", "Claude account email", `data-usage-note>Usage shows after Claude reports it<`} { |
| 69 | if !strings.Contains(row, want) { |
| 70 | t.Fatalf("the token row lacks %q: %s", want, row) |
| 71 | } |
| 72 | } |
| 73 | if strings.Contains(row, "(added by you)") || strings.Contains(row, `data-account-email="`+token+`" open`) { |
| 74 | t.Fatal("the token row claims an email, or opens the field unasked") |
| 75 | } |
| 76 | 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) { |
| 77 | t.Fatalf("the named, metered row changed: %s", other) |
| 78 | } |
| 79 | // Just signed in: the question is open, once. |
| 80 | if row = rowHTML(t, r.get(owner, page+"?added="+token, 200), token); !strings.Contains(row, `data-account-email="`+token+`" open`) { |
| 81 | t.Fatal("the question is not asked after the sign-in") |
| 82 | } |
| 83 | |
| 84 | // Refused: a teammate, a cross-site post, a bad shape. |
| 85 | email := func(c *http.Cookie, id, v string) int { |
| 86 | status, _ := r.post(c, "/conversation/connections/"+id+"/email", url.Values{"email": {v}}) |
| 87 | return status |
| 88 | } |
| 89 | if status := email(r.memberCookie(), token, "mallory@example.com"); status != http.StatusNotFound { |
| 90 | t.Fatalf("a teammate set the email: %d", status) |
| 91 | } |
| 92 | cross, _ := http.NewRequest("POST", r.inst.URL+"/conversation/connections/"+token+"/email", strings.NewReader(url.Values{"email": {"cross@example.com"}}.Encode())) |
| 93 | cross.AddCookie(owner) |
| 94 | cross.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 95 | cross.Header.Set("Origin", "https://elsewhere.example") |
| 96 | cross.Header.Set("Sec-Fetch-Site", "cross-site") |
| 97 | if res, err := http.DefaultClient.Do(cross); err != nil || res.StatusCode != http.StatusForbidden { |
| 98 | t.Fatalf("a cross-site post: %v %v", res.StatusCode, err) |
| 99 | } else { |
| 100 | res.Body.Close() |
| 101 | } |
| 102 | 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"} { |
| 103 | if status := email(owner, token, bad); status != http.StatusUnprocessableEntity { |
| 104 | t.Fatalf("%q: %d", bad, status) |
| 105 | } |
| 106 | } |
| 107 | if status := email(owner, login, "me@example.com"); status != http.StatusConflict { |
| 108 | t.Fatalf("an email on a login the provider names: %d", status) |
| 109 | } |
| 110 | if row = rowHTML(t, r.get(owner, page, 200), token); strings.Contains(row, "(added by you)") { |
| 111 | t.Fatal("a refused email was kept") |
| 112 | } |
| 113 | |
| 114 | // The owner's: theirs on the row, nowhere else. |
| 115 | if status, loc := r.post(owner, "/conversation/connections/"+token+"/email", url.Values{"email": {" me@example.com "}}); status != http.StatusSeeOther || loc != page { |
| 116 | t.Fatalf("the owner's email: %d to %q", status, loc) |
| 117 | } |
| 118 | row = rowHTML(t, r.get(owner, page, 200), token) |
| 119 | for _, want := range []string{"<span data-stated-email>me@example.com</span> <span equipo-conn-said>(added by you)</span>", "Change the account email", `value="me@example.com"`} { |
| 120 | if !strings.Contains(row, want) { |
| 121 | t.Fatalf("the stated email row lacks %q: %s", want, row) |
| 122 | } |
| 123 | } |
| 124 | list, err := r.client.Connections(r.ctx) |
| 125 | if err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | for _, c := range list.Items { |
| 129 | if c.ID == token && c.Label != "" { |
| 130 | t.Fatalf("the stated email became the provider's label: %+v", c) |
| 131 | } |
| 132 | } |
| 133 | var sent int |
| 134 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE body LIKE '%me@example.com%'").Scan(&sent); err != nil || sent != 0 { |
| 135 | t.Fatalf("the stated email went to the runner: %d %v", sent, err) |
| 136 | } |
| 137 | if raw := rawEvents(t, r.rig); strings.Contains(raw, "me@example.com") { |
| 138 | t.Fatal("the stated email is in the event stream") |
| 139 | } |
| 140 | // The next hello keeps it; the provider's own name would win. |
| 141 | hello := held(wire.HeldConnection{LocalID: "claude-9", Person: Owner, Provider: "claude", Kind: wire.KindToken, State: wire.ConnHealthy}) |
| 142 | hello.Generation = runner.Welcome.Generation |
| 143 | if err := runner.Send(r.ctx, "h2", wire.FrameHello, hello); err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | r.wait("the login signed out", func(c *Conversation) bool { return c.Connections[login].State == wire.ConnSignedOut }) |
| 147 | if row = rowHTML(t, r.get(owner, page, 200), token); !strings.Contains(row, "me@example.com</span> <span equipo-conn-said>") { |
| 148 | t.Fatal("the next hello dropped the stated email") |
| 149 | } |
| 150 | |
| 151 | // Turns answered on it a while ago and still no window: Claude has |
| 152 | // not reported usage for it. A recent one, or a check, proves nothing. |
| 153 | usage := func(role string, ago time.Duration) { |
| 154 | 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 { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | } |
| 158 | usage("check", time.Hour) |
| 159 | usage("coordinator", time.Minute) |
| 160 | if row = rowHTML(t, r.get(owner, page, 200), token); !strings.Contains(row, "Usage shows after Claude reports it") { |
| 161 | t.Fatal("a check or a fresh turn was taken as usage never reported") |
| 162 | } |
| 163 | usage("coordinator", time.Hour) |
| 164 | if row = rowHTML(t, r.get(owner, page, 200), token); !strings.Contains(row, `data-usage-note>Claude has not reported usage for this connection<`) { |
| 165 | t.Fatalf("the served row does not say usage is not reported: %s", row) |
| 166 | } |
| 167 | } |
| 168 | |