from __future__ import annotations import tempfile from pathlib import Path from application.csv_manager import CSVManager, sanitize_csv_cell_value def test_sanitize_collapses_newlines() -> None: assert sanitize_csv_cell_value("a\nb") == "a b" assert sanitize_csv_cell_value("a\r\nb") == "a b" assert sanitize_csv_cell_value(" x\v\fy ") == "x y" def test_sanitize_unicode_line_separators() -> None: assert sanitize_csv_cell_value("a\u2028b\u2029c") == "a b c" assert sanitize_csv_cell_value("a\x85b") == "a b" def test_sanitize_preserves_none_and_scalars() -> None: assert sanitize_csv_cell_value(None) is None assert sanitize_csv_cell_value(42) == 42 assert sanitize_csv_cell_value(3.14) == 3.14 assert sanitize_csv_cell_value(False) is False assert sanitize_csv_cell_value(True) is True def test_sanitize_plain_string_unchanged_identity() -> None: s = "sin saltos" assert sanitize_csv_cell_value(s) is s def test_append_rows_one_physical_line_per_record() -> None: with tempfile.TemporaryDirectory() as tmp: path = Path(tmp) / "batch.csv" mgr = CSVManager() header = ["radicado", "descripcion"] rows = [ { "radicado": "235662283", "descripcion": ( '"Las retenciones son a nivel informativo.\n' "Paciente: 1010079549 - HERAZO\n" 'Contrato No. URGENCIAS."' ), }, {"radicado": "2", "descripcion": "ok"}, ] mgr.append_rows(str(path), rows, header) text = path.read_text(encoding="utf-8-sig") physical_lines = text.splitlines() assert len(physical_lines) == 3