Update AnyNotification to include notification params (#86)
* Update AnyNotification to not wipe out params that come with a notification * Add test coverage for notification with params * Allow params to be omitted when empty --------- Co-authored-by: Stephen Tallent <stallent@mercury.io> Co-authored-by: Mattt Zmuda <mattt@loopwork.com>
This commit is contained in:
parent
25dee3bb4c
commit
99517b0564
1 changed files with 89 additions and 5 deletions
|
|
@ -86,4 +86,87 @@ struct NotificationTests {
|
|||
|
||||
#expect(decoded.method == InitializedNotification.name)
|
||||
}
|
||||
|
||||
@Test("Resource updated notification with parameters")
|
||||
func testResourceUpdatedNotification() throws {
|
||||
let params = ResourceUpdatedNotification.Parameters(uri: "test://resource")
|
||||
let notification = ResourceUpdatedNotification.message(params)
|
||||
|
||||
#expect(notification.method == ResourceUpdatedNotification.name)
|
||||
#expect(notification.params.uri == "test://resource")
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
let decoder = JSONDecoder()
|
||||
|
||||
let data = try encoder.encode(notification)
|
||||
|
||||
// Verify the exact JSON structure
|
||||
let json = try JSONDecoder().decode([String: Value].self, from: data)
|
||||
#expect(json["jsonrpc"] == "2.0")
|
||||
#expect(json["method"] == "notifications/resources/updated")
|
||||
#expect(json["params"] != nil)
|
||||
#expect(json.count == 3, "Should contain jsonrpc, method, and params fields")
|
||||
|
||||
// Verify we can decode it back
|
||||
let decoded = try decoder.decode(Message<ResourceUpdatedNotification>.self, from: data)
|
||||
#expect(decoded.method == ResourceUpdatedNotification.name)
|
||||
#expect(decoded.params.uri == "test://resource")
|
||||
}
|
||||
|
||||
@Test("AnyNotification decoding - without params")
|
||||
func testAnyNotificationDecodingWithoutParams() throws {
|
||||
// Test decoding when params field is missing
|
||||
let jsonString = """
|
||||
{"jsonrpc":"2.0","method":"notifications/initialized"}
|
||||
"""
|
||||
let data = jsonString.data(using: .utf8)!
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
let decoded = try decoder.decode(AnyMessage.self, from: data)
|
||||
|
||||
#expect(decoded.method == InitializedNotification.name)
|
||||
}
|
||||
|
||||
@Test("AnyNotification decoding - with null params")
|
||||
func testAnyNotificationDecodingWithNullParams() throws {
|
||||
// Test decoding when params field is null
|
||||
let jsonString = """
|
||||
{"jsonrpc":"2.0","method":"notifications/initialized","params":null}
|
||||
"""
|
||||
let data = jsonString.data(using: .utf8)!
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
let decoded = try decoder.decode(AnyMessage.self, from: data)
|
||||
|
||||
#expect(decoded.method == InitializedNotification.name)
|
||||
}
|
||||
|
||||
@Test("AnyNotification decoding - with empty params")
|
||||
func testAnyNotificationDecodingWithEmptyParams() throws {
|
||||
// Test decoding when params field is empty
|
||||
let jsonString = """
|
||||
{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}
|
||||
"""
|
||||
let data = jsonString.data(using: .utf8)!
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
let decoded = try decoder.decode(AnyMessage.self, from: data)
|
||||
|
||||
#expect(decoded.method == InitializedNotification.name)
|
||||
}
|
||||
|
||||
@Test("AnyNotification decoding - with non-empty params")
|
||||
func testAnyNotificationDecodingWithNonEmptyParams() throws {
|
||||
// Test decoding when params field has values
|
||||
let jsonString = """
|
||||
{"jsonrpc":"2.0","method":"notifications/resources/updated","params":{"uri":"test://resource"}}
|
||||
"""
|
||||
let data = jsonString.data(using: .utf8)!
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
let decoded = try decoder.decode(AnyMessage.self, from: data)
|
||||
|
||||
#expect(decoded.method == ResourceUpdatedNotification.name)
|
||||
#expect(decoded.params.objectValue?["uri"]?.stringValue == "test://resource")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue