using Omika.App.Local.Abstractions;
using Omika.App.Local.Models;
using Omika.App.Local.Repositories;
using Omika.App.Local.Services;
using Omika.App.Local.Sqlite;
namespace Omika.Tests.Mobile.Local;
///
/// Base de test pour l'infrastructure locale offline-first.
/// Crée une base SQLite en mémoire avec le schéma complet.
///
public abstract class LocalTestBase : IDisposable
{
protected LocalDatabase Database { get; }
protected LocalRepository ClientRepository { get; }
protected LocalRepository SiteRepository { get; }
protected LocalRepository InterventionRepository { get; }
protected LocalRepository RendezVousRepository { get; }
protected LocalRepository DossierRepository { get; }
protected LocalRepository DocumentMetadataRepository { get; }
protected LocalRepository PersonneRepository { get; }
protected LocalRepository CollaborateurRepository { get; }
protected LocalRepository SuiviTempsRepository { get; }
protected OutboxRepository OutboxRepository { get; }
protected IdentityMappingRepository IdentityMappingRepository { get; }
protected DocumentCacheRepository DocumentCacheRepository { get; }
protected NoteDocumentLocalRepository NoteDocumentRepository { get; }
protected SyncStateRepository SyncStateRepository { get; }
protected LocalBootstrapRepository BootstrapRepository { get; }
protected SuiviTempsDraftRepository SuiviTempsDraftRepository { get; }
protected RendezVousTempsValidationRepository RendezVousTempsValidationRepository { get; }
protected PreferencesUtilisateurRepository PreferencesUtilisateurRepository { get; }
protected IOutboxService OutboxService { get; }
protected IMobileIdentityReconciliationService ReconciliationService { get; }
protected InterventionCacheRepository InterventionCacheRepository { get; }
protected IContextInterventionCacheRepository ContextInterventionCacheRepository { get; }
protected DossierLocalStateRepository DossierStateRepository { get; }
protected ClientLocalStateRepository ClientStateRepository { get; }
protected SiteLocalStateRepository SiteStateRepository { get; }
protected IDossierInterventionCacheRepository DossierInterventionCacheRepository { get; }
protected IMobileApiClient ApiClient { get; }
protected StubSessionService SessionService { get; } = new();
/// Connectivité simulée partagée (online par défaut).
protected FakeNetwork Network { get; } = new();
protected LocalTestBase()
{
Database = new LocalDatabase(new LocalDatabaseOptions());
Database.InitializeSchemaAsync().GetAwaiter().GetResult();
ClientRepository = new LocalRepository(Database, "Client", []);
SiteRepository = new LocalRepository(Database, "Site", ["ClientId"]);
InterventionRepository = new LocalRepository(Database, "Intervention", ["ClientId", "SiteId", "DossierId"]);
RendezVousRepository = new LocalRepository(Database, "RendezVous", ["ClientId", "SiteId", "DossierId", "InterventionId"]);
DossierRepository = new LocalRepository(Database, "Dossier", ["ClientId", "SiteId"]);
DocumentMetadataRepository = new LocalRepository(Database, "DocumentMetadata", ["InterventionId"]);
PersonneRepository = new LocalRepository(Database, "Personne", ["ClientId"]);
CollaborateurRepository = new LocalRepository(Database, "Collaborateur", ["SocieteId"]);
SuiviTempsRepository = new LocalRepository(Database, "SuiviTemps", ["CollaborateurId", "InterventionId", "RendezVousId"]);
OutboxRepository = new OutboxRepository(Database);
IdentityMappingRepository = new IdentityMappingRepository(Database);
DocumentCacheRepository = new DocumentCacheRepository(Database);
NoteDocumentRepository = new NoteDocumentLocalRepository(Database);
SyncStateRepository = new SyncStateRepository(Database);
BootstrapRepository = new LocalBootstrapRepository(Database);
SuiviTempsDraftRepository = new SuiviTempsDraftRepository(Database);
RendezVousTempsValidationRepository = new RendezVousTempsValidationRepository(Database);
PreferencesUtilisateurRepository = new PreferencesUtilisateurRepository(Database);
OutboxService = new OutboxService(OutboxRepository);
ReconciliationService = new MobileIdentityReconciliationService(
Database, IdentityMappingRepository, OutboxRepository, []);
InterventionCacheRepository = new InterventionCacheRepository(Database);
ContextInterventionCacheRepository = new ContextInterventionCacheRepository(Database);
DossierStateRepository = new DossierLocalStateRepository(Database);
ClientStateRepository = new ClientLocalStateRepository(Database);
SiteStateRepository = new SiteLocalStateRepository(Database);
DossierInterventionCacheRepository = new DossierInterventionCacheRepository(Database);
ApiClient = new StubMobileApiClient();
}
public void Dispose()
{
Database.Dispose();
GC.SuppressFinalize(this);
}
/// Connectivité réseau simulée (online par défaut, modifiable par test).
protected sealed class FakeNetwork : INetworkConnectivity
{
public bool Online { get; set; } = true;
public Task IsOnlineAsync(CancellationToken cancellationToken = default)
=> Task.FromResult(Online);
}
}