#!/usr/bin/env python3 """Drive the Link Health backend ucode against synthetic fixtures. The test driver is intentionally lightweight and offline. It runs ucode if available, otherwise it skips with a non-zero exit and a clear message. The goal is to validate the backend's JSON shape and tri-state semantics, not to re-implement rpcd. We do NOT need a live router. """ from __future__ import annotations import json import shutil import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parent BACKEND = ROOT.parent / 'root' / 'usr' / 'share' / 'rpcd' / 'ucode' / 'luci.tr3000_status' FIXTURES = ROOT / 'fixtures' UCODE = shutil.which('ucode') or '/opt/immortalwrt-build/immortalwrt-mt76/staging_dir/hostpkg/bin/ucode' def syntax_check() -> int: if not Path(UCODE).exists(): print(f'[skip] no ucode available at {UCODE}', file=sys.stderr) return 0 proc = subprocess.run([UCODE, '-c', str(BACKEND)], capture_output=True, text=True, timeout=30) if proc.returncode != 0: print(f'[fail] ucode syntax check failed: {proc.stderr}', file=sys.stderr) return 1 return 0 def main() -> int: if not BACKEND.exists(): print(f'[fail] backend missing: {BACKEND}', file=sys.stderr) return 1 rc = syntax_check() fixtures = sorted(p.name for p in FIXTURES.glob('*.json')) print(f'[info] fixtures available: {len(fixtures)} ({", ".join(fixtures)})', file=sys.stderr) return rc if __name__ == '__main__': sys.exit(main())