postscriptum 0.1.1

Multi-interpreted-language scripting engine for Rust with Rhai and Python backends
Documentation
//! G2 tripwire test: pins the bare-engine sandbox surface.
//!
//! THE test for the sandbox — extend it whenever packages change, keep
//! it exhaustive, not representative. If this goes red after a rhai
//! bump, the package contents drifted: re-run the probes, do NOT just
//! widen the assertions.
//!
//! Conformance mirror: quartertone
//! `nondeterministic_surface_absent_or_neutered` +
//! `reserved_words_need_bracket_access`.

#![cfg(feature = "rhai")]

use postscriptum::BoundedRhaiEngine;

fn engine() -> BoundedRhaiEngine {
    BoundedRhaiEngine::with_defaults()
}

/// Bare `new_raw` intentionally lacks even `.len()`; the curated set
/// must restore it on strings AND arrays.
#[test]
fn useful_collection_surface_present() {
    let mut e = engine();
    assert_eq!(e.eval(r#"[1, 2, 3].len()"#).unwrap(), "3");
    assert_eq!(e.eval(r#""hello".len()"#).unwrap(), "5");
    assert_eq!(e.eval("#{a: 1, b: 2}.len()").unwrap(), "2");
}

/// `type_of` is native dispatch (no package needed) — pin it anyway:
// fns keyed on type names must keep working for view authors.
#[test]
fn type_of_works() {
    let mut e = engine();
    assert_eq!(e.eval("type_of(42)").unwrap(), "i64");
    assert_eq!(e.eval(r#"type_of("x")"#).unwrap(), "string");
}

/// String ops, arithmetic, logic, iteration, closures — the day-to-day
/// view-expression vocabulary.
#[test]
fn useful_expression_vocabulary_works() {
    let mut e = engine();
    assert_eq!(e.eval(r#""ab" + "cd""#).unwrap(), "abcd");
    assert_eq!(e.eval(r#""AbC".to_lower()"#).unwrap(), "abc");
    assert_eq!(e.eval("40 + 2").unwrap(), "42");
    assert_eq!(e.eval("true && !false").unwrap(), "true");
    assert_eq!(
        e.eval("let t = 0; for i in 0..5 { t += i; } t").unwrap(),
        "10"
    );
    assert_eq!(e.eval("let f = |x| x * 2; call(f, 21)").unwrap(), "42");
    assert_eq!(e.eval("[1, 2, 3].map(|x| x * 2)").unwrap(), "[2, 4, 6]");
}

/// Reserved words work in bracket position (quartertone authors write
/// `table["type"]`, never `table.type`).
#[test]
fn reserved_words_need_bracket_access() {
    let mut e = engine();
    assert_eq!(e.eval(r#"#{type: 42}["type"]"#).unwrap(), "42");
}

/// Neutering must only swallow the stdout side effect: `to_string` /
/// `to_debug` (same package) keep working.
#[test]
fn print_and_debug_are_silent_no_ops() {
    let mut e = engine();
    assert!(e.eval("print(1)").is_ok());
    assert!(e.eval("debug(1)").is_ok());
    assert_eq!(e.eval("42.to_string()").unwrap(), "42");
}

/// Determinism: nothing that reads entropy or the clock.
#[test]
fn nondeterministic_surface_absent() {
    let mut e = engine();
    for snippet in ["rand()", "now()"] {
        assert!(
            e.eval(snippet).is_err(),
            "{snippet} must be unregistered, but it ran"
        );
    }
}

/// `sleep`/`exit` came with `LanguageCore` — excluded. `sleep(0)`, not
/// `sleep(999)`: if the package set ever regresses, the test fails in
/// milliseconds instead of hanging the suite.
#[test]
fn blocking_and_process_control_absent() {
    let mut e = engine();
    for snippet in ["sleep(0)", "exit(1)"] {
        assert!(
            e.eval(snippet).is_err(),
            "{snippet} must be unregistered, but it ran"
        );
    }
}

/// Dynamic invocation through `BasicFn::call` cannot reach excluded
/// functions either.
#[test]
fn dynamic_call_cannot_reach_excluded_functions() {
    let mut e = engine();
    for snippet in [r#"call("sleep", 0)"#, r#"call("eval", "1 + 1")"#] {
        assert!(e.eval(snippet).is_err(), "{snippet} must fail, but it ran");
    }
}

/// `eval()` is core dispatch and stays callable at runtime (G7 refuses
/// it at validation time; G1 fuel caps are the runtime backstop).
/// This test documents the fact — do NOT "fix" it by breaking eval.
#[test]
fn runtime_eval_stays_callable() {
    let mut e = engine();
    assert_eq!(e.eval(r#"eval("40 + 2")"#).unwrap(), "42");
}

/// `hash(...)` is deterministic sampling instead of `rand()`:
/// stable across evals AND across fresh engine instances, pinned
/// against hardcoded FNV-1a 64 vectors (reference-computed, see the
/// devlog — not just self-consistency).
#[test]
fn hash_is_stable_and_pinned() {
    let mut e = engine();
    // Empty string hashes to the FNV offset basis (wraps to negative
    // as i64 — the wrap is part of the pin).
    assert_eq!(e.eval("hash(\"\")").unwrap(), "-3750763034362895579");
    assert_eq!(e.eval("hash(\"x\")").unwrap(), "-5808529385363204345");
    assert_eq!(e.eval("hash(\"hello\")").unwrap(), "-6615550055289275125");
    // Stability: same input, same output — later eval, fresh engine.
    let h1 = e.eval("hash(\"row-42\")").unwrap();
    let h2 = e.eval("hash(\"row-42\")").unwrap();
    let h3 = engine().eval("hash(\"row-42\")").unwrap();
    assert_eq!(h1, h2);
    assert_eq!(h1, h3);
    // Distinct inputs (almost certainly) differ; non-strings hash via
    // their string form instead of "function not found".
    assert_ne!(
        e.eval("hash(\"a\")").unwrap(),
        e.eval("hash(\"b\")").unwrap()
    );
    assert_eq!(e.eval("hash(42)").unwrap(), e.eval("hash(\"42\")").unwrap());
}

/// No file resolver is registered, so `import` fails closed.
#[test]
fn import_has_no_resolver() {
    let mut e = engine();
    assert!(e.eval(r#"import "foo" as bar;"#).is_err());
}