//go:build browser package headless import ( "context" "os" "path/filepath" "strconv" "testing" "amadan.net/oficina/equipo/internal/browsertest" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/chromedp" ) // Paul: clicking anywhere in the compose box, the dock around the pill, // the pill's edge, the space beside the send arrow or between the + and // the pill, puts the cursor in the message; the arrow and the + keep their // own clicks. Desktop with a mouse at 1280, touch at 390. func TestBrowserComposerFocus(t *testing.T) { _, inst, r := localEnv(t) r.start(t) c := inst.Cookie() for _, tc := range []struct { width, height int64 touch bool }{{1280, 900, false}, {390, 844, true}} { p := openPage(t, inst.URL, c.Name, c.Value, tc.width, tc.height, !tc.touch) if tc.touch { p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { _, err := cdp.Call(ctx, target, emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: true, MaxTouchPoints: 5}) return err })) } click := func(x, y float64) { t.Helper() if tc.touch { p.run(chromedp.TapXY(x, y)) } else { p.run(chromedp.MouseClickXY(x, y)) } } point := func(expr string) (float64, float64) { t.Helper() var v []float64 p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { var err error v, err = chromedp.Evaluate[[]float64](expr)(ctx, target) return err })) return v[0], v[1] } for name, expr := range map[string]string{ "outer padding": `(()=>{const b=document.querySelector('#chat-composer').getBoundingClientRect();const f=document.querySelector('#composer').getBoundingClientRect();return [f.right+(b.right-f.right)/2,f.y+f.height/2]})()`, "below the pill": `(()=>{const b=document.querySelector('#chat-composer').getBoundingClientRect();const f=document.querySelector('[equipo-pill]').getBoundingClientRect();return [f.x+f.width/2,f.bottom+(b.bottom-f.bottom)/2]})()`, "pill edge": `(()=>{const b=document.querySelector('[equipo-pill]').getBoundingClientRect();return [b.x+2,b.y+b.height/2]})()`, "beside the arrow": `(()=>{const b=document.querySelector('[equipo-send]').getBoundingClientRect();return [b.x-3,b.y+b.height/2]})()`, "between + and pill": `(()=>{const a=document.querySelector('[equipo-plus]').getBoundingClientRect();const f=document.querySelector('[equipo-pill]').getBoundingClientRect();return [(a.right+f.x)/2,f.y+f.height/2]})()`, } { // A tap in the narrow gap beside the + lands on the + (touch // adjustment snaps taps to the nearest control), as on a phone. if tc.touch && name == "between + and pill" { continue } p.check(`(()=>{document.activeElement&&document.activeElement.blur();return document.activeElement!==document.querySelector('#chat-body')})()`, "blur") x, y := point(expr) click(x, y) p.run(browsertest.Wait(`document.activeElement===document.querySelector('#chat-body')`)) t.Logf("%dpx %s: focused", tc.width, name) } // The + keeps its own click: its menu opens and the message keeps // no focus it did not have. p.check(`(()=>{document.activeElement&&document.activeElement.blur();return true})()`, "blur") x, y := point(`(()=>{const b=document.querySelector('[equipo-plus]').getBoundingClientRect();return [b.x+b.width/2,b.y+b.height/2]})()`) click(x, y) p.run(browsertest.Wait(`!document.querySelector('[equipo-add-menu]').hidden && document.activeElement!==document.querySelector('#chat-body')`)) click(x, y) p.run(browsertest.Wait(`document.querySelector('[equipo-add-menu]').hidden`)) // Send keeps its own click: the message goes. text := "composer focus " + map[bool]string{true: "touch", false: "mouse"}[tc.touch] p.check(`(()=>{const b=document.querySelector('#chat-body');b.value=`+jsonString(text)+`;b.dispatchEvent(new Event('input',{bubbles:true}));return true})()`, "type") p.run(browsertest.Wait(`getComputedStyle(document.querySelector('[equipo-send]')).visibility==='visible' && getComputedStyle(document.querySelector('[equipo-send]')).opacity==='1'`)) x, y = point(`(()=>{const b=document.querySelector('[equipo-send]').getBoundingClientRect();return [b.x+b.width/2,b.y+b.height/2]})()`) click(x, y) p.run(browsertest.Wait(`[...document.querySelectorAll('[equipo-message="own"] [equipo-message-body]')].some(e=>e.textContent===` + jsonString(text) + `)`)) p.stop() } } // effectiveBackground is the colour painted behind an element: its own // background, else its nearest ancestor's. const effectiveBackground = `(el=>{for(;el;el=el.parentElement){const c=getComputedStyle(el).backgroundColor;if(c!=='rgba(0, 0, 0, 0)'&&c!=='transparent')return c}return getComputedStyle(document.documentElement).backgroundColor})` // Paul: the chat area's background matches the composer's, in both // themes: the dock is the feed's colour (no band, no hard edge), a short // fade from transparent to that colour separates messages at its top, // and the compose box keeps its own field surface and border. Set // EQUIPO_SCREENSHOTS to a directory to save composer-*.png. func TestBrowserComposerDock(t *testing.T) { _, inst, r := localEnv(t) r.start(t) c := inst.Cookie() for _, tc := range []struct { width, height int64 pointer bool }{{1280, 900, true}, {390, 844, false}} { p := openPage(t, inst.URL, c.Name, c.Value, tc.width, tc.height, tc.pointer) for i := 0; i < 8; i++ { p.send("A message to fill the feed, number " + jsonNumber(float64(i)) + ".") } p.run(browsertest.Wait(`document.querySelectorAll('[equipo-message="own"]').length>=8 && document.querySelectorAll('[equipo-message="incoming"] [equipo-markdown]').length>=1`)) for _, theme := range []string{"light", "dark"} { p.check(`(()=>{document.documentElement.dataset.theme=`+jsonString(theme)+`;const s=document.querySelector('[equipo-scroll]');s.scrollTop=s.scrollHeight;return true})()`, "theme") p.check(`(`+effectiveBackground+`)(document.querySelector('#chat-composer'))===(`+effectiveBackground+`)(document.querySelector('[equipo-scroll]'))`, theme+": the dock is not the feed's colour") p.check(`(()=>{const own=getComputedStyle(document.querySelector('#chat-composer')).backgroundColor;return own==='rgba(0, 0, 0, 0)'||own===(`+effectiveBackground+`)(document.querySelector('[equipo-scroll]'))})()`, theme+": the dock paints a band") p.check(`(()=>{const f=getComputedStyle(document.querySelector('#chat-composer'),'::before');return f.content!=='none'&&f.backgroundImage.includes('linear-gradient')&&f.backgroundImage.includes((`+effectiveBackground+`)(document.querySelector('[equipo-scroll]')))&&f.pointerEvents==='none'&&parseFloat(f.height)>0&&parseFloat(f.height)<=40})()`, theme+": no short fade at the dock's top") p.check(`(()=>{const b=getComputedStyle(document.querySelector('[equipo-pill]'));return parseFloat(b.borderTopWidth)>=1&&b.backgroundColor!=='rgba(0, 0, 0, 0)'})()`, theme+": the compose box lost its field") if dir := os.Getenv("EQUIPO_SCREENSHOTS"); dir != "" { var shot []byte p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { var err error shot, err = chromedp.CaptureScreenshot()(ctx, target) return err })) if err := os.WriteFile(filepath.Join(dir, "composer-"+strconv.FormatInt(tc.width, 10)+"-"+theme+".png"), shot, 0644); err != nil { t.Fatal(err) } } } p.stop() } }