Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,73 @@ 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();
});
},
};
}

// 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 = 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 ?? "";
};
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);
});

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);
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
9 changes: 8 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,13 @@ export class StreamReveal {
this.shown = this.received;
}

/// Reduce Motion changed while the row is mounted: on, everything received shows now and later
/// text shows as it arrives; off, later text is paced again.
setReduceMotion(on: boolean): void {
this.reduceMotion = on;
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
if (on) 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