Completes the scheduled jobs module with default jobs, a themed scheduler page and aligned recovery semantics

This commit is contained in:
BinaryAlley committed 2026-09-04 13:56:04 -07:00
1 parent 99a6159f7f
commit 4b7cfbbe11
67 files changed
+3324 -745

No files matched your search

@@ -146,24 +146,39 @@ public class ScheduledJobEntityMappingTests
}
[Fact]
public void ToResponse_WhenRepositoryEntityHasDailySchedule_ShouldMapToResponse()
public void ToDomainEntity_WhenRepositoryEntityHasOnceAtStartupSchedule_ShouldMapToOnceAtStartupScheduledJob()
{
// Arrange
ScheduledJobEntity repositoryEntity = _scheduledJobEntityFixture.Create(
scheduleType: ScheduleType.OnceAtStartup,
status: ScheduledJobStatus.Active);
// Act
Result<ScheduledJob> result = repositoryEntity.ToDomainEntity();
// Assert
Assert.False(result.IsFailure);
OnceAtStartupSchedule onceAtStartupSchedule = Assert.IsType<OnceAtStartupSchedule>(result.Value.Schedule);
Assert.Equal(ScheduleType.OnceAtStartup, onceAtStartupSchedule.ScheduleType);
}
[Fact]
public void ToResponse_WhenRepositoryEntityHasOnceAtStartupSchedule_ShouldMapToResponse()
{
// Arrange
ScheduledJobEntity repositoryEntity = _scheduledJobEntityFixture.Create(
id: Guid.NewGuid(),
scheduleType: ScheduleType.DailyAtHourAndMinute,
hour: 6,
minute: 30,
status: ScheduledJobStatus.Added);
scheduleType: ScheduleType.OnceAtStartup,
status: ScheduledJobStatus.Active);
// Act
ScheduledJobResponse result = repositoryEntity.ToResponse();
// Assert
Assert.NotNull(result);
Assert.Equal(ScheduleType.DailyAtHourAndMinute, result.ScheduleType);
Assert.Equal(ScheduleType.OnceAtStartup, result.ScheduleType);
Assert.Null(result.IntervalMinutes);
Assert.Equal(6, result.Hour);
Assert.Equal(30, result.Minute);
Assert.Null(result.Hour);
Assert.Null(result.Minute);
}
}
@@ -1,18 +1,24 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.Seed;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DomainEvents;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Security;
using Lumina.Application.Common.Infrastructure.Time;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
using Lumina.Contracts.Responses.Authentication;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.Events;
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
@@ -35,10 +41,13 @@ public class SetupApplicationCommandHandlerTests
private readonly ITotpTokenGenerator _mockTotpTokenGenerator;
private readonly IQRCodeGenerator _mockQRCodeGenerator;
private readonly IUserRepository _mockUserRepository;
private readonly IScheduledJobRepository _mockScheduledJobRepository;
private readonly IDateTimeProvider _mockDateTimeProvider;
private readonly IDataSeedService _mockDataSeedService;
private readonly IDomainEventsQueue _mockDomainEventsQueue;
private readonly SetupApplicationCommandHandler _sut;
private readonly UserEntityFixture _userEntityFixture = new();
private readonly ScheduledJobEntityFixture _scheduledJobEntityFixture = new();
private readonly SetupApplicationCommandFixture _setupApplicationCommandFixture = new();
/// <summary>
@@ -52,14 +61,21 @@ public class SetupApplicationCommandHandlerTests
_mockTotpTokenGenerator = Substitute.For<ITotpTokenGenerator>();
_mockQRCodeGenerator = Substitute.For<IQRCodeGenerator>();
_mockUserRepository = Substitute.For<IUserRepository>();
_mockScheduledJobRepository = Substitute.For<IScheduledJobRepository>();
_mockDateTimeProvider = Substitute.For<IDateTimeProvider>();
_mockDataSeedService = Substitute.For<IDataSeedService>();
_mockDomainEventsQueue = Substitute.For<IDomainEventsQueue>();
IValidator<SetupApplicationCommand> mockValidator = Substitute.For<IValidator<SetupApplicationCommand>>();
mockValidator.Validate(Arg.Any<SetupApplicationCommand>())
.Returns([]);
_mockUnitOfWork.UserRepository.Returns(_mockUserRepository);
_mockUnitOfWork.ScheduledJobRepository.Returns(_mockScheduledJobRepository);
_mockDateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
_mockDataSeedService.SetDefaultScheduledJobsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<ScheduledJobEntity>()));
_sut = new SetupApplicationCommandHandler(
_mockUnitOfWork,
@@ -69,6 +85,7 @@ public class SetupApplicationCommandHandlerTests
_mockQRCodeGenerator,
_mockDateTimeProvider,
_mockDataSeedService,
_mockDomainEventsQueue,
mockValidator);
}
@@ -336,6 +353,40 @@ public class SetupApplicationCommandHandlerTests
await _mockDataSeedService.Received(1).SetDefaultAuthorizationRolesAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockDataSeedService.Received(1).SetAdminRolePermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockDataSeedService.Received(1).SetAdminRoleToAdministratorAccount(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockDataSeedService.DidNotReceive().SetDefaultScheduledJobsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenSeedingDefaultScheduledJobsFails_ShouldReturnError()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to seed default scheduled jobs");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationPermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationRolesAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetAdminRolePermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetAdminRoleToAdministratorAccount(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultScheduledJobsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(error);
// Act
Result<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockDataSeedService.Received(1).SetAdminRoleToAdministratorAccount(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockDataSeedService.Received(1).SetDefaultScheduledJobsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
@@ -369,5 +420,40 @@ public class SetupApplicationCommandHandlerTests
await _mockDataSeedService.Received(1).SetDefaultAuthorizationRolesAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockDataSeedService.Received(1).SetAdminRolePermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockDataSeedService.Received(1).SetAdminRoleToAdministratorAccount(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockDataSeedService.Received(1).SetDefaultScheduledJobsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenDefaultScheduledJobsAreSeeded_ShouldQueueACycleStartedEventForEachActiveJob()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
ScheduledJobEntity activeScheduledJob1 = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Active);
ScheduledJobEntity activeScheduledJob2 = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Active);
ScheduledJobEntity addedScheduledJob = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Added);
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationPermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationRolesAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetAdminRolePermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetAdminRoleToAdministratorAccount(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(new[] { activeScheduledJob1, activeScheduledJob2, addedScheduledJob });
// Act
Result<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
_mockDomainEventsQueue.Received(1).Enqueue(Arg.Is<ScheduledJobCycleStartedDomainEvent>(domainEvent => domainEvent.ScheduledJobId.Value == activeScheduledJob1.Id));
_mockDomainEventsQueue.Received(1).Enqueue(Arg.Is<ScheduledJobCycleStartedDomainEvent>(domainEvent => domainEvent.ScheduledJobId.Value == activeScheduledJob2.Id));
_mockDomainEventsQueue.DidNotReceive().Enqueue(Arg.Is<ScheduledJobCycleStartedDomainEvent>(domainEvent => domainEvent.ScheduledJobId.Value == addedScheduledJob.Id));
}
}