@@ -345,12 +345,10 @@ struct ClientTests {
345345
346346 let request1 = Ping . request ( )
347347 let request2 = Ping . request ( )
348- nonisolated ( unsafe) var resultTask1 : Task < Ping . Result , Swift . Error > ?
349- nonisolated ( unsafe) var resultTask2 : Task < Ping . Result , Swift . Error > ?
350-
351- try await client. withBatch { batch in
352- resultTask1 = try await batch. addRequest ( request1)
353- resultTask2 = try await batch. addRequest ( request2)
348+ let ( resultTask1, resultTask2) = try await client. withBatch { batch in
349+ let task1 = try await batch. addRequest ( request1)
350+ let task2 = try await batch. addRequest ( request2)
351+ return ( task1, task2)
354352 }
355353
356354 // Check if batch message was sent (after initialize and initialized notification)
@@ -381,13 +379,8 @@ struct ClientTests {
381379 try await transport. queue ( batch: [ anyResponse1, anyResponse2] )
382380
383381 // Wait for results and verify
384- guard let task1 = resultTask1, let task2 = resultTask2 else {
385- #expect( Bool ( false ) , " Result tasks not created " )
386- return
387- }
388-
389- _ = try await task1. value // Should succeed
390- _ = try await task2. value // Should succeed
382+ _ = try await resultTask1. value // Should succeed
383+ _ = try await resultTask2. value // Should succeed
391384
392385 #expect( Bool ( true ) ) // Reaching here means success
393386
@@ -426,11 +419,11 @@ struct ClientTests {
426419 let request1 = Ping . request ( ) // Success
427420 let request2 = Ping . request ( ) // Error
428421
429- nonisolated ( unsafe ) var resultTasks : [ Task < Ping . Result , Swift . Error > ] = [ ]
430-
431- try await client . withBatch { batch in
432- resultTasks . append ( try await batch. addRequest ( request1 ) )
433- resultTasks . append ( try await batch . addRequest ( request2 ) )
422+ let resultTasks = try await client . withBatch { batch in
423+ [
424+ try await batch . addRequest ( request1 ) ,
425+ try await batch. addRequest ( request2 ) ,
426+ ]
434427 }
435428
436429 // Check if batch message was sent (after initialize and initialized notification)
@@ -514,6 +507,49 @@ struct ClientTests {
514507 await client. disconnect ( )
515508 }
516509
510+ @Test ( " Batch request - empty with non-Void return " )
511+ func testBatchRequestEmptyNonVoid( ) async throws {
512+ let transport = MockTransport ( )
513+ let client = Client ( name: " TestClient " , version: " 1.0 " )
514+
515+ // Set up a task to handle the initialize response
516+ let initTask = Task {
517+ try await Task . sleep ( for: . milliseconds( 10 ) )
518+ if let lastMessage = await transport. sentMessages. last,
519+ let data = lastMessage. data ( using: . utf8) ,
520+ let request = try ? JSONDecoder ( ) . decode ( Request< Initialize> . self , from: data)
521+ {
522+ let response = Initialize . response (
523+ id: request. id,
524+ result: . init(
525+ protocolVersion: Version . latest,
526+ capabilities: . init( ) ,
527+ serverInfo: . init( name: " TestServer " , version: " 1.0 " ) ,
528+ instructions: nil
529+ )
530+ )
531+ try await transport. queue ( response: response)
532+ }
533+ }
534+
535+ try await client. connect ( transport: transport)
536+ try await Task . sleep ( for: . milliseconds( 10 ) )
537+ initTask. cancel ( )
538+
539+ // Call withBatch with non-Void return but don't add any requests
540+ let result : Int = try await client. withBatch { _ in
541+ 42
542+ }
543+
544+ // Verify the closure's return value is passed through
545+ #expect( result == 42 )
546+
547+ // Check that only initialize message and initialized notification were sent
548+ #expect( await transport. sentMessages. count == 2 )
549+
550+ await client. disconnect ( )
551+ }
552+
517553 @Test ( " Notify method sends notifications " )
518554 func testClientNotify( ) async throws {
519555 let transport = MockTransport ( )
0 commit comments