using Heimdall.Application.Agents; using Heimdall.Application.DTOs; using Heimdall.Domain.Entities; using Heimdall.Domain.Enums; namespace Heimdall.Application.Tests.Agents; public class AgentExtensionsTests { [Fact] public void ToResponse_ShouldMapBasicFields() { var agent = CreateAgent(); agent.UpdateCapabilities(true, 8, 16384, GpuAcceleration.NvidiaCuda); var response = agent.ToResponse(); response.Name.ShouldBe("test-agent"); response.Hostname.ShouldBe("test-host"); response.Version.ShouldBe("1.0.0"); response.HasGpu.ShouldBeTrue(); response.CpuCores.ShouldBe(8); response.MemoryMb.ShouldBe(16384); response.GpuType.ShouldBe("NvidiaCuda"); } [Fact] public void ToResponse_ShouldMapLiveMetrics() { var agent = CreateAgent(); agent.UpdateLiveMetrics(42.5, 4_294_967_296, 73.1, 86400); var response = agent.ToResponse(); response.CpuUsagePercent.ShouldBe(42.5); response.MemoryUsageBytes.ShouldBe(4_294_967_296); response.DiskUsagePercent.ShouldBe(73.1); response.UptimeSeconds.ShouldBe(86400); } [Fact] public void ToResponse_ShouldMapCpuInventory() { var agent = CreateAgent(); agent.UpdateCpuInventory(new CpuInventoryData( "GenuineIntel", "Intel Core i7-12700K", "x86_64", 12, 20, 2, 3600.0, "avx,avx2,fma", 25_165_824, 200)); var response = agent.ToResponse(); response.CpuVendor.ShouldBe("GenuineIntel"); response.CpuModel.ShouldBe("Intel Core i7-12700K"); response.CpuArchitecture.ShouldBe("x86_64"); response.PhysicalCores.ShouldBe(12); response.LogicalCores.ShouldBe(20); response.ThreadsPerCore.ShouldBe(2); response.CpuFrequencyMhz.ShouldBe(3600.0); response.CpuFlags.ShouldBe("avx,avx2,fma"); response.CpuCacheBytes.ShouldBe(25_165_824); response.ContainerCpuLimit.ShouldBe(200); } [Fact] public void ToResponse_ShouldMapMemoryInventory() { var agent = CreateAgent(); agent.UpdateMemoryDiskInventory(new MemoryDiskInventoryData( 17_179_869_184, 8_589_934_592, 2_147_483_648, 1_073_741_824, 4_294_967_296, """[{"Mount":"/"}]""")); var response = agent.ToResponse(); response.MemTotalBytes.ShouldBe(17_179_869_184); response.MemAvailableBytes.ShouldBe(8_589_934_592); response.SwapTotalBytes.ShouldBe(2_147_483_648); response.SwapFreeBytes.ShouldBe(1_073_741_824); response.ContainerMemoryLimitBytes.ShouldBe(4_294_967_296); response.DiskInventoryJson.ShouldBe("""[{"Mount":"/"}]"""); } [Fact] public void ToResponse_ShouldMapGpuInventory() { var agent = CreateAgent(); agent.UpdateGpuInventory("""[{"Name":"RTX 4090"}]"""); var response = agent.ToResponse(); response.GpuInventoryJson.ShouldBe("""[{"Name":"RTX 4090"}]"""); } [Fact] public void ToResponse_ShouldMapHostInventory() { var agent = CreateAgent(); agent.UpdateHostInventory(new HostInventoryData( "Ubuntu 22.04 LTS", "5.15.0-76-generic", "x86_64", 100000, "abc123", "heimdall/agent:latest", """[{"Name":"eth0"}]""", """[{"Zone":"coretemp"}]""")); var response = agent.ToResponse(); response.OsName.ShouldBe("Ubuntu 22.04 LTS"); response.KernelVersion.ShouldBe("5.15.0-76-generic"); response.Arch.ShouldBe("x86_64"); response.UptimeMachineSeconds.ShouldBe(100000); response.ContainerId.ShouldBe("abc123"); response.ContainerImage.ShouldBe("heimdall/agent:latest"); response.NetworkInterfacesJson.ShouldBe("""[{"Name":"eth0"}]"""); response.TemperatureJson.ShouldBe("""[{"Zone":"coretemp"}]"""); } [Fact] public void ToResponse_WithNullSite_ShouldReturnEmptySiteName() { var agent = CreateAgent(); var response = agent.ToResponse(); response.SiteName.ShouldBe(string.Empty); } [Fact] public void ToResponse_ShouldMapSiteName_WhenSiteIsSet() { var agent = CreateAgent(); // Site navigation is set by EF Core; test with reflection var siteProperty = typeof(Agent).GetProperty("Site", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); var site = new Site("Test Site"); siteProperty?.SetValue(agent, site); var response = agent.ToResponse(); response.SiteName.ShouldBe("Test Site"); } [Fact] public void ToResponse_AllTelemetryFields_ShouldBeMapped() { var agent = CreateAgent(); agent.UpdateLiveMetrics(42.5, 4_294_967_296, 73.1, 86400); agent.UpdateCpuInventory(new CpuInventoryData( "AMD", "Ryzen 9", "x86_64", 8, 16, 2, 3400.0, "sse4", 16_777_216, null)); agent.UpdateMemoryDiskInventory(new MemoryDiskInventoryData( 8_000_000_000, 4_000_000_000, 2_000_000_000, 1_000_000_000, null, null)); agent.UpdateGpuInventory("""[{"Name":"RX 7900"}]"""); agent.UpdateHostInventory(new HostInventoryData( "Fedora", "6.5.0", "x86_64", 50000, "def", "img", "[]", "[]")); var response = agent.ToResponse(); // Live metrics response.CpuUsagePercent.ShouldBe(42.5); response.MemoryUsageBytes.ShouldBe(4_294_967_296); response.DiskUsagePercent.ShouldBe(73.1); response.UptimeSeconds.ShouldBe(86400); // CPU response.CpuVendor.ShouldBe("AMD"); response.CpuModel.ShouldBe("Ryzen 9"); // Memory response.MemTotalBytes.ShouldBe(8_000_000_000); // GPU response.GpuInventoryJson.ShouldBe("""[{"Name":"RX 7900"}]"""); // Host response.OsName.ShouldBe("Fedora"); } [Fact] public void ToTelemetryFields_ShouldMapAllFields() { var agent = CreateAgent(); agent.UpdateLiveMetrics(42.5, 4_294_967_296, 73.1, 86400); agent.UpdateCpuInventory(new CpuInventoryData( "Intel", "i7", "x86_64", 12, 20, 2, 3600.0, "avx", 25_165_824, 200)); agent.UpdateMemoryDiskInventory(new MemoryDiskInventoryData( 17_179_869_184, 8_589_934_592, 2_147_483_648, 1_073_741_824, 4_294_967_296, """[{"Mount":"/"}]""")); agent.UpdateGpuInventory("""[{"Name":"RTX 4090"}]"""); agent.UpdateHostInventory(new HostInventoryData( "Ubuntu", "5.15.0", "x86_64", 86400, "abc", "img", "[]", "[]")); var fields = agent.ToTelemetryFields(); fields.CpuUsagePercent.ShouldBe(42.5); fields.MemoryUsageBytes.ShouldBe(4_294_967_296); fields.DiskUsagePercent.ShouldBe(73.1); fields.UptimeSeconds.ShouldBe(86400); fields.CpuVendor.ShouldBe("Intel"); fields.CpuModel.ShouldBe("i7"); fields.MemTotalBytes.ShouldBe(17_179_869_184); fields.GpuInventoryJson.ShouldBe("""[{"Name":"RTX 4090"}]"""); fields.OsName.ShouldBe("Ubuntu"); } private static Agent CreateAgent() { return new Agent(Guid.NewGuid(), "test-agent", "test-host", "1.0.0"); } }