| 1 | //go:build browser |
| 2 | |
| 3 | package browsertest |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/chromedp/chromedp" |
| 14 | |
| 15 | "amadan.net/rastrillo/rastrillo/harness" |
| 16 | ) |
| 17 | |
| 18 | // pasteSample is one of testdata/paste.json's clipboards: its HTML and the |
| 19 | // Markdown it pastes as (null: the plain text, left to the browser). The |
| 20 | // app's PasteMarkdownTests check the same samples. |
| 21 | type pasteSample struct { |
| 22 | Name string `json:"name"` |
| 23 | HTML string `json:"html"` // "" for the app's RTF samples |
| 24 | Markdown *string `json:"markdown"` |
| 25 | } |
| 26 | |
| 27 | // A rich paste into the composer becomes the Markdown the chat draws (Paul, |
| 28 | // 2026-11-26), and Undo takes it back; a paste whose HTML formats nothing |
| 29 | // is the browser's own. |
| 30 | func TestAskPasteMarkdown(t *testing.T) { |
| 31 | raw, err := os.ReadFile("testdata/paste.json") |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | var samples []pasteSample |
| 36 | if err := json.Unmarshal(raw, &samples); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | rig, _ := askBase(t) |
| 40 | openPanel(t, rig) |
| 41 | type result struct { |
| 42 | Prevented bool `json:"prevented"` |
| 43 | Value string `json:"value"` |
| 44 | } |
| 45 | paste := func(before, html string) result { |
| 46 | t.Helper() |
| 47 | js := `(() => { |
| 48 | const f = document.querySelector("#ask-text"); |
| 49 | f.focus(); f.value = ` + strconv.Quote(before) + `; f.setSelectionRange(f.value.length, f.value.length); |
| 50 | const data = new DataTransfer(); |
| 51 | data.setData("text/html", ` + strconv.Quote(html) + `); |
| 52 | data.setData("text/plain", "plain"); |
| 53 | const e = new ClipboardEvent("paste", { clipboardData: data, bubbles: true, cancelable: true }); |
| 54 | f.dispatchEvent(e); |
| 55 | return JSON.stringify({ prevented: e.defaultPrevented, value: f.value }); |
| 56 | })()` |
| 57 | var r result |
| 58 | if err := json.Unmarshal([]byte(harness.Evaluate[string](rig, js)), &r); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | return r |
| 62 | } |
| 63 | for _, s := range samples { |
| 64 | if s.HTML == "" { |
| 65 | continue |
| 66 | } |
| 67 | got := paste("", s.HTML) |
| 68 | switch { |
| 69 | case s.Markdown == nil && (got.Prevented || got.Value != ""): |
| 70 | t.Errorf("%s: pasted %q, want the browser's own paste", s.Name, got.Value) |
| 71 | case s.Markdown != nil && (!got.Prevented || got.Value != *s.Markdown): |
| 72 | t.Errorf("%s:\n got %q\nwant %q", s.Name, got.Value, *s.Markdown) |
| 73 | } |
| 74 | } |
| 75 | // At the cursor, after what is there; and Undo takes the paste back. |
| 76 | if got := paste("Note: ", samples[0].HTML); got.Value != "Note: "+*samples[0].Markdown { |
| 77 | t.Errorf("pasted after text: %q", got.Value) |
| 78 | } |
| 79 | if got := harness.Evaluate[string](rig, `(document.execCommand("undo"), document.querySelector("#ask-text").value)`); got != "Note: " { |
| 80 | t.Errorf("after Undo: %q", got) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Fable's S5 and S6: a big or hostile paste never freezes the tab. The |
| 85 | // walk is linear and stops once the Markdown passes the field's 16,000 |
| 86 | // characters; nesting past 32 levels is flattened. |
| 87 | func TestAskPasteIsBounded(t *testing.T) { |
| 88 | rig, _ := askBase(t) |
| 89 | openPanel(t, rig) |
| 90 | // The whole paste, the field's own insert included. |
| 91 | got := harness.Evaluate[string](rig, `(() => { |
| 92 | const f = document.querySelector("#ask-text"); |
| 93 | const run = (html) => { |
| 94 | f.focus(); f.value = ""; |
| 95 | const data = new DataTransfer(); |
| 96 | data.setData("text/html", html); |
| 97 | const e = new ClipboardEvent("paste", { clipboardData: data, bubbles: true, cancelable: true }); |
| 98 | const t0 = performance.now(); |
| 99 | f.dispatchEvent(e); |
| 100 | return { ms: performance.now() - t0, value: f.value }; |
| 101 | }; |
| 102 | const out = []; |
| 103 | { |
| 104 | const items = run("<ul>" + "<li><b>item</b></li>".repeat(50000) + "</ul>"); |
| 105 | if (items.ms > 200) out.push("50,000 items took " + Math.round(items.ms) + " ms"); |
| 106 | if (!items.value.startsWith("- **item**\n- **item**") || items.value.length > 16100) out.push("items pasted " + items.value.length + " characters"); |
| 107 | const deep = run("<div><b>".repeat(5000) + "deep" + "</b></div>".repeat(5000)); |
| 108 | if (deep.ms > 500) out.push("5,000 levels took " + Math.round(deep.ms) + " ms"); |
| 109 | if (deep.value !== "**deep**") out.push("deep pasted " + JSON.stringify(deep.value.slice(0, 40))); |
| 110 | } |
| 111 | return out.join("; "); |
| 112 | })()`) |
| 113 | if got != "" { |
| 114 | t.Error(got) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Fable's R1: the whole paste of about 4,000 lines, the field's own |
| 119 | // insert included (nothing stubbed), is quick, and the field holds all of |
| 120 | // it, with the panel's listeners told. |
| 121 | func TestAskLongPasteIsQuick(t *testing.T) { |
| 122 | rig, _ := askBase(t) |
| 123 | openPanel(t, rig) |
| 124 | var r struct { |
| 125 | MS float64 `json:"ms"` |
| 126 | Value string `json:"value"` |
| 127 | Input bool `json:"input"` |
| 128 | } |
| 129 | raw := harness.Evaluate[string](rig, `(() => { |
| 130 | const f = document.querySelector("#ask-text"); |
| 131 | f.focus(); f.value = ""; |
| 132 | let input = false; |
| 133 | f.addEventListener("input", () => { input = true; }, { once: true }); |
| 134 | const data = new DataTransfer(); |
| 135 | data.setData("text/html", "<p><b>Rows</b></p>" + Array.from({ length: 4000 }, (_, i) => "<div>r" + (i % 10) + "</div>").join("")); |
| 136 | const e = new ClipboardEvent("paste", { clipboardData: data, bubbles: true, cancelable: true }); |
| 137 | const t0 = performance.now(); |
| 138 | f.dispatchEvent(e); |
| 139 | return JSON.stringify({ ms: performance.now() - t0, value: f.value, input }); |
| 140 | })()`) |
| 141 | if err := json.Unmarshal([]byte(raw), &r); err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | var want strings.Builder |
| 145 | want.WriteString("**Rows**\n") |
| 146 | for i := range 4000 { |
| 147 | want.WriteString("\nr" + strconv.Itoa(i%10)) |
| 148 | } |
| 149 | if r.Value != want.String() { |
| 150 | t.Errorf("pasted %d characters, want %d", len(r.Value), want.Len()) |
| 151 | } |
| 152 | if r.MS > 500 { |
| 153 | t.Errorf("4,000 lines took %.0f ms", r.MS) |
| 154 | } |
| 155 | if !r.Input { |
| 156 | t.Error("no input event: the panel's listeners weren't told") |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // The person's own messages are drawn in the Assistant's little Markdown, |
| 161 | // with emphasis only at word boundaries, by the script and the server |
| 162 | // alike (internal/web/testdata/person.json, which the server's and the |
| 163 | // app's tests check too); plain words stay plain text, and unsafe links |
| 164 | // stay text. |
| 165 | func TestAskPersonMarkdown(t *testing.T) { |
| 166 | raw, err := os.ReadFile("../web/testdata/person.json") |
| 167 | if err != nil { |
| 168 | t.Fatal(err) |
| 169 | } |
| 170 | var cases []struct{ Text, HTML string } |
| 171 | if err := json.Unmarshal(raw, &cases); err != nil { |
| 172 | t.Fatal(err) |
| 173 | } |
| 174 | said := make([]string, len(cases)) |
| 175 | for i, c := range cases { |
| 176 | said[i] = c.Text |
| 177 | } |
| 178 | rig, _ := askBase(t) |
| 179 | openPanel(t, rig) |
| 180 | for i, s := range said { |
| 181 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`(() => { const f = document.querySelector("#ask-text"); f.value = `+strconv.Quote(s)+`; |
| 182 | f.dispatchEvent(new Event("input", { bubbles: true })); document.querySelector("#ask-panel form.composer").requestSubmit() })()`)) |
| 183 | n := strconv.Itoa(i + 1) |
| 184 | drive(t, rig, chromedp.Poll[chromedp.Void](`document.querySelectorAll("#ask-panel .msg.person").length === `+n+ |
| 185 | ` && document.querySelectorAll("#ask-panel .msg.coordinator").length === `+n+` && document.querySelector("#ask-panel button.stop").hidden`, |
| 186 | chromedp.WithPollingTimeout(10*time.Second))) |
| 187 | } |
| 188 | got := harness.Evaluate[string](rig, `(async () => { |
| 189 | const page = new DOMParser().parseFromString(await (await fetch("/ask")).text(), "text/html"); |
| 190 | const out = []; |
| 191 | for (const m of document.querySelectorAll("#ask-panel .msg.person")) { |
| 192 | const s = page.querySelector('.msg.person[data-id="' + CSS.escape(m.dataset.id) + '"]'); |
| 193 | if (!s) out.push(m.dataset.id + ": not on /ask"); |
| 194 | else if (s.innerHTML !== m.innerHTML) out.push(JSON.stringify(m.textContent) + "\n script " + m.innerHTML + "\n server " + s.innerHTML); |
| 195 | } |
| 196 | return out.join("\n"); |
| 197 | })()`, chromedp.EvalAwaitPromise) |
| 198 | if got != "" { |
| 199 | t.Errorf("the script and the server draw differently:\n%s", got) |
| 200 | } |
| 201 | var drawn []string |
| 202 | if err := json.Unmarshal([]byte(harness.Evaluate[string](rig, `JSON.stringify([...document.querySelectorAll("#ask-panel .msg.person")].map(m => m.innerHTML))`)), &drawn); err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | for i, c := range cases { |
| 206 | if i < len(drawn) && drawn[i] != c.HTML { |
| 207 | t.Errorf("%q drawn\n %s\nwant %s", c.Text, drawn[i], c.HTML) |
| 208 | } |
| 209 | } |
| 210 | // Their links keep the bubble's ink; their lists have markers. |
| 211 | if got := harness.Evaluate[string](rig, `(() => { |
| 212 | const a = document.querySelector("#ask-panel .msg.person a"), p = []; |
| 213 | if (getComputedStyle(a).color !== getComputedStyle(a.closest(".msg")).color) p.push("link colour " + getComputedStyle(a).color); |
| 214 | for (const l of document.querySelectorAll("#ask-panel .msg.person ul, #ask-panel .msg.person ol")) |
| 215 | if (!/^(disc|decimal)$/.test(getComputedStyle(l).listStyleType)) p.push(l.tagName + " " + getComputedStyle(l).listStyleType); |
| 216 | return p.join("; "); |
| 217 | })()`); got != "" { |
| 218 | t.Errorf("person styles: %s", got) |
| 219 | } |
| 220 | rig.Screen("#ask-panel", "the person's messages in simple Markdown") |
| 221 | } |
| 222 | |