Files
claude-seo/tests/test_fetch_page_decoding.py
Agrici DanielandGPT-5 00ce62da15 chore(lint): clean ruff findings
Normalize imports and remove Ruff-reported unused code and
redundant f-string prefixes across tracked scripts, extensions, and
tests.

Preserve the parse_html script-local import guard because it is
required when the script is loaded outside normal CLI execution. No
release metadata, skill instructions, or private-mirror files changed.

Verified:
- ruff check .: passed with 0 findings
- python3 -m pytest tests/ -q: 439 passed
- python3 scripts/portability_check.py --strict: 0 errors, 0 warnings
- python3 scripts/consistency_check.py --strict: passed, 380 files
- python3 -m compileall -q scripts hooks tests extensions: passed
- pip-audit -r requirements.txt: no known vulnerabilities
- plugin JSON parsing with jq: passed
- staged diff and added-line em dash scans: passed

Known remaining issues: no live API integration run, because
credentials and external service behavior are outside this mechanical
lint cleanup.

Co-Authored-By: GPT-5 <noreply@openai.com>
2026-08-26 14:23:53 +03:00

43 lines
1.4 KiB
Python

"""Deterministic response decoding for fetch_page.py."""
from __future__ import annotations
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
SCRIPTS_DIR = REPO_ROOT / "scripts"
if str(SCRIPTS_DIR) not in sys.path:
sys.path.insert(0, str(SCRIPTS_DIR))
import fetch_page # noqa: E402
class FakeResponse:
def __init__(self, content: bytes, content_type: str = "") -> None:
self.content = content
self.headers = {"Content-Type": content_type} if content_type else {}
def test_no_charset_defaults_to_utf8_with_replacement() -> None:
response = FakeResponse("Café".encode("utf-8"))
assert fetch_page._decode_response_content(response) == "Café"
def test_explicit_charset_from_content_type_wins() -> None:
response = FakeResponse("Café".encode("iso-8859-1"), "text/html; charset=iso-8859-1")
assert fetch_page._decode_response_content(response) == "Café"
def test_meta_charset_is_used_when_header_has_no_charset() -> None:
html = '<meta charset="windows-1252"><p>Smart quote: “</p>'.encode("windows-1252")
response = FakeResponse(html, "text/html")
assert "Smart quote: “" in fetch_page._decode_response_content(response)
def test_invalid_bytes_are_replaced_not_dropped() -> None:
response = FakeResponse(b"valid utf8 then invalid: \x8f")
decoded = fetch_page._decode_response_content(response)
assert "valid utf8" in decoded
assert "\ufffd" in decoded