Seed glances from the snapshot; steady thinking bubble
Review fixes: the relay now passes a glance only for a person turn it knows to be in flight, seeded at connect from the conversation's snapshot and kept by turn events; unknown, team_update and ended turns are dropped, including after the current turn ends. The glance's 1.5 s hold now starts when its fade-in actually starts playing, and the test measures from when it is visible. Paul's report from the live build: swapping the bubble's line no longer blanks or bounces. Dots, glance and activity lines share one grid cell and crossfade in place (the outgoing line fades from full, so a swap mid-crossfade is never blank); the line stays on one line with an ellipsis; the bubble keeps its height and only grows wider, easing over 200 ms, until the reply. Reduced motion swaps instantly by the same size rules.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+255
−50
internal/browsertest/thinking_test.go+93 −10internal/look/static/app.css+7 −0internal/look/static/ask.js+61 −12internal/web/ask_relay.go+46 −19internal/web/relay_internal_test.go+48 −9
diff --git a/internal/browsertest/thinking_test.go b/internal/browsertest/thinking_test.go| index d47ea0e..006db00 100644 |
| --- a/internal/browsertest/thinking_test.go |
| +++ b/internal/browsertest/thinking_test.go |
| @@ -382,15 +382,20 @@ func TestAskStreamCountsBytes(t *testing.T) { |
| } |
| // watchJS keeps the thinking bubble in window.__b, where it is, and from |
| -// now on the page's times (performance.now) at which it first says g |
| -// (window.__g) and first says anything else after that (window.__x). |
| +// now on, frame by frame, the page's times (performance.now) at which g |
| +// first shows in it, its line laid out and not transparent (window.__g), |
| +// and then first starts to go: fading out, or replaced (window.__x). |
| func watchJS(g string) string { |
| - return `(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `); |
| + return `(() => { const b = document.querySelector(` + strconv.Quote(thinkingSel) + `), g = ` + strconv.Quote(g) + `; |
| window.__b = b; window.__at = [...b.parentElement.children].indexOf(b); window.__r = b.getBoundingClientRect(); window.__g = 0; window.__x = 0; |
| - const look = () => { const t = b.textContent, now = performance.now(); |
| - if (!window.__g && t === ` + strconv.Quote(g) + `) window.__g = now; |
| - if (window.__g && !window.__x && t !== ` + strconv.Quote(g) + `) window.__x = now; }; |
| - new MutationObserver(look).observe(b, { subtree: true, childList: true, characterData: true }); })()` |
| + const look = () => { const l = b.querySelector(":scope > .think-line"), now = performance.now(); |
| + const s = l && getComputedStyle(l), says = b.textContent === g, kid = l && [...l.children].find(c => c.textContent === g); |
| + if (!window.__g && says && kid && s.display !== "none" && Number(s.opacity) > 0 && Number(getComputedStyle(kid).opacity) > 0) window.__g = now; |
| + const fading = l && l.getAnimations({ subtree: true }).some(a => { const k = a.effect.getKeyframes(); |
| + return (a.effect.target === l || a.effect.target === kid) && Number(k[0].opacity) === 1 && Number(k[k.length - 1].opacity) === 0; }); |
| + if (window.__g && !window.__x && (!says || fading)) window.__x = now; |
| + if (!window.__x && b.isConnected) requestAnimationFrame(look); }; |
| + requestAnimationFrame(look); })()` |
| } |
| // inPlaceJS reports how the watched bubble is not the reply text, where it |
| @@ -424,7 +429,9 @@ func TestAskGlance(t *testing.T) { |
| f.Glance(id, " "+glance+"\n") |
| th.wait(`window.__g > 0`) |
| th.wait(`(() => { const l = document.querySelector(` + strconv.Quote(thinkingSel+" .think-line") + `); |
| - return l && l.getAnimations().some(a => a.playState === "running" && Number(a.effect.getKeyframes()[0].opacity) === 0); })()`) |
| + return l && l.getAnimations({ subtree: true }).some(a => a.playState === "running" && a.effect.target.textContent === ` + strconv.Quote(glance) + ` && |
| + Number(a.effect.getKeyframes()[0].opacity) === 0); })()`) |
| + th.wait(`!window.__b.querySelector(".dots")`) // faded out under the glance |
| if got := th.eval(`(() => { const b = window.__b, p = []; |
| if (b.querySelector(".dots")) p.push("the dots still show"); |
| if (!b.matches(".thinking") || !b.querySelector("canvas")) p.push("no longer thinking, with its spark"); |
| @@ -445,8 +452,8 @@ func TestAskGlance(t *testing.T) { |
| const soon = "Two meetings and a review." |
| f.Answer(id, soon, true) |
| th.wait(`window.__b.dataset.id && window.__b.textContent === ` + strconv.Quote(soon) + ` && !document.querySelector("#ask-panel .think-line")`) |
| - if held := harness.Evaluate[float64](rig, `window.__x - window.__g`); held < 1450 || held > 2500 { |
| - t.Errorf("a reply 200 ms after the glance replaced it after %.0f ms, want about 1500", held) |
| + if held := harness.Evaluate[float64](rig, `window.__x - window.__g`); held < 1480 || held > 2300 { |
| + t.Errorf("a reply 200 ms after the glance showed began to replace it %.0f ms after it showed, want about 1500", held) |
| } |
| if got := th.eval(inPlaceJS(soon)); got != "" { |
| t.Errorf("the reply soon after a glance: %s", got) |
| @@ -521,3 +528,79 @@ func TestAskGlance(t *testing.T) { |
| f.Answer(id, "All done.", false) |
| th.wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id && m.textContent === "All done.") && !document.querySelector("#ask-panel .thinking")`) |
| } |
| + |
| +// steadyJS watches the thinking bubble frame by frame from now on, until |
| +// window.__stop: in window.__steady, every frame's problem (no words or |
| +// dots in view, the line's text empty once words have come, the bubble's |
| +// height changing, its width shrinking), and in window.__frames how many |
| +// frames it saw, and in window.__anims how many animations were started |
| +// in it. |
| +const steadyJS = `(() => { const b = document.querySelector("#ask-panel .thread > li.msg.coordinator.thinking"), l = b.querySelector(":scope > .think-line"); |
| + const h0 = b.getBoundingClientRect().height; let w = b.getBoundingClientRect().width, worded = false; |
| + window.__steady = []; window.__frames = 0; window.__stop = false; window.__anims = 0; |
| + if (!window.__animWrapped) { const animate = Element.prototype.animate; window.__animWrapped = true; |
| + Element.prototype.animate = function (...args) { if (this.closest && this.closest(".thinking")) window.__anims++; return animate.apply(this, args); }; } |
| + const look = () => { const r = b.getBoundingClientRect(), p = []; |
| + window.__frames++; |
| + if (![...l.children].some(c => Number(getComputedStyle(c).opacity) > 0)) p.push("nothing in view"); |
| + if (l.textContent) worded = true; else if (worded) p.push("the line is empty"); |
| + if (Math.abs(r.height - h0) > 0.5) p.push("height " + r.height + ", was " + h0); |
| + if (r.width < w - 0.5) p.push("width " + r.width + ", was " + w); |
| + w = Math.max(w, r.width); |
| + if (p.length && window.__steady.length < 20) window.__steady.push(p.join(", ")); |
| + if (!window.__stop && b.isConnected) requestAnimationFrame(look); }; |
| + requestAnimationFrame(look); })()` |
| + |
| +// While the Assistant thinks, its bubble holds steady as its line changes |
| +// (dots, then one activity line after another, long and short): the new |
| +// words fade in over the old as they fade out, never a blank moment; the |
| +// line stays on one line (a long one ends in an ellipsis); the bubble's |
| +// height holds and its width only grows, easing. With reduced motion the |
| +// swaps are instant, by the same size rules. |
| +func TestAskThinkingHoldsSteady(t *testing.T) { |
| + rig, f := askBase(t) |
| + long := "Reading through every one of your projects, their open tasks, recent changes and the notes your team left on each of them"[:120] |
| + lines := []string{"Looking at what I can do…", "Checking", long, "Reading your recent projects and what changed in them…"} |
| + for _, still := range []bool{false, true} { |
| + name := map[bool]string{false: "moving", true: "reduced motion"}[still] |
| + t.Run(name, func(t *testing.T) { |
| + th := thinking{t, rig, f} |
| + drive(t, rig, reducedMotion("dark", still)) |
| + defer drive(t, rig, reducedMotion("dark", false)) |
| + openPanel(t, rig) |
| + id := th.send("What's going on?") |
| + drive(t, rig, chromedp.Sleep(300*time.Millisecond), chromedp.Evaluate[chromedp.Void](steadyJS)) |
| + for i, line := range lines { |
| + f.Emit("activity", map[string]string{"turn_id": id, "label": line}) |
| + if i == 1 { |
| + // One comes hard on the last's heels, mid-crossfade. |
| + drive(t, rig, chromedp.Sleep(100*time.Millisecond)) |
| + continue |
| + } |
| + th.wait(`document.querySelector(` + strconv.Quote(thinkingSel+" .think-line") + `).textContent === ` + strconv.Quote(line)) |
| + if line == long { |
| + if got := th.eval(`(() => { const l = document.querySelector(` + strconv.Quote(thinkingSel+" .think-line") + `), c = l.lastElementChild, s = getComputedStyle(c); |
| + return [c.getClientRects().length, Math.round(c.getBoundingClientRect().height) <= Math.ceil(parseFloat(s.lineHeight)), c.scrollWidth > c.clientWidth, s.textOverflow].join(" "); })()`); got != "1 true true ellipsis" { |
| + t.Errorf("the 120-character line: boxes, one line high, clipped, overflow %s; want 1 true true ellipsis", got) |
| + } |
| + } |
| + drive(t, rig, chromedp.Sleep(400*time.Millisecond)) |
| + } |
| + drive(t, rig, chromedp.Evaluate[chromedp.Void](`window.__stop = true`)) |
| + if got := th.eval(`JSON.stringify(window.__steady)`); got != "[]" { |
| + t.Errorf("thinking through %d lines: %s", len(lines), got) |
| + } |
| + if n := harness.Evaluate[int](rig, `window.__frames`); n < 20 { |
| + t.Errorf("watched only %d frames", n) |
| + } |
| + if n := harness.Evaluate[int](rig, `window.__anims`); (n == 0) != still { |
| + t.Errorf("reduced motion %v: %d animations in the bubble", still, n) |
| + } |
| + if !still { |
| + rig.Screen("#ask-panel", "the thinking bubble after a long activity line") |
| + } |
| + drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.stop`))) |
| + th.wait(`!document.querySelector("#ask-panel .thinking")`) |
| + }) |
| + } |
| +} |
diff --git a/internal/look/static/app.css b/internal/look/static/app.css| index 4c1bd3f..c4524bd 100644 |
| --- a/internal/look/static/app.css |
| +++ b/internal/look/static/app.css |
| @@ -728,6 +728,13 @@ aside.ask-panel { position: absolute; top: 16px; right: 16px; bottom: 16px; widt |
| @keyframes think-dot { 0%, 70%, 100% { opacity: .45; transform: translateY(0) scale(.85); } 35% { opacity: 1; transform: translateY(-2px) scale(1); } } |
| @keyframes think-hue { 0%, 100% { color: var(--edge-a); } 33% { color: var(--edge-b); } 66% { color: var(--spk-rose); } } |
| @media (forced-colors: active) { .msg .dots i { forced-color-adjust: none; color: CanvasText; box-shadow: none; } } |
| +/* What the bubble says while it thinks (its dots, Equipo's words) sits in |
| + one grid cell of its line, so new words fade in over the old in place; |
| + always one line, ending in an ellipsis when it is too long. (ask.js |
| + keeps the bubble's width from shrinking until the reply.) */ |
| +.msg > .think-line:has(> .say, > .dots) { display: grid; } |
| +.think-line > .say, .think-line > .dots { grid-area: 1 / 1; min-width: 0; } |
| +.think-line > .say { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } |
| .ask-page { display: flex; flex-direction: column; padding: 22px 0 0; } |
| .ask-page .crumb { padding: 0 24px; } |
| .ask-page .ask-head { padding-left: 24px; } |
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js| index 3e3ce43..4b82651 100644 |
| --- a/internal/look/static/ask.js |
| +++ b/internal/look/static/ask.js |
| @@ -128,10 +128,11 @@ function start(root) { |
| waits.set(id, []); |
| const go = () => release(id); |
| setTimeout(go, 1000); // should the crossfade be cut short |
| - swap(line, () => { line.textContent = ""; write(li, line, d); }, go); |
| + swap(line, () => { unwiden(li); line.textContent = ""; write(li, line, d); }, go); |
| return; |
| } |
| for (const n of [line, li]) for (const a of n.getAnimations()) a.cancel(); |
| + unwiden(li); |
| line.style.display = ""; |
| line.textContent = ""; |
| } else if (!li) { |
| @@ -946,20 +947,64 @@ function start(root) { |
| spark.start(li); |
| scroll(); |
| } |
| - // say puts words in bubble b in place of what it says; shown runs once |
| - // they are there. |
| + // say puts words in bubble b in place of what it says (its dots, or |
| + // other words), crossfading in place: both sit in the line's one grid |
| + // cell, the new fading in as the old fade out, so the bubble is never |
| + // blank, and it keeps its height (one line, an ellipsis if need be) and |
| + // only grows wider (widen). shown runs as the words come into view. |
| + // Instant with reduced motion, or out of sight. |
| + const FADE = 260; |
| function say(b, words, shown) { |
| const line = b.querySelector(":scope > .think-line"); |
| - if (line && line.textContent !== words) swap(line, () => { line.textContent = words; if (shown) shown(); }); |
| + const now = line && line.lastElementChild; |
| + if (!line || (now && now.matches(".say") && now.textContent === words)) return; |
| + for (const c of [...line.children]) if (c !== now) c.remove(); // already on their way out |
| + const w0 = b.getBoundingClientRect().width; |
| + const next = span("say", words); |
| + line.append(next); |
| + const instant = still.matches || !line.getClientRects().length; |
| + widen(b, w0, instant); |
| + if (instant) { |
| + if (now) now.remove(); |
| + if (shown) shown(); |
| + return; |
| + } |
| + if (now) { |
| + // Fully in view as it starts to go, even if it was still fading in. |
| + for (const a of now.getAnimations()) a.cancel(); |
| + now.animate([{ opacity: 1 }, { opacity: 0 }], { duration: FADE, fill: "forwards" }).onfinish = () => now.remove(); |
| + } |
| + const fade = next.animate([{ opacity: 0 }, { opacity: 1 }], { duration: FADE }); |
| + if (shown) fade.ready.then(() => { if (fade.playState === "running") shown(); }, () => {}); |
| + } |
| + // widen keeps thinking bubble b at least as wide as it has been (w0 |
| + // before its line changed, and every width before), easing out to a |
| + // wider one; the reply lets it go (unwiden). |
| + const widths = new WeakMap(); |
| + function widen(b, w0, instant) { |
| + const keep = Math.max(widths.get(b) || 0, w0, b.getBoundingClientRect().width); |
| + widths.set(b, keep); |
| + for (const a of b.getAnimations()) if (a.id === "widen") a.cancel(); |
| + b.style.minWidth = `min(${w0}px, 100%)`; |
| + const done = () => { if (widths.get(b) === keep) b.style.minWidth = `min(${keep}px, 100%)`; }; |
| + if (instant || keep <= w0 + 0.5) return done(); |
| + const grow = b.animate([{ width: w0 + "px" }, { width: keep + "px" }], { duration: 200, easing: "cubic-bezier(.2,.8,.2,1)" }); |
| + grow.id = "widen"; |
| + grow.onfinish = done; |
| + } |
| + function unwiden(b) { |
| + widths.delete(b); |
| + for (const a of b.getAnimations()) if (a.id === "widen") a.cancel(); |
| + b.style.minWidth = ""; |
| } |
| // Equipo's glance: one line on the turn in flight, before its first |
| // words (or never). It takes the bubble's dots or activity line, and |
| // activity lines after it leave it be. It is never kept: a reload shows |
| - // the dots again. A reply that comes before the glance has shown for |
| - // HOLD waits until it has (held), then takes its place. |
| + // the dots again. A reply that comes before the glance has been in view |
| + // for HOLD waits until it has (held), then takes its place. |
| const HOLD = 1500; |
| - const glances = new Map(); // turn id: when its glance showed |
| + const glances = new Map(); // turn id: when its glance came into view (till then, when it came) |
| const waits = new Map(); // turn id: what waits for its glance |
| function glance(g) { |
| const id = g && g.turn_id, text = g && typeof g.text === "string" ? g.text.trim() : ""; |
| @@ -1025,7 +1070,7 @@ function start(root) { |
| b.removeAttribute("aria-busy"); |
| b.dataset.id = li.dataset.id; |
| const line = b.querySelector(":scope > .think-line"); |
| - const land = () => line.replaceChildren(...li.childNodes); |
| + const land = () => { unwiden(b); line.replaceChildren(...li.childNodes); }; |
| const unwrap = () => { if (line.isConnected) line.replaceWith(...line.childNodes); }; |
| if (thinking) swap(line, land, unwrap); |
| else { |
| @@ -1036,15 +1081,17 @@ function start(root) { |
| } |
| // swap changes the words in el, a bubble's line: the old ones fade out, |
| // the bubble eases to its new size (the conversation following its foot |
| - // while it is pinned there), then the new ones fade in, and done runs. |
| - // A straight swap with reduced motion, or out of sight. |
| - function swap(el, apply, done) { |
| + // while it is pinned there), then the new ones fade in (shown runs as |
| + // they start to), and done runs. A straight swap with reduced motion, or |
| + // out of sight. |
| + function swap(el, apply, done, shown) { |
| const box = el.parentElement; |
| for (const n of [el, box]) for (const a of n.getAnimations()) a.cancel(); |
| el.style.display = ""; |
| const finish = () => { if (done) done(); }; |
| if (still.matches || !el.getClientRects().length) { |
| apply(); |
| + if (shown) shown(); |
| finish(); |
| return; |
| } |
| @@ -1064,7 +1111,9 @@ function start(root) { |
| el.style.display = ""; |
| follow(); |
| out.cancel(); |
| - el.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 260, easing: "ease-out" }).onfinish = finish; |
| + const fade = el.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 260, easing: "ease-out" }); |
| + fade.onfinish = finish; |
| + if (shown) fade.ready.then(() => { if (fade.playState === "running") shown(); }, () => {}); |
| }; |
| grow.oncancel = () => { |
| el.style.display = ""; |
diff --git a/internal/web/ask_relay.go b/internal/web/ask_relay.go| index 45d5a01..349ec98 100644 |
| --- a/internal/web/ask_relay.go |
| +++ b/internal/web/ask_relay.go |
| @@ -207,6 +207,7 @@ func (a *app) askEvents(w http.ResponseWriter, r *http.Request) { |
| } |
| ctx, cancel := context.WithTimeout(r.Context(), streamLife) |
| defer cancel() |
| + seen := a.askTurns(ctx, sec.Token) |
| res, err := a.ask.client.Events(ctx, sec.Token, after) |
| if errors.Is(err, equipo.ErrUnauthenticated) { |
| a.askLost(r.Context(), me, l.DeviceID) |
| @@ -224,7 +225,24 @@ func (a *app) askEvents(w http.ResponseWriter, r *http.Request) { |
| if down.send("retry: 3000\n\n") != nil { |
| return |
| } |
| - relay(down, res.Body, streamIdle, cancel) |
| + relay(down, res.Body, streamIdle, cancel, seen) |
| +} |
| + |
| +// snapshotForTurns bounds the snapshot a stream reads as it starts. |
| +const snapshotForTurns = 3 * time.Second |
| + |
| +// askTurns is what a stream knows of the turns as it starts, read from |
| +// the conversation's snapshot (seedTurns), so a glance can pass for a turn |
| +// that started before the stream did. Without an answer it knows none, and |
| +// passes no glance until a turn's events start one. |
| +func (a *app) askTurns(ctx context.Context, token string) turnsSeen { |
| + ctx, cancel := context.WithTimeout(ctx, snapshotForTurns) |
| + defer cancel() |
| + s, err := a.ask.client.Snapshot(ctx, token) |
| + if err != nil { |
| + return turnsSeen{} |
| + } |
| + return seedTurns(s) |
| } |
| // downstream is the browser's end of a stream. |
| @@ -258,17 +276,17 @@ func endStream(w http.ResponseWriter, state string) { |
| } |
| // relay reads Equipo's events from upstream and writes the conversation's |
| -// to d as frames of its own (see frameFor); upstream's lines never reach |
| +// to d as frames of its own (see frame; seen is what it knows of the |
| +// turns as it starts); upstream's lines never reach |
| // the browser. It parses lines as a browser does (LF, CRLF or a bare CR |
| // ends one), and ends the stream at an event over the size bounds, at |
| // idle without a byte from upstream (calling stop, which must make the |
| // pending read fail), or when the browser stops reading. |
| -func relay(d downstream, upstream io.Reader, idle time.Duration, stop func()) { |
| +func relay(d downstream, upstream io.Reader, idle time.Duration, stop func(), seen turnsSeen) { |
| quiet := time.AfterFunc(idle, stop) |
| defer quiet.Stop() |
| in := bufio.NewReaderSize(upstream, 32<<10) |
| var ev sseEvent |
| - var seen turnsSeen |
| size, lines := 0, 0 |
| for { |
| line, err := readLine(in, maxEventBytes-size) |
| @@ -433,15 +451,24 @@ type glance struct { |
| // maxGlance bounds a glance, in characters; Equipo keeps to 60. |
| const maxGlance = 60 |
| -// turnsSeen is what one stream has said of its turns, so a glance passes |
| -// only for the person's turn in flight before its first words: cur is the |
| -// newest such turn, past every turn no glance may be for (a team_update |
| -// turn, an ended one, one whose words have started). A stream that began |
| -// after its turn started knows none of this, and passes the glance on: the |
| -// page knows its own turn. |
| +// turnsSeen is what one stream knows of its turns, so a glance passes |
| +// only for the person's turn in flight before its first words: live holds |
| +// those turns, past every turn no glance may be for (a team_update turn, |
| +// an ended one, one whose words have started), which stays there even if |
| +// an older event replayed after the cursor has it running. A stream starts |
| +// knowing the turns in the conversation's snapshot (seedTurns), and passes |
| +// no glance for a turn it does not know. |
| type turnsSeen struct { |
| - cur string |
| - past map[string]bool |
| + live, past map[string]bool |
| +} |
| + |
| +// seedTurns is what the snapshot s says of its turns. |
| +func seedTurns(s equipo.Snapshot) turnsSeen { |
| + var seen turnsSeen |
| + for _, t := range s.Turns { |
| + seen.note(t) |
| + } |
| + return seen |
| } |
| func (s *turnsSeen) note(data any) { |
| @@ -450,7 +477,10 @@ func (s *turnsSeen) note(data any) { |
| if v.Kind == equipo.TurnTeamUpdate || !equipo.InFlight(v.State) { |
| s.over(v.ID) |
| } else if !s.past[v.ID] { |
| - s.cur = v.ID |
| + if s.live == nil { |
| + s.live = map[string]bool{} |
| + } |
| + s.live[v.ID] = true |
| } |
| case delta: |
| s.over(v.TurnID) |
| @@ -462,17 +492,14 @@ func (s *turnsSeen) over(id string) { |
| s.past = map[string]bool{} |
| } |
| s.past[id] = true |
| - if s.cur == id { |
| - s.cur = "" |
| - } |
| + delete(s.live, id) |
| } |
| // glance projects a glance event for turn id (the event's own id, which |
| -// is the turn's), cleaned (cleanGlance), if it may pass. |
| +// is the turn's), cleaned (cleanGlance), if it is for a turn in flight. |
| func (s *turnsSeen) glance(id string, raw json.RawMessage) (any, bool) { |
| var g glance |
| - if json.Unmarshal(raw, &g) != nil || g.TurnID == "" || (id != "" && id != g.TurnID) || |
| - s.past[g.TurnID] || (s.cur != "" && s.cur != g.TurnID) { |
| + if json.Unmarshal(raw, &g) != nil || g.TurnID == "" || (id != "" && id != g.TurnID) || !s.live[g.TurnID] { |
| return nil, false |
| } |
| g.Text = cleanGlance(g.Text) |
diff --git a/internal/web/relay_internal_test.go b/internal/web/relay_internal_test.go| index 81a53f9..48f5077 100644 |
| --- a/internal/web/relay_internal_test.go |
| +++ b/internal/web/relay_internal_test.go |
| @@ -8,6 +8,8 @@ import ( |
| "strings" |
| "testing" |
| "time" |
| + |
| + "amadan.net/carlos/base/internal/equipo" |
| ) |
| const ( |
| @@ -21,11 +23,18 @@ func frame(id, typ, data string) string { |
| // relayTo runs relay over upstream and returns what reached the browser. |
| func relayTo(t *testing.T, upstream io.Reader, idle time.Duration, stop func()) string { |
| + t.Helper() |
| + return relaySeeded(t, turnsSeen{}, upstream, idle, stop) |
| +} |
| + |
| +// relaySeeded is relayTo with what the stream knows of its turns as it |
| +// starts. |
| +func relaySeeded(t *testing.T, seen turnsSeen, upstream io.Reader, idle time.Duration, stop func()) string { |
| t.Helper() |
| rec := httptest.NewRecorder() |
| done := make(chan struct{}) |
| go func() { |
| - relay(downstream{rec, http.NewResponseController(rec)}, upstream, idle, stop) |
| + relay(downstream{rec, http.NewResponseController(rec)}, upstream, idle, stop, seen) |
| close(done) |
| }() |
| select { |
| @@ -156,9 +165,8 @@ func turnFrame(cursor, id, kind, state string) string { |
| // A glance, Equipo's one line on the turn in flight, passes for the |
| // person's turn before its first words, cleaned: one trimmed line of plain |
| // text, at most 60 characters, cut at a word. It is dropped for any other |
| -// turn (a team_update turn, an ended one, one that is not the turn in |
| -// flight), once the turn's words have started, and when nothing is left |
| -// of it. |
| +// turn (a team_update turn, an ended one, one not known to be in flight), |
| +// once the turn's words have started, and when nothing is left of it. |
| func TestRelayGlance(t *testing.T) { |
| long := "Looking through every one of your projects and the recent work in them" |
| delta := frame("60", "delta", `{"cursor":60,"type":"delta","resource":"turn","id":"t_1","revision":0,"turn_id":"t_1","data":{"turn_id":"t_1","offset":0,"text":"Here"}}`) |
| @@ -203,10 +211,41 @@ func TestRelayGlance(t *testing.T) { |
| t.Errorf("relayed %q:\n%s", banned, out) |
| } |
| } |
| - // A stream that has not seen the turn start (it began after) passes the |
| - // glance on: the page knows its turn. |
| - out = relayTo(t, strings.NewReader(glanceEvent("70", "t_7", "Checking your projects")), time.Second, func() {}) |
| - if !strings.HasPrefix(out, "event: glance\n") { |
| - t.Errorf("a glance for a turn the stream has not seen:\n%s", out) |
| + // Once the turn has ended, a glance for a turn the stream has never |
| + // seen is dropped too: nothing it knows of is in flight. |
| + out = relayTo(t, strings.NewReader(turnFrame("50", "t_1", "", "running")+turnFrame("51", "t_1", "", "replied")+ |
| + glanceEvent("52", "t_10", "Unseen turn's glance")), time.Second, func() {}) |
| + if strings.Contains(out, "Unseen") { |
| + t.Errorf("relayed a glance after the turn in flight ended:\n%s", out) |
| + } |
| +} |
| + |
| +// A stream that begins after its turn started knows the turns from the |
| +// conversation's snapshot: a glance passes for the person's turn in flight |
| +// there, and for no other (an unknown turn, a team_update turn, an ended |
| +// one, even when an older event replayed after the cursor has it running). |
| +// Without a snapshot no glance passes for a turn the stream has not seen |
| +// start. |
| +func TestRelayGlanceFromSnapshot(t *testing.T) { |
| + upstream := glanceEvent("70", "t_7", "Checking your projects") + glanceEvent("71", "t_6", "Unknown turn's glance") + |
| + glanceEvent("72", "t_5", "Quiet turn's glance") + turnFrame("73", "t_4", "", "running") + |
| + glanceEvent("74", "t_4", "Ended turn's glance") |
| + out := relayTo(t, strings.NewReader(upstream), time.Second, func() {}) |
| + for _, banned := range []string{"Checking", "Unknown", "Quiet"} { |
| + if strings.Contains(out, banned) { |
| + t.Errorf("without a snapshot, relayed %q:\n%s", banned, out) |
| + } |
| + } |
| + snap := equipo.Snapshot{Turns: []equipo.Turn{ |
| + {ID: "t_4", State: equipo.TurnReplied}, {ID: "t_5", Kind: equipo.TurnTeamUpdate, State: equipo.TurnRunning}, |
| + {ID: "t_7", State: equipo.TurnRunning}}} |
| + out = relaySeeded(t, seedTurns(snap), strings.NewReader(upstream), time.Second, func() {}) |
| + if !strings.HasPrefix(out, "event: glance\ndata: {\"type\":\"glance\",\"data\":{\"turn_id\":\"t_7\",\"text\":\"Checking your projects\"}}\n\n") { |
| + t.Errorf("with the turn in flight in the snapshot, the glance did not pass:\n%s", out) |
| + } |
| + for _, banned := range []string{"Unknown", "Quiet", "Ended"} { |
| + if strings.Contains(out, banned) { |
| + t.Errorf("relayed %q:\n%s", banned, out) |
| + } |
| } |
| } |