using Heimdall.Domain.Entities; using Heimdall.Domain.Enums; using Heimdall.Infrastructure.Data; using Heimdall.Infrastructure.Data.Interceptors; using Heimdall.Infrastructure.Jobs; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using NSubstitute; using Shouldly; namespace Heimdall.Infrastructure.Tests; public class RetentionCheckJobTests : IDisposable { private readonly SqliteConnection _connection; private readonly TestDbContextFactory _dbFactory; private readonly ILogger _logger = Substitute.For>(); public RetentionCheckJobTests() { _connection = new SqliteConnection("DataSource=:memory:"); _connection.Open(); _dbFactory = CreateFactory(_connection); } public void Dispose() { _connection?.Close(); _connection?.Dispose(); GC.SuppressFinalize(this); } private static TestDbContextFactory CreateFactory(SqliteConnection connection) { var opts = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; using var ctx = new AppDbContext(opts, new AuditInterceptor()); ctx.Database.EnsureCreated(); return new TestDbContextFactory(opts); } private async Task<(Guid CameraId, Guid PolicyId)> SeedAsync() { await using var db = await _dbFactory.CreateDbContextAsync(); var site = new Site("Site A"); db.Sites.Add(site); await db.SaveChangesAsync(); var camera = new Camera(site.Id, "Cam 1", "rtsp://cam/stream"); db.Cameras.Add(camera); await db.SaveChangesAsync(); var policy = new StoragePolicy("30 days", 30, StorageProvider.S3); db.StoragePolicies.Add(policy); await db.SaveChangesAsync(); return (camera.Id, policy.Id); } private async Task AddRecordingAsync(Guid cameraId, Guid? policyId, DateTime timestamp, int seq) { await using var db = await _dbFactory.CreateDbContextAsync(); db.Recordings.Add(new Recording( cameraId, timestamp, 10.0, $"/recordings/rec-{seq}.mp4", 1000, StorageProvider.S3, policyId)); await db.SaveChangesAsync(); } [Fact] public async Task ExecuteAsync_ExpiredWithPolicy_IsDeleted() { var (cameraId, policyId) = await SeedAsync(); // 40 jours > rétention 30 j → expiré await AddRecordingAsync(cameraId, policyId, DateTime.UtcNow.AddDays(-40), 1); // 10 jours < 30 j → conservé await AddRecordingAsync(cameraId, policyId, DateTime.UtcNow.AddDays(-10), 2); await new RetentionCheckJob(_dbFactory, _logger).ExecuteAsync(CancellationToken.None); await using var verify = await _dbFactory.CreateDbContextAsync(); var remaining = await verify.Recordings.ToListAsync(); remaining.Count.ShouldBe(1); } [Fact] public async Task ExecuteAsync_ExpiredWithoutPolicy_IsDeletedAfterDefault30Days() { var (cameraId, _) = await SeedAsync(); await AddRecordingAsync(cameraId, null, DateTime.UtcNow.AddDays(-45), 3); // > 30 j défaut await AddRecordingAsync(cameraId, null, DateTime.UtcNow.AddDays(-5), 4); // conservé await new RetentionCheckJob(_dbFactory, _logger).ExecuteAsync(CancellationToken.None); await using var verify = await _dbFactory.CreateDbContextAsync(); (await verify.Recordings.CountAsync()).ShouldBe(1); } private sealed class TestDbContextFactory(DbContextOptions opts) : IDbContextFactory { private static readonly AuditInterceptor Audit = new(); public AppDbContext CreateDbContext() => new(opts, Audit); public Task CreateDbContextAsync(CancellationToken ct = default) => Task.FromResult(new AppDbContext(opts, Audit)); } }