85 lines
3.1 KiB
C#
85 lines
3.1 KiB
C#
using System.Text.Json;
|
|||
|
|
using NucleicProtocol.Interop;
|
||
|
|
|
||
|
|
namespace NucleicApp.Tests;
|
||
|
|
|
||
|
|
public sealed class ProtocolInteropTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public void EveryGoldenJsonFixtureHasTheFilenameDiscriminator()
|
||
|
|
{
|
||
|
|
var root = Path.Combine(TestRepository.Root, "fixtures", "protocol-abi", "v1");
|
||
|
|
var clients = Directory.GetFiles(Path.Combine(root, "client"), "*.json");
|
||
|
|
var hosts = Directory.GetFiles(Path.Combine(root, "host"), "*.json");
|
||
|
|
|
||
|
|
Assert.Equal(65, clients.Length);
|
||
|
|
Assert.Equal(52, hosts.Length);
|
||
|
|
foreach (var path in clients.Concat(hosts))
|
||
|
|
{
|
||
|
|
var expected = Path.GetFileNameWithoutExtension(path);
|
||
|
|
var message = ProtocolMessage.Parse(File.ReadAllText(path));
|
||
|
|
Assert.Equal(expected, message.Tag);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ManagedClientPinsCallbackContextUntilNativeCloseReturns()
|
||
|
|
{
|
||
|
|
var native = new FakeNativeProtocol();
|
||
|
|
var messages = new List<string>();
|
||
|
|
using (var client = new ProtocolClient(new ProtocolClientConfiguration
|
||
|
|
{
|
||
|
|
IdentityDirectory = @"C:\Users\me\AppData\Roaming\Nucleic-local\renderer",
|
||
|
|
Channel = "local",
|
||
|
|
}, native))
|
||
|
|
{
|
||
|
|
client.MessageJsonReceived += messages.Add;
|
||
|
|
native.EmitEvent("{\"t\":\"pong\"}");
|
||
|
|
var result = client.ConnectLocal(ValidRendezvous());
|
||
|
|
Assert.Equal(ProtocolResult.Ok, result);
|
||
|
|
Assert.Contains("\"hostID\":\"host-1\"", native.RendezvousJson);
|
||
|
|
}
|
||
|
|
|
||
|
|
Assert.True(native.Closed);
|
||
|
|
Assert.Single(messages, "{\"t\":\"pong\"}");
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ProjectionCopiesThenFreesNativeString()
|
||
|
|
{
|
||
|
|
var native = new FakeNativeProtocol
|
||
|
|
{
|
||
|
|
ProjectionResult = "{\"sessionID\":\"s1\",\"splices\":[],\"version\":1}",
|
||
|
|
};
|
||
|
|
using var projection = new ProtocolProjection(native);
|
||
|
|
var diff = projection.Apply("{\"t\":\"pong\"}");
|
||
|
|
Assert.Equal("s1", diff.Root.GetProperty("sessionID").GetString());
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void IntentHelpersMatchFrozenWireShapes()
|
||
|
|
{
|
||
|
|
AssertJsonEqual(TestRepository.Fixture("client", "listSessions"), ClientIntents.ListSessions());
|
||
|
|
AssertJsonEqual(TestRepository.Fixture("client", "unsubscribe"), ClientIntents.Unsubscribe("session-1"));
|
||
|
|
var expectedSend = "{\"input\":{\"parts\":[{\"text\":\"continue\",\"type\":\"text\"}]},\"sessionID\":\"session-1\",\"t\":\"sendInput\"}";
|
||
|
|
AssertJsonEqual(expectedSend, ClientIntents.SendInput("session-1", "continue"));
|
||
|
|
}
|
||
|
|
|
||
|
|
internal static HostdRendezvous ValidRendezvous() => new()
|
||
|
|
{
|
||
|
|
ProcessId = Environment.ProcessId,
|
||
|
|
Host = "127.0.0.1",
|
||
|
|
Port = 51900,
|
||
|
|
LocalPsk = Convert.ToBase64String(new byte[32]),
|
||
|
|
HostId = "host-1",
|
||
|
|
};
|
||
|
|
|
||
|
|
private static void AssertJsonEqual(string expected, string actual)
|
||
|
|
{
|
||
|
|
using var expectedDocument = JsonDocument.Parse(expected);
|
||
|
|
using var actualDocument = JsonDocument.Parse(actual);
|
||
|
|
Assert.True(JsonElement.DeepEquals(expectedDocument.RootElement, actualDocument.RootElement),
|
||
|
|
$"expected {expected}\nactual {actual}");
|
||
|
|
}
|
||
|
|
}
|