swift-sdk/Tests/MCPTests/RequestTests.swift
2025-02-11 15:22:35 -08:00

79 lines
2.4 KiB
Swift

import Testing
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder
@testable import MCP
@Suite("Request Tests")
struct RequestTests {
struct TestMethod: Method {
struct Parameters: Codable, Hashable, Sendable {
let value: String
}
struct Result: Codable, Hashable, Sendable {
let success: Bool
}
static let name = "test.method"
}
struct EmptyMethod: Method {
static let name = "empty.method"
}
@Test("Request initialization with parameters")
func testRequestInitialization() throws {
let id: ID = "test-id"
let params = TestMethod.Parameters(value: "test")
let request = Request<TestMethod>(id: id, method: TestMethod.name, params: params)
#expect(request.id == id)
#expect(request.method == TestMethod.name)
#expect(request.params.value == "test")
}
@Test("Request encoding and decoding")
func testRequestEncodingDecoding() throws {
let request = TestMethod.request(id: "test-id", TestMethod.Parameters(value: "test"))
let encoder = JSONEncoder()
let decoder = JSONDecoder()
let data = try encoder.encode(request)
let decoded = try decoder.decode(Request<TestMethod>.self, from: data)
#expect(decoded.id == request.id)
#expect(decoded.method == request.method)
#expect(decoded.params.value == request.params.value)
}
@Test("Empty parameters request encoding")
func testEmptyParametersRequestEncoding() throws {
let request = EmptyMethod.request(id: "test-id")
let encoder = JSONEncoder()
let decoder = JSONDecoder()
let data = try encoder.encode(request)
// Verify we can decode it back
let decoded = try decoder.decode(Request<EmptyMethod>.self, from: data)
#expect(decoded.id == request.id)
#expect(decoded.method == request.method)
}
@Test("Empty parameters request decoding")
func testEmptyParametersRequestDecoding() throws {
// Create a minimal JSON string
let jsonString = """
{"jsonrpc":"2.0","id":"test-id","method":"empty.method"}
"""
let data = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let decoded = try decoder.decode(Request<EmptyMethod>.self, from: data)
#expect(decoded.id == "test-id")
#expect(decoded.method == EmptyMethod.name)
}
}