oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package browsertest
4
5import (
6 "fmt"
7 "strings"
8 "testing"
9 "time"
10
11 "github.com/chromedp/chromedp"
12
13 "amadan.net/rastrillo/rastrillo/harness"
14)
15
16// lastVisible is "" when the conversation's last bubble is wholly inside
17// the log, above the status row and the composer, and the log is at its
18// bottom; else what is wrong.
19const lastVisible = `(() => {
20 const p = document.querySelector("#ask-panel"), log = p.querySelector(".thread");
21 const items = log.querySelectorAll(":scope > li"), last = items[items.length - 1];
22 if (!last) return "no bubbles";
23 const l = log.getBoundingClientRect(), b = last.getBoundingClientRect();
24 const foot = p.querySelector(".ask-foot").getBoundingClientRect(), comp = p.querySelector("form.composer").getBoundingClientRect();
25 const out = [];
26 if (b.bottom > l.bottom + 1) out.push("last bubble ends at " + Math.round(b.bottom) + ", below the log's " + Math.round(l.bottom));
27 if (b.bottom > foot.top + 1 && foot.height > 0) out.push("last bubble under the status row");
28 if (l.bottom > comp.top + 1) out.push("log runs under the composer");
29 if (foot.height > 0 && l.bottom > foot.top + 1) out.push("log runs under the status row");
30 if (log.scrollHeight - log.scrollTop - log.clientHeight > 2) out.push("log not at its bottom");
31 return out.join("; ");
32})()`
33
34// The conversation follows its newest line docked and on a phone:
35// after sending it is at the bottom, with the last bubble wholly above the
36// status row; streaming keeps it there; scrolled up, it stays put and
37// offers Jump to latest.
38func TestAskScrollFollowsNewest(t *testing.T) {
39 for _, size := range []struct {
40 name string
41 w, h int64
42 }{{"docked", 1280, 800}, {"phone", 390, 844}} {
43 t.Run(size.name, func(t *testing.T) {
44 var view chromedp.Action[chromedp.Void]
45 if size.name == "phone" {
46 view = chromedp.EmulateViewport(size.w, size.h, chromedp.EmulateMobile, chromedp.EmulateScale(3))
47 }
48 rig, f, _ := askBaseOn(t, nil, view)
49 f.Reply = func(string) string {
50 return strings.Repeat("Here is a long answer that streams in word by word. ", 12)
51 }
52 for i := 1; i <= 30; i++ {
53 f.Say(fmt.Sprintf("Earlier line %d of a long conversation.", i))
54 }
55 if size.name == "phone" {
56 drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(`a.base-btn`)),
57 chromedp.Click(chromedp.CSS(`a.base-btn`)), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)),
58 chromedp.Poll[chromedp.Void](readJS, chromedp.WithPollingTimeout(5*time.Second)), chromedp.Sleep(100*time.Millisecond))
59 } else {
60 openPanel(t, rig)
61 }
62 wait := func(js string) {
63 t.Helper()
64 drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second)))
65 }
66 wait(`document.querySelectorAll("#ask-panel .thread > li").length >= 30`)
67 if got := harness.Evaluate[string](rig, lastVisible); got != "" {
68 t.Errorf("on opening: %s", got)
69 }
70
71 // Send while the coordinator has not started: the Assistant's
72 // thinking bubble, under the person's, is wholly above Stop.
73 send := func(text string) {
74 t.Helper()
75 // Enter sends, as a person does on a keyboard; scrolled to the
76 // top first, so sending must bring the log back down.
77 drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-panel .thread").scrollTop = 0`),
78 chromedp.SendKeys(chromedp.CSS(`#ask-text`), text+"\r"))
79 }
80 f.Hold = true
81 send("Can we fix a bug in Tito?")
82 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 {
83 t.Fatalf("no thinking bubble: %v; person bubbles %d, status %q, field %q", err,
84 harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .msg.person").length`),
85 harness.Evaluate[string](rig, `document.querySelector("#ask-panel .ask-status").textContent`),
86 harness.Evaluate[string](rig, `document.querySelector("#ask-text").value`))
87 }
88 drive(t, rig, chromedp.Sleep(200*time.Millisecond))
89 if got := harness.Evaluate[string](rig, lastVisible); got != "" {
90 t.Errorf("sent, waiting for an answer: %s", got)
91 }
92 drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`)))
93 wait(`document.querySelector("#ask-panel form.composer button.stop").hidden`)
94 f.Hold = false
95
96 // Send: the log goes to the bottom, and stays there as the reply
97 // streams into the thinking bubble.
98 send("What can I do")
99 wait(`(() => { const d = document.querySelector("#ask-panel .msg.draft"); return d && d.textContent.length > 120 })()`)
100 if got := harness.Evaluate[string](rig, lastVisible); got != "" {
101 t.Errorf("while streaming: %s", got)
102 }
103 wait(`!document.querySelector("#ask-panel .msg.draft") && document.querySelector("#ask-panel .ask-status").textContent === ""`)
104 if got := harness.Evaluate[string](rig, lastVisible); got != "" {
105 t.Errorf("after the reply: %s", got)
106 }
107
108 // Scrolled up to read: a new line does not move the log.
109 drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-panel .thread").scrollTop = 100`),
110 chromedp.Sleep(200*time.Millisecond)) // the scroll event, a frame later
111 f.Say("A new line while you read.")
112 wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent === "A new line while you read.")`)
113 if got := harness.Evaluate[int](rig, `Math.round(document.querySelector("#ask-panel .thread").scrollTop)`); got != 100 {
114 t.Errorf("reading earlier lines, the log moved to %d", got)
115 }
116 jump := `#ask-panel button.jump`
117 wait(`!document.querySelector("#ask-panel button.jump").hidden`)
118 if got := harness.Evaluate[string](rig, `document.querySelector("`+jump+`").textContent`); got != "Jump to latest" {
119 t.Errorf("jump control reads %q", got)
120 }
121 drive(t, rig, chromedp.Click(chromedp.CSS(jump)))
122 wait(`document.querySelector("#ask-panel button.jump").hidden`)
123 if got := harness.Evaluate[string](rig, lastVisible); got != "" {
124 t.Errorf("after Jump to latest: %s", got)
125 }
126 })
127 }
128}
129