up
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using StellaOps.Findings.Ledger.Domain;
|
||||
using StellaOps.Findings.Ledger.Infrastructure;
|
||||
using StellaOps.Findings.Ledger.Services;
|
||||
using StellaOps.Findings.Ledger.Services.Incident;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Findings.Ledger.Tests.Services;
|
||||
|
||||
public class LedgerEventWriteServiceIncidentTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task AppendAsync_sequence_mismatch_records_conflict_snapshot()
|
||||
{
|
||||
var repo = new Mock<ILedgerEventRepository>();
|
||||
repo.Setup(r => r.GetByEventIdAsync(It.IsAny<string>(), It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync((LedgerEventRecord?)null);
|
||||
|
||||
var chainId = Guid.NewGuid();
|
||||
var chainHead = new LedgerEventRecord(
|
||||
"tenant-a",
|
||||
chainId,
|
||||
1,
|
||||
Guid.NewGuid(),
|
||||
LedgerEventConstants.EventFindingCreated,
|
||||
"v1",
|
||||
"finding-1",
|
||||
"artifact-1",
|
||||
null,
|
||||
"actor-1",
|
||||
"operator",
|
||||
DateTimeOffset.UtcNow,
|
||||
DateTimeOffset.UtcNow,
|
||||
new JsonObject(),
|
||||
"hash-prev",
|
||||
LedgerEventConstants.EmptyHash,
|
||||
"leaf-hash",
|
||||
"{}");
|
||||
|
||||
repo.Setup(r => r.GetChainHeadAsync("tenant-a", chainId, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(chainHead);
|
||||
|
||||
var scheduler = new Mock<IMerkleAnchorScheduler>();
|
||||
var diagnostics = new Mock<ILedgerIncidentDiagnostics>();
|
||||
|
||||
var service = new LedgerEventWriteService(
|
||||
repo.Object,
|
||||
scheduler.Object,
|
||||
NullLogger<LedgerEventWriteService>.Instance,
|
||||
diagnostics.Object);
|
||||
|
||||
var draft = new LedgerEventDraft(
|
||||
"tenant-a",
|
||||
chainId,
|
||||
3,
|
||||
Guid.NewGuid(),
|
||||
LedgerEventConstants.EventFindingCreated,
|
||||
"v1",
|
||||
"finding-1",
|
||||
"artifact-1",
|
||||
null,
|
||||
"actor-1",
|
||||
"operator",
|
||||
DateTimeOffset.UtcNow,
|
||||
DateTimeOffset.UtcNow,
|
||||
new JsonObject(),
|
||||
new JsonObject(),
|
||||
null);
|
||||
|
||||
var result = await service.AppendAsync(draft, CancellationToken.None);
|
||||
|
||||
result.Status.Should().Be(LedgerWriteStatus.Conflict);
|
||||
diagnostics.Verify(d => d.RecordConflict(It.Is<ConflictSnapshot>(s => s.Reason == "sequence_mismatch")), Times.Once);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user