|
| 1 | +""" |
| 2 | +Tests for configuration validation. |
| 3 | +
|
| 4 | +Verifies that config validation catches invalid parameter combinations. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(autouse=True) |
| 13 | +def setup_test_env(): |
| 14 | + """Set up environment for each test, restoring original values after.""" |
| 15 | + from hindsight_api.config import clear_config_cache |
| 16 | + |
| 17 | + # Save original environment values |
| 18 | + env_vars_to_save = [ |
| 19 | + "HINDSIGHT_API_RETAIN_MAX_COMPLETION_TOKENS", |
| 20 | + "HINDSIGHT_API_RETAIN_CHUNK_SIZE", |
| 21 | + "HINDSIGHT_API_LLM_PROVIDER", |
| 22 | + "HINDSIGHT_API_LLM_MODEL", |
| 23 | + ] |
| 24 | + |
| 25 | + # Save original values |
| 26 | + original_values = {} |
| 27 | + for key in env_vars_to_save: |
| 28 | + original_values[key] = os.environ.get(key) |
| 29 | + |
| 30 | + clear_config_cache() |
| 31 | + |
| 32 | + yield |
| 33 | + |
| 34 | + # Restore original environment |
| 35 | + for key, original_value in original_values.items(): |
| 36 | + if original_value is None: |
| 37 | + os.environ.pop(key, None) |
| 38 | + else: |
| 39 | + os.environ[key] = original_value |
| 40 | + |
| 41 | + clear_config_cache() |
| 42 | + |
| 43 | + |
| 44 | +def test_retain_max_completion_tokens_must_be_greater_than_chunk_size(): |
| 45 | + """Test that RETAIN_MAX_COMPLETION_TOKENS > RETAIN_CHUNK_SIZE validation works.""" |
| 46 | + from hindsight_api.config import HindsightConfig |
| 47 | + |
| 48 | + # Set invalid config: max_completion_tokens <= chunk_size |
| 49 | + os.environ["HINDSIGHT_API_RETAIN_MAX_COMPLETION_TOKENS"] = "1000" |
| 50 | + os.environ["HINDSIGHT_API_RETAIN_CHUNK_SIZE"] = "2000" |
| 51 | + os.environ["HINDSIGHT_API_LLM_PROVIDER"] = "mock" |
| 52 | + |
| 53 | + # Should raise ValueError with helpful message |
| 54 | + with pytest.raises(ValueError) as exc_info: |
| 55 | + HindsightConfig.from_env() |
| 56 | + |
| 57 | + error_message = str(exc_info.value) |
| 58 | + |
| 59 | + # Verify error message contains helpful information |
| 60 | + assert "HINDSIGHT_API_RETAIN_MAX_COMPLETION_TOKENS" in error_message |
| 61 | + assert "1000" in error_message |
| 62 | + assert "HINDSIGHT_API_RETAIN_CHUNK_SIZE" in error_message |
| 63 | + assert "2000" in error_message |
| 64 | + assert "must be greater than" in error_message |
| 65 | + assert "You have two options to fix this:" in error_message |
| 66 | + assert "Increase HINDSIGHT_API_RETAIN_MAX_COMPLETION_TOKENS" in error_message |
| 67 | + assert "Use a model that supports" in error_message |
| 68 | + |
| 69 | + |
| 70 | +def test_retain_max_completion_tokens_equal_to_chunk_size_fails(): |
| 71 | + """Test that RETAIN_MAX_COMPLETION_TOKENS == RETAIN_CHUNK_SIZE also fails.""" |
| 72 | + from hindsight_api.config import HindsightConfig |
| 73 | + |
| 74 | + # Set invalid config: max_completion_tokens == chunk_size |
| 75 | + os.environ["HINDSIGHT_API_RETAIN_MAX_COMPLETION_TOKENS"] = "3000" |
| 76 | + os.environ["HINDSIGHT_API_RETAIN_CHUNK_SIZE"] = "3000" |
| 77 | + os.environ["HINDSIGHT_API_LLM_PROVIDER"] = "mock" |
| 78 | + |
| 79 | + # Should raise ValueError |
| 80 | + with pytest.raises(ValueError) as exc_info: |
| 81 | + HindsightConfig.from_env() |
| 82 | + |
| 83 | + error_message = str(exc_info.value) |
| 84 | + assert "must be greater than" in error_message |
| 85 | + |
| 86 | + |
| 87 | +def test_valid_retain_config_succeeds(): |
| 88 | + """Test that valid config with max_completion_tokens > chunk_size works.""" |
| 89 | + from hindsight_api.config import HindsightConfig |
| 90 | + |
| 91 | + # Set valid config: max_completion_tokens > chunk_size |
| 92 | + os.environ["HINDSIGHT_API_RETAIN_MAX_COMPLETION_TOKENS"] = "64000" |
| 93 | + os.environ["HINDSIGHT_API_RETAIN_CHUNK_SIZE"] = "3000" |
| 94 | + os.environ["HINDSIGHT_API_LLM_PROVIDER"] = "mock" |
| 95 | + |
| 96 | + # Should not raise |
| 97 | + config = HindsightConfig.from_env() |
| 98 | + assert config.retain_max_completion_tokens == 64000 |
| 99 | + assert config.retain_chunk_size == 3000 |
| 100 | + |
| 101 | + |
| 102 | +# Note: The BadRequestError wrapping is implemented in fact_extraction.py |
| 103 | +# but requires a complex integration test setup. The functionality is |
| 104 | +# straightforward: when a BadRequestError containing keywords like |
| 105 | +# "max_tokens", "max_completion_tokens", or "maximum context" is caught, |
| 106 | +# it's wrapped in a ValueError with helpful guidance. |
| 107 | +# |
| 108 | +# The config validation tests above ensure users get early feedback |
| 109 | +# about invalid configurations before runtime errors occur. |
0 commit comments