#!/usr/bin/env python3
"""Exercise production snapshot/activity callbacks while the manual reader owns UI."""
from pathlib import Path
import re
import subprocess
import tempfile
repo=Path(__file__).resolve().parents[2]
source=(repo/'boards/m5stack_stopwatch/board.cpp').read_text()
names=('reply_snapshot_updated','board_set_pi_snapshot','board_append_pi_activity')
with tempfile.TemporaryDirectory(prefix='reader-snapshot-') as folder:
    temp=Path(folder)
    state=re.search(r'^struct PiState \{.*?^};',source,re.M|re.S).group()
    functions='\n'.join(re.search(r'^[^\n]*\b'+name+r'\([^;]*?\)\s*\{.*?^}',source,re.M|re.S).group() for name in names)
    (temp/'reader_state.inc').write_text(state)
    (temp/'reader_callbacks.inc').write_text(functions)
    subprocess.run(['c++','-std=c++17','-Wall','-Wextra','-Werror','-I',str(temp),
        '-I',str(repo/'boards/common/include'),'-I',str(repo/'firmware/tests/nvs_stub'),
        str(repo/'firmware/tests/reader_snapshot_test.cpp'),'-o',str(temp/'test')],check=True)
    subprocess.run([str(temp/'test')],check=True)
