from pathlib import Path import sqlite3 from jarca_ledger_mcp.db import init_db from jarca_ledger_mcp.application.monthly_settlement_service import ( get_monthly_settlement_history_record, list_monthly_settlement_history, ) def test_monthly_history_lists_close_and_correction_and_reads_one_record(tmp_path: Path): db = tmp_path / "history.sqlite3" init_db(str(db)) with sqlite3.connect(db) as conn: conn.execute( """INSERT INTO asset_snapshot_runs (id,basis_date,close_status,correction_of_run_id,correction_reason, created_at,settlement_month,draft_version,approved_at) VALUES (?,?,?,?,?,?,?,?,?)""", ("run-1", "2026-09-30", "complete_exact", "", "", "2026-10-01T00:00:00Z", "2026-09", 1, "2026-10-01T00:00:00Z"), ) conn.execute( """INSERT INTO asset_snapshot_runs (id,basis_date,close_status,correction_of_run_id,correction_reason, created_at,settlement_month,draft_version,approved_at) VALUES (?,?,?,?,?,?,?,?,?)""", ("run-2", "2026-09-30", "conditional", "run-1", "누락 거래 반영", "2026-10-02T00:00:00Z", "2026-09", 2, "2026-10-02T00:00:00Z"), ) conn.commit() listed = list_monthly_settlement_history("2026-09", db_path=str(db)) assert listed["success"] assert [row["run_id"] for row in listed["history"]] == ["run-2", "run-1"] correction = listed["history"][0] assert correction["version"] == 2 assert correction["correction_target"] == "run-1" assert correction["reason"] == "누락 거래 반영" one = get_monthly_settlement_history_record("run-2", db_path=str(db)) assert one["success"] assert one["history"]["run_id"] == "run-2" assert one["history"]["closed_at"] == "2026-10-02T00:00:00Z"