using System.Text.Json; using System.Text.Json.Nodes; using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Generation; using GAJ3T.MusicGen.RiffGrooveBuilder.Generation; using GAJ3T.MusicGen.RiffGrooveBuilder.Generation.Profiles; using GAJ3T.MusicGen.RiffGrooveBuilder.Server.Protocol; using GAJ3T.MusicGen.RiffGrooveBuilder.Server.Tools; using GAJ3T.MusicGen.RiffGrooveBuilder.UnitTests.Generation; namespace GAJ3T.MusicGen.RiffGrooveBuilder.UnitTests.Protocol; public sealed class MusicGenToolsTests : IDisposable { private readonly string directory = Path.Combine(AppContext.BaseDirectory, "music-tools-" + Guid.NewGuid().ToString("N")); private JsonElement Arguments() => JsonSerializer.SerializeToElement(new { description = "palm-muted eighth-note lead", outputDirectory = directory, projectName = "song", formats = new[] { "mid" }, profiles = new { lead = GenerationServiceTests.Aglp } }); [Fact] public void FactoryRegistersExactlyTwoProductionToolsWithExplicitStrictSchemas() { var registry = MusicGenTools.CreateRegistry(); Assert.Equal(new[] { "generate_project", "list_instrument_profiles" }, registry.List() .Select(t => JsonSerializer.SerializeToElement(t).GetProperty("name").GetString())); Assert.Empty(new McpToolRegistry().List()); var descriptor = JsonSerializer.SerializeToElement(registry.Find("generate_project")!.Descriptor); var input = descriptor.GetProperty("inputSchema"); Assert.False(input.GetProperty("additionalProperties").GetBoolean()); Assert.Equal(new[] { "description", "outputDirectory", "projectName" }, input.GetProperty("required").EnumerateArray().Select(e => e.GetString())); var properties = input.GetProperty("properties"); Assert.Equal(1, properties.GetProperty("formats").GetProperty("minItems").GetInt32()); Assert.True(properties.GetProperty("formats").GetProperty("uniqueItems").GetBoolean()); Assert.Equal(new[] { "agp", "sdp", "mid" }, properties.GetProperty("formats").GetProperty("items").GetProperty("enum").EnumerateArray().Select(e => e.GetString())); Assert.True(properties.GetProperty("fallbackToMidi").GetProperty("default").GetBoolean()); Assert.False(properties.GetProperty("profiles").GetProperty("additionalProperties").GetBoolean()); SchemaAssert.Matches(input, Arguments()); var output = descriptor.GetProperty("outputSchema"); Assert.Equal(new[] { "complete", "fallback", "failed" }, output.GetProperty("properties").GetProperty("status").GetProperty("enum").EnumerateArray().Select(e => e.GetString())); Assert.True(output.TryGetProperty("allOf", out _)); } [Theory] [InlineData("{}")] [InlineData("""{"role":"lead"}""")] [InlineData("""{"role":"bass"}""")] [InlineData("""{"role":"drums"}""")] public async Task DiscoverySerializationMatchesAdvertisedOutputAndText(string json) { var tool = MusicGenTools.CreateRegistry().Find("list_instrument_profiles")!; var result = await tool.InvokeAsync(ProtocolTestConnection.Json(json), CancellationToken.None); AssertOutput(tool, result, false); var structured = JsonSerializer.SerializeToElement(result).GetProperty("structuredContent"); var filter = ProtocolTestConnection.Json(json); var role = filter.TryGetProperty("role", out var supplied) ? supplied.GetString() : null; Assert.Equal(role switch { "lead" => 4, "bass" => 1, "drums" => 0, _ => 5 }, structured.GetProperty("profiles").GetArrayLength()); if (role == "bass") { var bass = Assert.Single(structured.GetProperty("profiles").EnumerateArray()); Assert.Equal(GenerationServiceTests.Abj, bass.GetProperty("id").GetString()); Assert.Equal("sustain", Assert.Single(bass.GetProperty("articulations").EnumerateArray()) .GetProperty("articulation").GetString()); Assert.Equal(1, bass.GetProperty("channel").GetInt32()); } } [Theory] [InlineData("""{"role":null}""")] [InlineData("""{"role":0}""")] [InlineData("""{"role":"Lead"}""")] [InlineData("""{"role":"LEAD"}""")] [InlineData("""{"role":"other"}""")] [InlineData("""{"unknown":1}""")] [InlineData("""{"role":"lead","role":"bass"}""")] [InlineData("null")] [InlineData("[]")] public async Task DiscoveryRejectsValuesNotInItsInputSchema(string json) { var error = await Assert.ThrowsAsync(() => MusicGenTools.CreateRegistry() .Find("list_instrument_profiles")!.InvokeAsync(ProtocolTestConnection.Json(json), CancellationToken.None)); Assert.Equal(-32602, error.Code); } [Theory] [InlineData("description", null)] [InlineData("description", "null")] [InlineData("description", "1")] [InlineData("description", "\"\"")] [InlineData("outputDirectory", null)] [InlineData("outputDirectory", "null")] [InlineData("outputDirectory", "\"\"")] [InlineData("projectName", null)] [InlineData("projectName", "null")] [InlineData("projectName", "\"\"")] [InlineData("formats", "[]")] [InlineData("formats", "null")] [InlineData("formats", """["mid","mid"]""")] [InlineData("formats", """["Mid"]""")] [InlineData("formats", """["MID"]""")] [InlineData("formats", """["mp3"]""")] [InlineData("formats", "[0]")] [InlineData("profiles", "null")] [InlineData("profiles", "[]")] [InlineData("profiles", """{"lead":null}""")] [InlineData("profiles", """{"lead":""}""")] [InlineData("profiles", """{"lead":123}""")] [InlineData("profiles", """{"guitar":"x"}""")] [InlineData("profiles", """{"lead":"one","lead":"two"}""")] [InlineData("fallbackToMidi", "null")] [InlineData("fallbackToMidi", "\"true\"")] [InlineData("unknown", "true")] public async Task GenerationRejectsMissingNullUnknownEnumAndArrayContractViolations(string field, string? replacement) { var arguments = JsonNode.Parse(Arguments().GetRawText())!.AsObject(); if (replacement is null) arguments.Remove(field); else arguments[field] = JsonNode.Parse(replacement); var tool = MusicGenTools.CreateRegistry().Find("generate_project")!; var error = await Assert.ThrowsAsync(() => tool.InvokeAsync( ProtocolTestConnection.Json(arguments.ToJsonString()), CancellationToken.None)); Assert.Equal(-32602, error.Code); Assert.False(Directory.Exists(directory)); } [Fact] public async Task DuplicateRootPropertyAndExcessiveDescriptionAreRejected() { var tool = MusicGenTools.CreateRegistry().Find("generate_project")!; var duplicate = Arguments().GetRawText().Replace("\"description\":", "\"description\":\"lead\",\"description\":", StringComparison.Ordinal); Assert.Equal(-32602, (await Assert.ThrowsAsync(() => tool.InvokeAsync(ProtocolTestConnection.Json(duplicate), CancellationToken.None))).Code); var arguments = JsonNode.Parse(Arguments().GetRawText())!; arguments["description"] = new string('x', 16385); Assert.Equal(-32602, (await Assert.ThrowsAsync(() => tool.InvokeAsync( ProtocolTestConnection.Json(arguments.ToJsonString()), CancellationToken.None))).Code); } [Theory] [InlineData("complete")] [InlineData("fallback")] [InlineData("failed")] [InlineData("parse-failed")] public async Task EveryGenerationOutcomeMatchesItsSchemaAndContent(string status) { var arguments = JsonNode.Parse(Arguments().GetRawText())!; if (status == "fallback") arguments["formats"] = new JsonArray("agp", "mid"); if (status == "failed") arguments.AsObject().Remove("profiles"); if (status == "parse-failed") arguments["description"] = "unsupported request"; var tool = MusicGenTools.CreateRegistry().Find("generate_project")!; var response = await tool.InvokeAsync(ProtocolTestConnection.Json(arguments.ToJsonString()), CancellationToken.None); var structured = AssertOutput(tool, response, status is "failed" or "parse-failed"); Assert.Equal(status == "parse-failed" ? "failed" : status, structured.GetProperty("status").GetString()); if (status == "parse-failed") { Assert.Equal(JsonValueKind.Null, structured.GetProperty("timing").ValueKind); Assert.Equal(JsonValueKind.Null, structured.GetProperty("interpretedComposition").ValueKind); } else if (status == "complete") { var invalid = JsonNode.Parse(structured.GetRawText())!; invalid["timing"] = null; Assert.ThrowsAny(() => SchemaAssert.Matches( JsonSerializer.SerializeToElement(tool.Descriptor).GetProperty("outputSchema"), ProtocolTestConnection.Json(invalid.ToJsonString()))); } } [Theory] [InlineData("unknown")] [InlineData("numeric-enum")] [InlineData("missing-required")] [InlineData("zero-bytes")] public async Task OutputSchemaRejectsIncorrectSerializedShapes(string mutation) { var tool = MusicGenTools.CreateRegistry().Find("generate_project")!; var response = JsonSerializer.SerializeToElement(await tool.InvokeAsync(Arguments(), CancellationToken.None)); var invalid = JsonNode.Parse(response.GetProperty("structuredContent").GetRawText())!.AsObject(); switch (mutation) { case "unknown": invalid["extra"] = true; break; case "numeric-enum": invalid["status"] = 0; break; case "missing-required": invalid.Remove("timing"); break; case "zero-bytes": invalid["artifacts"]![0]!["byteCount"] = 0; break; } Assert.ThrowsAny(() => SchemaAssert.Matches( JsonSerializer.SerializeToElement(tool.Descriptor).GetProperty("outputSchema"), ProtocolTestConnection.Json(invalid.ToJsonString()))); } [Fact] public async Task OmittedFormatsAndFallbackUseDocumentedDefaultsButNoProfileDefault() { var arguments = JsonNode.Parse(Arguments().GetRawText())!; arguments.AsObject().Remove("formats"); var tool = MusicGenTools.CreateRegistry().Find("generate_project")!; var structured = AssertOutput(tool, await tool.InvokeAsync( ProtocolTestConnection.Json(arguments.ToJsonString()), CancellationToken.None), false); Assert.Equal("fallback", structured.GetProperty("status").GetString()); Assert.Equal(2, structured.GetProperty("targets").GetArrayLength()); Assert.Single(structured.GetProperty("artifacts").EnumerateArray()); } [Fact] public async Task PingAndCancellationStayResponsiveDuringSynchronousGenerationWork() { var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var cancelled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var service = new GenerationService(InstrumentProfileCatalog.LoadEmbedded(), token => { started.SetResult(); if (!token.WaitHandle.WaitOne(TimeSpan.FromSeconds(5))) throw new TimeoutException("Transport reader was blocked."); cancelled.SetResult(); token.ThrowIfCancellationRequested(); }); await using var connection = new ProtocolTestConnection(MusicGenTools.CreateRegistry(service)); await connection.ReadyAsync(); await connection.SendAsync(Call("generate")); await started.Task.WaitAsync(TimeSpan.FromSeconds(5)); await connection.SendAsync("""{"jsonrpc":"2.0","id":"ping","method":"ping"}"""); Assert.Equal("ping", (await connection.ReadAsync()).GetProperty("id").GetString()); await connection.SendAsync("""{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":"generate"}}"""); await cancelled.Task.WaitAsync(TimeSpan.FromSeconds(5)); await connection.SendAsync("""{"jsonrpc":"2.0","id":"after-cancel","method":"ping"}"""); Assert.Equal("after-cancel", (await connection.ReadAsync()).GetProperty("id").GetString()); await connection.AssertNoMoreResponsesAsync(); Assert.False(Directory.Exists(directory)); Assert.Empty(connection.Diagnostics.ToString()); } [Fact] public async Task UnexpectedGenerationFailureUsesExistingLoggedInternalErrorBoundary() { var service = new GenerationService(InstrumentProfileCatalog.LoadEmbedded(), _ => throw new InvalidOperationException("test unexpected generation detail")); await using var connection = new ProtocolTestConnection(MusicGenTools.CreateRegistry(service)); await connection.ReadyAsync(); await connection.SendAsync(Call("failure")); var response = await connection.ReadAsync(); Assert.Equal(-32603, response.GetProperty("error").GetProperty("code").GetInt32()); Assert.DoesNotContain("unexpected generation detail", response.GetRawText()); Assert.Contains("unexpected generation detail", connection.Diagnostics.ToString()); Assert.False(Directory.Exists(directory)); await connection.AssertNoMoreResponsesAsync(); } private static JsonElement AssertOutput(McpToolDefinition tool, object response, bool isError) { var root = JsonSerializer.SerializeToElement(response); Assert.Equal(isError, root.GetProperty("isError").GetBoolean()); var structured = root.GetProperty("structuredContent"); var text = Assert.Single(root.GetProperty("content").EnumerateArray()); Assert.Equal("text", text.GetProperty("type").GetString()); Assert.Equal(structured.GetRawText(), text.GetProperty("text").GetString()); SchemaAssert.Matches(JsonSerializer.SerializeToElement(tool.Descriptor).GetProperty("outputSchema"), structured); return structured; } private string Call(string id) => JsonSerializer.Serialize(new { jsonrpc = "2.0", id, method = "tools/call", @params = new { name = "generate_project", arguments = Arguments() } }); public void Dispose() { if (Directory.Exists(directory)) Directory.Delete(directory, true); } } // Covers the keywords emitted by the typed contracts, not a runtime schema engine. internal static class SchemaAssert { internal static void Matches(JsonElement schema, JsonElement value) { if (schema.TryGetProperty("type", out var type)) { var actual = value.ValueKind switch { JsonValueKind.Object => "object", JsonValueKind.Array => "array", JsonValueKind.String => "string", JsonValueKind.Number => value.TryGetInt64(out _) ? "integer" : "number", JsonValueKind.True or JsonValueKind.False => "boolean", _ => "null" }; var types = type.ValueKind == JsonValueKind.Array ? type.EnumerateArray().Select(t => t.GetString()).ToArray() : [type.GetString()]; Assert.True(types.Contains(actual) || actual == "integer" && types.Contains("number"), $"Expected {type}, got {value}."); } if (schema.TryGetProperty("enum", out var enums)) Assert.Contains(enums.EnumerateArray(), e => JsonElement.DeepEquals(e, value)); if (value.ValueKind == JsonValueKind.Number && schema.TryGetProperty("minimum", out var minimum)) Assert.True(value.GetDouble() >= minimum.GetDouble()); if (value.ValueKind == JsonValueKind.Object) { if (schema.TryGetProperty("required", out var required)) foreach (var field in required.EnumerateArray()) Assert.True(value.TryGetProperty(field.GetString()!, out _), field.GetString()); if (schema.TryGetProperty("properties", out var properties)) foreach (var property in value.EnumerateObject()) { if (properties.TryGetProperty(property.Name, out var definition)) Matches(definition, property.Value); else if (schema.TryGetProperty("additionalProperties", out var additional)) Assert.NotEqual(JsonValueKind.False, additional.ValueKind); } } if (value.ValueKind == JsonValueKind.Array) { if (schema.TryGetProperty("minItems", out var min)) Assert.True(value.GetArrayLength() >= min.GetInt32()); if (schema.TryGetProperty("maxItems", out var max)) Assert.True(value.GetArrayLength() <= max.GetInt32()); if (schema.TryGetProperty("items", out var items)) foreach (var item in value.EnumerateArray()) Matches(items, item); if (schema.TryGetProperty("uniqueItems", out var unique) && unique.GetBoolean()) Assert.Equal(value.GetArrayLength(), value.EnumerateArray().Select(e => e.GetRawText()).Distinct().Count()); } if (value.ValueKind == JsonValueKind.String) { if (schema.TryGetProperty("minLength", out var min)) Assert.True(value.GetString()!.Length >= min.GetInt32()); if (schema.TryGetProperty("maxLength", out var max)) Assert.True(value.GetString()!.Length <= max.GetInt32()); } if (schema.TryGetProperty("allOf", out var all)) foreach (var child in all.EnumerateArray()) Matches(child, value); if (schema.TryGetProperty("if", out var condition)) { var matches = true; try { Matches(condition, value); } catch (Xunit.Sdk.XunitException) { matches = false; } if (schema.TryGetProperty(matches ? "then" : "else", out var branch)) Matches(branch, value); } } }