from __future__ import annotations

from pathlib import Path

from jarvis_finance.imports.dry_run import dry_run_csv
from jarvis_finance.imports.row_hash import compute_row_hash


def test_row_hash_normalizes_whitespace_key_order_and_missing_values() -> None:
    left = {" b ": " 2 ", "a": None, "c": ""}
    right = {"c": None, "a": "", "b": "2"}
    assert compute_row_hash(left) == compute_row_hash(right)


def test_dry_run_reports_new_existing_and_duplicate_rows(tmp_path: Path) -> None:
    path = tmp_path / "rows.csv"
    path.write_text("id,name\n1,Demo\n1,Demo\n2,Other\n", encoding="utf-8")
    existing = {compute_row_hash({"id": "2", "name": "Other"})}

    result = dry_run_csv(path, {"id", "name"}, existing_hashes=existing)

    assert result.rows_total == 3
    assert result.rows_new == 1
    assert result.rows_existing == 1
    assert result.rows_duplicate == 1
    assert result.rows_failed == 1
    assert "Duplicate row hash" in result.errors[0]
