"""Tests to verify except clauses capture the exception variable and that
debug logging has been added in strategy modules (PR #2010)."""

import inspect


class TestBareExceptFixes:
    """Verify that except clauses use 'except Exception as e' (not bare
    'except Exception:') and that debug logging is present."""

    def test_synonym_expansion_captures_exception(self):
        from local_deep_research.advanced_search_system.candidate_exploration.adaptive_explorer import (
            AdaptiveExplorer,
        )

        source = inspect.getsource(AdaptiveExplorer._synonym_expansion_query)
        assert "except Exception as e" in source, (
            "_synonym_expansion_query should use 'except Exception as e'"
        )

    def test_synonym_expansion_logs_error(self):
        from local_deep_research.advanced_search_system.candidate_exploration.adaptive_explorer import (
            AdaptiveExplorer,
        )

        source = inspect.getsource(AdaptiveExplorer._synonym_expansion_query)
        assert "logger.debug" in source, (
            "_synonym_expansion_query should have logger.debug for exceptions"
        )

    def test_related_terms_captures_exception(self):
        from local_deep_research.advanced_search_system.candidate_exploration.adaptive_explorer import (
            AdaptiveExplorer,
        )

        source = inspect.getsource(AdaptiveExplorer._related_terms_query)
        assert "except Exception as e" in source, (
            "_related_terms_query should use 'except Exception as e'"
        )

    def test_related_terms_logs_error(self):
        from local_deep_research.advanced_search_system.candidate_exploration.adaptive_explorer import (
            AdaptiveExplorer,
        )

        source = inspect.getsource(AdaptiveExplorer._related_terms_query)
        assert "logger.debug" in source, (
            "_related_terms_query should have logger.debug for exceptions"
        )

    def test_get_synonyms_captures_exception(self):
        from local_deep_research.advanced_search_system.strategies.evidence_based_strategy_v2 import (
            EnhancedEvidenceBasedStrategy,
        )

        source = inspect.getsource(EnhancedEvidenceBasedStrategy._get_synonyms)
        assert "except Exception as e" in source, (
            "_get_synonyms should use 'except Exception as e'"
        )

    def test_get_synonyms_logs_error(self):
        from local_deep_research.advanced_search_system.strategies.evidence_based_strategy_v2 import (
            EnhancedEvidenceBasedStrategy,
        )

        source = inspect.getsource(EnhancedEvidenceBasedStrategy._get_synonyms)
        assert "logger.debug" in source, (
            "_get_synonyms should have logger.debug for exceptions"
        )
