using ClosedXML.Excel; using TrafagSalesExporter.Models; using TrafagSalesExporter.Services; namespace TrafagSalesExporter.Tests; public class ExcelExportServiceTests { [Fact] public void CreateConsolidatedExcelFile_Uses_Germany_Finance_Response_Rules() { var outputDirectory = Path.Combine(Path.GetTempPath(), $"trafag-export-{Guid.NewGuid():N}"); var service = new ExcelExportService(); var records = new List { CreateGermanyRecord("Normal GmbH", "Deutschland", "RE250001", 100m), CreateGermanyRecord("Trafag AG", "Schweiz", "RE250002", 40m), CreateGermanyRecord("Magnetic Sense GmbH", "Deutschland", "RE250003", 30m), CreateGermanyRecord("Normal GmbH", "Deutschland", "GS2510096", 20m), CreateGermanyRecord("Normal GmbH", "Deutschland", "GS2510095", 10m) }; try { var path = service.CreateConsolidatedExcelFile(outputDirectory, new DateTime(2026, 5, 20), records); using var workbook = new XLWorkbook(path); var summary = workbook.Worksheet("Finance Summary"); var deSummaryRow = summary.RowsUsed() .Where(row => row.RowNumber() > 4) .Single(row => row.Cell(1).GetValue() == 2025 && row.Cell(2).GetString() == "DE"); Assert.Equal(2, deSummaryRow.Cell(4).GetValue()); Assert.Equal(80m, deSummaryRow.Cell(5).GetValue()); Assert.Equal(3, deSummaryRow.Cell(6).GetValue()); var sales = workbook.Worksheet("Sales"); var includedGermanyRows = sales.RowsUsed() .Skip(1) .Where(row => row.Cell(43).GetValue() == 2025) .Where(row => row.Cell(44).GetString() == "DE") .Where(row => row.Cell(48).GetString() == "TRUE") .ToList(); Assert.Equal(2, includedGermanyRows.Count); Assert.Equal(80m, includedGermanyRows.Sum(row => row.Cell(46).GetValue())); var details = workbook.Worksheet("Finance Details"); var includedGermanyDetailRows = details.RowsUsed() .Where(row => row.RowNumber() > 4) .Where(row => row.Cell(1).GetValue() == 2025) .Where(row => row.Cell(2).GetString() == "DE") .ToList(); Assert.Equal(2, includedGermanyDetailRows.Count); Assert.Equal(80m, includedGermanyDetailRows.Sum(row => row.Cell(5).GetValue())); Assert.All(includedGermanyDetailRows, row => Assert.Equal("Sales Price/Value", row.Cell(6).GetString())); } finally { if (Directory.Exists(outputDirectory)) Directory.Delete(outputDirectory, recursive: true); } } private static SalesRecord CreateGermanyRecord(string customerName, string customerCountry, string invoiceNumber, decimal value) => new() { ExtractionDate = new DateTime(2026, 5, 20), Tsc = "TRDE", Land = "Deutschland", InvoiceNumber = invoiceNumber, PositionOnInvoice = 1, CustomerName = customerName, CustomerCountry = customerCountry, SalesPriceValue = value, SalesCurrency = "EUR", CompanyCurrency = "EUR", DocumentType = "Alphaplan Excel" }; }