fix session expired and try variant timing
This commit is contained in:
parent
867c6f3152
commit
b4e84e430e
6 changed files with 211 additions and 9 deletions
66
tests/test_randomized_timing.py
Normal file
66
tests/test_randomized_timing.py
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
"""Tests for randomized timing functionality"""
|
||||
import pytest
|
||||
import random
|
||||
|
||||
|
||||
def get_randomized_interval(base_interval: int = 150, variance: int = 30) -> int:
|
||||
"""Get check interval with random variance to avoid bot detection patterns."""
|
||||
variance_value = random.randint(-variance, variance)
|
||||
interval = base_interval + variance_value
|
||||
# Ensure minimum of 60 seconds to avoid excessive load
|
||||
return max(60, interval)
|
||||
|
||||
|
||||
class TestRandomizedTiming:
|
||||
"""Test randomized check interval functionality"""
|
||||
|
||||
def test_randomized_interval_within_bounds(self):
|
||||
"""Test that randomized interval stays within expected bounds"""
|
||||
# Test multiple intervals to ensure randomization
|
||||
intervals = [get_randomized_interval(150, 30) for _ in range(100)]
|
||||
|
||||
# All intervals should be between 120 and 180 (150 ± 30)
|
||||
assert all(120 <= i <= 180 for i in intervals), f"Some intervals outside bounds"
|
||||
|
||||
# Should have some variance (not all the same)
|
||||
assert len(set(intervals)) > 1, "No randomization detected - all intervals are identical"
|
||||
|
||||
def test_minimum_interval_enforced(self):
|
||||
"""Test that minimum 60s interval is enforced even with high variance"""
|
||||
# Test multiple times with extreme variance
|
||||
intervals = [get_randomized_interval(90, 50) for _ in range(100)]
|
||||
|
||||
# All intervals should be >= 60
|
||||
assert all(i >= 60 for i in intervals), f"Some intervals below 60s minimum: {min(intervals)}"
|
||||
|
||||
def test_zero_variance_possible(self):
|
||||
"""Test that variance can be disabled by setting to 0"""
|
||||
# All intervals should be exactly 150
|
||||
intervals = [get_randomized_interval(150, 0) for _ in range(20)]
|
||||
assert all(i == 150 for i in intervals), "Variance=0 should produce constant interval"
|
||||
|
||||
def test_interval_distribution(self):
|
||||
"""Test that randomization produces reasonable distribution"""
|
||||
# Generate many samples
|
||||
intervals = [get_randomized_interval(150, 30) for _ in range(1000)]
|
||||
|
||||
avg = sum(intervals) / len(intervals)
|
||||
|
||||
# Average should be close to 150 (within 5 seconds)
|
||||
assert 145 <= avg <= 155, f"Average {avg} too far from expected 150"
|
||||
|
||||
# Should have good spread (at least 20 different values)
|
||||
assert len(set(intervals)) >= 20, "Not enough variance in intervals"
|
||||
|
||||
def test_custom_parameters(self):
|
||||
"""Test with various custom base intervals and variances"""
|
||||
test_cases = [
|
||||
(120, 20, 100, 140), # base, variance, min_expected, max_expected
|
||||
(180, 40, 140, 220),
|
||||
(200, 10, 190, 210),
|
||||
]
|
||||
|
||||
for base, variance, min_exp, max_exp in test_cases:
|
||||
intervals = [get_randomized_interval(base, variance) for _ in range(50)]
|
||||
assert all(min_exp <= i <= max_exp for i in intervals), \
|
||||
f"Failed for base={base}, variance={variance}"
|
||||
82
tests/test_session_validation.py
Normal file
82
tests/test_session_validation.py
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
"""Tests for session validation and re-login logic"""
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add parent directory to path for imports
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from state_manager import StateManager
|
||||
|
||||
|
||||
class TestSessionValidation:
|
||||
"""Test session validation and automatic re-login"""
|
||||
|
||||
@pytest.fixture
|
||||
def state_manager(self, tmp_path):
|
||||
"""Create a StateManager with a temporary state file"""
|
||||
state_file = tmp_path / "state.json"
|
||||
return StateManager(state_file)
|
||||
|
||||
def test_logged_in_status_toggle(self, state_manager):
|
||||
"""Test that logged_in status can be toggled"""
|
||||
assert state_manager.is_logged_in() is False
|
||||
|
||||
state_manager.set_logged_in(True)
|
||||
assert state_manager.is_logged_in() is True
|
||||
|
||||
state_manager.set_logged_in(False)
|
||||
assert state_manager.is_logged_in() is False
|
||||
|
||||
def test_logged_in_persistence_is_memory_only(self, state_manager, tmp_path):
|
||||
"""Test that logged_in status is memory-only (not persisted to disk)"""
|
||||
state_manager.set_logged_in(True)
|
||||
assert state_manager.is_logged_in() is True
|
||||
|
||||
# Create new state manager with same file - should start as False
|
||||
new_state_manager = StateManager(tmp_path / "state.json")
|
||||
assert new_state_manager.is_logged_in() is False
|
||||
|
||||
def test_zero_listings_counter_increments(self):
|
||||
"""Test that zero_listings_count increments when no listings are fetched"""
|
||||
# This would be tested in integration tests with the main loop
|
||||
# For now, we test the logic conceptually
|
||||
zero_listings_count = 0
|
||||
max_zero = 3
|
||||
|
||||
# Simulate 3 consecutive zero fetches
|
||||
for _ in range(3):
|
||||
current_listings = [] # Empty result
|
||||
if not current_listings:
|
||||
zero_listings_count += 1
|
||||
|
||||
assert zero_listings_count == max_zero
|
||||
|
||||
def test_zero_listings_counter_resets_on_success(self):
|
||||
"""Test that counter resets when listings are successfully fetched"""
|
||||
zero_listings_count = 2
|
||||
|
||||
# Simulate successful fetch
|
||||
current_listings = [{"id": "test123", "rooms": "2"}]
|
||||
if current_listings and len(current_listings) > 0:
|
||||
zero_listings_count = 0
|
||||
|
||||
assert zero_listings_count == 0
|
||||
|
||||
def test_session_validation_logic(self, state_manager):
|
||||
"""Test the session validation workflow"""
|
||||
# Start logged in
|
||||
state_manager.set_logged_in(True)
|
||||
assert state_manager.is_logged_in() is True
|
||||
|
||||
# Simulate session expiration detection (zero listings threshold reached)
|
||||
zero_listings_count = 3
|
||||
max_zero = 3
|
||||
|
||||
if zero_listings_count >= max_zero:
|
||||
state_manager.set_logged_in(False)
|
||||
|
||||
# Verify session was reset
|
||||
assert state_manager.is_logged_in() is False
|
||||
Loading…
Add table
Add a link
Reference in a new issue