| |
| """Tests for agent/translate.py β pure functions only, no LLM calls.""" |
| import copy |
| import pytest |
| from agent.translate import _collect_segments, _set_at_path |
| from agent.prompts import PROSE_FIELDS, PROSE_LIST_FIELDS |
|
|
|
|
| |
|
|
| def test_collect_segments_prose_field(): |
| brief = {"what_matters_most": "Revenue grew strongly."} |
| segs = _collect_segments(brief) |
| assert len(segs) == 1 |
| path, text = segs[0] |
| assert path == ("what_matters_most",) |
| assert text == "Revenue grew strongly." |
|
|
|
|
| def test_collect_segments_excludes_evidence_snippet(): |
| brief = {"evidence_snippet": "verbatim quote here", "headline": "Q3 beat"} |
| segs = _collect_segments(brief) |
| paths = [p for p, _ in segs] |
| assert ("evidence_snippet",) not in paths |
| assert ("headline",) in paths |
|
|
|
|
| def test_collect_segments_skips_quarter_deltas_subtree(): |
| brief = { |
| "what_matters_most": "Growth accelerated.", |
| "quarter_deltas": {"headline": "Should NOT be collected", "text": "Also skip"}, |
| } |
| segs = _collect_segments(brief) |
| texts = [t for _, t in segs] |
| assert "Growth accelerated." in texts |
| assert "Should NOT be collected" not in texts |
| assert "Also skip" not in texts |
|
|
|
|
| def test_collect_segments_prose_list_field(): |
| brief = {"what_to_watch": ["Watch margins closely.", "Monitor capex spend."]} |
| segs = _collect_segments(brief) |
| assert len(segs) == 2 |
| paths = [p for p, _ in segs] |
| assert ("what_to_watch", 0) in paths |
| assert ("what_to_watch", 1) in paths |
|
|
|
|
| def test_collect_segments_nested_dict(): |
| brief = { |
| "bull_points": [ |
| {"text": "Revenue beat expectations.", "source": "10-K", "reliability": "HIGH"} |
| ] |
| } |
| segs = _collect_segments(brief) |
| paths = [p for p, _ in segs] |
| assert ("bull_points", 0, "text") in paths |
| |
| assert ("bull_points", 0, "source") not in paths |
| assert ("bull_points", 0, "reliability") not in paths |
|
|
|
|
| def test_collect_segments_skips_empty_strings(): |
| brief = {"what_matters_most": "", "headline": "Has content"} |
| segs = _collect_segments(brief) |
| paths = [p for p, _ in segs] |
| assert ("what_matters_most",) not in paths |
| assert ("headline",) in paths |
|
|
|
|
| def test_collect_segments_nested_key_quote(): |
| brief = {"key_quote": {"text": "This is the key quote.", "source": "transcript"}} |
| segs = _collect_segments(brief) |
| paths = [p for p, _ in segs] |
| assert ("key_quote", "text") in paths |
| assert ("key_quote", "source") not in paths |
|
|
|
|
| |
|
|
| def test_set_at_path_top_level(): |
| obj = {"what_matters_most": "English text"} |
| _set_at_path(obj, ("what_matters_most",), "Texte franΓ§ais") |
| assert obj["what_matters_most"] == "Texte franΓ§ais" |
|
|
|
|
| def test_set_at_path_nested(): |
| obj = {"bull_points": [{"text": "English", "reliability": "HIGH"}]} |
| _set_at_path(obj, ("bull_points", 0, "text"), "FranΓ§ais") |
| assert obj["bull_points"][0]["text"] == "FranΓ§ais" |
| assert obj["bull_points"][0]["reliability"] == "HIGH" |
|
|
|
|
| def test_set_at_path_missing_key_is_silent(): |
| obj = {"bull_points": []} |
| _set_at_path(obj, ("bull_points", 0, "text"), "FranΓ§ais") |
| |
|
|
|
|
| def test_set_at_path_empty_path_is_silent(): |
| obj = {"x": 1} |
| _set_at_path(obj, (), "value") |
| assert obj == {"x": 1} |
|
|
|
|
| |
|
|
| def test_prose_fields_in_language_directive(): |
| """Every name in PROSE_FIELDS | PROSE_LIST_FIELDS appears in language_directive output.""" |
| from agent.prompts import language_directive |
| directive = language_directive("French") |
| for field in PROSE_FIELDS | PROSE_LIST_FIELDS: |
| assert field in directive, f"Field '{field}' missing from language_directive" |
|
|