//! The sign convention is the whole file: a line's direction is from the //! ACCOUNT's point of view, so a card spend is `out` while a card's printed //! balance (what is OWED) moves the other way. use super::*; use crate::constants::LineDirection::{In, Out}; use crate::constants::StatementKind::{Bank, CreditCard}; /// A line, with the boring fields filled in. fn line(line_no: f64, amount_minor: f64, direction: LineDirection) -> CheckLine { CheckLine { line_no, amount_minor, direction, balance_after_minor: None, ignored: false, } } fn with_balance(mut line: CheckLine, balance_after_minor: f64) -> CheckLine { line.balance_after_minor = Some(balance_after_minor); line } fn ignored(mut line: CheckLine) -> CheckLine { line.ignored = true; line } fn bank_statement() -> CheckInput { CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_150_000.0), lines: vec![line(1.0, 200_000.0, In), line(2.0, 50_000.0, Out)], } } // ── a bank statement ───────────────────────────────────────────────────────── /// RULE: A bank statement is checked by adding its money in and taking away its money out from the opening balance. #[test] fn a_bank_statement_adds_up_opening_plus_in_minus_out() { assert_eq!( check_statement(&bank_statement()).expect("a readable statement"), CheckResult { balance_check: BalanceCheck::Ok, sum_in_minor: 200_000.0, sum_out_minor: 50_000.0, expected_closing_minor: Some(1_150_000.0), first_break_line_no: None, } ); } #[test] fn says_mismatch_when_the_printed_closing_disagrees_and_by_how_much() { let mut input = bank_statement(); input.closing_minor = Some(1_150_001.0); let result = check_statement(&input).expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Mismatch); assert_eq!(result.expected_closing_minor, Some(1_150_000.0)); } #[test] fn excludes_an_ignored_line_from_both_sums_and_the_arithmetic() { let mut input = bank_statement(); input.lines.push(ignored(line(3.0, 999_999.0, Out))); let result = check_statement(&input).expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Ok); assert_eq!(result.sum_out_minor, 50_000.0); } // ── a credit card ──────────────────────────────────────────────────────────── /// A spend RAISES what is owed; a payment lowers it. #[test] fn a_card_adds_up_the_other_way_opening_owed_plus_out_minus_in() { let result = check_statement(&CheckInput { kind: CreditCard, opening_minor: Some(1_250_000.0), closing_minor: Some(980_000.0), lines: vec![line(1.0, 130_000.0, Out), line(2.0, 400_000.0, In)], }) .expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Ok); assert_eq!(result.sum_in_minor, 400_000.0); assert_eq!(result.sum_out_minor, 130_000.0); assert_eq!(result.expected_closing_minor, Some(980_000.0)); } /// RULE: A credit card statement read as if it were a bank account fails the balance check rather than quietly adding up wrong. /// The failure this file exists for: read a card as a bank and every statement /// "adds up" at exactly twice the wrong number. #[test] fn does_not_silently_pass_a_card_read_with_the_banks_sign_convention() { let card_lines = vec![line(1.0, 130_000.0, Out), line(2.0, 400_000.0, In)]; let as_bank = check_statement(&CheckInput { kind: Bank, opening_minor: Some(1_250_000.0), closing_minor: Some(980_000.0), lines: card_lines, }) .expect("a readable statement"); assert_eq!(as_bank.balance_check, BalanceCheck::Mismatch); // Node vector: the wrong convention answers 1_520_000, which is the right // answer plus twice the period's turnover. assert_eq!(as_bank.expected_closing_minor, Some(1_520_000.0)); } // ── the running balance ────────────────────────────────────────────────────── /// RULE: When a statement's running balance stops agreeing, the check names the first line where it broke. #[test] fn localises_the_first_line_that_breaks_the_run() { let result = check_statement(&CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_150_000.0), lines: vec![ with_balance(line(1.0, 200_000.0, In), 1_200_000.0), // The reader read this amount wrong; the printed running balance // says so. with_balance(line(2.0, 50_000.0, Out), 1_149_000.0), with_balance(line(3.0, 1.0, In), 1_149_001.0), ], }) .expect("a readable statement"); assert_eq!(result.first_break_line_no, Some(2.0)); assert_eq!(result.balance_check, BalanceCheck::Mismatch); } #[test] fn walks_lines_in_line_no_order_however_they_arrive() { let result = check_statement(&CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_150_000.0), lines: vec![ with_balance(line(2.0, 50_000.0, Out), 1_150_000.0), with_balance(line(1.0, 200_000.0, In), 1_200_000.0), ], }) .expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Ok); assert_eq!(result.first_break_line_no, None); } /// An opening-balance ROW prints the balance and moves no money. #[test] fn carries_an_ignored_lines_printed_balance_forward_without_booking_it() { let result = check_statement(&CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_200_000.0), lines: vec![ ignored(with_balance(line(1.0, 1.0, In), 1_000_000.0)), with_balance(line(2.0, 200_000.0, In), 1_200_000.0), ], }) .expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Ok); assert_eq!(result.first_break_line_no, None); assert_eq!(result.sum_in_minor, 200_000.0); } /// Two reading errors that cancel: the totals agree and the paper does not. #[test] fn mismatches_on_a_broken_run_even_when_the_closing_balance_happens_to_agree() { let result = check_statement(&CheckInput { kind: Bank, opening_minor: Some(0.0), closing_minor: Some(0.0), lines: vec![ with_balance(line(1.0, 100.0, In), 999.0), with_balance(line(2.0, 100.0, Out), 0.0), ], }) .expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Mismatch); assert_eq!(result.first_break_line_no, Some(1.0)); } /// A "Total spent this period" row is ignored (not booked) and its printed /// number has nothing to do with the running balance — unlike an opening row, /// it need not happen to already agree. Comparing it anyway would fail an /// otherwise perfectly readable statement. #[test] fn does_not_compare_an_ignored_summary_rows_stray_balance_against_the_run() { let result = check_statement(&CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_150_000.0), lines: vec![ with_balance(line(1.0, 200_000.0, In), 1_200_000.0), ignored(with_balance(line(2.0, 999_999.0, Out), 424_242.0)), with_balance(line(3.0, 50_000.0, Out), 1_150_000.0), ], }) .expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Ok); assert_eq!(result.first_break_line_no, None); assert_eq!(result.sum_in_minor, 200_000.0); assert_eq!(result.sum_out_minor, 50_000.0); } #[test] fn localises_the_running_break_on_a_credit_card_statement_too() { let result = check_statement(&CheckInput { kind: CreditCard, opening_minor: Some(1_250_000.0), closing_minor: Some(980_000.0), lines: vec![ with_balance(line(1.0, 130_000.0, Out), 1_380_000.0), with_balance(line(2.0, 400_000.0, In), 980_000.0), ], }) .expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Ok); assert_eq!(result.first_break_line_no, None); } // ── what it cannot know ────────────────────────────────────────────────────── #[test] fn is_unknown_when_the_paper_printed_no_opening_balance() { let mut input = bank_statement(); input.opening_minor = None; let result = check_statement(&input).expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Unknown); assert_eq!(result.expected_closing_minor, None); assert_eq!(result.first_break_line_no, None); } #[test] fn is_unknown_with_an_opening_and_no_closing_but_still_sums() { let mut input = bank_statement(); input.closing_minor = None; let result = check_statement(&input).expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Unknown); assert_eq!(result.expected_closing_minor, Some(1_150_000.0)); assert_eq!(result.sum_in_minor, 200_000.0); } #[test] fn is_unknown_not_ok_on_a_statement_with_no_lines_at_all() { let result = check_statement(&CheckInput { kind: Bank, opening_minor: None, closing_minor: None, lines: vec![], }) .expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Unknown); assert_eq!(result.sum_in_minor, 0.0); assert_eq!(result.sum_out_minor, 0.0); } #[test] fn is_ok_on_a_statement_with_no_lines_whose_balances_agree() { let result = check_statement(&CheckInput { kind: Bank, opening_minor: Some(500.0), closing_minor: Some(500.0), lines: vec![], }) .expect("a readable statement"); assert_eq!(result.balance_check, BalanceCheck::Ok); } // ── loud about nonsense ────────────────────────────────────────────────────── /// RULE: An amount that is not a whole number of paise is refused outright, never rounded. #[test] fn refuses_a_non_integer_amount() { let mut input = bank_statement(); input.lines = vec![line(1.0, 1.5, In)]; let error = check_statement(&input).expect_err("refused"); assert_eq!(error.kind(), "TypeError"); } /// RULE: A statement line's amount is always positive; whether the money came in or went out is stated separately. #[test] fn refuses_an_amount_that_is_not_positive_because_direction_carries_the_sign() { let mut input = bank_statement(); input.lines = vec![line(1.0, -1.0, In)]; let error = check_statement(&input).expect_err("refused"); assert_eq!(error.kind(), "RangeError"); } #[test] fn refuses_two_lines_claiming_one_line_number() { let mut input = bank_statement(); input.lines = vec![line(1.0, 100.0, In), line(1.0, 200.0, Out)]; let error = check_statement(&input).expect_err("refused"); assert_eq!(error.kind(), "RangeError"); assert_eq!( error.to_string(), "checkStatement: two lines claim line_no 1" ); } #[test] fn refuses_a_non_integer_printed_balance() { let mut input = bank_statement(); input.lines = vec![with_balance(line(1.0, 100.0, In), 1.5)]; let error = check_statement(&input).expect_err("refused"); assert_eq!(error.kind(), "TypeError"); } #[test] fn refuses_a_non_integer_opening_balance() { let mut input = bank_statement(); input.opening_minor = Some(1_000_000.5); assert_eq!( check_statement(&input).expect_err("refused").kind(), "TypeError" ); } #[test] fn refuses_a_non_integer_closing_balance() { let mut input = bank_statement(); input.closing_minor = Some(1_150_000.5); assert_eq!( check_statement(&input).expect_err("refused").kind(), "TypeError" ); } #[test] fn refuses_a_non_integer_line_no() { let mut input = bank_statement(); input.lines = vec![line(1.5, 100.0, In)]; assert_eq!( check_statement(&input).expect_err("refused").kind(), "TypeError" ); } #[test] fn refuses_a_non_positive_line_no() { let mut input = bank_statement(); input.lines = vec![line(0.0, 100.0, In)]; assert_eq!( check_statement(&input).expect_err("refused").kind(), "RangeError" ); } // ── Node vectors ───────────────────────────────────────────────────────────── /// Node vectors: eighteen whole readings, each answer read out of the /// TypeScript rather than reasoned about. /// /// The three that surprise: a card read as a BANK answers 1_520_000 (twice the /// turnover away from the truth, and the mistake this module exists to catch); /// a statement with an opening, a closing and NO lines is `ok` because zero /// movement is a real answer; and a NEGATIVE opening balance is perfectly legal /// — an overdrawn account is still an account. #[test] fn whole_readings_match_node() { struct Case { input: CheckInput, expected: CheckResult, } let result = |balance_check, sum_in_minor, sum_out_minor, expected_closing_minor, first_break_line_no| CheckResult { balance_check, sum_in_minor, sum_out_minor, expected_closing_minor, first_break_line_no, }; let cases = vec![ Case { input: CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_150_000.0), lines: vec![line(1.0, 200_000.0, In), line(2.0, 50_000.0, Out)], }, expected: result( BalanceCheck::Ok, 200_000.0, 50_000.0, Some(1_150_000.0), None, ), }, Case { input: CheckInput { kind: CreditCard, opening_minor: Some(1_250_000.0), closing_minor: Some(980_000.0), lines: vec![line(1.0, 130_000.0, Out), line(2.0, 400_000.0, In)], }, expected: result( BalanceCheck::Ok, 400_000.0, 130_000.0, Some(980_000.0), None, ), }, Case { input: CheckInput { kind: Bank, opening_minor: Some(1_250_000.0), closing_minor: Some(980_000.0), lines: vec![line(1.0, 130_000.0, Out), line(2.0, 400_000.0, In)], }, expected: result( BalanceCheck::Mismatch, 400_000.0, 130_000.0, Some(1_520_000.0), None, ), }, Case { input: CheckInput { kind: Bank, opening_minor: None, closing_minor: Some(1_150_000.0), lines: vec![line(1.0, 200_000.0, In)], }, expected: result(BalanceCheck::Unknown, 200_000.0, 0.0, None, None), }, Case { input: CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: None, lines: vec![line(1.0, 200_000.0, In)], }, expected: result( BalanceCheck::Unknown, 200_000.0, 0.0, Some(1_200_000.0), None, ), }, Case { input: CheckInput { kind: Bank, opening_minor: None, closing_minor: None, lines: vec![], }, expected: result(BalanceCheck::Unknown, 0.0, 0.0, None, None), }, Case { input: CheckInput { kind: Bank, opening_minor: Some(500.0), closing_minor: Some(500.0), lines: vec![], }, expected: result(BalanceCheck::Ok, 0.0, 0.0, Some(500.0), None), }, Case { input: CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_150_000.0), lines: vec![ with_balance(line(1.0, 200_000.0, In), 1_200_000.0), with_balance(line(2.0, 50_000.0, Out), 1_149_000.0), with_balance(line(3.0, 1.0, In), 1_149_001.0), ], }, expected: result( BalanceCheck::Mismatch, 200_001.0, 50_000.0, Some(1_150_001.0), Some(2.0), ), }, Case { input: CheckInput { kind: Bank, opening_minor: Some(0.0), closing_minor: Some(0.0), lines: vec![ with_balance(line(1.0, 100.0, In), 999.0), with_balance(line(2.0, 100.0, Out), 0.0), ], }, expected: result(BalanceCheck::Mismatch, 100.0, 100.0, Some(0.0), Some(1.0)), }, Case { input: CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_200_000.0), lines: vec![ ignored(with_balance(line(1.0, 1.0, In), 1_000_000.0)), with_balance(line(2.0, 200_000.0, In), 1_200_000.0), ], }, expected: result(BalanceCheck::Ok, 200_000.0, 0.0, Some(1_200_000.0), None), }, Case { input: CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_150_000.0), lines: vec![ with_balance(line(1.0, 200_000.0, In), 1_200_000.0), ignored(with_balance(line(2.0, 999_999.0, Out), 424_242.0)), with_balance(line(3.0, 50_000.0, Out), 1_150_000.0), ], }, expected: result( BalanceCheck::Ok, 200_000.0, 50_000.0, Some(1_150_000.0), None, ), }, Case { input: CheckInput { kind: CreditCard, opening_minor: Some(1_250_000.0), closing_minor: Some(980_000.0), lines: vec![ with_balance(line(1.0, 130_000.0, Out), 1_380_000.0), with_balance(line(2.0, 400_000.0, In), 980_000.0), ], }, expected: result( BalanceCheck::Ok, 400_000.0, 130_000.0, Some(980_000.0), None, ), }, Case { input: CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_150_000.0), lines: vec![ with_balance(line(2.0, 50_000.0, Out), 1_150_000.0), with_balance(line(1.0, 200_000.0, In), 1_200_000.0), ], }, expected: result( BalanceCheck::Ok, 200_000.0, 50_000.0, Some(1_150_000.0), None, ), }, Case { input: CheckInput { kind: CreditCard, opening_minor: Some(0.0), closing_minor: Some(0.0), lines: vec![], }, expected: result(BalanceCheck::Ok, 0.0, 0.0, Some(0.0), None), }, Case { input: CheckInput { kind: CreditCard, opening_minor: None, closing_minor: Some(5.0), lines: vec![line(9.0, 5.0, Out), line(3.0, 7.0, In)], }, expected: result(BalanceCheck::Unknown, 7.0, 5.0, None, None), }, Case { input: CheckInput { kind: Bank, opening_minor: Some(-1000.0), closing_minor: Some(-500.0), lines: vec![with_balance(line(1.0, 500.0, In), -500.0)], }, expected: result(BalanceCheck::Ok, 500.0, 0.0, Some(-500.0), None), }, Case { input: CheckInput { kind: CreditCard, opening_minor: Some(100.0), closing_minor: Some(100.0), lines: vec![ with_balance(line(1.0, 50.0, Out), 150.0), with_balance(line(2.0, 50.0, In), 100.0), ], }, expected: result(BalanceCheck::Ok, 50.0, 50.0, Some(100.0), None), }, Case { input: CheckInput { kind: Bank, opening_minor: Some(1_000_000.0), closing_minor: Some(1_150_001.0), lines: vec![line(1.0, 200_000.0, In), line(2.0, 50_000.0, Out)], }, expected: result( BalanceCheck::Mismatch, 200_000.0, 50_000.0, Some(1_150_000.0), None, ), }, ]; assert_eq!(cases.len(), 18); for (index, case) in cases.iter().enumerate() { assert_eq!( check_statement(&case.input).expect("a readable statement"), case.expected, "case {index}" ); } } /// Node vectors: every refusal sentence, with `String(n)` doing the rendering. /// /// The order of the guards is part of the contract — `-1e21` is an INTEGER, so /// it passes the type guard and is refused by the positive guard instead, and a /// caller reading the message learns which of the two it broke. #[test] fn the_refusal_sentences_match_node() { let fractions: &[(f64, &str)] = &[ (10.5, "10.5"), (-10.5, "-10.5"), (0.01, "0.01"), (1e-7, "1e-7"), (1234567.89, "1234567.89"), (f64::NAN, "NaN"), (f64::INFINITY, "Infinity"), (f64::NEG_INFINITY, "-Infinity"), (0.1 + 0.2, "0.30000000000000004"), (2.5e-10, "2.5e-10"), ]; for (value, rendered) in fractions { let opening = check_statement(&CheckInput { kind: Bank, opening_minor: Some(*value), closing_minor: None, lines: vec![], }) .expect_err("refused"); assert_eq!( opening.to_string(), format!( "checkStatement: openingMinor must be an integer (paise) or null, got {rendered}" ) ); let closing = check_statement(&CheckInput { kind: Bank, opening_minor: None, closing_minor: Some(*value), lines: vec![], }) .expect_err("refused"); assert_eq!( closing.to_string(), format!( "checkStatement: closingMinor must be an integer (paise) or null, got {rendered}" ) ); let amount = check_statement(&CheckInput { kind: Bank, opening_minor: None, closing_minor: None, lines: vec![line(4.0, *value, In)], }) .expect_err("refused"); assert_eq!( amount.to_string(), format!( "checkStatement: line 4 amountMinor must be an integer (paise), got {rendered}" ) ); } // `-1e21` is an integer, so the POSITIVE guard is the one that catches it. let big_negative = check_statement(&CheckInput { kind: Bank, opening_minor: None, closing_minor: None, lines: vec![line(4.0, -1e21, In)], }) .expect_err("refused"); assert_eq!( big_negative.to_string(), "checkStatement: line 4 amountMinor must be positive — direction carries the sign, got -1e+21" ); assert_eq!(big_negative.kind(), "RangeError"); // …and `1e21` is admitted outright. assert!(check_statement(&CheckInput { kind: Bank, opening_minor: None, closing_minor: None, lines: vec![line(4.0, 1e21, In)], }) .is_ok()); let line_nos: &[(f64, &str, &str)] = &[ ( 0.0, "checkStatement: lineNo must be positive, got 0", "RangeError", ), ( -1.0, "checkStatement: lineNo must be positive, got -1", "RangeError", ), ( -1.5, "checkStatement: lineNo must be an integer, got -1.5", "TypeError", ), ( 1.5, "checkStatement: lineNo must be an integer, got 1.5", "TypeError", ), ( f64::NAN, "checkStatement: lineNo must be an integer, got NaN", "TypeError", ), ( f64::INFINITY, "checkStatement: lineNo must be an integer, got Infinity", "TypeError", ), ]; for (value, message, kind) in line_nos { let error = check_statement(&CheckInput { kind: Bank, opening_minor: None, closing_minor: None, lines: vec![line(*value, 100.0, In)], }) .expect_err("refused"); assert_eq!(error.to_string(), *message); assert_eq!(error.kind(), *kind); } let non_positive: &[(f64, &str)] = &[(0.0, "0"), (-1.0, "-1")]; for (value, rendered) in non_positive { let error = check_statement(&CheckInput { kind: Bank, opening_minor: None, closing_minor: None, lines: vec![line(7.0, *value, In)], }) .expect_err("refused"); assert_eq!( error.to_string(), format!( "checkStatement: line 7 amountMinor must be positive — direction carries the sign, got {rendered}" ) ); } let balances: &[(f64, &str)] = &[(1.5, "1.5"), (f64::NAN, "NaN"), (-0.25, "-0.25")]; for (value, rendered) in balances { let error = check_statement(&CheckInput { kind: Bank, opening_minor: None, closing_minor: None, lines: vec![with_balance(line(2.0, 100.0, In), *value)], }) .expect_err("refused"); assert_eq!( error.to_string(), format!( "checkStatement: line 2 balanceAfterMinor must be an integer (paise), got {rendered}" ) ); } let duplicate = check_statement(&CheckInput { kind: Bank, opening_minor: None, closing_minor: None, lines: vec![line(5.0, 100.0, In), line(5.0, 200.0, Out)], }) .expect_err("refused"); assert_eq!( duplicate.to_string(), "checkStatement: two lines claim line_no 5" ); assert_eq!(duplicate.kind(), "RangeError"); } /// RULE: The closing a statement's kept totals give is the closing its line-by-line check expects, on a bank and on a card, so a page saying "Off by" says the check's own difference. #[test] fn the_totals_give_the_closing_the_check_expects() { for kind in [Bank, CreditCard] { let mut input = bank_statement(); input.kind = kind; input.lines.push(ignored(line(3.0, 999.0, In))); let result = check_statement(&input).expect("a readable statement"); assert_eq!( Some(closing_from_totals(kind, 1_000_000, result.sum_in_minor as i64, result.sum_out_minor as i64) as f64), result.expected_closing_minor, "{kind:?}" ); } }