|
| 1 | +"""Tests for focus_nudge utility — should_nudge() logic and nudge_unity_focus() gating.""" |
| 2 | + |
| 3 | +import time |
| 4 | +from unittest.mock import patch, AsyncMock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from utils.focus_nudge import ( |
| 9 | + should_nudge, |
| 10 | + reset_nudge_backoff, |
| 11 | + nudge_unity_focus, |
| 12 | + _is_available, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class TestShouldNudge: |
| 17 | + """Tests for should_nudge() decision logic.""" |
| 18 | + |
| 19 | + def test_returns_false_when_not_running(self): |
| 20 | + assert should_nudge(status="succeeded", editor_is_focused=False, last_update_unix_ms=0, current_time_ms=99999) is False |
| 21 | + |
| 22 | + def test_returns_false_when_focused(self): |
| 23 | + assert should_nudge(status="running", editor_is_focused=True, last_update_unix_ms=0, current_time_ms=99999) is False |
| 24 | + |
| 25 | + def test_returns_true_when_stalled_and_unfocused(self): |
| 26 | + now_ms = int(time.time() * 1000) |
| 27 | + stale_ms = now_ms - 5000 # 5s ago |
| 28 | + assert should_nudge(status="running", editor_is_focused=False, last_update_unix_ms=stale_ms, current_time_ms=now_ms) is True |
| 29 | + |
| 30 | + def test_returns_false_when_recently_updated(self): |
| 31 | + now_ms = int(time.time() * 1000) |
| 32 | + recent_ms = now_ms - 1000 # 1s ago (within 3s threshold) |
| 33 | + assert should_nudge(status="running", editor_is_focused=False, last_update_unix_ms=recent_ms, current_time_ms=now_ms) is False |
| 34 | + |
| 35 | + def test_returns_true_when_no_updates_yet(self): |
| 36 | + """No last_update_unix_ms means tests might be stuck at start.""" |
| 37 | + assert should_nudge(status="running", editor_is_focused=False, last_update_unix_ms=None) is True |
| 38 | + |
| 39 | + def test_custom_stall_threshold(self): |
| 40 | + now_ms = int(time.time() * 1000) |
| 41 | + stale_ms = now_ms - 2000 # 2s ago |
| 42 | + # Default threshold (3s) — not stale yet |
| 43 | + assert should_nudge(status="running", editor_is_focused=False, last_update_unix_ms=stale_ms, current_time_ms=now_ms) is False |
| 44 | + # Custom threshold (1s) — stale |
| 45 | + assert should_nudge(status="running", editor_is_focused=False, last_update_unix_ms=stale_ms, current_time_ms=now_ms, stall_threshold_ms=1000) is True |
| 46 | + |
| 47 | + def test_returns_false_for_failed_status(self): |
| 48 | + assert should_nudge(status="failed", editor_is_focused=False, last_update_unix_ms=0, current_time_ms=99999) is False |
| 49 | + |
| 50 | + def test_returns_false_for_cancelled_status(self): |
| 51 | + assert should_nudge(status="cancelled", editor_is_focused=False, last_update_unix_ms=0, current_time_ms=99999) is False |
| 52 | + |
| 53 | + |
| 54 | +class TestResetNudgeBackoff: |
| 55 | + """Tests for reset_nudge_backoff() state management.""" |
| 56 | + |
| 57 | + def test_resets_consecutive_nudges(self): |
| 58 | + import utils.focus_nudge as fn |
| 59 | + fn._consecutive_nudges = 5 |
| 60 | + reset_nudge_backoff() |
| 61 | + assert fn._consecutive_nudges == 0 |
| 62 | + |
| 63 | + def test_updates_last_progress_time(self): |
| 64 | + import utils.focus_nudge as fn |
| 65 | + old_time = fn._last_progress_time |
| 66 | + reset_nudge_backoff() |
| 67 | + assert fn._last_progress_time >= old_time |
| 68 | + |
| 69 | + |
| 70 | +class TestNudgeUnityFocus: |
| 71 | + """Tests for nudge_unity_focus() gating logic.""" |
| 72 | + |
| 73 | + @pytest.mark.asyncio |
| 74 | + async def test_skips_when_not_available(self): |
| 75 | + with patch("utils.focus_nudge._is_available", return_value=False): |
| 76 | + result = await nudge_unity_focus(force=True) |
| 77 | + assert result is False |
| 78 | + |
| 79 | + @pytest.mark.asyncio |
| 80 | + async def test_skips_when_unity_already_focused(self): |
| 81 | + from utils.focus_nudge import _FrontmostAppInfo |
| 82 | + with patch("utils.focus_nudge._is_available", return_value=True), \ |
| 83 | + patch("utils.focus_nudge._get_frontmost_app", return_value=_FrontmostAppInfo(name="Unity")): |
| 84 | + result = await nudge_unity_focus(force=True) |
| 85 | + assert result is False |
| 86 | + |
| 87 | + @pytest.mark.asyncio |
| 88 | + async def test_skips_when_frontmost_app_unknown(self): |
| 89 | + with patch("utils.focus_nudge._is_available", return_value=True), \ |
| 90 | + patch("utils.focus_nudge._get_frontmost_app", return_value=None): |
| 91 | + result = await nudge_unity_focus(force=True) |
| 92 | + assert result is False |
| 93 | + |
| 94 | + @pytest.mark.asyncio |
| 95 | + async def test_rate_limited_by_backoff(self): |
| 96 | + import utils.focus_nudge as fn |
| 97 | + from utils.focus_nudge import _FrontmostAppInfo |
| 98 | + # Simulate a very recent nudge |
| 99 | + fn._last_nudge_time = time.monotonic() |
| 100 | + fn._consecutive_nudges = 0 |
| 101 | + with patch("utils.focus_nudge._is_available", return_value=True), \ |
| 102 | + patch("utils.focus_nudge._get_frontmost_app", return_value=_FrontmostAppInfo(name="Terminal")): |
| 103 | + result = await nudge_unity_focus(force=False) |
| 104 | + assert result is False |
0 commit comments