using System.Diagnostics; using Omika.Core.Configuration; using Omika.Core.Observability; using Omika.Core.Observability.Diagnostic; namespace Omika.Tests.Observability.Diagnostic; public class OmikaLocalDiagnosticsTests : IDisposable { private readonly string _dir = DiagnosticTestHarness.CreateTempDirectory(); [Fact] public async Task EndToEnd_EventWrittenWithSessionContext() { var host = new OmikaLocalDiagnostics(new LocalDiagnosticOptions { Directory = _dir }); host.Start(); host.Telemetry.Track(new TelemetryEvent { EventCode = TelemetryEventCodes.MobileAppStarted }); await DiagnosticTestHarness.WaitUntilAsync(() => host.Queue.Count == 0); await host.StopAsync(); string content = File.ReadAllText(Assert.Single(Directory.EnumerateFiles(_dir, "*.jsonl"))); Assert.Contains(host.Session.SessionId.ToString("N"), content); Assert.Contains("\"sessionId\"", content); } [Fact] public async Task TelemetryDisabled_NoFileCreated() { var host = new OmikaLocalDiagnostics( new LocalDiagnosticOptions { Directory = _dir }, new ObservabilityOptions { Enabled = false }); host.Start(); host.Telemetry.Track(new TelemetryEvent { EventCode = TelemetryEventCodes.MobileAppStarted }); await Task.Delay(150); await host.StopAsync(); Assert.Empty(Directory.EnumerateFiles(_dir, "*.jsonl")); } [Fact] public async Task StopAsync_IsBounded_AndNeverThrows() { var host = new OmikaLocalDiagnostics(new LocalDiagnosticOptions { Directory = _dir, FlushTimeout = TimeSpan.FromMilliseconds(100) }); host.Start(); for (int i = 0; i < 20; i++) { host.Telemetry.Track(new TelemetryEvent { EventCode = TelemetryEventCodes.MobileAppStarted }); } var stopwatch = Stopwatch.StartNew(); await host.StopAsync(); stopwatch.Stop(); Assert.True(stopwatch.Elapsed < TimeSpan.FromSeconds(2)); } [Fact] public async Task Saturation_100kEvents_Bounded_NoException() { var host = new OmikaLocalDiagnostics(new LocalDiagnosticOptions { Directory = _dir }); host.Start(); for (int i = 0; i < 100_000; i++) { host.Telemetry.Track(new TelemetryEvent { EventCode = TelemetryEventCodes.MobileAppStarted }); } await host.StopAsync(); Assert.True(Directory.EnumerateFiles(_dir, "*.jsonl").Count() <= 5); } public void Dispose() => DiagnosticTestHarness.DeleteDirectory(_dir); }