Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Agents/HeartbeatHandlerTests.cs
T
Arnaud Dartois 1e3322cd1a fix(agent): heartbeat X-API-Key complet + métriques temps réel + hub config
- ApiKeyAuthenticationHandler accepte l'API key via query string access_token
  sur les chemins /hubs (WebSocket SignalR ne peut pas poser de header custom)
  → le ConfigReceiverService de l'agent peut se connecter au ConfigHub
- ConfigReceiverService.FetchFullConfigAsync : X-API-Key au lieu de Bearer
- Métriques heartbeat : CpuUsagePercent/MemoryUsageBytes/DiskUsagePercent/
  UptimeSeconds + IsOffline désormais transportées jusqu'à l'entité Agent
  (migration AddAgentLiveMetrics) ; IsOffline marque l'agent Offline
  immédiatement au lieu d'attendre le timeout 90s du HeartbeatMonitorJob
- docker-compose.local.yml : override aussi l'image agent (heimdall-agent:local)

Tests : +2 unit AgentRegistrationService (X-API-Key), +2 HeartbeatHandler
(métriques, offline), +3 intégration SignalR (ConfigHub query string) — verts.
2026-08-10 10:26:31 +02:00

128 lines
4.9 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>());
}
[Fact]
public async Task Heartbeat_should_update_live_metrics()
{
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,
IsOffline: false,
CpuUsagePercent: 42.5,
MemoryUsageBytes: 8_589_934_592,
DiskUsagePercent: 61.3,
UptimeSeconds: 3600);
await handler.Handle(command, CancellationToken.None);
_existingAgent.CpuUsagePercent.ShouldBe(42.5);
_existingAgent.MemoryUsageBytes.ShouldBe(8_589_934_592);
_existingAgent.DiskUsagePercent.ShouldBe(61.3);
_existingAgent.UptimeSeconds.ShouldBe(3600);
_existingAgent.Status.ShouldBe(AgentStatus.Online);
}
[Fact]
public async Task Heartbeat_isOffline_should_mark_agent_offline()
{
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,
IsOffline: true,
CpuUsagePercent: 12.0);
await handler.Handle(command, CancellationToken.None);
_existingAgent.Status.ShouldBe(AgentStatus.Offline);
// Metrics are not refreshed on a shutdown heartbeat
_existingAgent.CpuUsagePercent.ShouldBeNull();
}
}