using System.Text; using Khaemhat.Core.Treasury; namespace Khaemhat.Fiscal.Italy.Tests; /// The CBI current account statement (issue #71): balances, movements and their description lines. [TestClass] public sealed class CbiStatementReaderTests { private static readonly CbiStatementReader Reader = new(); [TestMethod] public void Read_SampleStatement_WithBalancesMovementsAndDescriptions() { var statement = Read(string.Join("\r\n", Sample())); Assert.AreEqual(StatementFormat.Cbi, Reader.Format); Assert.AreEqual(StatementFormat.Cbi, statement.Format); Assert.AreEqual("IT60X0542811101000000123456", statement.AccountIban); Assert.AreEqual("EUR", statement.Currency); Assert.AreEqual(1000m, statement.OpeningBalance); Assert.AreEqual(2207.50m, statement.ClosingBalance); Assert.AreEqual(new DateOnly(2026, 10, 31), statement.FromDate); Assert.AreEqual(new DateOnly(2026, 11, 3), statement.ToDate); Assert.HasCount(3, statement.Movements); Assert.AreEqual(statement.ClosingBalance, statement.OpeningBalance + statement.Movements.Sum(m => m.Amount)); var first = statement.Movements[0]; Assert.AreEqual(new DateOnly(2026, 11, 2), first.BookingDate); Assert.AreEqual(new DateOnly(2026, 11, 3), first.ValueDate); Assert.AreEqual(1220.50m, first.Amount); Assert.AreEqual("48", first.BankCode); Assert.AreEqual("CRO123456", first.Reference); Assert.AreEqual("BONIFICO A VOSTRO FAVORE DA CLIENTE UNO SPA SALDO FT 388", first.Description); var second = statement.Movements[1]; Assert.AreEqual(-12.50m, second.Amount); Assert.AreEqual("COMMISSIONI", second.Description); Assert.IsNull(second.Reference); // The amount with the comma as some banks write it. Assert.AreEqual(-0.50m, statement.Movements[2].Amount); } [TestMethod] public void Read_LinesWithoutLeadingSpace_AndWithoutLineBreaks() { var trimmed = string.Join("\n", Sample().Select(l => l[1..])); Assert.HasCount(3, Read(trimmed).Movements); var stream = string.Concat(Sample()); var statement = Read(stream); Assert.HasCount(3, statement.Movements); Assert.AreEqual(2207.50m, statement.ClosingBalance); } [TestMethod] public void Recognizes_TheRhHeader_WithOrWithoutLeadingSpace() { var lines = Sample(); Assert.IsTrue(Reader.Recognizes(Encoding.ASCII.GetBytes(string.Join("\r\n", lines)))); Assert.IsTrue(Reader.Recognizes(Encoding.ASCII.GetBytes("\r\n" + string.Join("\r\n", lines.Select(l => l[1..]))))); Assert.IsTrue(Reader.Recognizes(Encoding.ASCII.GetBytes(string.Concat(lines)))); Assert.IsFalse(Reader.Recognizes(Encoding.ASCII.GetBytes(" RH short\r\n"))); Assert.IsFalse(Reader.Recognizes(Encoding.ASCII.GetBytes("Data;Descrizione;Importo\r\n"))); Assert.IsFalse(Reader.Recognizes(Encoding.ASCII.GetBytes(" IB" + new string(' ', 117)))); } [TestMethod] public void Read_Errors_AreBankFileExceptions() { var lines = Sample(); Assert.ThrowsExactly(() => Read(string.Join("\r\n", lines.Skip(1)))); var badSign = lines.ToArray(); badSign[2] = Put(badSign[2], 26, "X"); Assert.ThrowsExactly(() => Read(string.Join("\r\n", badSign))); var badAmount = lines.ToArray(); badAmount[2] = Put(badAmount[2], 27, "00000ABC0000000"); Assert.ThrowsExactly(() => Read(string.Join("\r\n", badAmount))); var badDate = lines.ToArray(); badDate[2] = Put(badDate[2], 20, "321326"); Assert.ThrowsExactly(() => Read(string.Join("\r\n", badDate))); var orphan = lines.ToList(); orphan.Insert(2, Record("63", (4, "0000001"), (11, "001"), (14, "ORFANA"))); Assert.ThrowsExactly(() => Read(string.Join("\r\n", orphan))); } private static List Sample() => [ Record("RH", (4, "05428"), (9, "A1B2C"), (14, "041126"), (20, "ESTRATTO-2026-11"), (114, "E")), Record( "61", (4, "0000001"), (52, "X"), (53, "05428"), (58, "11101"), (63, "000000123456"), (75, "EUR"), (78, "311026"), (84, "C"), (85, "000000000100000"), (100, "IT"), (102, "60")), Record( "62", (4, "0000001"), (11, "001"), (14, "031126"), (20, "021126"), (26, "C"), (27, "000000000122050"), (42, "48"), (44, "00"), (62, "CRO123456"), (87, "BONIFICO A VOSTRO FAVORE")), Record("63", (4, "0000001"), (11, "001"), (14, "DA CLIENTE UNO SPA")), Record("63", (4, "0000001"), (11, "001"), (14, "SALDO FT 388")), Record("62", (4, "0000001"), (11, "002"), (14, "031126"), (20, "031126"), (26, "D"), (27, "000000000001250"), (42, "26"), (87, "COMMISSIONI")), Record("62", (4, "0000001"), (11, "003"), (14, "031126"), (20, "031126"), (26, "D"), (27, " 0,50"), (42, "26"), (87, "BOLLO")), Record("64", (4, "0000001"), (11, "EUR"), (14, "031126"), (20, "C"), (21, "000000000220750"), (36, "C"), (37, "000000000220750")), Record("EF", (4, "05428"), (9, "A1B2C"), (14, "041126"), (20, "ESTRATTO-2026-11"), (46, "0000001"), (83, "0000009"), (114, "E")), ]; private static string Record(string type, params (int Position, string Value)[] fields) { var line = " " + type + new string(' ', 117); foreach (var (position, value) in fields) { line = Put(line, position, value); } return line; } private static string Put(string line, int position, string value) => line[..(position - 1)] + value + line[(position - 1 + value.Length)..]; private static StatementFile Read(string text) { using var stream = new MemoryStream(Encoding.Latin1.GetBytes(text)); return Reader.Read(stream); } }