package thintest import ( "context" "io" "net/http" "net/url" "regexp" "slices" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/wire" ) // Try again (Paul, 2026-11-22): the note under a failed reply says why, // names the account it ran on, and offers Try again: a new turn takes // the failed turn's exchange over as its next attempt (chat.Retry). The // message is never sent twice, and its words never ride again as // unanswered. // retryRig is a rig whose runner is online with conns (toolHello's one // Claude connection, claude-a, labelled with the owner's email, when nil). func retryRig(t *testing.T, conns []wire.HeldConnection) (*rig, *FakeRunner) { t.Helper() r := newRig(t) runner := r.dial(0) hello := toolHello(runner.Welcome.Generation) if conns != nil { hello.Connections = conns } 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 }) return r, runner } // running is the next message frame, acked, once its turn runs. func (r *rig) running(runner *FakeRunner) *wire.MessageCommand { r.t.Helper() f, body, err := runner.Expect(r.ctx, wire.FrameMessage) if err != nil { r.t.Fatal(err) } runner.Send(r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}) cmd := body.(*wire.MessageCommand) r.turnIs(cmd.TurnID, wire.TurnRunning) return cmd } // say sends text and returns its running turn's frame. func (r *rig) say(runner *FakeRunner, text, key string) *wire.MessageCommand { r.t.Helper() if _, err := r.client.Send(r.ctx, text, key); err != nil { r.t.Fatal(err) } return r.running(runner) } // attempt is a turn's current attempt (a retry is its failed turn's next). func (r *rig) attempt(id string) int64 { r.t.Helper() v, err := r.client.Turn(r.ctx, id) if err != nil { r.t.Fatal(err) } return v.Attempt } // failOn fails a running turn for reason; local names the connection // its attempt ran on (a usage row, as a runner reports one), or none. func (r *rig) failOn(runner *FakeRunner, id, reason, local string) { r.t.Helper() attempt := r.attempt(id) if local != "" { runner.Send(r.ctx, "u-"+id, wire.FrameUsage, wire.UsageReport{TurnID: id, Rows: []wire.Usage{{Provider: wire.ProviderClaude, Connection: local, Model: "claude-sonnet", Attempt: attempt, Outcome: wire.OutcomeFailed}}}) } runner.Send(r.ctx, "f-"+id, wire.FrameTurn, wire.TurnReport{TurnID: id, Attempt: attempt, State: wire.TurnFailed, Reason: reason}) r.turnIs(id, wire.TurnFailed) } // tryAgain posts the page's Try again as the owner's browser, from site. func (r *rig) tryAgain(id, site string) (int, string) { r.t.Helper() req, _ := http.NewRequest("POST", r.inst.URL+"/conversation/turns/"+id+"/retry", strings.NewReader(url.Values{}.Encode())) req.AddCookie(r.inst.Cookie()) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") origin := r.inst.URL if site != "same-origin" { origin = "https://foreign.example" } req.Header.Set("Origin", origin) req.Header.Set("Sec-Fetch-Site", site) res, err := (&http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}).Do(req) if err != nil { r.t.Fatal(err) } io.Copy(io.Discard, res.Body) res.Body.Close() return res.StatusCode, res.Header.Get("Location") } func (r *rig) count(query string, args ...any) int { r.t.Helper() var n int if err := r.inst.DB.Writer().QueryRow(query, args...).Scan(&n); err != nil { r.t.Fatal(err) } return n } func retryForm(id string) string { return `