Skip to content

Commit adc83ab

Browse files
test(web): cover the sidebar rename focus guard without booting the app
The #27195 regression test mounted AppRoot with a fake environment and took ~10s of the browser project's 15s budget. The thread-open composer focus effect, guard included, now lives in useFocusComposerOnThreadOpen, and a focused browser test drives it with fake frames in under 100ms. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 34a88ed commit adc83ab

4 files changed

Lines changed: 63 additions & 70 deletions

File tree

‎apps/web/src/components/ChatView.browser.test.tsx‎

Lines changed: 0 additions & 58 deletions
This file was deleted.

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits";
44
import { queuedRunEditBlockedReason } from "@t3tools/client-runtime/queued-run-edit";
55
import { ScrollToEndButton, WorkspaceFileDropOverlay } from "./chat/TimelineOverlays";
66
import { usePasteToFocusComposer } from "./chat/usePasteToFocusComposer";
7+
import { useFocusComposerOnThreadOpen } from "./chat/useFocusComposerOnThreadOpen";
78
import { announcedRun, isDetailBehindShell } from "./chat/threadActivityAnnouncement";
89
import {
910
deriveAvailableRevertTurnCounts,
@@ -2093,18 +2094,11 @@ export default function ChatView(props: ChatViewProps) {
20932094
setTimelineAnchor,
20942095
});
20952096

2096-
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- schedules a frame to focus the composer DOM element when a thread opens with the terminal closed
2097-
useEffect(() => {
2098-
if (!activeThread?.id || terminalUiState.terminalOpen) return;
2099-
const frame = window.requestAnimationFrame(() => {
2100-
// Double-clicking a sidebar row opens its thread and its rename field in one gesture.
2101-
if (document.activeElement?.matches("[data-sidebar-row] input")) return;
2102-
focusComposerStable();
2103-
});
2104-
return () => {
2105-
window.cancelAnimationFrame(frame);
2106-
};
2107-
}, [activeThread?.id, focusComposerStable, terminalUiState.terminalOpen]);
2097+
useFocusComposerOnThreadOpen(
2098+
activeThread?.id,
2099+
!terminalUiState.terminalOpen,
2100+
focusComposerStable,
2101+
);
21082102

21092103
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- window focus listener that refocuses the composer after the window regains focus
21102104
useEffect(() => {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { useRef } from "react";
2+
import { expect, it, vi } from "vite-plus/test";
3+
4+
import { act, fakeTimers, render } from "../../../test/render";
5+
import { useFocusComposerOnThreadOpen } from "./useFocusComposerOnThreadOpen";
6+
7+
function ThreadProbe(props: { threadId: string }) {
8+
const composerRef = useRef<HTMLTextAreaElement>(null);
9+
useFocusComposerOnThreadOpen(props.threadId, true, () => composerRef.current?.focus());
10+
return (
11+
<>
12+
<div data-sidebar-row="thread:beta">
13+
<input aria-label="Thread title" />
14+
</div>
15+
<textarea aria-label="Message" ref={composerRef} />
16+
</>
17+
);
18+
}
19+
20+
// regression: #27195 double-clicking another thread's row lost its rename field to the composer
21+
it("keeps the sidebar rename input when a double-click opens another thread", async () => {
22+
// The first render schedules the frame.
23+
using _frames = fakeTimers();
24+
await using screen = await render(<ThreadProbe threadId="alpha" />, {
25+
// A bare hook probe; nothing here navigates.
26+
router: false,
27+
});
28+
await act(() => vi.advanceTimersToNextFrame());
29+
await expect.element(screen.getByRole("textbox", { name: "Message" })).toHaveFocus();
30+
31+
const renameInput = screen.getByRole("textbox", { name: "Thread title" });
32+
renameInput.element().focus();
33+
await screen.rerender(<ThreadProbe threadId="beta" />);
34+
await act(() => vi.advanceTimersToNextFrame());
35+
await expect.element(renameInput).toHaveFocus();
36+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useEffect } from "react";
2+
3+
/** Lands the caret in the composer a frame after a thread opens. */
4+
export function useFocusComposerOnThreadOpen(
5+
threadId: string | undefined,
6+
enabled: boolean,
7+
focusComposer: () => void,
8+
) {
9+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- schedules a frame to focus the composer DOM element when a thread opens with the terminal closed
10+
useEffect(() => {
11+
if (!threadId || !enabled) return;
12+
const frame = window.requestAnimationFrame(() => {
13+
// Double-clicking a sidebar row opens its thread and its rename field in one gesture.
14+
if (document.activeElement?.matches("[data-sidebar-row] input")) return;
15+
focusComposer();
16+
});
17+
return () => {
18+
window.cancelAnimationFrame(frame);
19+
};
20+
}, [threadId, enabled, focusComposer]);
21+
}

0 commit comments

Comments
 (0)