from __future__ import annotations from jarca_ledger_mcp import service def test_uncategorized_workbench_recommends_from_rules_and_applies_single(tmp_path): db_path = str(tmp_path / "ledger.sqlite3") service.seed_default_category_rules(db_path=db_path) tx = service.create_ledger_transaction( "2026-06-01", "expense", 10_000, merchant="스타벅스강남", raw_summary="체크카드", db_path=db_path )["transaction"] workbench = service.get_uncategorized_workbench(db_path=db_path) assert workbench["success"] is True assert workbench["summary"]["uncategorized_count"] == 1 assert workbench["summary"]["recommendable_count"] == 1 row = workbench["transactions"][0] assert row["id"] == tx["id"] assert row["recommended_category"] == "식비" assert row["recommended_subcategory"] == "카페" assert row["recommendation"]["source"] == "rule" applied = service.apply_uncategorized_recommendation(tx["id"], db_path=db_path) assert applied["success"] is True assert applied["applied_count"] == 1 saved = service.list_ledger_transactions(limit=10, db_path=db_path)["transactions"][0] assert saved["category"] == "식비" assert saved["subcategory"] == "카페" def test_uncategorized_workbench_recommends_from_history_and_applies_group(tmp_path): db_path = str(tmp_path / "ledger.sqlite3") service.create_ledger_transaction( "2026-06-01", "expense", 7_000, category="식비", subcategory="카페", merchant="동네커피", raw_summary="체크카드", db_path=db_path ) tx1 = service.create_ledger_transaction( "2026-06-02", "expense", 8_000, merchant="동네커피", raw_summary="체크카드", db_path=db_path )["transaction"] tx2 = service.create_ledger_transaction( "2026-06-03", "expense", 9_000, merchant="동네커피", raw_summary="체크카드", db_path=db_path )["transaction"] workbench = service.get_uncategorized_workbench(db_path=db_path) assert workbench["summary"]["uncategorized_count"] == 2 assert workbench["summary"]["recommendable_count"] == 2 assert workbench["summary"]["repeated_group_count"] == 1 group = workbench["groups"][0] assert group["count"] == 2 assert group["recommendation"]["category"] == "식비" assert group["recommendation"]["source"] == "history" applied = service.apply_uncategorized_group_recommendation("동네커피", "체크카드", "expense", db_path=db_path) assert applied["success"] is True assert applied["applied_count"] == 2 transactions = service.list_ledger_transactions(limit=10, db_path=db_path)["transactions"] by_id = {tx["id"]: tx for tx in transactions} assert by_id[tx1["id"]]["category"] == "식비" assert by_id[tx2["id"]]["subcategory"] == "카페" def test_uncategorized_workbench_supports_amount_and_date_sort(tmp_path): db_path = str(tmp_path / "ledger.sqlite3") service.create_ledger_transaction( "2026-06-01", "expense", 30_000, merchant="오래된고액", raw_summary="체크카드", db_path=db_path ) service.create_ledger_transaction( "2026-06-03", "expense", 5_000, merchant="최신소액", raw_summary="체크카드", db_path=db_path ) service.create_ledger_transaction( "2026-06-02", "expense", 10_000, merchant="중간", raw_summary="체크카드", db_path=db_path ) by_amount = service.get_uncategorized_workbench(sort_by="amount_desc", db_path=db_path)["transactions"] by_date = service.get_uncategorized_workbench(sort_by="date_desc", db_path=db_path)["transactions"] assert [row["merchant"] for row in by_amount] == ["오래된고액", "중간", "최신소액"] assert [row["merchant"] for row in by_date] == ["최신소액", "중간", "오래된고액"]