Files
Omika/tests/Omika.Diagnostics.Tests/DiagnosticRecentCorrelationTests.cs
2026-10-23 18:46:50 +02:00

119 lines
5.0 KiB
C#

using Omika.Diagnostics.Queries;
using Omika.Diagnostics.Tests.Fakes;
namespace Omika.Diagnostics.Tests;
/// <summary>
/// Tâche 12 — Traces / opérations récentes : exclusion des NULL (C10), groupement
/// distinct (C11/C13), tri dernière activité DESC (C12) et limite (C14).
/// </summary>
public class DiagnosticRecentCorrelationTests
{
private static readonly DateTimeOffset From = new(2025, 1, 1, 0, 0, 0, TimeSpan.Zero);
// ------------------------------------------- C10 : traces récentes : null exclu ----
[Fact]
public async Task RecentTraces_ExcludesNullTraceIds()
{
var repo = new FakeDiagnosticEventRepository();
await repo.InsertBatchAsync(new[]
{
DiagnosticTestData.Event(Guid.NewGuid(), "A", From.AddMinutes(1), traceId: Guid.NewGuid()),
DiagnosticTestData.Event(Guid.NewGuid(), "B", From.AddMinutes(2), traceId: null), // exclu
DiagnosticTestData.Event(Guid.NewGuid(), "C", From.AddMinutes(3), traceId: Guid.NewGuid()),
});
IReadOnlyList<DiagnosticRecentCorrelation> result = await repo.GetRecentCorrelationsAsync(
DiagnosticCorrelationDimension.Trace, From, 10);
Assert.Equal(2, result.Count);
Assert.All(result, r => Assert.NotEqual(Guid.Empty, r.CorrelationId));
}
// ---------------------------------- C11 : traces récentes : groupement distinct ----
[Fact]
public async Task RecentTraces_GroupsDistinctTraceIds()
{
Guid traceA = Guid.NewGuid();
Guid traceB = Guid.NewGuid();
var repo = new FakeDiagnosticEventRepository();
await repo.InsertBatchAsync(new[]
{
DiagnosticTestData.Event(Guid.NewGuid(), "A", From.AddMinutes(1), traceId: traceA),
DiagnosticTestData.Event(Guid.NewGuid(), "B", From.AddMinutes(2), traceId: traceA),
DiagnosticTestData.Event(Guid.NewGuid(), "C", From.AddMinutes(3), traceId: traceB),
});
IReadOnlyList<DiagnosticRecentCorrelation> result = await repo.GetRecentCorrelationsAsync(
DiagnosticCorrelationDimension.Trace, From, 10);
Assert.Equal(2, result.Count);
DiagnosticRecentCorrelation groupA = Assert.Single(result, r => r.CorrelationId == traceA);
Assert.Equal(2, groupA.EventCount);
Assert.Equal(1, Assert.Single(result, r => r.CorrelationId == traceB).EventCount);
}
// ------------------------------ C12 : traces récentes : tri dernière activité DESC ----
[Fact]
public async Task RecentTraces_OrdersByLastActivityDescending()
{
Guid older = Guid.NewGuid();
Guid newer = Guid.NewGuid();
var repo = new FakeDiagnosticEventRepository();
await repo.InsertBatchAsync(new[]
{
DiagnosticTestData.Event(Guid.NewGuid(), "A", From.AddMinutes(1), traceId: older),
DiagnosticTestData.Event(Guid.NewGuid(), "B", From.AddMinutes(10), traceId: newer),
DiagnosticTestData.Event(Guid.NewGuid(), "C", From.AddMinutes(2), traceId: older),
});
IReadOnlyList<DiagnosticRecentCorrelation> result = await repo.GetRecentCorrelationsAsync(
DiagnosticCorrelationDimension.Trace, From, 10);
Assert.Equal(2, result.Count);
Assert.Equal(newer, result[0].CorrelationId); // dernière activité la plus récente
Assert.Equal(older, result[1].CorrelationId);
}
// ------------------------------- C13 : opérations récentes : distinct correct ----
[Fact]
public async Task RecentOperations_GroupsDistinctOperationIds()
{
Guid opA = Guid.NewGuid();
Guid opB = Guid.NewGuid();
var repo = new FakeDiagnosticEventRepository();
await repo.InsertBatchAsync(new[]
{
DiagnosticTestData.Event(Guid.NewGuid(), "A", From.AddMinutes(1), operationId: opA),
DiagnosticTestData.Event(Guid.NewGuid(), "B", From.AddMinutes(2), operationId: opB),
DiagnosticTestData.Event(Guid.NewGuid(), "C", From.AddMinutes(3), operationId: opA),
});
IReadOnlyList<DiagnosticRecentCorrelation> result = await repo.GetRecentCorrelationsAsync(
DiagnosticCorrelationDimension.Operation, From, 10);
Assert.Equal(2, result.Count);
Assert.Contains(result, r => r.CorrelationId == opA && r.EventCount == 2);
Assert.Contains(result, r => r.CorrelationId == opB && r.EventCount == 1);
}
// ------------------------------------------------- C14 : limite respectée ----
[Fact]
public async Task RecentTraces_RespectsLimit()
{
var repo = new FakeDiagnosticEventRepository();
for (int i = 0; i < 5; i++)
{
await repo.InsertBatchAsync(new[]
{
DiagnosticTestData.Event(Guid.NewGuid(), $"E{i}", From.AddMinutes(i), traceId: Guid.NewGuid())
});
}
IReadOnlyList<DiagnosticRecentCorrelation> result = await repo.GetRecentCorrelationsAsync(
DiagnosticCorrelationDimension.Trace, From, 2);
Assert.Equal(2, result.Count);
}
}