| """Call It topic filtering — verbatim pass vs judge rewrite.""" |
| import json |
| import sys |
| from unittest.mock import patch |
|
|
| import content |
| import engine |
| import llm |
|
|
|
|
| def _state_with_judge() -> engine.GameState: |
| state = engine.GameState() |
| state.current_judge = content.JUDGES[0] |
| state.current_topic = "A placeholder topic from round start" |
| state.judge_take = "Old take." |
| return state |
|
|
|
|
| def _apply_call_it(state: engine.GameState, topic: str, llm_response: str) -> engine.GameState: |
| with patch.object(llm, "complete", return_value=llm_response): |
| engine._apply_ability_at_commit( |
| state, |
| "call_it", |
| {"custom_topic": topic}, |
| ) |
| return state |
|
|
|
|
| def _assert_verbatim_pass() -> None: |
| state = _state_with_judge() |
| topic = "Rape culture is a real problem we ignore" |
| response = json.dumps({ |
| "passed": True, |
| "judge_take": "Finally, a topic with some teeth.", |
| }) |
| _apply_call_it(state, topic, response) |
| assert state.current_topic == topic |
| assert state.call_it_active is True |
| assert state.call_it_rewritten is False |
| assert state.call_it_original_topic == topic |
| assert state.judge_take == "Finally, a topic with some teeth." |
|
|
|
|
| def _assert_truncates_before_filter() -> None: |
| state = _state_with_judge() |
| topic = " ".join(f"word{i}" for i in range(25)) |
| expected = " ".join(f"word{i}" for i in range(20)) |
| response = json.dumps({"passed": True, "judge_take": "Sure."}) |
| _apply_call_it(state, topic, response) |
| assert state.current_topic == expected |
| assert state.call_it_original_topic == expected |
|
|
|
|
| def _assert_rewrite_on_fail() -> None: |
| state = _state_with_judge() |
| topic = "something offensive submitted" |
| rewritten = "Immigration policy should be debated honestly" |
| response = json.dumps({ |
| "passed": False, |
| "topic": rewritten, |
| "judge_take": "I cleaned that up. You're welcome.", |
| }) |
| _apply_call_it(state, topic, response) |
| assert state.current_topic == rewritten |
| assert state.call_it_rewritten is True |
| assert state.call_it_original_topic == topic |
| assert "cleaned" in state.judge_take.lower() |
|
|
|
|
| def _assert_llm_failure_falls_back_verbatim() -> None: |
| state = _state_with_judge() |
| topic = "Capitalism rewards the ruthless" |
| with patch.object(llm, "complete", side_effect=RuntimeError("api down")): |
| with patch.object(engine, "_generate_judge_take", return_value="Fallback take."): |
| engine._apply_ability_at_commit( |
| state, |
| "call_it", |
| {"custom_topic": topic}, |
| ) |
| assert state.current_topic == topic |
| assert state.call_it_rewritten is False |
| assert state.judge_take == "Fallback take." |
|
|
|
|
| def main() -> None: |
| _assert_verbatim_pass() |
| _assert_truncates_before_filter() |
| _assert_rewrite_on_fail() |
| _assert_llm_failure_falls_back_verbatim() |
| print("All Call It tests passed.") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
| sys.exit(0) |
|
|