| 1 | //go:build browser |
| 2 | |
| 3 | package browsertest |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/chromedp/chromedp" |
| 11 | ) |
| 12 | |
| 13 | // offerJS says where connect card id is: "none" while it is not drawn, |
| 14 | // "loose" on its own in the conversation (with its place among the |
| 15 | // thread's lines and the thinking bubble's or draft's), or "in reply" once |
| 16 | // it is the last thing in a settled reply that starts with reply, its |
| 17 | // button going to Base's own connect page in this tab. |
| 18 | func offerJS(id, reply string) string { |
| 19 | return `(() => { const o = document.querySelector('#ask-panel [data-card=' + ` + strconv.Quote(strconv.Quote(id)) + ` + ']'); |
| 20 | if (!o) return "none"; |
| 21 | const li = o.closest("li.msg.coordinator"); |
| 22 | if (!li) { const kids = [...document.querySelectorAll("#ask-panel .thread > li")]; |
| 23 | const busy = kids.findIndex((k) => k.matches(".thinking, [data-draft]")); |
| 24 | return "loose " + kids.indexOf(o) + "/" + busy + "/" + kids.length; } |
| 25 | if (!li.dataset.id || li.matches(".thinking, [data-draft]")) return "in a draft"; |
| 26 | if (li.lastElementChild !== o) return "not last in its reply"; |
| 27 | if (!li.textContent.startsWith(` + strconv.Quote(reply) + `)) return "in another reply: " + li.textContent.slice(0, 40); |
| 28 | const a = o.querySelector("a.connect-btn"); |
| 29 | if (!a) return "no button"; |
| 30 | if (a.hasAttribute("target") || new URL(a.href).pathname !== "/ask/oficina/connect") return "button goes " + a.href + " " + a.target; |
| 31 | return "in reply"; })()` |
| 32 | } |
| 33 | |
| 34 | // A connect card is a button at the end of the Assistant's reply that |
| 35 | // raised it, never above it: its event comes while the reply streams, and |
| 36 | // it waits for the reply to land. When no words come for a while it shows |
| 37 | // on its own below the thinking bubble, and moves into the reply once it |
| 38 | // lands. Once connected it is a quiet line in the same place. |
| 39 | func TestAskConnectButtonFollowsItsReply(t *testing.T) { |
| 40 | rig, f, _ := askBaseOn(t, nil, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))) |
| 41 | th := thinking{t, rig, f} |
| 42 | drive(t, rig, reducedMotion("dark", false), chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(`a.base-btn`)), |
| 43 | chromedp.Click(chromedp.CSS(`a.base-btn`)), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)), |
| 44 | chromedp.Poll[chromedp.Void](readJS, chromedp.WithPollingTimeout(5*time.Second)), chromedp.Sleep(500*time.Millisecond)) |
| 45 | |
| 46 | const reply = "Connect your Oficina apps and I'll show you this week." |
| 47 | turn := th.send("What's on my calendar this week?") |
| 48 | id := f.RaiseConnect(turn, rig.Origin+"/ask/oficina/connect") |
| 49 | drive(t, rig, chromedp.Sleep(600*time.Millisecond)) |
| 50 | if got := th.eval(offerJS(id, reply)); got != "none" { |
| 51 | t.Errorf("while the reply is on its way the card is %q, want held (none)", got) |
| 52 | } |
| 53 | f.Stream(turn, reply) |
| 54 | if got := th.eval(offerJS(id, reply)); got != "none" { |
| 55 | t.Errorf("while the reply streams the card is %q, want held (none)", got) |
| 56 | } |
| 57 | f.Answer(turn, reply, false) |
| 58 | th.wait(`!!document.querySelector('#ask-panel .thread > li.msg.coordinator[data-id]') && !document.querySelector("#ask-panel .thinking, #ask-panel [data-draft]")`) |
| 59 | drive(t, rig, chromedp.Sleep(1500*time.Millisecond)) |
| 60 | viewportShot(t, rig, "connect-390-reply") |
| 61 | th.wait(offerJS(id, reply) + ` === "in reply"`) |
| 62 | 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" { |
| 63 | t.Errorf("the offer says %q", got) |
| 64 | } |
| 65 | if n := th.eval(`String(document.querySelectorAll("#ask-panel li.msg.card").length)`); n != "0" { |
| 66 | t.Errorf("%s loose cards beside the offer, want none", n) |
| 67 | } |
| 68 | |
| 69 | // Connected: the button goes, a quiet line takes its place. |
| 70 | f.SetCard(id, "resolved", "") |
| 71 | th.wait(`(() => { const o = document.querySelector('#ask-panel li.msg.coordinator > .connect-offer.connected'); return !!o && !o.querySelector("a") && o.innerText.trim() === "Connected"; })()`) |
| 72 | viewportShot(t, rig, "connect-390-connected") |
| 73 | |
| 74 | // No words for a while: the card shows on its own after the thinking |
| 75 | // bubble, the draft stays above it, and the reply takes it in. |
| 76 | const again = "Connect your Oficina apps and I'll find you a free slot." |
| 77 | turn = th.send("When am I free tomorrow?") |
| 78 | id = f.RaiseConnect(turn, rig.Origin+"/ask/oficina/connect") |
| 79 | th.wait(offerJS(id, again) + `.startsWith("loose")`) |
| 80 | 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" { |
| 81 | t.Errorf("without words the card is not right after the thinking bubble: %s", th.eval(offerJS(id, again))) |
| 82 | } |
| 83 | f.Stream(turn, again) |
| 84 | 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" { |
| 85 | t.Errorf("while words stream the card is not right below the draft: %s", th.eval(offerJS(id, again))) |
| 86 | } |
| 87 | f.Answer(turn, again, false) |
| 88 | th.wait(offerJS(id, again) + ` === "in reply"`) |
| 89 | // The first, connected, stays with its own reply. |
| 90 | if n := th.eval(`String(document.querySelectorAll("#ask-panel .connect-offer.connected").length)`); n != "1" { |
| 91 | t.Errorf("%s connected lines, want the first one kept", n) |
| 92 | } |
| 93 | viewportShot(t, rig, "connect-390-second") |
| 94 | drive(t, rig, reducedMotion("light", false), chromedp.Sleep(300*time.Millisecond)) |
| 95 | viewportShot(t, rig, "connect-390-second-light") |
| 96 | noOverflow(t, rig, "the chat with a connect button") |
| 97 | } |
| 98 | |