2026-07-23 15:37:20 +00:00
|
|
|
using Heimdall.Agent.Services;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
|
|
|
|
|
|
public class FrameAnalyzerServiceTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_WhenModelNotFound_DoesNotThrow()
|
|
|
|
|
{
|
|
|
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
|
|
|
service.ShouldNotBeNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task AnalyzeFrameAsync_WhenPassthrough_ReturnsNull()
|
|
|
|
|
{
|
|
|
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
|
|
|
var result = await service.AnalyzeFrameAsync([1, 2, 3]);
|
|
|
|
|
result.ShouldBeNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AnalyzeFrame_WhenPassthrough_ReturnsNull()
|
|
|
|
|
{
|
|
|
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
|
|
|
var result = service.AnalyzeFrame([1, 2, 3]);
|
|
|
|
|
result.ShouldBeNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task AnalyzeFrameAsync_EmptyArray_ReturnsNull()
|
|
|
|
|
{
|
|
|
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
|
|
|
var result = await service.AnalyzeFrameAsync([]);
|
|
|
|
|
result.ShouldBeNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task AnalyzeFrameAsync_LargeBuffer_ReturnsNull()
|
|
|
|
|
{
|
|
|
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
|
|
|
var result = await service.AnalyzeFrameAsync(new byte[1024 * 1024]);
|
|
|
|
|
result.ShouldBeNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Dispose_MultipleTimes_DoesNotThrow()
|
|
|
|
|
{
|
|
|
|
|
var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
|
|
|
service.Dispose();
|
|
|
|
|
service.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task AnalyzeFrameAsync_AfterDispose_ReturnsNull()
|
|
|
|
|
{
|
|
|
|
|
var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
|
|
|
service.Dispose();
|
|
|
|
|
var result = await service.AnalyzeFrameAsync([1, 2, 3]);
|
|
|
|
|
result.ShouldBeNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task AnalyzeFrameAsync_ConcurrentCalls_DoNotThrow()
|
|
|
|
|
{
|
|
|
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
|
|
|
var tasks = Enumerable.Range(0, 10)
|
|
|
|
|
.Select(_ => service.AnalyzeFrameAsync([1, 2, 3]))
|
|
|
|
|
.ToArray();
|
|
|
|
|
var results = await Task.WhenAll(tasks);
|
|
|
|
|
results.ShouldAllBe(r => r == null);
|
|
|
|
|
}
|
2026-08-19 23:06:06 +02:00
|
|
|
|
|
|
|
|
// ── NMS unit tests ──────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ApplyNms_EmptyList_ReturnsEmpty()
|
|
|
|
|
{
|
|
|
|
|
var result = FrameAnalyzerService.ApplyNms([]);
|
|
|
|
|
result.ShouldBeEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ApplyNms_SingleDetection_ReturnsItUnchanged()
|
|
|
|
|
{
|
|
|
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
|
|
|
{
|
|
|
|
|
new(10, 10, 50, 50, 0.9f, 0)
|
|
|
|
|
};
|
|
|
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
|
|
|
result.Length.ShouldBe(1);
|
|
|
|
|
result[0].Label.ShouldBe("person");
|
|
|
|
|
result[0].Confidence.ShouldBe(0.9f, 0.001f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ApplyNms_TwoOverlapping_SameClass_KeepsHigherConfidence()
|
|
|
|
|
{
|
|
|
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
|
|
|
{
|
|
|
|
|
new(10, 10, 50, 50, 0.9f, 0), // person, high confidence
|
|
|
|
|
new(12, 12, 52, 52, 0.7f, 0), // person, overlapping, lower
|
|
|
|
|
};
|
|
|
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
|
|
|
result.Length.ShouldBe(1);
|
|
|
|
|
result[0].Confidence.ShouldBe(0.9f, 0.001f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ApplyNms_TwoOverlapping_DifferentClass_KeepsBoth()
|
|
|
|
|
{
|
|
|
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
|
|
|
{
|
|
|
|
|
new(10, 10, 50, 50, 0.9f, 0), // person
|
|
|
|
|
new(12, 12, 52, 52, 0.8f, 2), // car, overlapping but different class
|
|
|
|
|
};
|
|
|
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
|
|
|
result.Length.ShouldBe(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ApplyNms_TwoNonOverlapping_KeepsBoth()
|
|
|
|
|
{
|
|
|
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
|
|
|
{
|
|
|
|
|
new(10, 10, 50, 50, 0.9f, 0), // top-left
|
|
|
|
|
new(200, 200, 250, 250, 0.8f, 0), // bottom-right
|
|
|
|
|
};
|
|
|
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
|
|
|
result.Length.ShouldBe(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ApplyNms_ThreeDetections_MixedOverlaps_KeepsBest()
|
|
|
|
|
{
|
|
|
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
|
|
|
{
|
|
|
|
|
new(10, 10, 50, 50, 0.9f, 0), // person, best
|
|
|
|
|
new(11, 11, 51, 51, 0.8f, 0), // person, overlaps with #1 → suppressed
|
|
|
|
|
new(12, 12, 52, 52, 0.7f, 0), // person, overlaps with #1 → suppressed
|
|
|
|
|
};
|
|
|
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
|
|
|
result.Length.ShouldBe(1);
|
|
|
|
|
result[0].Confidence.ShouldBe(0.9f, 0.001f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ApplyNms_BoundingBoxConvertedCorrectly()
|
|
|
|
|
{
|
|
|
|
|
// RawDetection(10, 20, 60, 80) → DetectionResult(X=10, Y=20, Width=50, Height=60)
|
|
|
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
|
|
|
{
|
|
|
|
|
new(10, 20, 60, 80, 0.95f, 0)
|
|
|
|
|
};
|
|
|
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
|
|
|
result.Length.ShouldBe(1);
|
|
|
|
|
result[0].X.ShouldBe(10f, 0.001f);
|
|
|
|
|
result[0].Y.ShouldBe(20f, 0.001f);
|
|
|
|
|
result[0].Width.ShouldBe(50f, 0.001f);
|
|
|
|
|
result[0].Height.ShouldBe(60f, 0.001f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ApplyNms_ClassIndexMappedToLabel()
|
|
|
|
|
{
|
|
|
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
|
|
|
{
|
|
|
|
|
new(0, 0, 10, 10, 0.5f, 2), // class 2 = "car"
|
|
|
|
|
new(0, 0, 10, 10, 0.5f, 15), // class 15 = "cat"
|
|
|
|
|
new(0, 0, 10, 10, 0.5f, 79), // class 79 = "toothbrush"
|
|
|
|
|
};
|
|
|
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
|
|
|
// Different classes → all kept (IoU = 1.0 but different class)
|
|
|
|
|
result.Length.ShouldBe(3);
|
|
|
|
|
result.ShouldContain(d => d.Label == "car");
|
|
|
|
|
result.ShouldContain(d => d.Label == "cat");
|
|
|
|
|
result.ShouldContain(d => d.Label == "toothbrush");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ApplyNms_UnknownClassIndex_GeneratesFallbackLabel()
|
|
|
|
|
{
|
|
|
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
|
|
|
{
|
|
|
|
|
new(0, 0, 10, 10, 0.5f, 999)
|
|
|
|
|
};
|
|
|
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
|
|
|
result.Length.ShouldBe(1);
|
|
|
|
|
result[0].Label.ShouldBe("class_999");
|
|
|
|
|
}
|
2026-07-23 15:37:20 +00:00
|
|
|
}
|