142 lines
4.8 KiB
C#
142 lines
4.8 KiB
C#
using System.Net;
|
|
using System.Text;
|
|
using Heimdall.Agent.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
public class RemoteFrameBrokerServiceTests
|
|
{
|
|
private static (RemoteFrameBrokerService Service, AgentState State) CreateBroker(
|
|
HttpMessageHandler? handler = null)
|
|
{
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "test-key" };
|
|
var logger = Substitute.For<ILogger<RemoteFrameBrokerService>>();
|
|
|
|
IHttpClientFactory httpClientFactory;
|
|
if (handler is not null)
|
|
{
|
|
var client = new HttpClient(handler) { BaseAddress = new Uri("http://broker:8080/") };
|
|
httpClientFactory = Substitute.For<IHttpClientFactory>();
|
|
httpClientFactory.CreateClient("Broker").Returns(client);
|
|
}
|
|
else
|
|
{
|
|
httpClientFactory = Substitute.For<IHttpClientFactory>();
|
|
}
|
|
|
|
var service = new RemoteFrameBrokerService(httpClientFactory, state, logger);
|
|
return (service, state);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SubmitFrameAsync_UnregisteredAgent_ReturnsNull()
|
|
{
|
|
var state = new AgentState { AgentId = Guid.Empty, ApiKey = "" };
|
|
var logger = Substitute.For<ILogger<RemoteFrameBrokerService>>();
|
|
var httpClientFactory = Substitute.For<IHttpClientFactory>();
|
|
|
|
var service = new RemoteFrameBrokerService(httpClientFactory, state, logger);
|
|
|
|
var result = await service.SubmitFrameAsync(
|
|
Guid.NewGuid(), [1, 2, 3], DateTime.UtcNow);
|
|
|
|
result.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsBrokerReachable_DefaultsTrue()
|
|
{
|
|
var (service, _) = CreateBroker();
|
|
service.IsBrokerReachable.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Counters_InitiallyZero()
|
|
{
|
|
var (service, _) = CreateBroker();
|
|
service.SubmittedFrames.ShouldBe(0);
|
|
service.FailedSubmissions.ShouldBe(0);
|
|
service.BrokerUnreachableCount.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SubmitFrameAsync_BrokerReturns404_ReturnsNull()
|
|
{
|
|
var handler = new StubHttpMessageHandler(HttpStatusCode.NotFound);
|
|
var (service, _) = CreateBroker(handler);
|
|
|
|
var result = await service.SubmitFrameAsync(
|
|
Guid.NewGuid(), [1, 2, 3], DateTime.UtcNow);
|
|
|
|
// Health check returns 404 → broker marked unreachable → no POST made
|
|
result.ShouldBeNull();
|
|
service.IsBrokerReachable.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SubmitFrameAsync_BrokerUnreachable_SetsUnreachableFlag()
|
|
{
|
|
var handler = new StubHttpMessageHandler(new HttpRequestException("Connection refused"));
|
|
var (service, _) = CreateBroker(handler);
|
|
|
|
var result = await service.SubmitFrameAsync(
|
|
Guid.NewGuid(), [1, 2, 3], DateTime.UtcNow);
|
|
|
|
result.ShouldBeNull();
|
|
service.IsBrokerReachable.ShouldBeFalse();
|
|
service.BrokerUnreachableCount.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SubmitFrameAsync_BrokerReturnsDetections_MapsCorrectly()
|
|
{
|
|
var json = """{"detections":[{"label":"person","confidence":0.92,"x":0.1,"y":0.2,"width":0.3,"height":0.4}],"processingMs":42}""";
|
|
var handler = new StubHttpMessageHandler(HttpStatusCode.OK, json);
|
|
var (service, _) = CreateBroker(handler);
|
|
|
|
var result = await service.SubmitFrameAsync(
|
|
Guid.NewGuid(), [1, 2, 3], DateTime.UtcNow);
|
|
|
|
result.ShouldNotBeNull();
|
|
result.ProcessingMs.ShouldBe(42);
|
|
result.Detections.ShouldHaveSingleItem();
|
|
result.Detections[0].Label.ShouldBe("person");
|
|
result.Detections[0].Confidence.ShouldBe(0.92f);
|
|
service.SubmittedFrames.ShouldBe(1);
|
|
}
|
|
|
|
/// <summary>Minimal HTTP handler for testing without real network calls.</summary>
|
|
private sealed class StubHttpMessageHandler : HttpMessageHandler
|
|
{
|
|
private readonly HttpStatusCode _statusCode;
|
|
private readonly string? _responseBody;
|
|
private readonly Exception? _exception;
|
|
|
|
public StubHttpMessageHandler(HttpStatusCode statusCode, string? responseBody = null)
|
|
{
|
|
_statusCode = statusCode;
|
|
_responseBody = responseBody;
|
|
}
|
|
|
|
public StubHttpMessageHandler(Exception exception)
|
|
{
|
|
_exception = exception;
|
|
}
|
|
|
|
protected override Task<HttpResponseMessage> SendAsync(
|
|
HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
if (_exception is not null)
|
|
throw _exception;
|
|
|
|
var response = new HttpResponseMessage(_statusCode);
|
|
if (_responseBody is not null)
|
|
response.Content = new StringContent(_responseBody, Encoding.UTF8, "application/json");
|
|
return Task.FromResult(response);
|
|
}
|
|
}
|
|
}
|