"""Публичная витрина показывает только общий нормативный корпус.""" import json import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) def _tsn(root: Path, key: str, title: str, fetched: str): d = root / "snapshots" / "niac_tsn" / "collections" / key d.mkdir(parents=True) (d / "extract.json").write_text("{}", encoding="utf-8") (d / "meta.json").write_text(json.dumps({"title": title, "fetched_at": fetched, "key": key}), encoding="utf-8") def _bim(root: Path, key: str, fetched: str): d = root / "snapshots" / "fgis" / "bim_letters" / key d.mkdir(parents=True) (d / "letter.pdf").write_bytes(b"%PDF-1.4") (d / "meta.json").write_text(json.dumps({"source": "fgis_bim", "fetched_at": fetched}), encoding="utf-8") def test_counts_only_available_public_documents_and_orders_feed(tmp_path, monkeypatch): import ext_state import public_documents monkeypatch.setattr(ext_state, "ext_dir", lambda: tmp_path) public_documents.clear_cache() _tsn(tmp_path, "older", "ТСН: старый", "2026-10-01T10:00:00Z") _tsn(tmp_path, "newer", "ТСН: новый", "2026-11-18T10:00:00Z") _bim(tmp_path, "letter", "2026-11-01T10:00:00Z") incomplete = tmp_path / "snapshots" / "niac_tsn" / "collections" / "incomplete" incomplete.mkdir() (incomplete / "meta.json").write_text('{"title":"не загружен"}', encoding="utf-8") # Пользовательские файлы не могут попасть в публичную выборку. private = tmp_path / "users" / "secret" private.mkdir(parents=True) (private / "contract.pdf").write_bytes(b"private") result = public_documents.snapshot() assert result["count"] == 3 assert [d["title"] for d in result["recent"]] == [ "ТСН: новый", "Письмо-индекс Минстроя", "ТСН: старый"] assert all("contract" not in str(d) for d in result["recent"]) def test_public_markup_escapes_titles_and_has_honest_empty_state(tmp_path, monkeypatch): import ext_state import public_documents monkeypatch.setattr(ext_state, "ext_dir", lambda: tmp_path) public_documents.clear_cache() html = public_documents.render() assert "Документы пока не загружены" in html assert 'id="public-doc-count">0<' in html _tsn(tmp_path, "xss", '', "2026-11-18T10:00:00Z") public_documents.clear_cache() html = public_documents.render() assert "<img" in html assert "1<' in html def test_landing_contains_public_documents_section(): html = (ROOT / "static" / "chat.html").read_text(encoding="utf-8") assert 'id="public-documents"' in html assert "{{PUBLIC_DOCUMENTS}}" in html def test_homepage_renders_public_feed_without_login(tmp_path, monkeypatch): import ext_state import public_documents import main from fastapi.testclient import TestClient monkeypatch.chdir(ROOT) monkeypatch.setattr(ext_state, "ext_dir", lambda: tmp_path) public_documents.clear_cache() _tsn(tmp_path, "new", "ТСН: опубликованный", "2026-11-18T10:00:00Z") response = TestClient(main.app).get("/?v=" + main.APP_VERSION) assert response.status_code == 200 assert 'id="public-doc-count">1<' in response.text assert "ТСН: опубликованный" in response.text assert "{{PUBLIC_DOCUMENTS}}" not in response.text