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 "os"
8 "path/filepath"
9 "strconv"
10 "testing"
11
12 "amadan.net/oficina/equipo/internal/browsertest"
13 "amadan.net/oficina/equipo/internal/runner/thin"
14 "amadan.net/oficina/equipo/internal/thintest"
15 "amadan.net/rastrillo/rastrillo/harness"
16 "github.com/chromedp/cdproto/cdp"
17 "github.com/chromedp/cdproto/input"
18 "github.com/chromedp/cdproto/network"
19 "github.com/chromedp/chromedp"
20)
21
22type page struct {
23 t *testing.T
24 ctx context.Context
25 stop func()
26 check func(expression, reason string)
27 run func(actions ...chromedp.Action[chromedp.Void])
28}
29
30// openPage signs a browser in and opens /conversation at width x height.
31func openPage(t *testing.T, url, cookieName, cookieValue string, width, height int64, pointer bool) *page {
32 opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.ExecPath(harness.ChromePath(t)), chromedp.Flag("headless", true), chromedp.NoSandbox)
33 if pointer {
34 // Headless Chrome reports no hover; a desktop has a mouse (Blink's
35 // hover type 2 is hover, pointer type 4 is fine).
36 opts = append(opts, chromedp.Flag("blink-settings", "primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4"))
37 } else {
38 opts = append(opts, chromedp.Flag("blink-settings", "primaryHoverType=1,availableHoverTypes=1,primaryPointerType=2,availablePointerTypes=2"))
39 }
40 alloc, cancelAlloc := chromedp.NewExecAllocator(context.Background(), opts...)
41 ctx, cancel := chromedp.NewContext(alloc)
42 p := &page{t: t, ctx: ctx, stop: func() { cancel(); cancelAlloc() }}
43 p.run = func(actions ...chromedp.Action[chromedp.Void]) {
44 t.Helper()
45 if err := chromedp.Do(ctx, actions...); err != nil {
46 t.Fatal(err)
47 }
48 }
49 p.check = func(expression, reason string) {
50 t.Helper()
51 var ok bool
52 p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
53 var err error
54 ok, err = chromedp.Evaluate[bool](expression)(ctx, target)
55 return err
56 }))
57 if !ok {
58 t.Fatal(reason)
59 }
60 }
61 p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
62 _, err := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: cookieName, Value: cookieValue, URL: url})
63 return err
64 }), chromedp.EmulateViewport(width, height))
65 p.run(chromedp.Navigate(url+"/conversation"), browsertest.Wait(`!!document.querySelector('[data-chat-stream]') && document.querySelector('[equipo-offline]').hidden`))
66 return p
67}
68
69func (p *page) send(text string) {
70 p.t.Helper()
71 p.check(`(()=>{document.querySelector('#chat-body').value=`+jsonString(text)+`;document.querySelector('#composer').requestSubmit();return true})()`, "send")
72}
73
74// receiptAboveReply: the receipt of a turn sits before that turn's reply
75// (compareDocumentPosition 4: the reply follows).
76const receiptAboveReply = `(()=>{const r=document.querySelector('[equipo-receipt][data-state="added"]');if(!r)return false;const m=[...document.querySelectorAll('[equipo-message="incoming"][data-turn-id="'+r.dataset.turnId+'"]')][0];return !!m && !!(r.compareDocumentPosition(m)&4)})()`
77
78const actionsShown = `getComputedStyle(document.querySelector('[equipo-receipt] [equipo-receipt-actions]')).opacity==='1'`
79
80// Paul, 2b: a receipt (Added, Working in, New projects go to) is a quiet
81// line placed where its action happened, before the reply; its actions
82// show only on hover or keyboard focus, without moving anything.
83func TestBrowserReceiptsDesktop(t *testing.T) {
84 _, inst, r := localEnv(t)
85 r.start(t)
86 c := inst.Cookie()
87 p := openPage(t, inst.URL, c.Name, c.Value, 1280, 900, true)
88 defer p.stop()
89 p.send("Fix the README typo in sample.\ntool:add_project:oficina/sample")
90 p.run(browsertest.Wait(`[...document.querySelectorAll('[equipo-message="incoming"] [equipo-markdown]:not([data-stream-text])')].some(e=>e.textContent.includes('Added oficina/sample'))`))
91 p.run(browsertest.Wait(receiptAboveReply))
92 p.check(`(()=>{const r=document.querySelector('[equipo-receipt]');return !r.querySelector('[rst-btn]:not(details [rst-btn])') && getComputedStyle(r).borderTopColor==='rgba(0, 0, 0, 0)' || getComputedStyle(r).backgroundColor==='rgba(0, 0, 0, 0)'})()`, "a receipt has button chrome")
93 p.check(`!(`+actionsShown+`) && getComputedStyle(document.querySelector('[equipo-receipt-more]')).display==='none'`, "actions shown before hover")
94 var box struct{ X, Y, H float64 }
95 p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
96 v, err := chromedp.Evaluate[[]float64](`(()=>{const b=document.querySelector('[equipo-receipt]').getBoundingClientRect();return [b.x+8,b.y+b.height/2,b.height]})()`)(ctx, target)
97 box.X, box.Y, box.H = v[0], v[1], v[2]
98 return err
99 }))
100 p.run(chromedp.MouseEvent(input.DispatchMouseEventTypeMouseMoved, box.X, box.Y))
101 p.run(browsertest.Wait(actionsShown))
102 p.check(`Math.abs(document.querySelector('[equipo-receipt]').getBoundingClientRect().height-`+jsonNumber(box.H)+`)<0.5`, "showing the actions moved the line")
103 // Keyboard: focus reaches the actions and shows them.
104 p.run(chromedp.MouseEvent(input.DispatchMouseEventTypeMouseMoved, 5, 5))
105 p.run(browsertest.Wait(`!(` + actionsShown + `)`))
106 p.check(`(()=>{const b=document.querySelector('[equipo-receipt] [equipo-receipt-actions] button[equipo-link]');b.focus();return document.activeElement===b && b.textContent.trim()!==''})()`, "Remove not focusable")
107 p.run(browsertest.Wait(actionsShown))
108 // The order holds in the server render too.
109 p.run(chromedp.Reload(), browsertest.Wait(`!!document.querySelector('[data-chat-stream]')`), browsertest.Wait(receiptAboveReply))
110 // An unclear home request asks with a prominent one-tap card; the tap
111 // turns it into a quiet receipt with Undo.
112 p.send("What about oficina for new projects\ntool:set_project_home:oficina")
113 p.run(browsertest.Wait(`!!document.querySelector('[equipo-card="home"][data-state="ask"] [rst-btn="primary"][value="confirm"]')`))
114 p.check(`!document.querySelector('[equipo-card="home"][data-state="ask"]').hasAttribute('equipo-receipt')`, "a question rendered as a receipt")
115 p.check(`(()=>{document.querySelector('[equipo-card="home"][data-state="ask"] [value="confirm"]').click();return true})()`, "tap")
116 p.run(browsertest.Wait(`!!document.querySelector('[equipo-receipt][equipo-card="home"][data-state="set"] [equipo-link][value="undo"]')`))
117}
118
119// On touch (no hover) a small ⋯ at the end of the line shows the actions.
120func TestBrowserReceiptsTouch(t *testing.T) {
121 _, inst, r := localEnv(t)
122 r.start(t)
123 c := inst.Cookie()
124 p := openPage(t, inst.URL, c.Name, c.Value, 390, 844, false)
125 defer p.stop()
126 p.send("Fix the README typo in sample.\ntool:add_project:oficina/sample")
127 p.run(browsertest.Wait(receiptAboveReply))
128 p.check(`getComputedStyle(document.querySelector('[equipo-receipt-more]')).display!=='none' && !(`+actionsShown+`)`, "no ⋯ on touch, or actions already shown")
129 p.check(`(()=>{const m=document.querySelector('[equipo-receipt-more]');return m.getAttribute('aria-label')!=='' && m.getAttribute('aria-expanded')==='false'})()`, "⋯ has no accessible name or state")
130 p.check(`(()=>{window.__h=document.querySelector('[equipo-receipt]').getBoundingClientRect().height;document.querySelector('[equipo-receipt-more]').click();return true})()`, "tap ⋯")
131 p.run(browsertest.Wait(actionsShown))
132 p.check(`document.querySelector('[equipo-receipt-more]').getAttribute('aria-expanded')==='true' && Math.abs(document.querySelector('[equipo-receipt]').getBoundingClientRect().height-window.__h)<0.5`, "⋯ moved the line or kept its state")
133}
134
135// Astra 2b review 1: the owner opens Notebook from the conversation
136// header, sees each entry and where it came from, and Reset starts the
137// coordinator afresh: it moves in again and introduces itself.
138func TestBrowserNotebook(t *testing.T) {
139 _, inst, r := localEnv(t)
140 r.opts.NoMoveIn = false
141 r.start(t)
142 c := inst.Cookie()
143 p := openPage(t, inst.URL, c.Name, c.Value, 1280, 900, true)
144 defer p.stop()
145 p.run(browsertest.Wait(`[...document.querySelectorAll('[equipo-markdown]:not([data-stream-text])')].some(e=>e.textContent.includes("I'm your coordinator"))`))
146 p.check(`(()=>{const a=document.querySelector('[equipo-chat-header] a[href="/conversation/settings/notebook"]');if(!a)return false;a.click();return true})()`, "no Notebook link in the header")
147 p.run(browsertest.Wait(`location.pathname==='/conversation/settings/notebook' && [...document.querySelectorAll('[equipo-note]')].some(e=>e.textContent.includes('From oficina/equipo/README.md'))`))
148 // Reset saves in place, as every Settings change does: the notebook
149 // empties, "Saved", no navigation; the conversation then has the new
150 // coordinator's introduction.
151 p.check(`(()=>{window.__stay=1;document.querySelector('[data-notebook-reset]').click();return true})()`, "reset")
152 p.run(browsertest.Wait(`window.__stay===1 && location.pathname==='/conversation/settings/notebook' && !document.querySelector('[equipo-note]') && !!document.querySelector('#notebook [equipo-saved-tick][data-shown]')`))
153 p.run(chromedp.Navigate(inst.URL + "/conversation"))
154 p.run(browsertest.Wait(`[...document.querySelectorAll('[equipo-markdown]:not([data-stream-text])')].filter(e=>e.textContent.includes("I'm your coordinator")).length===2`))
155}
156
157func jsonNumber(f float64) string { return strconv.FormatFloat(f, 'f', -1, 64) }
158
159// Astra 2b minor: an explore turn that ends with nothing to say leaves no
160// thinking bubble behind.
161func TestBrowserEmptyExplore(t *testing.T) {
162 _, inst, r := localEnv(t)
163 r.opts.NoMoveIn = false
164 r.opts.Claude.MoveInNote = "\nmovein-pause:400"
165 r.start(t)
166 c := inst.Cookie()
167 p := openPage(t, inst.URL, c.Name, c.Value, 1280, 900, true)
168 defer p.stop()
169 p.run(browsertest.Wait(`[...document.querySelectorAll('[equipo-markdown]:not([data-stream-text])')].some(e=>e.textContent.includes("I'm your coordinator"))`))
170 // The runner lost its record of moving in but explored this access
171 // already: the instance makes it an explore, which has nothing to say.
172 r.stop(t)
173 desk, err := thin.OpenDesk(thin.SessionRoot(r.cfg), thintest.Owner)
174 if err != nil {
175 t.Fatal(err)
176 }
177 if err := os.WriteFile(filepath.Join(filepath.Dir(desk.Dir), "desk.json"), []byte(`{"moved_in":false,"namespaces":["oficina"],"next_id":50}`), 0600); err != nil {
178 t.Fatal(err)
179 }
180 p.check(`(()=>{window.__bubble=false;setInterval(()=>{if(document.querySelector('[equipo-thinking]'))window.__bubble=true},10);return true})()`, "watch")
181 r.start(t)
182 p.run(browsertest.Wait(`window.__bubble===true`))
183 p.run(browsertest.Wait(`!document.querySelector('[equipo-thinking]') && !document.querySelector('[data-ended]')`))
184 p.check(`[...document.querySelectorAll('[equipo-markdown]:not([data-stream-text])')].filter(e=>e.textContent.includes("I'm your coordinator")).length===1`, "a second introduction")
185}
186