147 lines
7.4 KiB
C#
147 lines
7.4 KiB
C#
using System.Text.Json;
|
|
|
|
namespace GAJ3T.MusicGen.RiffGrooveBuilder.IntegrationTests.Stdio;
|
|
|
|
public sealed class McpProtocolTests
|
|
{
|
|
[Theory]
|
|
[InlineData(false, "\n")]
|
|
[InlineData(false, "\r\n")]
|
|
[InlineData(true, "\n")]
|
|
[InlineData(true, "\r\n")]
|
|
public async Task RealApphostNegotiatesListsProductionToolsPingsAndExitsCleanly(bool independentWorkingDirectory, string delimiter)
|
|
{
|
|
await using var server = new McpProcess(independentWorkingDirectory);
|
|
await server.SendAsync("""
|
|
{"jsonrpc":"2.0","id":"init-001","method":"initialize",
|
|
"params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"音楽 🎸","version":"1"}}}
|
|
""".ReplaceLineEndings(""), delimiter);
|
|
var initialize = await server.ReadAsync();
|
|
Assert.Equal("init-001", initialize.GetProperty("id").GetString());
|
|
var result = initialize.GetProperty("result");
|
|
Assert.Equal("2025-11-25", result.GetProperty("protocolVersion").GetString());
|
|
Assert.Equal("GAJ3T.MusicGen.RiffGrooveBuilder", result.GetProperty("serverInfo").GetProperty("name").GetString());
|
|
Assert.False(string.IsNullOrWhiteSpace(result.GetProperty("serverInfo").GetProperty("version").GetString()));
|
|
var capability = Assert.Single(result.GetProperty("capabilities").EnumerateObject());
|
|
Assert.Equal("tools", capability.Name);
|
|
Assert.False(capability.Value.GetProperty("listChanged").GetBoolean());
|
|
|
|
await server.SendAsync("""{"jsonrpc":"2.0","method":"notifications/initialized"}""", delimiter);
|
|
await server.SendAsync("""{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":"unknown","reason":"too late"}}""", delimiter);
|
|
await server.SendAsync("""{"jsonrpc":"2.0","method":"unsupported/notification"}""", delimiter);
|
|
await server.SendAsync("""{"jsonrpc":"2.0","id":2,"method":"tools/list"}""", delimiter);
|
|
var listing = await server.ReadAsync();
|
|
Assert.Equal(2, listing.GetProperty("id").GetInt32());
|
|
Assert.Equal(new[] { "generate_project", "list_instrument_profiles" },
|
|
listing.GetProperty("result").GetProperty("tools").EnumerateArray().Select(t => t.GetProperty("name").GetString()));
|
|
Assert.False(listing.GetProperty("result").TryGetProperty("nextCursor", out _));
|
|
await server.SendAsync("""{"jsonrpc":"2.0","id":"音楽\n🎸","method":"ping","params":{}}""", delimiter);
|
|
var ping = await server.ReadAsync();
|
|
Assert.Equal("音楽\n🎸", ping.GetProperty("id").GetString());
|
|
Assert.Equal("{}", ping.GetProperty("result").GetRawText());
|
|
Assert.Equal("Ignored unsupported notification.", (await server.FinishAsync()).Trim());
|
|
server.AssertNoMoreResponses();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RealApphostRecoversFromErrorsAndNegotiatesUnsupportedVersion()
|
|
{
|
|
await using var server = new McpProcess(independentWorkingDirectory: true);
|
|
await server.SendAsync("not-json");
|
|
var malformed = await server.ReadAsync();
|
|
Assert.Equal(JsonValueKind.Null, malformed.GetProperty("id").ValueKind);
|
|
Assert.Equal(-32700, ErrorCode(malformed));
|
|
await server.SendAsync("""{"jsonrpc":"2.0","id":0,"method":"tools/list"}""");
|
|
Assert.Equal(-32002, ErrorCode(await server.ReadAsync()));
|
|
await server.SendAsync("""
|
|
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"future",
|
|
"capabilities":{},"clientInfo":{"name":"test","version":"1"}}}
|
|
""".ReplaceLineEndings(""));
|
|
Assert.Equal("2025-11-25", (await server.ReadAsync()).GetProperty("result").GetProperty("protocolVersion").GetString());
|
|
await server.SendAsync("""{"jsonrpc":"2.0","method":"notifications/initialized"}""");
|
|
|
|
var failures = new[]
|
|
{
|
|
("""{"jsonrpc":"2.0","id":"method","method":"unknown"}""", -32601, "method"),
|
|
("""{"jsonrpc":"2.0","id":"tool","method":"tools/call","params":{"name":"generate_project","arguments":{}}}""", -32602, "tool"),
|
|
("""{"jsonrpc":"2.0","id":"args","method":"tools/call","params":{"name":"x","arguments":[]}}""", -32602, "args"),
|
|
("""{"jsonrpc":"2.0","id":"params","method":"ping","params":{"invalid":true}}""", -32602, "params"),
|
|
("""{"jsonrpc":"2.0","id":"repeat","method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"x","version":"1"}}}""", -32002, "repeat"),
|
|
("""[{"jsonrpc":"2.0","id":10,"method":"ping"}]""", -32600, (string?)null)
|
|
};
|
|
foreach (var (request, code, id) in failures)
|
|
{
|
|
await server.SendAsync(request);
|
|
var response = await server.ReadAsync();
|
|
Assert.Equal(id, response.GetProperty("id").GetString());
|
|
Assert.Equal(code, ErrorCode(response));
|
|
}
|
|
|
|
await server.SendAsync("""{"jsonrpc":"2.0","id":9223372036854775807,"method":"ping"}""");
|
|
Assert.Equal(long.MaxValue, (await server.ReadAsync()).GetProperty("id").GetInt64());
|
|
Assert.Empty(await server.FinishAsync());
|
|
server.AssertNoMoreResponses();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public async Task RealApphostRejectsBoundedFramingErrorsAndResynchronizes(bool invalidUtf8)
|
|
{
|
|
await using var server = new McpProcess();
|
|
if (invalidUtf8)
|
|
{
|
|
await server.SendBytesAsync([0xff, 0xc0, 0x80, (byte)'\n']);
|
|
}
|
|
else
|
|
{
|
|
await server.SendAsync(new string('x', 1024 * 1024 + 1));
|
|
}
|
|
|
|
var error = await server.ReadAsync();
|
|
Assert.Equal(invalidUtf8 ? -32700 : -32600, ErrorCode(error));
|
|
Assert.Equal(JsonValueKind.Null, error.GetProperty("id").ValueKind);
|
|
await server.SendAsync("""{"jsonrpc":"2.0","id":1,"method":"ping"}""");
|
|
Assert.Equal(1, (await server.ReadAsync()).GetProperty("id").GetInt32());
|
|
var diagnostics = await server.FinishAsync();
|
|
Assert.Contains(invalidUtf8 ? "UTF-8" : "byte limit", diagnostics);
|
|
Assert.True(diagnostics.Length < 200);
|
|
server.AssertNoMoreResponses();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RealApphostAcceptsExactlyOneMiBWithCrLf()
|
|
{
|
|
const string request = """{"jsonrpc":"2.0","id":1,"method":"ping"}""";
|
|
await using var server = new McpProcess();
|
|
await server.SendAsync(request.PadRight(1024 * 1024), "\r\n");
|
|
Assert.Equal(1, (await server.ReadAsync()).GetProperty("id").GetInt32());
|
|
Assert.Empty(await server.FinishAsync());
|
|
server.AssertNoMoreResponses();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("""{"jsonrpc":"2.0","id":1,"method":"ping"}""", true)]
|
|
[InlineData("invalid-json", false)]
|
|
public async Task RealApphostHandlesFinalNonNewlineFrameAtEof(string input, bool valid)
|
|
{
|
|
await using var server = new McpProcess();
|
|
await server.SendAsync(input, delimiter: "");
|
|
Assert.Empty(await server.FinishAsync());
|
|
var response = await server.ReadAsync();
|
|
if (valid)
|
|
{
|
|
Assert.Equal(1, response.GetProperty("id").GetInt32());
|
|
Assert.True(response.TryGetProperty("result", out _));
|
|
}
|
|
else
|
|
{
|
|
Assert.Equal(-32700, ErrorCode(response));
|
|
}
|
|
|
|
server.AssertNoMoreResponses();
|
|
}
|
|
|
|
private static int ErrorCode(JsonElement response) => response.GetProperty("error").GetProperty("code").GetInt32();
|
|
}
|