//go:build browser package thintest import ( "context" "os" "path/filepath" "strconv" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/wire" "github.com/chromedp/chromedp" ) // The Settings UX pass (Paul, 2026-11-11: "Can we make sure all the // settings screens have proper UX and aren't just random forms? The // "Save" button should probably be fixed to the bottom of the screen, // bottom right?"). // settingsShot writes docs/mockups/built/2026-11-11/settings--.png // when EQUIPO_CONN_SHOTS is set, as connShot does for Connections. func settingsShot(t *testing.T, p *innerPage, name string, width int64) { t.Helper() if os.Getenv("EQUIPO_CONN_SHOTS") == "" { return } dir := filepath.Join("..", "..", "docs", "mockups", "built", "2026-11-11") if err := os.MkdirAll(dir, 0755); err != nil { t.Fatal(err) } p.wait(`document.getAnimations().every(a=>a.playState!=='running'||a.effect&&a.effect.getTiming().iterations===Infinity)`, "the page settled") var shot []byte p.do(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { var err error shot, err = chromedp.FullScreenshot(90)(ctx, target) return err })) if err := os.WriteFile(filepath.Join(dir, "settings-"+name+"-"+strconv.FormatInt(width, 10)+".png"), shot, 0644); err != nil { t.Fatal(err) } } var settingsSizes = []struct { w, h int64 touch bool }{{1280, 900, false}, {390, 844, true}} // setField changes a field the way a person does: its value, then the // change event. func setField(selector, value string) string { return `(()=>{const e=document.querySelector(` + jsString(selector) + `);e.value=` + jsString(value) + `;e.dispatchEvent(new Event('change',{bubbles:true}));return true})()` } // leaveWarns is whether leaving now would ask first. const leaveWarns = `(()=>{const e=new Event('beforeunload',{cancelable:true});dispatchEvent(e);return e.defaultPrevented})()` // TestBrowserSettingsSaveBar: on a screen with editable fields Save sits // in a bar fixed to the foot of the screen, on the right where the cards // end (the whole width on a phone); it is off until something changes, // then says Unsaved changes with Discard beside it, and leaving asks // first. Discard goes back; Save saves in place, says Saved, and the // page is never reloaded. Quiet hours' times are off while the switch // is, also when the page is opened again. func TestBrowserSettingsSaveBar(t *testing.T) { r := newToolRig(t) for _, size := range settingsSizes { p := openInner(t, r.inst.URL, r.inst.Cookie(), size.w, size.h, size.touch, "light", "no-preference") w := strconv.FormatInt(size.w, 10) p.open("/conversation/settings/housekeeping") p.wait(`!!document.querySelector('[data-savebar-save]')`, "the savebar") // The bar: fixed at the foot, Save on its right under the cards' edge. 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") 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") p.check(`!`+leaveWarns, w+": leaving warns with nothing changed") settingsShot(t, p, "housekeeping", size.w) p.check(`(()=>{window.__nav=performance.getEntriesByType('navigation').length;window.__href=location.href;return true})()`, "mark") // The switch off: the hours go off with it, and the bar wakes. p.click(`#hk-quiet [rst-switch]`) p.wait(`!document.querySelector('[data-hk-quiet-on]').checked`, "switched off") 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") 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") p.check(leaveWarns, w+": leaving with unsaved changes does not ask") settingsShot(t, p, "housekeeping-unsaved", size.w) // Discard: back to what was saved, nothing to warn about. p.click(`[data-savebar-discard]`) 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") p.check(`!`+leaveWarns, w+": leaving warns after Discard") // Save: new hours, in place. from := map[int64]string{1280: "20", 390: "19"}[size.w] p.check(setField(`[data-hk-quiet-from]`, from), "From") p.check(`!document.querySelector('[data-savebar-save]').disabled`, w+": Save stays off after a change") p.click(`[data-savebar-save]`) p.wait(`!document.querySelector('[data-savebar-saved]').hidden && document.querySelector('[data-savebar-save]').disabled && document.querySelector('[data-savebar-unsaved]').hidden`, "Saved in the bar") p.stayed(w + ": Save") p.check(`!`+leaveWarns, w+": leaving warns after Save") want, _ := strconv.Atoi(from) p.waitFor(func() bool { return r.housekeeping().Quiet == wire.QuietHours{From: want, Until: 8} }, "the hours saved") settingsShot(t, p, "housekeeping-saved", size.w) // What Discard goes back to is now what was saved. p.check(setField(`[data-hk-quiet-until]`, "9"), "Until") p.click(`[data-savebar-discard]`) p.wait(`document.querySelector('[data-hk-quiet-until]').value==='8' && document.querySelector('[data-hk-quiet-from]').value===`+jsString(from), "discarded to the saved hours") // Quiet hours off, saved: opened again, the times are off. p.click(`#hk-quiet [rst-switch]`) p.click(`[data-savebar-save]`) p.waitFor(func() bool { q := r.housekeeping().Quiet; return q.From == q.Until }, "quiet hours off") p.wait(`document.querySelector('[data-savebar-save]').disabled`, "saved") p.open("/conversation/settings/housekeeping") 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") settingsShot(t, p, "housekeeping-quiet-off", size.w) // And on again for the next size. p.click(`#hk-quiet [rst-switch]`) p.click(`[data-savebar-save]`) p.waitFor(func() bool { q := r.housekeeping().Quiet; return q.From != q.Until }, "quiet hours on") p.wait(`document.querySelector('[data-savebar-save]').disabled`, "saved") p.stop() } } // TestBrowserSettingsHousekeepingGroups: Pause belongs with the state it // changes, in the Status group, and applies at once with a tick; Quiet // hours are their own group with the savebar. Every control sits on one // right edge (on a phone the hours drop under their label). func TestBrowserSettingsHousekeepingGroups(t *testing.T) { r := newToolRig(t) for _, size := range settingsSizes { p := openInner(t, r.inst.URL, r.inst.Cookie(), size.w, size.h, size.touch, "light", "no-preference") w := strconv.FormatInt(size.w, 10) p.open("/conversation/settings/housekeeping") 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") 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") 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") 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") p.fits("housekeeping") p.check(`(()=>{window.__nav=performance.getEntriesByType('navigation').length;window.__href=location.href;return true})()`, "mark") // Unsaved hours survive a Pause, and save over the new revision. p.check(setField(`[data-hk-quiet-until]`, "7"), "Until") p.click(`[data-hk-pause]`) 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") 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") p.stayed(w + ": Pause") settingsShot(t, p, "housekeeping-paused", size.w) p.click(`[data-savebar-save]`) p.waitFor(func() bool { h := r.housekeeping(); return h.Paused && h.Quiet.Until == 7 }, "the hours saved after Pause") p.click(`[data-hk-resume]`) p.wait(`!!document.querySelector('[data-hk-status]:not([data-paused]) [data-hk-pause]')`, "resumed") p.waitFor(func() bool { return !r.housekeeping().Paused }, "resumed on the server") p.check(setField(`[data-hk-quiet-until]`, "8"), "Until back") p.click(`[data-savebar-save]`) p.waitFor(func() bool { return r.housekeeping().Quiet.Until == 8 }, "Until back saved") p.stop() } } // TestBrowserSettingsTeams: Teams is a Settings section like the others. // The current team is one header row; remembered teams are a list whose // rows open the team, "Last signed in" words with the time on hover; // Forget is quiet, with Undo before it takes; Add a team is a button that // opens a compact form, the address first and the name from it. func TestBrowserSettingsTeams(t *testing.T) { r := newToolRig(t) owner := equipo.Actor{Address: Owner} for i, size := range settingsSizes { 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)}) if err != nil { t.Fatal(err) } p := openInner(t, r.inst.URL, r.inst.Cookie(), size.w, size.h, size.touch, "light", "no-preference") w := strconv.FormatInt(size.w, 10) p.open("/teams") 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") if size.w > 800 { 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") } row := `[data-team-row][data-team-name="Studio"]` 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") 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") p.fits("teams") settingsShot(t, p, "teams", size.w) p.check(`(()=>{window.__nav=performance.getEntriesByType('navigation').length;window.__href=location.href;return true})()`, "mark") // Forget, then Undo: the row back, the link kept. p.click(row + ` [data-team-forget]`) 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") settingsShot(t, p, "teams-undo", size.w) p.click(`[data-team-undo]`) p.wait(`!document.querySelector(`+jsString(row)+`).hidden && !document.querySelector('[data-team-forgotten]')`, "undone") links, err := r.inst.Store.TeamLinks(r.ctx, owner) if err != nil || !hasLink(links, studio.ID) { t.Fatalf("%s: Undo did not keep the link: %v %v", w, links, err) } // Forget for good: it goes after the wait. p.click(row + ` [data-team-forget]`) p.waitForLong(func() bool { l, err := r.inst.Store.TeamLinks(r.ctx, owner) return err == nil && !hasLink(l, studio.ID) }, "forgotten for good") 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") p.stayed(w + ": Forget") // Add a team: compact, address first, the name from it. p.click(`[data-team-add] > summary`) p.wait(`document.querySelector('[data-team-origin]').checkVisibility()`, "the add form") 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