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 thintest
4
5import (
6 "context"
7 "os"
8 "path/filepath"
9 "strconv"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/equipo"
14 "amadan.net/oficina/equipo/internal/wire"
15 "github.com/chromedp/chromedp"
16)
17
18// The Settings UX pass (Paul, 2026-11-11: "Can we make sure all the
19// settings screens have proper UX and aren't just random forms? The
20// "Save" button should probably be fixed to the bottom of the screen,
21// bottom right?").
22
23// settingsShot writes docs/mockups/built/2026-11-11/settings-<name>-<width>.png
24// when EQUIPO_CONN_SHOTS is set, as connShot does for Connections.
25func settingsShot(t *testing.T, p *innerPage, name string, width int64) {
26 t.Helper()
27 if os.Getenv("EQUIPO_CONN_SHOTS") == "" {
28 return
29 }
30 dir := filepath.Join("..", "..", "docs", "mockups", "built", "2026-11-11")
31 if err := os.MkdirAll(dir, 0755); err != nil {
32 t.Fatal(err)
33 }
34 p.wait(`document.getAnimations().every(a=>a.playState!=='running'||a.effect&&a.effect.getTiming().iterations===Infinity)`, "the page settled")
35 var shot []byte
36 p.do(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
37 var err error
38 shot, err = chromedp.FullScreenshot(90)(ctx, target)
39 return err
40 }))
41 if err := os.WriteFile(filepath.Join(dir, "settings-"+name+"-"+strconv.FormatInt(width, 10)+".png"), shot, 0644); err != nil {
42 t.Fatal(err)
43 }
44}
45
46var settingsSizes = []struct {
47 w, h int64
48 touch bool
49}{{1280, 900, false}, {390, 844, true}}
50
51// setField changes a field the way a person does: its value, then the
52// change event.
53func setField(selector, value string) string {
54 return `(()=>{const e=document.querySelector(` + jsString(selector) + `);e.value=` + jsString(value) + `;e.dispatchEvent(new Event('change',{bubbles:true}));return true})()`
55}
56
57// leaveWarns is whether leaving now would ask first.
58const leaveWarns = `(()=>{const e=new Event('beforeunload',{cancelable:true});dispatchEvent(e);return e.defaultPrevented})()`
59
60// TestBrowserSettingsSaveBar: on a screen with editable fields Save sits
61// in a bar fixed to the foot of the screen, on the right where the cards
62// end (the whole width on a phone); it is off until something changes,
63// then says Unsaved changes with Discard beside it, and leaving asks
64// first. Discard goes back; Save saves in place, says Saved, and the
65// page is never reloaded. Quiet hours' times are off while the switch
66// is, also when the page is opened again.
67func TestBrowserSettingsSaveBar(t *testing.T) {
68 r := newToolRig(t)
69 for _, size := range settingsSizes {
70 p := openInner(t, r.inst.URL, r.inst.Cookie(), size.w, size.h, size.touch, "light", "no-preference")
71 w := strconv.FormatInt(size.w, 10)
72 p.open("/conversation/settings/housekeeping")
73 p.wait(`!!document.querySelector('[data-savebar-save]')`, "the savebar")
74 // The bar: fixed at the foot, Save on its right under the cards' edge.
75 p.check(`(()=>{const b=document.querySelector('[data-savebar]').getBoundingClientRect(),s=document.querySelector('[data-savebar-save]').getBoundingClientRect(),card=document.querySelector('#hk-quiet [equipo-sp-card]').getBoundingClientRect();const pos=getComputedStyle(document.querySelector('[data-savebar]')).position;const rail=document.querySelector('[rst-shell-rail]').getBoundingClientRect();return pos==='fixed'&&Math.abs(b.bottom-innerHeight)<1&&Math.abs(s.right-card.right)<=2&&(innerWidth<800?Math.abs(b.left)<1&&Math.abs(b.width-innerWidth)<1:Math.abs(b.left-rail.right)<=1)})()`, w+": the savebar is not fixed at the foot with Save under the cards' right edge")
76 p.check(`document.querySelector('[data-savebar-save]').disabled && document.querySelector('[data-savebar-unsaved]').hidden && document.querySelector('[data-savebar-discard]').hidden`, w+": Save is on, or Unsaved changes shows, before any change")
77 p.check(`!`+leaveWarns, w+": leaving warns with nothing changed")
78 settingsShot(t, p, "housekeeping", size.w)
79 p.check(`(()=>{window.__nav=performance.getEntriesByType('navigation').length;window.__href=location.href;return true})()`, "mark")
80
81 // The switch off: the hours go off with it, and the bar wakes.
82 p.click(`#hk-quiet [rst-switch]`)
83 p.wait(`!document.querySelector('[data-hk-quiet-on]').checked`, "switched off")
84 p.check(`document.querySelector('#hk-hours').disabled && document.querySelector('[data-hk-quiet-from]').matches(':disabled') && document.querySelector('[data-hk-quiet-until]').matches(':disabled')`, w+": the hours are not off with the switch")
85 p.check(`!document.querySelector('[data-savebar-save]').disabled && !document.querySelector('[data-savebar-unsaved]').hidden && document.querySelector('[data-savebar-unsaved]').checkVisibility() && document.querySelector('[data-savebar-discard]').checkVisibility()`, w+": no Unsaved changes, Discard and Save after a change")
86 p.check(leaveWarns, w+": leaving with unsaved changes does not ask")
87 settingsShot(t, p, "housekeeping-unsaved", size.w)
88
89 // Discard: back to what was saved, nothing to warn about.
90 p.click(`[data-savebar-discard]`)
91 p.wait(`document.querySelector('[data-hk-quiet-on]').checked && !document.querySelector('#hk-hours').disabled && document.querySelector('[data-savebar-save]').disabled && document.querySelector('[data-savebar-unsaved]').hidden`, "discarded")
92 p.check(`!`+leaveWarns, w+": leaving warns after Discard")
93
94 // Save: new hours, in place.
95 from := map[int64]string{1280: "20", 390: "19"}[size.w]
96 p.check(setField(`[data-hk-quiet-from]`, from), "From")
97 p.check(`!document.querySelector('[data-savebar-save]').disabled`, w+": Save stays off after a change")
98 p.click(`[data-savebar-save]`)
99 p.wait(`!document.querySelector('[data-savebar-saved]').hidden && document.querySelector('[data-savebar-save]').disabled && document.querySelector('[data-savebar-unsaved]').hidden`, "Saved in the bar")
100 p.stayed(w + ": Save")
101 p.check(`!`+leaveWarns, w+": leaving warns after Save")
102 want, _ := strconv.Atoi(from)
103 p.waitFor(func() bool { return r.housekeeping().Quiet == wire.QuietHours{From: want, Until: 8} }, "the hours saved")
104 settingsShot(t, p, "housekeeping-saved", size.w)
105 // What Discard goes back to is now what was saved.
106 p.check(setField(`[data-hk-quiet-until]`, "9"), "Until")
107 p.click(`[data-savebar-discard]`)
108 p.wait(`document.querySelector('[data-hk-quiet-until]').value==='8' && document.querySelector('[data-hk-quiet-from]').value===`+jsString(from), "discarded to the saved hours")
109
110 // Quiet hours off, saved: opened again, the times are off.
111 p.click(`#hk-quiet [rst-switch]`)
112 p.click(`[data-savebar-save]`)
113 p.waitFor(func() bool { q := r.housekeeping().Quiet; return q.From == q.Until }, "quiet hours off")
114 p.wait(`document.querySelector('[data-savebar-save]').disabled`, "saved")
115 p.open("/conversation/settings/housekeeping")
116 p.check(`!document.querySelector('[data-hk-quiet-on]').checked && document.querySelector('#hk-hours').disabled && document.querySelector('[data-savebar-save]').disabled`, w+": quiet hours off, but the times are on when the page opens")
117 settingsShot(t, p, "housekeeping-quiet-off", size.w)
118 // And on again for the next size.
119 p.click(`#hk-quiet [rst-switch]`)
120 p.click(`[data-savebar-save]`)
121 p.waitFor(func() bool { q := r.housekeeping().Quiet; return q.From != q.Until }, "quiet hours on")
122 p.wait(`document.querySelector('[data-savebar-save]').disabled`, "saved")
123 p.stop()
124 }
125}
126
127// TestBrowserSettingsHousekeepingGroups: Pause belongs with the state it
128// changes, in the Status group, and applies at once with a tick; Quiet
129// hours are their own group with the savebar. Every control sits on one
130// right edge (on a phone the hours drop under their label).
131func TestBrowserSettingsHousekeepingGroups(t *testing.T) {
132 r := newToolRig(t)
133 for _, size := range settingsSizes {
134 p := openInner(t, r.inst.URL, r.inst.Cookie(), size.w, size.h, size.touch, "light", "no-preference")
135 w := strconv.FormatInt(size.w, 10)
136 p.open("/conversation/settings/housekeeping")
137 p.check(`(()=>{const st=document.querySelector('[data-hk-status]');const row=st.querySelector('[equipo-sp-row]');const pause=row.querySelector('[data-hk-pause]');const t=row.querySelector('[equipo-sp-title]');return st.querySelector('h2').textContent==='Status'&&t.textContent.trim()==='Housekeeping is on'&&!!pause&&Math.abs(pause.getBoundingClientRect().top+pause.getBoundingClientRect().height/2-(row.getBoundingClientRect().top+row.getBoundingClientRect().height/2))<4&&!st.querySelector('[data-savebar]')})()`, w+": Pause is not beside Housekeeping is on in the Status group")
138 p.check(`(()=>{const q=document.querySelector('#hk-quiet');return q.querySelector('h2').textContent.startsWith('Quiet hours')&&!!q.querySelector('[data-savebar]')&&!q.querySelector('[data-hk-pause],[data-hk-resume]')})()`, w+": Quiet hours are not their own group with the savebar")
139 p.check(`(()=>{const e=s=>document.querySelector(s).getBoundingClientRect().right;const xs=[e('[data-hk-pause]'),e('#hk-quiet [rst-switch]')];if(innerWidth>=560)xs.push(e('[data-hk-quiet-until]'));return Math.max(...xs)-Math.min(...xs)<=2})()`, w+": the controls are not on one right edge")
140 p.check(`(()=>{const a=document.querySelector('[equipo-sp-head] h1'),b=document.querySelector('#hk-status h2'),c=document.querySelector('#hk-quiet h2');return a.getBoundingClientRect().left===b.getBoundingClientRect().left&&b.getBoundingClientRect().left===c.getBoundingClientRect().left})()`, w+": the headings do not share a left edge")
141 p.fits("housekeeping")
142 p.check(`(()=>{window.__nav=performance.getEntriesByType('navigation').length;window.__href=location.href;return true})()`, "mark")
143 // Unsaved hours survive a Pause, and save over the new revision.
144 p.check(setField(`[data-hk-quiet-until]`, "7"), "Until")
145 p.click(`[data-hk-pause]`)
146 p.wait(`!!document.querySelector('[data-hk-status][data-paused] [data-hk-resume]') && !!document.querySelector('#hk-status [equipo-saved-tick][data-shown]:not([data-gone])')`, "paused at once")
147 p.check(`document.querySelector('#hk-status [equipo-sp-title]').textContent.trim()==='Housekeeping is paused' && document.querySelector('[data-hk-quiet-until]').value==='7' && !document.querySelector('[data-savebar-save]').disabled`, w+": Pause lost the unsaved hours or did not say paused")
148 p.stayed(w + ": Pause")
149 settingsShot(t, p, "housekeeping-paused", size.w)
150 p.click(`[data-savebar-save]`)
151 p.waitFor(func() bool { h := r.housekeeping(); return h.Paused && h.Quiet.Until == 7 }, "the hours saved after Pause")
152 p.click(`[data-hk-resume]`)
153 p.wait(`!!document.querySelector('[data-hk-status]:not([data-paused]) [data-hk-pause]')`, "resumed")
154 p.waitFor(func() bool { return !r.housekeeping().Paused }, "resumed on the server")
155 p.check(setField(`[data-hk-quiet-until]`, "8"), "Until back")
156 p.click(`[data-savebar-save]`)
157 p.waitFor(func() bool { return r.housekeeping().Quiet.Until == 8 }, "Until back saved")
158 p.stop()
159 }
160}
161
162// TestBrowserSettingsTeams: Teams is a Settings section like the others.
163// The current team is one header row; remembered teams are a list whose
164// rows open the team, "Last signed in" words with the time on hover;
165// Forget is quiet, with Undo before it takes; Add a team is a button that
166// opens a compact form, the address first and the name from it.
167func TestBrowserSettingsTeams(t *testing.T) {
168 r := newToolRig(t)
169 owner := equipo.Actor{Address: Owner}
170 for i, size := range settingsSizes {
171 studio, err := r.inst.Store.RememberTeam(r.ctx, owner, equipo.TeamLink{Origin: "https://studio" + strconv.Itoa(i) + ".example.com", Name: "Studio"}, equipo.Mutation{RequestID: "teams-ux-" + strconv.Itoa(i)})
172 if err != nil {
173 t.Fatal(err)
174 }
175 p := openInner(t, r.inst.URL, r.inst.Cookie(), size.w, size.h, size.touch, "light", "no-preference")
176 w := strconv.FormatInt(size.w, 10)
177 p.open("/teams")
178 p.check(`(()=>{const h=document.querySelector('[equipo-sp-head]');return h.querySelector('h1').textContent==='Teams'&&h.querySelectorAll('p').length===1&&!!document.querySelector('[equipo-current-team][href="/work"]')})()`, w+": Teams has no Settings head or current team row")
179 if size.w > 800 {
180 p.check(`document.querySelector('[equipo-settings-rail]').checkVisibility() && document.querySelector('[equipo-settings-rail] [aria-current="page"]').dataset.railSettings==='teams'`, "1280: Teams is not a section in the Settings rail")
181 }
182 row := `[data-team-row][data-team-name="Studio"]`
183 p.check(`(()=>{const row=document.querySelector(`+jsString(row)+`);const a=row.querySelector('[equipo-row-link]');const r=row.getBoundingClientRect();const hit=document.elementFromPoint(r.left+r.width*0.45,r.bottom-6);return a.getAttribute('href')==='https://studio`+strconv.Itoa(i)+`.example.com'&&(hit===a||a.contains(hit))&&getComputedStyle(row.querySelector('[data-team-forget]')).backgroundColor!==getComputedStyle(document.querySelector('[data-team-add] > summary')).backgroundColor})()`, w+": the row does not open the team, or Forget is not quiet")
184 p.check(`(()=>{const add=document.querySelector('[data-team-add]');return !add.open&&!add.querySelector('[data-team-origin]').checkVisibility()})()`, w+": the add form shows before Add a team")
185 p.fits("teams")
186 settingsShot(t, p, "teams", size.w)
187 p.check(`(()=>{window.__nav=performance.getEntriesByType('navigation').length;window.__href=location.href;return true})()`, "mark")
188
189 // Forget, then Undo: the row back, the link kept.
190 p.click(row + ` [data-team-forget]`)
191 p.wait(`document.querySelector(`+jsString(row)+`).hidden && document.querySelector('[data-team-forgotten]').textContent.includes('Studio forgotten.') && document.activeElement===document.querySelector('[data-team-undo]')`, "forgotten, with Undo")
192 settingsShot(t, p, "teams-undo", size.w)
193 p.click(`[data-team-undo]`)
194 p.wait(`!document.querySelector(`+jsString(row)+`).hidden && !document.querySelector('[data-team-forgotten]')`, "undone")
195 links, err := r.inst.Store.TeamLinks(r.ctx, owner)
196 if err != nil || !hasLink(links, studio.ID) {
197 t.Fatalf("%s: Undo did not keep the link: %v %v", w, links, err)
198 }
199 // Forget for good: it goes after the wait.
200 p.click(row + ` [data-team-forget]`)
201 p.waitForLong(func() bool {
202 l, err := r.inst.Store.TeamLinks(r.ctx, owner)
203 return err == nil && !hasLink(l, studio.ID)
204 }, "forgotten for good")
205 p.wait(`!document.querySelector(`+jsString(row)+`) && !document.querySelector('[data-team-forgotten]') && document.querySelector('[data-empty-state="teams.none"]').textContent==='No other teams yet.'`, "the row gone, none left")
206 p.stayed(w + ": Forget")
207
208 // Add a team: compact, address first, the name from it.
209 p.click(`[data-team-add] > summary`)
210 p.wait(`document.querySelector('[data-team-origin]').checkVisibility()`, "the add form")
211 p.check(`(()=>{const f=[...document.querySelectorAll('[equipo-team-add-form] input:not([type=hidden])')];const card=document.querySelector('[equipo-team-add-form]').getBoundingClientRect();return f[0].name==='origin'&&f[1].name==='name'&&(innerWidth<800||f.every(i=>i.getBoundingClientRect().width<card.width*0.75))})()`, w+": the address is not first, or the fields are full-bleed")
212 p.check(setField(`[data-team-origin]`, "https://design.example.org"), "address")
213 p.check(`document.querySelector('[data-team-add] [data-team-name]').value==='Design'`, w+": the name is not filled in from the address")
214 p.fits("the add form")
215 settingsShot(t, p, "teams-add", size.w)
216 p.click(`[data-team-remember]`)
217 p.wait(`location.pathname==='/teams' && !!document.querySelector('[data-team-row][data-team-name="Design"]')`, "the new team")
218 links, _ = r.inst.Store.TeamLinks(r.ctx, owner)
219 for _, l := range links {
220 if l.Name == "Design" {
221 if err := r.inst.Store.ForgetTeam(r.ctx, owner, l.ID, equipo.Mutation{RequestID: "teams-ux-design-" + w, Revision: l.Revision}); err != nil {
222 t.Fatal(err)
223 }
224 }
225 }
226 p.stop()
227 }
228}
229
230func hasLink(links []equipo.TeamLink, id string) bool {
231 for _, l := range links {
232 if l.ID == id {
233 return true
234 }
235 }
236 return false
237}
238
239// waitForLong polls a server-side condition for up to fifteen seconds.
240func (p *innerPage) waitForLong(cond func() bool, what string) {
241 p.t.Helper()
242 for range 3 {
243 for i := 0; i < 100; i++ {
244 if cond() {
245 return
246 }
247 time.Sleep(50 * time.Millisecond)
248 }
249 }
250 p.t.Fatalf("timed out waiting for %s", what)
251}
252
253// TestBrowserSettingsShots: every Settings screen at 1280 and 390, one
254// head and one content width, nothing wider than the screen, and the
255// after shots.
256func TestBrowserSettingsShots(t *testing.T) {
257 r := newToolRig(t)
258 if _, err := r.inst.Store.RememberTeam(r.ctx, equipo.Actor{Address: Owner}, equipo.TeamLink{Origin: "https://studio.example.com", Name: "Studio"}, equipo.Mutation{RequestID: "shots-team"}); err != nil {
259 t.Fatal(err)
260 }
261 for _, size := range settingsSizes {
262 p := openInner(t, r.inst.URL, r.inst.Cookie(), size.w, size.h, size.touch, "light", "no-preference")
263 var width float64
264 for _, s := range []struct{ name, path string }{{"thinking", "/conversation/settings/thinking"}, {"connections", "/conversation/settings/connections"}, {"connections-add", "/conversation/settings/connections/add"}, {"machines", "/conversation/settings/machines"}, {"machines-add", "/conversation/settings/machines/add"}, {"housekeeping", "/conversation/settings/housekeeping"}, {"notebook", "/conversation/settings/notebook"}, {"teams", "/teams"}} {
265 p.open(s.path)
266 p.check(`document.querySelectorAll('[equipo-settings-body] h1').length===1 && !!document.querySelector('[equipo-settings-body] [equipo-sp-head] h1') && getComputedStyle(document.querySelector('[equipo-sp-head] h1')).fontSize==='24px'`, s.name+": not one Settings head")
267 got := eval[float64](p, `document.querySelector('[equipo-settings-body]').getBoundingClientRect().width`)
268 if width == 0 {
269 width = got
270 } else if got != width {
271 t.Fatalf("%d %s: content width %v, the others %v", size.w, s.name, got, width)
272 }
273 p.fits(s.name)
274 if s.name != "housekeeping" && s.name != "teams" {
275 settingsShot(t, p, s.name, size.w)
276 }
277 }
278 p.stop()
279 }
280}
281