Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,102 @@ test("the last word shows once text stops arriving, while the reply still stream
expect(seen.at(-1)).toBe("Let me check the tests.");
});

/// A stand-in for `prefers-reduced-motion: reduce` that a test turns on and off, as the system
/// setting does while the app runs.
function reducedMotionQuery(initial: boolean) {
const listeners = new Set<() => void>();
const query = {
matches: initial,
media: "(prefers-reduced-motion: reduce)",
addEventListener: (_type: string, listener: () => void) => listeners.add(listener),
removeEventListener: (_type: string, listener: () => void) => listeners.delete(listener),
};
Object.assign(dom!.window, { matchMedia: () => query });
return {
listeners,
set(on: boolean) {
query.matches = on;
act(() => {
for (const listener of listeners) listener();
});
},
};
}

/// Display frames whose cancel takes the frame back, as `cancelAnimationFrame` does. `frame(now)`
/// runs the frames still pending and returns the reply's text.
function cancellableFrames() {
const pending = new Map<number, (now: number) => void>();
let handles = 0;
revealFrames.request = (callback) => {
handles += 1;
pending.set(handles, callback);
return handles;
};
revealFrames.cancel = (handle) => void pending.delete(handle);
const frame = (now: number) => {
const due = [...pending.values()];
pending.clear();
act(() => {
for (const callback of due) callback(now);
});
return container.querySelector(".cv-md")?.textContent ?? "";
};
return { pending, frame };
}

// Reduce Motion turned on while a reply streams: the stylesheet follows at once, and so does the
// reveal. The row shows all of the reply in that frame, its frame loop stops and no step is left
// fading; text that arrives later shows whole, as in a row that mounted with Reduce Motion on.
test("turning Reduce Motion on mid-reply shows the whole reply and stops the reveal", () => {
const motion = reducedMotionQuery(false);
const { pending, frame } = cancellableFrames();
const reply = LONG_REPLY.trim();
root = createRoot(container);
render("", true);
render(reply, true);
const paced = [frame(0), frame(16), frame(32)];
expect(paced.at(-1)!.length).toBeGreaterThan(0);
expect(paced.at(-1)!.length).toBeLessThan(reply.length);

motion.set(true);
expect(container.querySelector(".cv-md")?.textContent).toBe(reply);
expect(container.querySelectorAll(".cv-fresh").length).toBe(0);
expect(pending.size).toBe(0);

const more = `${reply} The release went out.`;
render(more, true);
expect(frame(48)).toBe(more);
expect(container.querySelectorAll(".cv-fresh").length).toBe(0);
expect(pending.size).toBe(0);

act(() => root!.unmount());
root = undefined;
expect(motion.listeners.size).toBe(0);
});

// Text can render and have its pending frame cancelled before the reveal reads it. Turning Reduce
// Motion on there shows that text. Turning it off again must pace only what arrives after it,
// never re-reveal from the reveal's older length: what shows never shrinks.
test("Reduce Motion on then off before a frame keeps what shows when later text arrives", () => {
dom!.window.document.documentElement.dataset.streamWords = "off";
const motion = reducedMotionQuery(false);
const { frame } = cancellableFrames();
root = createRoot(container);
render("", true);
render("The checks on the merged head", true);
frame(0);
frame(16);
// Rendered, with its frame still pending when Reduce Motion turns on.
const shown = `${LONG_REPLY.trim()} and the notes are ready.`;
render(shown, true);
motion.set(true);
expect(container.querySelector(".cv-md")?.textContent).toBe(shown);
motion.set(false);
render(`${shown} The release went out.`, true);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This regression test does not verify that turning Reduce Motion off resumes pacing or that the later delta eventually appears. Assert the first post-toggle frame is shorter than the new text, then assert a later frame reaches the complete reply.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At webviews/src/agent-session/acpmux/conversation/RevealedMarkdown.test.tsx, line 321:

<comment>This regression test does not verify that turning Reduce Motion off resumes pacing or that the later delta eventually appears. Assert the first post-toggle frame is shorter than the new text, then assert a later frame reaches the complete reply.</comment>

<file context>
@@ -226,6 +226,102 @@ test("the last word shows once text stops arriving, while the reply still stream
+  motion.set(true);
+  expect(container.querySelector(".cv-md")?.textContent).toBe(shown);
+  motion.set(false);
+  render(`${shown} The release went out.`, true);
+  for (let index = 0; index < 20; index++) expect(frame(32 + index * 16).length).toBeGreaterThanOrEqual(shown.length);
+});
</file context>

for (let index = 0; index < 20; index++) expect(frame(32 + index * 16).length).toBeGreaterThanOrEqual(shown.length);
});

test("wordEdge ends at a whole word, keeps what shows, and lets a long word through", () => {
expect(wordEdge("alpha beta gam", 14, false, 0)).toBe(10);
expect(wordEdge("alpha beta gam", 8, false, 0)).toBe(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,31 @@ export function useStreamReveal(text: string, streaming: boolean): { visible: st
state.handle = 0;
};
}, []);
// Reduce Motion can change while the row is mounted, and the stylesheet follows it at once.
// Turned on, the whole reply shows now: the pending frame is cancelled and no step is left
// fading; later text shows as it arrives. Turned off, later text is paced again.
useLayoutEffect(() => {
const query = typeof window === "undefined" ? undefined : window.matchMedia?.("(prefers-reduced-motion: reduce)");
if (!query) return;
const changed = () => {
const on = query.matches;
if (reduce.current === on) return;
reduce.current = on;
reveal.current!.setReduceMotion(on, latest.current.text);
if (!on) return;
const state = loop.current;
revealFrames.cancel(state.handle);
state.handle = 0;
const { text: target, shown: before } = latest.current;
if (before.length === target.length && !before.fresh.length) return;
const next = { length: target.length, fresh: [], now: before.now };
latest.current.shown = next;
setShown(next);
};
changed();
query.addEventListener("change", changed);
return () => query.removeEventListener("change", changed);
}, []);
return {
visible: text.slice(0, Math.min(shown.length, text.length)),
fresh: shown.fresh,
Expand Down
12 changes: 11 additions & 1 deletion webviews/src/agent-session/acpmux/streamReveal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class StreamReveal {
private gaps: number[] = [];
private smoothedRate = 0;
private drainUntil: number | undefined;
private readonly reduceMotion: boolean;
private reduceMotion: boolean;

constructor(options: StreamRevealOptions = {}) {
this.shown = options.initial?.length ?? 0;
Expand All @@ -64,6 +64,16 @@ export class StreamReveal {
this.shown = this.received;
}

/// Reduce Motion changed while the row is mounted: on, all of `text` (the latest, which may not
/// have reached `advance` yet) shows now and later text shows as it arrives; off, only text after
/// it is paced.
setReduceMotion(on: boolean, text: string): void {
this.reduceMotion = on;
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
if (!on) return;
this.received = Math.max(this.received, text.length);
this.flush();
}

/// The visible length of `text` for the frame at `now` (ms); `done` when the stream ended.
advance(text: string, now: number, done: boolean): number {
const elapsed =
Expand Down