Skip to content

Commit 6f35f8f

Browse files
committed
Fix tests for notifications
1 parent 6ec6d3c commit 6f35f8f

File tree

5 files changed

+48
-51
lines changed

5 files changed

+48
-51
lines changed

kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/FeatureNotificationService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ internal class FeatureNotificationService(
348348
job.start()
349349
}
350350

351-
/** Returns the current timestamp in milliseconds. */
351+
/** Returns the current timestamp in milliseconds since Unix epoch. */
352352
@OptIn(ExperimentalTime::class)
353353
private fun getCurrentTimestamp(): Long = clock.now().toEpochMilliseconds()
354354

kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerPromptsNotificationTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class ServerPromptsNotificationTest : AbstractServerFeaturesTest() {
2323
@Test
2424
fun `addPrompt should send notification`() = runTest {
2525
// Configure notification handler
26-
var promptListChangedNotificationReceived = false
26+
val notifications = mutableListOf<PromptListChangedNotification>()
2727
client.setNotificationHandler<PromptListChangedNotification>(Method.Defined.NotificationsPromptsListChanged) {
28-
promptListChangedNotificationReceived = true
28+
notifications.add(it)
2929
CompletableDeferred(Unit)
3030
}
3131

@@ -46,16 +46,16 @@ class ServerPromptsNotificationTest : AbstractServerFeaturesTest() {
4646

4747
// Verify that the notification was sent
4848
await untilAsserted {
49-
assertTrue(promptListChangedNotificationReceived, "Notification should be sent when prompt is added")
49+
assertTrue(notifications.isNotEmpty(), "Notification should be sent when prompt is added")
5050
}
5151
}
5252

5353
@Test
5454
fun `removePrompts should remove multiple prompts and send two notifications`() = runTest {
5555
// Configure notification handler
56-
var promptListChangedNotificationReceivedCount = 0
56+
val notifications = mutableListOf<PromptListChangedNotification>()
5757
client.setNotificationHandler<PromptListChangedNotification>(Method.Defined.NotificationsPromptsListChanged) {
58-
promptListChangedNotificationReceivedCount += 1
58+
notifications.add(it)
5959
CompletableDeferred(Unit)
6060
}
6161

@@ -86,7 +86,7 @@ class ServerPromptsNotificationTest : AbstractServerFeaturesTest() {
8686
await untilAsserted {
8787
assertEquals(
8888
4,
89-
promptListChangedNotificationReceivedCount,
89+
notifications.size,
9090
"Two notifications should be sent when prompts are added and two when removed",
9191
)
9292
}
@@ -95,9 +95,9 @@ class ServerPromptsNotificationTest : AbstractServerFeaturesTest() {
9595
@Test
9696
fun `notification should not be send when removed prompt does not exists`() = runTest {
9797
// Track notifications
98-
var promptListChangedNotificationReceived = false
98+
val notifications = mutableListOf<PromptListChangedNotification>()
9999
client.setNotificationHandler<PromptListChangedNotification>(Method.Defined.NotificationsPromptsListChanged) {
100-
promptListChangedNotificationReceived = true
100+
notifications.add(it)
101101
CompletableDeferred(Unit)
102102
}
103103

@@ -107,8 +107,8 @@ class ServerPromptsNotificationTest : AbstractServerFeaturesTest() {
107107
// Verify the result
108108
assertFalse(result, "Removing non-existent prompt should return false")
109109
await untilAsserted {
110-
assertFalse(
111-
promptListChangedNotificationReceived,
110+
assertTrue(
111+
notifications.isEmpty(),
112112
"No notification should be sent when prompt doesn't exist",
113113
)
114114
}

kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerResourcesNotificationSubscribeTest.kt

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@ package io.modelcontextprotocol.kotlin.sdk.server
22

33
import io.modelcontextprotocol.kotlin.sdk.types.Method
44
import io.modelcontextprotocol.kotlin.sdk.types.ReadResourceResult
5-
import io.modelcontextprotocol.kotlin.sdk.types.ResourceListChangedNotification
65
import io.modelcontextprotocol.kotlin.sdk.types.ResourceUpdatedNotification
76
import io.modelcontextprotocol.kotlin.sdk.types.ServerCapabilities
87
import io.modelcontextprotocol.kotlin.sdk.types.SubscribeRequest
98
import io.modelcontextprotocol.kotlin.sdk.types.SubscribeRequestParams
109
import io.modelcontextprotocol.kotlin.sdk.types.TextResourceContents
10+
import kotlin.test.assertFalse
11+
import kotlin.test.assertTrue
1112
import kotlinx.coroutines.CompletableDeferred
1213
import kotlinx.coroutines.test.runTest
1314
import org.awaitility.kotlin.await
1415
import org.awaitility.kotlin.untilAsserted
1516
import org.junit.jupiter.api.Test
16-
import kotlin.test.assertEquals
17-
import kotlin.test.assertFalse
18-
import kotlin.test.assertTrue
1917

2018
class ServerResourcesNotificationSubscribeTest : AbstractServerFeaturesTest() {
2119

@@ -52,8 +50,6 @@ class ServerResourcesNotificationSubscribeTest : AbstractServerFeaturesTest() {
5250
)
5351
}
5452

55-
client.subscribeResource(SubscribeRequest(SubscribeRequestParams(uri = testResourceUri1)))
56-
5753
server.addResource(
5854
uri = testResourceUri2,
5955
name = "Test Resource 2",
@@ -71,6 +67,8 @@ class ServerResourcesNotificationSubscribeTest : AbstractServerFeaturesTest() {
7167
)
7268
}
7369

70+
client.subscribeResource(SubscribeRequest(SubscribeRequestParams(uri = testResourceUri1)))
71+
7472
// Remove the resource
7573
val result = server.removeResource(testResourceUri1)
7674

@@ -79,9 +77,11 @@ class ServerResourcesNotificationSubscribeTest : AbstractServerFeaturesTest() {
7977

8078
// Verify that the notification was sent
8179
await untilAsserted {
82-
assertEquals(1, notifications.size, "Notification should be sent when resource 1 was deleted")
80+
assertTrue(
81+
notifications.any { it.params.uri == testResourceUri1 },
82+
"Notification should be sent when resource 1 was deleted"
83+
)
8384
}
84-
assertEquals(testResourceUri1, notifications[0].params.uri, "Notification should contain the resource 1 URI")
8585
}
8686

8787
@Test
@@ -144,9 +144,8 @@ class ServerResourcesNotificationSubscribeTest : AbstractServerFeaturesTest() {
144144
println(notifications.map { it.params.uri })
145145
// Verify that the notification was sent
146146
await untilAsserted {
147-
assertEquals(
148-
2,
149-
notifications.size,
147+
assertTrue(
148+
notifications.any { it.params.uri == testResourceUri1 },
150149
"Notification should be sent when resource 1 and resource 2 was deleted",
151150
)
152151
}
@@ -165,11 +164,9 @@ class ServerResourcesNotificationSubscribeTest : AbstractServerFeaturesTest() {
165164
@Test
166165
fun `notification should not be send when removed resource does not exists`() = runTest {
167166
// Track notifications
168-
var resourceListChangedNotificationReceived = false
169-
client.setNotificationHandler<ResourceListChangedNotification>(
170-
Method.Defined.NotificationsResourcesListChanged,
171-
) {
172-
resourceListChangedNotificationReceived = true
167+
val notifications = mutableListOf<ResourceUpdatedNotification>()
168+
client.setNotificationHandler<ResourceUpdatedNotification>(Method.Defined.NotificationsResourcesUpdated) {
169+
notifications.add(it)
173170
CompletableDeferred(Unit)
174171
}
175172

@@ -178,8 +175,8 @@ class ServerResourcesNotificationSubscribeTest : AbstractServerFeaturesTest() {
178175

179176
// Verify the result
180177
assertFalse(result, "Removing non-existent resource should return false")
181-
assertFalse(
182-
resourceListChangedNotificationReceived,
178+
assertTrue(
179+
notifications.isEmpty(),
183180
"No notification should be sent when resource doesn't exist",
184181
)
185182
}

kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerResourcesNotificationTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class ServerResourcesNotificationTest : AbstractServerFeaturesTest() {
2323
@Test
2424
fun `addResource should send notification`() = runTest {
2525
// Configure notification handler
26-
var resourceListChangedNotificationReceived = false
26+
val notifications = mutableListOf<ResourceListChangedNotification>()
2727
client.setNotificationHandler<ResourceListChangedNotification>(
2828
Method.Defined.NotificationsResourcesListChanged,
2929
) {
30-
resourceListChangedNotificationReceived = true
30+
notifications.add(it)
3131
CompletableDeferred(Unit)
3232
}
3333

@@ -58,18 +58,18 @@ class ServerResourcesNotificationTest : AbstractServerFeaturesTest() {
5858

5959
// Verify that the notification was sent
6060
await untilAsserted {
61-
assertTrue(resourceListChangedNotificationReceived, "Notification should be sent when resource is added")
61+
assertTrue(notifications.isNotEmpty(), "Notification should be sent when resource is added")
6262
}
6363
}
6464

6565
@Test
6666
fun `removeResources should remove multiple resources and send two notifications`() = runTest {
6767
// Configure notification handler
68-
var resourceListChangedNotificationReceivedCount = 0
68+
val notifications = mutableListOf<ResourceListChangedNotification>()
6969
client.setNotificationHandler<ResourceListChangedNotification>(
7070
Method.Defined.NotificationsResourcesListChanged,
7171
) {
72-
resourceListChangedNotificationReceivedCount += 1
72+
notifications.add(it)
7373
CompletableDeferred(Unit)
7474
}
7575

@@ -120,7 +120,7 @@ class ServerResourcesNotificationTest : AbstractServerFeaturesTest() {
120120
await untilAsserted {
121121
assertEquals(
122122
4,
123-
resourceListChangedNotificationReceivedCount,
123+
notifications.size,
124124
"Two notifications should be sent when resources are added and two when removed",
125125
)
126126
}
@@ -129,11 +129,11 @@ class ServerResourcesNotificationTest : AbstractServerFeaturesTest() {
129129
@Test
130130
fun `notification should not be send when removed resource does not exists`() = runTest {
131131
// Track notifications
132-
var resourceListChangedNotificationReceived = false
132+
val notifications = mutableListOf<ResourceListChangedNotification>()
133133
client.setNotificationHandler<ResourceListChangedNotification>(
134134
Method.Defined.NotificationsResourcesListChanged,
135135
) {
136-
resourceListChangedNotificationReceived = true
136+
notifications.add(it)
137137
CompletableDeferred(Unit)
138138
}
139139

@@ -142,8 +142,8 @@ class ServerResourcesNotificationTest : AbstractServerFeaturesTest() {
142142

143143
// Verify the result
144144
assertFalse(result, "Removing non-existent resource should return false")
145-
assertFalse(
146-
resourceListChangedNotificationReceived,
145+
assertTrue(
146+
notifications.isEmpty(),
147147
"No notification should be sent when resource doesn't exist",
148148
)
149149
}

kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerToolsNotificationTest.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import io.modelcontextprotocol.kotlin.sdk.types.ServerCapabilities
66
import io.modelcontextprotocol.kotlin.sdk.types.TextContent
77
import io.modelcontextprotocol.kotlin.sdk.types.ToolListChangedNotification
88
import io.modelcontextprotocol.kotlin.sdk.types.ToolSchema
9+
import kotlin.test.assertEquals
10+
import kotlin.test.assertFalse
11+
import kotlin.test.assertTrue
912
import kotlinx.coroutines.CompletableDeferred
1013
import kotlinx.coroutines.test.runTest
1114
import org.awaitility.kotlin.await
1215
import org.awaitility.kotlin.untilAsserted
1316
import org.junit.jupiter.api.Test
14-
import kotlin.test.assertEquals
15-
import kotlin.test.assertFalse
16-
import kotlin.test.assertTrue
1717

1818
class ServerToolsNotificationTest : AbstractServerFeaturesTest() {
1919

@@ -24,9 +24,9 @@ class ServerToolsNotificationTest : AbstractServerFeaturesTest() {
2424
@Test
2525
fun `addTool should send notification`() = runTest {
2626
// Configure notification handler
27-
var toolListChangedNotificationReceived = false
27+
val notifications = mutableListOf<ToolListChangedNotification>()
2828
client.setNotificationHandler<ToolListChangedNotification>(Method.Defined.NotificationsToolsListChanged) {
29-
toolListChangedNotificationReceived = true
29+
notifications.add(it)
3030
CompletableDeferred(Unit)
3131
}
3232

@@ -43,16 +43,16 @@ class ServerToolsNotificationTest : AbstractServerFeaturesTest() {
4343

4444
// Verify that the notification was sent
4545
await untilAsserted {
46-
assertTrue(toolListChangedNotificationReceived, "Notification should be sent when tool is added")
46+
assertTrue(notifications.isNotEmpty(), "Notification should be sent when tool is added")
4747
}
4848
}
4949

5050
@Test
5151
fun `removeTools should remove multiple tools and send two notifications`() = runTest {
5252
// Configure notification handler
53-
var toolListChangedNotificationReceivedCount = 0
53+
val notifications = mutableListOf<ToolListChangedNotification>()
5454
client.setNotificationHandler<ToolListChangedNotification>(Method.Defined.NotificationsToolsListChanged) {
55-
toolListChangedNotificationReceivedCount += 1
55+
notifications.add(it)
5656
CompletableDeferred(Unit)
5757
}
5858

@@ -73,7 +73,7 @@ class ServerToolsNotificationTest : AbstractServerFeaturesTest() {
7373
await untilAsserted {
7474
assertEquals(
7575
4,
76-
toolListChangedNotificationReceivedCount,
76+
notifications.size,
7777
"Two notifications should be sent when tools are added and two when removed",
7878
)
7979
}
@@ -82,9 +82,9 @@ class ServerToolsNotificationTest : AbstractServerFeaturesTest() {
8282
@Test
8383
fun `notification should not be send when removed tool does not exists`() = runTest {
8484
// Track notifications
85-
var toolListChangedNotificationReceived = false
85+
val notifications = mutableListOf<ToolListChangedNotification>()
8686
client.setNotificationHandler<ToolListChangedNotification>(Method.Defined.NotificationsToolsListChanged) {
87-
toolListChangedNotificationReceived = true
87+
notifications.add(it)
8888
CompletableDeferred(Unit)
8989
}
9090

@@ -95,6 +95,6 @@ class ServerToolsNotificationTest : AbstractServerFeaturesTest() {
9595

9696
// Verify the result
9797
assertFalse(result, "Removing non-existent tool should return false")
98-
assertFalse(toolListChangedNotificationReceived, "No notification should be sent when tool doesn't exist")
98+
assertTrue(notifications.isEmpty(), "No notification should be sent when tool doesn't exist")
9999
}
100100
}

0 commit comments

Comments
 (0)