//go:build browser package thintest import ( "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // TestBrowserTryAgain: a reply fails while the conversation is open. The // note comes from the event at once, then says why in the server's words, // with the account and Try again. Try again goes in place, with no // reload: the note and its button go, the thinking line comes, and the // runner has the same message again, sent once. Without scripts Try again // is a form post (TestContractRetryAnswersAgain). Tried again from // another device, the open page drops the note as the retry starts. Saved as // try-again-.png when EQUIPO_BROWSER_ARTIFACTS names a directory. func TestBrowserTryAgain(t *testing.T) { r, runner := retryRig(t, nil) p := openInner(t, r.inst.URL, r.inst.Cookie(), 390, 844, true, "light", "no-preference") defer p.stop() p.open("/conversation") p.wait(`document.querySelector('[data-chat-stream]')`, "the conversation") const words = "Where is the login bug?" failed := r.say(runner, words, "k1").TurnID r.failOn(runner, failed, "provider_failed", "claude-a") note := `document.querySelector('[data-ended=` + jsString(failed) + `]')` p.wait(`(`+note+`?.querySelector('p')||{}).textContent===`+jsString("Claude (owner@example.com) stopped before finishing the reply. Try again.")+` && !!`+note+`.querySelector('button[data-retry-turn]')`, "the note's reason, account and Try again") p.check(`(b=>b.textContent==='Try again' && b.getBoundingClientRect().height>0 && b.getBoundingClientRect().width>0)(`+note+`.querySelector('[data-retry-turn]'))`, "Try again is not visible") p.fits("the failed reply's note") p.shot("try-again-390") p.click(`[data-retry-turn]`) cmd := r.running(runner) if cmd.TurnID == failed || strings.Join(cmd.PersonText, "|") != words { t.Fatalf("Try again sent %s %q", cmd.TurnID, cmd.PersonText) } p.wait(`!document.querySelector('[data-ended]') && !document.querySelector('[data-retry-turn]') && !!document.querySelector('[equipo-thinking][data-turn=`+jsString(cmd.TurnID)+`]')`, "the note gone and the thinking line there") p.check(`[...document.querySelectorAll('[equipo-message="own"] [equipo-message-body]')].filter(e=>e.textContent===`+jsString(words)+`).length===1`, "the message shows twice, or not at all") p.stayed("Try again") if n := r.count("SELECT count(*) FROM chat_turns WHERE retry_of = ?", failed); n != 1 { t.Fatalf("%d retries queued", n) } // It fails again, and another device tries again: the open page // follows, the note going as the retry starts. r.failOn(runner, cmd.TurnID, "turn_timeout", "") again := `document.querySelector('[data-ended=` + jsString(cmd.TurnID) + `] [data-retry-turn]')` p.wait(again, "the failed retry's Try again") if _, err := r.client.Retry(r.ctx, cmd.TurnID); err != nil { t.Fatal(err) } p.wait(`!document.querySelector('[data-ended]')`, "the note gone when another device tried again") p.stayed("another device's Try again") } // TestBrowserEndedNoteStaysGone: a reply fails, then the next message // waits for a connection. The note goes as that turn starts, and the // refresh that brings the connection card does not bring it back. func TestBrowserEndedNoteStaysGone(t *testing.T) { r, runner := retryRig(t, nil) p := openInner(t, r.inst.URL, r.inst.Cookie(), 390, 844, true, "light", "no-preference") defer p.stop() p.open("/conversation") p.wait(`document.querySelector('[data-chat-stream]')`, "the conversation") failed := r.say(runner, "First", "k1").TurnID r.failOn(runner, failed, "turn_timeout", "") p.wait(`document.querySelector('[data-ended=`+jsString(failed)+`] [data-retry-turn]')`, "the failed reply's note") waiting := r.say(runner, "Second", "k2").TurnID runner.Send(r.ctx, "w-"+waiting, wire.FrameTurn, wire.TurnReport{TurnID: waiting, Attempt: 1, State: wire.TurnWaiting, Reason: wire.ReasonNoConnection}) p.wait(`!!document.querySelector('[data-no-connection]') && !document.querySelector('[data-ended]')`, "the connection card, and no note") p.check(`!document.querySelector('[data-ended]')`, "the note came back") p.stayed("waiting for a connection") }