//go:build browser package thintest import ( "amadan.net/oficina/equipo/internal/wire" "fmt" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" "strings" "testing" ) func TestBrowserRosterVisual(t *testing.T) { r := newRollupRig(t) for _, width := range []int64{1280, 390} { for _, scheme := range []string{"light", "dark"} { t.Run(fmt.Sprintf("%d-%s", width, scheme), func(t *testing.T) { p := openInner(t, r.inst.URL, r.inst.Cookie(), width, 844, width == 390, scheme, "reduce") defer p.stop() p.open("/conversation") p.wait(`document.querySelector('[equipo-offline]').hidden`, "connected") p.check(`!!document.querySelector('[equipo-roster-byline]')`, "roster bylines missing") // A report beside its decision card is one line; with the card // set aside it shows its track, facts and actions. p.check(`document.querySelector('[equipo-roster-report]').hasAttribute('data-compact') || !!document.querySelector('[equipo-card="merge"] [equipo-aside-stub]')`, "report repeats its decision card") if eval[bool](p, `!!document.querySelector('[equipo-card="merge"] [data-set-aside]')`) { p.click(`[equipo-card="merge"] [data-set-aside]`) } p.wait(`document.querySelector('[equipo-roster-report] [data-track]')`, "full report") p.check(`[...document.querySelectorAll("[equipo-daybar] i")].every(i=>i.getBoundingClientRect().width>0) && !document.querySelector("[equipo-daybar] [style]")`, "roll-up bar violates the style policy") p.check(`document.querySelector('[equipo-roster-report] [data-track]').children.length===5`, "report needs five stages") p.check(`!!document.querySelector('[equipo-roster-facts]')`, "report facts missing") p.check(`(()=>{const r=document.querySelector("[equipo-roster-report]"),c=r.closest("[equipo-roster-card]");return c.getBoundingClientRect().bottom-r.getBoundingClientRect().bottom<20})()`, "hidden task actions reserve empty space") p.check(`(()=>{const s=getComputedStyle(document.querySelector('[equipo-coordinator-mark]'));return s.backgroundColor==='rgba(0, 0, 0, 0)' && s.boxShadow==='none'})()`, "coordinator has a tile") p.check(`(()=>{const r=document.querySelector('[equipo-roster-report]');return !r.closest('[data-card]').querySelector('[equipo-receipt-more]') && !r.querySelector('[data-pushed]') && !r.querySelector('[equipo-roster-links] a').hasAttribute('rst-btn') && !!r.querySelector('[data-report-next]') && [...r.querySelectorAll('[equipo-roster-links] a')].every(a=>getComputedStyle(a).textDecorationLine==='none')})()`, "report actions drifted") p.check(`document.querySelector('[equipo-roster-report] [aria-current]').dataset.tone==='warning'`, "waiting for you looks failed") p.check(`[...document.querySelectorAll('[equipo-roster-facts] small')].every(e=>getComputedStyle(e).whiteSpace==='normal' && e.scrollWidth<=e.clientWidth+1)`, "fact details truncate") p.check(`(()=>{const c=document.querySelector('[equipo-roster-byline] b'),m=document.querySelector('[equipo-message="incoming"] [equipo-message-meta] strong');return !m||getComputedStyle(c).fontSize===getComputedStyle(m).fontSize})()`, "byline sizes differ") p.check(`[...document.querySelectorAll('[equipo-card="task"], [equipo-card="merge"]')].every(c=>c.querySelector('[equipo-roster-project]')?.textContent.includes('Sample') && c.querySelector('[equipo-roster-project] [equipo-mono]'))`, "task and coordinator cards lack project context") p.check(`(()=>{const r=document.querySelector('[equipo-roster-report]'),c=r.closest('[equipo-roster-card]'),b=c.previousElementSibling;return parseFloat(getComputedStyle(c).paddingTop)===0 && parseFloat(getComputedStyle(b).marginBottom)===8 && parseFloat(getComputedStyle(r).rowGap)===12 && parseFloat(getComputedStyle(c.querySelector('[equipo-roster-facts]')).paddingTop)===12})()`, "report spacing left the inset scale") if width == 390 { p.check(`document.querySelector('[equipo-roster-links] [data-report-next]').getBoundingClientRect().height>=44`, "task button lost its phone target") p.check(`parseFloat(getComputedStyle(document.querySelector('[equipo-roster-report-head] h3 a'),'::after').height)>=44`, "task title has a small phone hit area") } p.fits("Roster") p.check(`(()=>{const s=document.querySelector('[equipo-scroll]');s.scrollTop=0;s.dispatchEvent(new Event('scroll'));return true})()`, "top") p.shot(fmt.Sprintf("roster-%d-%s", width, scheme)) p.check(`(()=>{document.querySelector('[equipo-card="merge"]').scrollIntoView({block:'center'});return true})()`, "show coordinator decision") p.shot(fmt.Sprintf("roster-question-%d-%s", width, scheme)) if width == 1280 { p.check(`(()=>{document.querySelector('[equipo-conversation]').style.inlineSize='440px';return true})()`, "narrow conversation container") p.check(`getComputedStyle(document.querySelector('[equipo-roster-avatar]')).width==='30px'`, "narrow container did not use phone roster") } }) } } } func TestBrowserRosterSetAside(t *testing.T) { for _, width := range []int64{1280, 390} { for _, scheme := range []string{"light", "dark"} { t.Run(fmt.Sprintf("%d-%s", width, scheme), func(t *testing.T) { r := newRollupRig(t) p := openInner(t, r.inst.URL, r.inst.Cookie(), width, 844, width == 390, scheme, "reduce") defer p.stop() p.open("/conversation") card := `[data-card="` + r.askCard.ID + `"]` p.wait(`document.querySelector('`+card+` [data-set-aside]')`, "Set aside") before := eval[string](p, `document.querySelector("[equipo-needs-pill]").textContent`) p.click(card + ` [data-set-aside]`) p.wait(`document.querySelector('`+card+` [equipo-aside-stub]')`, "persisted stub") p.check(`document.querySelector("[equipo-needs-pill]").textContent===`+jsString(before), "set aside changed Needs you count") p.wait(`document.querySelector('[equipo-roster-toast] [data-aside-undo]')`, "toast undo") p.check(`(()=>{const s=document.querySelector('[equipo-aside-stub]');return parseFloat(getComputedStyle(s).fontSize)<=14 && s.querySelector('[data-aside-title]') && s.getBoundingClientRect().height<=64})()`, "aside is a full question instead of a compact stub") p.check(`(()=>{const t=document.querySelector('[equipo-roster-toast]'),c=document.querySelector('#chat-composer');return t.getBoundingClientRect().left>=c.getBoundingClientRect().left && t.getBoundingClientRect().bottom<=c.getBoundingClientRect().bottom})()`, "toast outside composer column") p.check(`(()=>{const t=document.querySelector('[equipo-roster-toast]').getBoundingClientRect(),b=document.querySelector('[data-turn-stop]')?.getBoundingClientRect();return !b || (b.bottom<=t.top && b.bottom<=document.querySelector('[equipo-scroll]').getBoundingClientRect().bottom)})()`, "toast covers Stop") p.check(`document.querySelector('`+card+` [equipo-roster-project]')?.textContent.includes('Sample')`, "aside lost project context") if width == 390 { p.check(`document.querySelector('[equipo-aside-stub] button').getBoundingClientRect().width>=44`, "Undo has a narrow phone target") } p.shot(fmt.Sprintf("roster-aside-%d-%s", width, scheme)) p.open("/conversation") p.wait(`document.querySelector('`+card+` [equipo-aside-stub]')`, "stub after reload") p.open("/tasks?needs=you") p.check(`!!document.querySelector('a[href="/tasks/`+r.asked+`"]')`, "set-aside task left Needs you") p.open("/conversation") p.click(card + ` [data-aside-undo]`) p.wait(`document.querySelector('`+card+` [data-set-aside]')`, "restored card") p.open("/conversation") p.check(`!document.querySelector('`+card+` [equipo-aside-stub]')`, "undo did not persist") p.fits("set aside and undo") p.click(card + ` [data-set-aside]`) p.wait(`document.querySelector('`+card+` [equipo-aside-stub]')`, "set aside again") if _, err := r.client.Answer(r.ctx, r.askCard.ID, wire.ChoiceWaitMac, r.askCard.Revision); err != nil { t.Fatal(err) } p.wait(`!document.querySelector('`+card+` [equipo-aside-stub]') && document.querySelector('`+card+` [equipo-card-done]')`, "answer from elsewhere resolves the stub") }) } } } func TestBrowserRosterSubchat(t *testing.T) { for _, width := range []int64{1280, 390} { for _, scheme := range []string{"light", "dark"} { t.Run(fmt.Sprintf("%d-%s", width, scheme), func(t *testing.T) { r := newToolRig(t) task := taskID(t, r.call("thread-task", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Improve sign-in", "brief": "Keep sign-in clear."})) if res := r.call("roster-q", wire.ToolAskPerson, map[string]any{"question": "Which page should change?", "options": []string{}}); res.Code != "asked" { t.Fatalf("ask: %+v", res) } if _, err := r.inst.DB.Writer().Exec("UPDATE chat_cards SET task_id=?,project_id=(SELECT project_id FROM chat_tasks WHERE id=?) WHERE kind='question'", task, task); err != nil { t.Fatal(err) } if err := r.runner.Send(r.ctx, "roster-q-reply", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "One question before I start."}); err != nil { t.Fatal(err) } r.wait("replied", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) p := openInner(t, r.inst.URL, r.inst.Cookie(), width, 844, width == 390, scheme, "reduce") defer p.stop() p.open("/conversation") p.wait(`document.querySelector('[data-question-thread] [equipo-mini-composer]')`, "question as a sub-chat") p.check(`document.querySelector('[data-question-thread] [equipo-thread-turn]').textContent.includes('Which page should change?')`, "first word is not a turn") p.check(`(()=>{const i=document.querySelector('[data-question-thread] [equipo-mini-composer] textarea');i.value='The sign-in page.';i.dispatchEvent(new Event('input',{bubbles:true}));i.focus();return true})()`, "type") p.shot(fmt.Sprintf("roster-thread-typing-%d-%s", width, scheme)) p.click(`[data-set-aside]`) p.wait(`document.querySelector('[equipo-aside-stub]')`, "question set aside") p.wait(`document.querySelector('[equipo-roster-toast] [data-aside-undo]')`, "question toast") p.click(`[equipo-roster-toast] [data-aside-undo]`) p.wait(`document.querySelector('[data-question-thread] [equipo-mini-composer] textarea')?.value==='The sign-in page.'`, "undo retains the draft") p.check(`(()=>{window.__fetch=window.fetch;window.fetch=(url,opts)=>opts?.method==='POST'?new Promise(resolve=>{window.__fail=()=>resolve(new Response('{}',{status:503}))}):window.__fetch(url,opts);return true})()`, "hold question send") p.click(`[data-question-thread] [equipo-mini-composer] [data-thread-send]`) p.check(`document.querySelector('[data-question-thread] [data-thread-send]').getAttribute('aria-busy')==='true'`, "question busy") p.shot(fmt.Sprintf("roster-thread-busy-%d-%s", width, scheme)) p.check(`(()=>{window.__fail();window.fetch=window.__fetch;return true})()`, "question send fails") p.wait(`document.querySelector('[data-question-thread]').parentElement.querySelector('[equipo-card-error]:not([hidden])')`, "question send error") p.shot(fmt.Sprintf("roster-thread-error-%d-%s", width, scheme)) p.click(`[data-question-thread] [equipo-mini-composer] [data-thread-send]`) f, body := r.expect(wire.FrameMessage) m := body.(*wire.MessageCommand) r.runner.Send(r.ctx, "roster-a", wire.FrameAck, wire.Ack{ID: f.ID}) p.wait(`document.querySelector('[data-question-thread] [data-own]') && document.querySelector('[equipo-thread-wait]')`, "sent and waiting") p.shot(fmt.Sprintf("roster-thread-waiting-%d-%s", width, scheme)) r.runner.Send(r.ctx, "roster-delta", wire.FrameDelta, wire.DeltaReport{TurnID: m.TurnID, Attempt: 1, Offset: 0, Text: "I’ll start"}) p.wait(`document.querySelector('[equipo-thread-draft]')?.textContent==='I’ll start'`, "streamed reply in the drawer") r.runner.Send(r.ctx, "roster-answer", wire.FrameReply, wire.Reply{TurnID: m.TurnID, Attempt: 1, Text: "I’ll start with the sign-in page."}) p.wait(`document.querySelector('[data-question-thread]').textContent.includes('I’ll start with the sign-in page.')`, "reply inside the sub-chat") p.open("/conversation") p.wait(`document.querySelector('[data-question-thread]').textContent.includes('I’ll start with the sign-in page.')`, "thread after reload") p.check(`!document.querySelector('[equipo-thread-turn][data-own] [equipo-thread-who]')`, "own thread turn repeats a byline") p.check(`parseFloat(getComputedStyle(document.querySelector('[data-first] [equipo-thread-bubble]')).fontWeight)<600`, "question is entirely title weight") p.check(`(()=>{const c=document.querySelector('[data-question-thread]').closest('[data-card]');return c.querySelector('[equipo-roster-project]')?.textContent.includes('Sample') && c.querySelector('[equipo-roster-task]')?.textContent==='Improve sign-in'})()`, "thread lost project or task context") p.fits("sub-chat") p.shot(fmt.Sprintf("roster-thread-answered-%d-%s", width, scheme)) p.check(`(()=>{const i=document.querySelector('[data-question-thread] [equipo-mini-composer] textarea');i.value='Keep the email link.';i.dispatchEvent(new Event('input',{bubbles:true}));return true})()`, "follow-up") p.click(`[data-question-thread] [equipo-mini-composer] [data-thread-send]`) f2, b2 := r.expect(wire.FrameMessage) m2 := b2.(*wire.MessageCommand) if !strings.Contains(m2.Text, "Which page should change?") || len(m2.PersonText) != 1 || m2.PersonText[0] != "Keep the email link." { t.Fatalf("follow-up context %+v", m2) } r.runner.Send(r.ctx, "roster-a2", wire.FrameAck, wire.Ack{ID: f2.ID}) r.runner.Send(r.ctx, "roster-answer2", wire.FrameReply, wire.Reply{TurnID: m2.TurnID, Attempt: 1, Text: "The email link will stay."}) p.wait(`document.querySelector('[data-question-thread]').textContent.includes('The email link will stay.') && document.querySelectorAll('[data-question-thread] [data-own]').length===2`, "second round stays in the thread") }) } } } func TestBrowserRosterDecisionStates(t *testing.T) { for _, width := range []int64{1280, 390} { for _, scheme := range []string{"light", "dark"} { t.Run(fmt.Sprintf("%d-%s", width, scheme), func(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) id, c := r.toDirection("roster", "Explore a shorter setup") _, built := r.toDirection("roster-build", "Explore compact navigation") p := openInner(t, r.inst.URL, r.inst.Cookie(), width, 844, width == 390, scheme, "reduce") defer p.stop() p.open("/conversation") card := `[data-card="` + c.ID + `"]` p.wait(`document.querySelector('`+card+` [data-direction-choice="build"]')`, "direction") p.check(`(()=>{document.querySelector('`+card+` [equipo-actions] [name=revision]').value='0';return true})()`, "make stale") p.click(card + ` [data-direction-choice="build"]`) p.wait(`document.querySelector('`+card+` [equipo-card-error]').textContent.includes('This item has changed')`, "stale revision error") if k := r.task(id); k.Stage != wire.StageInReview { t.Fatalf("stale choice changed task: %s", k.Stage) } p.shot(fmt.Sprintf("roster-decision-stale-%d-%s", width, scheme)) p.click(card + ` [equipo-card-error] button`) p.wait(`document.querySelector('`+card+` [equipo-actions] [name=revision]').value==='1'`, "refresh stale decision") p.check(`(()=>{const b=document.querySelector('`+card+` [data-direction-choice="build"]');b.scrollIntoView({block:'center'});b.addEventListener('click',e=>e.preventDefault(),{once:true});return true})()`, "inspect pointer states") xy := eval[[]float64](p, `(()=>{const r=document.querySelector('`+card+` [data-direction-choice="build"]').getBoundingClientRect();return [r.x+r.width/2,r.y+r.height/2]})()`) p.do(chromedp.MouseEvent(input.DispatchMouseEventTypeMouseMoved, xy[0], xy[1])) p.check(`document.querySelector('`+card+` [data-direction-choice="build"]').matches(':hover')`, "hover state") p.shot(fmt.Sprintf("roster-decision-hover-%d-%s", width, scheme)) // FullScreenshot can restore a different scroll offset on a phone. xy = eval[[]float64](p, `(()=>{const r=document.querySelector('`+card+` [data-direction-choice="build"]').getBoundingClientRect();return [r.x+r.width/2,r.y+r.height/2]})()`) p.do(chromedp.MouseEvent(input.DispatchMouseEventTypeMouseMoved, xy[0], xy[1])) p.do(chromedp.MouseEvent(input.DispatchMouseEventTypeMousePressed, xy[0], xy[1], chromedp.ButtonLeft, chromedp.ClickCount(1))) p.check(`getComputedStyle(document.querySelector('`+card+` [data-direction-choice="build"]')).transform!=='none'`, "pressed state") p.shot(fmt.Sprintf("roster-decision-pressed-%d-%s", width, scheme)) p.do(chromedp.MouseEvent(input.DispatchMouseEventTypeMouseReleased, xy[0], xy[1], chromedp.ButtonLeft, chromedp.ClickCount(1))) p.check(`(()=>{window.__fetch=window.fetch;window.fetch=function(url,opts){if(opts&&opts.method==='POST')return new Promise(resolve=>{window.__release=()=>resolve(new Response('{}',{status:503}));});return window.__fetch(url,opts)};return true})()`, "hold request") p.click(card + ` [data-direction-choice="build"]`) p.check(`(()=>{const c=document.querySelector('`+card+`');return c.getAttribute('aria-busy')==='true' && c.querySelector('[aria-busy="true"]').textContent.includes('Building') && [...c.querySelectorAll('button')].every(b=>b.disabled)})()`, "busy and sibling disabled") p.shot(fmt.Sprintf("roster-decision-busy-%d-%s", width, scheme)) p.check(`(()=>{window.__release();return true})()`, "fail") p.wait(`!document.querySelector('`+card+` [equipo-card-error]').hidden`, "in-place error") p.shot(fmt.Sprintf("roster-decision-error-%d-%s", width, scheme)) p.check(`(()=>{window.fetch=window.__fetch;return true})()`, "restore request") p.click(card + ` [data-thread-open]`) p.check(`document.activeElement===document.querySelector('`+card+` textarea')`, "Try again focuses sub-chat") p.check(`(()=>{const i=document.querySelector('`+card+` textarea');i.value='Keep the runner step, drop the tour.';i.dispatchEvent(new Event('input',{bubbles:true}));return true})()`, "note") p.click(card + ` [data-thread-send]`) p.wait(`document.querySelector('`+card+` [data-own]')`, "sent direction note") if k := r.task(id); k.Stage != wire.StageInProgress { t.Fatalf("note did not choose another round: %s", k.Stage) } p.check(`document.querySelector('`+card+` [equipo-roster-project]')?.textContent.includes('Sample')`, "decision lost project context") p.fits("direction states") p.shot(fmt.Sprintf("roster-decision-sent-%d-%s", width, scheme)) build := `[data-card="` + built.ID + `"]` p.click(build + ` [data-direction-choice="build"]`) p.wait(`document.querySelector('`+build+` [data-done]')`, "Build collapses to success") p.check(`getComputedStyle(document.querySelector('`+build+` [equipo-card-line]')).display==='none'`, "success retained the open card title") p.shot(fmt.Sprintf("roster-decision-success-%d-%s", width, scheme)) }) } } } func TestBrowserRosterThinking(t *testing.T) { for _, width := range []int64{1280, 390} { for _, scheme := range []string{"light", "dark"} { t.Run(fmt.Sprintf("%d-%s", width, scheme), func(t *testing.T) { r := newToolRig(t) p := openInner(t, r.inst.URL, r.inst.Cookie(), width, 844, width == 390, scheme, "reduce") defer p.stop() p.open("/conversation") p.wait(`document.querySelector('[equipo-thinking] [data-turn-stop]')`, "Stop on thinking line") p.check(`(()=>{const s=getComputedStyle(document.querySelector('[equipo-thinking] [equipo-sparkle]'));return s.display==='none'})()`, "filled thinking mark remains") p.wait(`(()=>{const c=document.querySelector('[equipo-thinking]'),n=c.querySelector('[equipo-message-meta] [equipo-coordinator-mark] svg'),a=c.querySelector('[equipo-phrase]') || c.querySelector('[equipo-activity]'),b=c.querySelector('[data-turn-stop]');return n.getBoundingClientRect().width>0 && Math.abs(n.getBoundingClientRect().top-a.getBoundingClientRect().top)<10 && getComputedStyle(b).boxShadow==='none'})()`, "one thinking line with a quiet Stop") if width == 390 { p.check(`document.querySelector('[data-turn-stop]').getBoundingClientRect().height>=44`, "Stop has a small touch target") } p.wait(`[...document.querySelectorAll('[equipo-thinking] [equipo-phrase] [data-in] > span')].every(e=>Number(getComputedStyle(e).opacity)>=.99)`, "settled thinking phrase") p.fits("thinking line") p.shot(fmt.Sprintf("roster-thinking-%d-%s", width, scheme)) p.click(`[equipo-thinking] [data-turn-stop]`) cf, _ := r.expect(wire.FrameCancel) r.runner.Send(r.ctx, "roster-stop-ack", wire.FrameAck, wire.Ack{ID: cf.ID}) r.runner.Send(r.ctx, "roster-stop-report", wire.FrameTurn, wire.TurnReport{TurnID: r.turn, Attempt: 1, State: wire.TurnCancelled, Reason: "cancelled"}) r.wait("stopped", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnCancelled }) p.wait(`!document.querySelector('[equipo-thinking]')`, "stopped line removed") }) } } }