use rust_decimal_macros::dec;
use rustledger_core::{Amount, BookingMethod, Cost, CostSpec, Inventory, Position};
#[test]
fn tla_fifo_should_select_oldest_by_date_not_insertion_order() {
let mut inv = Inventory::new();
inv.add(Position::with_cost(
Amount::new(dec!(10), "AAPL"),
Cost::new(dec!(150), "USD").with_date(rustledger_core::naive_date(2024, 1, 2).unwrap()),
));
inv.add(Position::with_cost(
Amount::new(dec!(10), "AAPL"),
Cost::new(dec!(100), "USD").with_date(rustledger_core::naive_date(2024, 1, 1).unwrap()),
));
let result = inv
.reduce(
&Amount::new(dec!(-5), "AAPL"),
Some(&CostSpec::default()),
BookingMethod::Fifo,
)
.expect("reduction should succeed");
let cost_basis = result.cost_basis.expect("should have cost basis");
assert_eq!(
cost_basis.number,
dec!(500),
"FIFO should select oldest lot by DATE, not insertion order. \
Got cost basis ${}, expected $500 (5 units @ $100 from 2024-01-01 lot)",
cost_basis.number
);
}