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 "encoding/json"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/browsertest"
12 "amadan.net/rastrillo/rastrillo/harness"
13 "github.com/chromedp/cdproto/cdp"
14 "github.com/chromedp/cdproto/emulation"
15 "github.com/chromedp/cdproto/network"
16 "github.com/chromedp/chromedp"
17)
18
19// At 390 px: the thinking line counts up and shows what the coordinator
20// is doing beside a sparkle, a cobalt wave running through its letters
21// unless reduced motion is asked for; the Added card
22// removes its project, a question card takes a tap; and with a smaller
23// visual viewport (a phone keyboard) the newest bubble stays above the
24// composer.
25func TestBrowserThinkingAndCards(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 send := func(text string) {
52 t.Helper()
53 check(`(()=>{document.querySelector('#chat-body').value=`+jsonString(text)+`;document.querySelector('#composer').requestSubmit();return true})()`, "send")
54 }
55 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
56 _, err := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: cookie.Name, Value: cookie.Value, URL: inst.URL})
57 return err
58 }), chromedp.EmulateViewport(390, 844))
59 run(chromedp.Navigate(inst.URL+"/conversation"), browsertest.Wait(`!!document.querySelector('[data-chat-stream]') && document.querySelector('[equipo-offline]').hidden`))
60 // The allowlist arrives with the runner's hello.
61 run(browsertest.Wait(`true`))
62
63 // The timer counts in seconds from the send; the line shows the
64 // latest activity, then gives way to the streamed reply.
65 send("think pause:2600 sleep:1500 tool:status\ntool:add_project:oficina/sample")
66 run(browsertest.Wait(`(()=>{const l=document.querySelector('[equipo-thinking] [equipo-activity]');return !!l && l.textContent==='Adding oficina/sample…'})()`))
67 run(browsertest.Wait(`(()=>{const e=document.querySelector('[equipo-thinking] [equipo-elapsed]');return !!e && /^[1-9][0-9]*s$/.test(e.textContent)})()`))
68 check(`!!document.querySelector('[equipo-thinking] [equipo-sparkle] svg') && [...document.querySelectorAll('[equipo-thinking] [equipo-phrase] [data-in] > span')].some(s=>getComputedStyle(s).animationName.includes('equipo-wave'))`, "no sparkle or wave")
69 check(`!document.querySelector('[equipo-thinking-dots]')`, "dots still shown")
70 run(browsertest.Wait(`!!document.querySelector('[equipo-thinking][data-streaming]') && getComputedStyle(document.querySelector('[equipo-thinking] [equipo-activity-line]')).visibility==='hidden'`))
71 run(browsertest.Wait(`!document.querySelector('[equipo-thinking]')`))
72 run(browsertest.Wait(`[...document.querySelectorAll('[equipo-card="project"][data-state="added"]')].some(e=>e.textContent.includes('Added oficina/sample'))`))
73
74 // Reduced motion: the line is still.
75 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
76 _, err := cdp.Call(ctx, target, emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{Features: []*emulation.MediaFeature{{Name: "prefers-reduced-motion", Value: "reduce"}}})
77 return err
78 }))
79 send("think pause:2000")
80 run(browsertest.Wait(`!!document.querySelector('[equipo-thinking] [equipo-phrase] [data-in] > span')`))
81 check(`getComputedStyle(document.querySelector('[equipo-thinking] [equipo-sparkle] path')).animationName==='none' && ![...document.querySelectorAll('[equipo-thinking] [equipo-phrase] span')].some(s=>getComputedStyle(s).animationName.includes('equipo-wave'))`, "reduced motion still animates")
82 run(browsertest.Wait(`!document.querySelector('[equipo-thinking]')`))
83
84 // Remove on the Added card takes the project out, without navigating.
85 check(`(()=>{window.__page=1;document.querySelector('[equipo-card="project"][data-state="added"] form[action$="/remove"] button').click();return true})()`, "remove")
86 run(browsertest.Wait(`[...document.querySelectorAll('[equipo-card="project"][data-state="removed"]')].some(e=>e.textContent.includes('Removed oficina/sample'))`))
87 check(`window.__page===1 && performance.getEntriesByType('navigation').length===1`, "remove navigated")
88
89 // A question card takes one tap; the answer is the person's message.
90 send("tool:ask_person:Should the panes slide on iPad too?|Yes|No")
91 run(browsertest.Wait(`!!document.querySelector('[equipo-card="question"][data-state="open"] button[value="Yes"]')`))
92 check(`(()=>{document.querySelector('[equipo-card="question"][data-state="open"] button[value="Yes"]').click();return true})()`, "answer")
93 run(browsertest.Wait(`[...document.querySelectorAll('[equipo-card="question"][data-state="answered"] [equipo-thread-turn][data-own] [equipo-thread-text]')].some(e=>e.textContent.trim()==='Yes')`))
94 run(browsertest.Wait(`!document.querySelector('[equipo-thinking]')`))
95
96 // A keyboard shrinks the visual viewport: after a send the newest
97 // bubble sits fully above the composer.
98 for range 6 {
99 send("filler")
100 run(browsertest.Wait(`!document.querySelector('[equipo-thinking]') && !document.querySelector('[data-pending]')`))
101 }
102 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
103 // Only the visual viewport shrinks, as with the iOS keyboard: the
104 // layout viewport keeps its height.
105 _, err := cdp.Call(ctx, target, emulation.SetPageScaleFactor, emulation.SetPageScaleFactorParams{PageScaleFactor: 1.6})
106 return err
107 }))
108 run(browsertest.Wait(`window.visualViewport.height<window.innerHeight*0.7`))
109 send("one more pause:2500")
110 run(browsertest.Wait(`!!document.querySelector('[equipo-thinking]')`))
111 run(browsertest.Wait(`(()=>{const b=document.querySelector('[equipo-thinking]').getBoundingClientRect();const c=document.querySelector('#chat-composer').getBoundingClientRect();return b.bottom<=c.top+1 && b.top>=0 && c.bottom<=window.visualViewport.height+1})()`))
112 // Reading back up is not yanked down by new text.
113 check(`(()=>{const s=document.querySelector('[equipo-scroll]');s.scrollTop=0;s.dispatchEvent(new Event('scroll'));return true})()`, "scroll up")
114 run(browsertest.Wait(`!document.querySelector('[equipo-thinking]')`))
115 check(`document.querySelector('[equipo-scroll]').scrollTop<50`, "a reply yanked the reader to the bottom")
116}
117
118func jsonString(s string) string { raw, _ := json.Marshal(s); return string(raw) }
119
120// Review fix 4: refreshes overlap when a card and the reply land close
121// together. A slow first refresh carrying an older page must not undo the
122// newer one: refreshes are serialised and an older page is never applied.
123func TestBrowserRefreshRace(t *testing.T) {
124 _, inst, r := localEnv(t)
125 r.start(t)
126 alloc, cancelAlloc := chromedp.NewExecAllocator(context.Background(), append(chromedp.DefaultExecAllocatorOptions[:], chromedp.ExecPath(harness.ChromePath(t)), chromedp.Flag("headless", true), chromedp.NoSandbox)...)
127 defer cancelAlloc()
128 ctx, cancel := chromedp.NewContext(alloc)
129 defer cancel()
130 cookie := inst.Cookie()
131 run := func(actions ...chromedp.Action[chromedp.Void]) {
132 t.Helper()
133 if err := chromedp.Do(ctx, actions...); err != nil {
134 t.Fatal(err)
135 }
136 }
137 check := func(expression, reason string) {
138 t.Helper()
139 var ok bool
140 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
141 var err error
142 ok, err = chromedp.Evaluate[bool](expression)(ctx, target)
143 return err
144 }))
145 if !ok {
146 t.Fatal(reason)
147 }
148 }
149 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
150 _, err := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: cookie.Name, Value: cookie.Value, URL: inst.URL})
151 return err
152 }), chromedp.EmulateViewport(390, 844))
153 run(chromedp.Navigate(inst.URL+"/conversation"), browsertest.Wait(`!!document.querySelector('[data-chat-stream]') && document.querySelector('[equipo-offline]').hidden`))
154 // The first refresh answers late with the page as it was at load.
155 check(`(()=>{const x=new XMLHttpRequest();x.open('GET',location.pathname,false);x.send();const stale=x.responseText;window.__fetch=window.fetch;let first=true;window.fetch=function(u,o){if(first&&(!o||!o.method||o.method==='GET')&&String(u).endsWith('/conversation')){first=false;return new Promise(res=>setTimeout(()=>res(new Response(stale,{status:200,headers:{'Content-Type':'text/html'}})),1500))}return window.__fetch(u,o)};return true})()`, "setup")
156 check(`(()=>{document.querySelector('#chat-body').value='tool:add_project:oficina/sample';document.querySelector('#composer').requestSubmit();return true})()`, "send")
157 run(browsertest.Wait(`[...document.querySelectorAll('[equipo-markdown]:not([data-stream-text])')].some(e=>e.textContent.includes('Added oficina/sample'))`))
158 // After the slow stale page has come and gone, the newest state stays.
159 run(chromedp.Sleep(2500 * time.Millisecond))
160 check(`[...document.querySelectorAll('[equipo-markdown]:not([data-stream-text])')].some(e=>e.textContent.includes('Added oficina/sample')) && !!document.querySelector('[equipo-card="project"][data-state="added"]')`, "a stale refresh undid the newer page")
161}
162
163// Paul, 2026-11-27: a tap on a card's choice, read from above the bottom
164// of the conversation, left the view where it was while the follow-up
165// came in below. The answer settles the view at the bottom, where the
166// follow-up shows, as a send does; without script the answer's redirect
167// lands there too.
168func TestBrowserCardAnswerSettlesAtBottom(t *testing.T) {
169 _, inst, r := localEnv(t)
170 r.start(t)
171 c := inst.Cookie()
172 p := openPage(t, inst.URL, c.Name, c.Value, 390, 844, false)
173 defer p.stop()
174 const quiet = `!document.querySelector('[equipo-thinking]') && !document.querySelector('[data-pending]')`
175 const atBottom = `(()=>{const s=document.querySelector('[equipo-scroll]');return s.scrollHeight-s.clientHeight>400 && s.scrollHeight-s.scrollTop-s.clientHeight<2})()`
176 const readUp = `(()=>{const s=document.querySelector('[equipo-scroll]');s.scrollTop=s.scrollHeight-s.clientHeight-400;s.dispatchEvent(new Event('scroll'));return s.scrollHeight-s.scrollTop-s.clientHeight>300})()`
177 for range 6 {
178 p.send("filler")
179 p.run(browsertest.Wait(quiet))
180 }
181 p.send("What about oficina for new projects\ntool:set_project_home:oficina")
182 p.run(browsertest.Wait(`!!document.querySelector('[equipo-card="home"][data-state="ask"] [value="confirm"]')`), browsertest.Wait(quiet))
183
184 // With script: read back up, tap the choice; the view settles at the bottom.
185 p.check(readUp, "could not read back up")
186 p.check(`(()=>{document.querySelector('[equipo-card="home"][data-state="ask"] [value="confirm"]').click();return true})()`, "tap")
187 p.run(browsertest.Wait(`!!document.querySelector('[equipo-receipt][equipo-card="home"][data-state="set"]')`), browsertest.Wait(quiet))
188 p.run(browsertest.Wait(atBottom))
189
190 // Without script the page opens at the top of the feed; the answer's
191 // redirect (Undo on the receipt) lands at the bottom.
192 p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
193 _, err := cdp.Call(ctx, target, emulation.SetScriptExecutionDisabled, emulation.SetScriptExecutionDisabledParams{Value: true})
194 return err
195 }), chromedp.Navigate(inst.URL+"/conversation"))
196 p.run(browsertest.Wait(`!!document.querySelector('[equipo-receipt][equipo-card="home"][data-state="set"] [value="undo"]')`))
197 p.check(`!`+atBottom, "the page opened at the bottom without script")
198 p.check(`(()=>{document.querySelector('[equipo-receipt][equipo-card="home"][data-state="set"] [value="undo"]').closest('form').requestSubmit(document.querySelector('[equipo-receipt][equipo-card="home"][data-state="set"] [value="undo"]'));return true})()`, "undo")
199 p.run(browsertest.Wait(`location.pathname==='/conversation' && location.hash==='#conversation-end'`), browsertest.Wait(atBottom))
200}
201