wasm-rquickjs-cli 0.4.14

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
use crate::common::{
    CompiledTest, NodeModulesAppEntry, NodeModulesAppTestEntry, TestInstance, copy_dir_recursive,
    load_node_modules_apps_config,
};
use camino::{Utf8Path, Utf8PathBuf};
use camino_tempfile::Utf8TempDir;
use std::env;
use std::fs;
use std::process::Command;
use std::sync::{Arc, Mutex, OnceLock};
use test_r::core::{DynamicTestRegistration, TestProperties};
use test_r::{test_dep, test_gen};
use wasmtime::component::Val;

const CONFIG_PATH: &str = "tests/node_modules_apps/config.jsonc";
const NODE_MODULES_APP_BASELINE_MAJOR: u32 = 22;
const NODE_MODULES_APP_BASELINE_MINOR: u32 = 14;
const NODE_MODULES_APP_BASELINE_PATCH: u32 = 0;
const NODE_MODULES_APP_STRICT_BASELINE_ENV: &str = "NODE_MODULES_APP_STRICT_NODE_BASELINE";
const FLAKY_MAX_ATTEMPTS: usize = 3;

#[test_dep(tagged_as = "node_modules_app_runner", scope = Cloneable)]
async fn compiled_node_modules_app_runner() -> CompiledTest {
    let path = Utf8Path::new("examples/runtime/node-modules-app-runner");
    CompiledTest::new_with_features(path, true, crate::common::FeatureCombination::FullNoLogging)
        .await
        .expect("Failed to compile node-modules-app-runner")
}

struct PreparedNodeModulesApp {
    _temp_dir: Utf8TempDir,
    app_dir: Utf8PathBuf,
}

struct InstalledNodeModulesAppTemplate {
    _temp_dir: Utf8TempDir,
    app_dir: Utf8PathBuf,
}

fn install_node_modules_app(app_name: &str, app_dir: &Utf8Path) -> anyhow::Result<()> {
    let status = Command::new("npm")
        .arg("ci")
        .arg("--install-links")
        .arg("--ignore-scripts")
        .arg("--no-audit")
        .arg("--no-fund")
        .current_dir(app_dir)
        .status()?;
    anyhow::ensure!(status.success(), "npm ci failed for {app_name}");
    Ok(())
}

fn prepare_uncached_node_modules_app(app_name: &str) -> anyhow::Result<PreparedNodeModulesApp> {
    let source_dir = Utf8Path::new("tests")
        .join("node_modules_apps")
        .join("apps")
        .join(app_name);
    let temp_dir = Utf8TempDir::new()?;
    let app_dir = temp_dir.path().join("app");

    copy_dir_recursive(source_dir.as_std_path(), app_dir.as_std_path())?;
    install_node_modules_app(app_name, &app_dir)?;

    Ok(PreparedNodeModulesApp {
        _temp_dir: temp_dir,
        app_dir,
    })
}

fn installed_node_modules_app_template(
    app_name: &str,
) -> anyhow::Result<Arc<InstalledNodeModulesAppTemplate>> {
    static INSTALLED_APPS: OnceLock<
        Mutex<std::collections::HashMap<String, Arc<InstalledNodeModulesAppTemplate>>>,
    > = OnceLock::new();
    static APP_INSTALL_LOCKS: OnceLock<Mutex<std::collections::HashMap<String, Arc<Mutex<()>>>>> =
        OnceLock::new();

    let installed_apps = INSTALLED_APPS
        .get_or_init(|| Mutex::new(std::collections::HashMap::new()))
        .lock()
        .unwrap();
    if let Some(app) = installed_apps.get(app_name) {
        return Ok(app.clone());
    }
    drop(installed_apps);

    let app_install_lock = {
        let mut locks = APP_INSTALL_LOCKS
            .get_or_init(|| Mutex::new(std::collections::HashMap::new()))
            .lock()
            .unwrap();
        locks
            .entry(app_name.to_string())
            .or_insert_with(|| Arc::new(Mutex::new(())))
            .clone()
    };
    let _install_guard = app_install_lock.lock().unwrap();

    let installed_apps = INSTALLED_APPS
        .get_or_init(|| Mutex::new(std::collections::HashMap::new()))
        .lock()
        .unwrap();
    if let Some(app) = installed_apps.get(app_name) {
        return Ok(app.clone());
    }
    drop(installed_apps);

    let prepared = prepare_uncached_node_modules_app(app_name)?;
    let template = Arc::new(InstalledNodeModulesAppTemplate {
        _temp_dir: prepared._temp_dir,
        app_dir: prepared.app_dir,
    });
    INSTALLED_APPS
        .get_or_init(|| Mutex::new(std::collections::HashMap::new()))
        .lock()
        .unwrap()
        .insert(app_name.to_string(), template.clone());
    Ok(template)
}

fn prepare_node_modules_app(app_name: &str) -> anyhow::Result<PreparedNodeModulesApp> {
    let template = installed_node_modules_app_template(app_name)?;
    let temp_dir = Utf8TempDir::new()?;
    let app_dir = temp_dir.path().join("app");
    copy_dir_recursive(template.app_dir.as_std_path(), app_dir.as_std_path())?;

    Ok(PreparedNodeModulesApp {
        _temp_dir: temp_dir,
        app_dir,
    })
}

fn strict_node_baseline_enabled() -> bool {
    env::var(NODE_MODULES_APP_STRICT_BASELINE_ENV)
        .map(|value| {
            matches!(
                value.to_ascii_lowercase().as_str(),
                "1" | "true" | "yes" | "on"
            )
        })
        .unwrap_or(false)
}

fn parse_node_version(version: &str) -> anyhow::Result<(u32, u32, u32)> {
    let mut parts = version.trim().split('.');
    let major = parts
        .next()
        .ok_or_else(|| anyhow::anyhow!("missing Node.js major version"))?
        .parse::<u32>()?;
    let minor = parts
        .next()
        .ok_or_else(|| anyhow::anyhow!("missing Node.js minor version"))?
        .parse::<u32>()?;
    let patch = parts
        .next()
        .ok_or_else(|| anyhow::anyhow!("missing Node.js patch version"))?
        .parse::<u32>()?;
    Ok((major, minor, patch))
}

fn ensure_node_supports_require_esm() -> anyhow::Result<String> {
    let output = Command::new("node")
        .arg("-p")
        .arg("process.versions.node")
        .output()?;
    anyhow::ensure!(
        output.status.success(),
        "failed to determine host Node.js version: {}",
        String::from_utf8_lossy(&output.stderr),
    );
    let version = String::from_utf8_lossy(&output.stdout).trim().to_string();
    let (major, minor, patch) = parse_node_version(&version)?;
    let baseline = format!(
        "{NODE_MODULES_APP_BASELINE_MAJOR}.{NODE_MODULES_APP_BASELINE_MINOR}.{NODE_MODULES_APP_BASELINE_PATCH}"
    );

    if strict_node_baseline_enabled() {
        anyhow::ensure!(
            (major, minor, patch)
                == (
                    NODE_MODULES_APP_BASELINE_MAJOR,
                    NODE_MODULES_APP_BASELINE_MINOR,
                    NODE_MODULES_APP_BASELINE_PATCH
                ),
            "node_modules app strict Node baseline requires Node.js {baseline}; found {version}",
        );
        return Ok(version);
    }

    anyhow::ensure!(
        major == NODE_MODULES_APP_BASELINE_MAJOR
            && (minor, patch)
                >= (
                    NODE_MODULES_APP_BASELINE_MINOR,
                    NODE_MODULES_APP_BASELINE_PATCH,
                ),
        "node_modules app tests require Node.js major {NODE_MODULES_APP_BASELINE_MAJOR} at or after {baseline}; found {version}",
    );

    if (minor, patch)
        != (
            NODE_MODULES_APP_BASELINE_MINOR,
            NODE_MODULES_APP_BASELINE_PATCH,
        )
    {
        eprintln!(
            "warning: node_modules app tests are baselined against Node.js {baseline}; local Node.js {version} may differ in Node-baselined fixture behavior"
        );
    }

    Ok(version)
}

fn verify_with_node(app: &PreparedNodeModulesApp, test_file: &str) -> anyhow::Result<()> {
    let node_version = ensure_node_supports_require_esm()?;
    println!("Verifying node_modules app baseline with Node.js {node_version}");
    let output = Command::new("node")
        .arg("run-node.mjs")
        .arg(test_file)
        .current_dir(&app.app_dir)
        .output()?;
    anyhow::ensure!(
        output.status.success(),
        "Node baseline failed for {}:\n[stdout]\n{}\n[stderr]\n{}",
        test_file,
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    Ok(())
}

async fn run_node_modules_app_test(
    compiled_test: &CompiledTest,
    app_name: &str,
    test_file: &str,
    timeout_secs: u64,
) -> anyhow::Result<()> {
    let baseline_app = prepare_node_modules_app(app_name)?;
    verify_with_node(&baseline_app, test_file)?;

    let mut instance = TestInstance::new(compiled_test.wasm_path()).await?;

    let guest_app = prepare_node_modules_app(app_name)?;
    let mounted_app_dir = instance.temp_dir_path().join("app");
    fs::create_dir_all(&mounted_app_dir)?;
    copy_dir_recursive(
        guest_app.app_dir.as_std_path(),
        mounted_app_dir.as_std_path(),
    )?;

    // Fixture preparation is host-side setup and can be expensive under concurrent
    // group9 load. The deadline should bound guest execution, not app copying.
    instance.set_epoch_deadline(timeout_secs);
    let guest_test_path = format!("/app/{test_file}");
    let (result, stdout, stderr) = instance
        .invoke_and_capture_output_with_stderr(None, "run-test", &[Val::String(guest_test_path)])
        .await;

    let result = result?;
    println!("Output:\n{}", stdout);
    if !stderr.trim().is_empty() {
        println!("Stderr:\n{}", stderr);
    }
    match result {
        Some(Val::String(s)) if s.starts_with("PASS:") => Ok(()),
        other => anyhow::bail!("Unexpected node_modules app result: {:?}", other),
    }
}

fn test_name(app: &NodeModulesAppEntry, test: &NodeModulesAppTestEntry) -> String {
    format!(
        "node_modules_app__{}__{}",
        sanitize_name(&app.name),
        sanitize_name(&test.file)
    )
}

fn sanitize_name(value: &str) -> String {
    value
        .chars()
        .map(|ch| if ch.is_ascii_alphanumeric() { ch } else { '_' })
        .collect()
}

#[test_gen]
fn gen_node_modules_app_tests(r: &mut DynamicTestRegistration) {
    let apps =
        load_node_modules_apps_config(CONFIG_PATH).expect("Failed to load node_modules app config");
    let dependency_name = "compiledtest_node_modules_app_runner".to_string();

    for app in apps {
        for test in app.tests.clone() {
            let app_name = app.name.clone();
            let test_file = test.file.clone();
            let timeout_secs = test.timeout_secs;
            let flaky = test.flaky;
            let generated_test_name = test_name(&app, &test);
            let props = TestProperties {
                is_ignored: test.category.should_ignore_in_runner(),
                ..TestProperties::unit_test()
            };

            r.add_async_test(
                generated_test_name,
                props,
                Some(vec![dependency_name.clone()]),
                move |deps| {
                    let compiled_test: Arc<CompiledTest> = deps
                        .get("compiledtest_node_modules_app_runner")
                        .expect("CompiledTest dependency not found")
                        .downcast::<CompiledTest>()
                        .expect("CompiledTest type mismatch");
                    let app_name = app_name.clone();
                    let test_file = test_file.clone();
                    Box::pin(async move {
                        let max_attempts = if flaky { FLAKY_MAX_ATTEMPTS } else { 1 };
                        let mut last_err = None;
                        for attempt in 1..=max_attempts {
                            match run_node_modules_app_test(
                                compiled_test.as_ref(),
                                &app_name,
                                &test_file,
                                timeout_secs,
                            )
                            .await
                            {
                                Ok(()) => return Ok(()),
                                Err(e) => {
                                    if flaky && attempt < max_attempts {
                                        println!(
                                            "Flaky node_modules app test attempt {attempt}/{max_attempts} failed, retrying: {e:?}"
                                        );
                                    }
                                    last_err = Some(e);
                                }
                            }
                        }
                        Err(last_err.expect("at least one node_modules app test attempt"))
                    })
                },
            );
        }
    }
}