mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 04:34:44 +00:00
mcp: consolidate tests and cleanup imports
This commit is contained in:
@@ -95,3 +95,70 @@ pub const Resource = struct {
|
||||
description: ?[]const u8 = null,
|
||||
mimeType: ?[]const u8 = null,
|
||||
};
|
||||
|
||||
const testing = @import("../testing.zig");
|
||||
|
||||
test "protocol request parsing" {
|
||||
const raw_json =
|
||||
\\{
|
||||
\\ "jsonrpc": "2.0",
|
||||
\\ "id": 1,
|
||||
\\ "method": "initialize",
|
||||
\\ "params": {
|
||||
\\ "protocolVersion": "2024-11-05",
|
||||
\\ "capabilities": {},
|
||||
\\ "clientInfo": {
|
||||
\\ "name": "test-client",
|
||||
\\ "version": "1.0.0"
|
||||
\\ }
|
||||
\\ }
|
||||
\\}
|
||||
;
|
||||
|
||||
const parsed = try std.json.parseFromSlice(Request, testing.allocator, raw_json, .{ .ignore_unknown_fields = true });
|
||||
defer parsed.deinit();
|
||||
|
||||
const req = parsed.value;
|
||||
try testing.expectString("2.0", req.jsonrpc);
|
||||
try testing.expectString("initialize", req.method);
|
||||
try testing.expect(req.id.? == .integer);
|
||||
try testing.expectEqual(@as(i64, 1), req.id.?.integer);
|
||||
try testing.expect(req.params != null);
|
||||
|
||||
// Test nested parsing of InitializeParams
|
||||
const init_params = try std.json.parseFromValue(InitializeParams, testing.allocator, req.params.?, .{ .ignore_unknown_fields = true });
|
||||
defer init_params.deinit();
|
||||
|
||||
try testing.expectString("2024-11-05", init_params.value.protocolVersion);
|
||||
try testing.expectString("test-client", init_params.value.clientInfo.name);
|
||||
try testing.expectString("1.0.0", init_params.value.clientInfo.version);
|
||||
}
|
||||
|
||||
test "protocol response formatting" {
|
||||
const response = Response{
|
||||
.id = .{ .integer = 42 },
|
||||
.result = .{ .string = "success" },
|
||||
};
|
||||
|
||||
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
|
||||
defer aw.deinit();
|
||||
try std.json.Stringify.value(response, .{ .emit_null_optional_fields = false }, &aw.writer);
|
||||
|
||||
try testing.expectString("{\"jsonrpc\":\"2.0\",\"id\":42,\"result\":\"success\"}", aw.written());
|
||||
}
|
||||
|
||||
test "protocol error formatting" {
|
||||
const response = Response{
|
||||
.id = .{ .string = "abc" },
|
||||
.@"error" = .{
|
||||
.code = -32601,
|
||||
.message = "Method not found",
|
||||
},
|
||||
};
|
||||
|
||||
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
|
||||
defer aw.deinit();
|
||||
try std.json.Stringify.value(response, .{ .emit_null_optional_fields = false }, &aw.writer);
|
||||
|
||||
try testing.expectString("{\"jsonrpc\":\"2.0\",\"id\":\"abc\",\"error\":{\"code\":-32601,\"message\":\"Method not found\"}}", aw.written());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user