Files
scadalink-design/tests/ScadaLink.NotificationOutbox.Tests/NotificationOutboxActorIngestTests.cs
Joseph Doherty b31747a632 feat(notif): NotificationOutboxActor + CentralAuditWriter wired (#23 M4)
M4 Bundle B (B1) — add the central-only ICentralAuditWriter implementation
and inject it into NotificationOutboxActor so subsequent tasks (B2/B3) can
route attempt + terminal lifecycle events through the direct-write audit path.

- CentralAuditWriter: thin wrapper around IAuditLogRepository.InsertIfNotExistsAsync;
  scope-per-call (matches AuditLogIngestActor / NotificationOutboxActor pattern);
  stamps IngestedAtUtc; swallows all internal failures (alog.md §13).
- Registered as a singleton in AddAuditLog.
- NotificationOutboxActor ctor takes ICentralAuditWriter (validated non-null).
- Host wiring resolves the writer once from the root provider and passes it
  into the singleton's Props.Create call.
- Existing TestKit fixtures updated with a NoOpCentralAuditWriter helper so
  tests that don't exercise audit emission still compile and pass.
2026-05-20 16:04:01 -04:00

119 lines
4.4 KiB
C#

using Akka.Actor;
using Akka.TestKit.Xunit2;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using ScadaLink.Commons.Entities.Notifications;
using ScadaLink.Commons.Interfaces.Repositories;
using ScadaLink.Commons.Messages.Notification;
using ScadaLink.Commons.Types.Enums;
using ScadaLink.NotificationOutbox.Delivery;
using ScadaLink.NotificationOutbox.Tests.TestSupport;
namespace ScadaLink.NotificationOutbox.Tests;
/// <summary>
/// Task 13: Tests for the <see cref="NotificationOutboxActor"/> ingest path — building a
/// <see cref="Notification"/> from a <see cref="NotificationSubmit"/>, persisting it via
/// <see cref="INotificationOutboxRepository.InsertIfNotExistsAsync"/>, and acking the sender.
/// </summary>
public class NotificationOutboxActorIngestTests : TestKit
{
private readonly INotificationOutboxRepository _repository =
Substitute.For<INotificationOutboxRepository>();
private IServiceProvider BuildServiceProvider()
{
var services = new ServiceCollection();
services.AddScoped(_ => _repository);
return services.BuildServiceProvider();
}
private IActorRef CreateActor()
{
return Sys.ActorOf(Props.Create(() => new NotificationOutboxActor(
BuildServiceProvider(),
new NotificationOutboxOptions(),
new NoOpCentralAuditWriter(),
NullLogger<NotificationOutboxActor>.Instance)));
}
private static NotificationSubmit MakeSubmit(string? notificationId = null)
{
return new NotificationSubmit(
NotificationId: notificationId ?? Guid.NewGuid().ToString(),
ListName: "ops-team",
Subject: "Tank overflow",
Body: "Tank 3 level critical",
SourceSiteId: "site-1",
SourceInstanceId: "instance-42",
SourceScript: "AlarmScript",
SiteEnqueuedAt: new DateTimeOffset(2026, 5, 19, 8, 30, 0, TimeSpan.Zero));
}
[Fact]
public void NotificationSubmit_PersistsMappedNotification_AndAcksAccepted()
{
_repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
.Returns(true);
var submit = MakeSubmit();
var actor = CreateActor();
actor.Tell(submit, TestActor);
var ack = ExpectMsg<NotificationSubmitAck>();
Assert.Equal(submit.NotificationId, ack.NotificationId);
Assert.True(ack.Accepted);
Assert.Null(ack.Error);
_repository.Received(1).InsertIfNotExistsAsync(
Arg.Is<Notification>(n =>
n.NotificationId == submit.NotificationId &&
n.Type == NotificationType.Email &&
n.ListName == submit.ListName &&
n.Subject == submit.Subject &&
n.Body == submit.Body &&
n.SourceSiteId == submit.SourceSiteId &&
n.SourceInstanceId == submit.SourceInstanceId &&
n.SourceScript == submit.SourceScript &&
n.SiteEnqueuedAt == submit.SiteEnqueuedAt &&
n.Status == NotificationStatus.Pending &&
n.CreatedAt != default),
Arg.Any<CancellationToken>());
}
[Fact]
public void DuplicateSubmit_RepositoryReturnsFalse_StillAcksAccepted()
{
_repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
.Returns(false);
var submit = MakeSubmit();
var actor = CreateActor();
actor.Tell(submit, TestActor);
var ack = ExpectMsg<NotificationSubmitAck>();
Assert.Equal(submit.NotificationId, ack.NotificationId);
Assert.True(ack.Accepted);
Assert.Null(ack.Error);
}
[Fact]
public void RepositoryThrows_AcksNotAcceptedWithError()
{
_repository.InsertIfNotExistsAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
.ThrowsAsync(new InvalidOperationException("database unavailable"));
var submit = MakeSubmit();
var actor = CreateActor();
actor.Tell(submit, TestActor);
var ack = ExpectMsg<NotificationSubmitAck>();
Assert.Equal(submit.NotificationId, ack.NotificationId);
Assert.False(ack.Accepted);
Assert.NotNull(ack.Error);
Assert.Contains("database unavailable", ack.Error);
}
}