| 1 | //go:build browser |
| 2 | |
| 3 | package browsertest |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "regexp" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/chromedp/chromedp" |
| 14 | |
| 15 | "amadan.net/rastrillo/rastrillo/harness" |
| 16 | |
| 17 | "amadan.net/oficina/base/internal/equipo/equipotest" |
| 18 | ) |
| 19 | |
| 20 | // thinkingSel is the Assistant's thinking bubble: on its side of the |
| 21 | // conversation, where its answer will appear. |
| 22 | const thinkingSel = `#ask-panel .thread > li.msg.coordinator.thinking` |
| 23 | |
| 24 | // countFramesJS counts the page's requestAnimationFrame calls from now on, |
| 25 | // in window.__raf: the spark's loop is the only thing in the chat that |
| 26 | // asks for frames while it waits. (chromedp's Poll asks for frames too: |
| 27 | // count from after the last one.) |
| 28 | const countFramesJS = `(() => { window.__raf = 0; if (!window.__rafWrapped) { const r = window.requestAnimationFrame.bind(window); |
| 29 | window.requestAnimationFrame = (cb) => { window.__raf++; return r(cb); }; window.__rafWrapped = true; } })()` |
| 30 | |
| 31 | // noGLJS makes this page's canvases refuse a WebGL context, as a browser |
| 32 | // without WebGL does. |
| 33 | const noGLJS = `(() => { const get = HTMLCanvasElement.prototype.getContext; |
| 34 | HTMLCanvasElement.prototype.getContext = function (type, ...rest) { return /webgl/.test(type) ? null : get.call(this, type, ...rest); }; })()` |
| 35 | |
| 36 | type thinking struct { |
| 37 | t *testing.T |
| 38 | rig *harness.Rig |
| 39 | f *equipotest.Fake |
| 40 | } |
| 41 | |
| 42 | func (th thinking) wait(js string) { |
| 43 | th.t.Helper() |
| 44 | drive(th.t, th.rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second))) |
| 45 | } |
| 46 | |
| 47 | func (th thinking) eval(js string) string { |
| 48 | th.t.Helper() |
| 49 | return harness.Evaluate[string](th.rig, js) |
| 50 | } |
| 51 | |
| 52 | // send asks text and holds the turn running; it returns the turn's id, |
| 53 | // once the bubble shows. |
| 54 | func (th thinking) send(text string) string { |
| 55 | th.t.Helper() |
| 56 | th.f.Hold = true |
| 57 | drive(th.t, th.rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), text+"\r")) |
| 58 | th.wait(`!!document.querySelector(` + strconv.Quote(thinkingSel) + `) && !document.querySelector("#ask-panel form.composer button.stop").hidden`) |
| 59 | m := regexp.MustCompile(`^/ask/turns/([^/]+)/cancel$`).FindStringSubmatch(th.eval(`document.querySelector("#ask-panel form.cancel").getAttribute("action")`)) |
| 60 | if m == nil { |
| 61 | th.t.Fatal("no turn to cancel while the bubble shows") |
| 62 | } |
| 63 | return m[1] |
| 64 | } |
| 65 | |
| 66 | // statusJS is the status line above the field: its text, and whether it |
| 67 | // takes any room. |
| 68 | const statusJS = `(() => { const s = document.querySelector("#ask-panel .ask-status"); |
| 69 | return JSON.stringify(s.textContent) + (s.getBoundingClientRect().height > 0 ? " shown" : " empty"); })()` |
| 70 | |
| 71 | // While the Assistant answers, its thinking bubble sits at the end of the |
| 72 | // conversation: three soft dots shimmering through Base's palette, no |
| 73 | // words, until Equipo says what it is doing; the status line above the |
| 74 | // field stays empty; a spark runs round the bubble's edge on a WebGL |
| 75 | // canvas made for it. The bubble becomes the reply in place, streamed or |
| 76 | // not; Stop and a failure take it away; a team_update turn shows none; |
| 77 | // scripts off, the status line says it as before. |
| 78 | func TestAskThinkingBubble(t *testing.T) { |
| 79 | rig, f := askBase(t) |
| 80 | openPanel(t, rig) |
| 81 | th := thinking{t, rig, f} |
| 82 | if got := th.eval(`document.querySelectorAll("#ask-panel canvas, #ask-panel .thinking").length + ""`); got != "0" { |
| 83 | t.Fatalf("idle, the chat holds %s canvases or bubbles", got) |
| 84 | } |
| 85 | id := th.send("What can I do") |
| 86 | drive(t, rig, chromedp.Evaluate[chromedp.Void](countFramesJS), chromedp.Sleep(300*time.Millisecond)) |
| 87 | if got := th.eval(`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `), list = b.parentElement, p = []; |
| 88 | if (list.lastElementChild !== b) p.push("not at the end of the conversation"); |
| 89 | if (document.querySelectorAll("#ask-panel .thinking").length !== 1) p.push(document.querySelectorAll("#ask-panel .thinking").length + " bubbles"); |
| 90 | if (b.textContent !== "") p.push("says " + JSON.stringify(b.textContent)); |
| 91 | const d = b.querySelectorAll(":scope > .think-line > .dots > i"); |
| 92 | if (d.length !== 3) p.push(d.length + " dots"); |
| 93 | if (b.querySelector(".dots")?.getAttribute("aria-label") !== "Thinking…") p.push("the dots are not named for assistive tech"); |
| 94 | for (const i of d) { |
| 95 | const s = getComputedStyle(i); |
| 96 | if (s.animationName !== "think-dot, think-hue") p.push("a dot is still: " + s.animationName); |
| 97 | if (i.getBoundingClientRect().width < 4) p.push("a dot is " + i.getBoundingClientRect().width + "px"); |
| 98 | } |
| 99 | if (d.length === 3 && getComputedStyle(d[0]).animationDelay === getComputedStyle(d[2]).animationDelay) p.push("the dots move together"); |
| 100 | if (b.getBoundingClientRect().left - list.getBoundingClientRect().left > 20) p.push("not on the Assistant's side"); |
| 101 | if (b.classList.contains("no-gl")) p.push("fell back to the CSS sweep"); |
| 102 | const c = b.querySelector("canvas"); |
| 103 | if (!c) return p.concat("no spark").join("; "); |
| 104 | const dpr = Math.min(devicePixelRatio || 1, 2); |
| 105 | if (Math.abs(c.width - Math.round((b.offsetWidth + 16) * dpr)) > 1 || Math.abs(c.height - Math.round((b.offsetHeight + 16) * dpr)) > 1) |
| 106 | p.push("canvas " + c.width + "x" + c.height + " for a bubble " + b.offsetWidth + "x" + b.offsetHeight + " at " + dpr + "x"); |
| 107 | if (getComputedStyle(c).pointerEvents !== "none") p.push("the canvas takes the pointer"); |
| 108 | if (c.getAttribute("aria-hidden") !== "true") p.push("the canvas is not hidden from assistive tech"); |
| 109 | if (!window.__raf) p.push("no frames: the spark is not running"); |
| 110 | return p.join("; "); })()`); got != "" { |
| 111 | t.Errorf("thinking: %s", got) |
| 112 | } |
| 113 | if got := th.eval(statusJS); got != `"" empty` { |
| 114 | t.Errorf("thinking, the status line above the field is %s", got) |
| 115 | } |
| 116 | rig.Screen("#ask-panel", "the Assistant's thinking bubble") |
| 117 | |
| 118 | // Equipo's activity line shows in the bubble, not above the field. |
| 119 | f.Emit("activity", map[string]string{"turn_id": id, "label": "Looking at what I can do…"}) |
| 120 | th.wait(`document.querySelector(` + strconv.Quote(thinkingSel) + `)?.textContent === "Looking at what I can do…"`) |
| 121 | if got := th.eval(statusJS); got != `"" empty` { |
| 122 | t.Errorf("with an activity line, the status line is %s", got) |
| 123 | } |
| 124 | if n := th.eval(`document.querySelectorAll("#ask-panel .thinking").length + ""`); n != "1" { |
| 125 | t.Errorf("with an activity line, %s bubbles", n) |
| 126 | } |
| 127 | |
| 128 | // The reply, whole: the bubble becomes it, where it is. |
| 129 | same := func(text string) { |
| 130 | t.Helper() |
| 131 | th.wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id && m.textContent === ` + strconv.Quote(text) + `) && |
| 132 | !document.querySelector("#ask-panel .think-line")`) |
| 133 | if got := th.eval(`(() => { const b = window.__b, p = [], r = b.getBoundingClientRect(); |
| 134 | const all = [...document.querySelectorAll("#ask-panel .msg.coordinator")].filter(m => m.textContent === ` + strconv.Quote(text) + `); |
| 135 | if (all.length !== 1) p.push(all.length + " messages say it"); |
| 136 | if (all[0] !== b) p.push("drawn as a new element"); |
| 137 | if (!b.isConnected || [...b.parentElement.children].indexOf(b) !== window.__at) p.push("moved from " + window.__at); |
| 138 | if (Math.abs(r.left - window.__r.left) > 0.5 || (Math.abs(r.top - window.__r.top) > 1 && Math.abs(r.bottom - window.__r.bottom) > 1)) |
| 139 | p.push("jumped from " + JSON.stringify(window.__r) + " to " + JSON.stringify(r)); |
| 140 | if (b.matches(".thinking, .draft") || b.querySelector(".think-line")) p.push("still thinking: " + b.outerHTML); |
| 141 | if (document.querySelector("#ask-panel .thinking")) p.push("a bubble still shows"); |
| 142 | return p.join("; "); })()`); got != "" { |
| 143 | t.Errorf("the reply %q: %s", text, got) |
| 144 | } |
| 145 | // The spark fades and its loop ends. |
| 146 | th.wait(`!document.querySelector("#ask-panel canvas")`) |
| 147 | drive(t, rig, chromedp.Evaluate[chromedp.Void](countFramesJS), chromedp.Sleep(300*time.Millisecond)) |
| 148 | if n := harness.Evaluate[int](rig, `window.__raf`); n != 0 { |
| 149 | t.Errorf("answered, the page asked for %d frames", n) |
| 150 | } |
| 151 | if got := th.eval(statusJS); got != `"" empty` { |
| 152 | t.Errorf("answered, the status line is %s", got) |
| 153 | } |
| 154 | } |
| 155 | mark := chromedp.Evaluate[chromedp.Void](`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `); |
| 156 | window.__b = b; window.__at = [...b.parentElement.children].indexOf(b); window.__r = b.getBoundingClientRect(); })()`) |
| 157 | drive(t, rig, mark) |
| 158 | f.Answer(id, "Here's what I can do.", false) |
| 159 | same("Here's what I can do.") |
| 160 | |
| 161 | // Streamed: the words fill the bubble, which then becomes the reply. |
| 162 | id = th.send("And what else?") |
| 163 | drive(t, rig, mark) |
| 164 | f.Answer(id, "Plan work, check on a project, or start a task.", true) |
| 165 | same("Plan work, check on a project, or start a task.") |
| 166 | |
| 167 | // Scripts off, the status line says it, as before; back on, the bubble |
| 168 | // is drawn again. |
| 169 | said := th.eval(`document.querySelectorAll("#ask-panel .msg.coordinator").length + ""`) |
| 170 | id = th.send("Build me a site") |
| 171 | drive(t, rig, scriptsDisabled(true), chromedp.Navigate(rig.Origin+"/ask"), chromedp.WaitVisible(chromedp.CSS(`.ask-page textarea`))) |
| 172 | if got := harness.Evaluate[string](rig, `(() => { const s = document.querySelector(".ask-page .ask-status"); |
| 173 | return s.textContent + " " + s.checkVisibility() + " " + document.querySelectorAll(".thinking").length; })()`); got != "Thinking… true 0" { |
| 174 | t.Errorf("scripts off, while answering: %s, want the status line and no bubble", got) |
| 175 | } |
| 176 | drive(t, rig, scriptsDisabled(false)) |
| 177 | openPanel(t, rig) |
| 178 | th.wait(`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `); return b && b.textContent === "" && b.querySelectorAll(".dots > i").length === 3; })()`) |
| 179 | |
| 180 | // Stop takes the bubble away, and its spark with it. |
| 181 | drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| 182 | th.wait(`!document.querySelector("#ask-panel .thinking") && document.querySelector("#ask-panel form.composer button.stop").hidden`) |
| 183 | if got := th.eval(`document.querySelectorAll("#ask-panel canvas").length + " " + document.querySelectorAll("#ask-panel .msg.coordinator").length`); got != "0 "+said { |
| 184 | t.Errorf("stopped: canvases and Assistant lines %s, want 0 %s", got, said) |
| 185 | } |
| 186 | if got := th.eval(statusJS); got != `"" empty` { |
| 187 | t.Errorf("stopped, the status line is %s", got) |
| 188 | } |
| 189 | if f.Turn(id) != "cancelled" { |
| 190 | t.Errorf("Stop left the turn %s", f.Turn(id)) |
| 191 | } |
| 192 | |
| 193 | // A failure takes it away too, and says so where it always has. |
| 194 | id = th.send("Try this") |
| 195 | f.FailTurn(id) |
| 196 | th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| 197 | if got := th.eval(statusJS); got != `"That didn't get an answer. Try again." shown` { |
| 198 | t.Errorf("failed, the status line is %s", got) |
| 199 | } |
| 200 | if n := th.eval(`document.querySelectorAll("#ask-panel canvas").length + ""`); n != "0" { |
| 201 | t.Errorf("failed, %s canvases", n) |
| 202 | } |
| 203 | // Failing after some words have streamed in takes them away too. |
| 204 | id = th.send("Try this again") |
| 205 | f.Stream(id, "Half an answer that never") |
| 206 | th.wait(`document.querySelector("#ask-panel .msg.draft")?.textContent === "Half an answer that never"`) |
| 207 | f.FailTurn(id) |
| 208 | th.wait(`document.querySelector("#ask-panel form.composer button.stop").hidden`) |
| 209 | drive(t, rig, chromedp.Sleep(200*time.Millisecond)) |
| 210 | if got := th.eval(`document.querySelectorAll("#ask-panel .thinking, #ask-panel .draft, #ask-panel canvas").length + " " + |
| 211 | [...document.querySelectorAll("#ask-panel .msg.coordinator")].filter(m => m.textContent.includes("Half an answer")).length`); got != "0 0" { |
| 212 | t.Errorf("failed while streaming: bubbles, drafts and canvases, then lines with its words %s, want 0 0", got) |
| 213 | } |
| 214 | if got := th.eval(statusJS); got != `"That didn't get an answer. Try again." shown` { |
| 215 | t.Errorf("failed while streaming, the status line is %s", got) |
| 216 | } |
| 217 | |
| 218 | // A team_update turn is quiet: no bubble. |
| 219 | f.Hold = false |
| 220 | quiet := f.EmitTurn("team_update", "running") |
| 221 | f.Emit("activity", map[string]string{"turn_id": quiet, "label": "Looking at the job"}) |
| 222 | f.Say("Have a look.") |
| 223 | th.wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent === "Have a look.")`) |
| 224 | drive(t, rig, chromedp.Sleep(200*time.Millisecond)) |
| 225 | if got := th.eval(`document.querySelectorAll("#ask-panel .thinking, #ask-panel canvas").length + " " + document.querySelector("#ask-panel form.composer button.stop").hidden`); got != "0 true" { |
| 226 | t.Errorf("with a team_update turn running: bubbles and canvases, Stop hidden %s", got) |
| 227 | } |
| 228 | |
| 229 | // An ordinary send, answered at once: the turn's events (and its first |
| 230 | // words) can come before the send's own answer, which carries the turn |
| 231 | // as it was queued. Once the reply is in, nothing is left thinking. |
| 232 | drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What can I do\r")) |
| 233 | th.wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id && m.textContent.includes("What would you like to do?"))`) |
| 234 | drive(t, rig, chromedp.Sleep(800*time.Millisecond)) |
| 235 | if got := th.eval(`document.querySelectorAll("#ask-panel .thinking, #ask-panel .draft, #ask-panel .think-line, #ask-panel canvas").length + " " + |
| 236 | document.querySelector("#ask-panel form.composer button.stop").hidden + " " + JSON.stringify(document.querySelector("#ask-panel .ask-status").textContent)`); got != `0 true ""` { |
| 237 | t.Errorf("answered at once: bubbles, drafts and canvases, Stop hidden, status %s", got) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // With reduced motion the bubble glows, still, and its dots hold still in |
| 242 | // teal, violet and rose: no spark, no frames. Without |
| 243 | // WebGL, or when its context is lost, a CSS sweep runs round the edge |
| 244 | // instead. |
| 245 | func TestAskThinkingStillAndFallback(t *testing.T) { |
| 246 | rig, f := askBase(t) |
| 247 | t.Run("reduced motion", func(t *testing.T) { |
| 248 | th := thinking{t, rig, f} |
| 249 | drive(t, rig, reducedMotion("dark", true)) |
| 250 | defer drive(t, rig, reducedMotion("dark", false)) |
| 251 | openPanel(t, rig) |
| 252 | th.send("What can I do") |
| 253 | drive(t, rig, chromedp.Evaluate[chromedp.Void](countFramesJS), chromedp.Sleep(400*time.Millisecond)) |
| 254 | if got := th.eval(`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `), s = getComputedStyle(b, "::before"), p = []; |
| 255 | if (b.querySelector("canvas")) p.push("a spark canvas"); |
| 256 | if (window.__raf) p.push(window.__raf + " frames"); |
| 257 | if (s.animationName !== "none") p.push("the edge moves: " + s.animationName); |
| 258 | if (!(Number(s.opacity) > 0)) p.push("no glow on the edge"); |
| 259 | const d = [...b.querySelectorAll(".dots > i")]; |
| 260 | if (d.length !== 3) p.push(d.length + " dots"); |
| 261 | for (const i of d) { |
| 262 | const c = getComputedStyle(i); |
| 263 | if (c.animationName !== "none" || i.getAnimations().length) p.push("a dot moves: " + c.animationName); |
| 264 | if (!(Number(c.opacity) > 0.3)) p.push("a dot is faint: " + c.opacity); |
| 265 | } |
| 266 | if (d.length === 3 && getComputedStyle(d[0]).backgroundColor === getComputedStyle(d[2]).backgroundColor) p.push("the still dots are one colour"); |
| 267 | return p.join("; "); })()`); got != "" { |
| 268 | t.Errorf("reduced motion, thinking: %s", got) |
| 269 | } |
| 270 | drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| 271 | th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| 272 | }) |
| 273 | t.Run("no WebGL", func(t *testing.T) { |
| 274 | th := thinking{t, rig, f} |
| 275 | openPanel(t, rig) |
| 276 | drive(t, rig, chromedp.Evaluate[chromedp.Void](noGLJS)) |
| 277 | th.send("What can I do") |
| 278 | th.wait(`document.querySelector(` + strconv.Quote(thinkingSel) + `).matches(".no-gl")`) |
| 279 | if got := th.eval(`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `), s = getComputedStyle(b, "::before"); |
| 280 | return b.classList.contains("no-gl") + " " + !!b.querySelector("canvas") + " " + s.animationName; })()`); got != "true false sweep" { |
| 281 | t.Errorf("without WebGL: fallback, canvas, edge animation %s; want true false sweep", got) |
| 282 | } |
| 283 | rig.Screen("#ask-panel", "the thinking bubble without WebGL") |
| 284 | drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| 285 | th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| 286 | }) |
| 287 | t.Run("context lost", func(t *testing.T) { |
| 288 | th := thinking{t, rig, f} |
| 289 | openPanel(t, rig) |
| 290 | th.send("What can I do") |
| 291 | th.wait(`!!document.querySelector(` + strconv.Quote(thinkingSel+" canvas") + `)`) |
| 292 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector(`+strconv.Quote(thinkingSel+" canvas")+`).getContext("webgl").getExtension("WEBGL_lose_context").loseContext()`)) |
| 293 | th.wait(`(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `); return b.classList.contains("no-gl") && !b.querySelector("canvas"); })()`) |
| 294 | drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| 295 | th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| 296 | }) |
| 297 | } |
| 298 | |
| 299 | // The composer's focus is a thin teal-to-violet edge round the field that |
| 300 | // drifts, over a soft glow that breathes, in place of the outline; no |
| 301 | // spark. With reduced motion both hold still. |
| 302 | func TestAskComposerFocusEdge(t *testing.T) { |
| 303 | rig, f := askBase(t) |
| 304 | th := thinking{t, rig, f} |
| 305 | openPanel(t, rig) |
| 306 | edge := `(() => { const ta = document.querySelector("#ask-text"), s = getComputedStyle(ta), e = getComputedStyle(ta.parentElement, "::after"); |
| 307 | return [document.activeElement === ta, e.opacity, s.outlineStyle, s.boxShadow !== "none", s.animationName, e.animationName, e.animationPlayState, |
| 308 | /conic-gradient/.test(e.backgroundImage), e.getPropertyValue("--ring").trim(), e.top, document.querySelectorAll("#ask-panel form.composer canvas, #ask-panel form.composer .spark").length].join(" "); })()` |
| 309 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").focus()`)) |
| 310 | th.wait(`getComputedStyle(document.querySelector("#ask-text").parentElement, "::after").opacity === "1"`) |
| 311 | if got, want := th.eval(edge), "true 1 none true breathe-glow edge-turn running true 2px -3px 0"; got != want { |
| 312 | t.Errorf("focused: %s, want %s", got, want) |
| 313 | } |
| 314 | rig.Screen("#ask-panel", "the composer's focus edge") |
| 315 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").blur()`)) |
| 316 | th.wait(`getComputedStyle(document.querySelector("#ask-text").parentElement, "::after").opacity === "0"`) |
| 317 | if got := th.eval(`getComputedStyle(document.querySelector("#ask-text")).boxShadow`); got != "none" { |
| 318 | t.Errorf("blurred, the field's glow is %s", got) |
| 319 | } |
| 320 | |
| 321 | drive(t, rig, reducedMotion("dark", true), chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").focus()`)) |
| 322 | defer drive(t, rig, reducedMotion("dark", false)) |
| 323 | th.wait(`getComputedStyle(document.querySelector("#ask-text").parentElement, "::after").opacity === "1"`) |
| 324 | if got, want := th.eval(edge), "true 1 none true none none running true 2px -3px 0"; got != want { |
| 325 | t.Errorf("reduced motion, focused: %s, want %s", got, want) |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // TestAskThinkingShots keeps the thinking bubble and the focus edge at |
| 330 | // 1280 and 390, dark and light, in BASE_BROWSER_ARTIFACTS when it is set. |
| 331 | func TestAskThinkingShots(t *testing.T) { |
| 332 | rig, f := askBase(t) |
| 333 | th := thinking{t, rig, f} |
| 334 | for _, size := range []struct { |
| 335 | name string |
| 336 | view chromedp.Action[chromedp.Void] |
| 337 | }{ |
| 338 | {"1280", chromedp.EmulateViewport(1280, 800)}, |
| 339 | {"390", chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))}, |
| 340 | } { |
| 341 | for _, scheme := range []string{"dark", "light"} { |
| 342 | drive(t, rig, size.view, reducedMotion(scheme, false)) |
| 343 | if size.name == "390" { |
| 344 | drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(`a.base-btn`)), |
| 345 | chromedp.Click(chromedp.CSS(`a.base-btn`)), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)), |
| 346 | chromedp.Poll[chromedp.Void](readJS, chromedp.WithPollingTimeout(5*time.Second)), chromedp.Sleep(500*time.Millisecond)) |
| 347 | } else { |
| 348 | openPanel(t, rig) |
| 349 | } |
| 350 | name := fmt.Sprintf("%s-%s", size.name, scheme) |
| 351 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").focus()`), chromedp.Sleep(700*time.Millisecond)) |
| 352 | viewportShot(t, rig, name+"-focus") |
| 353 | id := th.send("Can you make me a website?") |
| 354 | f.Emit("activity", map[string]string{"turn_id": id, "label": "Looking at what I can do…"}) |
| 355 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`), chromedp.Sleep(1200*time.Millisecond)) |
| 356 | viewportShot(t, rig, name+"-thinking") |
| 357 | drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| 358 | th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Equipo's delta offsets count UTF-8 bytes: a reply with letters beyond |
| 364 | // ASCII streams into the bubble whole, every piece after the first é or ’ |
| 365 | // included, before the reply itself arrives. |
| 366 | func TestAskStreamCountsBytes(t *testing.T) { |
| 367 | rig, f := askBase(t) |
| 368 | openPanel(t, rig) |
| 369 | th := thinking{t, rig, f} |
| 370 | const text = "café ’quoted’ – done" |
| 371 | id := th.send("Say something") |
| 372 | f.Stream(id, text) |
| 373 | drive(t, rig, chromedp.Poll[chromedp.Void](`document.querySelector("#ask-panel .msg.draft")?.textContent === `+strconv.Quote(text), |
| 374 | chromedp.WithPollingTimeout(5*time.Second))) |
| 375 | if got := th.eval(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].filter(m => m.dataset.id).length + " " + document.querySelectorAll("#ask-panel .msg.coordinator").length`); got != "0 1" { |
| 376 | t.Errorf("streamed, before the reply: delivered and drawn Assistant lines %s, want 0 1", got) |
| 377 | } |
| 378 | f.Answer(id, text, false) |
| 379 | th.wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id && m.textContent === ` + strconv.Quote(text) + `)`) |
| 380 | if n := th.eval(`document.querySelectorAll("#ask-panel .msg.coordinator").length + ""`); n != "1" { |
| 381 | t.Errorf("answered: %s Assistant lines, want 1", n) |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | // watchJS keeps the thinking bubble in window.__b, where it is, and from |
| 386 | // now on, frame by frame, the page's times (performance.now) at which g |
| 387 | // first shows in it, its line laid out and not transparent (window.__g), |
| 388 | // and then first starts to go: fading out, or replaced (window.__x). |
| 389 | func watchJS(g string) string { |
| 390 | return `(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `), g = ` + strconv.Quote(g) + `; |
| 391 | window.__b = b; window.__at = [...b.parentElement.children].indexOf(b); window.__r = b.getBoundingClientRect(); window.__g = 0; window.__x = 0; |
| 392 | const look = () => { const l = b.querySelector(":scope > .think-line"), now = performance.now(); |
| 393 | const s = l && getComputedStyle(l), says = b.textContent === g, kid = l && [...l.children].find(c => c.textContent === g); |
| 394 | if (!window.__g && says && kid && s.display !== "none" && Number(s.opacity) > 0 && Number(getComputedStyle(kid).opacity) > 0) window.__g = now; |
| 395 | const fading = l && l.getAnimations({ subtree: true }).some(a => { const k = a.effect.getKeyframes(); |
| 396 | return (a.effect.target === l || a.effect.target === kid) && Number(k[0].opacity) === 1 && Number(k[k.length - 1].opacity) === 0; }); |
| 397 | if (window.__g && !window.__x && (!says || fading)) window.__x = now; |
| 398 | if (!window.__x && b.isConnected) requestAnimationFrame(look); }; |
| 399 | requestAnimationFrame(look); })()` |
| 400 | } |
| 401 | |
| 402 | // inPlaceJS reports how the watched bubble is not the reply text, where it |
| 403 | // was. |
| 404 | func inPlaceJS(text string) string { |
| 405 | return `(() => { const b = window.__b, p = [], r = b.getBoundingClientRect(); |
| 406 | if (!b.isConnected || [...b.parentElement.children].indexOf(b) !== window.__at) p.push("moved from " + window.__at); |
| 407 | if (b.textContent !== ` + strconv.Quote(text) + ` || !b.dataset.id) p.push("says " + JSON.stringify(b.textContent)); |
| 408 | if (Math.abs(r.left - window.__r.left) > 0.5) p.push("jumped from " + window.__r.left + " to " + r.left); |
| 409 | if (document.querySelector("#ask-panel .thinking, #ask-panel .draft")) p.push("still thinking"); |
| 410 | return p.join("; "); })()` |
| 411 | } |
| 412 | |
| 413 | // Equipo's glance, a first line on the turn in flight, takes the dots' |
| 414 | // place in the bubble, fading in, and stays over Equipo's activity lines. |
| 415 | // A reply that comes soon after waits until the glance has shown for 1.5 |
| 416 | // s, then takes its place where it is; a reply that comes later takes it |
| 417 | // at once. A glance after the turn's first words, for a team_update turn, |
| 418 | // or for a turn the page is not answering shows nowhere. Glances are live |
| 419 | // only, as Equipo sends them: no id, and a page that comes back never sees |
| 420 | // one again. |
| 421 | func TestAskGlance(t *testing.T) { |
| 422 | rig, f := askBase(t) |
| 423 | openPanel(t, rig) |
| 424 | th := thinking{t, rig, f} |
| 425 | const glance = "Checking your projects" |
| 426 | |
| 427 | // The glance takes the dots' place, fading in, and stays. |
| 428 | id := th.send("What's on today?") |
| 429 | drive(t, rig, chromedp.Evaluate[chromedp.Void](watchJS(glance))) |
| 430 | f.Glance(id, " "+glance+"\n") |
| 431 | th.wait(`window.__g > 0`) |
| 432 | th.wait(`(() => { const l = document.querySelector(` + strconv.Quote(thinkingSel+" .think-line") + `); |
| 433 | return l && l.getAnimations({ subtree: true }).some(a => a.playState === "running" && a.effect.target.textContent === ` + strconv.Quote(glance) + ` && |
| 434 | Number(a.effect.getKeyframes()[0].opacity) === 0); })()`) |
| 435 | th.wait(`!window.__b.querySelector(":scope > .think-line > .dots")`) // faded out under the glance |
| 436 | if got := th.eval(`(() => { const b = window.__b, p = []; |
| 437 | if (b.querySelector(":scope > .think-line > .dots")) p.push("the wordless dots still show"); |
| 438 | if (!b.matches(".thinking") || !b.querySelector("canvas")) p.push("no longer thinking, with its spark"); |
| 439 | if (document.querySelectorAll("#ask-panel .thinking").length !== 1) p.push(document.querySelectorAll("#ask-panel .thinking").length + " bubbles"); |
| 440 | if (document.querySelector("#ask-panel .ask-status").textContent) p.push("the status line says " + document.querySelector("#ask-panel .ask-status").textContent); |
| 441 | return p.join("; "); })()`); got != "" { |
| 442 | t.Errorf("with a glance: %s", got) |
| 443 | } |
| 444 | rig.Screen("#ask-panel", "the thinking bubble with Equipo's glance") |
| 445 | f.Emit("activity", map[string]string{"turn_id": id, "label": "Looking at what I can do…"}) |
| 446 | |
| 447 | // A reply 200 ms after the glance waits until it has shown 1.5 s, then |
| 448 | // takes its place. |
| 449 | g := harness.Evaluate[float64](rig, `window.__g`) |
| 450 | for harness.Evaluate[float64](rig, `performance.now()`) < g+200 { |
| 451 | time.Sleep(10 * time.Millisecond) |
| 452 | } |
| 453 | const soon = "Two meetings and a review." |
| 454 | f.Answer(id, soon, true) |
| 455 | th.wait(`window.__b.dataset.id && window.__b.textContent === ` + strconv.Quote(soon) + ` && !document.querySelector("#ask-panel .think-line")`) |
| 456 | if held := harness.Evaluate[float64](rig, `window.__x - window.__g`); held < 1480 || held > 2300 { |
| 457 | t.Errorf("a reply 200 ms after the glance showed began to replace it %.0f ms after it showed, want about 1500", held) |
| 458 | } |
| 459 | if got := th.eval(inPlaceJS(soon)); got != "" { |
| 460 | t.Errorf("the reply soon after a glance: %s", got) |
| 461 | } |
| 462 | |
| 463 | // A reply 3 s after the glance takes its place at once. |
| 464 | id = th.send("And tomorrow?") |
| 465 | drive(t, rig, chromedp.Evaluate[chromedp.Void](watchJS(glance))) |
| 466 | f.Glance(id, glance) |
| 467 | th.wait(`window.__g > 0`) |
| 468 | f.Emit("activity", map[string]string{"turn_id": id, "label": "Looking at what I can do…"}) |
| 469 | drive(t, rig, chromedp.Sleep(3*time.Second)) |
| 470 | if got := th.eval(`window.__b.textContent`); got != glance { |
| 471 | t.Errorf("3 s after the glance (an activity line came), the bubble says %q", got) |
| 472 | } |
| 473 | const later = "Nothing yet." |
| 474 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`window.__sent = performance.now()`)) |
| 475 | f.Answer(id, later, false) |
| 476 | th.wait(`window.__b.dataset.id && window.__b.textContent === ` + strconv.Quote(later) + ` && !document.querySelector("#ask-panel .think-line")`) |
| 477 | if took := harness.Evaluate[float64](rig, `window.__x - window.__sent`); took > 700 { |
| 478 | t.Errorf("a reply 3 s after the glance took %.0f ms to replace it", took) |
| 479 | } |
| 480 | if got := th.eval(inPlaceJS(later)); got != "" { |
| 481 | t.Errorf("the reply 3 s after a glance: %s", got) |
| 482 | } |
| 483 | |
| 484 | // Without a glance, an activity line still takes the dots' place. |
| 485 | id = th.send("What else?") |
| 486 | f.Emit("activity", map[string]string{"turn_id": id, "label": "Looking at what I can do…"}) |
| 487 | th.wait(`document.querySelector(` + strconv.Quote(thinkingSel) + `)?.textContent === "Looking at what I can do…"`) |
| 488 | |
| 489 | // A glance after the first words shows nowhere. |
| 490 | f.Stream(id, "Half an answer") |
| 491 | th.wait(`document.querySelector("#ask-panel .msg.draft")?.textContent === "Half an answer"`) |
| 492 | f.Glance(id, "A late glance") |
| 493 | drive(t, rig, chromedp.Sleep(500*time.Millisecond)) |
| 494 | if got := th.eval(`document.querySelector("#ask-panel .msg.draft")?.textContent + " " + document.querySelector("#ask-panel").textContent.includes("A late glance")`); got != "Half an answer false" { |
| 495 | t.Errorf("a glance after the first words: draft and glance shown %s", got) |
| 496 | } |
| 497 | drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| 498 | th.wait(`!document.querySelector("#ask-panel .draft, #ask-panel .thinking") && document.querySelector("#ask-panel form.composer button.stop").hidden`) |
| 499 | |
| 500 | // A glance for a turn the page is not answering shows nowhere, nor for |
| 501 | // a team_update turn, even on a page that has come back since (whose |
| 502 | // stream has not seen the turn start). |
| 503 | id = th.send("One more") |
| 504 | f.Hold = false |
| 505 | quiet := f.EmitTurn("team_update", "running") |
| 506 | f.Glance(quiet, "Checking the job") |
| 507 | drive(t, rig, chromedp.Sleep(300*time.Millisecond)) |
| 508 | openPanel(t, rig) |
| 509 | th.wait(`document.querySelectorAll(` + strconv.Quote(thinkingSel) + `).length === 1`) |
| 510 | f.Glance(quiet, "Checking the job") |
| 511 | f.Glance("t_9999", "Someone else's turn") |
| 512 | drive(t, rig, chromedp.Sleep(500*time.Millisecond)) |
| 513 | if got := th.eval(`(() => { const p = document.querySelector("#ask-panel"), b = document.querySelectorAll(` + strconv.Quote(thinkingSel) + `); |
| 514 | return [p.textContent.includes("Checking the job"), p.textContent.includes("Someone else"), b.length, b[0] && b[0].querySelectorAll(".dots > i").length].join(" "); })()`); got != "false false 1 3" { |
| 515 | t.Errorf("glances for other turns: quiet shown, other shown, bubbles, dots %s; want false false 1 3", got) |
| 516 | } |
| 517 | f.Glance(id, glance) |
| 518 | th.wait(`document.querySelector(` + strconv.Quote(thinkingSel) + `)?.textContent === ` + strconv.Quote(glance)) |
| 519 | |
| 520 | // A glance is live only (no id, never sent again) and never kept: the |
| 521 | // page that comes back shows the dots, and the events after it still |
| 522 | // arrive. |
| 523 | openPanel(t, rig) |
| 524 | th.wait(`document.querySelectorAll(` + strconv.Quote(thinkingSel+" .dots > i") + `).length === 3`) |
| 525 | drive(t, rig, chromedp.Sleep(300*time.Millisecond)) |
| 526 | if got := th.eval(`document.querySelector("#ask-panel").textContent.includes(` + strconv.Quote(glance) + `) + ""`); got != "false" { |
| 527 | t.Errorf("after coming back, the glance shows again") |
| 528 | } |
| 529 | f.Answer(id, "All done.", false) |
| 530 | th.wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id && m.textContent === "All done.") && !document.querySelector("#ask-panel .thinking")`) |
| 531 | } |
| 532 | |
| 533 | // glanceJS reports how the watched bubble's glance (window.__b) does not |
| 534 | // read as a status: secondary ink (want), italic, one line, the three dots |
| 535 | // after its words, hidden from assistive tech, shimmering or (still) not. |
| 536 | func glanceJS(want string, still bool) string { |
| 537 | return `(() => { const b = window.__b, say = b.querySelector(":scope > .think-line > .say"), p = []; |
| 538 | if (!say) return "no status words"; |
| 539 | const w = say.querySelector(":scope > .words"), d = [...say.querySelectorAll(":scope > .dots > i")], s = getComputedStyle(say); |
| 540 | if (s.fontStyle !== "italic" || getComputedStyle(w).fontStyle !== "italic") p.push("upright"); |
| 541 | if (s.color !== ` + strconv.Quote(want) + `) p.push("ink " + s.color); |
| 542 | if (getComputedStyle(w).whiteSpace !== "nowrap") p.push("wraps"); |
| 543 | if (d.length !== 3) p.push(d.length + " dots after the words"); |
| 544 | if (say.querySelector(".dots")?.getAttribute("aria-hidden") !== "true") p.push("the dots are read out"); |
| 545 | if (d.length && d[0].getBoundingClientRect().left < w.getBoundingClientRect().right - 0.5) p.push("the dots are not after the words"); |
| 546 | for (const i of d) { |
| 547 | const c = getComputedStyle(i), moving = c.animationName === "think-dot, think-hue"; |
| 548 | if (moving === ` + strconv.FormatBool(still) + `) p.push("a dot: " + c.animationName); |
| 549 | if (i.getBoundingClientRect().width < 4 || !(Number(c.opacity) > 0.3)) p.push("a dot hardly shows"); |
| 550 | } |
| 551 | return p.join("; "); })()` |
| 552 | } |
| 553 | |
| 554 | // A glance (or an activity line) is a status, not a reply: secondary ink, |
| 555 | // italic, AA on the bubble in dark and light, and the soft dots after it, |
| 556 | // still shimmering, so the Assistant is plainly still working (holding |
| 557 | // still with reduced motion). The reply that takes its place is upright, |
| 558 | // in full ink, with no dots. |
| 559 | func TestAskGlanceReadsAsStatus(t *testing.T) { |
| 560 | rig, f := askBase(t) |
| 561 | th := thinking{t, rig, f} |
| 562 | const glance, reply = "Yo, Paul.", "Here is what I found." |
| 563 | for _, c := range []struct { |
| 564 | scheme string |
| 565 | still bool |
| 566 | haze, frost string |
| 567 | }{ |
| 568 | {"dark", false, "rgb(221, 227, 240)", "rgb(238, 242, 248)"}, |
| 569 | {"light", false, "rgb(74, 82, 117)", "rgb(30, 36, 64)"}, |
| 570 | {"dark", true, "rgb(221, 227, 240)", "rgb(238, 242, 248)"}, |
| 571 | } { |
| 572 | when := c.scheme + map[bool]string{false: "", true: ", reduced motion"}[c.still] |
| 573 | drive(t, rig, chromedp.EmulateViewport(1280, 800), reducedMotion(c.scheme, c.still)) |
| 574 | openPanel(t, rig) |
| 575 | id := th.send("Anything new?") |
| 576 | drive(t, rig, chromedp.Evaluate[chromedp.Void](watchJS(glance))) |
| 577 | f.Glance(id, glance) |
| 578 | th.wait(`window.__g > 0 && !window.__b.querySelector(":scope > .think-line > .dots") && !window.__b.getAnimations({ subtree: true }).some(a => !(a instanceof CSSAnimation))`) |
| 579 | if got := th.eval(glanceJS(c.haze, c.still)); got != "" { |
| 580 | t.Errorf("%s, the glance: %s", when, got) |
| 581 | } |
| 582 | if !c.still { |
| 583 | worst, need, where := textContrast(t, rig, thinkingSel+" .say") |
| 584 | t.Logf("%s: the glance's contrast %.2f (%q)", when, worst, where) |
| 585 | if worst < need { |
| 586 | t.Errorf("%s: the glance %q has contrast %.2f, needs %.1f", when, where, worst, need) |
| 587 | } |
| 588 | rig.Screen("#ask-panel", "the glance as a status, "+when) |
| 589 | } |
| 590 | f.Answer(id, reply, false) |
| 591 | th.wait(`window.__b.dataset.id && window.__b.textContent === ` + strconv.Quote(reply) + ` && !document.querySelector("#ask-panel .think-line") && !window.__b.getAnimations({ subtree: true }).length`) |
| 592 | if got := th.eval(`(() => { const b = window.__b, p = []; |
| 593 | for (const e of [b, ...b.querySelectorAll("*")]) { const s = getComputedStyle(e); |
| 594 | if (s.fontStyle !== "normal") p.push(e.tagName + " " + s.fontStyle); |
| 595 | if (s.color !== ` + strconv.Quote(c.frost) + `) p.push(e.tagName + " ink " + s.color); } |
| 596 | if (b.querySelector(".dots, .say")) p.push("status left in it: " + b.innerHTML); |
| 597 | return p.join("; "); })()`); got != "" { |
| 598 | t.Errorf("%s, the reply: %s", when, got) |
| 599 | } |
| 600 | } |
| 601 | drive(t, rig, reducedMotion("dark", false)) |
| 602 | } |
| 603 | |
| 604 | // steadyJS watches the thinking bubble frame by frame from now on, until |
| 605 | // window.__stop: in window.__steady, every frame's problem (no words or |
| 606 | // dots in view, the line's text empty once words have come, the bubble's |
| 607 | // height changing, its width shrinking), and in window.__frames how many |
| 608 | // frames it saw, and in window.__anims how many animations were started |
| 609 | // in it. |
| 610 | const steadyJS = `(() => { const b = document.querySelector("#ask-panel .thread > li.msg.coordinator.thinking"), l = b.querySelector(":scope > .think-line"); |
| 611 | const h0 = b.getBoundingClientRect().height; let w = b.getBoundingClientRect().width, worded = false; |
| 612 | window.__steady = []; window.__frames = 0; window.__stop = false; window.__anims = 0; |
| 613 | if (!window.__animWrapped) { const animate = Element.prototype.animate; window.__animWrapped = true; |
| 614 | Element.prototype.animate = function (...args) { if (this.closest && this.closest(".thinking")) window.__anims++; return animate.apply(this, args); }; } |
| 615 | const look = () => { const r = b.getBoundingClientRect(), p = []; |
| 616 | window.__frames++; |
| 617 | if (![...l.children].some(c => Number(getComputedStyle(c).opacity) > 0)) p.push("nothing in view"); |
| 618 | if (l.textContent) worded = true; else if (worded) p.push("the line is empty"); |
| 619 | if (Math.abs(r.height - h0) > 0.5) p.push("height " + r.height + ", was " + h0); |
| 620 | if (r.width < w - 0.5) p.push("width " + r.width + ", was " + w); |
| 621 | w = Math.max(w, r.width); |
| 622 | if (p.length && window.__steady.length < 20) window.__steady.push(p.join(", ")); |
| 623 | if (!window.__stop && b.isConnected) requestAnimationFrame(look); }; |
| 624 | requestAnimationFrame(look); })()` |
| 625 | |
| 626 | // While the Assistant thinks, its bubble holds steady as its line changes |
| 627 | // (dots, then one activity line after another, long and short): the new |
| 628 | // words fade in over the old as they fade out, never a blank moment; the |
| 629 | // line stays on one line (a long one ends in an ellipsis); the bubble's |
| 630 | // height holds and its width only grows, easing. With reduced motion the |
| 631 | // swaps are instant, by the same size rules. |
| 632 | func TestAskThinkingHoldsSteady(t *testing.T) { |
| 633 | rig, f := askBase(t) |
| 634 | long := "Reading through every one of your projects, their open tasks, recent changes and the notes your team left on each of them"[:120] |
| 635 | lines := []string{"Looking at what I can do…", "Checking", long, "Reading your recent projects and what changed in them…"} |
| 636 | for _, still := range []bool{false, true} { |
| 637 | name := map[bool]string{false: "moving", true: "reduced motion"}[still] |
| 638 | t.Run(name, func(t *testing.T) { |
| 639 | th := thinking{t, rig, f} |
| 640 | drive(t, rig, reducedMotion("dark", still)) |
| 641 | defer drive(t, rig, reducedMotion("dark", false)) |
| 642 | openPanel(t, rig) |
| 643 | id := th.send("What's going on?") |
| 644 | drive(t, rig, chromedp.Sleep(300*time.Millisecond), chromedp.Evaluate[chromedp.Void](steadyJS)) |
| 645 | for i, line := range lines { |
| 646 | f.Emit("activity", map[string]string{"turn_id": id, "label": line}) |
| 647 | if i == 1 { |
| 648 | // One comes hard on the last's heels, mid-crossfade. |
| 649 | drive(t, rig, chromedp.Sleep(100*time.Millisecond)) |
| 650 | continue |
| 651 | } |
| 652 | th.wait(`document.querySelector(` + strconv.Quote(thinkingSel+" .think-line") + `).textContent === ` + strconv.Quote(line)) |
| 653 | if line == long { |
| 654 | if got := th.eval(`(() => { const l = document.querySelector(` + strconv.Quote(thinkingSel+" .think-line") + `), c = l.lastElementChild.querySelector(".words"), s = getComputedStyle(c); |
| 655 | const d = l.lastElementChild.querySelector(".dots").getBoundingClientRect(), b = l.parentElement.getBoundingClientRect(); |
| 656 | return [c.getClientRects().length, Math.round(c.getBoundingClientRect().height) <= Math.ceil(parseFloat(s.lineHeight)), c.scrollWidth > c.clientWidth, s.textOverflow, |
| 657 | d.width > 0 && d.left >= c.getBoundingClientRect().right - 0.5 && d.right <= b.right].join(" "); })()`); got != "1 true true ellipsis true" { |
| 658 | t.Errorf("the 120-character line: boxes, one line high, clipped, overflow, dots in view after it %s; want 1 true true ellipsis true", got) |
| 659 | } |
| 660 | } |
| 661 | drive(t, rig, chromedp.Sleep(400*time.Millisecond)) |
| 662 | } |
| 663 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`window.__stop = true`)) |
| 664 | if got := th.eval(`JSON.stringify(window.__steady)`); got != "[]" { |
| 665 | t.Errorf("thinking through %d lines: %s", len(lines), got) |
| 666 | } |
| 667 | if n := harness.Evaluate[int](rig, `window.__frames`); n < 20 { |
| 668 | t.Errorf("watched only %d frames", n) |
| 669 | } |
| 670 | if n := harness.Evaluate[int](rig, `window.__anims`); (n == 0) != still { |
| 671 | t.Errorf("reduced motion %v: %d animations in the bubble", still, n) |
| 672 | } |
| 673 | if !still { |
| 674 | rig.Screen("#ask-panel", "the thinking bubble after a long activity line") |
| 675 | } |
| 676 | drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| 677 | th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| 678 | }) |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | // A streamed reply shows formatted as it arrives: bold, a link and a list |
| 683 | // item split across deltas never show a "**", "[" or "](" mid-stream (an |
| 684 | // opener with no closer yet is plain words, a link on its way shows its |
| 685 | // words), and the reply that settles is drawn as the same reply that |
| 686 | // came whole. |
| 687 | func TestAskStreamFormatsAsItArrives(t *testing.T) { |
| 688 | rig, f := askBase(t) |
| 689 | openPanel(t, rig) |
| 690 | th := thinking{t, rig, f} |
| 691 | const text = "Here is **bold text** and a [guide page](https://example.com/guide) too.\n- first *item* here\n- second [more](https://example.com/more)\n" |
| 692 | parts := []string{"Here is *", "*bo", "ld te", "xt**", " and a [gu", "ide page", "](http", "s://example.com/gu", "ide) too.\n-", " first *it", "em* here\n", "- sec", "ond [more]", "(https://example.com/more)", "\n"} |
| 693 | if strings.Join(parts, "") != text { |
| 694 | t.Fatal("the test's pieces are not its text") |
| 695 | } |
| 696 | id := th.send("Say it formatted") |
| 697 | th.eval(`(() => { window.__seen = []; window.__bad = []; |
| 698 | const look = () => { for (const d of document.querySelectorAll("#ask-panel .msg.draft")) { |
| 699 | const t = d.textContent; window.__seen.push(d.innerHTML); |
| 700 | if (/\*\*|\[|\]\(/.test(t)) window.__bad.push(t); } }; |
| 701 | new MutationObserver(look).observe(document.querySelector("#ask-panel"), { childList: true, subtree: true, characterData: true }); |
| 702 | return "ok"; })()`) |
| 703 | f.StreamParts(id, parts) |
| 704 | th.wait(`document.querySelector("#ask-panel .msg.draft strong") && document.querySelector("#ask-panel .msg.draft li:last-child a[href$=\"/more\"]")`) |
| 705 | if got := th.eval(`JSON.stringify(window.__bad)`); got != "[]" { |
| 706 | t.Errorf("markup showed while streaming: %s", got) |
| 707 | } |
| 708 | if got := th.eval(`window.__seen.length + ""`); got == "0" { |
| 709 | t.Error("nothing was drawn as it streamed") |
| 710 | } |
| 711 | last := th.eval(`(document.querySelector("#ask-panel .msg.draft > .think-line") || document.querySelector("#ask-panel .msg.draft")).innerHTML`) |
| 712 | f.Answer(id, text, false) |
| 713 | th.wait(`document.querySelector("#ask-panel .msg.coordinator[data-id] strong")`) |
| 714 | streamed := th.eval(`(() => { const m = [...document.querySelectorAll("#ask-panel .msg.coordinator")].find(m => m.dataset.id && m.querySelector("strong")); return (m.querySelector(":scope > .think-line") || m).innerHTML })()`) |
| 715 | if streamed != last { |
| 716 | t.Errorf("the settled reply differs from the last draft:\n%s\n%s", streamed, last) |
| 717 | } |
| 718 | // The same reply that was never streamed. |
| 719 | id2 := th.send("Say it again") |
| 720 | f.Answer(id2, text, false) |
| 721 | th.wait(`document.querySelectorAll("#ask-panel .msg.coordinator[data-id] strong").length === 2`) |
| 722 | whole := th.eval(`(() => { const m = [...document.querySelectorAll("#ask-panel .msg.coordinator")].filter(m => m.dataset.id && m.querySelector("strong"))[1]; return (m.querySelector(":scope > .think-line") || m).innerHTML })()`) |
| 723 | if streamed != whole { |
| 724 | t.Errorf("streamed and whole replies differ:\n%s\n%s", streamed, whole) |
| 725 | } |
| 726 | if !strings.Contains(whole, "<strong>bold text</strong>") || !strings.Contains(whole, `href="https://example.com/guide"`) || strings.Count(whole, "<li>") != 2 { |
| 727 | t.Errorf("the reply is not formatted: %s", whole) |
| 728 | } |
| 729 | } |
| 730 | |