File size: 4,418 Bytes
beec002 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | # tests/test_translate.py
"""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
# ββ _collect_segments ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
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
# source and reliability are enum fields, should not be collected
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
# ββ _set_at_path ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
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" # untouched
def test_set_at_path_missing_key_is_silent():
obj = {"bull_points": []}
_set_at_path(obj, ("bull_points", 0, "text"), "FranΓ§ais")
# Should not raise; list is empty so index 0 doesn't exist
def test_set_at_path_empty_path_is_silent():
obj = {"x": 1}
_set_at_path(obj, (), "value")
assert obj == {"x": 1} # unchanged
# ββ drift guard ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
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"
|