//go:build browser package browsertest import ( "bytes" "encoding/json" "fmt" "image/png" "strconv" "testing" "time" "github.com/chromedp/chromedp" "amadan.net/rastrillo/rastrillo/harness" ) // plainCompositeJS lays a sheet over the page that sets every mask layer // of the composer's edge and the thinking bubble's back to the default // compositing (add), as Safari ended up with (Paul's screenshot, // 2026-11-19: the conic gradient over the whole field). An edge that only // shows as an edge through mask-composite: exclude fills the field then. // on false takes the sheet away. hide also hides the edge itself, for a // picture of what is under it. func edgeSheetJS(plain, hide bool) string { css := "" if plain { css += `.composer .field::after, .msg.thinking::before, .msg.answered::before { -webkit-mask-composite: source-over !important; mask-composite: add !important; }` } if hide { css += `.composer .field::after, .msg.thinking::before, .msg.answered::before { visibility: hidden !important; }` } return `(() => { const s = new CSSStyleSheet(); s.replaceSync(` + strconv.Quote(css) + `); document.adoptedStyleSheets = [s]; })()` } // rgbAt is the colour at CSS pixel (x, y) of a screenshot taken at dpr. func rgbAt(t *testing.T, shot []byte, dpr, x, y float64) [3]int { t.Helper() img, err := png.Decode(bytes.NewReader(shot)) if err != nil { t.Fatal(err) } r, g, b, _ := img.At(int(x*dpr), int(y*dpr)).RGBA() return [3]int{int(r >> 8), int(g >> 8), int(b >> 8)} } func apart(a, b [3]int) int { d := 0 for i := range a { if a[i] > b[i] { d += a[i] - b[i] } else { d += b[i] - a[i] } } return d } type box struct{ X, Y, W, H float64 } func boxOf(t *testing.T, rig *harness.Rig, sel string) box { t.Helper() var b box js := `(() => { const r = document.querySelector(` + strconv.Quote(sel) + `).getBoundingClientRect(); return JSON.stringify({X: r.left, Y: r.top, W: r.width, H: r.height}); })()` if err := json.Unmarshal([]byte(harness.Evaluate[string](rig, js)), &b); err != nil { t.Fatal(err) } return b } // shotWith takes a screenshot with the edge sheet given. func shotWith(t *testing.T, rig *harness.Rig, plain, hide bool) []byte { t.Helper() drive(t, rig, chromedp.Evaluate[chromedp.Void](edgeSheetJS(plain, hide)), chromedp.Sleep(150*time.Millisecond)) return harness.Read(rig, chromedp.CaptureScreenshot()) } // The composer's focus edge and the thinking bubble's edge are edges: the // gradient shows in a thin ring round the field (and the bubble) and never // over what is inside, whatever the browser does with mask compositing. // The pictures are compared with the edge hidden, everything held still // (reduced motion), in both themes; the edge itself must still show. func TestAskEdgeLeavesTheFieldClear(t *testing.T) { rig, f := askBase(t) th := thinking{t, rig, f} dpr := 1.0 for _, scheme := range []string{"light", "dark"} { for _, plain := range []bool{false, true} { name := fmt.Sprintf("%s, plain compositing %v", scheme, plain) drive(t, rig, reducedMotion(scheme, true)) openPanel(t, rig) drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").focus()`)) th.wait(`getComputedStyle(document.querySelector("#ask-text").parentElement, "::after").opacity === "1"`) dpr = harness.Evaluate[float64](rig, `window.devicePixelRatio`) field := boxOf(t, rig, "#ask-panel .composer .field") under, over := shotWith(t, rig, plain, true), shotWith(t, rig, plain, false) if plain { viewportShot(t, rig, "edge-plain-compositing-"+scheme) } // Inside the field, right of the placeholder: untouched. ix, iy := field.X+field.W*0.6, field.Y+field.H/2 if d := apart(rgbAt(t, under, dpr, ix, iy), rgbAt(t, over, dpr, ix, iy)); d > 12 { t.Errorf("%s: the edge paints over the field: %v under it, %v with it", name, rgbAt(t, under, dpr, ix, iy), rgbAt(t, over, dpr, ix, iy)) } // On the ring, 2px above the field's top: the edge shows. ex, ey := field.X+field.W/2, field.Y-2 if d := apart(rgbAt(t, under, dpr, ex, ey), rgbAt(t, over, dpr, ex, ey)); d < 60 { t.Errorf("%s: no edge round the field: %v under it, %v with it", name, rgbAt(t, under, dpr, ex, ey), rgbAt(t, over, dpr, ex, ey)) } // The thinking bubble's edge (still, with reduced motion: a // gradient on its ::before). drive(t, rig, chromedp.Evaluate[chromedp.Void](edgeSheetJS(false, false))) th.send("Can you make me a website?") drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`), chromedp.Sleep(500*time.Millisecond)) bub := boxOf(t, rig, thinkingSel) under, over = shotWith(t, rig, plain, true), shotWith(t, rig, plain, false) // In its left padding, clear of the dots and of the 1.5px ring. bx, by := bub.X+7, bub.Y+bub.H/2 if d := apart(rgbAt(t, under, dpr, bx, by), rgbAt(t, over, dpr, bx, by)); d > 12 { t.Errorf("%s: the thinking edge paints over the bubble: %v under it, %v with it", name, rgbAt(t, under, dpr, bx, by), rgbAt(t, over, dpr, bx, by)) } drive(t, rig, chromedp.Evaluate[chromedp.Void](edgeSheetJS(false, false)), chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) th.wait(`!document.querySelector("#ask-panel .thinking")`) } } drive(t, rig, reducedMotion("dark", false)) }