#!/bin/bash # Test script to verify event-triggered screenshot protection # Tests BOTH metadata and latest.jpg file protection set -e SCREENSHOT_DIR="src/screenshots" META_FILE="$SCREENSHOT_DIR/meta.json" LATEST_FILE="$SCREENSHOT_DIR/latest.jpg" echo "=== Screenshot Event-Triggered Protection Test ===" echo "" echo "Testing that periodic screenshots don't overwrite event-triggered captures..." echo "" # Create test directory if needed mkdir -p "$SCREENSHOT_DIR" # Step 1: Create mock event_start screenshot and metadata echo "Step 1: Simulating event_start screenshot and metadata..." echo "MOCK EVENT_START IMAGE" > "$LATEST_FILE" cat > "$META_FILE" << 'EOF' {"captured_at": "2026-03-29T10:05:33.516Z", "file": "screenshot_20260329_100533.jpg", "type": "event_start", "send_immediately": true} EOF echo " Created: $META_FILE" echo " Created: $LATEST_FILE (with event_start content)" echo "" # Step 2: Simulate periodic screenshot capture echo "Step 2: Simulating periodic screenshot capture (should NOT overwrite meta or latest.jpg)..." python3 << 'PYEOF' import json import os import sys screenshot_dir = "src/screenshots" meta_path = os.path.join(screenshot_dir, 'meta.json') latest_path = os.path.join(screenshot_dir, 'latest.jpg') # Read current meta with open(meta_path, 'r', encoding='utf-8') as f: existing_meta = json.load(f) print("[CHECK] Meta status: type={}, send_immediately={}".format( existing_meta.get('type', 'unknown'), existing_meta.get('send_immediately', False) )) # Simulate _write_screenshot_meta() protection for metadata should_update_meta = True if existing_meta.get('send_immediately'): should_update_meta = False print("[PROTECTED] Would skip meta.json update - pending {} still marked send_immediately=True".format( existing_meta.get('type'))) # Simulate latest.jpg update protection should_update_latest = True if existing_meta.get('send_immediately'): should_update_latest = False print("[PROTECTED] Would skip latest.jpg update - pending {} not yet transmitted".format( existing_meta.get('type'))) if not should_update_meta and not should_update_latest: print("[SUCCESS] Both meta.json and latest.jpg protected from periodic overwrite!") sys.exit(0) else: print("[FAILED] Would have overwritten protected files!") sys.exit(1) PYEOF echo "" echo "Step 3: Verify both files still contain event_start metadata..." if grep -q '"type": "event_start"' "$META_FILE" && grep -q '"send_immediately": true' "$META_FILE"; then if grep -q "MOCK EVENT_START IMAGE" "$LATEST_FILE"; then echo "[SUCCESS] Both files preserved correctly!" echo " - meta.json: Still contains type=event_start with send_immediately=true" echo " - latest.jpg: Still contains original event_start image" echo "" echo "Test passed! The fix prevents periodic screenshots from overwriting" echo "both the metadata AND the actual screenshot file when event-triggered" echo "captures are pending transmission." else echo "[FAILED] latest.jpg was overwritten!" exit 1 fi else echo "[FAILED] meta.json was overwritten!" exit 1 fi echo "" echo "=== Test Complete ==="