Update transports to send and receive data instead of strings (#42)

* Update transports to send and receive data instead of strings

* Rename MockTransport helper methods

* Adopt Task.sleep(for:...) instead of variant taking nanoseconds

* Update comment

* Revert inadvertant change to visibility of send method
This commit is contained in:
Mattt 2025-04-03 04:42:56 -07:00 • committed by GitHub
commit a9bcbf9ad9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -22,8 +22,8 @@ struct ServerTests {
let transport = MockTransport()
// Queue an initialize request
try await transport.queueRequest(
Initialize.request(
try await transport.queue(
request: Initialize.request(
.init(
protocolVersion: Version.latest,
capabilities: .init(),
@ -42,7 +42,11 @@ struct ServerTests {
try await Task.sleep(nanoseconds: 10_000_000) // 10ms
#expect(await transport.sentMessages.count == 1)
#expect(await transport.sentMessages[0].contains(Initialize.name))
let messages = await transport.sentMessages
if let response = messages.first {
#expect(response.contains("serverInfo"))
}
// Clean up
await server.stop()
@ -68,13 +72,13 @@ struct ServerTests {
#expect(clientInfo.version == "1.0")
await state.setHookCalled()
}
// Wait for server to initialize
try await Task.sleep(nanoseconds: 10_000_000) // 10ms
try await Task.sleep(for: .milliseconds(10))
// Queue an initialize request
try await transport.queueRequest(
Initialize.request(
try await transport.queue(
request: Initialize.request(
.init(
protocolVersion: Version.latest,
capabilities: .init(),
@ -83,7 +87,7 @@ struct ServerTests {
))
// Wait for message processing and hook execution
try await Task.sleep(nanoseconds: 200_000_000) // 200ms
try await Task.sleep(for: .milliseconds(500))
#expect(await state.wasHookCalled() == true)
#expect(await transport.sentMessages.count >= 1)
@ -94,6 +98,7 @@ struct ServerTests {
}
await server.stop()
await transport.disconnect()
}
@Test("Initialize hook - rejection")
@ -107,13 +112,13 @@ struct ServerTests {
throw Error.invalidRequest("Client not allowed")
}
}
// Wait for server to initialize
try await Task.sleep(nanoseconds: 10_000_000) // 10ms
// Queue an initialize request from blocked client
try await transport.queueRequest(
Initialize.request(
try await transport.queue(
request: Initialize.request(
.init(
protocolVersion: Version.latest,
capabilities: .init(),
@ -124,13 +129,15 @@ struct ServerTests {
// Wait for message processing
try await Task.sleep(nanoseconds: 200_000_000) // 200ms
#expect(await transport.sentMessages.count >= 2)
#expect(await transport.sentMessages.count >= 1)
let messages = await transport.sentMessages
if let response = messages.first {
#expect(response.contains("error"))
#expect(response.contains("Client not allowed"))
}
await server.stop()
await transport.disconnect()
}
}