oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package browsertest
4
5import (
6 "fmt"
7 "strconv"
8 "strings"
9 "testing"
10 "time"
11
12 "github.com/chromedp/chromedp"
13 "github.com/chromedp/chromedp/kb"
14
15 "amadan.net/rastrillo/rastrillo/harness"
16
17 "amadan.net/oficina/base/internal/config"
18 "amadan.net/oficina/base/internal/equipo"
19)
20
21// The Assistant's cards in the panel, drawn from their fields alone: an
22// open outcome card with its buttons and link keeps the conversation at
23// its newest line; answering it posts the option's value and brings no
24// bubble and no turn; a card that has ended shows its result and link;
25// a card of a kind Base does not draw never shows.
26func TestAskCardsInPanel(t *testing.T) {
27 rig, f := askBase(t)
28 openPanel(t, rig)
29 wait := func(js string) {
30 t.Helper()
31 drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second)))
32 }
33 for i := 1; i <= 20; i++ {
34 f.Say("Earlier line " + strconv.Itoa(i) + " of a long conversation.")
35 }
36 wait(`document.querySelectorAll("#ask-panel .thread > li").length >= 20`)
37
38 options := []equipo.Option{{Value: "go", Label: "Go ahead"}, {Value: "later", Label: "Not yet"}}
39 preview := []equipo.Link{{Label: "See it", URL: "https://preview.example.test/"}}
40 open := f.AskOutcome("This is ready. Go ahead?", options, preview)
41 card := `#ask-panel [data-card="` + open + `"]`
42 wait(`(() => { const c = document.querySelector(` + strconv.Quote(card) + `);
43 return c && c.textContent.includes("This is ready. Go ahead?") &&
44 [...c.querySelectorAll("button")].map(b => b.textContent + "=" + b.value).join("|") === "Go ahead=go|Not yet=later" &&
45 !!c.querySelector('a.card-link[href="https://preview.example.test/"][target="_blank"][rel="noopener"]') })()`)
46 drive(t, rig, chromedp.Sleep(200*time.Millisecond))
47 if got := harness.Evaluate[string](rig, lastVisible); got != "" {
48 t.Errorf("with an open card: %s", got)
49 }
50 mobileShot(t, rig, "1-outcome-open")
51
52 // Answering posts the value; Equipo starts no turn for it, so there is
53 // no bubble, no status and no Stop.
54 drive(t, rig, chromedp.Click(chromedp.CSS(card+` button[value="go"]`)))
55 wait(`document.querySelector(` + strconv.Quote(card+` .card-state`) + `)?.textContent === "Go ahead"`)
56 drive(t, rig, chromedp.Sleep(300*time.Millisecond))
57 if got := harness.Evaluate[string](rig, `[...document.querySelectorAll("#ask-panel .msg.person")].map(m => "[" + m.textContent + "]").join("") + "|" +
58 document.querySelector("#ask-panel .ask-status").textContent + "|" + document.querySelector("#ask-panel form.composer button.stop").hidden`); got != "||true" {
59 t.Errorf("after answering an outcome card: bubbles, status and Stop hidden %q", got)
60 }
61 if c := f.Calls("/api/v1/assistant/cards/" + open + "/answer"); len(c) != 1 || strings.TrimSpace(c[0].Body) != `{"choice":"go","revision":1}` {
62 t.Fatalf("answers: %+v", c)
63 }
64
65 // An ended card says how it ended, with its link, and has no buttons.
66 site := f.AskOutcome("Your site is ready. Publish it?", nil, []equipo.Link{{Label: "Open it", URL: "https://site.example.test/"}})
67 f.SetCard(site, "resolved", "Published")
68 scard := `#ask-panel [data-card="` + site + `"]`
69 wait(`(() => { const c = document.querySelector(` + strconv.Quote(scard) + `);
70 return c && c.querySelector(".card-state")?.textContent === "Published" && !c.querySelector("form") &&
71 !!c.querySelector('a.card-link[href="https://site.example.test/"][rel="noopener"]') })()`)
72 mobileShot(t, rig, "2-outcome-result")
73
74 task := f.AskKind("task", "Merge tk0001?", options, nil)
75 f.Say("Try [this](javascript:alert(1)), [that](/tasks/tk1) or <img src=x onerror=alert(1)>.")
76 wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent.includes("<img src=x"))`)
77 if n := harness.Evaluate[int](rig, `document.querySelectorAll('#ask-panel a[href^="javascript"], #ask-panel a[href*="/tasks/"], #ask-panel .msg img, #ask-panel [data-card="`+task+`"]').length`); n != 0 {
78 t.Fatalf("unsafe markup, a task link or a task card rendered: %d", n)
79 }
80 rig.Screen("#ask-panel", "the panel with the Assistant's cards")
81}
82
83// jobJS is a summary of the job with id in the panel: how many li.job
84// there are, then the job's state, status, link and the ids of the cards
85// under it, or "none".
86func jobJS(id string) string {
87 return `(() => { const all = document.querySelectorAll("#ask-panel li.job"), j = document.querySelector('#ask-panel li.job[data-job="` + id + `"]');
88 if (!j) return all.length + " none";
89 const link = j.querySelector(".pill-link");
90 return all.length + " " + j.dataset.state + " " + j.querySelector(".pill-stage").textContent + " [" + (link ? link.textContent : "") + "] " +
91 [...j.querySelectorAll("ol.job-cards > li")].map(c => c.dataset.card).join(","); })()`
92}
93
94// A job is one pill, updated in place as it moves: one li.job throughout,
95// its state, status and name following each revision. A team_update turn
96// is quiet: its activity sets no status line and it shows no Stop.
97func TestAskJobPillInPanel(t *testing.T) {
98 rig, f := askBase(t)
99 openPanel(t, rig)
100 wait := func(js string) {
101 t.Helper()
102 drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second)))
103 }
104 f.Say("On it.")
105 id := f.AskJob("Building your website")
106 is := func(want string) {
107 t.Helper()
108 wait(jobJS(id) + ` === ` + strconv.Quote(want))
109 }
110 is("1 working Getting started [] ")
111 if got := harness.Evaluate[string](rig, `(() => { const j = document.querySelector("#ask-panel li.job"), p = [];
112 if (j.querySelector(".card, .card-link")) p.push("drawn as a card");
113 if (j.querySelector(".pill-title").textContent !== "Building your website") p.push("title " + j.querySelector(".pill-title").textContent);
114 if (j.querySelector("summary.pill-main").getAttribute("aria-label") !== "Building your website, Getting started") p.push("name " + j.querySelector("summary").getAttribute("aria-label"));
115 if (j.querySelector(".state-mark svg")) p.push("working has an icon");
116 if (!getComputedStyle(j.querySelector(".state-mark"), "::after").animationName.includes("turn")) p.push("working mark still");
117 if (/equipo/i.test(document.querySelector("#ask-panel").innerText)) p.push("names Equipo");
118 const pages = document.querySelectorAll('#ask-panel a[href*="/jobs/"]');
119 if (pages.length !== 1 || !pages[0].matches(".jobview > header > h3 > a.jv-title")) p.push("links the job's page outside the job view's title");
120 return p.join("; "); })()`); got != "" {
121 t.Errorf("the working pill: %s", got)
122 }
123 rig.Screen("#ask-panel", "a job's pill, working")
124 // With reduced motion the working mark holds still.
125 drive(t, rig, reducedMotion("dark", true))
126 if got := harness.Evaluate[string](rig, `(() => { const m = document.querySelector("#ask-panel li.job .state-mark");
127 return getComputedStyle(m, "::after").animationName + " " + getComputedStyle(m, "::before").animationName; })()`); got != "none none" {
128 t.Errorf("the working mark with reduced motion: %s", got)
129 }
130 drive(t, rig, reducedMotion("dark", false))
131 f.SetJob(id, "working", "Building it", "Writing the pages.")
132 is("1 working Building it [] ")
133 // A link shows only once there is something to see.
134 try := equipo.Link{Label: "Try it", URL: "https://preview.example.test/"}
135 f.SetJob(id, "asking", "One question for you", "Waiting for your answer.", try)
136 is("1 asking One question for you [] ")
137 f.SetJob(id, "ready", "Ready to try", "Have a look.", try)
138 is("1 ready Ready to try [Try it] ")
139 if got := harness.Evaluate[string](rig, `(() => { const j = document.querySelector("#ask-panel li.job"), a = j.querySelector(".pill-link");
140 return j.dataset.revision + " " + j.querySelector("summary").getAttribute("aria-label") + " " + a.href + " " + a.target + " " + a.rel + " " + !!j.querySelector(".state-mark svg"); })()`); got != "4 Building your website, Ready to try https://preview.example.test/ _blank noopener true" {
141 t.Errorf("the ready pill: %s", got)
142 }
143 rig.Screen("#ask-panel", "a job's pill, ready")
144
145 // A quiet turn: running, with an activity line, and neither shows.
146 turn := f.EmitTurn("team_update", "running")
147 f.Emit("activity", map[string]string{"turn_id": turn, "label": "Looking at the job"})
148 f.Say("Have a look.")
149 wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent === "Have a look.")`)
150 drive(t, rig, chromedp.Sleep(200*time.Millisecond))
151 if got := harness.Evaluate[string](rig, `JSON.stringify(document.querySelector("#ask-panel .ask-status").textContent) + " " +
152 document.querySelector("#ask-panel form.composer button.stop").hidden + " " + document.querySelector("#ask-panel form.cancel").getAttribute("action")`); got != `"" true /ask` {
153 t.Errorf("with a team_update turn running: status, Stop hidden, cancel %s", got)
154 }
155}
156
157// Ruling P6j: the go-ahead under a ready pill stays under it as the job is
158// redrawn, still ready and then working again; answering it posts its
159// value and moves it into the job view; a Publish card that arrives after
160// the redraw lands under the same pill. A card that comes before its job
161// is drawn joins it when it is.
162func TestAskJobKeepsItsCards(t *testing.T) {
163 rig, f := askBase(t)
164 // The docked chat at its widest, where a ready pill has room for its
165 // status beside its link.
166 drive(t, rig, chromedp.EmulateViewport(1500, 900))
167 openPanel(t, rig)
168 wait := func(js string) {
169 t.Helper()
170 drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second)))
171 }
172 options := []equipo.Option{{Value: "go", Label: "Go ahead"}, {Value: "later", Label: "Not yet"}}
173 try := equipo.Link{Label: "Try it", URL: "https://preview.example.test/"}
174 id := f.AskJob("Building your website")
175 f.SetJob(id, "ready", "Ready to try", "Have a look.", try)
176 goAhead := f.AskOutcomeFor(id, "This is ready. Go ahead?", options, nil)
177 wait(jobJS(id) + ` === ` + strconv.Quote("1 ready Ready to try [Try it] "+goAhead))
178 rig.Screen("#ask-panel", "a ready pill with its go-ahead")
179 // Legible in the dark and in the light.
180 for _, scheme := range []string{"dark", "light"} {
181 drive(t, rig, prefersScheme(scheme), chromedp.Sleep(300*time.Millisecond))
182 mobileShot(t, rig, "job-ready-"+scheme)
183 checkContrast(t, rig, "a ready pill, "+scheme, "#ask-panel .pill-title", "#ask-panel .pill-stage", "#ask-panel .pill-link")
184 }
185 drive(t, rig, prefersScheme("dark"))
186 f.SetJob(id, "ready", "Ready to try", "Still ready.", try)
187 wait(`document.querySelector('#ask-panel li.job').dataset.revision === "3" && ` + jobJS(id) + ` === ` + strconv.Quote("1 ready Ready to try [Try it] "+goAhead))
188 f.SetJob(id, "working", "Almost ready", "Putting the finishing touches on it.")
189 wait(jobJS(id) + ` === ` + strconv.Quote("1 working Almost ready [] "+goAhead))
190 if n := harness.Evaluate[int](rig, `document.querySelectorAll('#ask-panel [data-card="`+goAhead+`"]').length`); n != 1 {
191 t.Fatalf("the go-ahead is drawn %d times", n)
192 }
193
194 card := `#ask-panel li.job ol.job-cards [data-card="` + goAhead + `"]`
195 drive(t, rig, chromedp.Click(chromedp.CSS(card+` button[value="go"]`)))
196 // Answered, it leaves the pill for the job view.
197 wait(jobJS(id) + ` === ` + strconv.Quote("1 working Almost ready [] ") + ` && ` + endedJS(id) + ` === "This is ready. Go ahead?|Go ahead"`)
198 if c := f.Calls("/api/v1/assistant/cards/" + goAhead + "/answer"); len(c) != 1 || strings.TrimSpace(c[0].Body) != `{"choice":"go","revision":1}` {
199 t.Fatalf("answers: %+v", c)
200 }
201 f.SetJob(id, "ready", "Ready to publish", "Publish it when you're happy.", try)
202 publish := f.AskOutcomeFor(id, "Your site is ready. Publish it?", options, nil)
203 wait(jobJS(id) + ` === ` + strconv.Quote("1 ready Ready to publish [Try it] "+publish) + ` && ` + endedJS(id) + ` === "This is ready. Go ahead?|Go ahead"`)
204
205 // A card of a job not yet drawn is loose, and moves under it when the
206 // job arrives. (The fake numbers its cards in turn: the next job is
207 // the card after this one.)
208 early := f.AskOutcomeFor("", "Go ahead with the other one?", options, nil)
209 n, err := strconv.Atoi(strings.TrimPrefix(early, "c_"))
210 if err != nil {
211 t.Fatalf("card id %q", early)
212 }
213 later := f.AskOutcomeFor(fmt.Sprintf("c_%04d", n+2), "And this one?", options, nil)
214 wait(`!!document.querySelector('#ask-panel .thread > [data-card="` + later + `"]')`)
215 other := f.AskJob("Fixing the login")
216 if other != fmt.Sprintf("c_%04d", n+2) {
217 t.Fatalf("the next job is %s", other)
218 }
219 wait(jobJS(other) + ` === ` + strconv.Quote("2 working Getting started [] "+later))
220 rig.Screen("#ask-panel", "two pills with their cards")
221 if got := harness.Evaluate[string](rig, `[...document.querySelectorAll("#ask-panel .thread > li")].map(l => l.dataset.job ? "job" : l.dataset.card ? "card" : "msg").join(",")`); got != "job,card,job" {
222 t.Errorf("the thread: %s", got)
223 }
224}
225
226// endedJS lists the cards in job id's view, each as its question and
227// its result line ("question|result"), comma-separated; "none" without
228// the job.
229func endedJS(id string) string {
230 return `(() => { const j = document.querySelector('#ask-panel li.job[data-job="` + id + `"]');
231 if (!j) return "none";
232 return [...j.querySelectorAll(".jobview ol.jv-cards > li")].map(c => [...c.children].map(p => p.textContent).join("|")).join(","); })()`
233}
234
235// Paul's live check (2026-11-16): only a card that needs an answer hangs
236// under its job's pill. A question answered and a go-ahead given move
237// into the job view, under its detail, each as its question and how it
238// ended, with nothing to press; an open go-ahead still shows under the
239// pill, and moves into the view once answered. The view keeps them as
240// the job is redrawn, and an ended card of a job not yet drawn joins its
241// view when it is. A card of no job stays in the conversation.
242func TestAskJobSettledCardsInView(t *testing.T) {
243 rig, f := askBase(t)
244 drive(t, rig, chromedp.EmulateViewport(1500, 900))
245 openPanel(t, rig)
246 wait := func(js string) {
247 t.Helper()
248 drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second)))
249 }
250 options := []equipo.Option{{Value: "go", Label: "Go ahead"}, {Value: "later", Label: "Not yet"}}
251 id := f.AskLegacyJob("Can you make me a website?")
252 question := f.AskKindFor("question", id, "Which website?", []equipo.Option{{Value: "new", Label: "A new one"}}, nil)
253 goAhead := f.AskOutcomeFor(id, "This is ready. Go ahead?", options, nil)
254 f.SetCard(question, "answered", "A new one")
255 f.SetCard(goAhead, "answered", "Go ahead")
256 f.SetJob(id, "stopped", "Stopped", "It can't go on until something is sorted out.")
257 both := "Which website?|A new one,This is ready. Go ahead?|Go ahead"
258 wait(jobJS(id) + ` === "1 stopped Stopped [] " && ` + endedJS(id) + ` === ` + strconv.Quote(both))
259 job := `#ask-panel li.job[data-job="` + id + `"]`
260 if got := harness.Evaluate[string](rig, `(() => { const j = document.querySelector(`+strconv.Quote(job)+`), p = [];
261 if (document.querySelectorAll('#ask-panel .thread > [data-card]').length) p.push("a card loose in the conversation");
262 const list = j.querySelector(".jobview ol.jv-cards");
263 if (list.previousElementSibling !== j.querySelector(".jobview .jv-detail")) p.push("not under the detail");
264 if (list.querySelector("form, button, a")) p.push("something to press");
265 return p.join("; "); })()`); got != "" {
266 t.Errorf("the ended cards: %s", got)
267 }
268 drive(t, rig, chromedp.Click(chromedp.CSS(job+` summary`)), chromedp.Sleep(250*time.Millisecond))
269 if n := harness.Evaluate[int](rig, `[...document.querySelectorAll(`+strconv.Quote(job+` .jobview ol.jv-cards > li`)+`)].filter(c => c.checkVisibility()).length`); n != 2 {
270 t.Errorf("%d ended cards show in the open view", n)
271 }
272 rig.Screen("#ask-panel", "a stopped job's view with its ended cards")
273 for _, scheme := range []string{"dark", "light"} {
274 drive(t, rig, prefersScheme(scheme), chromedp.Sleep(300*time.Millisecond))
275 mobileShot(t, rig, "jobview-settled-"+scheme)
276 checkContrast(t, rig, "the ended cards, "+scheme, job+" .jv-cards .jv-q", job+" .jv-cards .jv-result")
277 }
278 drive(t, rig, prefersScheme("dark"), chromedp.KeyEvent(kb.Escape), chromedp.Sleep(200*time.Millisecond))
279
280 // An open go-ahead hangs under the pill; answered, it joins the view.
281 f.SetJob(id, "ready", "Ready to try", "Have a look.")
282 again := f.AskOutcomeFor(id, "Try it again?", options, nil)
283 wait(jobJS(id) + ` === ` + strconv.Quote("1 ready Ready to try [] "+again) + ` && ` + endedJS(id) + ` === ` + strconv.Quote(both))
284 drive(t, rig, chromedp.Click(chromedp.CSS(job+` ol.job-cards [data-card="`+again+`"] button[value="go"]`)))
285 wait(jobJS(id) + ` === "1 ready Ready to try [] " && ` + endedJS(id) + ` === ` + strconv.Quote(both+",Try it again?|Go ahead"))
286 // Redrawn, the job keeps them in its view.
287 f.SetJob(id, "working", "Almost ready", "Putting the finishing touches on it.")
288 wait(jobJS(id) + ` === "1 working Almost ready [] " && ` + endedJS(id) + ` === ` + strconv.Quote(both+",Try it again?|Go ahead"))
289
290 // An ended card of a job not yet drawn joins its view when it is; a
291 // card of no job stays where it is. (The fake numbers its cards in
292 // turn: the next job is the card after the next.)
293 loose := f.AskOutcome("Something else?", options, nil)
294 n, err := strconv.Atoi(strings.TrimPrefix(loose, "c_"))
295 if err != nil {
296 t.Fatalf("card id %q", loose)
297 }
298 early := f.AskOutcomeFor(fmt.Sprintf("c_%04d", n+2), "And this one?", options, nil)
299 f.SetCard(early, "resolved", "Not needed any more")
300 f.SetCard(loose, "answered", "Go ahead")
301 wait(`!!document.querySelector('#ask-panel .thread > [data-card="` + early + `"]')`)
302 if other := f.AskJob("Fixing the login"); other != fmt.Sprintf("c_%04d", n+2) {
303 t.Fatalf("the next job is %s", other)
304 }
305 wait(jobJS(fmt.Sprintf("c_%04d", n+2)) + ` === ` + `"2 working Getting started [] "` + ` && ` + endedJS(fmt.Sprintf("c_%04d", n+2)) + ` === "And this one?|Not needed any more"`)
306 if got := harness.Evaluate[string](rig, `[...document.querySelectorAll("#ask-panel .thread > li")].map(l => l.dataset.job ? "job" : l.dataset.card ? "card" : "msg").join(",")`); got != "job,card,job" {
307 t.Errorf("the thread: %s", got)
308 }
309
310 // Read afresh, the same.
311 drive(t, rig, chromedp.Reload(), chromedp.Sleep(300*time.Millisecond))
312 openPanel(t, rig)
313 wait(jobJS(id) + ` === "2 working Almost ready [] " && ` + endedJS(id) + ` === ` + strconv.Quote(both+",Try it again?|Go ahead"))
314}
315
316// statusShownJS checks the pill of job id: its status is drawn (a box with
317// width, neither display:none nor visibility:hidden) and its title is cut
318// short with an ellipsis before the status is; "" when both hold.
319func statusShownJS(id string) string {
320 return `(() => { const j = document.querySelector('#ask-panel li.job[data-job="` + id + `"]');
321 if (!j) return "no job";
322 const s = j.querySelector(".pill-stage"), t = j.querySelector(".pill-title"), cs = getComputedStyle(s), p = [];
323 if (cs.display === "none" || cs.visibility === "hidden" || s.getBoundingClientRect().width < 1 || !s.checkVisibility({ visibilityProperty: true })) p.push("the status is not drawn");
324 if (t.scrollWidth <= t.clientWidth) p.push("the title is not cut short");
325 // The status is cut short only once the title is down to its least
326 // (measured to the fraction of a pixel: a sliver less already draws
327 // an ellipsis). Its whole width is a copy's, set beside it with no
328 // cap and no shrinking.
329 const drawn = s.getBoundingClientRect().width, copy = s.cloneNode(true);
330 copy.style.cssText = "position: absolute; visibility: hidden; flex: none; max-width: none; min-width: 0; width: max-content";
331 s.parentElement.append(copy);
332 const whole = copy.getBoundingClientRect().width;
333 copy.remove();
334 const title = t.getBoundingClientRect().width, least = parseFloat(getComputedStyle(t).minWidth);
335 if (whole - drawn > 0.01 && title > least + 0.5) p.push("the status is cut short (" + drawn + "px of " + whole + ") while the title is " + title + "px, more than its least " + least);
336 if (!j.querySelector(".pill-link")) p.push("no link");
337 const pill = j.querySelector(".pill").getBoundingClientRect(), log = document.querySelector("#ask-panel .thread").getBoundingClientRect();
338 if (pill.right > log.right + 0.5) p.push("the pill overflows the chat");
339 return p.join("; "); })()`
340}
341
342// Ruling R26: a ready or done pill keeps its status in sight at every
343// width, beside its link: on the default desktop's docked chat and on a
344// phone, a long title is cut short first.
345func TestAskJobStatusStaysVisible(t *testing.T) {
346 const title = "Building a website about disability benefits in Ireland now"
347 for _, view := range []struct {
348 name string
349 act chromedp.Action[chromedp.Void]
350 }{
351 {"desktop", nil},
352 {"phone", chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))},
353 } {
354 t.Run(view.name, func(t *testing.T) {
355 rig, f, _ := askBaseOn(t, nil, view.act)
356 if view.act == nil {
357 openPanel(t, rig)
358 } else {
359 // On a phone the chat opens full screen from the field on Home.
360 drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS("a.ask-field")),
361 chromedp.Click(chromedp.CSS("a.ask-field")), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)),
362 chromedp.Poll[chromedp.Void](readJS, chromedp.WithPollingTimeout(5*time.Second)))
363 }
364 id := f.AskJob(title)
365 for _, st := range []struct{ state, result, label string }{
366 {"ready", "Ready to try", "Try it"},
367 {"done", "Done", "Open your site"},
368 {"ready", "Ready to try on a phone, a tablet and a computer", "Try it"},
369 {"done", "Done, and live for everyone who visits", "Open your site"},
370 } {
371 f.SetJob(id, st.state, st.result, "One sentence more.", equipo.Link{Label: st.label, URL: "https://site.example.test/"})
372 drive(t, rig, chromedp.Poll[chromedp.Void](`document.querySelector('#ask-panel li.job .pill-stage')?.textContent === `+strconv.Quote(st.result),
373 chromedp.WithPollingTimeout(10*time.Second)))
374 // Measured in the page's own font, once it has loaded.
375 drive(t, rig, chromedp.Poll[chromedp.Void](`document.fonts.status === "loaded"`, chromedp.WithPollingTimeout(10*time.Second)),
376 chromedp.Sleep(100*time.Millisecond))
377 if got := harness.Evaluate[string](rig, statusShownJS(id)); got != "" {
378 t.Errorf("%s, %s: %s", view.name, st.state, got)
379 }
380 mobileShot(t, rig, "job-"+st.state+"-"+strconv.Itoa(len(st.result))+"-"+view.name)
381 }
382 })
383 }
384}
385
386// jobViewJS checks the open view of job id, docked on a wide screen: its
387// <details> is open and the summary says so; it is drawn whole and on top
388// (nothing covers its middle), inside the chat below its header, beside
389// the pill (side: above it when there is room, else below), with no
390// scrim; focus is on its Close; its title is the one link to the job's
391// page, in a new tab; its status says status. "" when all hold.
392func jobViewJS(id, status, side string) string {
393 return `(() => { const j = document.querySelector('#ask-panel li.job[data-job="` + id + `"]');
394 if (!j) return "no job";
395 const d = j.querySelector("details"), s = j.querySelector("summary"), v = j.querySelector(".jobview"), p = [];
396 if (!d.open) p.push("the details are closed");
397 if (s.getAttribute("aria-expanded") !== "true") p.push("aria-expanded " + s.getAttribute("aria-expanded"));
398 if (!v.checkVisibility({ visibilityProperty: true })) return p.concat("the view is not drawn").join("; ");
399 v.getAnimations().forEach((a) => a.finish()); // measured where it rests, not mid-rise
400 const r = v.getBoundingClientRect(), pill = j.querySelector(".pill").getBoundingClientRect();
401 const chat = document.querySelector("#ask-panel").getBoundingClientRect(), head = document.querySelector("#ask-panel .ask-head").getBoundingClientRect();
402 if (r.left < chat.left + 15.5 || r.right > chat.right - 15.5 || r.top < head.bottom || r.bottom > chat.bottom) p.push("outside the chat: " + JSON.stringify(r) + " in " + JSON.stringify(chat));
403 const room = pill.top - head.bottom - 8 >= r.height + 8;
404 if (` + strconv.Quote(side) + ` === "above" && (!room || r.bottom > pill.top - 4 || pill.top - r.bottom > 12)) p.push("not just above the pill: view bottom " + r.bottom + ", pill top " + pill.top + ", room " + room);
405 if (` + strconv.Quote(side) + ` === "below" && (room || r.top < pill.bottom + 4 || r.top - pill.bottom > 12)) p.push("not just below the pill: view top " + r.top + ", pill bottom " + pill.bottom + ", room above " + room);
406 const mid = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2);
407 if (!v.contains(mid)) p.push("covered at its middle by " + (mid && mid.className));
408 const close = v.querySelector("[data-job-close]");
409 if (close.hidden || !close.checkVisibility()) p.push("no Close");
410 if (document.activeElement !== close) p.push("focus on " + document.activeElement.tagName + "." + document.activeElement.className);
411 if (document.querySelector("#ask-panel .scrim")?.checkVisibility()) p.push("a scrim on a wide screen");
412 const pages = document.querySelectorAll('#ask-panel a[href*="/jobs/"]'), a = v.querySelector("h3 > a.jv-title");
413 if (!a || [...pages].some(l => !l.matches(".jobview > header > h3 > a.jv-title"))) p.push("the job's page is linked outside a job view's title");
414 else if (a.target !== "_blank" || a.rel !== "noopener" || a.title !== "Open in Equipo" || a.textContent !== j.querySelector(".pill-title").textContent) p.push("the title link: " + a.outerHTML);
415 if (v.querySelector(".jv-stage").textContent !== ` + strconv.Quote(status) + `) p.push("status " + v.querySelector(".jv-stage").textContent);
416 return p.join("; "); })()`
417}
418
419// closedViewJS is "" once job id's view is closed: the details shut, the
420// summary saying so, the view and any scrim gone, and focus back on the
421// pill's summary.
422func closedViewJS(id string) string {
423 return `(() => { const j = document.querySelector('#ask-panel li.job[data-job="` + id + `"]'), p = [];
424 if (!j) return "no job";
425 const s = j.querySelector("summary");
426 if (j.querySelector("details").open) p.push("the details are open");
427 if (s.getAttribute("aria-expanded") !== "false") p.push("aria-expanded " + s.getAttribute("aria-expanded"));
428 if (j.querySelector(".jobview").checkVisibility()) p.push("the view shows");
429 if (document.querySelector("#ask-panel .scrim")?.checkVisibility()) p.push("the scrim shows");
430 if (document.activeElement !== s) p.push("focus on " + document.activeElement.tagName + "." + document.activeElement.className);
431 return p.join("; "); })()`
432}
433
434// The job view on a wide screen (plan decision D13): tapping the pill opens
435// it as a popover beside the pill, inside the docked chat, with focus on
436// its Close; Escape closes it (and only it: the chat stays open), and
437// focus goes back to the pill. Close, a tap on the pill and a tap outside
438// close it too. A new revision while it is open keeps it open, with the
439// new status, and focus where it was. When it started moves on in words
440// as the server says it, the day in Base's time zone.
441func TestAskJobViewOpens(t *testing.T) {
442 auckland, err := time.LoadLocation("Pacific/Auckland")
443 if err != nil {
444 t.Fatal(err)
445 }
446 rig, f, _ := askBaseWith(t, func(c *config.Config) { c.Timezone = auckland })
447 openPanel(t, rig)
448 wait := func(js string) {
449 t.Helper()
450 drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second)))
451 }
452 pending := func(js string) string { return harness.Evaluate[string](rig, js) }
453 settle := func() { drive(t, rig, chromedp.Sleep(250*time.Millisecond)) }
454 // A job at the top of the conversation, with no room above it: its
455 // view opens below it.
456 first := f.AskJob("Fixing the login")
457 for _, line := range []string{"On it.", "Got it.", "I'll let you know.", "Anything else?", "Sure.", "On it."} {
458 f.Say(line)
459 }
460 id := f.AskJob("Building your website")
461 job := `#ask-panel li.job[data-job="` + id + `"]`
462 summary, view := job+` summary`, job+` .jobview`
463 wait(`!!document.querySelector(` + strconv.Quote(summary) + `) && document.querySelectorAll("#ask-panel .msg.coordinator").length === 6`)
464 drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel li.job[data-job="`+first+`"] summary`)))
465 settle()
466 if got := pending(jobViewJS(first, "Getting started", "below")); got != "" {
467 t.Errorf("the first job, tapped open: %s", got)
468 }
469 // Tapping another pill closes the first view and opens its own.
470 drive(t, rig, chromedp.Click(chromedp.CSS(summary)))
471 settle()
472 if got := pending(jobViewJS(id, "Getting started", "above")); got != "" {
473 t.Errorf("tapped open: %s", got)
474 }
475 if n := harness.Evaluate[int](rig, `[...document.querySelectorAll("#ask-panel .jobview")].filter(v => v.checkVisibility()).length`); n != 1 {
476 t.Errorf("%d job views show", n)
477 }
478 if got := pending(`(() => { const v = document.querySelector(` + strconv.Quote(view) + `);
479 return [v.querySelector(".jv-detail")?.textContent, v.querySelector(".jv-started")?.textContent].join("|"); })()`); got != "Working out what's needed.|Started just now" {
480 t.Errorf("the view's detail and when it started: %s", got)
481 }
482 // The title's arrow: hidden at rest where there is a pointer that
483 // hovers; always there on a touch screen (headless Chromium may say
484 // it is one).
485 arrow := `getComputedStyle(document.querySelector(` + strconv.Quote(view+` .jv-title svg`) + `)).opacity`
486 if got := pending(`matchMedia("(hover: none)").matches + " " + ` + arrow); got != "true 1" && got != "false 0" {
487 t.Errorf("the title's arrow at rest (touch screen, opacity): %s", got)
488 }
489 rig.Screen("#ask-panel", "a working job's view, open beside its pill")
490 for _, scheme := range []string{"dark", "light"} {
491 drive(t, rig, prefersScheme(scheme), chromedp.Sleep(300*time.Millisecond))
492 mobileShot(t, rig, "jobview-working-desktop-"+scheme)
493 checkContrast(t, rig, "the job view, "+scheme, view+" h3", view+" .jv-stage", view+" .jv-detail", view+" .jv-started")
494 }
495 // The title's arrow shows on keyboard focus (Shift+Tab from Close).
496 drive(t, rig, chromedp.KeyEvent(kb.Tab, chromedp.KeyModifiers(chromedp.ModifierShift)))
497 settle()
498 if got := pending(`document.activeElement.className + " " + ` + arrow); got != "jv-title 1" {
499 t.Errorf("the title focused: %s", got)
500 }
501 drive(t, rig, prefersScheme("dark"))
502
503 // Escape closes the view, and the chat stays open.
504 drive(t, rig, chromedp.KeyEvent(kb.Escape))
505 settle()
506 if got := pending(closedViewJS(id)); got != "" {
507 t.Errorf("after Escape: %s", got)
508 }
509 if !harness.Evaluate[bool](rig, settledJS) {
510 t.Errorf("Escape on the job view closed the chat")
511 }
512
513 // Close, a second tap on the pill, and a tap outside it close it too.
514 drive(t, rig, chromedp.Click(chromedp.CSS(summary)))
515 settle()
516 drive(t, rig, chromedp.Click(chromedp.CSS(view+` [data-job-close]`)))
517 settle()
518 if got := pending(closedViewJS(id)); got != "" {
519 t.Errorf("after Close: %s", got)
520 }
521 drive(t, rig, chromedp.Click(chromedp.CSS(summary)))
522 settle()
523 drive(t, rig, chromedp.Click(chromedp.CSS(summary)))
524 settle()
525 if got := pending(closedViewJS(id)); got != "" {
526 t.Errorf("after a second tap on the pill: %s", got)
527 }
528 drive(t, rig, chromedp.Click(chromedp.CSS(summary)))
529 settle()
530 drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel .ask-head h2`)))
531 settle()
532 // A tap on the chat's own words (nothing to focus there) gives focus
533 // back to the pill.
534 if got := pending(closedViewJS(id)); got != "" {
535 t.Errorf("after a tap outside on the chat's header: %s", got)
536 }
537 // A tap on something that takes focus, the composer, keeps it there.
538 drive(t, rig, chromedp.Click(chromedp.CSS(summary)))
539 settle()
540 drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel textarea`)))
541 settle()
542 if got := pending(`(() => { const j = document.querySelector(` + strconv.Quote(job) + `);
543 return j.querySelector("details").open + " " + j.querySelector(".jobview").checkVisibility() + " " + j.querySelector("summary").getAttribute("aria-expanded") + " " + document.activeElement.tagName; })()`); got != "false false false TEXTAREA" {
544 t.Errorf("after a tap outside on the composer: open, shown, expanded, focus: %s", got)
545 }
546
547 // A new revision while it is open: still open, the new status, focus
548 // on Close.
549 drive(t, rig, chromedp.Click(chromedp.CSS(summary)))
550 settle()
551 f.SetJob(id, "working", "Building it", "Writing the pages.")
552 wait(`document.querySelector(` + strconv.Quote(job) + `).dataset.revision === "2"`)
553 settle()
554 if got := pending(jobViewJS(id, "Building it", "above")); got != "" {
555 t.Errorf("redrawn while open: %s", got)
556 }
557 try := equipo.Link{Label: "Try it", URL: "https://preview.example.test/"}
558 f.SetJob(id, "ready", "Ready to try", "Have a look.", try)
559 wait(`document.querySelector(` + strconv.Quote(job) + `).dataset.revision === "3"`)
560 settle()
561 if got := pending(jobViewJS(id, "Ready to try", "above")); got != "" {
562 t.Errorf("ready, open: %s", got)
563 }
564 if got := pending(`(() => { const a = document.querySelector(` + strconv.Quote(view+` a.jv-go`) + `); return a ? a.textContent + " " + a.href + " " + a.target : "none"; })()`); got != "Try it https://preview.example.test/ _blank" {
565 t.Errorf("a ready view's link: %s", got)
566 }
567 for _, scheme := range []string{"dark", "light"} {
568 drive(t, rig, prefersScheme(scheme), chromedp.Sleep(300*time.Millisecond))
569 mobileShot(t, rig, "jobview-ready-desktop-"+scheme)
570 checkContrast(t, rig, "a ready job's view, "+scheme, view+" h3", view+" .jv-stage", view+" .jv-go")
571 }
572 drive(t, rig, prefersScheme("dark"))
573 for _, st := range []struct{ state, result string }{{"asking", "One question for you"}, {"done", "Done"}, {"stopped", "Stopped"}} {
574 f.SetJob(id, st.state, st.result, "One sentence more.", try)
575 wait(`document.querySelector(` + strconv.Quote(view+` .jv-stage`) + `).textContent === ` + strconv.Quote(st.result))
576 for _, scheme := range []string{"dark", "light"} {
577 drive(t, rig, prefersScheme(scheme), chromedp.Sleep(300*time.Millisecond))
578 mobileShot(t, rig, "jobview-"+st.state+"-desktop-"+scheme)
579 }
580 drive(t, rig, prefersScheme("dark"))
581 }
582
583 // When it started, as the server words it: hours, then the day in
584 // Base's time zone (Auckland's, a day ahead of UTC's evening).
585 started := func(at time.Time, want string) {
586 t.Helper()
587 f.SetJobStarted(id, at)
588 wait(`document.querySelector(` + strconv.Quote(view+` .jv-started`) + `)?.textContent === ` + strconv.Quote(want))
589 }
590 started(time.Now().Add(-3*time.Hour-time.Minute), "Started 3 hours ago")
591 old := time.Date(2026, 3, 4, 20, 0, 0, 0, time.UTC)
592 started(old, "Started on "+old.In(auckland).Format("2 January"))
593 if old.In(auckland).Day() == old.Day() {
594 t.Fatalf("the day is the same in both zones")
595 }
596 if got := pending(jobViewJS(id, "Stopped", "above")); got != "" {
597 t.Errorf("after the redraws: %s", got)
598 }
599}
600
601// The job view goes with the chat: closing the docked chat from the
602// keyboard (its X, with Enter), or a narrowing screen that puts the chat
603// away, closes the view too. Escape afterwards is the page's own again,
604// and focus stays where closing the chat put it.
605func TestAskJobViewClosesWithTheChat(t *testing.T) {
606 rig, f := askBase(t)
607 openPanel(t, rig)
608 id := f.AskJob("Building your website")
609 job := `#ask-panel li.job[data-job="` + id + `"]`
610 drive(t, rig, chromedp.WaitVisible(chromedp.CSS(job+" summary")),
611 chromedp.Evaluate[chromedp.Void](`window.__esc = []; document.addEventListener("keydown", (e) => { if (e.key === "Escape") window.__esc.push(e.defaultPrevented); })`))
612 gone := `(() => { const j = document.querySelector(` + strconv.Quote(job) + `), p = [];
613 if (j.querySelector("details").open || j.querySelector("summary").getAttribute("aria-expanded") !== "false") p.push("still open");
614 if (j.querySelector(".jobview").matches(":popover-open")) p.push("the popover shows");
615 if (document.querySelector("#ask-panel .scrim")?.checkVisibility()) p.push("the scrim shows");
616 return p.join("; "); })()`
617 escape := func(when, focus string) {
618 t.Helper()
619 drive(t, rig, chromedp.Evaluate[chromedp.Void](`window.__esc = []`), chromedp.KeyEvent(kb.Escape), chromedp.Sleep(200*time.Millisecond))
620 if got := harness.Evaluate[string](rig, `JSON.stringify(window.__esc) + " " + document.activeElement.className`); got != "[false] "+focus {
621 t.Errorf("%s, Escape (taken, focus): %s, want [false] %s", when, got, focus)
622 }
623 }
624
625 // Closed from the keyboard: the chat's X, with Enter.
626 drive(t, rig, chromedp.Click(chromedp.CSS(job+" summary")), chromedp.Sleep(250*time.Millisecond),
627 chromedp.Focus(chromedp.CSS(`#ask-panel [data-ask-close]`)), chromedp.KeyEvent(kb.Enter), chromedp.Sleep(600*time.Millisecond))
628 if got := harness.Evaluate[string](rig, gone); got != "" {
629 t.Errorf("the chat closed from its X: %s", got)
630 }
631 escape("the chat closed from its X", "base-btn")
632
633 // Put away by a narrowing screen: a wide screen's open chat is not a
634 // phone's, which opens from the springboard.
635 drive(t, rig, chromedp.Click(chromedp.CSS(".base-btn")), chromedp.Poll[chromedp.Void](settledJS, chromedp.WithPollingTimeout(3*time.Second)),
636 chromedp.WaitVisible(chromedp.CSS(job+" summary")), chromedp.Click(chromedp.CSS(job+" summary")), chromedp.Sleep(250*time.Millisecond))
637 if !harness.Evaluate[bool](rig, `document.querySelector(`+strconv.Quote(job+" .jobview")+`).matches(":popover-open")`) {
638 t.Fatalf("the view did not open again")
639 }
640 drive(t, rig, chromedp.EmulateViewport(390, 844), chromedp.Sleep(600*time.Millisecond))
641 if got := harness.Evaluate[string](rig, gone); got != "" {
642 t.Errorf("narrowed to a phone: %s", got)
643 }
644 drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.body.focus()`))
645 escape("narrowed to a phone", "")
646 drive(t, rig, chromedp.EmulateViewport(1280, 800), chromedp.Sleep(600*time.Millisecond))
647 if got := harness.Evaluate[string](rig, gone); got != "" {
648 t.Errorf("widened again: %s", got)
649 }
650}
651
652// A job redrawn (a new revision) keeps focus where it was: on a decision
653// under its pill, which moves to the new pill; on the pill or its link to
654// the work; and with its view open, on the view's title, its link to the
655// work, or Close. A decision drawn before its job keeps focus as the job
656// arrives and the decision moves under it.
657func TestAskJobRedrawKeepsFocus(t *testing.T) {
658 rig, f := askBase(t)
659 // Wide enough for a ready pill's link beside its status.
660 drive(t, rig, chromedp.EmulateViewport(1500, 900))
661 openPanel(t, rig)
662 wait := func(js string) {
663 t.Helper()
664 drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second)))
665 }
666 options := []equipo.Option{{Value: "go", Label: "Go ahead"}, {Value: "later", Label: "Not yet"}}
667 try := equipo.Link{Label: "Try it", URL: "https://preview.example.test/"}
668 id := f.AskJob("Building your website")
669 f.SetJob(id, "ready", "Ready to try", "Have a look.", try)
670 goAhead := f.AskOutcomeFor(id, "This is ready. Go ahead?", options, nil)
671 job := `#ask-panel li.job[data-job="` + id + `"]`
672 wait(jobJS(id) + ` === ` + strconv.Quote("1 ready Ready to try [Try it] "+goAhead))
673 focusedJS := func(sel string) string {
674 return `(() => { const a = document.activeElement, want = document.querySelector(` + strconv.Quote(sel) + `);
675 if (!want) return "no " + ` + strconv.Quote(sel) + `;
676 return a === want ? "" : "focus on " + a.tagName + "." + a.className + " " + (a.textContent || "").trim().slice(0, 30); })()`
677 }
678 rev := 2
679 redraw := func(where, sel string) {
680 t.Helper()
681 drive(t, rig, chromedp.Focus(chromedp.CSS(sel)))
682 if got := harness.Evaluate[string](rig, focusedJS(sel)); got != "" {
683 t.Fatalf("focusing %s: %s", where, got)
684 }
685 rev++
686 f.SetJob(id, "ready", "Ready to try", fmt.Sprintf("Revision %d.", rev), try)
687 wait(`document.querySelector(` + strconv.Quote(job) + `)?.dataset.revision === "` + strconv.Itoa(rev) + `"`)
688 drive(t, rig, chromedp.Sleep(150*time.Millisecond))
689 if got := harness.Evaluate[string](rig, focusedJS(sel)); got != "" {
690 t.Errorf("focused on %s, redrawn: %s", where, got)
691 }
692 }
693 redraw("the go-ahead's button", job+` ol.job-cards [data-card="`+goAhead+`"] button[value="go"]`)
694 redraw("the pill", job+` summary`)
695 redraw("the pill's link", job+` .pill-link`)
696 drive(t, rig, chromedp.Click(chromedp.CSS(job+` summary`)), chromedp.Sleep(250*time.Millisecond))
697 redraw("the job view's title", job+` .jobview a.jv-title`)
698 redraw("the job view's link", job+` .jobview a.jv-go`)
699 redraw("the job view's Close", job+` .jobview [data-job-close]`)
700 if got := harness.Evaluate[string](rig, `(() => { const j = document.querySelector(`+strconv.Quote(job)+`);
701 return j.querySelector("details").open + " " + j.querySelector(".jobview").matches(":popover-open"); })()`); got != "true true" {
702 t.Errorf("after the redraws the view (open, shown): %s", got)
703 }
704 drive(t, rig, chromedp.KeyEvent(kb.Escape), chromedp.Sleep(200*time.Millisecond))
705
706 // A decision of a job not yet drawn, focused, as the job arrives. (The
707 // fake numbers its cards in turn: the job is the card after it.)
708 n, err := strconv.Atoi(strings.TrimPrefix(goAhead, "c_"))
709 if err != nil {
710 t.Fatalf("card id %q", goAhead)
711 }
712 early := f.AskOutcomeFor(fmt.Sprintf("c_%04d", n+2), "And this one?", options, nil)
713 button := `#ask-panel [data-card="` + early + `"] button[value="go"]`
714 wait(`!!document.querySelector('#ask-panel .thread > [data-card="` + early + `"]')`)
715 drive(t, rig, chromedp.Focus(chromedp.CSS(button)))
716 if other := f.AskJob("Fixing the login"); other != fmt.Sprintf("c_%04d", n+2) {
717 t.Fatalf("the next job is %s", other)
718 }
719 wait(`!!document.querySelector('#ask-panel li.job ol.job-cards > [data-card="` + early + `"]')`)
720 drive(t, rig, chromedp.Sleep(150*time.Millisecond))
721 if got := harness.Evaluate[string](rig, focusedJS(button)); got != "" {
722 t.Errorf("focused on a decision as its job arrived: %s", got)
723 }
724}
725
726// Without scripts the job view opens in place, in the conversation's flow
727// (plan decision D13): opened, it pushes what follows down, the decision
728// under its pill and the next job, rather than covering them.
729func TestAskJobViewScriptsOffInPlace(t *testing.T) {
730 rig, f := askBase(t)
731 options := []equipo.Option{{Value: "go", Label: "Go ahead"}, {Value: "later", Label: "Not yet"}}
732 try := equipo.Link{Label: "Try it", URL: "https://preview.example.test/"}
733 id := f.AskJob("Building your website")
734 f.SetJob(id, "ready", "Ready to try", "Have a look.", try)
735 goAhead := f.AskOutcomeFor(id, "This is ready. Go ahead?", options, nil)
736 next := f.AskJob("Fixing the login")
737 drive(t, rig, scriptsDisabled(true))
738 defer drive(t, rig, scriptsDisabled(false))
739 for _, view := range []chromedp.Action[chromedp.Void]{chromedp.EmulateViewport(1280, 800), chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))} {
740 page := `.ask-page li.job[data-job="` + id + `"]`
741 drive(t, rig, view, chromedp.Navigate(rig.Origin+"/ask"), chromedp.WaitVisible(chromedp.CSS(page+" summary")),
742 chromedp.Click(chromedp.CSS(page+" summary")), chromedp.Sleep(200*time.Millisecond))
743 if got := harness.Evaluate[string](rig, `(() => { const j = document.querySelector(`+strconv.Quote(page)+`), v = j.querySelector(".jobview"), p = [];
744 if (!j.querySelector("details").open) p.push("closed");
745 if (!v.checkVisibility()) return p.concat("the view is not drawn").join("; ");
746 const r = v.getBoundingClientRect(), s = j.querySelector("summary").getBoundingClientRect();
747 if (r.top < s.bottom || r.top - s.bottom > 12 || r.left < s.left - 0.5) p.push("not under the pill: " + JSON.stringify(r) + " pill " + JSON.stringify(s));
748 if (getComputedStyle(v).position !== "static") p.push("out of the flow: " + getComputedStyle(v).position);
749 const clear = (sel, what) => {
750 const el = document.querySelector(sel);
751 if (!el) return p.push("no " + what);
752 el.scrollIntoView({ block: "center" });
753 const o = el.getBoundingClientRect(), v2 = v.getBoundingClientRect();
754 if (o.top < v2.bottom && o.bottom > v2.top && o.left < v2.right && o.right > v2.left) p.push("covers " + what + ": " + JSON.stringify(o) + " view " + JSON.stringify(v2));
755 const at = document.elementFromPoint(o.left + o.width / 2, o.top + Math.min(o.height / 2, 20));
756 if (!el.contains(at)) p.push(what + " covered by " + (at && at.className));
757 };
758 clear(`+strconv.Quote(page+` ol.job-cards [data-card="`+goAhead+`"]`)+`, "the go-ahead");
759 clear(`+strconv.Quote(`.ask-page li.job[data-job="`+next+`"] .pill`)+`, "the next job");
760 if (document.documentElement.scrollWidth > document.documentElement.clientWidth) p.push("the page overflows");
761 return p.join("; "); })()`); got != "" {
762 t.Errorf("scripts off, the job view opened: %s", got)
763 }
764 rig.Screen(".ask-page", "the job view opened in place, scripts off")
765 }
766}
767
768// A paused job (clearer paused work, 2026-11-24): the pill's clock mark on
769// the calm indigo, its status as Equipo wrote it, and in its view the
770// sentence and "Carry on now" with the line under it, readable in both
771// themes. Carry on now posts once, the card's revision and a key (the
772// body's idempotency_key and the Idempotency-Key header), and the job
773// redraws in place, working, with no button. Stopped's mark is a square.
774func TestAskJobPausedCarriesOn(t *testing.T) {
775 rig, f := askBase(t)
776 openPanel(t, rig)
777 wait := func(js string) {
778 t.Helper()
779 drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second)))
780 }
781 settle := func() { drive(t, rig, chromedp.Sleep(250*time.Millisecond)) }
782 stopped := f.AskJob("Invoice reminders")
783 f.SetJob(stopped, "stopped", "Stopped: needs you", "It needs you.")
784 id := f.AskJob("Bakery website")
785 f.PauseJob(id, "Paused until 21:00", "It has reached the limit on its plan for now. It will carry on by itself at 21:00 (in 9 hours).", true)
786 job := `#ask-panel li.job[data-job="` + id + `"]`
787 wait(`document.querySelector(` + strconv.Quote(job) + `)?.dataset.state === "paused" && document.querySelector(` + strconv.Quote(job+` .jv-act`) + `) !== null`)
788 drive(t, rig, prefersScheme("dark"), chromedp.Sleep(300*time.Millisecond))
789 if got := harness.Evaluate[string](rig, `[document.querySelector(`+strconv.Quote(job+` .pill-stage`)+`).textContent,
790 document.querySelector(`+strconv.Quote(job+` .pill-main .state-mark path`)+`).getAttribute("d"),
791 getComputedStyle(document.querySelector(`+strconv.Quote(job+` .pill-main .state-mark`)+`)).backgroundColor,
792 document.querySelector('#ask-panel li.job[data-job="`+stopped+`"] .pill-main .state-mark path').getAttribute("d")].join("|")`); got !=
793 "Paused until 21:00|M19.2 12a7.2 7.2 0 1 1-14.4 0a7.2 7.2 0 1 1 14.4 0zM12 8.6V12l2.4 1.6|rgb(174, 184, 255)|M9 7.5h6a1.5 1.5 0 0 1 1.5 1.5v6a1.5 1.5 0 0 1-1.5 1.5H9a1.5 1.5 0 0 1-1.5-1.5V9A1.5 1.5 0 0 1 9 7.5z" {
794 t.Errorf("paused pill (status|mark|colour|stopped's mark): %s", got)
795 }
796 drive(t, rig, chromedp.Click(chromedp.CSS(job+` summary`)))
797 settle()
798 view := job + ` .jobview`
799 if got := harness.Evaluate[string](rig, `(() => { const v = document.querySelector(`+strconv.Quote(view)+`);
800 return [v.checkVisibility(), v.querySelector(".jv-stage").textContent, v.querySelector(".jv-detail").textContent,
801 v.querySelector(".jv-act button").textContent, v.querySelector(".jv-act small").textContent].join("|"); })()`); got !=
802 "true|Paused until 21:00|It has reached the limit on its plan for now. It will carry on by itself at 21:00 (in 9 hours).|Carry on now|Uses your other plan, at no extra cost." {
803 t.Errorf("the paused job's view: %s", got)
804 }
805 for _, scheme := range []string{"dark", "light"} {
806 drive(t, rig, prefersScheme(scheme), chromedp.Sleep(300*time.Millisecond))
807 mobileShot(t, rig, "jobview-paused-"+scheme)
808 checkContrast(t, rig, "the paused job's view, "+scheme, view+" .jv-stage", view+" .jv-detail", view+" .jv-act button", view+" .jv-act small")
809 iconContrast(t, rig, "the paused mark, "+scheme, job+" .pill-main .state-mark svg")
810 }
811 if got := harness.Evaluate[string](rig, `getComputedStyle(document.querySelector(`+strconv.Quote(job+` .pill-main .state-mark`)+`)).backgroundColor`); got != "rgb(75, 85, 201)" {
812 t.Errorf("paused's colour in light: %s", got)
813 }
814 drive(t, rig, prefersScheme("dark"))
815 rig.Screen("#ask-panel", "a paused job's view, with Carry on now")
816
817 // Equipo not answering: the view says so under the button (Fable's
818 // review S3: on a phone the sheet hides the status line), and the
819 // button comes back. Pressing again sends the same key.
820 f.FailCarryOn(503)
821 drive(t, rig, chromedp.Click(chromedp.CSS(view+` .jv-act button`)))
822 wait(`document.querySelector(` + strconv.Quote(view+` .jv-act .jv-error[role="status"]`) + `)?.textContent === "That didn't send. Try again." &&
823 !document.querySelector(` + strconv.Quote(view+` .jv-act button`) + `).disabled`)
824 // Said once (Fable's re-review R4): in the view's live region only, not
825 // on the status line too.
826 if got := harness.Evaluate[string](rig, `[...document.querySelectorAll("#ask-panel [role=status], #ask-panel [role=alert], #ask-panel [aria-live]")]
827 .filter(el => el.textContent.includes("That didn't send")).map(el => el.className).join("|")`); got != "jv-error" {
828 t.Errorf("the failure is said in %q, want once, in the view (jv-error)", got)
829 }
830 f.FailCarryOn(0)
831 drive(t, rig, chromedp.Click(chromedp.CSS(view+` .jv-act button`)))
832 wait(`document.querySelector(` + strconv.Quote(job) + `)?.dataset.state === "working"`)
833 settle()
834 if n := harness.Evaluate[int](rig, `document.querySelectorAll(`+strconv.Quote(job+` .jv-act`)+`).length`); n != 0 {
835 t.Errorf("Carry on now still shows on a working job")
836 }
837 calls := f.Calls("/api/v1/assistant/cards/" + id + "/carry_on")
838 if len(calls) != 2 {
839 t.Fatalf("Carry on now went %d times, want the failure and its retry", len(calls))
840 }
841 key := calls[0].Header.Get("Idempotency-Key")
842 if want := `{"idempotency_key":"` + key + `","revision":3}`; key == "" || strings.TrimSpace(calls[0].Body) != want || calls[1].Body != calls[0].Body {
843 t.Errorf("sent %s then %s with key %q, want %s twice", calls[0].Body, calls[1].Body, key, want)
844 }
845}
846