| 1 | //go:build browser |
| 2 | |
| 3 | package thintest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | "github.com/chromedp/chromedp" |
| 15 | ) |
| 16 | |
| 17 | // TestBrowserTrackPulse: the stage track's current stop pulses while a |
| 18 | // specialist works on it, on the conversation's report card and on the |
| 19 | // task page, and is still while the task waits or nothing runs. Reduced |
| 20 | // motion keeps a still ring. A step starting and stopping starts and |
| 21 | // stops the pulse live, with no reload. |
| 22 | func TestBrowserTrackPulse(t *testing.T) { |
| 23 | g := newGallery(t) |
| 24 | a := g.a |
| 25 | report := func(id string) string { |
| 26 | return `document.querySelector('[equipo-roster-report] [data-track-task="` + id + `"]')` |
| 27 | } |
| 28 | cur := func(track string) string { return track + `?.querySelector('[aria-current="step"]')` } |
| 29 | ring := func(track string) string { |
| 30 | return `getComputedStyle(` + cur(track) + `.querySelector('[equipo-dot]'),'::before')` |
| 31 | } |
| 32 | cue := func(track string) string { |
| 33 | return `getComputedStyle(` + cur(track) + `.querySelector('[data-working-cue]')).display` |
| 34 | } |
| 35 | for _, motion := range []string{"no-preference", "reduce"} { |
| 36 | for _, scheme := range []string{"light", "dark"} { |
| 37 | t.Run(motion+"-"+scheme, func(t *testing.T) { |
| 38 | p := openInner(t, a.inst.URL, a.inst.Cookie(), 1280, 900, false, scheme, motion) |
| 39 | defer p.stop() |
| 40 | p.open("/conversation") |
| 41 | run := report(g.running) |
| 42 | p.wait(cur(run), "the running task's report") |
| 43 | p.check(cur(run)+`.hasAttribute('data-working') && `+cue(run)+`!=='none'`, "the running stop is not working") |
| 44 | p.check(`(()=>{const w=[...document.querySelectorAll('[equipo-roster-report] [data-track] [data-working]')];return w.length>0 && w.every(li=>li.matches('[aria-current="step"]') && ['`+g.running+`','`+g.merging+`'].includes(li.parentElement.dataset.trackTask))})()`, "a stop with nobody at work pulses") |
| 45 | p.check(`[...document.querySelectorAll('[equipo-roster-report] [data-track] [aria-current="step"]:not([data-working])')].every(li=>getComputedStyle(li.querySelector('[equipo-dot]'),'::before').content==='none' && getComputedStyle(li.querySelector('[data-working-cue]')).display==='none')`, "an idle stop draws the ring or the cue") |
| 46 | if motion == "reduce" { |
| 47 | p.check(ring(run)+`.animationName==='none' && `+ring(run)+`.opacity==='0.6' && `+ring(run)+`.transform!=='none'`, "reduced motion lost the still ring") |
| 48 | p.check(`getComputedStyle(`+cur(run)+`.previousElementSibling,'::before').animationName==='none'`, "the line moves under reduced motion") |
| 49 | } else { |
| 50 | p.check(ring(run)+`.animationName==='equipo-tl-ring' && `+ring(run)+`.animationDuration==='2.4s'`, "the working stop does not pulse") |
| 51 | p.check(`getComputedStyle(`+cur(run)+`.previousElementSibling,'::before').animationName==='equipo-tl-flow'`, "the line into the working stop is still") |
| 52 | p.check(`document.getAnimations().filter(x=>x.animationName==='equipo-tl-ring').every(x=>x.startTime===0)`, "pulses out of phase") |
| 53 | } |
| 54 | // The task page's track: the living mark, and the ring. |
| 55 | p.open("/tasks/" + g.running) |
| 56 | full := `document.querySelector('[equipo-track-full]')` |
| 57 | p.check(cur(full)+`.hasAttribute('data-working') && !!`+cur(full)+`.querySelector('[equipo-mark]') && `+cue(full)+`!=='none'`, "the task page's stop is not working") |
| 58 | if motion == "reduce" { |
| 59 | p.check(ring(full)+`.animationName==='none' && `+ring(full)+`.opacity==='0.6'`, "task page: reduced motion lost the still ring") |
| 60 | } else { |
| 61 | p.check(ring(full)+`.animationName==='equipo-tl-ring'`, "task page: the working stop does not pulse") |
| 62 | } |
| 63 | |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Live, light and dark side by side: a new task's step queues, runs |
| 69 | // and stops, and its card's pulse follows with no reload. |
| 70 | pages := map[string]*innerPage{} |
| 71 | for _, scheme := range []string{"light", "dark"} { |
| 72 | p := openInner(t, a.inst.URL, a.inst.Cookie(), 1280, 900, false, scheme, "no-preference") |
| 73 | defer p.stop() |
| 74 | pages[scheme] = p |
| 75 | } |
| 76 | each := func(f func(scheme string, p *innerPage)) { |
| 77 | for _, scheme := range []string{"light", "dark"} { |
| 78 | f(scheme, pages[scheme]) |
| 79 | } |
| 80 | } |
| 81 | task := taskID(t, a.call("tp-rec", wire.ToolRecordTask, map[string]any{"repo": "oficina/equipo", "title": "Keep the filter when the list refreshes", "brief": "The task filter resets when the list refreshes itself.", "separate": true})) |
| 82 | track := report(task) |
| 83 | card := `[data-card]:has([data-track-task="` + task + `"])` |
| 84 | show := `(()=>{` + cur(track) + `.closest('[data-card]').scrollIntoView({block:'center'});return true})()` |
| 85 | each(func(_ string, p *innerPage) { |
| 86 | p.open("/conversation") |
| 87 | p.wait(cur(track), "the new task's report") |
| 88 | p.check(`!`+cur(track)+`.hasAttribute('data-working') && `+ring(track)+`.content==='none'`, "a noted task pulses") |
| 89 | }) |
| 90 | a.call("tp-start", wire.ToolStartTask, map[string]string{"task": task, "brief": "Keep the filter."}) |
| 91 | _, body := a.expect(wire.FrameDispatch) |
| 92 | d := body.(*wire.DispatchCommand) |
| 93 | each(func(_ string, p *innerPage) { |
| 94 | p.wait(cur(track)+`?.hasAttribute('data-working')`, "the queued step's pulse") |
| 95 | }) |
| 96 | a.step("tp-run", wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("8", 40), Family: "claude", Account: "claude-a"}) |
| 97 | each(func(scheme string, p *innerPage) { |
| 98 | p.wait(cur(track)+`?.hasAttribute('data-working') && `+cur(track)+`.textContent.startsWith('In progress')`, "the running step's pulse") |
| 99 | p.check(ring(track)+`.animationName==='equipo-tl-ring'`, "the running card does not pulse") |
| 100 | p.check(show, "show the card") |
| 101 | p.cardShot(card, "track-pulse-running-"+scheme) |
| 102 | }) |
| 103 | a.step("tp-fail", wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) |
| 104 | each(func(scheme string, p *innerPage) { |
| 105 | p.wait(cur(track)+` && !`+cur(track)+`.hasAttribute('data-working') && `+ring(track)+`.content==='none' && `+cue(track)+`==='none'`, "the pulse stopped") |
| 106 | p.stayed("conversation") |
| 107 | p.check(show, "show the card") |
| 108 | p.cardShot(card, "track-pulse-idle-"+scheme) |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | // cardShot saves the element sel at twice its size when |
| 113 | // EQUIPO_BROWSER_ARTIFACTS names a directory. |
| 114 | func (p *innerPage) cardShot(sel, name string) { |
| 115 | p.t.Helper() |
| 116 | dir := os.Getenv("EQUIPO_BROWSER_ARTIFACTS") |
| 117 | if dir == "" { |
| 118 | return |
| 119 | } |
| 120 | // A live refresh may swap the card mid-capture: settle, then retry. |
| 121 | var buf []byte |
| 122 | var err error |
| 123 | for try := 0; try < 4; try++ { |
| 124 | time.Sleep(700 * time.Millisecond) |
| 125 | err = chromedp.Do(p.ctx, chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 126 | var err error |
| 127 | buf, err = chromedp.ScreenshotScale(chromedp.CSS(sel), 2)(ctx, target) |
| 128 | return err |
| 129 | })) |
| 130 | if err == nil { |
| 131 | break |
| 132 | } |
| 133 | } |
| 134 | if err != nil { |
| 135 | p.t.Fatal(err) |
| 136 | } |
| 137 | if err := os.MkdirAll(dir, 0700); err != nil { |
| 138 | p.t.Fatal(err) |
| 139 | } |
| 140 | if err := os.WriteFile(filepath.Join(dir, name+".png"), buf, 0600); err != nil { |
| 141 | p.t.Fatal(err) |
| 142 | } |
| 143 | } |
| 144 | |