"""Browser layout check for a built video lesson. The compile/render check proves the lesson runs; it cannot see that a drawing crosses a title or that a label lands on a card. This opens the finished page in headless Chrome at sample frames, measures every visible text box and samples points along every visible SVG stroke, and reports collisions in the 1920x1080 composition's own coordinates so the writer can move things. """ from __future__ import annotations import json import os from pathlib import Path import re import shutil import subprocess # nosec B404 - fixed argv, local file only import tempfile CAPTION_TOP = 880 # the kit's captions own everything below this line _MAX_ISSUES = 14 _PROBE = r""" """ _HOST = """
""" def chrome_path() -> str | None: """Use an explicit browser first, then standard cross-platform installs.""" configured = os.environ.get("DEEPTUTOR_CHROME_EXECUTABLE", "").strip() if configured: path = Path(configured).expanduser() return str(path) if path.is_file() else None for name in ( "google-chrome", "google-chrome-stable", "chromium", "chromium-browser", "chrome", "msedge", ): found = shutil.which(name) if found: return found candidates = [ Path("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"), Path.home() / "Applications/Google Chrome.app/Contents/MacOS/Google Chrome", Path("/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"), ] for name in ("PROGRAMFILES", "PROGRAMFILES(X86)", "LOCALAPPDATA"): root = os.environ.get(name) if root: candidates.extend( Path(root) / suffix for suffix in ( "Google/Chrome/Application/chrome.exe", "Microsoft/Edge/Application/msedge.exe", ) ) return next((str(path) for path in candidates if path.is_file()), None) def valid_scene_lengths(lengths: object, total: int) -> bool: """Validate bounded lengths before using sandbox output for browser sampling.""" return ( isinstance(lengths, list) and 0 < len(lengths) <= 64 and all(type(length) is int and 0 < length <= total for length in lengths) and sum(lengths) == total ) def scene_sample_points(lengths: list[int], fps: int = 30) -> set[int]: """Scene starts, early frames, and last frames on the fixed player timeline.""" starts = [] start = 0 for length in lengths: starts.append(start) start += length return { point for start, length in zip(starts, lengths) for point in (start, start + min(length - 1, max(1, fps // 2)), start + length - 1) } def sample_frames( total: int, fps: int = 30, every_s: float = 4.0, *, scene_lengths: list[int] | None = None ) -> list[int]: """At most 48 samples, prioritizing validated scene transitions.""" if total <= 1: return [0] chosen = {0} if valid_scene_lengths(scene_lengths, total): starts = [] start = 0 for length in scene_lengths: starts.append((start, length)) start += length for points in ( [start for start, _ in starts], [start + min(length - 1, max(1, fps // 2)) for start, length in starts], [start + length - 1 for start, length in starts], ): for point in points: if len(chosen) == 48: break chosen.add(point) step = max(1, int(every_s * fps)) periodic = range(max(1, step // 2), max(total - 1, 1), step) remaining = 48 - len(chosen) if remaining > 0 and periodic: if len(periodic) > remaining: indices = [ round(i * (len(periodic) - 1) / max(1, remaining - 1)) for i in range(remaining) ] chosen.update(periodic[i] for i in indices) else: chosen.update(periodic) return sorted(chosen) def check_layout( page: Path, frames: list[int], timeout_s: int = 180, *, require_player_frame: bool = False, expected_scene_lengths: list[int] | None = None, ) -> list[str] | None: """Collision report; ``None`` if a browser or a required frame is unavailable.""" chrome = chrome_path() if not chrome or not frames: return None probe = ( _PROBE.replace("__CAPTION_TOP__", str(CAPTION_TOP)) .replace("__STRICT_FRAME__", "true" if require_player_frame else "false") .replace("__EXPECTED_SCENES__", json.dumps(expected_scene_lengths)) ) with tempfile.TemporaryDirectory(prefix="vl-layout-") as tmp: html = page.read_text(encoding="utf-8") Path(tmp, "probe.html").write_text( html.replace("