Files
wehub-resource-sync 7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

122 lines
4.6 KiB
Python

# allow: no-sut-import — guardian; sweeps engine source files to enforce the centralized USER_AGENT constant
"""Regression tests for the User-Agent invariant across search engines.
Follow-up to issue #4130: nine search-engine modules used to hand-roll
their own User-Agent strings (mostly ``"LocalDeepResearch/1.0 (...)"``
with the version pinned to 1.0, or ad-hoc ``"Local-Deep-Research-Agent"``)
instead of referencing the centralized ``USER_AGENT`` constant. The
strings drifted over time and never carried the actual project version.
This test sweeps the engine source files and asserts that any
``"User-Agent": ...`` header set against the canonical ``USER_AGENT``
constant rather than a literal string, so future copy-pastes can't
silently re-introduce stale version numbers.
Engines that are deliberately NOT included here:
* ``search_engine_searxng.py`` — sends a full browser UA + Referer +
Upgrade-Insecure-Requests on purpose, because some public SearXNG
instances rate-limit non-browser scrapers. Touching this risks
breaking real users.
* ``search_engine_wayback.py`` — uses a hybrid bot-identifying UA
(``"Mozilla/5.0 (Local Deep Research Bot; research project)"``)
that is honest, just not the canonical constant.
"""
import re
from pathlib import Path
import pytest
SRC_ROOT = (
Path(__file__).resolve().parents[2]
/ "src"
/ "local_deep_research"
/ "web_search_engines"
/ "engines"
)
# Engines that are required to use ``USER_AGENT`` (with or without a
# polite-pool suffix). See module docstring for the rationale behind
# the SearXNG/Wayback exclusions.
CANONICAL_UA_ENGINES = [
"search_engine_github.py",
"search_engine_gutenberg.py",
"search_engine_stackexchange.py",
"search_engine_wikinews.py",
"search_engine_pubchem.py",
"search_engine_openlibrary.py",
"search_engine_nasa_ads.py",
"search_engine_zenodo.py",
"search_engine_openalex.py",
]
def _source(filename: str) -> str:
path = SRC_ROOT / filename
assert path.is_file(), f"engine source not found: {path}"
return path.read_text(encoding="utf-8")
# The pre-fix strings that must never reappear. These are the exact
# literals that lived in the source before the #4130 follow-up. A more
# general pattern is enforced separately by
# ``test_no_string_literal_user_agent``.
_LEGACY_LITERALS = (
"LocalDeepResearch/1.0 (https://github.com/LearningCircuit/local-deep-research)",
"Local-Deep-Research-Agent",
"local-deep-research-wikinews-search-engine",
)
@pytest.mark.parametrize("filename", CANONICAL_UA_ENGINES)
def test_no_legacy_user_agent_string(filename):
"""No engine should hand-roll one of the pre-fix UA literals."""
src = _source(filename)
for legacy in _LEGACY_LITERALS:
assert legacy not in src, (
f"{filename} still contains the legacy UA literal {legacy!r}. "
"Use USER_AGENT from local_deep_research.constants instead."
)
@pytest.mark.parametrize("filename", CANONICAL_UA_ENGINES)
def test_user_agent_constant_is_imported(filename):
"""USER_AGENT must be imported from the central constants module."""
src = _source(filename)
assert re.search(
r"from\s+\.{2,3}constants\s+import\s+[^\n]*\bUSER_AGENT\b", src
), (
f"{filename} does not import USER_AGENT from ...constants — the "
"canonical source of truth for the project User-Agent."
)
@pytest.mark.parametrize("filename", CANONICAL_UA_ENGINES)
def test_no_string_literal_user_agent(filename):
"""Any ``"User-Agent": ...`` header must reference USER_AGENT, not a
bare string literal.
Catches new hand-rolled UAs even if they don't match one of the
historical legacy literals — e.g. someone bumping the version by
hand to ``"LocalDeepResearch/2.0 (...)"``.
"""
src = _source(filename)
# Find every ``"User-Agent": <value>`` site. The regex stops at the
# first comma or closing brace so multi-key dicts work.
matches = re.findall(r'"User-Agent"\s*:\s*([^,\n}]+?)(?=[,\n}])', src)
assert matches, (
f"Sanity check: expected at least one User-Agent header in "
f"{filename}, found none — the test is checking the wrong thing."
)
for value in matches:
stripped = value.strip()
# Accept either the bare constant, or an f-string composition
# involving the constant (used for polite-pool email suffixing
# in OpenAlex).
assert "USER_AGENT" in stripped, (
f"{filename} sets User-Agent to {stripped!r}, which does not "
"reference the USER_AGENT constant. Use USER_AGENT (or a "
"composition like f'{USER_AGENT} ({email})') instead."
)