@@ -138,3 +138,144 @@ describe("desktop app control server", () => {
138138 } ) ,
139139 ) ;
140140} ) ;
141+
142+ describe ( "desktop app control server connection requests" , ( ) => {
143+ it . effect ( "routes connection requests separately from activation" , ( ) =>
144+ Effect . gen ( function * ( ) {
145+ const platform = yield * HostProcessPlatform ;
146+ const userId = yield * HostProcessUserId ;
147+ yield * Effect . promise ( async ( ) => {
148+ const root = await NodeFSP . mkdtemp ( NodePath . join ( NodeOS . tmpdir ( ) , "t3-app-conn-test-" ) ) ;
149+ const target = makeTarget ( NodePath . join ( root , "userdata" ) , platform , userId ) ;
150+ const activations : string [ ] = [ ] ;
151+ const connections : string [ ] = [ ] ;
152+ const server = await startDesktopAppControlServer ( {
153+ ...target ,
154+ userId,
155+ handle : async ( input ) => {
156+ activations . push ( input . requestId ) ;
157+ return {
158+ version : 1 ,
159+ requestId : input . requestId ,
160+ ok : true ,
161+ projectId : ProjectId . make ( "project-1" ) ,
162+ threadId : ThreadId . make ( "thread-1" ) ,
163+ } ;
164+ } ,
165+ cancel : ( ) => undefined ,
166+ handleConnection : async ( input ) => {
167+ connections . push ( input . requestId ) ;
168+ return {
169+ version : 1 ,
170+ requestId : input . requestId ,
171+ ok : true ,
172+ result : { environments : [ ] } ,
173+ } ;
174+ } ,
175+ cancelConnection : ( ) => undefined ,
176+ } ) ;
177+ openServers . push ( server ) ;
178+
179+ const listed = await exchange ( target . address , {
180+ version : 1 ,
181+ requestId : "list-1" ,
182+ type : "connection" ,
183+ operation : "listEnvironments" ,
184+ } as unknown as DesktopAppActivationRequest ) ;
185+ expect ( listed ) . toMatchObject ( {
186+ ok : true ,
187+ requestId : "list-1" ,
188+ result : { environments : [ ] } ,
189+ } ) ;
190+
191+ const invalid = await exchange ( target . address , {
192+ version : 1 ,
193+ requestId : "bad-1" ,
194+ type : "connection" ,
195+ operation : "dispatch" ,
196+ environmentId : "env" ,
197+ command : { type : "thread.checkpoint.revert" } ,
198+ } as unknown as DesktopAppActivationRequest ) ;
199+ expect ( invalid ) . toMatchObject ( { ok : false , requestId : "bad-1" , code : "invalid-request" } ) ;
200+
201+ const activated = await exchange ( target . address , request ( "open-1" , platform ) ) ;
202+ expect ( activated ) . toMatchObject ( { ok : true , requestId : "open-1" } ) ;
203+ expect ( activations ) . toEqual ( [ "open-1" ] ) ;
204+ expect ( connections ) . toEqual ( [ "list-1" ] ) ;
205+
206+ await server . close ( ) ;
207+ openServers . splice ( openServers . indexOf ( server ) , 1 ) ;
208+ await NodeFSP . rm ( root , { recursive : true , force : true } ) ;
209+ } ) ;
210+ } ) ,
211+ ) ;
212+ } ) ;
213+
214+ describe ( "desktop app control server cancellation ownership" , ( ) => {
215+ it . effect ( "a rejected duplicate id disconnecting never cancels the first request" , ( ) =>
216+ Effect . gen ( function * ( ) {
217+ const platform = yield * HostProcessPlatform ;
218+ const userId = yield * HostProcessUserId ;
219+ yield * Effect . promise ( async ( ) => {
220+ const root = await NodeFSP . mkdtemp ( NodePath . join ( NodeOS . tmpdir ( ) , "t3-app-dup-test-" ) ) ;
221+ const target = makeTarget ( NodePath . join ( root , "userdata" ) , platform , userId ) ;
222+ const cancelled : unknown [ ] = [ ] ;
223+ let releaseFirst : ( ) => void = ( ) => undefined ;
224+ const firstStarted = new Promise < void > ( ( resolve ) => {
225+ releaseFirst = resolve ;
226+ } ) ;
227+ let seen = 0 ;
228+ const server = await startDesktopAppControlServer ( {
229+ ...target ,
230+ userId,
231+ handle : ( ) => new Promise ( ( ) => undefined ) ,
232+ cancel : ( ) => undefined ,
233+ handleConnection : ( input ) => {
234+ seen += 1 ;
235+ if ( seen === 1 ) {
236+ releaseFirst ( ) ;
237+ return new Promise ( ( ) => undefined ) ;
238+ }
239+ return Promise . resolve ( {
240+ version : 1 ,
241+ requestId : input . requestId ,
242+ ok : false ,
243+ code : "invalid-request" ,
244+ message : "The request id is already in use." ,
245+ } ) ;
246+ } ,
247+ cancelConnection : ( request ) => cancelled . push ( request ) ,
248+ } ) ;
249+ openServers . push ( server ) ;
250+ const payload = {
251+ version : 1 ,
252+ requestId : "same" ,
253+ type : "connection" ,
254+ operation : "listEnvironments" ,
255+ } as unknown as DesktopAppActivationRequest ;
256+
257+ const first = NodeNet . createConnection ( target . address ) ;
258+ await new Promise < void > ( ( resolve , reject ) => {
259+ first . once ( "error" , reject ) ;
260+ first . once ( "connect" , ( ) => first . write ( `${ JSON . stringify ( payload ) } \n` , ( ) => resolve ( ) ) ) ;
261+ } ) ;
262+ await firstStarted ;
263+
264+ const duplicate = await exchange ( target . address , payload ) ;
265+ expect ( duplicate ) . toMatchObject ( { ok : false , code : "invalid-request" } ) ;
266+ await new Promise ( ( resolve ) => setImmediate ( resolve ) ) ;
267+ expect ( cancelled ) . toEqual ( [ ] ) ;
268+
269+ first . destroy ( ) ;
270+ await new Promise ( ( resolve ) => setImmediate ( resolve ) ) ;
271+ await new Promise ( ( resolve ) => setImmediate ( resolve ) ) ;
272+ expect ( cancelled ) . toHaveLength ( 1 ) ;
273+ expect ( cancelled [ 0 ] ) . toMatchObject ( { requestId : "same" , operation : "listEnvironments" } ) ;
274+
275+ await server . close ( ) ;
276+ openServers . splice ( openServers . indexOf ( server ) , 1 ) ;
277+ await NodeFSP . rm ( root , { recursive : true , force : true } ) ;
278+ } ) ;
279+ } ) ,
280+ ) ;
281+ } ) ;
0 commit comments