"""Check Files smoothness safeguards added in v2026.06.20.31."""
from pathlib import Path
import json
from datetime import datetime

ROOT = Path(__file__).resolve().parent.parent
REPORT = ROOT / "data" / "files_smoothness_latest.json"

files_page = (ROOT / "pages" / "files_page.py").read_text(encoding="utf-8", errors="ignore")
app_py = (ROOT / "app.py").read_text(encoding="utf-8", errors="ignore")

checks = {
    "search_text_cache": "_cached_doc_search_text" in files_page and "_file_search_cache" in files_page,
    "cache_pruning": "_prune_file_search_cache" in files_page,
    "pending_change_flush": "def flush_pending_changes" in files_page,
    "page_switch_flush": 'flush_pending_changes("switch_page")' in app_py,
    "exit_flush": 'flush_pending_changes("exit_app")' in app_py,
    "large_list_chunk_guard": "len(items) > 220" in files_page,
}
issues = [name for name, ok in checks.items() if not ok]
payload = {
    "time": datetime.now().isoformat(timespec="seconds"),
    "status": "OK" if not issues else "FAILED",
    "checks": checks,
    "issues": issues,
}
REPORT.parent.mkdir(parents=True, exist_ok=True)
REPORT.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
print("Files smoothness check", payload["status"])
if issues:
    for issue in issues:
        print("-", issue)
raise SystemExit(0 if not issues else 1)
