11import { createMemoryHistory } from "@tanstack/react-router" ;
2- import { scopeProjectRef } from "@t3tools/client-runtime/environment" ;
2+ import { scopeProjectRef , scopeThreadRef } from "@t3tools/client-runtime/environment" ;
33import { ThreadId } from "@t3tools/contracts" ;
44import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings" ;
55import type * as Context from "effect/Context" ;
66import { expect , it , vi } from "vite-plus/test" ;
7- import { page } from "vite-plus/test/browser" ;
7+ import { page , userEvent } from "vite-plus/test/browser" ;
8+
9+ import "~/index.css" ;
810
911import { AppRoot } from "~/AppRoot" ;
1012// The route loads ChatView lazily, and its first transform (React Compiler over ~4k lines)
1113// takes 6-11s; importing it here keeps that out of the test's timeout.
1214import "~/components/ChatView" ;
1315import { DraftId , useComposerDraftStore } from "~/composerDraftStore" ;
1416import { __setClientSettingsForTests } from "~/hooks/useSettings" ;
17+ import { startWorkspaceFocus } from "~/lib/workspaceFocus" ;
18+ import { useRightPanelStore } from "~/rightPanelStore" ;
1519import { getRouter } from "~/router" ;
1620import {
1721 ENVIRONMENT_ID ,
1822 PROJECT_ID ,
1923 installFakeEnvironment ,
24+ threadWithHistory ,
2025 type FakeDriver ,
2126} from "../../test/fakeEnvironment" ;
2227import { render } from "../../test/render" ;
@@ -36,8 +41,13 @@ vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
3641 trackRpcRequestSent : ( ) => { } ,
3742} ) ) ;
3843
44+ const parent = threadWithHistory ( "focus-parent" , 1 ) ;
45+ const side = { ...threadWithHistory ( "focus-side" , 1 ) , sideChatOf : parent . id } ;
46+ const otherSide = { ...threadWithHistory ( "focus-other-side" , 1 ) , sideChatOf : parent . id } ;
47+ const THREADS = [ parent , side , otherSide ] ;
48+
3949it ( "a new draft does not load its reserved thread before the server lists it" , async ( ) => {
40- using _environment = installFakeEnvironment ( [ ] ) ;
50+ using _environment = installFakeEnvironment ( THREADS ) ;
4151 using fetchSpy = vi . spyOn ( globalThis , "fetch" ) ;
4252 __setClientSettingsForTests ( {
4353 ...DEFAULT_CLIENT_SETTINGS ,
@@ -65,3 +75,51 @@ it("a new draft does not load its reserved thread before the server lists it", a
6575 // A 404 here marks the thread deleted, and the view never loads it once the send creates it.
6676 expect ( threadRequests ) . toEqual ( [ ] ) ;
6777} ) ;
78+
79+ it ( "accepts typing after clicking a side-chat tab without tabbing into its composer" , async ( ) => {
80+ await page . viewport ( 1400 , 900 ) ;
81+ using _environment = installFakeEnvironment ( THREADS ) ;
82+ __setClientSettingsForTests ( {
83+ ...DEFAULT_CLIENT_SETTINGS ,
84+ onboardingCompletedAt : "2026-01-01T00:00:00.000Z" ,
85+ } ) ;
86+ const parentRef = scopeThreadRef ( ENVIRONMENT_ID , parent . id ) ;
87+ useRightPanelStore . getState ( ) . openSideChat ( parentRef , otherSide . id ) ;
88+ useRightPanelStore . getState ( ) . openSideChat ( parentRef , side . id ) ;
89+ const router = getRouter (
90+ createMemoryHistory ( { initialEntries : [ `/${ ENVIRONMENT_ID } /${ parent . id } ` ] } ) ,
91+ ) ;
92+ await router . load ( ) ;
93+ const container = document . createElement ( "div" ) ;
94+ container . style . cssText = "height: 900px; width: 1400px;" ;
95+ await using screen = await render ( < AppRoot router = { router } /> , {
96+ container,
97+ // AppRoot brings its own router.
98+ router : false ,
99+ } ) ;
100+ using _focus = { [ Symbol . dispose ] : startWorkspaceFocus ( router ) } ;
101+ const mainEditor = screen . getByRole ( "textbox" , { name : `Message ${ parent . title } ` , exact : true } ) ;
102+ const sideEditor = screen . getByRole ( "textbox" , { name : `Message ${ side . title } ` , exact : true } ) ;
103+ await expect . element ( sideEditor ) . toBeVisible ( ) ;
104+ await userEvent . click ( mainEditor ) ;
105+ await userEvent . keyboard ( "main draft" ) ;
106+ await userEvent . click ( screen . getByRole ( "button" , { name : side . title , exact : true } ) ) ;
107+ await expect . element ( sideEditor ) . toHaveFocus ( ) ;
108+ await userEvent . keyboard ( "side draft" ) ;
109+ await expect . element ( sideEditor ) . toHaveTextContent ( "side draft" ) ;
110+ await expect . element ( mainEditor ) . toHaveTextContent ( "main draft" ) ;
111+
112+ await userEvent . click ( screen . getByRole ( "button" , { name : otherSide . title , exact : true } ) ) ;
113+ const otherEditor = screen . getByRole ( "textbox" , {
114+ name : `Message ${ otherSide . title } ` ,
115+ exact : true ,
116+ } ) ;
117+ await expect . element ( otherEditor ) . toHaveFocus ( ) ;
118+ await userEvent . keyboard ( "other draft" ) ;
119+ await expect . element ( otherEditor ) . toHaveTextContent ( "other draft" ) ;
120+ await userEvent . click ( screen . getByRole ( "button" , { name : side . title , exact : true } ) ) ;
121+ await expect . element ( sideEditor ) . toHaveFocus ( ) ;
122+ await userEvent . keyboard ( " again" ) ;
123+ await expect . element ( sideEditor ) . toHaveTextContent ( "side draft again" ) ;
124+ await expect . element ( mainEditor ) . toHaveTextContent ( "main draft" ) ;
125+ } ) ;
0 commit comments