|
| 1 | +"""OCR-based validation for TUI demos. |
| 2 | +
|
| 3 | +TUI demos (Zellij, interactive UIs) can't be validated via text output because |
| 4 | +VHS only captures the outer terminal, not content rendered inside terminal |
| 5 | +multiplexers. Instead, we extract key frames from the GIF and use OCR to verify |
| 6 | +expected content appears. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + from shared.validation import validate_tui_demo, TUI_CHECKPOINTS |
| 10 | +
|
| 11 | + # Validate after building |
| 12 | + errors = validate_tui_demo("wt-zellij-omnibus", gif_path) |
| 13 | + if errors: |
| 14 | + print("Validation failed:", errors) |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import subprocess |
| 20 | +import tempfile |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +# Checkpoint definitions per TUI demo |
| 24 | +# Format: {demo_name: [(frame_number, expected_patterns, forbidden_patterns), ...]} |
| 25 | +# |
| 26 | +# Frame numbers are calibrated from actual GIF content at 30fps. |
| 27 | +# Expected patterns must ALL be present (case-insensitive). |
| 28 | +# Forbidden patterns must ALL be absent (case-insensitive). |
| 29 | + |
| 30 | +TUI_CHECKPOINTS: dict[str, list[tuple[int, list[str], list[str]]]] = { |
| 31 | + # Frame numbers calibrated with permissions cache fix (no permission dialog). |
| 32 | + # At 30fps: frame 100 = ~3.3s, frame 500 = ~16.7s, frame 2000 = ~66.7s |
| 33 | + "wt-zellij-omnibus": [ |
| 34 | + # Frame 100: After wt list - should see branch table, no permission dialog |
| 35 | + (100, ["Branch", "Status", "main", "hooks"], ["Allow?", "permission"]), |
| 36 | + # Frame 500: Claude UI visible - shows Opus model indicator and worktree |
| 37 | + (500, ["Opus", "acme"], ["command not found", "Unknown command"]), |
| 38 | + # Frame 2000: Near end - wt list --full showing all worktrees |
| 39 | + (2000, ["Branch", "main", "feature", "billing"], ["CONFLICT", "error:", "failed"]), |
| 40 | + ], |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +def check_dependencies() -> list[str]: |
| 45 | + """Check that required tools are available. Returns list of missing tools.""" |
| 46 | + missing = [] |
| 47 | + for cmd in ["ffmpeg", "tesseract"]: |
| 48 | + result = subprocess.run( |
| 49 | + ["which", cmd], capture_output=True, text=True |
| 50 | + ) |
| 51 | + if result.returncode != 0: |
| 52 | + missing.append(cmd) |
| 53 | + return missing |
| 54 | + |
| 55 | + |
| 56 | +def extract_frame(gif_path: Path, frame_number: int, output_path: Path) -> bool: |
| 57 | + """Extract a single frame from a GIF. Returns True on success.""" |
| 58 | + result = subprocess.run( |
| 59 | + [ |
| 60 | + "ffmpeg", |
| 61 | + "-loglevel", "error", |
| 62 | + "-i", str(gif_path), |
| 63 | + "-vf", f"select=eq(n\\,{frame_number})", |
| 64 | + "-vframes", "1", |
| 65 | + "-update", "1", |
| 66 | + str(output_path), |
| 67 | + ], |
| 68 | + capture_output=True, |
| 69 | + ) |
| 70 | + return result.returncode == 0 and output_path.exists() |
| 71 | + |
| 72 | + |
| 73 | +def ocr_image(image_path: Path) -> str: |
| 74 | + """Run OCR on an image and return the extracted text.""" |
| 75 | + with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as f: |
| 76 | + output_base = f.name[:-4] # Remove .txt suffix for tesseract |
| 77 | + |
| 78 | + result = subprocess.run( |
| 79 | + ["tesseract", str(image_path), output_base, "-l", "eng"], |
| 80 | + capture_output=True, |
| 81 | + ) |
| 82 | + |
| 83 | + output_path = Path(f"{output_base}.txt") |
| 84 | + if result.returncode == 0 and output_path.exists(): |
| 85 | + text = output_path.read_text() |
| 86 | + output_path.unlink() |
| 87 | + return text |
| 88 | + return "" |
| 89 | + |
| 90 | + |
| 91 | +def validate_checkpoint( |
| 92 | + gif_path: Path, |
| 93 | + frame_number: int, |
| 94 | + expected: list[str], |
| 95 | + forbidden: list[str], |
| 96 | + work_dir: Path, |
| 97 | +) -> list[str]: |
| 98 | + """Validate a single checkpoint. Returns list of error messages.""" |
| 99 | + errors = [] |
| 100 | + |
| 101 | + # Extract frame |
| 102 | + frame_path = work_dir / f"frame_{frame_number}.png" |
| 103 | + if not extract_frame(gif_path, frame_number, frame_path): |
| 104 | + return [f"Failed to extract frame {frame_number}"] |
| 105 | + |
| 106 | + # OCR the frame |
| 107 | + text = ocr_image(frame_path) |
| 108 | + if not text: |
| 109 | + return [f"OCR failed for frame {frame_number}"] |
| 110 | + |
| 111 | + text_lower = text.lower() |
| 112 | + |
| 113 | + # Check expected patterns |
| 114 | + for pattern in expected: |
| 115 | + if pattern.lower() not in text_lower: |
| 116 | + errors.append(f"Expected pattern not found: '{pattern}'") |
| 117 | + |
| 118 | + # Check forbidden patterns |
| 119 | + for pattern in forbidden: |
| 120 | + if pattern.lower() in text_lower: |
| 121 | + errors.append(f"Forbidden pattern found: '{pattern}'") |
| 122 | + |
| 123 | + return errors |
| 124 | + |
| 125 | + |
| 126 | +def validate_tui_demo(demo_name: str, gif_path: Path) -> list[str]: |
| 127 | + """Validate a TUI demo GIF against its checkpoints. |
| 128 | +
|
| 129 | + Args: |
| 130 | + demo_name: Name of the demo (e.g., "wt-zellij-omnibus") |
| 131 | + gif_path: Path to the GIF file to validate |
| 132 | +
|
| 133 | + Returns: |
| 134 | + List of error messages. Empty list means validation passed. |
| 135 | + """ |
| 136 | + if demo_name not in TUI_CHECKPOINTS: |
| 137 | + return [f"No checkpoints defined for demo: {demo_name}"] |
| 138 | + |
| 139 | + if not gif_path.exists(): |
| 140 | + return [f"GIF not found: {gif_path}"] |
| 141 | + |
| 142 | + # Check dependencies |
| 143 | + missing = check_dependencies() |
| 144 | + if missing: |
| 145 | + return [f"Missing required tools: {', '.join(missing)}"] |
| 146 | + |
| 147 | + checkpoints = TUI_CHECKPOINTS[demo_name] |
| 148 | + all_errors = [] |
| 149 | + |
| 150 | + with tempfile.TemporaryDirectory(prefix="wt-validate-") as work_dir: |
| 151 | + work_path = Path(work_dir) |
| 152 | + |
| 153 | + for frame_number, expected, forbidden in checkpoints: |
| 154 | + errors = validate_checkpoint( |
| 155 | + gif_path, frame_number, expected, forbidden, work_path |
| 156 | + ) |
| 157 | + if errors: |
| 158 | + all_errors.append(f"Frame {frame_number}: {'; '.join(errors)}") |
| 159 | + |
| 160 | + return all_errors |
| 161 | + |
| 162 | + |
| 163 | +def validate_tui_demo_verbose(demo_name: str, gif_path: Path) -> tuple[bool, str]: |
| 164 | + """Validate a TUI demo with verbose output. |
| 165 | +
|
| 166 | + Returns: |
| 167 | + (success, output_message) |
| 168 | + """ |
| 169 | + lines = [f"Validating {demo_name}: {gif_path}"] |
| 170 | + |
| 171 | + if demo_name not in TUI_CHECKPOINTS: |
| 172 | + return False, f"No checkpoints defined for demo: {demo_name}" |
| 173 | + |
| 174 | + if not gif_path.exists(): |
| 175 | + return False, f"GIF not found: {gif_path}" |
| 176 | + |
| 177 | + missing = check_dependencies() |
| 178 | + if missing: |
| 179 | + return False, f"Missing required tools: {', '.join(missing)}" |
| 180 | + |
| 181 | + checkpoints = TUI_CHECKPOINTS[demo_name] |
| 182 | + all_passed = True |
| 183 | + |
| 184 | + with tempfile.TemporaryDirectory(prefix="wt-validate-") as work_dir: |
| 185 | + work_path = Path(work_dir) |
| 186 | + |
| 187 | + for frame_number, expected, forbidden in checkpoints: |
| 188 | + errors = validate_checkpoint( |
| 189 | + gif_path, frame_number, expected, forbidden, work_path |
| 190 | + ) |
| 191 | + if errors: |
| 192 | + lines.append(f" ✗ Frame {frame_number}") |
| 193 | + for error in errors: |
| 194 | + lines.append(f" - {error}") |
| 195 | + all_passed = False |
| 196 | + else: |
| 197 | + lines.append(f" ✓ Frame {frame_number}") |
| 198 | + |
| 199 | + if all_passed: |
| 200 | + lines.append("✓ All checkpoints passed") |
| 201 | + else: |
| 202 | + lines.append("✗ Some checkpoints failed") |
| 203 | + |
| 204 | + return all_passed, "\n".join(lines) |
0 commit comments