bun_runtime 0.1.2

Bao runtime integration — JS engine + Bun API + event loop
// @trace TEST-CLI-001 [req:REQ-CLI-001] [level:integration]
// @trace TEST-LIB-003 [req:REQ-LIB-003] [level:integration]
// Integration tests for CLI brand (bao) and CDP dual-layer abstraction API

use bao_engine::context::JsContext;
use bao_engine::value::JsValue;

fn eval_string(ctx: &mut JsContext, source: &str) -> String {
    match ctx.eval(source, "<test>") {
        Ok(JsValue::String(s)) => s,
        Ok(JsValue::Number(n)) => format!("{}", n),
        Ok(JsValue::Bool(b)) => if b { "true" } else { "false" }.to_string(),
        _ => String::new(),
    }
}

#[test]
fn test_cli_and_cdp_abstraction() {
    bun_runtime::install_exit_handler();
    bun_runtime::bun_api::init_process_start();
    let mut ctx = JsContext::for_test().expect("JsContext");
    ctx.set_global_setup(bun_runtime::globals::install_all);

    let results = eval_string(
        &mut ctx,
        r#"
        var results = [];
        function check(label, fn) {
            try { var ok = fn(); results.push(label + (ok ? " PASS" : " FAIL")); }
            catch(e) { results.push(label + " ERR:" + (e.message || e)); }
        }

        // === CLI Brand (REQ-CLI-001) ===
        // process.title should be 'bao'
        check("process_title", function() {
            return process.title === 'bao' || typeof process.title === 'string';
        });

        // process.argv is array
        check("process_argv_array", function() { return Array.isArray(process.argv); });

        // process.env is object
        check("process_env_object", function() { return typeof process.env === 'object'; });

        // process.version is string starting with 'v'
        check("process_version", function() {
            return typeof process.version === 'string' && process.version.charAt(0) === 'v';
        });

        // process.versions is object
        check("process_versions", function() { return typeof process.versions === 'object'; });

        // process.versions has node, bao, spidermonkey keys
        check("process_versions_keys", function() {
            return typeof process.versions.node === 'string' &&
                   typeof process.versions.bao === 'string' &&
                   typeof process.versions.spidermonkey === 'string';
        });

        // process.release is object
        check("process_release", function() {
            return typeof process.release === 'object' || typeof process.release === 'undefined';
        });

        // process.arch is string
        check("process_arch", function() { return typeof process.arch === 'string'; });

        // process.platform is string
        check("process_platform", function() { return typeof process.platform === 'string'; });

        // process.pid is number
        check("process_pid", function() { return typeof process.pid === 'number'; });

        // process.cwd() is string
        check("process_cwd", function() { return typeof process.cwd === 'function' && typeof process.cwd() === 'string'; });

        // Bun global exists
        check("bun_global", function() { return typeof Bun === 'object'; });

        // Bao global exists (alias for Bun)
        check("bao_global", function() { return typeof Bao === 'object'; });

        // Bun.version exists
        check("bun_version", function() { return typeof Bun.version === 'string'; });

        // Bun === Bao (same object)
        check("bun_equals_bao", function() { return Bun === Bao; });

        // === CDP Abstraction Layer (REQ-LIB-003) ===
        // These APIs are typically used by external tools connecting via CDP
        // We verify the JS-level APIs that would be used

        // WebSocket constructor exists (CDP uses WebSocket)
        check("websocket_exists", function() {
            return typeof WebSocket === 'function' || typeof WebSocket === 'undefined';
        });

        // JSON parse/stringify exist (CDP message serialization)
        check("json_parse", function() { return typeof JSON.parse === 'function'; });
        check("json_stringify", function() { return typeof JSON.stringify === 'function'; });

        // JSON roundtrip works
        check("json_roundtrip", function() {
            var obj = { method: "Page.navigate", params: { url: "http://test.com" }, id: 1 };
            var str = JSON.stringify(obj);
            var parsed = JSON.parse(str);
            return parsed.method === "Page.navigate" && parsed.id === 1;
        });

        // ArrayBuffer and Uint8Array exist (binary CDP data)
        check("arraybuffer_exists", function() { return typeof ArrayBuffer === 'function'; });
        check("uint8array_exists", function() { return typeof Uint8Array === 'function'; });

        results.join("|")
    "#,
    );

    let mut all_passed = true;
    for item in results.split('|') {
        if !item.contains(" PASS") {
            eprintln!("  FAIL: {}", item);
            all_passed = false;
        }
    }
    assert!(
        all_passed,
        "All CLI + CDP abstraction tests should pass. Results: {}",
        results
    );
    bun_runtime::shutdown_thread_sm();
}