fixes for testing Jibo
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using Jibo.Cloud.Domain.Models;
|
||||
using Jibo.Cloud.Infrastructure.Telemetry;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Jibo.Cloud.Tests.Protocol;
|
||||
|
||||
public sealed class FileProtocolTelemetrySinkTests : IDisposable
|
||||
{
|
||||
private readonly string _workspaceRoot;
|
||||
private readonly string _repoRoot;
|
||||
private readonly string _appBaseDirectory;
|
||||
|
||||
public FileProtocolTelemetrySinkTests()
|
||||
{
|
||||
_workspaceRoot = Path.Combine(Path.GetTempPath(), "OpenJibo.ProtocolTelemetry.Tests", Guid.NewGuid().ToString("N"));
|
||||
_repoRoot = Path.Combine(_workspaceRoot, "OpenJibo");
|
||||
_appBaseDirectory = Path.Combine(_repoRoot, "src", "Jibo.Cloud", "dotnet", "src", "Jibo.Cloud.Api", "bin", "Debug", "net10.0");
|
||||
|
||||
Directory.CreateDirectory(_repoRoot);
|
||||
Directory.CreateDirectory(_appBaseDirectory);
|
||||
File.WriteAllText(Path.Combine(_repoRoot, "OpenJibo.slnx"), string.Empty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RecordAsync_ResolvesRelativePathAgainstOpenJiboRepoRoot()
|
||||
{
|
||||
var captureDirectory = CapturePathResolver.Resolve("captures/http", _repoRoot, _appBaseDirectory);
|
||||
var sink = new FileProtocolTelemetrySink(
|
||||
NullLogger<FileProtocolTelemetrySink>.Instance,
|
||||
Options.Create(new ProtocolTelemetryOptions
|
||||
{
|
||||
Enabled = true,
|
||||
DirectoryPath = captureDirectory
|
||||
}));
|
||||
|
||||
var envelope = new ProtocolEnvelope
|
||||
{
|
||||
HostName = "api.jibo.com",
|
||||
Method = "POST",
|
||||
Path = "/",
|
||||
ServicePrefix = "Notification_20150505",
|
||||
Operation = "NewRobotToken",
|
||||
BodyText = """{"deviceId":"robot-123"}"""
|
||||
};
|
||||
|
||||
await sink.RecordAsync(envelope, ProtocolDispatchResult.Ok(new { token = "token-robot-123" }));
|
||||
|
||||
var captureFile = Directory.GetFiles(captureDirectory, "*.events.ndjson").Single();
|
||||
var contents = await File.ReadAllTextAsync(captureFile);
|
||||
|
||||
Assert.Contains("Notification_20150505", contents);
|
||||
Assert.DoesNotContain(Path.Combine("bin", "Debug"), captureFile, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Directory.Exists(_workspaceRoot))
|
||||
{
|
||||
Directory.Delete(_workspaceRoot, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public sealed class JiboCloudProtocolServiceTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NewRobotToken_UsesBodyDeviceId()
|
||||
public async Task NewRobotToken_UsesBodyDeviceId_WhenHeaderDeviceIdIsEmpty()
|
||||
{
|
||||
var result = await _service.DispatchAsync(new ProtocolEnvelope
|
||||
{
|
||||
@@ -36,6 +36,7 @@ public sealed class JiboCloudProtocolServiceTests
|
||||
Method = "POST",
|
||||
ServicePrefix = "Notification_20160715",
|
||||
Operation = "NewRobotToken",
|
||||
DeviceId = string.Empty,
|
||||
BodyText = """{"deviceId":"robot-123"}"""
|
||||
});
|
||||
|
||||
|
||||
@@ -9,10 +9,14 @@ namespace Jibo.Cloud.Tests.WebSockets;
|
||||
public sealed class FileWebSocketTelemetrySinkTests : IDisposable
|
||||
{
|
||||
private readonly string _directoryPath;
|
||||
private readonly string _repoRoot;
|
||||
private readonly string _appBaseDirectory;
|
||||
|
||||
public FileWebSocketTelemetrySinkTests()
|
||||
{
|
||||
_directoryPath = Path.Combine(Path.GetTempPath(), "OpenJibo.Tests", Guid.NewGuid().ToString("N"));
|
||||
_repoRoot = Path.Combine(_directoryPath, "OpenJibo");
|
||||
_appBaseDirectory = Path.Combine(_repoRoot, "src", "Jibo.Cloud", "dotnet", "src", "Jibo.Cloud.Api", "bin", "Debug", "net10.0");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -61,6 +65,48 @@ public sealed class FileWebSocketTelemetrySinkTests : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RecordsFixtureUsingRepoRootForRelativePaths()
|
||||
{
|
||||
Directory.CreateDirectory(_repoRoot);
|
||||
Directory.CreateDirectory(_appBaseDirectory);
|
||||
File.WriteAllText(Path.Combine(_repoRoot, "OpenJibo.slnx"), string.Empty);
|
||||
var captureDirectory = CapturePathResolver.Resolve("captures/websocket", _repoRoot, _appBaseDirectory);
|
||||
|
||||
var sink = new FileWebSocketTelemetrySink(
|
||||
NullLogger<FileWebSocketTelemetrySink>.Instance,
|
||||
Options.Create(new WebSocketTelemetryOptions
|
||||
{
|
||||
Enabled = true,
|
||||
ExportFixtures = true,
|
||||
DirectoryPath = captureDirectory
|
||||
}));
|
||||
|
||||
var envelope = new WebSocketMessageEnvelope
|
||||
{
|
||||
ConnectionId = "conn-relative",
|
||||
HostName = "neo-hub.jibo.com",
|
||||
Path = "/listen",
|
||||
Kind = "neo-hub-listen",
|
||||
Token = "token-relative",
|
||||
Text = """{"type":"LISTEN","transID":"trans-relative","data":{"text":"hello"}}"""
|
||||
};
|
||||
var session = new CloudSession
|
||||
{
|
||||
Token = "token-relative",
|
||||
HostName = "neo-hub.jibo.com",
|
||||
Path = "/listen"
|
||||
};
|
||||
session.TurnState.TransId = "trans-relative";
|
||||
|
||||
await sink.RecordConnectionOpenedAsync(envelope, session);
|
||||
await sink.RecordOutboundAsync(envelope, session, [new WebSocketReply { Text = """{"type":"LISTEN"}""" }]);
|
||||
await sink.RecordConnectionClosedAsync(envelope, session, "test");
|
||||
|
||||
var fixtureDirectory = Path.Combine(captureDirectory, "fixtures");
|
||||
Assert.Single(Directory.GetFiles(fixtureDirectory, "*.flow.json"));
|
||||
}
|
||||
|
||||
private FileWebSocketTelemetrySink CreateSink()
|
||||
{
|
||||
return new FileWebSocketTelemetrySink(
|
||||
|
||||
Reference in New Issue
Block a user