Initial implementation
This commit is contained in:
parent
cc603d35fe
commit
be1e9583f8
1 changed files with 4304 additions and 884 deletions
79
Tests/MCPTests/RequestTests.swift
Normal file
79
Tests/MCPTests/RequestTests.swift
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue