Cubase-MCP-GAJ3T.MusicGen.R.../tests/GAJ3T.MusicGen.RiffGrooveBu.../Publishing/PublishedServerTests.cs

373 lines
19 KiB
C#

using System.Security.Cryptography;
using System.Text.Json;
using System.Text.Json.Nodes;
using GAJ3T.MusicGen.RiffGrooveBuilder.IntegrationTests.Stdio;
using Xunit.Abstractions;
using static GAJ3T.MusicGen.RiffGrooveBuilder.IntegrationTests.Stdio.McpGenerationTests;
namespace GAJ3T.MusicGen.RiffGrooveBuilder.IntegrationTests.Publishing;
public sealed class PublishedServerTests(PublishedPayload payload, ITestOutputHelper output)
: IClassFixture<PublishedPayload>
{
private const string Ame = "ample-ame-documented-defaults-2026-10-20";
private const string Aglp = "ample-aglp-documented-defaults-2026-10-20";
private const string Agm = "ample-agm-documented-defaults-2026-10-20";
private const string Agt = "ample-agt-documented-defaults-2026-10-20";
private const string Description = "4 bars at 120 BPM in E minor, 4/4: palm-muted eighth-note lead";
private JsonObject Arguments(string profile = Ame) => new()
{
["description"] = Description,
["outputDirectory"] = Path.Combine(payload.Root, "output-" + Guid.NewGuid().ToString("N")),
["projectName"] = "published-song",
["formats"] = new JsonArray("mid"),
["profiles"] = new JsonObject { ["lead"] = profile },
["fallbackToMidi"] = false
};
[Theory]
[InlineData(Ame)]
[InlineData(Aglp)]
[InlineData(Agm)]
[InlineData(Agt)]
public async Task IsolatedPublishedRpcDiscoversAndGeneratesReadableMidi(string profile)
{
await using var server = payload.Start();
await server.InitializeAsync();
await server.SendAsync("""{"jsonrpc":"2.0","id":10,"method":"tools/list"}""");
var tools = (await server.ReadAsync()).GetProperty("result").GetProperty("tools");
Assert.Equal(new[] { "generate_project", "list_instrument_profiles" },
tools.EnumerateArray().Select(t => t.GetProperty("name").GetString()));
var discovery = Structured(await server.CallToolAsync("list_instrument_profiles", new { }, 11), false);
Assert.Equal(7, discovery.GetProperty("instruments").GetArrayLength());
Assert.Equal(2, discovery.GetProperty("instruments").EnumerateArray()
.Count(i => i.GetProperty("availability").GetString() == "mapping_unavailable"));
Assert.Equal(new[] { Abj, Aglp, Agm, Agt, Ame }, discovery.GetProperty("profiles").EnumerateArray()
.Select(p => p.GetProperty("id").GetString()));
Assert.All(discovery.GetProperty("profiles").EnumerateArray(), p =>
Assert.Equal("documented", p.GetProperty("evidence").GetProperty("status").GetString()));
var bass = Assert.Single(discovery.GetProperty("profiles").EnumerateArray(),
p => p.GetProperty("id").GetString() == Abj);
Assert.Equal(1, bass.GetProperty("channel").GetInt32());
Assert.Equal("sustain", Assert.Single(bass.GetProperty("articulations").EnumerateArray())
.GetProperty("articulation").GetString());
var arguments = Arguments(profile);
var result = Structured(await server.CallToolAsync("generate_project", arguments, 12), false);
Assert.Equal("complete", result.GetProperty("status").GetString());
Assert.Equal(120, result.GetProperty("timing").GetProperty("leadInTicks").GetInt64());
Assert.Contains(result.GetProperty("warnings").EnumerateArray(), w =>
w.GetProperty("code").GetString() == "documented_profile" &&
w.GetProperty("message").GetString()!.Contains("NOT installed/application-verified"));
VerifyArtifact(result, arguments);
await PingAndFinish(server);
}
[Theory]
[InlineData("agp")]
[InlineData("agp+mid")]
[InlineData("omitted")]
public async Task PublishedNativeRequestsFallBackToExactlyOneActualMidi(string formats)
{
await using var server = payload.Start();
await server.InitializeAsync();
var arguments = Arguments();
arguments["fallbackToMidi"] = true;
if (formats == "omitted") arguments.Remove("formats");
else arguments["formats"] = new JsonArray(formats.Split('+').Select(f => (JsonNode?)JsonValue.Create(f)).ToArray());
var result = Structured(await server.CallToolAsync("generate_project", arguments), false);
Assert.Equal("fallback", result.GetProperty("status").GetString());
var native = Assert.Single(result.GetProperty("targets").EnumerateArray(),
t => t.GetProperty("requestedFormat").GetString() == "agp");
Assert.Equal("fallback", native.GetProperty("outcome").GetString());
Assert.Equal("native_writer_unavailable", native.GetProperty("reasonCode").GetString());
VerifyArtifact(result, arguments);
await PingAndFinish(server);
}
[Theory]
[InlineData("disabled", "native_fallback_disabled")]
[InlineData("bass", "no_default_profile")]
[InlineData("drums", "mapping_unavailable")]
[InlineData("abj-pm", "unsupported_articulation")]
[InlineData("unknown", "unknown_profile")]
[InlineData("wrong-role", "profile_role_mismatch")]
public async Task PublishedFailuresNeverDropRolesOrCreateOutput(string scenario, string code)
{
await using var server = payload.Start();
await server.InitializeAsync();
var arguments = Arguments();
if (scenario == "disabled") arguments["formats"] = new JsonArray("agp", "mid");
else if (scenario == "abj-pm")
{
arguments["description"] = "palm-muted bass";
arguments["profiles"] = new JsonObject { ["bass"] = Abj };
}
else if (scenario is "unknown" or "wrong-role")
arguments["profiles"]!["lead"] = scenario == "unknown" ? "unknown-profile" : Abj;
else arguments["description"] = "lead and " + scenario;
var result = Structured(await server.CallToolAsync("generate_project", arguments), true);
Assert.Equal("failed", result.GetProperty("status").GetString());
Assert.Empty(result.GetProperty("artifacts").EnumerateArray());
Assert.Contains(result.GetProperty("errors").EnumerateArray(), e => e.GetProperty("code").GetString() == code);
Assert.False(Directory.Exists(arguments["outputDirectory"]!.GetValue<string>()));
await PingAndFinish(server);
}
[Theory]
[InlineData(Ame, false)]
[InlineData(Ame, true)]
[InlineData(Aglp, false)]
[InlineData(Aglp, true)]
[InlineData(Agm, false)]
[InlineData(Agm, true)]
[InlineData(Agt, false)]
[InlineData(Agt, true)]
public async Task PublishedLeadBassUsesRefreshedProfilesAndOneReadableArtifact(string lead, bool native)
{
await using var server = payload.Start();
await server.InitializeAsync();
var arguments = Arguments(lead);
arguments["description"] = LeadBassDescription;
arguments["profiles"] = new JsonObject { ["lead"] = lead, ["bass"] = Abj };
arguments["formats"] = native ? new JsonArray("agp", "mid") : new JsonArray("mid");
arguments["fallbackToMidi"] = native;
var result = Structured(await server.CallToolAsync("generate_project", arguments), false);
var path = VerifyLeadBassArtifact(result, arguments, lead, native);
output.WriteLine($"Published Lead+Bass: {lead} + {Abj}; native={native}; bytes={new FileInfo(path).Length}; SHA256={Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(path)))}");
await PingAndFinish(server);
}
[Fact]
public void ExistingRetainedAmeExampleIsByteIdenticalAndStillReadable()
{
var path = Path.Combine(PublishedPayload.RepositoryRoot, "artifacts", "examples", "ame-lead-example.mid");
Assert.True(File.Exists(path), "Preserve the accepted retained AME example.");
Assert.Equal(371, new FileInfo(path).Length);
Assert.Equal("6F5791FB5C3A7A17E54AAF6436963E4D5723FB948057585060C09BF9B2AFCA3E",
Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(path))));
ReadBack(path);
output.WriteLine("Accepted 371-byte AME example preserved byte-for-byte and read back; no overwrite.");
}
[Fact]
public async Task PublishedCollisionPreservesActualMidiAndUnrelatedFile()
{
await using var server = payload.Start();
await server.InitializeAsync();
var arguments = Arguments();
var first = Structured(await server.CallToolAsync("generate_project", arguments), false);
var path = VerifyArtifact(first, arguments);
var original = File.ReadAllBytes(path);
var unrelated = Path.Combine(Path.GetDirectoryName(path)!, "owner.txt");
File.WriteAllText(unrelated, "preserve");
var second = Structured(await server.CallToolAsync("generate_project", arguments, 2), true);
Assert.Contains(second.GetProperty("errors").EnumerateArray(), e => e.GetProperty("code").GetString() == "output_exists");
Assert.Empty(second.GetProperty("artifacts").EnumerateArray());
Assert.Equal(original, File.ReadAllBytes(path));
Assert.Equal("preserve", File.ReadAllText(unrelated));
Assert.Equal(2, Directory.GetFiles(Path.GetDirectoryName(path)!).Length);
ReadBack(path);
await PingAndFinish(server);
}
[Fact]
public async Task PublishedMalformedInputRecoversAndEmptyEofExitsSilently()
{
await using (var empty = payload.Start())
{
Assert.Empty(await empty.FinishAsync());
empty.AssertNoMoreResponses();
}
await using var server = payload.Start();
await server.SendAsync("not-json");
Assert.Equal(-32700, (await server.ReadAsync()).GetProperty("error").GetProperty("code").GetInt32());
await server.InitializeAsync();
Assert.Equal(-32602, (await server.CallToolAsync("generate_project", new { })).GetProperty("error").GetProperty("code").GetInt32());
await PingAndFinish(server);
}
[Fact]
public async Task PublishedCancellationAndEofLeaveNoOutput()
{
await using var server = payload.Start();
await server.InitializeAsync();
var arguments = Arguments();
arguments["description"] = "100000 bars sustained lead";
var request = JsonSerializer.Serialize(new
{
jsonrpc = "2.0", id = "cancel", method = "tools/call",
@params = new { name = "generate_project", arguments }
});
await server.SendAsync(request + "\n" +
"""{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":"cancel"}}""" + "\n" +
"""{"jsonrpc":"2.0","id":"ping","method":"ping"}""");
Assert.Equal("ping", (await server.ReadAsync()).GetProperty("id").GetString());
Assert.Empty(await server.FinishAsync());
server.AssertNoMoreResponses();
Assert.False(Directory.Exists(arguments["outputDirectory"]!.GetValue<string>()));
}
[Fact]
public async Task PublishedEofCancelsActiveGenerationWithoutPublication()
{
await using var server = payload.Start();
await server.InitializeAsync();
var arguments = Arguments();
arguments["description"] = "100000 bars sustained lead";
await server.SendAsync(JsonSerializer.Serialize(new
{
jsonrpc = "2.0", id = "eof", method = "tools/call",
@params = new { name = "generate_project", arguments }
}));
Assert.Empty(await server.FinishAsync());
server.AssertNoMoreResponses();
Assert.False(Directory.Exists(arguments["outputDirectory"]!.GetValue<string>()));
}
[Fact]
public async Task PublishedRpcGeneratesReadBackExample()
{
await using var server = payload.Start();
await server.InitializeAsync();
var arguments = Arguments();
var retain = Environment.GetEnvironmentVariable("RIFFGROOVE_RETAIN_EXAMPLE") == "1";
if (retain)
{
var examples = Path.Combine(PublishedPayload.RepositoryRoot, "artifacts", "examples");
var stem = "ame-lead-example";
if (File.Exists(Path.Combine(examples, stem + ".mid")) || Directory.Exists(Path.Combine(examples, stem + ".mid")))
stem += "-" + Guid.NewGuid().ToString("N");
arguments["outputDirectory"] = examples;
arguments["projectName"] = stem;
}
var result = Structured(await server.CallToolAsync("generate_project", arguments), false);
Assert.Equal("complete", result.GetProperty("status").GetString());
Assert.Contains(result.GetProperty("warnings").EnumerateArray(),
w => w.GetProperty("code").GetString() == "documented_profile");
VerifyArtifact(result, arguments, exclusiveDirectory: !retain);
await PingAndFinish(server);
}
[Fact]
public async Task ExecutableAloneGeneratesMidiAndExtractsBundledNativeAssets()
{
var directory = Path.Combine(payload.Root, "executable-only");
Directory.CreateDirectory(directory);
var executable = Path.Combine(directory, McpProcess.ServerName + ".exe");
File.Copy(payload.Executable, executable);
var extraction = Path.Combine(payload.Root, "exe-only-extraction");
var trace = Path.Combine(payload.Root, "host-trace.txt");
await using var server = new McpProcess(true, executable, new Dictionary<string, string>
{
["DOTNET_BUNDLE_EXTRACT_BASE_DIR"] = extraction,
["DOTNET_HOST_TRACE"] = "1",
["DOTNET_HOST_TRACEFILE"] = trace
});
await server.InitializeAsync();
var arguments = Arguments();
VerifyArtifact(Structured(await server.CallToolAsync("generate_project", arguments), false), arguments);
await PingAndFinish(server);
Assert.Equal(new[] { executable }, Directory.GetFiles(directory));
var extracted = Directory.GetFiles(extraction, "*", SearchOption.AllDirectories);
foreach (var name in new[] { "Melanchall_DryWetMidi_Native32.dll", "Melanchall_DryWetMidi_Native64.dll" })
{
var file = Assert.Single(extracted, p => Path.GetFileName(p) == name);
output.WriteLine($"Extracted {name}: {new FileInfo(file).Length} bytes");
}
Assert.DoesNotContain(extracted, p => Path.GetFileName(p) == "Melanchall.DryWetMidi.dll");
Assert.Equal(2, extracted.Length);
Assert.Equal("080346C638E219EC7CD4748E370E5E11931ED3A228C17F76D84C4D10BA9C9E6F",
Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(Assert.Single(extracted,
p => Path.GetFileName(p) == "Melanchall_DryWetMidi_Native32.dll")))));
Assert.Equal("80CA89AF931E43642A125EC82E253D9BD7002088D000DABA6E0661CDAD89F5AA",
Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(Assert.Single(extracted,
p => Path.GetFileName(p) == "Melanchall_DryWetMidi_Native64.dll")))));
var hostTrace = File.ReadAllText(trace);
foreach (var line in hostTrace.Split('\n').Where(l =>
l.Contains("coreclr", StringComparison.OrdinalIgnoreCase) ||
l.Contains("hostpolicy", StringComparison.OrdinalIgnoreCase) ||
l.Contains("self-contained", StringComparison.OrdinalIgnoreCase) ||
l.Contains("extraction", StringComparison.OrdinalIgnoreCase)).Take(15))
output.WriteLine(line.TrimEnd());
output.WriteLine($"Extracted files: {string.Join(", ", extracted.Select(Path.GetFileName))}");
Assert.Contains("Executing as a self-contained app", hostTrace);
Assert.Contains("Using internal hostpolicy", hostTrace);
Assert.Contains("CoreCLR path = '', CoreCLR dir = ''", hostTrace);
output.WriteLine("Executable-only MIDI generation needs no loose managed DLL/catalog or optional sidecars. Runtime-free host acceptance remains pending.");
}
private string VerifyArtifact(JsonElement result, JsonObject arguments, bool exclusiveDirectory = true)
{
var artifact = Assert.Single(result.GetProperty("artifacts").EnumerateArray());
var path = artifact.GetProperty("absolutePath").GetString()!;
var directory = arguments["outputDirectory"]!.GetValue<string>();
Assert.Equal(Path.Combine(directory, arguments["projectName"]!.GetValue<string>() + ".mid"), path);
Assert.Equal("mid", artifact.GetProperty("format").GetString());
Assert.Equal(371, new FileInfo(path).Length);
Assert.Equal(new FileInfo(path).Length, artifact.GetProperty("byteCount").GetInt64());
if (exclusiveDirectory) Assert.Equal(new[] { path }, Directory.GetFiles(directory));
ReadBack(path);
output.WriteLine($"Published RPC: {path}; bytes={new FileInfo(path).Length}; SHA256={Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(path)))}");
output.WriteLine("DryWetMIDI 8.0.3: type1/960 PPQ, Conductor/Lead, 120 BPM/4/4, 32 notes64/96, switch26/100 on0/off30, lead-in120, end15480.");
return path;
}
private async Task PingAndFinish(McpProcess server)
{
await server.SendAsync("""{"jsonrpc":"2.0","id":"ready","method":"ping"}""");
Assert.Equal("ready", (await server.ReadAsync()).GetProperty("id").GetString());
Assert.Empty(await server.FinishAsync());
server.AssertNoMoreResponses();
output.WriteLine("Initialized/responded to RPC and ping; protocol-only stdout; empty stderr; EOF exit0.");
}
}
public sealed class PublishedPayload : IDisposable
{
public static string RepositoryRoot
{
get
{
var directory = new DirectoryInfo(AppContext.BaseDirectory);
while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "global.json")))
directory = directory.Parent;
return directory?.FullName ?? throw new InvalidOperationException("Cannot locate repository root.");
}
}
public string Root { get; } = Path.Combine(RepositoryRoot, "artifacts", "checks", "published-" + Guid.NewGuid().ToString("N"));
public string Executable { get; }
public PublishedPayload()
{
var source = Environment.GetEnvironmentVariable("RIFFGROOVE_PUBLISHED_SERVER") ??
Path.Combine(RepositoryRoot, "artifacts", "publish", "win-x64", McpProcess.ServerName + ".exe");
Assert.True(Path.IsPathFullyQualified(source));
Assert.True(File.Exists(source), $"Publish win-x64 first; published executable missing: {source}");
var sourceDirectory = Path.GetDirectoryName(source)!;
var files = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories);
Assert.DoesNotContain(files, f => Path.GetExtension(f) is ".dll" or ".json");
var isolated = Path.Combine(Root, "payload");
Directory.CreateDirectory(isolated);
foreach (var file in files)
{
var destination = Path.Combine(isolated, Path.GetRelativePath(sourceDirectory, file));
Directory.CreateDirectory(Path.GetDirectoryName(destination)!);
File.Copy(file, destination, overwrite: false);
}
Executable = Path.Combine(isolated, Path.GetFileName(source));
}
internal McpProcess Start() => new(independentWorkingDirectory: true, executablePath: Executable,
environment: new Dictionary<string, string>
{
["DOTNET_BUNDLE_EXTRACT_BASE_DIR"] = Path.Combine(Root, "extraction")
});
public void Dispose()
{
if (Directory.Exists(Root)) Directory.Delete(Root, recursive: true);
}
}