geolith-cli 0.2.1

Rust vector tile generator — OSM + Overture Maps → protomaps PMTiles
Documentation
#![allow(clippy::unwrap_used)] // tests and benches may unwrap (AGENTS.md).

//! End-to-end tests that spawn the real `geolith` binary.
//!
//! Everything else drives the library entry points directly, which cannot
//! reach [`geolith_cli::cli::parse_args`] or `run` because those read
//! `std::env::args_os`. Running the compiled binary covers argument parsing,
//! clap's diagnostics and the process exit codes an operator actually sees.

use std::io::Write;
use std::process::Command;

/// Path to the binary under test, provided by cargo for integration tests.
const GEOLITH: &str = env!("CARGO_BIN_EXE_geolith");

fn write_dem() -> tempfile::NamedTempFile {
    use tiff::encoder::{TiffEncoder, colortype};
    use tiff::tags::Tag;

    let mut buffer = std::io::Cursor::new(Vec::new());
    {
        let mut encoder = TiffEncoder::new(&mut buffer).expect("tiff encoder");
        let mut image = encoder
            .new_image::<colortype::Gray32Float>(4, 4)
            .expect("image encoder");
        let directory = image.encoder();
        directory
            .write_tag(Tag::Unknown(33550), &[10.0_f64, 10.0, 0.0][..])
            .expect("pixel scale");
        directory
            .write_tag(
                Tag::Unknown(33922),
                &[0.0_f64, 0.0, 0.0, 400_000.0, 5_100_000.0, 0.0][..],
            )
            .expect("tiepoint");
        directory
            .write_tag(
                Tag::Unknown(34735),
                &[1_u32, 1, 0, 1, 3072, 0, 1, 32_632][..],
            )
            .expect("geokeys");
        let values: Vec<f32> = (0..16).map(|i| 1000.0 + i as f32).collect();
        image.write_data(&values).expect("write samples");
    }
    use std::io::Seek as _;
    buffer.rewind().expect("rewind");

    let mut file = tempfile::Builder::new()
        .suffix(".tif")
        .tempfile()
        .expect("temp file");
    file.write_all(&buffer.into_inner()).expect("write tiff");
    file.flush().expect("flush");
    file
}

#[test]
fn binary_renders_terrain_from_a_geotiff() {
    let dem = write_dem();
    let dir = tempfile::tempdir().expect("tempdir");
    let output = dir.path().join("terrain.pmtiles");

    let status = Command::new(GEOLITH)
        .args([
            "terrain",
            "--dem",
            dem.path().to_str().expect("utf-8 path"),
            "--output",
            output.to_str().expect("utf-8 path"),
            "--min-zoom",
            "12",
            "--max-zoom",
            "13",
            "--tile-size",
            "256",
        ])
        .output()
        .expect("binary runs");

    assert!(
        status.status.success(),
        "terrain run failed: {}",
        String::from_utf8_lossy(&status.stderr)
    );
    let bytes = std::fs::read(&output).expect("archive written");
    assert_eq!(&bytes[..7], b"PMTiles");
}

#[test]
fn binary_reports_a_missing_dem() {
    let dir = tempfile::tempdir().expect("tempdir");
    let output = dir.path().join("out.pmtiles");

    let result = Command::new(GEOLITH)
        .args([
            "terrain",
            "--dem",
            "/nonexistent/dem.tif",
            "--output",
            output.to_str().expect("utf-8 path"),
        ])
        .output()
        .expect("binary runs");

    assert!(
        !result.status.success(),
        "a missing DEM must exit non-zero so pipelines fail loudly"
    );
}

#[test]
fn binary_rejects_an_unknown_flag() {
    let result = Command::new(GEOLITH)
        .args(["terrain", "--not-a-flag"])
        .output()
        .expect("binary runs");

    assert!(!result.status.success());
    assert!(
        String::from_utf8_lossy(&result.stderr).contains("--not-a-flag"),
        "clap names the offending flag"
    );
}

#[test]
fn binary_renders_subcommand_help() {
    for verb in ["terrain", "splat-tiles", "raster", "reconstruct"] {
        let result = Command::new(GEOLITH)
            .args([verb, "--help"])
            .output()
            .expect("binary runs");

        assert!(result.status.success(), "{verb} --help exits zero");
        let stdout = String::from_utf8_lossy(&result.stdout);
        assert!(
            stdout.contains(&format!("geolith {verb}")),
            "{verb} --help renders its own usage: {stdout}"
        );
    }
}

#[test]
fn binary_renders_dataset_help() {
    let result = Command::new(GEOLITH)
        .args(["dataset", "--help"])
        .output()
        .expect("binary runs");

    assert!(result.status.success(), "dataset --help exits zero");
    let stdout = String::from_utf8_lossy(&result.stdout);
    for action in ["areas", "normals", "build"] {
        assert!(
            stdout.contains(action),
            "dataset --help lists {action}: {stdout}"
        );
    }
}

#[test]
fn binary_requires_arguments_for_the_tile_pipeline() {
    let result = Command::new(GEOLITH).output().expect("binary runs");
    assert!(
        !result.status.success(),
        "a bare invocation cannot run the tile pipeline"
    );
}

#[test]
fn reconstruct_subcommand_parses_and_requires_video() {
    let result = Command::new(GEOLITH)
        .args(["reconstruct", "--output", "/tmp/recon-out"])
        .output()
        .expect("binary runs");

    assert!(!result.status.success(), "missing --video must fail");
    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        stderr.contains("--video"),
        "clap names the missing flag: {stderr}"
    );
}

#[test]
fn reconstruct_subcommand_accepts_full_flag_set() {
    let result = Command::new(GEOLITH)
        .args([
            "reconstruct",
            "--video",
            "/tmp/does-not-exist.mp4",
            "--output",
            "/tmp/recon-out",
            "--fps",
            "3",
            "--max-frames",
            "120",
            "--train-iters",
            "15000",
            "--longitude",
            "25.96",
            "--latitude",
            "58.73",
            "--crop-percentile",
            "0.1",
        ])
        .output()
        .expect("binary runs");

    // Parsing succeeds; the failure must be the missing video file, not
    // a clap error — proving every flag parsed cleanly.
    assert!(!result.status.success(), "missing video file must fail");
    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        !stderr.contains("error: unexpected"),
        "no clap parse error: {stderr}"
    );
    assert!(
        stderr.contains("video not found"),
        "the error names the missing video: {stderr}"
    );
}