100 lines
4.6 KiB
C#
100 lines
4.6 KiB
C#
using System.Globalization;
|
|||
|
|
using Heimdall.Agent;
|
||
|
|
using Heimdall.Agent.Services;
|
||
|
|
using Microsoft.Data.Sqlite;
|
||
|
|
using Microsoft.Extensions.Logging;
|
||
|
|
using Microsoft.Extensions.Options;
|
||
|
|
using NSubstitute;
|
||
|
|
using Shouldly;
|
||
|
|
|
||
|
|
namespace Heimdall.Agent.Tests;
|
||
|
|
|
||
|
|
public sealed class CameraStatusReporterServiceTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public async Task ReportAsync_PersistsPendingStatusWithEfCoreSchema()
|
||
|
|
{
|
||
|
|
var directory = CreateStateDirectory();
|
||
|
|
try
|
||
|
|
{
|
||
|
|
using var reporter = CreateReporter(directory);
|
||
|
|
var cameraId = Guid.NewGuid();
|
||
|
|
|
||
|
|
await reporter.ReportAsync(cameraId, "Online", CancellationToken.None);
|
||
|
|
reporter.FailedReports.ShouldBe(0);
|
||
|
|
|
||
|
|
await using var db = new SqliteConnection($"Data Source={Path.Combine(directory, "agent-outbox.db")};Mode=ReadOnly");
|
||
|
|
await db.OpenAsync();
|
||
|
|
await using var command = db.CreateCommand();
|
||
|
|
command.CommandText = "SELECT status FROM pending_camera_status WHERE camera_id=$id;";
|
||
|
|
command.Parameters.AddWithValue("$id", cameraId.ToString("D"));
|
||
|
|
(await command.ExecuteScalarAsync()).ShouldBe("Online");
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
if (Directory.Exists(directory)) Directory.Delete(directory, recursive: true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task ReportAsync_AdoptsLegacyOutboxAndPreservesPendingAndAcknowledgedStatuses()
|
||
|
|
{
|
||
|
|
var directory = CreateStateDirectory();
|
||
|
|
var databasePath = Path.Combine(directory, "agent-outbox.db");
|
||
|
|
var pendingCamera = Guid.NewGuid();
|
||
|
|
var acknowledgedCamera = Guid.NewGuid();
|
||
|
|
try
|
||
|
|
{
|
||
|
|
await using (var database = new SqliteConnection($"Data Source={databasePath}"))
|
||
|
|
{
|
||
|
|
await database.OpenAsync();
|
||
|
|
await using var setup = database.CreateCommand();
|
||
|
|
setup.CommandText = """
|
||
|
|
CREATE TABLE pending_camera_status (camera_id TEXT PRIMARY KEY NOT NULL, status TEXT NOT NULL, updated_at TEXT NOT NULL);
|
||
|
|
CREATE TABLE acknowledged_camera_status (camera_id TEXT PRIMARY KEY NOT NULL, status TEXT NOT NULL);
|
||
|
|
INSERT INTO pending_camera_status VALUES ($pending, 'Offline', '2026-01-01T00:00:00Z');
|
||
|
|
INSERT INTO acknowledged_camera_status VALUES ($acknowledged, 'Online');
|
||
|
|
PRAGMA user_version=1;
|
||
|
|
""";
|
||
|
|
setup.Parameters.AddWithValue("$pending", pendingCamera.ToString("D"));
|
||
|
|
setup.Parameters.AddWithValue("$acknowledged", acknowledgedCamera.ToString("D"));
|
||
|
|
await setup.ExecuteNonQueryAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
using var reporter = CreateReporter(directory);
|
||
|
|
await reporter.ReportAsync(pendingCamera, "Online", CancellationToken.None);
|
||
|
|
await reporter.ReportAsync(acknowledgedCamera, "Online", CancellationToken.None);
|
||
|
|
reporter.FailedReports.ShouldBe(0);
|
||
|
|
|
||
|
|
await using var verify = new SqliteConnection($"Data Source={databasePath};Mode=ReadOnly");
|
||
|
|
await verify.OpenAsync();
|
||
|
|
await using var checkPending = verify.CreateCommand();
|
||
|
|
checkPending.CommandText = "SELECT status FROM pending_camera_status WHERE camera_id=$id;";
|
||
|
|
checkPending.Parameters.AddWithValue("$id", pendingCamera.ToString("D"));
|
||
|
|
(await checkPending.ExecuteScalarAsync()).ShouldBe("Online");
|
||
|
|
await using var checkAck = verify.CreateCommand();
|
||
|
|
checkAck.CommandText = "SELECT status FROM acknowledged_camera_status WHERE camera_id=$id;";
|
||
|
|
checkAck.Parameters.AddWithValue("$id", acknowledgedCamera.ToString("D"));
|
||
|
|
(await checkAck.ExecuteScalarAsync()).ShouldBe("Online");
|
||
|
|
await using var count = verify.CreateCommand();
|
||
|
|
count.CommandText = "SELECT COUNT(*) FROM pending_camera_status;";
|
||
|
|
Convert.ToInt32(await count.ExecuteScalarAsync(), CultureInfo.InvariantCulture).ShouldBe(1);
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
if (Directory.Exists(directory)) Directory.Delete(directory, recursive: true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static string CreateStateDirectory()
|
||
|
|
{
|
||
|
|
var directory = Path.Combine(Path.GetTempPath(), $"heimdall-status-outbox-{Guid.NewGuid():N}");
|
||
|
|
Directory.CreateDirectory(directory);
|
||
|
|
return directory;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static CameraStatusReporterService CreateReporter(string directory) => new(
|
||
|
|
new AgentState(), Substitute.For<IHttpClientFactory>(),
|
||
|
|
Substitute.For<ILogger<CameraStatusReporterService>>(),
|
||
|
|
Options.Create(new AgentOptions { StateDirectory = directory }));
|
||
|
|
}
|