litz-cli 0.13.3

Command-line runner for Litz firmware simulations, designed to work as a GitHub Actions test step.
Documentation
//! The guest as a Wi-Fi access point: a MicroPython image brings up `AP_IF`,
//! and the virtual station of `[sim] wifi_sta` follows its beacons,
//! authenticates and associates, then takes a lease from the guest's own DHCP
//! server. The guest reports both from its side.
//!
//! The images are downloads, so this test is ignored by default:
//!
//! ```sh
//! tools/fetch-micropython.sh
//! LITZ_MICROPYTHON=build/micropython \
//!   cargo test -p litz-cli --test softap -- --ignored
//! ```

use litz_cli::runner::{Mode, execute};
use std::path::{Path, PathBuf};

fn asset_dir() -> PathBuf {
    let p = PathBuf::from(
        std::env::var("LITZ_MICROPYTHON").unwrap_or_else(|_| panic!("LITZ_MICROPYTHON is not set")),
    );
    if p.is_absolute() {
        p
    } else {
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../..").join(p)
    }
}

/// A project whose guest starts a SoftAP and then polls its station list until
/// the virtual station has joined and been leased an address.
fn run_softap_project(name: &str, board: &str, firmware: &Path, rom: &Path, flash_size: &str) {
    let dir = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../../target/litz-cli-tests")
        .join(name);
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(
        dir.join("diagram.json"),
        format!(
            r#"{{
  "version": 1,
  "parts": [
    {{"id": "esp", "type": "{board}", "attrs": {{}}}}
  ],
  "connections": [
    ["esp:TX", "$serialMonitor:RX", "", []]
  ]
}}"#
        ),
    )
    .unwrap();
    std::fs::write(
        dir.join("main.py"),
        r#"import network
import time

ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid="litz-ap", password="litz-pass", authmode=network.AUTH_WPA2_PSK)
print("litz: softap", ap.ifconfig()[0])
for _ in range(300):
    stations = ap.status("stations")
    if stations:
        print("litz: station", stations[0][0].hex(), stations[0][1])
        break
    time.sleep_ms(100)
else:
    print("litz: no station")
"#,
    )
    .unwrap();
    std::fs::write(
        dir.join("litz.toml"),
        format!(
            r#"
[project]
diagram = "diagram.json"

[sim]
firmware = "{}"
rom = "{}"
runtime = "micropython"
flash_size = "{flash_size}"
script = "main.py"
wifi_sta = "ssid=litz-ap,psk=litz-pass"

[[test]]
name = "a station joins the guest's access point"
timeout = "40s"
expect = "litz: station 025354410001 192.168.4.2"
"#,
            firmware.display(),
            rom.display()
        ),
    )
    .unwrap();

    let outcome = execute(&dir, Mode::Test, false, true).unwrap();
    assert_eq!(outcome.exit_code, 0, "{}", outcome.output);
    assert!(
        outcome
            .output
            .contains("litz: station 025354410001 192.168.4.2"),
        "the virtual station did not join and take a lease: {}",
        outcome.output
    );
}

#[test]
#[ignore = "needs LITZ_MICROPYTHON=build/micropython (tools/fetch-micropython.sh)"]
fn classic_esp32_runs_a_softap() {
    let a = asset_dir();
    run_softap_project(
        "softap-classic",
        "board-esp32-devkit-c-v4",
        &a.join("ESP32_GENERIC-20260824-v1.29.0.bin"),
        &a.join("esp32_rev300_rom.elf"),
        "4MB",
    );
}

#[test]
#[ignore = "needs LITZ_MICROPYTHON=build/micropython (tools/fetch-micropython.sh)"]
fn c3_runs_a_softap() {
    let a = asset_dir();
    run_softap_project(
        "softap-c3",
        "board-esp32-c3-devkitm-1",
        &a.join("ESP32_GENERIC_C3-20260824-v1.29.0.bin"),
        &a.join("esp32c3_rev3_rom.elf"),
        "4MB",
    );
}

#[test]
#[ignore = "needs LITZ_MICROPYTHON=build/micropython (tools/fetch-micropython.sh)"]
fn s3_runs_a_softap() {
    let a = asset_dir();
    run_softap_project(
        "softap-s3",
        "board-esp32-s3-devkitc-1",
        &a.join("ESP32_GENERIC_S3-20260824-v1.29.0.bin"),
        &a.join("esp32s3_rev0_rom.elf"),
        "8MB",
    );
}

#[test]
#[ignore = "needs LITZ_MICROPYTHON=build/micropython (tools/fetch-micropython.sh)"]
fn c6_runs_a_softap() {
    let a = asset_dir();
    run_softap_project(
        "softap-c6",
        "board-esp32-c6-devkitc-1",
        &a.join("ESP32_GENERIC_C6-20260824-v1.29.0.bin"),
        &a.join("esp32c6_rev0_rom.elf"),
        "8MB",
    );
}