added a first pass at websocket IO
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using System.Text.Json;
|
||||
using Jibo.Cloud.Domain.Models;
|
||||
|
||||
namespace Jibo.Cloud.Tests.Fixtures;
|
||||
|
||||
internal static class WebSocketFixtureLoader
|
||||
{
|
||||
public static WebSocketFixture Load(string relativePath)
|
||||
{
|
||||
var fullPath = Path.Combine(AppContext.BaseDirectory, relativePath);
|
||||
using var document = JsonDocument.Parse(File.ReadAllText(fullPath));
|
||||
var root = document.RootElement;
|
||||
|
||||
var session = root.GetProperty("session");
|
||||
var steps = new List<WebSocketFixtureStep>();
|
||||
foreach (var stepElement in root.GetProperty("steps").EnumerateArray())
|
||||
{
|
||||
steps.Add(new WebSocketFixtureStep
|
||||
{
|
||||
Message = new WebSocketMessageEnvelope
|
||||
{
|
||||
HostName = session.GetProperty("hostName").GetString() ?? "neo-hub.jibo.com",
|
||||
Path = session.GetProperty("path").GetString() ?? "/listen",
|
||||
Kind = session.GetProperty("kind").GetString() ?? "neo-hub-listen",
|
||||
Token = session.GetProperty("token").GetString(),
|
||||
Text = stepElement.TryGetProperty("text", out var text) ? text.GetRawText() : null,
|
||||
Binary = stepElement.TryGetProperty("binary", out var binary) && binary.ValueKind == JsonValueKind.Array
|
||||
? binary.EnumerateArray().Select(item => (byte)item.GetInt32()).ToArray()
|
||||
: null
|
||||
},
|
||||
ExpectedReplyTypes = stepElement.GetProperty("expectedReplyTypes")
|
||||
.EnumerateArray()
|
||||
.Select(item => item.GetString() ?? string.Empty)
|
||||
.Where(item => !string.IsNullOrWhiteSpace(item))
|
||||
.ToArray()
|
||||
});
|
||||
}
|
||||
|
||||
return new WebSocketFixture
|
||||
{
|
||||
Name = root.TryGetProperty("name", out var name) ? name.GetString() ?? Path.GetFileNameWithoutExtension(relativePath) : Path.GetFileNameWithoutExtension(relativePath),
|
||||
Steps = steps
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class WebSocketFixture
|
||||
{
|
||||
public string Name { get; init; } = string.Empty;
|
||||
public IReadOnlyList<WebSocketFixtureStep> Steps { get; init; } = [];
|
||||
}
|
||||
|
||||
internal sealed class WebSocketFixtureStep
|
||||
{
|
||||
public WebSocketMessageEnvelope Message { get; init; } = new();
|
||||
public IReadOnlyList<string> ExpectedReplyTypes { get; init; } = [];
|
||||
}
|
||||
@@ -24,6 +24,10 @@
|
||||
<Link>fixtures\%(Filename)%(Extension)</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="..\..\src\Jibo.Cloud\node\fixtures\websocket\*.json">
|
||||
<Link>fixtures\%(Filename)%(Extension)</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -2,25 +2,27 @@ using System.Text.Json;
|
||||
using Jibo.Cloud.Application.Services;
|
||||
using Jibo.Cloud.Domain.Models;
|
||||
using Jibo.Cloud.Infrastructure.Persistence;
|
||||
using Jibo.Cloud.Tests.Fixtures;
|
||||
|
||||
namespace Jibo.Cloud.Tests.WebSockets;
|
||||
|
||||
public sealed class JiboWebSocketServiceTests
|
||||
{
|
||||
private readonly InMemoryCloudStateStore _store;
|
||||
private readonly JiboWebSocketService _service;
|
||||
|
||||
public JiboWebSocketServiceTests()
|
||||
{
|
||||
var store = new InMemoryCloudStateStore();
|
||||
_store = new InMemoryCloudStateStore();
|
||||
_service = new JiboWebSocketService(
|
||||
store,
|
||||
_store,
|
||||
new ProtocolToTurnContextMapper(),
|
||||
new DemoConversationBroker(),
|
||||
new ResponsePlanToSocketMessagesMapper());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListenMessage_ReturnsResponseAndEos()
|
||||
public async Task ListenMessage_ReturnsSyntheticListenEosAndSkillAction()
|
||||
{
|
||||
var replies = await _service.HandleMessageAsync(new WebSocketMessageEnvelope
|
||||
{
|
||||
@@ -28,12 +30,17 @@ public sealed class JiboWebSocketServiceTests
|
||||
Path = "/listen",
|
||||
Kind = "neo-hub-listen",
|
||||
Token = "hub-test-token",
|
||||
Text = """{"type":"LISTEN","data":{"text":"hello jibo"}}"""
|
||||
Text = """{"type":"LISTEN","transID":"trans-hello","data":{"text":"hello jibo","rules":["wake-word"]}}"""
|
||||
});
|
||||
|
||||
Assert.Equal(2, replies.Count);
|
||||
Assert.Contains("OPENJIBO_RESPONSE", replies[0].Text);
|
||||
Assert.Contains("EOS", replies[1].Text);
|
||||
Assert.Equal(3, replies.Count);
|
||||
Assert.Equal("LISTEN", ReadReplyType(replies[0]));
|
||||
Assert.Equal("EOS", ReadReplyType(replies[1]));
|
||||
Assert.Equal("SKILL_ACTION", ReadReplyType(replies[2]));
|
||||
|
||||
using var listenPayload = JsonDocument.Parse(replies[0].Text!);
|
||||
Assert.Equal("hello jibo", listenPayload.RootElement.GetProperty("data").GetProperty("asr").GetProperty("text").GetString());
|
||||
Assert.Equal("chat", listenPayload.RootElement.GetProperty("data").GetProperty("nlu").GetProperty("intent").GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -52,4 +59,69 @@ public sealed class JiboWebSocketServiceTests
|
||||
Assert.Equal("OPENJIBO_AUDIO_RECEIVED", payload.RootElement.GetProperty("type").GetString());
|
||||
Assert.Equal(4, payload.RootElement.GetProperty("data").GetProperty("bytes").GetInt32());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ContextThenClientNlu_UsesFollowUpTurnStateAndSkipsSkillAction()
|
||||
{
|
||||
await _service.HandleMessageAsync(new WebSocketMessageEnvelope
|
||||
{
|
||||
HostName = "neo-hub.jibo.com",
|
||||
Path = "/listen",
|
||||
Kind = "neo-hub-listen",
|
||||
Token = "hub-follow-up-token",
|
||||
Text = """{"type":"LISTEN","transID":"trans-follow-up","data":{"text":"hello jibo","rules":["wake-word"]}}"""
|
||||
});
|
||||
|
||||
var contextReplies = await _service.HandleMessageAsync(new WebSocketMessageEnvelope
|
||||
{
|
||||
HostName = "neo-hub.jibo.com",
|
||||
Path = "/listen",
|
||||
Kind = "neo-hub-listen",
|
||||
Token = "hub-follow-up-token",
|
||||
Text = """{"type":"CONTEXT","transID":"trans-follow-up","data":{"topic":"conversation","screen":"home"}}"""
|
||||
});
|
||||
|
||||
Assert.Single(contextReplies);
|
||||
Assert.Equal("OPENJIBO_CONTEXT_ACK", ReadReplyType(contextReplies[0]));
|
||||
|
||||
var nluReplies = await _service.HandleMessageAsync(new WebSocketMessageEnvelope
|
||||
{
|
||||
HostName = "neo-hub.jibo.com",
|
||||
Path = "/listen",
|
||||
Kind = "neo-hub-listen",
|
||||
Token = "hub-follow-up-token",
|
||||
Text = """{"type":"CLIENT_NLU","transID":"trans-follow-up","data":{"intent":"joke"}}"""
|
||||
});
|
||||
|
||||
Assert.Equal(2, nluReplies.Count);
|
||||
Assert.Equal("LISTEN", ReadReplyType(nluReplies[0]));
|
||||
Assert.Equal("EOS", ReadReplyType(nluReplies[1]));
|
||||
|
||||
var session = _store.FindSessionByToken("hub-follow-up-token");
|
||||
Assert.NotNull(session);
|
||||
Assert.True(session!.FollowUpOpen);
|
||||
Assert.Equal("joke", session.LastIntent);
|
||||
Assert.Equal("trans-follow-up", session.LastTransId);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("fixtures\\neo-hub-client-asr-joke.flow.json")]
|
||||
[InlineData("fixtures\\neo-hub-context-client-nlu.flow.json")]
|
||||
public async Task WebSocketFixture_ReplaysSuccessfully(string relativePath)
|
||||
{
|
||||
var fixture = WebSocketFixtureLoader.Load(relativePath);
|
||||
|
||||
foreach (var step in fixture.Steps)
|
||||
{
|
||||
var replies = await _service.HandleMessageAsync(step.Message);
|
||||
var actualTypes = replies.Select(ReadReplyType).ToArray();
|
||||
Assert.Equal(step.ExpectedReplyTypes, actualTypes);
|
||||
}
|
||||
}
|
||||
|
||||
private static string ReadReplyType(WebSocketReply reply)
|
||||
{
|
||||
using var payload = JsonDocument.Parse(reply.Text!);
|
||||
return payload.RootElement.GetProperty("type").GetString() ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user