from pathlib import Path import sqlite3 from jarca_ledger_mcp.db import init_db from jarca_ledger_mcp.application.monthly_settlement_service import ( list_monthly_settlement_history, get_monthly_settlement_history_record, ) def _seed_history(path: str) -> None: init_db(path) with sqlite3.connect(path) as conn: conn.execute( """INSERT INTO asset_snapshot_runs (id,basis_date,close_status,draft_version,approved_at,correction_of_run_id, correction_reason,created_at,settlement_month,income_total,expense_total, net_cashflow,net_worth) VALUES ('run-1','2026-09-30','complete_exact',3,'2026-10-01','','', '2026-10-01','2026-09',100,40,60,1000)""" ) conn.execute( """INSERT INTO asset_snapshot_runs (id,basis_date,close_status,draft_version,approved_at,correction_of_run_id, correction_reason,created_at,settlement_month,income_total,expense_total, net_cashflow,net_worth) VALUES ('run-2','2026-09-30','conditional',4,'2026-10-02','run-1','fix', '2026-10-02','2026-09',100,45,55,995)""" ) def test_monthly_history_lists_close_and_correction_records(tmp_path: Path): path = str(tmp_path / "history.sqlite3") _seed_history(path) result = list_monthly_settlement_history("2026-09", db_path=path) assert result["success"] is True assert [row["run_id"] for row in result["history"]] == ["run-2", "run-1"] assert result["history"][0]["correction_target"] == "run-1" assert result["read_only"] is True def test_monthly_history_record_reads_exact_run(tmp_path: Path): path = str(tmp_path / "history-read.sqlite3") _seed_history(path) result = get_monthly_settlement_history_record("run-1", db_path=path) assert result["success"] is True assert result["history"]["run_id"] == "run-1" assert result["history"]["net_cashflow"] == 60 assert result["history"]["net_worth"] == 1000 def test_monthly_history_record_reports_missing_run(tmp_path: Path): path = str(tmp_path / "history-missing.sqlite3") init_db(path) result = get_monthly_settlement_history_record("missing", db_path=path) assert result["success"] is False assert result["error_code"] == "not_found"