//! Smoke test for `Image::ensure_baked`. Runs the same call
//! twice: first run bakes if needed (or reuses a compatible
//! existing snapshot), second run is the cache hit path.

use std::time::Instant;
use supermachine::Image;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let name = std::env::var("NAME").unwrap_or_else(|_| "rust_1_slim_ensure_smoke".into());
    let image_ref = std::env::var("IMAGE").unwrap_or_else(|_| "rust:1-slim".into());
    let memory: u32 = std::env::var("MEMORY")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(2048);

    println!("=== call 1 (cold or warm) ===");
    let t0 = Instant::now();
    let image = Image::ensure_baked(&name, &image_ref, |b| b.with_memory_mib(memory))?;
    println!(
        "  ensure_baked returned in {:?} — snapshot at {}",
        t0.elapsed(),
        image.snapshot_path().display()
    );

    println!("=== call 2 (hot path — should be near-instant) ===");
    let t0 = Instant::now();
    let _image2 = Image::ensure_baked(&name, &image_ref, |b| b.with_memory_mib(memory))?;
    println!("  ensure_baked returned in {:?}", t0.elapsed());

    println!("OK");
    Ok(())
}
