@@ -363,4 +363,120 @@ struct ClientTests {
363363
364364 await client. disconnect ( )
365365 }
366+
367+ @Test ( " Notify method sends notifications " )
368+ func testClientNotify( ) async throws {
369+ let transport = MockTransport ( )
370+ let client = Client ( name: " TestClient " , version: " 1.0 " )
371+
372+ try await client. connect ( transport: transport)
373+ try await Task . sleep ( for: . milliseconds( 10 ) )
374+
375+ // Create a test notification
376+ let notification = InitializedNotification . message ( )
377+ try await client. notify ( notification)
378+
379+ // Verify notification was sent
380+ #expect( await transport. sentMessages. count == 1 )
381+
382+ if let sentMessage = await transport. sentMessages. first,
383+ let data = sentMessage. data ( using: . utf8)
384+ {
385+
386+ // Decode as Message<InitializedNotification>
387+ let decoder = JSONDecoder ( )
388+ do {
389+ let decodedNotification = try decoder. decode (
390+ Message< InitializedNotification> . self , from: data)
391+ #expect( decodedNotification. method == InitializedNotification . name)
392+ } catch {
393+ #expect( Bool ( false ) , " Failed to decode notification: \( error) " )
394+ }
395+ } else {
396+ #expect( Bool ( false ) , " No message was sent " )
397+ }
398+
399+ await client. disconnect ( )
400+ }
401+
402+ @Test ( " Initialize sends initialized notification " )
403+ func testClientInitializeNotification( ) async throws {
404+ let transport = MockTransport ( )
405+ let client = Client ( name: " TestClient " , version: " 1.0 " )
406+
407+ try await client. connect ( transport: transport)
408+ try await Task . sleep ( for: . milliseconds( 10 ) )
409+
410+ // Create a task for initialize
411+ let initTask = Task {
412+ // Queue a response for the initialize request
413+ try await Task . sleep ( for: . milliseconds( 10 ) ) // Wait for request to be sent
414+
415+ if let lastMessage = await transport. sentMessages. last,
416+ let data = lastMessage. data ( using: . utf8) ,
417+ let request = try ? JSONDecoder ( ) . decode ( Request< Initialize> . self , from: data)
418+ {
419+
420+ // Create a valid initialize response
421+ let response = Initialize . response (
422+ id: request. id,
423+ result: . init(
424+ protocolVersion: Version . latest,
425+ capabilities: . init( ) ,
426+ serverInfo: . init( name: " TestServer " , version: " 1.0 " ) ,
427+ instructions: nil
428+ )
429+ )
430+
431+ try await transport. queue ( response: response)
432+
433+ // Now complete the initialize call
434+ _ = try await client. initialize ( )
435+
436+ // Verify that two messages were sent: initialize request and initialized notification
437+ #expect( await transport. sentMessages. count == 2 )
438+
439+ // Check that the second message is the initialized notification
440+ let notifications = await transport. sentMessages
441+ if notifications. count >= 2 {
442+ let notificationJson = notifications [ 1 ]
443+ if let notificationData = notificationJson. data ( using: . utf8) {
444+ do {
445+ let decoder = JSONDecoder ( )
446+ let decodedNotification = try decoder. decode (
447+ Message< InitializedNotification> . self , from: notificationData)
448+ #expect( decodedNotification. method == InitializedNotification . name)
449+ } catch {
450+ #expect( Bool ( false ) , " Failed to decode notification: \( error) " )
451+ }
452+ } else {
453+ #expect( Bool ( false ) , " Could not convert notification to data " )
454+ }
455+ } else {
456+ #expect(
457+ Bool ( false ) , " Expected both initialize request and initialized notification "
458+ )
459+ }
460+ }
461+ }
462+
463+ // Wait with timeout
464+ let timeoutTask = Task {
465+ try await Task . sleep ( for: . seconds( 1 ) )
466+ initTask. cancel ( )
467+ }
468+
469+ // Wait for the task to complete
470+ do {
471+ _ = try await initTask. value
472+ } catch is CancellationError {
473+ #expect( Bool ( false ) , " Test timed out " )
474+ } catch {
475+ #expect( Bool ( false ) , " Unexpected error: \( error) " )
476+ }
477+
478+ timeoutTask. cancel ( )
479+
480+ await client. disconnect ( )
481+ }
366482}
0 commit comments