"""Tests for the deterministic regex/lexicon pre-check. These are pure-function tests for ``app.services.output_review_lexicon.check`` — no LLM, no DB, no fixtures. They protect both the obvious-rejects and the false-positive guards (bare 'should', 'cut', 'hold' must not match in benign contexts).""" from __future__ import annotations import pytest from app.services.output_review_lexicon import check # --------------------------------------------------------------------------- # Obvious rejects — should hit and identify the firing rule # --------------------------------------------------------------------------- @pytest.mark.parametrize("text,expected_rule", [ # First-match-wins ordering: action_phrase runs before advice_phrase, so # a sentence with both ("you should buy") fires action_phrase. That's # fine — the goal is to catch a violation, not to attribute the rule. ("You should buy the dip.", "action_phrase"), ("Investors should consider buying defensives.", "advice_phrase"), ("We recommend trimming this position.", "advice_phrase"), ("That stock is a buy at these levels.", "action_phrase"), ("Take profit on the position.", "action_phrase"), ("Trim your exposure to growth.", "action_phrase"), ("Rotate into defensives.", "action_phrase"), ("Overweight the sector into Q1.", "action_phrase"), # Forecast / level register ("Our price target sits at $95.", "forecast_phrase"), ("Target of $93 looks reasonable.", "forecast_phrase"), ("There is support at $4,200.", "forecast_phrase"), ("Resistance near $570 is the level to watch.", "forecast_phrase"), # Composed patterns ("Brent close above $93 would confirm.", "level_trigger"), ("If SOXX breaks below 570 the bid fades.", "level_trigger"), ("Watch for a move above 4,600.", "level_trigger"), ("Floor at $90 looks intact.", "forecast_phrase"), # "X as a floor / ceiling" phrasing — not currently a hard rule in the # lexicon (LLM layer catches it); see future-tightening note in the # module docstring. ]) def test_lexicon_catches_obvious_violations(text, expected_rule): hit = check(text) assert hit is not None, f"should have flagged: {text!r}" assert hit.rule == expected_rule, ( f"expected rule {expected_rule!r}, got {hit.rule!r} for {text!r}" ) assert hit.snippet, "snippet should never be empty" # --------------------------------------------------------------------------- # False-positive guards from the brief — must NOT match # --------------------------------------------------------------------------- @pytest.mark.parametrize("text", [ # The brief specifically calls these out as false-positive risks for # bare-word matchers. "Saudi price cuts pushed energy lower this week.", "The cargo hold story dominated the tape.", "OPEC may hold output steady at the next meeting.", # State-level commentary the LLM layer should judge, not the lexicon. "Valuations are stretched after the rally.", "Real yields are restrictive across the curve.", "Positioning is crowded in megacap tech.", # Plain factual price citation — no trigger framing. "Brent is trading at $90, down 12% YTD.", "Gold is at $4,600 after a sharp move higher.", # Empty / whitespace "", " ", ]) def test_lexicon_lets_clean_text_through(text): hit = check(text) assert hit is None, f"lexicon false-positive on: {text!r} (rule={hit and hit.rule})"