@@ -6,8 +6,10 @@ import {
66 type ClientSettings ,
77 type DesktopPreviewBridge ,
88 type PreviewEvent ,
9+ type PreviewListResult ,
910} from "@t3tools/contracts" ;
1011import { AsyncResult , Atom } from "effect/reactivity" ;
12+ import { scopedThreadKey } from "@t3tools/client-runtime/environment" ;
1113import { act } from "react" ;
1214import { create , type ReactTestRenderer } from "react-test-renderer" ;
1315import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
@@ -46,7 +48,8 @@ vi.mock("~/state/primaryEnvironment", async () => {
4648vi . mock ( "~/state/preview" , ( ) => ( {
4749 previewEnvironment : {
4850 events : ( { environmentId } : { environmentId : string } ) => previewEventsFor ( environmentId ) ,
49- list : ( ) => previewList ,
51+ list : ( { input } : { input : { threadId ?: string } } ) =>
52+ input . threadId === undefined ? previewList : Atom . make ( AsyncResult . initial ( ) ) ,
5053 } ,
5154} ) ) ;
5255
@@ -68,14 +71,15 @@ import * as desktopTabLifetime from "./desktopTabLifetime";
6871import { HostedBrowserWebview } from "./HostedBrowserWebview" ;
6972import { ElectronBrowserHost } from "./ElectronBrowserHost" ;
7073import { previewRuntimeTabId } from "./previewRuntimeTabId" ;
71- import { resetPreviewStateForTests } from "~/previewStateStore" ;
74+ import { previewStateAtom , resetPreviewStateForTests } from "~/previewStateStore" ;
75+ import { usePreviewSession } from "~/components/preview/usePreviewSession" ;
7276import { AppAtomRegistryProvider , appAtomRegistry } from "~/rpc/atomRegistry" ;
7377
7478const previewEvents = new Map <
7579 string ,
7680 Atom . Writable < AsyncResult . AsyncResult < PreviewEvent > , AsyncResult . AsyncResult < PreviewEvent > >
7781> ( ) ;
78- const previewList = Atom . make ( AsyncResult . initial ( ) ) ;
82+ const previewList = Atom . make < AsyncResult . AsyncResult < PreviewListResult > > ( AsyncResult . initial ( ) ) ;
7983function previewEventsFor ( environmentId : string ) {
8084 let atom = previewEvents . get ( environmentId ) ;
8185 if ( ! atom ) {
@@ -99,6 +103,7 @@ function deferred<A>() {
99103
100104beforeEach ( ( ) => {
101105 resetPreviewStateForTests ( ) ;
106+ appAtomRegistry . set ( previewList , AsyncResult . initial ( ) ) ;
102107 for ( const atom of previewEvents . values ( ) ) appAtomRegistry . set ( atom , AsyncResult . initial ( ) ) ;
103108 __resetClientSettingsPersistenceForTests ( ) ;
104109 useBrowserSurfaceStore . setState ( { activityByTabId : { } , byTabId : { } } ) ;
@@ -132,6 +137,132 @@ beforeEach(() => {
132137} ) ;
133138
134139describe ( "Electron browser hosting outside the selected thread" , ( ) => {
140+ it ( "applies primary events once while a chat sync is mounted and still syncs remote threads" , async ( ) => {
141+ const local = {
142+ environmentId : EnvironmentId . make ( "desktop-primary" ) ,
143+ threadId : ThreadId . make ( "selected-thread" ) ,
144+ } ;
145+ const remote = { ...local , environmentId : EnvironmentId . make ( "remote-server" ) } ;
146+ function ChatSync ( ) {
147+ usePreviewSession ( local ) ;
148+ usePreviewSession ( remote ) ;
149+ return null ;
150+ }
151+ mocks . getClientSettings . mockResolvedValue ( DEFAULT_CLIENT_SETTINGS ) ;
152+ await act ( async ( ) => {
153+ await ensureClientSettingsHydrated ( ) ;
154+ renderer = create (
155+ < AppAtomRegistryProvider >
156+ < ElectronBrowserHost />
157+ < ChatSync />
158+ </ AppAtomRegistryProvider > ,
159+ {
160+ createNodeMock : ( element ) =>
161+ element . type === "webview"
162+ ? Object . assign ( new EventTarget ( ) , { getWebContentsId : ( ) => 44 } )
163+ : { scrollLeft : 0 , scrollTop : 0 , scrollTo : ( ) => undefined } ,
164+ } ,
165+ ) ;
166+ } ) ;
167+ const applied = vi . fn ( ) ;
168+ const stop = appAtomRegistry . subscribe ( previewStateAtom ( scopedThreadKey ( local ) ) , ( state ) => {
169+ if ( state . sessions [ "selected-tab" ] ) applied ( ) ;
170+ } ) ;
171+ const opened : PreviewEvent = {
172+ type : "opened" ,
173+ threadId : local . threadId ,
174+ tabId : "selected-tab" ,
175+ serverEpoch : "server" ,
176+ revision : 1 ,
177+ createdAt : "2026-10-07T00:00:00.000Z" ,
178+ snapshot : {
179+ threadId : local . threadId ,
180+ tabId : "selected-tab" ,
181+ runtime : "server" ,
182+ navStatus : { _tag : "Idle" } ,
183+ canGoBack : false ,
184+ canGoForward : false ,
185+ updatedAt : "2026-10-07T00:00:00.000Z" ,
186+ } ,
187+ } ;
188+ try {
189+ await act ( ( ) => {
190+ appAtomRegistry . set ( previewEventsFor ( local . environmentId ) , AsyncResult . success ( opened ) ) ;
191+ appAtomRegistry . set ( previewEventsFor ( remote . environmentId ) , AsyncResult . success ( opened ) ) ;
192+ } ) ;
193+ expect ( applied ) . toHaveBeenCalledOnce ( ) ;
194+ expect ( appAtomRegistry . get ( previewStateAtom ( scopedThreadKey ( remote ) ) ) . sessions ) . toEqual ( {
195+ "selected-tab" : opened . snapshot ,
196+ } ) ;
197+ expect ( mocks . createTab ) . toHaveBeenCalledOnce ( ) ;
198+ } finally {
199+ stop ( ) ;
200+ }
201+ } ) ;
202+
203+ it ( "attaches existing tabs from the initial list and reconciles a reconnect without events" , async ( ) => {
204+ mocks . getClientSettings . mockResolvedValue ( DEFAULT_CLIENT_SETTINGS ) ;
205+ await act ( async ( ) => {
206+ await ensureClientSettingsHydrated ( ) ;
207+ renderer = create (
208+ < AppAtomRegistryProvider >
209+ < ElectronBrowserHost />
210+ </ AppAtomRegistryProvider > ,
211+ {
212+ createNodeMock : ( element ) =>
213+ element . type === "webview"
214+ ? Object . assign ( new EventTarget ( ) , { getWebContentsId : ( ) => 43 } )
215+ : { scrollLeft : 0 , scrollTop : 0 , scrollTo : ( ) => undefined } ,
216+ } ,
217+ ) ;
218+ } ) ;
219+ const threadRef = {
220+ environmentId : EnvironmentId . make ( "desktop-primary" ) ,
221+ threadId : ThreadId . make ( "already-open" ) ,
222+ } ;
223+ const initialList : PreviewListResult = {
224+ serverEpoch : "existing-server" ,
225+ revision : 1 ,
226+ sessions : [
227+ {
228+ threadId : threadRef . threadId ,
229+ tabId : "existing-tab" ,
230+ runtime : "server" ,
231+ navStatus : { _tag : "Idle" } ,
232+ canGoBack : false ,
233+ canGoForward : false ,
234+ updatedAt : "2026-10-06T00:00:00.000Z" ,
235+ } ,
236+ ] ,
237+ } ;
238+ await act ( ( ) => {
239+ appAtomRegistry . set ( previewList , AsyncResult . success ( initialList ) ) ;
240+ } ) ;
241+ const runtimeTabId = previewRuntimeTabId ( threadRef , initialList . serverEpoch , "existing-tab" ) ;
242+ expect ( mocks . createTab ) . toHaveBeenCalledExactlyOnceWith (
243+ runtimeTabId ,
244+ expect . objectContaining ( {
245+ serverTab : { threadId : threadRef . threadId , tabId : "existing-tab" } ,
246+ } ) ,
247+ ) ;
248+ expect ( mocks . registerWebview ) . toHaveBeenCalledExactlyOnceWith ( runtimeTabId , 43 ) ;
249+
250+ vi . useFakeTimers ( ) ;
251+ await act ( ( ) => {
252+ appAtomRegistry . set ( previewList , AsyncResult . success ( initialList , { waiting : true } ) ) ;
253+ } ) ;
254+ await vi . advanceTimersByTimeAsync ( 0 ) ;
255+ expect ( mocks . closeTab ) . not . toHaveBeenCalled ( ) ;
256+ await act ( ( ) => {
257+ appAtomRegistry . set (
258+ previewList ,
259+ AsyncResult . success ( { ...initialList , revision : 2 , sessions : [ ] } ) ,
260+ ) ;
261+ } ) ;
262+ await vi . advanceTimersByTimeAsync ( 0 ) ;
263+ expect ( mocks . closeTab ) . toHaveBeenCalledExactlyOnceWith ( runtimeTabId ) ;
264+ } ) ;
265+
135266 it ( "attaches a primary-server tab without a chat view and releases it when closed" , async ( ) => {
136267 mocks . getClientSettings . mockResolvedValue ( DEFAULT_CLIENT_SETTINGS ) ;
137268 await act ( async ( ) => {
0 commit comments