Giuliano Paschoalino e49192dac1 Refatoração e atualizações de dependências
- Modificado `InvoiceIngestionService` para usar `IServiceScopeFactory`, permitindo melhor gerenciamento do ciclo de vida de dependências.
- Atualizações de pacotes em diversos projetos (`ComplianceNFs.Core`, `ComplianceNFs.Infrastructure`, `ComplianceNFs.Service`, `ComplianceNFs.Monitor`, `ComplianceNFs.Infrastructure.Tests`).
- Alterado namespace de `AuditComplianceNFsTest.cs` para `ComplianceNFs.Infrastructure.Tests`.
- Refatorações no `AttachmentRepository` e `ComplianceNFsDbContext` para simplificar código e melhorar legibilidade.
- Atualizado teste `InvoiceIngestionServiceTests` para refletir mudanças no uso de escopos de serviço.
- Melhorias gerais na arquitetura e alinhamento com boas práticas.
2025-10-01 09:10:18 -03:00

151 lines
8.7 KiB
C#

using System.Reflection;
using System.Text.Json;
using System.IO;
using NUnit.Framework;
namespace ComplianceNFs.Infrastructure.Tests
{
[TestFixture]
public class AuditComplianceNFsTest
{
private readonly string solutionRoot = @"x:\Back\Carteira x.x\Codigo\ComplianceNFs";
[Test]
public void ProjectStructure_ShouldContainAllProjects()
{
var expectedProjects = new[]
{
"ComplianceNFs.Core",
"ComplianceNFs.Infrastructure",
"ComplianceNFs.Service",
"ComplianceNFs.Monitor"
};
foreach (var proj in expectedProjects)
{
var csproj = Directory.GetFiles(solutionRoot, $"{proj}.csproj", SearchOption.AllDirectories).FirstOrDefault();
NUnit.Framework.Assert.That(csproj, Is.Not.Null, $"Project {proj} not found.");
}
}
[Test]
public void CoreProject_ShouldContainRequiredTypesAndInterfaces()
{
var coreDll = Directory.GetFiles(solutionRoot, "ComplianceNFs.Core.dll", SearchOption.AllDirectories).FirstOrDefault();
NUnit.Framework.Assert.That(coreDll, NUnit.Framework.Is.Not.Null, "ComplianceNFs.Core.dll not found. Build the solution first.");
var asm = Assembly.LoadFrom(coreDll!);
NUnit.Framework.Assert.That(asm.GetType("ComplianceNFs.Core.BuyingRecord"), NUnit.Framework.Is.Not.Null, "BuyingRecord class missing.");
NUnit.Framework.Assert.That(asm.GetType("ComplianceNFs.Core.EnergyInvoice"), NUnit.Framework.Is.Not.Null, "EnergyInvoice class missing.");
NUnit.Framework.Assert.That(asm.GetType("ComplianceNFs.Core.InvoiceStatus"), NUnit.Framework.Is.Not.Null, "InvoiceStatus enum missing.");
var interfaces = new[]
{
"IMailListener",
"IXmlParser",
"IPdfParser",
"IAccessDbRepository",
"IAttachmentRepository"
};
foreach (var iface in interfaces)
{
NUnit.Framework.Assert.That(asm.GetType($"ComplianceNFs.Core.{iface}"), NUnit.Framework.Is.Not.Null, $"Interface {iface} missing.");
}
}
[Test]
public void InfrastructureProject_ShouldImplementCoreInterfaces()
{
var infraDll = Directory.GetFiles(solutionRoot, "ComplianceNFs.Infrastructure.dll", SearchOption.AllDirectories).FirstOrDefault();
var coreDll = Directory.GetFiles(solutionRoot, "ComplianceNFs.Core.dll", SearchOption.AllDirectories).FirstOrDefault();
NUnit.Framework.Assert.That(infraDll, NUnit.Framework.Is.Not.Null, "ComplianceNFs.Infrastructure.dll not found.");
NUnit.Framework.Assert.That(coreDll, NUnit.Framework.Is.Not.Null, "ComplianceNFs.Core.dll not found.");
var infraAsm = Assembly.LoadFrom(infraDll!);
var coreAsm = Assembly.LoadFrom(coreDll!);
var interfaceImpls = new (string iface, string impl)[]
{
("IMailListener", "MailListener"),
("IXmlParser", "XmlParser"),
("IPdfParser", "PdfParser"),
("IAccessDbRepository", "AccessDbRepository"),
("IAttachmentRepository", "AttachmentRepository")
};
foreach (var (iface, impl) in interfaceImpls)
{
var ifaceType = coreAsm.GetType($"ComplianceNFs.Core.{iface}");
var implType = infraAsm.GetType($"ComplianceNFs.Infrastructure.{impl}");
NUnit.Framework.Assert.That(implType, NUnit.Framework.Is.Not.Null, $"Implementation {impl} missing.");
NUnit.Framework.Assert.That(ifaceType, NUnit.Framework.Is.Not.Null, $"Interface {iface} missing in core assembly.");
NUnit.Framework.Assert.That(ifaceType!.IsAssignableFrom(implType!), NUnit.Framework.Is.True, $"{impl} does not implement {iface}.");
}
var archiver = infraAsm.GetTypes().FirstOrDefault(t =>
t.Name.Contains("Archiver") || t.Name.Contains("FileArchiver"));
NUnit.Framework.Assert.That(archiver, NUnit.Framework.Is.Not.Null, "FileArchiver or equivalent not found.");
}
[Test]
public void ServiceProject_ShouldUseGenericHostAndRegisterServices()
{
var serviceDir = Directory.GetDirectories(solutionRoot, "ComplianceNFs.Service", SearchOption.TopDirectoryOnly).FirstOrDefault();
NUnit.Framework.Assert.That(serviceDir, NUnit.Framework.Is.Not.Null, "ComplianceNFs.Service directory not found.");
var programFile = Directory.GetFiles(serviceDir!, "Program.cs", SearchOption.AllDirectories).FirstOrDefault();
NUnit.Framework.Assert.That(programFile, NUnit.Framework.Is.Not.Null, "Program.cs not found in Service project.");
var code = File.ReadAllText(programFile!);
NUnit.Framework.Assert.That(code.Contains("Host.CreateDefaultBuilder"), NUnit.Framework.Is.True, "Generic Host not used.");
NUnit.Framework.Assert.That(code.Contains("ConfigureServices"), NUnit.Framework.Is.True, "DI registration missing.");
NUnit.Framework.Assert.That(code.Contains("MailListener"), NUnit.Framework.Is.True, "MailListener not referenced in startup.");
NUnit.Framework.Assert.That(code.Contains("appsettings.json"), NUnit.Framework.Is.True, "appsettings.json not referenced.");
}
[Test]
public void MonitorProject_ShouldContainWPFArtifacts()
{
var monitorDir = Directory.GetDirectories(solutionRoot, "ComplianceNFs.Monitor", SearchOption.TopDirectoryOnly).FirstOrDefault();
NUnit.Framework.Assert.That(monitorDir, NUnit.Framework.Is.Not.Null, "ComplianceNFs.Monitor directory not found.");
NUnit.Framework.Assert.That(File.Exists(Path.Combine(monitorDir!, "MainWindow.xaml")), NUnit.Framework.Is.True, "MainWindow.xaml missing.");
NUnit.Framework.Assert.That(File.Exists(Path.Combine(monitorDir!, "MonitorViewModel.cs")), NUnit.Framework.Is.True, "MonitorViewModel.cs missing.");
var mainWindowCode = File.ReadAllText(Path.Combine(monitorDir!, "MainWindow.xaml"));
NUnit.Framework.Assert.That(mainWindowCode.Contains("Force Scan"), NUnit.Framework.Is.True, "Force Scan button not found in MainWindow.xaml.");
}
[Test]
public void AppSettings_ShouldContainRequiredKeys()
{
var appsettings = Directory.GetFiles(solutionRoot, "appsettings.json", SearchOption.AllDirectories).FirstOrDefault();
NUnit.Framework.Assert.That(appsettings, NUnit.Framework.Is.Not.Null, "appsettings.json not found.");
var json = File.ReadAllText(appsettings!);
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
NUnit.Framework.Assert.That(root.TryGetProperty("AccessConnectionString", out _), NUnit.Framework.Is.True, "AccessConnectionString missing.");
NUnit.Framework.Assert.That(root.TryGetProperty("PostgresConnectionString", out _), NUnit.Framework.Is.True, "PostgresConnectionString missing.");
NUnit.Framework.Assert.That(root.TryGetProperty("Mail", out _), NUnit.Framework.Is.True, "Mail settings missing.");
NUnit.Framework.Assert.That(root.TryGetProperty("Tolerances", out _), NUnit.Framework.Is.True, "Tolerances missing.");
NUnit.Framework.Assert.That(root.TryGetProperty("ArchiveBasePath", out _), NUnit.Framework.Is.True, "ArchiveBasePath missing.");
}
[Test]
public void MatchingService_ShouldApplyTolerances()
{
// This is a stub: in a real test, instantiate MatchingService and test with in-memory data.
NUnit.Framework.Assert.Pass("Stub: Implement in-memory test for MatchingService tolerances (±1%, ±0.5%).");
}
[Test]
public void ComplianceService_ShouldComputeImpliedTaxWithinTolerance()
{
// This is a stub: in a real test, instantiate ComplianceService and test with in-memory data.
NUnit.Framework.Assert.Pass("Stub: Implement in-memory test for ComplianceService implied tax logic (±1%).");
}
[Test]
public void NotificationService_ShouldSendEmailOnMismatch()
{
// This is a stub: in a real test, mock NotificationService and verify email send on mismatch.
NUnit.Framework.Assert.Pass("Stub: Implement NotificationService email send test.");
}
[Test]
public void ArchivingService_ShouldMoveFilesToStatusFolders()
{
// This is a stub: in a real test, mock file system and verify archiving logic.
NUnit.Framework.Assert.Pass("Stub: Implement ArchivingService file move test.");
}
}
}