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 "bytes"
7 "encoding/json"
8 "fmt"
9 "image/png"
10 "strconv"
11 "testing"
12 "time"
13
14 "github.com/chromedp/chromedp"
15
16 "amadan.net/rastrillo/rastrillo/harness"
17)
18
19// plainCompositeJS lays a sheet over the page that sets every mask layer
20// of the composer's edge and the thinking bubble's back to the default
21// compositing (add), as Safari ended up with (Paul's screenshot,
22// 2026-11-19: the conic gradient over the whole field). An edge that only
23// shows as an edge through mask-composite: exclude fills the field then.
24// on false takes the sheet away. hide also hides the edge itself, for a
25// picture of what is under it.
26func edgeSheetJS(plain, hide bool) string {
27 css := ""
28 if plain {
29 css += `.composer .field::after, .msg.thinking::before, .msg.answered::before { -webkit-mask-composite: source-over !important; mask-composite: add !important; }`
30 }
31 if hide {
32 css += `.composer .field::after, .msg.thinking::before, .msg.answered::before { visibility: hidden !important; }`
33 }
34 return `(() => { const s = new CSSStyleSheet(); s.replaceSync(` + strconv.Quote(css) + `); document.adoptedStyleSheets = [s]; })()`
35}
36
37// rgbAt is the colour at CSS pixel (x, y) of a screenshot taken at dpr.
38func rgbAt(t *testing.T, shot []byte, dpr, x, y float64) [3]int {
39 t.Helper()
40 img, err := png.Decode(bytes.NewReader(shot))
41 if err != nil {
42 t.Fatal(err)
43 }
44 r, g, b, _ := img.At(int(x*dpr), int(y*dpr)).RGBA()
45 return [3]int{int(r >> 8), int(g >> 8), int(b >> 8)}
46}
47
48func apart(a, b [3]int) int {
49 d := 0
50 for i := range a {
51 if a[i] > b[i] {
52 d += a[i] - b[i]
53 } else {
54 d += b[i] - a[i]
55 }
56 }
57 return d
58}
59
60type box struct{ X, Y, W, H float64 }
61
62func boxOf(t *testing.T, rig *harness.Rig, sel string) box {
63 t.Helper()
64 var b box
65 js := `(() => { const r = document.querySelector(` + strconv.Quote(sel) + `).getBoundingClientRect(); return JSON.stringify({X: r.left, Y: r.top, W: r.width, H: r.height}); })()`
66 if err := json.Unmarshal([]byte(harness.Evaluate[string](rig, js)), &b); err != nil {
67 t.Fatal(err)
68 }
69 return b
70}
71
72// shotWith takes a screenshot with the edge sheet given.
73func shotWith(t *testing.T, rig *harness.Rig, plain, hide bool) []byte {
74 t.Helper()
75 drive(t, rig, chromedp.Evaluate[chromedp.Void](edgeSheetJS(plain, hide)), chromedp.Sleep(150*time.Millisecond))
76 return harness.Read(rig, chromedp.CaptureScreenshot())
77}
78
79// The composer's focus edge and the thinking bubble's edge are edges: the
80// gradient shows in a thin ring round the field (and the bubble) and never
81// over what is inside, whatever the browser does with mask compositing.
82// The pictures are compared with the edge hidden, everything held still
83// (reduced motion), in both themes; the edge itself must still show.
84func TestAskEdgeLeavesTheFieldClear(t *testing.T) {
85 rig, f := askBase(t)
86 th := thinking{t, rig, f}
87 dpr := 1.0
88 for _, scheme := range []string{"light", "dark"} {
89 for _, plain := range []bool{false, true} {
90 name := fmt.Sprintf("%s, plain compositing %v", scheme, plain)
91 drive(t, rig, reducedMotion(scheme, true))
92 openPanel(t, rig)
93 drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").focus()`))
94 th.wait(`getComputedStyle(document.querySelector("#ask-text").parentElement, "::after").opacity === "1"`)
95 dpr = harness.Evaluate[float64](rig, `window.devicePixelRatio`)
96 field := boxOf(t, rig, "#ask-panel .composer .field")
97 under, over := shotWith(t, rig, plain, true), shotWith(t, rig, plain, false)
98 if plain {
99 viewportShot(t, rig, "edge-plain-compositing-"+scheme)
100 }
101 // Inside the field, right of the placeholder: untouched.
102 ix, iy := field.X+field.W*0.6, field.Y+field.H/2
103 if d := apart(rgbAt(t, under, dpr, ix, iy), rgbAt(t, over, dpr, ix, iy)); d > 12 {
104 t.Errorf("%s: the edge paints over the field: %v under it, %v with it", name,
105 rgbAt(t, under, dpr, ix, iy), rgbAt(t, over, dpr, ix, iy))
106 }
107 // On the ring, 2px above the field's top: the edge shows.
108 ex, ey := field.X+field.W/2, field.Y-2
109 if d := apart(rgbAt(t, under, dpr, ex, ey), rgbAt(t, over, dpr, ex, ey)); d < 60 {
110 t.Errorf("%s: no edge round the field: %v under it, %v with it", name,
111 rgbAt(t, under, dpr, ex, ey), rgbAt(t, over, dpr, ex, ey))
112 }
113
114 // The thinking bubble's edge (still, with reduced motion: a
115 // gradient on its ::before).
116 drive(t, rig, chromedp.Evaluate[chromedp.Void](edgeSheetJS(false, false)))
117 th.send("Can you make me a website?")
118 drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`), chromedp.Sleep(500*time.Millisecond))
119 bub := boxOf(t, rig, thinkingSel)
120 under, over = shotWith(t, rig, plain, true), shotWith(t, rig, plain, false)
121 // In its left padding, clear of the dots and of the 1.5px ring.
122 bx, by := bub.X+7, bub.Y+bub.H/2
123 if d := apart(rgbAt(t, under, dpr, bx, by), rgbAt(t, over, dpr, bx, by)); d > 12 {
124 t.Errorf("%s: the thinking edge paints over the bubble: %v under it, %v with it", name,
125 rgbAt(t, under, dpr, bx, by), rgbAt(t, over, dpr, bx, by))
126 }
127 drive(t, rig, chromedp.Evaluate[chromedp.Void](edgeSheetJS(false, false)),
128 chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`)))
129 th.wait(`!document.querySelector("#ask-panel .thinking")`)
130 }
131 }
132 drive(t, rig, reducedMotion("dark", false))
133}
134