import sqlite3 from jarca_ledger_mcp.application.accounts_write_services import create_ledger_account from jarca_ledger_mcp.application.assets_service import ( create_manual_asset, list_manual_assets, preview_asset_snapshot_close, ) from jarca_ledger_mcp.infrastructure.sqlite.migrations import init_db def tables(path): with sqlite3.connect(path) as conn: return {r[0] for r in conn.execute("select name from sqlite_master where type='table'")} def test_fresh_and_repeated_init_retain_manual_assets(tmp_path): path = str(tmp_path / "fresh.sqlite3") init_db(path) init_db(path) assert "manual_assets" in tables(path) def test_existing_legacy_manual_assets_is_preserved_without_other_row_loss(tmp_path): path = str(tmp_path / "legacy.sqlite3") init_db(path) with sqlite3.connect(path) as conn: conn.execute("insert into manual_assets (id,name,created_at,updated_at) values ('old', 'legacy','t','t')") conn.execute("insert into accounts (id,name,type,active,created_at,updated_at) values ('a','Keep','bank',1,'t','t')") conn.commit() init_db(path) with sqlite3.connect(path) as conn: assert conn.execute("select count(*) from accounts where id='a'").fetchone()[0] == 1 assert conn.execute("select name from manual_assets where id='old'").fetchone() == ("legacy",) assert "manual_assets" in tables(path) def test_manual_asset_public_operations_are_supported(tmp_path): path = str(tmp_path / "supported.sqlite3") init_db(path) created = create_manual_asset("Emergency", 100, "2026-09-30", "cash", db_path=path) listed = list_manual_assets(db_path=path) assert created["success"] and listed["success"] assert listed["manual_assets"][0]["id"] == created["manual_asset"]["id"] def test_account_real_estate_creation_and_snapshot_preview_use_normal_path(tmp_path): path = str(tmp_path / "real-estate.sqlite3") init_db(path) created = create_ledger_account( "House", "other", liquidity_bucket="real_estate", last_balance=500000, last_balance_basis_date="2026-09-30", db_path=path, ) assert created["success"] is True assert created["account"]["liquidity_bucket"] == "real_estate" assert preview_asset_snapshot_close("2026-09-30", db_path=path)["success"] is True def test_manual_assets_present_after_initialization_with_existing_accounts(tmp_path): path = str(tmp_path / "accounts.sqlite3") init_db(path) with sqlite3.connect(path) as conn: before = conn.execute("select count(*) from accounts").fetchone()[0] init_db(path) with sqlite3.connect(path) as conn: assert conn.execute("select count(*) from accounts").fetchone()[0] == before assert "manual_assets" in tables(path)