oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package headless
4
5import (
6 "context"
7 "fmt"
8 "os"
9 "path/filepath"
10 "testing"
11
12 "amadan.net/oficina/equipo/internal/browsertest"
13 "amadan.net/rastrillo/rastrillo/harness"
14 "github.com/chromedp/cdproto/cdp"
15 "github.com/chromedp/cdproto/emulation"
16 "github.com/chromedp/cdproto/network"
17 "github.com/chromedp/chromedp"
18)
19
20// The reply streams into the message it becomes. The draft has no box or
21// border; when the reply is committed the same element stays, takes its
22// id and time, and keeps its size. At 1280 and 390 px, light and dark,
23// and once with reduced motion. EQUIPO_BROWSER_ARTIFACTS, when set,
24// receives thinking-*.png of the line, the stream and the settled reply.
25func TestBrowserDraftIsTheMessage(t *testing.T) {
26 _, inst, r := localEnv(t)
27 r.start(t)
28 alloc, cancelAlloc := chromedp.NewExecAllocator(context.Background(), append(chromedp.DefaultExecAllocatorOptions[:], chromedp.ExecPath(harness.ChromePath(t)), chromedp.Flag("headless", true), chromedp.NoSandbox)...)
29 defer cancelAlloc()
30 ctx, cancel := chromedp.NewContext(alloc)
31 defer cancel()
32 cookie := inst.Cookie()
33 run := func(actions ...chromedp.Action[chromedp.Void]) {
34 t.Helper()
35 if err := chromedp.Do(ctx, actions...); err != nil {
36 t.Fatal(err)
37 }
38 }
39 check := func(expression, reason string) {
40 t.Helper()
41 var ok bool
42 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
43 var err error
44 ok, err = chromedp.Evaluate[bool](expression)(ctx, target)
45 return err
46 }))
47 if !ok {
48 t.Fatal(reason)
49 }
50 }
51 shot := func(name string) {
52 t.Helper()
53 dir := os.Getenv("EQUIPO_BROWSER_ARTIFACTS")
54 if dir == "" {
55 return
56 }
57 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
58 png, err := chromedp.CaptureScreenshot()(ctx, target)
59 if err != nil {
60 return err
61 }
62 if err := os.MkdirAll(dir, 0o755); err != nil {
63 return err
64 }
65 return os.WriteFile(filepath.Join(dir, "thinking-"+name+".png"), png, 0o644)
66 }))
67 }
68 media := func(scheme, motion string) chromedp.Action[chromedp.Void] {
69 return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
70 _, err := cdp.Call(ctx, target, emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{Features: []*emulation.MediaFeature{{Name: "prefers-color-scheme", Value: scheme}, {Name: "prefers-reduced-motion", Value: motion}}})
71 return err
72 })
73 }
74 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
75 _, err := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: cookie.Name, Value: cookie.Value, URL: inst.URL})
76 return err
77 }))
78
79 for _, c := range []struct {
80 width, height int
81 scheme, motion string
82 }{
83 {1280, 800, "light", "no-preference"},
84 {1280, 800, "dark", "no-preference"},
85 {390, 844, "light", "no-preference"},
86 {390, 844, "dark", "no-preference"},
87 {390, 844, "light", "reduce"},
88 } {
89 name := fmt.Sprintf("%d-%s", c.width, c.scheme)
90 if c.motion == "reduce" {
91 name += "-reduced"
92 }
93 run(chromedp.EmulateViewport(int64(c.width), int64(c.height)), media(c.scheme, c.motion))
94 run(chromedp.Navigate(inst.URL+"/conversation"), browsertest.Wait(`!!document.querySelector('[data-chat-stream]') && document.querySelector('[equipo-offline]').hidden`))
95 run(browsertest.Wait(`true`))
96 check(`(()=>{document.querySelector('#chat-body').value='draft check pause:2200 sleep:1200 slowstream';document.querySelector('#composer').requestSubmit();return true})()`, "send")
97
98 // The line: sparkle, words and timer, above the composer.
99 run(browsertest.Wait(`(()=>{const e=document.querySelector('[equipo-thinking] [equipo-elapsed]');return !!document.querySelector('[equipo-thinking] [equipo-sparkle]') && !!e && /^[1-9]/.test(e.textContent)})()`))
100 check(`(()=>{const b=document.querySelector('[equipo-thinking]').getBoundingClientRect();const c=document.querySelector('#chat-composer').getBoundingClientRect();return b.bottom<=c.top+1 && b.top>=0})()`, "the thinking line is not fully above the composer")
101 shot(name + "-line")
102
103 // Record the draft as it streams, frame by frame, until it is
104 // committed.
105 check(`(()=>{window.__d=null;window.__m=null;const f=()=>{const d=document.querySelector('[equipo-thinking][data-streaming]');if(d&&!window.__d)window.__d=d;const x=window.__d;if(x&&x.hasAttribute('equipo-thinking')&&x.querySelector('[data-stream-text] p')){const b=x.querySelector('[equipo-bubble]'),s=getComputedStyle(b),r=x.getBoundingClientRect(),q=x.querySelector('[equipo-message-body]').getBoundingClientRect();window.__m={w:r.width,h:r.height,bw:q.width,bh:q.height,border:s.borderTopWidth+' '+s.borderLeftWidth,bg:s.backgroundColor,pad:s.paddingTop+' '+s.paddingLeft}}if(!x||x.hasAttribute('equipo-thinking'))requestAnimationFrame(f)};f();return true})()`, "record")
106 run(browsertest.Wait(`!!window.__m`))
107 check(`window.__m.border==='0px 0px' && window.__m.bg==='rgba(0, 0, 0, 0)' && window.__m.pad==='0px 0px'`, "the streaming draft has a box")
108 check(`!!window.__d.querySelector('[equipo-message-meta] strong') && !!window.__d.querySelector('[equipo-message-meta] time') && window.__d.querySelector('[equipo-message-body]').hasAttribute('equipo-markdown')`, "the draft is not marked up as a message")
109 shot(name + "-streaming")
110
111 // Committed: the same element, its id and time filled, the same size.
112 run(browsertest.Wait(`!!window.__d && !window.__d.hasAttribute('equipo-thinking') && !!window.__d.dataset.message`))
113 check(`window.__d.isConnected && document.querySelector('[data-message="'+CSS.escape(window.__d.dataset.message)+'"]')===window.__d`, "the committed reply replaced its draft")
114 check(`/^[0-9]{2}:[0-9]{2}$/.test(window.__d.querySelector('[equipo-message-meta] time').textContent)`, "no time on the committed reply")
115 check(`(()=>{const r=window.__d.getBoundingClientRect(),q=window.__d.querySelector('[equipo-message-body]').getBoundingClientRect(),m=window.__m;return Math.abs(r.width-m.w)<1 && Math.abs(r.height-m.h)<1 && Math.abs(q.width-m.bw)<1 && Math.abs(q.height-m.bh)<1})()`, "the reply changed size when committed")
116 run(browsertest.Wait(`!window.__d.querySelector('[equipo-activity-line]') && !document.querySelector('[equipo-thinking]')`))
117 shot(name + "-reply")
118 }
119}
120