using Omika.App.Local.Services; using Omika.Core.Observability.Outbox; namespace Omika.Tests.Mobile.Local; /// /// Tests du chargeur de configuration locale d'upload de diagnostic. L'upload /// mobile est OPT-IN via le fichier local diagnostics-upload.json (actif /// en Debug comme en Release). Ces tests documentent le comportement fail-open : /// fichier absent/corrompu → upload désactivé, jamais un blocage ni un appel réseau. /// public class DiagnosticUploadConfigurationLoaderTests { [Fact] public void Load_FichierAbsent_UploadDesactive() { string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"), "diagnostics-upload.json"); DiagnosticUploadOptions options = DiagnosticUploadConfigurationLoader.Load(path); Assert.False(options.Enabled); } [Fact] public void Load_FichierEnabledTrue_UploadActiveEtDefautsConserves() { string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); Directory.CreateDirectory(dir); string path = Path.Combine(dir, "diagnostics-upload.json"); File.WriteAllText(path, """{ "Enabled": true }"""); DiagnosticUploadOptions options = DiagnosticUploadConfigurationLoader.Load(path); Assert.True(options.Enabled); // Les valeurs par défaut bornées et sûres sont conservées (seul le // kill-switch est activé par le fichier de test). Assert.Equal("/api/diagnostics", options.EndpointPath); Assert.Equal(50, options.BatchSize); Assert.Equal(256 * 1024, options.MaxBatchBytes); Assert.Equal(TimeSpan.FromSeconds(5), options.Timeout); Assert.Equal(TimeSpan.FromHours(1), options.BackoffMax); } [Fact] public void Load_JsonInvalide_UploadDesactive() { string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); Directory.CreateDirectory(dir); string path = Path.Combine(dir, "diagnostics-upload.json"); File.WriteAllText(path, "{ json invalide"); DiagnosticUploadOptions options = DiagnosticUploadConfigurationLoader.Load(path); Assert.False(options.Enabled); } [Fact] public void Load_FichierVide_UploadDesactive() { string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); Directory.CreateDirectory(dir); string path = Path.Combine(dir, "diagnostics-upload.json"); File.WriteAllText(path, string.Empty); DiagnosticUploadOptions options = DiagnosticUploadConfigurationLoader.Load(path); Assert.False(options.Enabled); } [Fact] public void Enable_EcritFichierOptIn_ReluActif() { string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); string path = Path.Combine(dir, "diagnostics-upload.json"); DiagnosticUploadConfigurationLoader.Enable(path); Assert.True(File.Exists(path)); DiagnosticUploadOptions options = DiagnosticUploadConfigurationLoader.Load(path); Assert.True(options.Enabled); // Les valeurs par défaut bornées et sûres sont conservées. Assert.Equal("/api/diagnostics", options.EndpointPath); Assert.Equal(50, options.BatchSize); } [Fact] public void Enable_CreeDossierParentSiAbsent() { string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); string path = Path.Combine(dir, "diagnostics-upload.json"); // Le dossier parent n'existe pas encore : Enable doit le créer. DiagnosticUploadConfigurationLoader.Enable(path); Assert.True(File.Exists(path)); } [Fact] public void Exists_FichierAbsent_RetourneFalse() { string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"), "diagnostics-upload.json"); Assert.False(DiagnosticUploadConfigurationLoader.Exists(path)); } [Fact] public void Exists_FichierPresent_RetourneTrue() { string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); Directory.CreateDirectory(dir); string path = Path.Combine(dir, "diagnostics-upload.json"); File.WriteAllText(path, """{ "Enabled": false }"""); Assert.True(DiagnosticUploadConfigurationLoader.Exists(path)); } }