litz-cli 0.13.2

Command-line runner for Litz firmware simulations, designed to work as a GitHub Actions test step.
//! End-to-end deep sleep: a MicroPython image calls `machine.deepsleep`, the chip
//! powers its HP domain down, the wake source reboots it, and `boot.py` runs again
//! in the same run. `machine.reset_cause()` reports DEEPSLEEP_RESET (4) after the
//! wake on the classic ESP32, C3, S3 and C6 alike.
//!
//! 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 sleep_wake -- --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 that prints a marker from `boot.py`, sleeps for a second and never
/// reaches the line after `deepsleep`: seeing the marker twice means the wake ran
/// the boot script again.
fn run_sleep_project(
    name: &str,
    board: &str,
    firmware: &Path,
    rom: &Path,
    flash_size: &str,
    boot_py: &str,
    expect: &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": {{}}}},
    {{"id": "r1", "type": "litz-resistor", "attrs": {{"value": "220"}}}},
    {{"id": "led1", "type": "litz-led", "attrs": {{"color": "green"}}}}
  ],
  "connections": [
    ["esp:TX", "$serialMonitor:RX", "", []]
  ]
}}"#
        ),
    )
    .unwrap();
    std::fs::write(dir.join("boot.py"), boot_py).unwrap();
    std::fs::write(
        dir.join("main.py"),
        "import machine\nprint(\"litz: before sleep\")\nmachine.deepsleep(1000)\nprint(\"litz: after sleep\")\n",
    )
    .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"
scripts = ["boot.py = boot.py"]

[[test]]
name = "deepsleep wakes"
timeout = "8s"
expect = "{expect}"
"#,
            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(expect), "{}", outcome.output);
    assert!(
        !outcome.output.contains("litz: after sleep"),
        "the line after deepsleep must not run before the wake: {}",
        outcome.output
    );
}

const CAUSE_BOOT: &str = "import machine\nprint(\"litz: boot cause\", machine.reset_cause())\n";

#[test]
#[ignore = "needs LITZ_MICROPYTHON=build/micropython (tools/fetch-micropython.sh)"]
fn classic_esp32_wakes_from_deep_sleep() {
    let a = asset_dir();
    run_sleep_project(
        "sleep-wake-classic",
        "board-esp32-devkit-c-v4",
        &a.join("ESP32_GENERIC-20260824-v1.29.0.bin"),
        &a.join("esp32_rev300_rom.elf"),
        "4MB",
        CAUSE_BOOT,
        "litz: boot cause 4",
    );
}

#[test]
#[ignore = "needs LITZ_MICROPYTHON=build/micropython (tools/fetch-micropython.sh)"]
fn c3_wakes_from_deep_sleep() {
    let a = asset_dir();
    run_sleep_project(
        "sleep-wake-c3",
        "board-esp32-c3-devkitm-1",
        &a.join("ESP32_GENERIC_C3-20260824-v1.29.0.bin"),
        &a.join("esp32c3_rev3_rom.elf"),
        "4MB",
        CAUSE_BOOT,
        "litz: boot cause 4",
    );
}

#[test]
#[ignore = "needs LITZ_MICROPYTHON=build/micropython (tools/fetch-micropython.sh)"]
fn s3_wakes_from_deep_sleep() {
    let a = asset_dir();
    run_sleep_project(
        "sleep-wake-s3",
        "board-esp32-s3-devkitc-1",
        &a.join("ESP32_GENERIC_S3-20260824-v1.29.0.bin"),
        &a.join("esp32s3_rev0_rom.elf"),
        "8MB",
        CAUSE_BOOT,
        "litz: boot cause 4",
    );
}

#[test]
#[ignore = "needs LITZ_MICROPYTHON=build/micropython (tools/fetch-micropython.sh)"]
fn c6_wakes_from_deep_sleep() {
    let a = asset_dir();
    run_sleep_project(
        "sleep-wake-c6",
        "board-esp32-c6-devkitc-1",
        &a.join("ESP32_GENERIC_C6-20260824-v1.29.0.bin"),
        &a.join("esp32c6_rev0_rom.elf"),
        "8MB",
        CAUSE_BOOT,
        "litz: boot cause 4",
    );
}