"""Verify the example and exercise solution in a disposable copy."""
import os
import shutil
import subprocess
import tempfile
from pathlib import Path

ROOT = Path(__file__).resolve().parent
FILES = ['package.json', 'tsconfig.json', 'index.html', 'src/main.tsx', 'src/App.tsx', 'src/App.test.tsx', 'solution/App.test.tsx', 'package-lock.json']
COMMANDS = [['npm', 'ci', '--no-audit', '--no-fund'], ['npm', 'run', 'build'], ['npm', 'test'], ['python3', '-c', "from pathlib import Path\nPath('src/App.test.tsx').write_text(Path('solution/App.test.tsx').read_text())"], ['npm', 'test'], ['npm', 'run', 'build']]

with tempfile.TemporaryDirectory(prefix="appfabrik-learning-") as temporary:
    work = Path(temporary)
    for name in FILES:
        target = work / name
        target.parent.mkdir(parents=True, exist_ok=True)
        shutil.copyfile(ROOT / name, target)
    for command in COMMANDS:
        print("Running:", " ".join(command), flush=True)
        subprocess.run(command, cwd=work, check=True, timeout=900)
print("Example and solution checks completed.")
