Files
Heimdall/tests/Heimdall.Agent.Tests/FrameResizerTests.cs

120 lines
3.8 KiB
C#

using Heimdall.Agent.Services;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
namespace Heimdall.Agent.Tests;
public class FrameResizerTests
{
private static FrameResizer CreateResizer(int targetSize = 640)
{
return new FrameResizer(Substitute.For<ILogger<FrameResizer>>(), targetSize);
}
[Fact]
public void Resize_InvalidJpeg_ReturnsOriginalData()
{
var resizer = CreateResizer();
var garbage = new byte[] { 0x00, 0x01, 0x02, 0x03 };
var result = resizer.Resize(garbage);
result.ShouldBe(garbage);
}
[Fact]
public void Resize_ValidJpeg_ReturnsDifferentSize()
{
// Create a minimal valid JPEG (1x1 white pixel)
var jpeg1x1 = CreateMinimalJpeg();
var resizer = CreateResizer(640);
var result = resizer.Resize(jpeg1x1);
// The resized image should be a different (larger) JPEG
result.ShouldNotBeNull();
result.Length.ShouldNotBe(0);
// Output should be a valid JPEG (starts with FF D8)
result[0].ShouldBe((byte)0xFF);
result[1].ShouldBe((byte)0xD8);
}
[Fact]
public void Resize_DefaultTargetSize_Is640()
{
FrameResizer.DefaultTargetSize.ShouldBe(640);
}
[Fact]
public async Task Resize_ConcurrentCalls_DoesNotThrow()
{
var resizer = CreateResizer();
var jpeg = CreateMinimalJpeg();
// Run multiple resizes concurrently
var tasks = Enumerable.Range(0, 10)
.Select(_ => Task.Run(() => resizer.Resize(jpeg)))
.ToArray();
await Task.WhenAll(tasks);
foreach (var task in tasks)
task.IsCompletedSuccessfully.ShouldBeTrue();
}
/// <summary>Creates a minimal valid JPEG file (1x1 white pixel).</summary>
private static byte[] CreateMinimalJpeg()
{
// Minimal JPEG: SOI + APP0 + DQT + SOF0 + DHT + SOS + EOI
// This is the smallest valid JPEG that ImageSharp can load.
return [
0xFF, 0xD8, // SOI
0xFF, 0xE0, // APP0
0x00, 0x10, // Length
0x4A, 0x46, 0x49, 0x46, 0x00, // "JFIF\0"
0x01, 0x01, // Version
0x00, // Units
0x00, 0x01, // X density
0x00, 0x01, // Y density
0x00, 0x00, // Thumbnail
0xFF, 0xDB, // DQT
0x00, 0x43, // Length
0x00, // Table 0
// 64 quantization values (all 1 for simplicity)
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0xFF, 0xC0, // SOF0
0x00, 0x0B, // Length
0x08, // Precision
0x00, 0x01, // Height
0x00, 0x01, // Width
0x01, // Components
0x01, // Component 1: ID
0x11, // Sampling factors
0x00, // Quant table
0xFF, 0xC4, // DHT
0x00, 0x1F, // Length
0x00, // DC table 0
0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B,
0xFF, 0xDA, // SOS
0x00, 0x08, // Length
0x01, // Components
0x01, // Component 1
0x00, // DC table, AC table
0x00, 0x3F, // Ss, Se, Ah/Al
0x00, // Dummy data
0xFF, 0xD9, // EOI
];
}
}