/ GUNRPG.Tests / InfrastructureServiceExtensionsTests.cs
InfrastructureServiceExtensionsTests.cs
1 using GUNRPG.Infrastructure; 2 using GUNRPG.Infrastructure.Persistence; 3 using LiteDB; 4 using Microsoft.Extensions.Configuration; 5 using Microsoft.Extensions.DependencyInjection; 6 7 namespace GUNRPG.Tests; 8 9 public class InfrastructureServiceExtensionsTests : IDisposable 10 { 11 private readonly string _tempDbPath; 12 private readonly string _rootDir; 13 private readonly ServiceProvider _provider; 14 15 public InfrastructureServiceExtensionsTests() 16 { 17 _rootDir = Path.Combine( 18 "~/.gunrpg-tests", 19 Guid.NewGuid().ToString("N")); 20 21 _tempDbPath = Path.Combine( 22 _rootDir, 23 "data", 24 "combat_sessions.db"); 25 26 var configuration = new ConfigurationBuilder() 27 .AddInMemoryCollection(new Dictionary<string, string?> 28 { 29 [$"{StorageOptions.SectionName}:{nameof(StorageOptions.Provider)}"] = "LiteDB", 30 [$"{StorageOptions.SectionName}:{nameof(StorageOptions.LiteDbConnectionString)}"] = _tempDbPath 31 }) 32 .Build(); 33 34 var services = new ServiceCollection(); 35 services.AddCombatSessionStore(configuration); 36 37 _provider = services.BuildServiceProvider(); 38 } 39 40 [Fact] 41 public void AddCombatSessionStore_CreatesLiteDbDirectory() 42 { 43 // Resolve LiteDatabase to trigger directory creation and ensure the instance is usable. 44 var database = _provider.GetRequiredService<LiteDatabase>(); 45 Assert.NotNull(database); 46 47 var directory = Path.GetDirectoryName(PathHelpers.ExpandHomePath(_tempDbPath)); 48 Assert.False(string.IsNullOrEmpty(directory)); 49 Assert.True(Directory.Exists(directory)); 50 } 51 52 public void Dispose() 53 { 54 _provider.Dispose(); 55 56 var expandedRoot = PathHelpers.ExpandHomePath(_rootDir); 57 var expandedPath = PathHelpers.ExpandHomePath(_tempDbPath); 58 59 if (File.Exists(expandedPath)) 60 File.Delete(expandedPath); 61 62 if (!string.IsNullOrEmpty(expandedRoot) && Directory.Exists(expandedRoot)) 63 { 64 Directory.Delete(expandedRoot, recursive: true); 65 } 66 } 67 }