using System.Text.Json; using Heimdall.Agent.Services; namespace Heimdall.Agent.Tests; public class FrameAnalysisResultTests { private static readonly IReadOnlyList SampleDetections = [ new DetectionResult("person", 0.95f, 100f, 200f, 50f, 80f), new DetectionResult("car", 0.87f, 300f, 400f, 120f, 90f) ]; [Fact] public void Constructor_WithValues_SetsProperties() { var result = new FrameAnalysisResult("base64data", SampleDetections, 42); result.ImageBase64.ShouldBe("base64data"); result.Detections.ShouldBeSameAs(SampleDetections); result.Detections.Count.ShouldBe(2); result.ProcessingMs.ShouldBe(42); } [Fact] public void Constructor_WithEmptyDetections_HasEmptyList() { var result = new FrameAnalysisResult("base64data", [], 0); result.Detections.ShouldBeEmpty(); } [Fact] public void Equality_SameValues_AreEqual() { var detections = new List { new("person", 0.95f, 100f, 200f, 50f, 80f) }; var a = new FrameAnalysisResult("img1", detections, 10); var b = new FrameAnalysisResult("img1", detections, 10); (a == b).ShouldBeTrue(); a.Equals(b).ShouldBeTrue(); } [Fact] public void Equality_DifferentImageBase64_AreNotEqual() { var detections = new List { new("person", 0.95f, 100f, 200f, 50f, 80f) }; var a = new FrameAnalysisResult("img1", detections, 10); var b = new FrameAnalysisResult("img2", detections, 10); (a != b).ShouldBeTrue(); a.Equals(b).ShouldBeFalse(); } [Fact] public void JsonSerialization_Roundtrip_PreservesValues() { var original = new FrameAnalysisResult("dGVzdA==", SampleDetections, 123); var json = JsonSerializer.Serialize(original); var deserialized = JsonSerializer.Deserialize(json); deserialized.ShouldNotBeNull(); deserialized.ImageBase64.ShouldBe(original.ImageBase64); deserialized.ProcessingMs.ShouldBe(original.ProcessingMs); deserialized.Detections.Count.ShouldBe(original.Detections.Count); } [Fact] public void ProcessingMs_IsMeasurementDuration() { var result = new FrameAnalysisResult("data", SampleDetections, 0); result.ProcessingMs.ShouldBe(0); result = new FrameAnalysisResult("data", SampleDetections, long.MaxValue); result.ProcessingMs.ShouldBe(long.MaxValue); } [Fact] public void ImageBase64_HoldsEncodedImageData() { var imageBytes = new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 }; var base64 = Convert.ToBase64String(imageBytes); var result = new FrameAnalysisResult(base64, SampleDetections, 5); result.ImageBase64.ShouldBe(base64); Convert.FromBase64String(result.ImageBase64).ShouldBe(imageBytes); } [Fact] public void Detections_AreReadOnly() { var result = new FrameAnalysisResult("data", SampleDetections, 1); result.Detections.ShouldBeAssignableTo>(); } }