87 lines
3.3 KiB
C#
87 lines
3.3 KiB
C#
using Heimdall.Application.Agents.Commands.Heartbeat;
|
|
using Heimdall.Application.Common;
|
|
using Heimdall.Application.Tests.Common;
|
|
using Heimdall.Domain.Entities;
|
|
using Heimdall.Domain.Enums;
|
|
using NSubstitute;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Heimdall.Application.Tests.Handlers.Agents;
|
|
|
|
public class HeartbeatHandlerTests
|
|
{
|
|
private readonly Agent _existingAgent = new(Guid.NewGuid(), "agent-1", "agent-1", "1.0.0");
|
|
|
|
[Fact]
|
|
public async Task Heartbeat_should_return_success()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>([_existingAgent]));
|
|
var handler = new HeartbeatHandler(context);
|
|
var command = new HeartbeatCommand(_existingAgent.Id, "203.0.113.1", null, null, null, null);
|
|
|
|
var result = await handler.Handle(command, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Heartbeat_nonexistent_agent_should_return_not_found()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>([_existingAgent]));
|
|
var handler = new HeartbeatHandler(context);
|
|
var command = new HeartbeatCommand(Guid.NewGuid(), null, null, null, null, null);
|
|
|
|
var result = await handler.Handle(command, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
result.StatusCode.ShouldBe(404);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Heartbeat_should_update_agent_status()
|
|
{
|
|
_existingAgent.MarkOffline();
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>([_existingAgent]));
|
|
var handler = new HeartbeatHandler(context);
|
|
var command = new HeartbeatCommand(_existingAgent.Id, "10.0.0.1", null, null, null, null);
|
|
|
|
await handler.Handle(command, CancellationToken.None);
|
|
|
|
_existingAgent.Status.ShouldBe(AgentStatus.Online);
|
|
_existingAgent.LastHeartbeat.ShouldNotBeNull();
|
|
_existingAgent.PublicIp.ShouldBe("10.0.0.1");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Heartbeat_should_update_capabilities_when_provided()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>([_existingAgent]));
|
|
var handler = new HeartbeatHandler(context);
|
|
var command = new HeartbeatCommand(_existingAgent.Id, null, true, 8, 16384, GpuAcceleration.NvidiaCuda);
|
|
|
|
await handler.Handle(command, CancellationToken.None);
|
|
|
|
_existingAgent.HasGpu.ShouldBeTrue();
|
|
_existingAgent.CpuCores.ShouldBe(8);
|
|
_existingAgent.MemoryMb.ShouldBe(16384);
|
|
_existingAgent.GpuType.ShouldBe(GpuAcceleration.NvidiaCuda);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Heartbeat_should_save_changes()
|
|
{
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
context.Agents.Returns(MockDbSetHelper.CreateFor<Agent>([_existingAgent]));
|
|
var handler = new HeartbeatHandler(context);
|
|
var command = new HeartbeatCommand(_existingAgent.Id, null, null, null, null, null);
|
|
|
|
await handler.Handle(command, CancellationToken.None);
|
|
|
|
await context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
|
|
}
|
|
}
|