using Heimdall.Domain.Entities; using Heimdall.Domain.Enums; using Shouldly; namespace Heimdall.Domain.Tests.Entities; public class AgentInventoryTests { [Fact] public void UpdateCpuInventory_should_set_all_fields() { var agent = CreateAgent(); agent.UpdateCpuInventory(new CpuInventoryData( Vendor: "GenuineIntel", Model: "Intel(R) Core(TM) i7-12700K", Architecture: "x86_64", PhysicalCores: 12, LogicalCores: 20, ThreadsPerCore: 2, FrequencyMhz: 3600.0, Flags: "avx,avx2,fma,aes", CacheBytes: 25_165_824, ContainerCpuLimit: 200)); agent.CpuVendor.ShouldBe("GenuineIntel"); agent.CpuModel.ShouldBe("Intel(R) Core(TM) i7-12700K"); agent.CpuArchitecture.ShouldBe("x86_64"); agent.PhysicalCores.ShouldBe(12); agent.LogicalCores.ShouldBe(20); agent.ThreadsPerCore.ShouldBe(2); agent.CpuFrequencyMhz.ShouldBe(3600.0); agent.CpuFlags.ShouldBe("avx,avx2,fma,aes"); agent.CpuCacheBytes.ShouldBe(25_165_824); agent.ContainerCpuLimit.ShouldBe(200); } [Fact] public void UpdateCpuInventory_should_accept_nulls() { var agent = CreateAgent(); agent.UpdateCpuInventory(new CpuInventoryData(null, null, null, null, null, null, null, null, null, null)); agent.CpuVendor.ShouldBeNull(); agent.CpuModel.ShouldBeNull(); agent.PhysicalCores.ShouldBeNull(); } [Fact] public void UpdateMemoryDiskInventory_should_set_all_fields() { var agent = CreateAgent(); var diskJson = """[{"MountPoint":"/","FileSystem":"ext4","TotalBytes":107374182400,"UsedBytes":53687091200,"Type":"SSD"}]"""; agent.UpdateMemoryDiskInventory(new MemoryDiskInventoryData( MemTotalBytes: 17_179_869_184, MemAvailableBytes: 8_589_934_592, SwapTotalBytes: 2_147_483_648, SwapFreeBytes: 1_073_741_824, ContainerMemoryLimitBytes: 4_294_967_296, DiskInventoryJson: diskJson)); agent.MemTotalBytes.ShouldBe(17_179_869_184); agent.MemAvailableBytes.ShouldBe(8_589_934_592); agent.SwapTotalBytes.ShouldBe(2_147_483_648); agent.SwapFreeBytes.ShouldBe(1_073_741_824); agent.ContainerMemoryLimitBytes.ShouldBe(4_294_967_296); agent.DiskInventoryJson.ShouldBe(diskJson); } [Fact] public void UpdateGpuInventory_should_set_json() { var agent = CreateAgent(); var gpuJson = """[{"Vendor":"Nvidia","Model":"RTX 3090","VramBytes":25769803776}]"""; agent.UpdateGpuInventory(gpuJson); agent.GpuInventoryJson.ShouldBe(gpuJson); } [Fact] public void UpdateHostInventory_should_set_all_fields() { var agent = CreateAgent(); var netJson = """[{"Name":"eth0","IpAddress":"192.168.1.100","MacAddress":"aa:bb:cc:dd:ee:ff"}]"""; var tempJson = """[{"Zone":"coretemp","TemperatureCelsius":45.0}]"""; agent.UpdateHostInventory(new HostInventoryData( OsName: "Ubuntu 22.04 LTS", KernelVersion: "5.15.0-76-generic", Arch: "x86_64", UptimeMachineSeconds: 86400, ContainerId: "abc123def456", ContainerImage: "heimdall/agent:latest", NetworkInterfacesJson: netJson, TemperatureJson: tempJson)); agent.OsName.ShouldBe("Ubuntu 22.04 LTS"); agent.KernelVersion.ShouldBe("5.15.0-76-generic"); agent.Arch.ShouldBe("x86_64"); agent.UptimeMachineSeconds.ShouldBe(86400); agent.ContainerId.ShouldBe("abc123def456"); agent.ContainerImage.ShouldBe("heimdall/agent:latest"); agent.NetworkInterfacesJson.ShouldBe(netJson); agent.TemperatureJson.ShouldBe(tempJson); } [Fact] public void UpdateHostInventory_should_accept_nulls() { var agent = CreateAgent(); agent.UpdateHostInventory(new HostInventoryData(null, null, null, null, null, null, null, null)); agent.OsName.ShouldBeNull(); agent.KernelVersion.ShouldBeNull(); agent.ContainerId.ShouldBeNull(); } [Fact] public void All_new_fields_should_be_null_by_default() { var agent = CreateAgent(); // CPU agent.CpuVendor.ShouldBeNull(); agent.CpuModel.ShouldBeNull(); agent.CpuArchitecture.ShouldBeNull(); agent.PhysicalCores.ShouldBeNull(); agent.LogicalCores.ShouldBeNull(); agent.ThreadsPerCore.ShouldBeNull(); agent.CpuFrequencyMhz.ShouldBeNull(); agent.CpuFlags.ShouldBeNull(); agent.CpuCacheBytes.ShouldBeNull(); agent.ContainerCpuLimit.ShouldBeNull(); // Memory agent.MemTotalBytes.ShouldBeNull(); agent.MemAvailableBytes.ShouldBeNull(); agent.SwapTotalBytes.ShouldBeNull(); agent.SwapFreeBytes.ShouldBeNull(); agent.ContainerMemoryLimitBytes.ShouldBeNull(); // Disk agent.DiskInventoryJson.ShouldBeNull(); // GPU agent.GpuInventoryJson.ShouldBeNull(); // Host agent.OsName.ShouldBeNull(); agent.KernelVersion.ShouldBeNull(); agent.Arch.ShouldBeNull(); agent.UptimeMachineSeconds.ShouldBeNull(); // Container agent.ContainerId.ShouldBeNull(); agent.ContainerImage.ShouldBeNull(); // Network & Temperature agent.NetworkInterfacesJson.ShouldBeNull(); agent.TemperatureJson.ShouldBeNull(); } private static Agent CreateAgent() { var agent = new Agent(Guid.NewGuid(), "TestAgent", "test-host", "1.0.0"); agent.Register("hash", "pref"); return agent; } }