using Moq;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Services;
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Services;
///
/// Code review 2026-12-02 TPL-3: every mutation saved its change
/// and THEN staged its audit row with LogAsync, so on the management/CLI path (no later
/// save) the row was discarded — the rig had one Create row for instance ids up to 3876. These
/// run the service against a REAL audit service over SQLite and read the row back through a
/// second context, so only a committed row passes. The repository stays a mock: it never
/// touches the audit context, so nothing but the audit service itself can persist the row.
///
public class InstanceServiceAuditDurabilityTests : IDisposable
{
private readonly Mock _repoMock = new();
private readonly RealAuditDatabase _db = new();
private readonly InstanceService _sut;
/// Builds the service over a mocked repository and a real audit service.
public InstanceServiceAuditDurabilityTests()
{
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny())).ReturnsAsync(1);
_sut = new InstanceService(_repoMock.Object, _db.AuditService);
}
///
public void Dispose() => _db.Dispose();
/// Create persists its audit row (the CLI instance create path).
/// A task that represents the asynchronous operation.
[Fact]
public async Task CreateInstance_AuditRowIsPersisted()
{
_repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny()))
.ReturnsAsync(new Template("T") { Id = 1 });
_repoMock.Setup(r => r.GetInstanceByUniqueNameAsync("Inst1", It.IsAny()))
.ReturnsAsync((Instance?)null);
var result = await _sut.CreateInstanceAsync("Inst1", 1, 1, null, "admin");
Assert.True(result.IsSuccess);
var row = Assert.Single(await _db.PersistedRowsAsync());
Assert.Equal(("Create", "Instance", "Inst1", "admin"), (row.Action, row.EntityType, row.EntityName, row.User));
}
/// SetConnectionBindings persists its audit row (the CLI instance set-bindings path).
/// A task that represents the asynchronous operation.
[Fact]
public async Task SetConnectionBindings_AuditRowIsPersisted()
{
_repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny()))
.ReturnsAsync(new Instance("Inst1") { Id = 1 });
_repoMock.Setup(r => r.GetBindingsByInstanceIdAsync(1, It.IsAny()))
.ReturnsAsync(new List());
var result = await _sut.SetConnectionBindingsAsync(1, [new ConnectionBinding("Temp", 100)], "admin");
Assert.True(result.IsSuccess);
Assert.Equal("SetConnectionBindings", Assert.Single(await _db.PersistedRowsAsync()).Action);
}
/// Enable / Disable / Delete each persist their audit row.
/// The audited action.
/// A task that represents the asynchronous operation.
[Theory]
[InlineData("Enable")]
[InlineData("Disable")]
[InlineData("Delete")]
public async Task StateChanges_AuditRowIsPersisted(string action)
{
_repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny()))
.ReturnsAsync(new Instance("Inst1") { Id = 1, State = InstanceState.Disabled });
var ok = action switch
{
"Enable" => (await _sut.EnableAsync(1, "admin")).IsSuccess,
"Disable" => (await _sut.DisableAsync(1, "admin")).IsSuccess,
_ => (await _sut.DeleteAsync(1, "admin")).IsSuccess,
};
Assert.True(ok);
Assert.Equal(action, Assert.Single(await _db.PersistedRowsAsync()).Action);
}
}