| """Content moderation for Plant / Wildcard ability words.""" |
| import json |
| import sys |
| from unittest.mock import patch |
|
|
| import config |
| import engine |
| import llm |
|
|
|
|
| def _state() -> engine.GameState: |
| return engine.GameState() |
|
|
|
|
| def test_moderate_flags_via_llm() -> None: |
| state = _state() |
| with patch.object(llm, "complete", return_value='{"flagged": true}'): |
| args = engine._moderate_ability_word_args( |
| state, "wildcard_card", {"wildcard_word": "badword"} |
| ) |
| assert args["wildcard_word"] == config.FLAGGED_WORD_REPLACEMENT |
| assert args["word_flagged"] is True |
| assert state.content_flag_active is True |
| assert state.content_flag_ability == "wildcard_card" |
|
|
|
|
| def test_moderate_allows_clean_word() -> None: |
| state = _state() |
| with patch.object(llm, "complete", return_value='{"flagged": false}'): |
| args = engine._moderate_ability_word_args( |
| state, "plant", {"plant_word": "chaos"} |
| ) |
| assert args["plant_word"] == "chaos" |
| assert "word_flagged" not in args |
| assert state.content_flag_active is False |
|
|
|
|
| def test_commit_replaces_flagged_wildcard_in_pool() -> None: |
| state = _state() |
| state.waiting_for_ability_choice = True |
| state.player_pool = [ |
| engine.WordEntry(word="legacy", category="noun", tier="power"), |
| ] |
| with patch.object(llm, "complete", return_value='{"flagged": true}'): |
| state = engine.commit_round_ability( |
| state, "wildcard_card", {"wildcard_word": "slurword"} |
| ) |
| pool_words = [w.word for w in state.player_pool] |
| assert config.FLAGGED_WORD_REPLACEMENT in pool_words |
| assert "slurword" not in pool_words |
| assert state.pending_ability_args["word_flagged"] is True |
|
|
|
|
| def test_forced_plant_uses_replacement() -> None: |
| state = _state() |
| state.pending_ability_played = "plant" |
| state.pending_ability_args = { |
| "plant_word": config.FLAGGED_WORD_REPLACEMENT, |
| "word_flagged": True, |
| } |
| assert engine._forced_plant_words(state) == [config.FLAGGED_WORD_REPLACEMENT] |
|
|
|
|
| def test_calc_word_score_zeroes_inappropriate() -> None: |
| results = [{"word": config.FLAGGED_WORD_REPLACEMENT, "score": 2, "note": "sharp"}] |
| total, details = engine.calc_word_score(results, []) |
| assert total == 0 |
| assert details[0]["score"] == 0 |
| assert details[0]["note"] == "flagged replacement" |
|
|
|
|
| def test_sanitize_log_args() -> None: |
| out = engine.sanitize_ability_args_for_log( |
| {"plant_word": "hidden", "word_flagged": True} |
| ) |
| assert out["plant_word"] == config.FLAGGED_WORD_REPLACEMENT |
|
|
|
|
| def main() -> None: |
| test_moderate_flags_via_llm() |
| test_moderate_allows_clean_word() |
| test_commit_replaces_flagged_wildcard_in_pool() |
| test_forced_plant_uses_replacement() |
| test_calc_word_score_zeroes_inappropriate() |
| test_sanitize_log_args() |
| print("All content moderation tests passed.") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
| sys.exit(0) |
|
|