using System.Text.Json; using Omika.Core.Observability.Outbox; namespace Omika.Tests.Observability.Outbox; /// Tests du contrat de transport versionné (§26, §27, §10). public class DiagnosticTransportTests { private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); [Fact] public void SchemaVersion_IsOne() { Assert.Equal(1, DiagnosticTransportSchema.SchemaVersion); } [Fact] public void Batch_SerializesWithSchemaVersionAndBatchId() { var batch = new DiagnosticBatchDto { BatchId = Guid.Parse("00000000-0000-0000-0000-0000000000AA"), SentAtUtc = new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero), Events = [ new DiagnosticEventDto { EventId = Guid.Parse("00000000-0000-0000-0000-000000000001"), EventCode = "OMK-MOB-APP-001", TimestampUtc = new DateTimeOffset(2024, 1, 2, 3, 4, 5, TimeSpan.Zero), Level = "Info", Priority = "P3" } ] }; string json = JsonSerializer.Serialize(batch, JsonOptions); using JsonDocument document = JsonDocument.Parse(json); Assert.Equal(1, document.RootElement.GetProperty("schemaVersion").GetInt32()); Assert.Equal( "00000000-0000-0000-0000-0000000000aa", document.RootElement.GetProperty("batchId").GetString()); Assert.Equal(1, document.RootElement.GetProperty("events").GetArrayLength()); } [Fact] public void Batch_RoundTrips_BatchIdAndEventIdStable() { Guid batchId = Guid.NewGuid(); Guid eventId = Guid.NewGuid(); var batch = new DiagnosticBatchDto { BatchId = batchId, Events = [new DiagnosticEventDto { EventId = eventId, EventCode = "OMK-MOB-APP-001" }] }; string json = JsonSerializer.Serialize(batch, JsonOptions); DiagnosticBatchDto? back = JsonSerializer.Deserialize(json, JsonOptions); Assert.Equal(batchId, back!.BatchId); Assert.Equal(eventId, back.Events[0].EventId); } [Fact] public void Response_RoundTrips() { var response = new DiagnosticBatchResponseDto { BatchId = Guid.NewGuid(), AcceptedCount = 3 }; string json = JsonSerializer.Serialize(response, JsonOptions); DiagnosticBatchResponseDto? back = JsonSerializer.Deserialize(json, JsonOptions); Assert.Equal(response.BatchId, back!.BatchId); Assert.Equal(3, back.AcceptedCount); } }