import sys
import os
import time

folder_path = os.path.abspath(os.path.join("components", "system_tests"))
if folder_path not in sys.path:
    sys.path.append(folder_path)

from scan_serial import ESP32Runner

def test_wifi_initialize():
    runner = ESP32Runner(mode="SIM", port="COM9")
    runner.start()
    print("--- QEMU Runner Started ---", flush=True)
    try:
        start_time = time.time()
        while time.time() - start_time < 30:
            line = runner.get_line(timeout=1.0)
            if line:
                print(line, flush=True)
                if "WiFi initialized successfully" in line:
                    print("SUCCESS CRITERIA MET!", flush=True)
                    return 0
            if runner.process.poll() is not None:
                print(f"Process exited with code: {runner.process.returncode}", flush=True)
                return 1
    finally:
        runner.stop()
        print("Done.", flush=True)
    return 1

if __name__ == "__main__":
    exit_code = test_wifi_initialize()
    sys.exit(exit_code)
