| 1 | """Empty and qualifier-only GitHub topics must not be classified as ERROR (#953). |
| 2 | |
| 3 | Pre-#949, search_github('') built a site-wide created:> window query. |
| 4 | The qualifier strip then returned an error envelope with no network call, |
| 5 | and pipeline._result_outcome_artifact mapped that error to a failed attempt. |
| 6 | """ |
| 7 | |
| 8 | from unittest.mock import patch |
| 9 | |
| 10 | import pytest |
| 11 | |
| 12 | from lib import github, pipeline, schema |
| 13 | |
| 14 | |
| 15 | def _search(topic): |
| 16 | with patch.object(github, "_resolve_token", return_value="test-token"): |
| 17 | with patch.object(github, "_fetch_json") as mock_fetch: |
| 18 | result = github.search_github(topic, "2026-07-01", "2026-07-31") |
| 19 | return result, mock_fetch |
| 20 | |
| 21 | |
| 22 | def _bundle_from_envelope(envelope): |
| 23 | artifact = pipeline._result_outcome_artifact("github", envelope) |
| 24 | bundle = schema.RetrievalBundle() |
| 25 | bundle.mark_attempted("github") |
| 26 | outcome = artifact.get("_source_outcome") if isinstance(artifact, dict) else None |
| 27 | if isinstance(outcome, dict): |
| 28 | bundle.record_failure( |
| 29 | "github", |
| 30 | outcome["state"], |
| 31 | outcome["detail"], |
| 32 | attempted=outcome.get("attempted", True), |
| 33 | ) |
| 34 | items = github.parse_github_response(envelope) |
| 35 | bundle.add_items("primary", "github", items) |
| 36 | return artifact, bundle |
| 37 | |
| 38 | |
| 39 | def test_empty_topic_is_clean_no_results_not_error(): |
| 40 | result, mock_fetch = _search("") |
| 41 | mock_fetch.assert_not_called() |
| 42 | assert result["items"] == [] |
| 43 | assert "error" not in result |
| 44 | artifact, bundle = _bundle_from_envelope(result) |
| 45 | assert artifact == {} |
| 46 | assert "github" not in bundle.errors_by_source |
| 47 | outcome = bundle.source_status["github"] |
| 48 | assert outcome.state == schema.NO_RESULTS |
| 49 | assert outcome.attempted is True |
| 50 | |
| 51 | |
| 52 | def test_qualifier_only_topic_is_clean_no_results_not_error(): |
| 53 | result, mock_fetch = _search("created:>2025-03-20") |
| 54 | mock_fetch.assert_not_called() |
| 55 | assert result["items"] == [] |
| 56 | assert "error" not in result |
| 57 | artifact, bundle = _bundle_from_envelope(result) |
| 58 | assert artifact == {} |
| 59 | assert "github" not in bundle.errors_by_source |
| 60 | assert bundle.source_status["github"].state == schema.NO_RESULTS |
| 61 | |
| 62 | |
| 63 | def test_noise_plus_qualifier_topic_is_clean_no_results_not_error(): |
| 64 | # extract_core_subject('the best stars:>1000') -> 'stars:>1000' -> plain '' |
| 65 | result, mock_fetch = _search("the best stars:>1000") |
| 66 | mock_fetch.assert_not_called() |
| 67 | assert result["items"] == [] |
| 68 | assert "error" not in result |
| 69 | artifact, bundle = _bundle_from_envelope(result) |
| 70 | assert artifact == {} |
| 71 | assert "github" not in bundle.errors_by_source |
| 72 | assert bundle.source_status["github"].state == schema.NO_RESULTS |
| 73 | |
| 74 | |
| 75 | def test_quote_wrapped_qualifier_only_topic_is_clean_no_results_not_error(): |
| 76 | # Wrapper shapes from #952 still strip to nothing; #953 is the envelope. |
| 77 | result, mock_fetch = _search('"created:>2025-03-20"') |
| 78 | mock_fetch.assert_not_called() |
| 79 | assert result["items"] == [] |
| 80 | assert "error" not in result |
| 81 | artifact, bundle = _bundle_from_envelope(result) |
| 82 | assert artifact == {} |
| 83 | assert "github" not in bundle.errors_by_source |
| 84 | assert bundle.source_status["github"].state == schema.NO_RESULTS |
| 85 | |
| 86 | |
| 87 | @pytest.mark.parametrize("topic", [ |
| 88 | "(created:>2025-03-20", |
| 89 | "created:>2025-03-20)", |
| 90 | '(("created:>2025-03-20"))', |
| 91 | '[(("created:>2025-03-20"))', |
| 92 | "[is:issue", |
| 93 | "is:issue]", |
| 94 | "(created:>2025-03-20 [stars:>1000", |
| 95 | "[\u00a0created:>2025-03-20", |
| 96 | ]) |
| 97 | def test_malformed_wrapper_qualifier_only_topic_skips_network(topic): |
| 98 | result, mock_fetch = _search(topic) |
| 99 | mock_fetch.assert_not_called() |
| 100 | assert result["items"] == [] |
| 101 | assert "error" not in result |
| 102 | artifact, bundle = _bundle_from_envelope(result) |
| 103 | assert artifact == {} |
| 104 | assert "github" not in bundle.errors_by_source |
| 105 | assert bundle.source_status["github"].state == schema.NO_RESULTS |
| 106 |