Download tests/test_cancel_timeout.py from YGCXLab/ux-research-agent-slackbot: direct link, hf CLI and curl.
- Browser
- Download file 3.29 kB
-
https://huggingface.co/spaces/YGCXLab/ux-research-agent-slackbot/resolve/main/tests/test_cancel_timeout.py
- Command line
-
hf download hf://spaces/YGCXLab/ux-research-agent-slackbot/tests/test_cancel_timeout.py
-
curl -L -o test_cancel_timeout.py https://huggingface.co/spaces/YGCXLab/ux-research-agent-slackbot/resolve/main/tests/test_cancel_timeout.py
3.29 kB
| """Tests for cancel/timeout/confirmation safety features.""" | |
| import threading | |
| import time | |
| import pytest | |
| from unittest.mock import MagicMock, patch | |
| # --- Unit tests that don't require Slack/engine init --- | |
| class TestCancelPatterns: | |
| """Test cancel command regex detection.""" | |
| def _match(self, text: str) -> bool: | |
| """Import CANCEL_RE lazily to avoid full module init.""" | |
| import re | |
| CANCEL_PATTERNS = [r"^취소$", r"^중지$", r"^그만$", r"^stop$", r"^cancel$"] | |
| CANCEL_RE = re.compile("|".join(CANCEL_PATTERNS), re.IGNORECASE) | |
| return bool(CANCEL_RE.search(text.strip())) | |
| def test_korean_cancel(self): | |
| assert self._match("취소") | |
| assert self._match("중지") | |
| assert self._match("그만") | |
| def test_english_cancel(self): | |
| assert self._match("stop") | |
| assert self._match("cancel") | |
| assert self._match("STOP") | |
| assert self._match("Cancel") | |
| def test_non_cancel_messages(self): | |
| """Normal messages should NOT match cancel patterns.""" | |
| assert not self._match("검색 결과 보여줘") | |
| assert not self._match("취소 방법 알려줘") # 취소 embedded in longer text | |
| assert not self._match("1") | |
| assert not self._match("네") | |
| def test_whitespace_handling(self): | |
| assert self._match(" 취소 ") | |
| assert self._match(" stop ") | |
| class TestValidStrategies: | |
| """Test strategy validation logic.""" | |
| VALID_STRATEGIES = {"SIMPLE_SEARCH", "DEEP_RESEARCH", "ADVISORY"} | |
| def test_valid_strategies(self): | |
| for s in ["SIMPLE_SEARCH", "DEEP_RESEARCH", "ADVISORY"]: | |
| assert s in self.VALID_STRATEGIES | |
| def test_invalid_strategies(self): | |
| for s in ["recommended", "CHAT", "", "unknown", "1", "2", "3"]: | |
| assert s not in self.VALID_STRATEGIES | |
| class TestCancelEventMechanism: | |
| """Test threading.Event-based cancel mechanism.""" | |
| def test_cancel_event_stops_loop(self): | |
| """Simulates the streaming loop with cancel event.""" | |
| cancel = threading.Event() | |
| chunks_processed = 0 | |
| def fake_stream(): | |
| for i in range(100): | |
| yield f"chunk_{i}" | |
| time.sleep(0.01) | |
| for chunk in fake_stream(): | |
| if cancel.is_set(): | |
| break | |
| chunks_processed += 1 | |
| if chunks_processed == 5: | |
| cancel.set() # Simulate cancel after 5 chunks | |
| assert chunks_processed == 5 | |
| def test_timeout_triggers_cancel(self): | |
| """Timer should set cancel event after timeout.""" | |
| cancel = threading.Event() | |
| timer = threading.Timer(0.1, lambda: cancel.set()) | |
| timer.start() | |
| # Wait for cancel | |
| cancel.wait(timeout=1.0) | |
| assert cancel.is_set() | |
| timer.cancel() # cleanup | |
| def test_cancel_before_timeout(self): | |
| """Manual cancel should stop the timer from firing.""" | |
| cancel = threading.Event() | |
| timer = threading.Timer(5.0, lambda: cancel.set()) | |
| timer.start() | |
| # Cancel manually before timeout | |
| cancel.set() | |
| timer.cancel() | |
| timer.join(timeout=1.0) # Wait for timer thread to finish | |
| assert cancel.is_set() | |
| assert not timer.is_alive() | |