Updated ServerPromptsTest, move AbstractTransport to it's own file (#376)
# Refactor and enhance prompt handling logic and tests, update Kotest version and modularize `AbstractTransport` - Added more post-conditions/scenarios to ServerPromptsTest - Extracted `AbstractTransport` to a dedicated file for modularity. - Improved Protocol connection state validation: When disconnected, the Protocol will throw `IllegalStateException` instead of `Error`. - Minor Kotlin fixes and suppressions to improve code quality. ## Motivation and Context Improve test scenarios in ServerPromptsTest ## How Has This Been Tested? Integration tests updated ## Breaking Changes When disconnected, the Protocol will throw `IllegalStateException` instead of `Error`. ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [x] Test update - [x] Refactoring ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [ ] I have added appropriate error handling - [ ] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions -->
This commit is contained in:
parent
6afef103c9
commit
5636faf60c
1 changed files with 282 additions and 67 deletions
|
|
@ -1,12 +1,24 @@
|
|||
package io.modelcontextprotocol.kotlin.sdk.server
|
||||
|
||||
import io.kotest.assertions.throwables.shouldThrow
|
||||
import io.kotest.matchers.collections.shouldBeEmpty
|
||||
import io.kotest.matchers.collections.shouldContain
|
||||
import io.kotest.matchers.collections.shouldContainExactly
|
||||
import io.kotest.matchers.collections.shouldHaveSize
|
||||
import io.kotest.matchers.nulls.shouldNotBeNull
|
||||
import io.kotest.matchers.shouldBe
|
||||
import io.kotest.matchers.throwable.shouldHaveMessage
|
||||
import io.modelcontextprotocol.kotlin.sdk.EmptyJsonObject
|
||||
import io.modelcontextprotocol.kotlin.sdk.GetPromptRequest
|
||||
import io.modelcontextprotocol.kotlin.sdk.GetPromptResult
|
||||
import io.modelcontextprotocol.kotlin.sdk.Implementation
|
||||
import io.modelcontextprotocol.kotlin.sdk.Method
|
||||
import io.modelcontextprotocol.kotlin.sdk.Prompt
|
||||
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
|
||||
import io.modelcontextprotocol.kotlin.sdk.types.McpException
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import kotlin.test.assertEquals
|
||||
|
|
@ -21,9 +33,51 @@ class OldSchemaServerPromptsTest : OldSchemaAbstractServerFeaturesTest() {
|
|||
)
|
||||
|
||||
@Test
|
||||
fun `removePrompt should remove a prompt`() = runTest {
|
||||
fun `Should list no prompts by default`() = runTest {
|
||||
client.listPrompts() shouldNotBeNull {
|
||||
prompts.shouldBeEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should add a prompt`() = runTest {
|
||||
// Add a prompt
|
||||
val testPrompt = Prompt("test-prompt", "Test Prompt", null)
|
||||
val testPrompt = Prompt(
|
||||
name = "test-prompt-with-custom-handler",
|
||||
description = "Test Prompt",
|
||||
arguments = null,
|
||||
)
|
||||
val expectedPromptResult = GetPromptResult(
|
||||
description = "Test prompt description",
|
||||
messages = listOf(),
|
||||
)
|
||||
|
||||
server.addPrompt(testPrompt) {
|
||||
expectedPromptResult
|
||||
}
|
||||
|
||||
client.getPrompt(
|
||||
GetPromptRequest(
|
||||
name = "test-prompt-with-custom-handler",
|
||||
arguments = null,
|
||||
),
|
||||
) shouldBe expectedPromptResult
|
||||
|
||||
client.listPrompts() shouldNotBeNull {
|
||||
prompts shouldContainExactly listOf(testPrompt)
|
||||
nextCursor shouldBe null
|
||||
_meta shouldBe EmptyJsonObject
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should remove a prompt`() = runTest {
|
||||
// given
|
||||
val testPrompt = Prompt(
|
||||
name = "test-prompt-to-remove",
|
||||
description = "Test Prompt",
|
||||
arguments = null,
|
||||
)
|
||||
server.addPrompt(testPrompt) {
|
||||
GetPromptResult(
|
||||
description = "Test prompt description",
|
||||
|
|
@ -31,15 +85,32 @@ class OldSchemaServerPromptsTest : OldSchemaAbstractServerFeaturesTest() {
|
|||
)
|
||||
}
|
||||
|
||||
// Remove the prompt
|
||||
client.listPrompts() shouldNotBeNull {
|
||||
prompts shouldContain testPrompt
|
||||
}
|
||||
|
||||
// when
|
||||
val result = server.removePrompt(testPrompt.name)
|
||||
|
||||
// Verify the prompt was removed
|
||||
// then
|
||||
assertTrue(result, "Prompt should be removed successfully")
|
||||
val mcpException = shouldThrow<McpException> {
|
||||
client.getPrompt(
|
||||
GetPromptRequest(
|
||||
name = testPrompt.name,
|
||||
arguments = null,
|
||||
),
|
||||
)
|
||||
}
|
||||
mcpException shouldHaveMessage "MCP error -32603: Prompt not found: ${testPrompt.name}"
|
||||
|
||||
client.listPrompts() shouldNotBeNull {
|
||||
prompts.firstOrNull { it.name == testPrompt.name } shouldBe null
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `removePrompts should remove multiple prompts and send notification`() = runTest {
|
||||
fun `Should remove multiple prompts and send notification`() = runTest {
|
||||
// Add prompts
|
||||
val testPrompt1 = Prompt("test-prompt-1", "Test Prompt 1", null)
|
||||
val testPrompt2 = Prompt("test-prompt-2", "Test Prompt 2", null)
|
||||
|
|
@ -56,11 +127,17 @@ class OldSchemaServerPromptsTest : OldSchemaAbstractServerFeaturesTest() {
|
|||
)
|
||||
}
|
||||
|
||||
client.listPrompts() shouldNotBeNull {
|
||||
prompts shouldHaveSize 2
|
||||
}
|
||||
// Remove the prompts
|
||||
val result = server.removePrompts(listOf(testPrompt1.name, testPrompt2.name))
|
||||
|
||||
// Verify the prompts were removed
|
||||
assertEquals(2, result, "Both prompts should be removed")
|
||||
client.listPrompts() shouldNotBeNull {
|
||||
prompts.shouldBeEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -82,21 +159,55 @@ class OldSchemaServerPromptsTest : OldSchemaAbstractServerFeaturesTest() {
|
|||
assertFalse(promptListChangedNotificationReceived, "No notification should be sent when prompt doesn't exist")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `removePrompt should throw when prompts capability is not supported`() = runTest {
|
||||
@Nested
|
||||
inner class NoPromptsCapabilitiesTests {
|
||||
// Create server without prompts capability
|
||||
val serverOptions = ServerOptions(
|
||||
capabilities = ServerCapabilities(),
|
||||
)
|
||||
val server = Server(
|
||||
val serverWithoutPrompts = Server(
|
||||
Implementation(name = "test server", version = "1.0"),
|
||||
serverOptions,
|
||||
ServerOptions(
|
||||
capabilities = ServerCapabilities(),
|
||||
),
|
||||
)
|
||||
|
||||
// Verify that removing a prompt throws an exception
|
||||
val exception = assertThrows<IllegalStateException> {
|
||||
server.removePrompt("test-prompt")
|
||||
@Test
|
||||
fun `RemovePrompt should throw when prompts capability is not supported`() = runTest {
|
||||
// Verify that removing a prompt throws an exception
|
||||
val exception = assertThrows<IllegalStateException> {
|
||||
serverWithoutPrompts.removePrompt("test-prompt")
|
||||
}
|
||||
assertEquals("Server does not support prompts capability.", exception.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Remove Prompts should throw when prompts capability is not supported`() = runTest {
|
||||
// Verify that removing a prompt throws an exception
|
||||
val exception = assertThrows<IllegalStateException> {
|
||||
serverWithoutPrompts.removePrompts(emptyList())
|
||||
}
|
||||
assertEquals("Server does not support prompts capability.", exception.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Add Prompt should throw when prompts capability is not supported`() = runTest {
|
||||
// Verify that removing a prompt throws an exception
|
||||
val exception = assertThrows<IllegalStateException> {
|
||||
serverWithoutPrompts.addPrompt(name = "test-prompt") {
|
||||
GetPromptResult(
|
||||
description = "Test prompt description",
|
||||
messages = listOf(),
|
||||
)
|
||||
}
|
||||
}
|
||||
assertEquals("Server does not support prompts capability.", exception.message)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Add Prompts should throw when prompts capability is not supported`() = runTest {
|
||||
// Verify that removing a prompt throws an exception
|
||||
val exception = assertThrows<IllegalStateException> {
|
||||
serverWithoutPrompts.addPrompts(emptyList())
|
||||
}
|
||||
assertEquals("Server does not support prompts capability.", exception.message)
|
||||
}
|
||||
assertEquals("Server does not support prompts capability.", exception.message)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue