mini-static 0.21.0

A secure, async static file server with streaming, traversal protection, and connection limits.
Documentation
use super::*;
use tempfile::TempDir;

#[tokio::test]
async fn bundle_mode_runs_the_tool_against_the_entry() {
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    let entry = src.path().join("main.js");
    let output = out.path().join("bundle.js");
    fs::write(&entry, "const x = 1;").unwrap();

    build_js_bundle(
        JsTool::TestEcho,
        &JsOptions::new().bundle_entry(&entry, "bundle.js"),
        &entry,
        &output,
    )
    .await
    .unwrap();

    assert_eq!(fs::read_to_string(&output).unwrap(), "const x = 1;");
}

#[tokio::test]
async fn bundle_false_minify_false_is_a_passthrough_copy() {
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    let entry = src.path().join("main.js");
    let output = out.path().join("main.js");
    fs::write(&entry, "const x = 1;").unwrap();

    build_js_bundle(JsTool::TestMissing, &JsOptions::new(), &entry, &output)
        .await
        .unwrap();

    assert_eq!(fs::read_to_string(&output).unwrap(), "const x = 1;");
}

#[tokio::test]
async fn a_failing_bundle_tool_leaves_previous_output_untouched() {
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    let entry = src.path().join("main.js");
    let output = out.path().join("bundle.js");
    fs::write(&entry, "const x = 1;").unwrap();
    fs::write(&output, "/* previous good build */").unwrap();

    let result = build_js_bundle(
        JsTool::TestMissing,
        &JsOptions::new()
            .bundle_entry(&entry, "bundle.js")
            .minify(true),
        &entry,
        &output,
    )
    .await;

    assert!(result.is_err());
    assert_eq!(
        fs::read_to_string(&output).unwrap(),
        "/* previous good build */"
    );
}

#[tokio::test]
async fn per_file_mode_with_minify_false_copies_through_unchanged() {
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    let source = src.path().join("app.js");
    let output = out.path().join("app.js");
    fs::write(&source, "const x = 1;").unwrap();

    build_js_file(JsTool::TestMissing, &JsOptions::new(), &source, &output)
        .await
        .unwrap();

    assert_eq!(fs::read_to_string(&output).unwrap(), "const x = 1;");
}

#[tokio::test]
async fn per_file_mode_already_minified_skips_the_tool() {
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    let source = src.path().join("app.min.js");
    let output = out.path().join("app.min.js");
    fs::write(&source, "const x=1;").unwrap();

    build_js_file(
        JsTool::TestMissing,
        &JsOptions::new().minify(true),
        &source,
        &output,
    )
    .await
    .unwrap();

    assert_eq!(fs::read_to_string(&output).unwrap(), "const x=1;");
}

#[tokio::test]
async fn per_file_mode_degrades_to_raw_copy_when_the_tool_fails() {
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    let source = src.path().join("app.js");
    let output = out.path().join("app.js");
    fs::write(&source, "const x = 1;").unwrap();

    build_js_file(
        JsTool::TestMissing,
        &JsOptions::new().minify(true),
        &source,
        &output,
    )
    .await
    .unwrap();

    assert_eq!(
        fs::read_to_string(&output).unwrap(),
        "const x = 1;",
        "a failing tool must degrade to serving the raw source, not fail the pipeline"
    );
}