| 1 | //go:build browser |
| 2 | |
| 3 | package thintest |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // Stored finished steps make each report state independent of a runner's timing. |
| 15 | func reportState(r *toolRig, id, state string) { |
| 16 | r.t.Helper() |
| 17 | exec := func(q string, args ...any) { |
| 18 | r.t.Helper() |
| 19 | if _, err := r.inst.DB.Writer().Exec(q, args...); err != nil { |
| 20 | r.t.Fatal(err) |
| 21 | } |
| 22 | } |
| 23 | exec("DELETE FROM chat_steps WHERE task_id=?", id) |
| 24 | stage, kind, checks, verdict, round := wire.StageInReview, "", wire.ChecksPassed, wire.VerdictChanges, 2 |
| 25 | switch state { |
| 26 | case "checks": |
| 27 | checks, verdict = wire.ChecksFailed, "" |
| 28 | case "merge", "collapsed": |
| 29 | stage, verdict = wire.StageReadyToMerge, wire.VerdictApprove |
| 30 | case "preview": |
| 31 | kind, verdict = wire.KindExplore, wire.VerdictApprove |
| 32 | case "running": |
| 33 | stage, round = wire.StageInProgress, 0 |
| 34 | case "done": |
| 35 | stage, verdict = wire.StageDone, wire.VerdictApprove |
| 36 | case "automatic": |
| 37 | round = 0 |
| 38 | } |
| 39 | now := r.inst.Store.Chat().Now() |
| 40 | exec("UPDATE chat_tasks SET stage=?,kind=?,title='Show immediate loading feedback on action buttons',base_sha=?,repair_round=? WHERE id=?", stage, kind, strings.Repeat("1", 40), round, id) |
| 41 | exec("INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,attempt,round,base_sha,candidate_sha,pushed,terminated,revision,created_at,updated_at) VALUES(?,?,1,'engineering',?,'done',1,?,?,?,1,1,1,?,?)", id+"e", id, Owner, round, strings.Repeat("1", 40), candX, now, now) |
| 42 | exec("INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,attempt,candidate_sha,checks,terminated,revision,created_at,updated_at) VALUES(?,?,2,'checks',?,'done',1,?,?,1,1,?,?)", id+"c", id, Owner, candX, checks, now, now) |
| 43 | if verdict != "" { |
| 44 | exec("INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,attempt,candidate_sha,verdict,review_label,terminated,revision,created_at,updated_at) VALUES(?,?,3,'review',?,'done',1,?,?,'independent',1,1,?,?)", id+"r", id, Owner, candX, verdict, now, now) |
| 45 | } |
| 46 | if state == "running" || state == "automatic" { |
| 47 | exec("INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,attempt,revision,created_at,updated_at) VALUES(?,?,4,'engineering',?,'running',1,1,?,?)", id+"live", id, Owner, now, now) |
| 48 | } |
| 49 | if state == "preview" { |
| 50 | exec("INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,attempt,preview,terminated,revision,created_at,updated_at) VALUES(?,?,4,'preview',?,'done',1,?,1,1,?,?)", id+"p", id, Owner, `{"outcome":"deployed","url":"https://preview.example.test","app":"eq-report"}`, now, now) |
| 51 | } |
| 52 | exec("DELETE FROM chat_cards WHERE task_id=? AND kind='merge'", id) |
| 53 | if state == "collapsed" { |
| 54 | exec("INSERT INTO chat_cards(id,task_id,person,turn_id,kind,state,question,options,revision,created_at,updated_at) VALUES(?,?,?,'','merge','open','Merge this change?',?,1,?,?)", id+"merge", id, Owner, `["merge","not_now"]`, now, now) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestBrowserReportNextSteps(t *testing.T) { |
| 59 | for _, width := range []int64{1280, 390} { |
| 60 | for _, scheme := range []string{"light", "dark"} { |
| 61 | t.Run(fmt.Sprintf("%d-%s", width, scheme), func(t *testing.T) { |
| 62 | r := newToolRig(t) |
| 63 | id := r.shopTask("report") |
| 64 | if err := r.runner.Send(r.ctx, "report-ready", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Here is the report."}); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | r.wait("finished", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) |
| 68 | p := openInner(t, r.inst.URL, r.inst.Cookie(), width, 844, width == 390, scheme, "reduce") |
| 69 | defer p.stop() |
| 70 | for _, state := range []string{"changes", "checks", "merge", "preview", "running", "automatic", "done", "collapsed"} { |
| 71 | t.Run(state, func(t *testing.T) { |
| 72 | p.t = t |
| 73 | defer func() { p.t = r.t }() |
| 74 | reportState(r, id, state) |
| 75 | p.open("/conversation") |
| 76 | p.wait(`document.querySelector('[equipo-offline]').hidden && document.querySelector('[equipo-roster-report]')`, "report") |
| 77 | expected := map[string]string{"changes": "Try another round", "checks": "Try another round", "merge": "Merge", "preview": "Open the preview"}[state] |
| 78 | p.check(fmt.Sprintf(`(()=>{const b=document.querySelectorAll('[equipo-roster-report] [rst-btn="primary"]');return b.length===%d && (!b.length || b[0].textContent===%q)})()`, boolInt(expected != ""), expected), "next step for "+state) |
| 79 | p.check(`!!document.querySelector('[equipo-roster-report] [data-report-talk]')`, "Talk about this missing") |
| 80 | if state == "collapsed" { |
| 81 | p.check(`document.querySelector('[equipo-roster-report]').hasAttribute('data-compact') && !!document.querySelector('[equipo-card="merge"] [rst-btn="primary"]')`, "collapsed action missing or duplicated") |
| 82 | } else { |
| 83 | p.check(`[...document.querySelectorAll('[equipo-roster-links] a:not([data-report-talk]):not([data-report-next])')].every(a=>!a.hasAttribute('rst-btn'))`, "navigation is not quiet") |
| 84 | } |
| 85 | p.check(`(()=>{document.querySelector('[equipo-roster-report]').closest('[data-card]').scrollIntoView({block:'start'});document.querySelector('[equipo-scroll]').dispatchEvent(new Event('scroll'));return true})()`, "show report") |
| 86 | p.fits(state) |
| 87 | p.shot(fmt.Sprintf("report-%s-%d-%s", state, width, scheme)) |
| 88 | if state == "collapsed" { |
| 89 | p.click(`[equipo-card="merge"] [data-set-aside]`) |
| 90 | p.wait(`document.querySelector('[data-report-next]')`, "set-aside report action") |
| 91 | p.check(`(()=>{const f=document.querySelector('[data-report-next]').form;return f.action.endsWith('/cards/`+id+`merge/answer') && f.elements.revision.value==='1' && f.elements.choice.value==='merge'})()`, "report bypasses decision card revision") |
| 92 | p.shot(fmt.Sprintf("report-merge-aside-%d-%s", width, scheme)) |
| 93 | } |
| 94 | if expected != "" && state != "preview" { |
| 95 | action := "round" |
| 96 | if state == "merge" { |
| 97 | action = "merge" |
| 98 | } |
| 99 | p.check(fmt.Sprintf(`document.querySelector('[data-report-next]').form.action.endsWith('/tasks/%s/%s')`, id, action), "different server action") |
| 100 | taskPage := r.get(r.inst.Cookie(), "/tasks/"+id, 200) |
| 101 | if !strings.Contains(taskPage, "/conversation/tasks/"+id+"/"+action) { |
| 102 | t.Fatalf("task page does not offer %s", action) |
| 103 | } |
| 104 | } |
| 105 | }) |
| 106 | } |
| 107 | }) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | func boolInt(b bool) int { |
| 112 | if b { |
| 113 | return 1 |
| 114 | } |
| 115 | return 0 |
| 116 | } |
| 117 | |
| 118 | func TestBrowserReportThread(t *testing.T) { |
| 119 | for _, width := range []int64{1280, 390} { |
| 120 | for _, scheme := range []string{"light", "dark"} { |
| 121 | t.Run(fmt.Sprintf("%d-%s", width, scheme), func(t *testing.T) { |
| 122 | r := newToolRig(t) |
| 123 | id := r.shopTask("thread-report") |
| 124 | reportState(r, id, "changes") |
| 125 | if err := r.runner.Send(r.ctx, "report-finish", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Please review the report."}); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | r.wait("finished", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) |
| 129 | p := openInner(t, r.inst.URL, r.inst.Cookie(), width, 844, width == 390, scheme, "reduce") |
| 130 | defer p.stop() |
| 131 | p.open("/conversation") |
| 132 | p.wait(`document.querySelector('[equipo-offline]').hidden`, "connected") |
| 133 | p.click(`[data-report-talk]`) |
| 134 | p.wait(`document.querySelector('[data-report-thread][open] textarea')===document.activeElement`, "thread opens and focuses") |
| 135 | p.check(`(()=>{const i=document.activeElement;i.value='What should we change next?';i.dispatchEvent(new Event('input',{bubbles:true}));return true})()`, "type reply") |
| 136 | p.shot(fmt.Sprintf("report-thread-open-%d-%s", width, scheme)) |
| 137 | p.click(`[data-report-thread] [data-thread-send]`) |
| 138 | f, b := r.expect(wire.FrameMessage) |
| 139 | m := b.(*wire.MessageCommand) |
| 140 | for _, want := range []string{id, "Show immediate loading feedback", "Sample", "in_review", "changes_requested"} { |
| 141 | if !strings.Contains(m.Text, want) { |
| 142 | t.Fatalf("missing context %q: %s", want, m.Text) |
| 143 | } |
| 144 | } |
| 145 | r.runner.Send(r.ctx, "report-ack", wire.FrameAck, wire.Ack{ID: f.ID}) |
| 146 | p.wait(`document.querySelector('[data-report-thread] [data-own]') && document.querySelector('[data-report-thread] [equipo-thread-wait]')`, "sent thread") |
| 147 | p.shot(fmt.Sprintf("report-thread-waiting-%d-%s", width, scheme)) |
| 148 | r.runner.Send(r.ctx, "report-delta", wire.FrameDelta, wire.DeltaReport{TurnID: m.TurnID, Attempt: 1, Offset: 0, Text: "Let’s simplify"}) |
| 149 | p.wait(`document.querySelector('[data-report-thread] [equipo-thread-draft]')?.textContent==='Let’s simplify'`, "reply streams in thread") |
| 150 | r.runner.Send(r.ctx, "report-reply", wire.FrameReply, wire.Reply{TurnID: m.TurnID, Attempt: 1, Text: "Let’s simplify the button states."}) |
| 151 | p.wait(`document.querySelector('[data-report-thread]').textContent.includes('Let’s simplify the button states.')`, "coordinator reply") |
| 152 | p.open("/conversation") |
| 153 | p.wait(`document.querySelector('[data-report-thread][open]')?.textContent.includes('Let’s simplify the button states.')`, "persistent thread") |
| 154 | p.check(`document.querySelector('[data-report-thread] [equipo-thread-who]').textContent.includes('Coordinator')`, "wrong reply author") |
| 155 | p.check(`![...document.querySelectorAll('[equipo-message-body]')].some(e=>e.textContent.includes('Let’s simplify the button states.')||e.textContent.includes('What should we change next?'))`, "thread leaked into main chat") |
| 156 | p.check(`(()=>{document.querySelector('[data-report-thread]').scrollIntoView({block:'center'});return true})()`, "show thread") |
| 157 | p.fits("report thread") |
| 158 | p.shot(fmt.Sprintf("report-thread-replied-%d-%s", width, scheme)) |
| 159 | yesterday := r.inst.Store.Chat().Now().Add(-24 * time.Hour) |
| 160 | for _, table := range []string{"chat_messages", "chat_cards"} { |
| 161 | if _, err := r.inst.DB.Writer().Exec("UPDATE "+table+" SET created_at=?", yesterday); err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | } |
| 165 | p.open("/conversation?day=" + yesterday.Format("2006-01-02")) |
| 166 | p.wait(`document.querySelector('[data-report-thread]')?.textContent.includes('Let’s simplify the button states.')`, "historical thread") |
| 167 | p.check(`![...document.querySelectorAll('[equipo-message-body]')].some(e=>e.textContent.includes('Let’s simplify the button states.')||e.textContent.includes('What should we change next?'))`, "historical thread leaked into main chat") |
| 168 | }) |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | func TestBrowserReportActionFeedback(t *testing.T) { |
| 174 | for _, width := range []int64{1280, 390} { |
| 175 | for _, scheme := range []string{"light", "dark"} { |
| 176 | for _, state := range []string{"changes", "merge"} { |
| 177 | t.Run(fmt.Sprintf("%s-%d-%s", state, width, scheme), func(t *testing.T) { |
| 178 | r := newToolRig(t) |
| 179 | id := r.shopTask("feedback") |
| 180 | reportState(r, id, state) |
| 181 | p := openInner(t, r.inst.URL, r.inst.Cookie(), width, 844, width == 390, scheme, "reduce") |
| 182 | defer p.stop() |
| 183 | p.open("/conversation") |
| 184 | p.wait(`document.querySelector('[equipo-offline]').hidden && document.querySelector('[data-report-next]')`, "action") |
| 185 | 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 action") |
| 186 | p.click(`[data-report-next]`) |
| 187 | p.check(`document.querySelector('[data-report-next]').getAttribute('aria-busy')==='true' && document.querySelector('[data-report-talk]').disabled`, "busy feedback") |
| 188 | p.shot(fmt.Sprintf("report-%s-busy-%d-%s", state, width, scheme)) |
| 189 | p.check(`(()=>{window.__fail();window.fetch=window.__fetch;return true})()`, "fail action") |
| 190 | p.wait(`document.querySelector('[equipo-roster-report]').closest('[data-card]').querySelector('[equipo-card-error]:not([hidden])')`, "error feedback") |
| 191 | p.check(`!document.querySelector('[data-report-next]').disabled`, "action remains disabled") |
| 192 | p.shot(fmt.Sprintf("report-%s-error-%d-%s", state, width, scheme)) |
| 193 | p.click(`[equipo-card="task"] [equipo-card-error] button`) |
| 194 | p.wait(`document.querySelector('[equipo-roster-report] [equipo-card-done]')`, "success feedback") |
| 195 | p.check(`!document.querySelector('[equipo-roster-report] [rst-btn="primary"]')`, "success leaves stale primary") |
| 196 | p.shot(fmt.Sprintf("report-%s-success-%d-%s", state, width, scheme)) |
| 197 | reportState(r, id, "done") |
| 198 | p.click(`[data-report-talk]`) |
| 199 | p.check(`(()=>{const i=document.querySelector('[data-report-thread] textarea');i.value='Thanks.';i.dispatchEvent(new Event('input',{bubbles:true}));return true})()`, "reply after completion") |
| 200 | p.click(`[data-report-thread] [data-thread-send]`) |
| 201 | p.wait(`document.querySelector('[equipo-roster-report] [data-stage="done"]')`, "completed report refresh") |
| 202 | p.check(`!document.querySelector('[equipo-roster-report] [equipo-card-done]')`, "completed report still says work is starting") |
| 203 | }) |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |