//go:build browser package browsertest import ( "fmt" "strings" "testing" "time" "github.com/chromedp/chromedp" "amadan.net/rastrillo/rastrillo/harness" ) // lastVisible is "" when the conversation's last bubble is wholly inside // the log, above the status row and the composer, and the log is at its // bottom; else what is wrong. const lastVisible = `(() => { const p = document.querySelector("#ask-panel"), log = p.querySelector(".thread"); const items = log.querySelectorAll(":scope > li"), last = items[items.length - 1]; if (!last) return "no bubbles"; const l = log.getBoundingClientRect(), b = last.getBoundingClientRect(); const foot = p.querySelector(".ask-foot").getBoundingClientRect(), comp = p.querySelector("form.composer").getBoundingClientRect(); const out = []; if (b.bottom > l.bottom + 1) out.push("last bubble ends at " + Math.round(b.bottom) + ", below the log's " + Math.round(l.bottom)); if (b.bottom > foot.top + 1 && foot.height > 0) out.push("last bubble under the status row"); if (l.bottom > comp.top + 1) out.push("log runs under the composer"); if (foot.height > 0 && l.bottom > foot.top + 1) out.push("log runs under the status row"); if (log.scrollHeight - log.scrollTop - log.clientHeight > 2) out.push("log not at its bottom"); return out.join("; "); })()` // The conversation follows its newest line docked and on a phone: // after sending it is at the bottom, with the last bubble wholly above the // status row; streaming keeps it there; scrolled up, it stays put and // offers Jump to latest. func TestAskScrollFollowsNewest(t *testing.T) { for _, size := range []struct { name string w, h int64 }{{"docked", 1280, 800}, {"phone", 390, 844}} { t.Run(size.name, func(t *testing.T) { var view chromedp.Action[chromedp.Void] if size.name == "phone" { view = chromedp.EmulateViewport(size.w, size.h, chromedp.EmulateMobile, chromedp.EmulateScale(3)) } rig, f, _ := askBaseOn(t, nil, view) f.Reply = func(string) string { return strings.Repeat("Here is a long answer that streams in word by word. ", 12) } for i := 1; i <= 30; i++ { f.Say(fmt.Sprintf("Earlier line %d of a long conversation.", i)) } if size.name == "phone" { drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(`a.base-btn`)), chromedp.Click(chromedp.CSS(`a.base-btn`)), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)), chromedp.Poll[chromedp.Void](readJS, chromedp.WithPollingTimeout(5*time.Second)), chromedp.Sleep(100*time.Millisecond)) } else { openPanel(t, rig) } wait := func(js string) { t.Helper() drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second))) } wait(`document.querySelectorAll("#ask-panel .thread > li").length >= 30`) if got := harness.Evaluate[string](rig, lastVisible); got != "" { t.Errorf("on opening: %s", got) } // Send while the coordinator has not started: the Assistant's // thinking bubble, under the person's, is wholly above Stop. send := func(text string) { t.Helper() // Enter sends, as a person does on a keyboard; scrolled to the // top first, so sending must bring the log back down. drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-panel .thread").scrollTop = 0`), chromedp.SendKeys(chromedp.CSS(`#ask-text`), text+"\r")) } f.Hold = true send("Can we fix a bug in Tito?") if _, err := chromedp.Run(rig.Context(), chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel form.composer button.stop").hidden && document.querySelector("#ask-panel .thread > li:last-child.thinking")`, chromedp.WithPollingTimeout(10*time.Second))); err != nil { t.Fatalf("no thinking bubble: %v; person bubbles %d, status %q, field %q", err, harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .msg.person").length`), harness.Evaluate[string](rig, `document.querySelector("#ask-panel .ask-status").textContent`), harness.Evaluate[string](rig, `document.querySelector("#ask-text").value`)) } drive(t, rig, chromedp.Sleep(200*time.Millisecond)) if got := harness.Evaluate[string](rig, lastVisible); got != "" { t.Errorf("sent, waiting for an answer: %s", got) } drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) wait(`document.querySelector("#ask-panel form.composer button.stop").hidden`) f.Hold = false // Send: the log goes to the bottom, and stays there as the reply // streams into the thinking bubble. send("What can I do") wait(`(() => { const d = document.querySelector("#ask-panel .msg.draft"); return d && d.textContent.length > 120 })()`) if got := harness.Evaluate[string](rig, lastVisible); got != "" { t.Errorf("while streaming: %s", got) } wait(`!document.querySelector("#ask-panel .msg.draft") && document.querySelector("#ask-panel .ask-status").textContent === ""`) if got := harness.Evaluate[string](rig, lastVisible); got != "" { t.Errorf("after the reply: %s", got) } // Scrolled up to read: a new line does not move the log. drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-panel .thread").scrollTop = 100`), chromedp.Sleep(200*time.Millisecond)) // the scroll event, a frame later f.Say("A new line while you read.") wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent === "A new line while you read.")`) if got := harness.Evaluate[int](rig, `Math.round(document.querySelector("#ask-panel .thread").scrollTop)`); got != 100 { t.Errorf("reading earlier lines, the log moved to %d", got) } jump := `#ask-panel button.jump` wait(`!document.querySelector("#ask-panel button.jump").hidden`) if got := harness.Evaluate[string](rig, `document.querySelector("`+jump+`").textContent`); got != "Jump to latest" { t.Errorf("jump control reads %q", got) } drive(t, rig, chromedp.Click(chromedp.CSS(jump))) wait(`document.querySelector("#ask-panel button.jump").hidden`) if got := harness.Evaluate[string](rig, lastVisible); got != "" { t.Errorf("after Jump to latest: %s", got) } }) } }