//go:build browser package browsertest import ( "strconv" "testing" "time" "github.com/chromedp/chromedp" ) // offerJS says where connect card id is: "none" while it is not drawn, // "loose" on its own in the conversation (with its place among the // thread's lines and the thinking bubble's or draft's), or "in reply" once // it is the last thing in a settled reply that starts with reply, its // button going to Base's own connect page in this tab. func offerJS(id, reply string) string { return `(() => { const o = document.querySelector('#ask-panel [data-card=' + ` + strconv.Quote(strconv.Quote(id)) + ` + ']'); if (!o) return "none"; const li = o.closest("li.msg.coordinator"); if (!li) { const kids = [...document.querySelectorAll("#ask-panel .thread > li")]; const busy = kids.findIndex((k) => k.matches(".thinking, [data-draft]")); return "loose " + kids.indexOf(o) + "/" + busy + "/" + kids.length; } if (!li.dataset.id || li.matches(".thinking, [data-draft]")) return "in a draft"; if (li.lastElementChild !== o) return "not last in its reply"; if (!li.textContent.startsWith(` + strconv.Quote(reply) + `)) return "in another reply: " + li.textContent.slice(0, 40); const a = o.querySelector("a.connect-btn"); if (!a) return "no button"; if (a.hasAttribute("target") || new URL(a.href).pathname !== "/ask/oficina/connect") return "button goes " + a.href + " " + a.target; return "in reply"; })()` } // A connect card is a button at the end of the Assistant's reply that // raised it, never above it: its event comes while the reply streams, and // it waits for the reply to land. When no words come for a while it shows // on its own below the thinking bubble, and moves into the reply once it // lands. Once connected it is a quiet line in the same place. func TestAskConnectButtonFollowsItsReply(t *testing.T) { rig, f, _ := askBaseOn(t, nil, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))) th := thinking{t, rig, f} drive(t, rig, reducedMotion("dark", false), 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(500*time.Millisecond)) const reply = "Connect your Oficina apps and I'll show you this week." turn := th.send("What's on my calendar this week?") id := f.RaiseConnect(turn, rig.Origin+"/ask/oficina/connect") drive(t, rig, chromedp.Sleep(600*time.Millisecond)) if got := th.eval(offerJS(id, reply)); got != "none" { t.Errorf("while the reply is on its way the card is %q, want held (none)", got) } f.Stream(turn, reply) if got := th.eval(offerJS(id, reply)); got != "none" { t.Errorf("while the reply streams the card is %q, want held (none)", got) } f.Answer(turn, reply, false) th.wait(`!!document.querySelector('#ask-panel .thread > li.msg.coordinator[data-id]') && !document.querySelector("#ask-panel .thinking, #ask-panel [data-draft]")`) drive(t, rig, chromedp.Sleep(1500*time.Millisecond)) viewportShot(t, rig, "connect-390-reply") th.wait(offerJS(id, reply) + ` === "in reply"`) if got := th.eval(`[...document.querySelectorAll('#ask-panel .connect-offer')].map((o) => o.innerText.replace(/\s+/g, " ").trim()).join("|")`); got != "Connect Oficina Calendar, Docs, Sheets and more" { t.Errorf("the offer says %q", got) } if n := th.eval(`String(document.querySelectorAll("#ask-panel li.msg.card").length)`); n != "0" { t.Errorf("%s loose cards beside the offer, want none", n) } // Connected: the button goes, a quiet line takes its place. f.SetCard(id, "resolved", "") th.wait(`(() => { const o = document.querySelector('#ask-panel li.msg.coordinator > .connect-offer.connected'); return !!o && !o.querySelector("a") && o.innerText.trim() === "Connected"; })()`) viewportShot(t, rig, "connect-390-connected") // No words for a while: the card shows on its own after the thinking // bubble, the draft stays above it, and the reply takes it in. const again = "Connect your Oficina apps and I'll find you a free slot." turn = th.send("When am I free tomorrow?") id = f.RaiseConnect(turn, rig.Origin+"/ask/oficina/connect") th.wait(offerJS(id, again) + `.startsWith("loose")`) if got := th.eval(`(() => { const kids = [...document.querySelectorAll("#ask-panel .thread > li")]; return String(kids.at(-1).matches(".connect-offer") && kids.at(-2).matches(".thinking")); })()`); got != "true" { t.Errorf("without words the card is not right after the thinking bubble: %s", th.eval(offerJS(id, again))) } f.Stream(turn, again) if got := th.eval(`(() => { const kids = [...document.querySelectorAll("#ask-panel .thread > li")]; return String(kids.at(-1).matches(".connect-offer") && kids.at(-2).matches("[data-draft]")); })()`); got != "true" { t.Errorf("while words stream the card is not right below the draft: %s", th.eval(offerJS(id, again))) } f.Answer(turn, again, false) th.wait(offerJS(id, again) + ` === "in reply"`) // The first, connected, stays with its own reply. if n := th.eval(`String(document.querySelectorAll("#ask-panel .connect-offer.connected").length)`); n != "1" { t.Errorf("%s connected lines, want the first one kept", n) } viewportShot(t, rig, "connect-390-second") drive(t, rig, reducedMotion("light", false), chromedp.Sleep(300*time.Millisecond)) viewportShot(t, rig, "connect-390-second-light") noOverflow(t, rig, "the chat with a connect button") }