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 "strconv"
11 "testing"
12 "time"
13
14 "amadan.net/oficina/equipo/internal/browsertest"
15 "amadan.net/oficina/equipo/internal/thintest"
16 "amadan.net/oficina/equipo/internal/wire"
17 "github.com/chromedp/chromedp"
18)
19
20// Connections slice 1, gate 1d: the pages a person uses, in a browser.
21
22// waitLong is browsertest.Wait with room for a turn that moves
23// connection (two cold starts) to arrive.
24func waitLong(expression string) chromedp.Action[chromedp.Void] {
25 return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
26 deadline := time.Now().Add(45 * time.Second)
27 for {
28 ok, err := chromedp.Evaluate[bool]("document.readyState === 'complete' && ("+expression+")")(ctx, target)
29 if err == nil && ok {
30 return nil
31 }
32 if time.Now().After(deadline) {
33 return fmt.Errorf("waiting for %s (last evaluation: %v)", expression, err)
34 }
35 time.Sleep(50 * time.Millisecond)
36 }
37 })
38}
39
40func connectionsPage(t *testing.T, inst *thintest.Instance) *page {
41 t.Helper()
42 cookie := inst.Cookie()
43 p := openPage(t, inst.URL, cookie.Name, cookie.Value, 390, 844, false)
44 t.Cleanup(p.stop)
45 return p
46}
47
48// connShot writes docs/mockups/built/2026-11-08/conn-<name>-<width>.png
49// when EQUIPO_CONN_SHOTS is set.
50func connShot(t *testing.T, p *page, name string, width int64) {
51 t.Helper()
52 connShotOn(t, p, "2026-11-08", name, width)
53}
54
55// connShotOn is connShot into docs/mockups/built/<day>/.
56func connShotOn(t *testing.T, p *page, day, name string, width int64) {
57 t.Helper()
58 if os.Getenv("EQUIPO_CONN_SHOTS") == "" {
59 return
60 }
61 dir := filepath.Join("..", "..", "docs", "mockups", "built", day)
62 if err := os.MkdirAll(dir, 0755); err != nil {
63 t.Fatal(err)
64 }
65 var shot []byte
66 p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
67 var err error
68 shot, err = chromedp.FullScreenshot(90)(ctx, target)
69 return err
70 }))
71 if err := os.WriteFile(filepath.Join(dir, "conn-"+name+"-"+strconv.FormatInt(width, 10)+".png"), shot, 0644); err != nil {
72 t.Fatal(err)
73 }
74}
75
76// stayed is true while the page is the one first loaded: a reload or a
77// navigation loses the mark.
78const stayed = `window.__equipoStayed === true && performance.getEntriesByType('navigation').length === 1`
79
80// A1 and A4: Add Codex shows its card at once (a loading line, then the
81// three steps with the code once), Copy copies, and the approval lands in
82// place, with no reload; Add Claude shows the link the runner's own
83// setup-token printed, and the code field.
84func TestBrowserConnectionsAdd(t *testing.T) {
85 for _, width := range []int64{390, 1280} {
86 t.Run(strconv.FormatInt(width, 10), func(t *testing.T) {
87 _, inst, r := localEnv(t)
88 r.start(t)
89 c := inst.Cookie()
90 p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600)
91 t.Cleanup(p.stop)
92 p.run(chromedp.Navigate(inst.URL+"/conversation/settings/connections/add"), browsertest.Wait(`!!document.querySelector('[data-connections]')`))
93 p.check(`(()=>{window.__equipoStayed=true;return true})()`, "mark")
94 p.check(`!document.querySelector('meta[http-equiv="refresh"]')`, "the page refreshes itself")
95 // The chooser: Claude, Codex and TensorX live, two more greyed.
96 p.check(`(()=>{const c=document.querySelector('[equipo-conn-chooser]');return !!c && c.checkVisibility() && !document.querySelector('[data-add="codex"]').disabled && !document.querySelector('[data-add="claude"]').disabled && c.querySelectorAll('[equipo-conn-opt][disabled]').length===2 && !!c.querySelector('[data-add-metered="tensorx"]')})()`, "the chooser is not Claude, Codex and TensorX with two greyed")
97 p.check(`(()=>{document.querySelector('[data-add="codex"]').click();return !!document.querySelector('[data-connecting][data-provider="codex"]')})()`, "no card the instant Codex is chosen")
98 p.run(browsertest.Wait(`!!document.querySelector('[data-device-code]')`))
99 p.check(`(()=>{const card=document.querySelector('[data-connecting][data-provider="codex"]');const code=card.querySelector('[data-code]').textContent;const open=card.querySelector('[data-open-provider]');return /^FAKE-/.test(code) && card.textContent.split(code).length===2 && open.getAttribute('href')==='https://auth.openai.com/codex/device' && open.target==='_blank' && !card.textContent.includes('auth.openai.com') && card.querySelectorAll('ol > li').length===3 && card.querySelector('h3').textContent==='Connect Codex'})()`, "the device code card is not the three steps with the code once")
100 connShot(t, p, "codex-code", width)
101 p.check(`(()=>{document.querySelector('[data-device-code] [data-copy]:not([data-copy-link])').click();return true})()`, "Copy")
102 p.run(browsertest.Wait(`document.querySelector('[data-device-code] [data-copy]:not([data-copy-link])').textContent==='Copied'`))
103 // The approval lands with the success card, then Connections
104 // with the new row.
105 p.check(stayed, "the page reloaded or navigated while the code was shown")
106 writeJSON(t, filepath.Join(r.root, "accounts", ".fake-device.json"), map[string]any{"email": second, "plan": "pro"})
107 p.run(waitLong(`location.pathname==='/conversation/settings/connections' && [...document.querySelectorAll('[equipo-conn-row][data-provider="codex"]')].some(e=>!e.hidden && e.querySelector('[data-connection-state]').textContent==='Ready' && e.textContent.includes('` + second + `'))`))
108 connShot(t, p, "codex-connected", width)
109 // Prefer Codex: pressed at once, and still after the stream's refresh.
110 p.check(`(()=>{document.querySelector('[data-prefer="codex"]').click();return document.querySelector('[data-prefer="codex"]').getAttribute('aria-pressed')==='true'})()`, "Prefer Codex")
111 p.run(waitLong(`document.querySelector('[data-prefer="codex"]').getAttribute('aria-pressed')==='true' && !!document.querySelector('[equipo-conn-row][data-provider="codex"] [equipo-conn-pref]')`))
112 // Claude: the real runner starts its own sign-in (the fake
113 // setup-token) and the card shows the link it printed.
114 p.check(`(()=>{document.querySelector('[data-add-link]').click();return true})()`, "Add a connection")
115 p.run(waitLong(`location.pathname==='/conversation/settings/connections/add' && !!document.querySelector('[equipo-conn-chooser] [data-add="claude"]')`))
116 p.check(`(()=>{window.__equipoStayed=true;document.querySelector('[data-add="claude"]').click();return !!document.querySelector('[data-connecting][data-provider="claude"]')})()`, "no card the instant Claude is chosen")
117 p.run(waitLong(`!!document.querySelector('[data-code-steps]')`))
118 p.check(`(()=>{const open=document.querySelector('[data-code-steps] [data-open-anthropic]');const u=new URL(open.href);return u.host==='claude.com' && u.searchParams.get('redirect_uri')==='`+wire.EndpointClaudeCode+`' && open.target==='_blank' && !!document.querySelector('[data-code-field]') && !document.querySelector('[data-finish-on],[data-same-machine]')})()`, "the sign-in card is not the runner's link with the code field")
119 connShot(t, p, "claude-sign-in", width)
120 p.check(`(()=>{document.querySelector('[data-connecting][data-provider="claude"] [data-connection-cancel]').click();return true})()`, "Cancel")
121 p.run(waitLong(`!document.querySelector('[data-connecting]') && !!document.querySelector('[equipo-conn-chooser]')`))
122 p.check(stayed, "the page reloaded or navigated")
123 p.run(chromedp.Navigate(inst.URL+"/conversation/settings/connections"), browsertest.Wait(`!!document.querySelector('[equipo-conn-row]')`))
124 p.check(`(()=>{window.__equipoStayed=true;return true})()`, "mark")
125 // Signed out: one clear Connect again in the row's action column,
126 // full width under the state line at 390, the usage under it.
127 p.check(`(()=>{document.querySelector('[equipo-conn-row][data-provider="codex"] [data-sign-out]').click();return true})()`, "Sign out")
128 p.run(waitLong(`!!document.querySelector('[equipo-conn-row][data-provider="codex"][data-state="signed_out"] [data-recover]')`))
129 p.check(`(()=>{const row=document.querySelector('[equipo-conn-row][data-state="signed_out"]');const b=row.querySelector('[data-recover]').getBoundingClientRect(),u=row.querySelector('[equipo-conn-usage]').getBoundingClientRect(),r=row.getBoundingClientRect();const visible=getComputedStyle(row.querySelector('[data-recover]')).opacity==='1';return visible && b.bottom<=u.top+1 && (`+strconv.FormatBool(width < 600)+` ? b.width>r.width*0.7 : Math.abs(b.right-u.right)<2)})()`, "the recovery button is not in the action column with the usage under it")
130 connShot(t, p, "signed-out", width)
131 p.check(stayed, "the page reloaded or navigated")
132 })
133 }
134}
135
136// Paul, 2026-11-08: turns that wait for a connection show no thinking
137// line and no timer, live or after a reload: a quiet status under each
138// waiting message and one card for the conversation; once a connection
139// is added they run, in order.
140func TestBrowserWaitingTurns(t *testing.T) {
141 env, inst, r := localEnv(t)
142 r.start(t)
143 c := inst.Cookie()
144 const quiet = `document.querySelectorAll('[equipo-thinking]').length===0 && document.querySelectorAll('[data-no-connection]').length===1 && document.querySelectorAll('[data-waiting-connection]').length===%d && [...document.querySelectorAll('[data-waiting-connection]')].every(e=>e.textContent==='Waiting for a connection') && !document.body.textContent.includes('Thinking')`
145 p := openPage(t, inst.URL, c.Name, c.Value, 1280, 900, true)
146 t.Cleanup(p.stop)
147 p.send("Can we work on the shop? limit:" + thintest.Owner)
148 p.run(waitLong(fmt.Sprintf(quiet, 1)))
149 p.send("And the docs after that.")
150 p.run(waitLong(fmt.Sprintf(quiet, 2)))
151 // The card stands after the last waiting message.
152 p.check(`(()=>{const own=[...document.querySelectorAll('[equipo-message="own"]')];return !!(own[own.length-1].compareDocumentPosition(document.querySelector('[data-no-connection]'))&4)})()`, "the card is not after the last waiting message")
153 time.Sleep(1500 * time.Millisecond)
154 p.check(fmt.Sprintf(quiet, 2), "a thinking line or a timer came back")
155 p.run(chromedp.Reload(), browsertest.Wait(`!!document.querySelector('[data-chat-stream]')`))
156 p.check(fmt.Sprintf(quiet, 2), "after a reload, a waiting turn shows a thinking line, or not one card")
157 connShot(t, p, "waiting", 1280)
158 small := openPage(t, inst.URL, c.Name, c.Value, 390, 844, false)
159 t.Cleanup(small.stop)
160 small.check(fmt.Sprintf(quiet, 2), "at 390, a waiting turn shows a thinking line, or not one card")
161 connShot(t, small, "waiting", 390)
162 // A connection arrives: both turns start on their own, in order.
163 writeJSON(t, filepath.Join(r.root, "accounts", ".fake-device.json"), map[string]any{"email": "third@example.com", "plan": "pro"})
164 if _, err := env.Client.Connect(t.Context(), wire.Connect{Provider: "codex"}); err != nil {
165 t.Fatal(err)
166 }
167 p.run(waitLong(`!document.querySelector('[data-no-connection]') && !document.querySelector('[data-waiting-connection]') && document.querySelectorAll('[equipo-message="incoming"]:not([equipo-thinking])').length>=2 && document.querySelectorAll('[equipo-thinking]').length===0`))
168 p.check(`(()=>{const own=[...document.querySelectorAll('[equipo-message="own"]')].map(e=>e.dataset.turnId);const replies=[...document.querySelectorAll('[equipo-message="incoming"]:not([equipo-thinking])')].map(e=>e.dataset.turnId).filter(id=>own.includes(id));return replies.length===2 && replies[0]===own[0] && replies[1]===own[1]})()`, "the waiting turns did not answer in order")
169}
170
171// F1 and F7 on the conversation page: the switched line under a reply
172// that moved connection, and the waiting card that turns into the reply
173// once a connection is added, without a reload.
174func TestBrowserConnectionsConversation(t *testing.T) {
175 env, inst, r := localEnv(t)
176 r.addAccount(t, "claude", "claude-2", second)
177 r.start(t)
178 p := connectionsPage(t, inst)
179 p.send("Hello. limit:" + thintest.Owner)
180 p.run(waitLong(`!!document.querySelector('[data-switched]') && document.querySelector('[data-switched]').textContent.startsWith('Switched to Claude')`))
181 // F7: claude-2 limited too; nothing can answer.
182 p.send("Again. limit:" + second)
183 p.run(waitLong(`!!document.querySelector('[data-no-connection]') && document.querySelector('[data-no-connection]').textContent.includes('Nothing can answer right now.') && !!document.querySelector('[data-add-connection]')`))
184 p.check(`!document.body.textContent.includes('Send your message again')`, "the old retry line is shown")
185 writeJSON(t, filepath.Join(r.root, "accounts", ".fake-device.json"), map[string]any{"email": "third@example.com", "plan": "pro"})
186 ctx := t.Context()
187 c, err := env.Client.Connect(ctx, wire.Connect{Provider: "codex"})
188 if err != nil {
189 t.Fatal(err)
190 }
191 _ = c
192 p.run(waitLong(`!document.querySelector('[data-no-connection]') && [...document.querySelectorAll('[data-switched]')].some(e=>e.textContent.startsWith('Switched to Codex'))`))
193 var _ = time.Second
194}
195