"""Blocco 6: robustness fixes -- taskdoc slice/bound, report escaping, upsert count.""" from tht.report import _markdown_table, extract_reviewer_notes from tht.taskdoc import generate_task_doc def test_taskdoc_slices_to_promoted_tables(tmp_path): s = tmp_path / "sess" s.mkdir() (s / "question.md").write_text("q") (s / "schema_linking.json").write_text( '{"question":"q","candidates":[' '{"kind":"table","name":"pazienti","decision":"promoted"},' '{"kind":"table","name":"ricoveri","decision":"promoted"}],' '"joins":[],"excluded":[],"open_questions":[]}' ) doc = generate_task_doc(session_dir=s, phase=4, promoted_tables=["pazienti"]) assert "pazienti" in doc.body assert "ricoveri" not in doc.body # sliced out def test_taskdoc_truncates_over_budget(tmp_path): s = tmp_path / "sess" s.mkdir() (s / "question.md").write_text("# Domanda\n" + "x" * 200_000) doc = generate_task_doc(session_dir=s, phase=1) assert doc.byte_budget_ok is False assert len(doc.body.encode()) <= 80_000 assert "troncato" in doc.body def test_markdown_table_escapes_pipes_and_newlines(): table = _markdown_table(["c"], [("a|b\nc",)]) # the cell must not introduce a raw pipe or newline that breaks the row body_line = table.splitlines()[2] assert "\\|" in body_line assert "\n" not in body_line def test_extract_reviewer_notes_uses_last_heading(): report = ( "## Note del reviewer\nnella cella di dati appariva questo testo\n" "## Note del reviewer\nnota vera del reviewer" ) assert extract_reviewer_notes(report) == "nota vera del reviewer" def test_upsert_count_handles_postgrest_list_wrapping(): from unittest.mock import MagicMock from tht.vectorstore.rest_client import VectorRestClient client = VectorRestClient.__new__(VectorRestClient) client._call = MagicMock(return_value=[{"upserted": 7}]) # list-wrapped scalar assert client.upsert_records("memory", [{}, {}]) == 7