using Microsoft.EntityFrameworkCore; using PowderCoating.Core.Entities; using PowderCoating.Infrastructure.Data; using PowderCoating.Infrastructure.Services; namespace PowderCoating.UnitTests; public class PlatformSettingsServiceTests { [Fact] public async Task GetAsync_ReturnsStoredValue() { await using var context = CreateContext(); context.PlatformSettings.Add(new PlatformSetting { Key = "BrandName", Value = "Powder Coating Pro" }); await context.SaveChangesAsync(); var service = new PlatformSettingsService(context); var value = await service.GetAsync("BrandName"); Assert.Equal("Powder Coating Pro", value); } [Fact] public async Task GetAsync_WhenMissing_ReturnsNull() { await using var context = CreateContext(); var service = new PlatformSettingsService(context); var value = await service.GetAsync("MissingKey"); Assert.Null(value); } [Fact] public async Task SetAsync_WhenSettingExists_UpdatesValueAndAuditFields() { await using var context = CreateContext(); context.PlatformSettings.Add(new PlatformSetting { Key = "TrialsEnabled", Value = "true", UpdatedBy = "old-user" }); await context.SaveChangesAsync(); var service = new PlatformSettingsService(context); await service.SetAsync("TrialsEnabled", "false", "superadmin@example.com"); var setting = await context.PlatformSettings.SingleAsync(); Assert.Equal("false", setting.Value); Assert.Equal("superadmin@example.com", setting.UpdatedBy); Assert.True(setting.UpdatedAt.HasValue); } [Fact] public async Task SetAsync_WhenSettingMissing_InsertsRow() { await using var context = CreateContext(); var service = new PlatformSettingsService(context); await service.SetAsync("SupportEmail", "help@example.com", "setup"); var setting = await context.PlatformSettings.SingleAsync(); Assert.Equal("SupportEmail", setting.Key); Assert.Equal("help@example.com", setting.Value); Assert.Equal("setup", setting.UpdatedBy); } [Fact] public async Task GetAllAsync_OrdersByGroupThenKey() { await using var context = CreateContext(); context.PlatformSettings.AddRange( new PlatformSetting { Key = "Zeta", GroupName = "Billing", Value = "1" }, new PlatformSetting { Key = "Alpha", GroupName = "Billing", Value = "2" }, new PlatformSetting { Key = "Bravo", GroupName = "Alerts", Value = "3" }); await context.SaveChangesAsync(); var service = new PlatformSettingsService(context); var settings = await service.GetAllAsync(); Assert.Equal(new[] { "Bravo", "Alpha", "Zeta" }, settings.Select(s => s.Key).ToArray()); } private static ApplicationDbContext CreateContext() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; return new ApplicationDbContext(options); } }