Skip to content

Commit 8d12d70

Browse files
committed
Refactor test ServerTest structure
Split ServerTest per feature test classes
1 parent 913c0d1 commit 8d12d70

File tree

6 files changed

+443
-530
lines changed

6 files changed

+443
-530
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.modelcontextprotocol.kotlin.sdk.server
2+
3+
import io.modelcontextprotocol.kotlin.sdk.Implementation
4+
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
5+
import io.modelcontextprotocol.kotlin.sdk.client.Client
6+
import io.modelcontextprotocol.kotlin.sdk.shared.InMemoryTransport
7+
import kotlinx.coroutines.launch
8+
import kotlinx.coroutines.runBlocking
9+
import org.junit.jupiter.api.BeforeEach
10+
11+
abstract class AbstractServerFeaturesTest {
12+
13+
protected lateinit var server: Server
14+
protected lateinit var client: Client
15+
16+
abstract fun getServerCapabilities(): ServerCapabilities
17+
18+
protected open fun getServerInstructionsProvider(): (() -> String)? = null
19+
20+
@BeforeEach
21+
fun setUp() {
22+
val serverOptions = ServerOptions(
23+
capabilities = getServerCapabilities()
24+
)
25+
26+
server = Server(
27+
serverInfo=Implementation(name = "test server", version = "1.0"),
28+
options=serverOptions,
29+
instructionsProvider = getServerInstructionsProvider()
30+
)
31+
32+
val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair()
33+
34+
client = Client(
35+
clientInfo = Implementation(name = "test client", version = "1.0"),
36+
)
37+
38+
runBlocking {
39+
// Connect client and server
40+
launch { client.connect(clientTransport) }
41+
launch { server.createSession(serverTransport) }
42+
}
43+
}
44+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.modelcontextprotocol.kotlin.sdk.server
2+
3+
import io.modelcontextprotocol.kotlin.sdk.Implementation
4+
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
5+
import io.modelcontextprotocol.kotlin.sdk.client.Client
6+
import io.modelcontextprotocol.kotlin.sdk.shared.InMemoryTransport
7+
import kotlin.test.assertEquals
8+
import kotlinx.coroutines.test.runTest
9+
import org.junit.jupiter.api.Test
10+
import org.junit.jupiter.api.assertNull
11+
12+
class ServerInstructionsTest {
13+
14+
@Test
15+
fun `Server constructor should accept instructions provider parameter`() = runTest {
16+
val serverInfo = Implementation(name = "test server", version = "1.0")
17+
val serverOptions = ServerOptions(capabilities = ServerCapabilities())
18+
val instructions = "This is a test server. Use it for testing purposes only."
19+
20+
val server = Server(serverInfo, serverOptions, { instructions })
21+
22+
// The instructions should be stored internally and used in handleInitialize
23+
// We can't directly access the private field, but we can test it through initialization
24+
val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair()
25+
val client = Client(clientInfo = Implementation(name = "test client", version = "1.0"))
26+
27+
server.createSession(serverTransport)
28+
client.connect(clientTransport)
29+
30+
assertEquals(instructions, client.serverInstructions)
31+
}
32+
33+
@Test
34+
fun `Server constructor should accept instructions parameter`() = runTest {
35+
val serverInfo = Implementation(name = "test server", version = "1.0")
36+
val serverOptions = ServerOptions(capabilities = ServerCapabilities())
37+
val instructions = "This is a test server. Use it for testing purposes only."
38+
39+
val server = Server(serverInfo, serverOptions, instructions)
40+
41+
// The instructions should be stored internally and used in handleInitialize
42+
// We can't directly access the private field, but we can test it through initialization
43+
val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair()
44+
val client = Client(clientInfo = Implementation(name = "test client", version = "1.0"))
45+
46+
server.createSession(serverTransport)
47+
client.connect(clientTransport)
48+
49+
assertEquals(instructions, client.serverInstructions)
50+
}
51+
52+
@Test
53+
fun `Server constructor should work without instructions parameter`() = runTest {
54+
val serverInfo = Implementation(name = "test server", version = "1.0")
55+
val serverOptions = ServerOptions(capabilities = ServerCapabilities())
56+
57+
// Test that server works when instructions parameter is omitted (defaults to null)
58+
val server = Server(serverInfo, serverOptions)
59+
60+
val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair()
61+
val client = Client(clientInfo = Implementation(name = "test client", version = "1.0"))
62+
63+
server.createSession(serverTransport)
64+
client.connect(clientTransport)
65+
66+
assertNull(client.serverInstructions)
67+
}
68+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package io.modelcontextprotocol.kotlin.sdk.server
2+
3+
import io.modelcontextprotocol.kotlin.sdk.GetPromptResult
4+
import io.modelcontextprotocol.kotlin.sdk.Implementation
5+
import io.modelcontextprotocol.kotlin.sdk.Method
6+
import io.modelcontextprotocol.kotlin.sdk.Prompt
7+
import io.modelcontextprotocol.kotlin.sdk.PromptListChangedNotification
8+
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
9+
import kotlin.test.assertEquals
10+
import kotlin.test.assertFalse
11+
import kotlin.test.assertTrue
12+
import kotlinx.coroutines.CompletableDeferred
13+
import kotlinx.coroutines.test.runTest
14+
import org.junit.jupiter.api.Test
15+
import org.junit.jupiter.api.assertThrows
16+
17+
class ServerPromptsTest : AbstractServerFeaturesTest() {
18+
19+
override fun getServerCapabilities(): ServerCapabilities {
20+
return ServerCapabilities(
21+
prompts = ServerCapabilities.Prompts(false),
22+
)
23+
}
24+
25+
@Test
26+
fun `removePrompt should remove a prompt`() = runTest {
27+
// Add a prompt
28+
val testPrompt = Prompt("test-prompt", "Test Prompt", null)
29+
server.addPrompt(testPrompt) {
30+
GetPromptResult(
31+
description = "Test prompt description",
32+
messages = listOf(),
33+
)
34+
}
35+
36+
// Remove the prompt
37+
val result = server.removePrompt(testPrompt.name)
38+
39+
// Verify the prompt was removed
40+
assertTrue(result, "Prompt should be removed successfully")
41+
}
42+
43+
@Test
44+
fun `removePrompts should remove multiple prompts and send notification`() = runTest {
45+
// Add prompts
46+
val testPrompt1 = Prompt("test-prompt-1", "Test Prompt 1", null)
47+
val testPrompt2 = Prompt("test-prompt-2", "Test Prompt 2", null)
48+
server.addPrompt(testPrompt1) {
49+
GetPromptResult(
50+
description = "Test prompt description 1",
51+
messages = listOf(),
52+
)
53+
}
54+
server.addPrompt(testPrompt2) {
55+
GetPromptResult(
56+
description = "Test prompt description 2",
57+
messages = listOf(),
58+
)
59+
}
60+
61+
// Remove the prompts
62+
val result = server.removePrompts(listOf(testPrompt1.name, testPrompt2.name))
63+
64+
// Verify the prompts were removed
65+
assertEquals(2, result, "Both prompts should be removed")
66+
}
67+
68+
@Test
69+
fun `removePrompt should return false when prompt does not exist`() = runTest {
70+
// Track notifications
71+
var promptListChangedNotificationReceived = false
72+
client.setNotificationHandler<PromptListChangedNotification>(Method.Defined.NotificationsPromptsListChanged) {
73+
promptListChangedNotificationReceived = true
74+
CompletableDeferred(Unit)
75+
}
76+
77+
// Try to remove a non-existent prompt
78+
val result = server.removePrompt("non-existent-prompt")
79+
80+
// Verify the result
81+
assertFalse(result, "Removing non-existent prompt should return false")
82+
assertFalse(promptListChangedNotificationReceived, "No notification should be sent when prompt doesn't exist")
83+
}
84+
85+
@Test
86+
fun `removePrompt should throw when prompts capability is not supported`() = runTest {
87+
// Create server without prompts capability
88+
val serverOptions = ServerOptions(
89+
capabilities = ServerCapabilities(),
90+
)
91+
val server = Server(
92+
Implementation(name = "test server", version = "1.0"),
93+
serverOptions,
94+
)
95+
96+
// Verify that removing a prompt throws an exception
97+
val exception = assertThrows<IllegalStateException> {
98+
server.removePrompt("test-prompt")
99+
}
100+
assertEquals("Server does not support prompts capability.", exception.message)
101+
}
102+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package io.modelcontextprotocol.kotlin.sdk.server
2+
3+
import io.modelcontextprotocol.kotlin.sdk.Implementation
4+
import io.modelcontextprotocol.kotlin.sdk.Method
5+
import io.modelcontextprotocol.kotlin.sdk.ReadResourceResult
6+
import io.modelcontextprotocol.kotlin.sdk.ResourceListChangedNotification
7+
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
8+
import io.modelcontextprotocol.kotlin.sdk.TextResourceContents
9+
import kotlin.test.assertEquals
10+
import kotlin.test.assertFalse
11+
import kotlin.test.assertTrue
12+
import kotlinx.coroutines.CompletableDeferred
13+
import kotlinx.coroutines.test.runTest
14+
import org.junit.jupiter.api.Test
15+
import org.junit.jupiter.api.assertThrows
16+
17+
class ServerResourcesTest : AbstractServerFeaturesTest() {
18+
19+
override fun getServerCapabilities(): ServerCapabilities {
20+
return ServerCapabilities(
21+
resources = ServerCapabilities.Resources(null, null),
22+
)
23+
}
24+
25+
@Test
26+
fun `removeResource should remove a resource and send notification`() = runTest {
27+
// Add a resource
28+
val testResourceUri = "test://resource"
29+
server.addResource(
30+
uri = testResourceUri,
31+
name = "Test Resource",
32+
description = "A test resource",
33+
mimeType = "text/plain",
34+
) {
35+
ReadResourceResult(
36+
contents = listOf(
37+
TextResourceContents(
38+
text = "Test resource content",
39+
uri = testResourceUri,
40+
mimeType = "text/plain",
41+
),
42+
),
43+
)
44+
}
45+
46+
// Remove the resource
47+
val result = server.removeResource(testResourceUri)
48+
49+
// Verify the resource was removed
50+
assertTrue(result, "Resource should be removed successfully")
51+
}
52+
53+
@Test
54+
fun `removeResources should remove multiple resources and send notification`() = runTest {
55+
// Add resources
56+
val testResourceUri1 = "test://resource1"
57+
val testResourceUri2 = "test://resource2"
58+
server.addResource(
59+
uri = testResourceUri1,
60+
name = "Test Resource 1",
61+
description = "A test resource 1",
62+
mimeType = "text/plain",
63+
) {
64+
ReadResourceResult(
65+
contents = listOf(
66+
TextResourceContents(
67+
text = "Test resource content 1",
68+
uri = testResourceUri1,
69+
mimeType = "text/plain",
70+
),
71+
),
72+
)
73+
}
74+
server.addResource(
75+
uri = testResourceUri2,
76+
name = "Test Resource 2",
77+
description = "A test resource 2",
78+
mimeType = "text/plain",
79+
) {
80+
ReadResourceResult(
81+
contents = listOf(
82+
TextResourceContents(
83+
text = "Test resource content 2",
84+
uri = testResourceUri2,
85+
mimeType = "text/plain",
86+
),
87+
),
88+
)
89+
}
90+
91+
// Remove the resources
92+
val result = server.removeResources(listOf(testResourceUri1, testResourceUri2))
93+
94+
// Verify the resources were removed
95+
assertEquals(2, result, "Both resources should be removed")
96+
}
97+
98+
99+
@Test
100+
fun `removeResource should return false when resource does not exist`() = runTest {
101+
// Track notifications
102+
var resourceListChangedNotificationReceived = false
103+
client.setNotificationHandler<ResourceListChangedNotification>(
104+
Method.Defined.NotificationsResourcesListChanged,
105+
) {
106+
resourceListChangedNotificationReceived = true
107+
CompletableDeferred(Unit)
108+
}
109+
110+
// Try to remove a non-existent resource
111+
val result = server.removeResource("non-existent-resource")
112+
113+
// Verify the result
114+
assertFalse(result, "Removing non-existent resource should return false")
115+
assertFalse(
116+
resourceListChangedNotificationReceived,
117+
"No notification should be sent when resource doesn't exist",
118+
)
119+
}
120+
121+
@Test
122+
fun `removeResource should throw when resources capability is not supported`() = runTest {
123+
// Create server without resources capability
124+
val serverOptions = ServerOptions(
125+
capabilities = ServerCapabilities(),
126+
)
127+
val server = Server(
128+
Implementation(name = "test server", version = "1.0"),
129+
serverOptions,
130+
)
131+
132+
// Verify that removing a resource throws an exception
133+
val exception = assertThrows<IllegalStateException> {
134+
server.removeResource("test://resource")
135+
}
136+
assertEquals("Server does not support resources capability.", exception.message)
137+
}
138+
}

0 commit comments

Comments
 (0)