Lines
100 %
Functions
Branches
//! Request and file handling, value envelopes and the error payloads a form produces.
use super::*;
use crate::envelope::ErrorCode;
use nomiscript::Number;
#[tokio::test]
async fn evaluates_arithmetic_and_returns_value_envelope() {
let response = handle_form_smoke("(:id 1 :form (+ 1 2))").await;
assert_eq!(response, "(:id 1 :value 3)");
}
async fn evaluates_nested_arithmetic() {
let response = handle_form_smoke("(:id 5 :form (* (+ 1 2) (- 10 4)))").await;
assert_eq!(response, "(:id 5 :value 18)");
async fn print_in_eval_mode_does_not_panic() {
// Regression: PRINT / DISPLAY / NEWLINE / DEBUG lower to `env.log`,
// which was script-mode-only. In eval mode the missing func index used
// to SIGABRT the compiler (`registry.rs` `HashMap[key]`). The eval-mode
// `log` import + the rpc `env.log` host fn now make it compile + run.
let response = handle_form_smoke("(:id 1 :form (print \"hi\"))").await;
assert!(response.contains(":id 1"), "got: {response}");
assert!(!response.contains(":code"), "must not error: {response}");
async fn dolist_with_print_in_eval_mode_runs() {
// The exact shape from the Metro script that first surfaced the panic.
let response = handle_form_smoke("(:id 2 :form (dolist (x (list 1 2 3)) (print x)))").await;
assert!(response.contains(":id 2"), "got: {response}");
async fn handle_request_captures_output_and_value() {
// `(print "hi")` writes "hi" to the per-request buffer (via env.log) and
// its own return value is nil; the structured outcome must carry the
// captured text in `output` AND the value payload — the SLYNK mREPL
// renders these as `:write-string` + `:write-values` respectively.
let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
let outcome = session.handle_request("(print \"hi\")").await;
assert!(
outcome.output.contains("hi"),
"captured output should contain the printed text, got: {:?}",
outcome.output
);
matches!(outcome.payload, ResponsePayload::Value(_)),
"payload should be a Value, got: {:?}",
outcome.payload
async fn handle_request_value_only_has_empty_output() {
let outcome = session.handle_request("(+ 1 2)").await;
assert!(outcome.output.is_empty(), "got: {:?}", outcome.output);
assert_eq!(
outcome.payload,
ResponsePayload::Value(Value::Number(Number::Index(3)))
async fn handle_request_rejects_plist_shaped_injection() {
// Adversarial review: the source must be parsed as a standalone AST, not
// interpolated into a `(:id 0 :form …)` envelope string — otherwise
// `1 :form (+ 2 3)` would re-read as a plist and eval to `1`. It must
// instead error (more than one top-level form), never silently return 1.
let outcome = session.handle_request("1 :form (+ 2 3)").await;
matches!(outcome.payload, ResponsePayload::Error { .. }),
"plist-shaped input must error, got: {:?}",
async fn handle_request_interrupt_latch_aborts_before_eval() {
// An interrupt latched before the request (the SLYNK reader arms it on
// `(:emacs-interrupt)`) must abort the eval at the pre-start check, even
// for a form that would otherwise compile fine.
session.interrupt_handle().interrupt();
match outcome.payload {
ResponsePayload::Error { code, .. } => {
assert_eq!(code.as_symbol(), ErrorCode::INTERRUPTED);
other => panic!("expected interrupted error, got: {other:?}"),
async fn handle_file_evaluates_all_forms_and_persists_state() {
// A file with a defun + a call to it: state accumulates across forms,
// and the load returns a summary value (not the last form's value).
let dir = std::env::temp_dir();
let path = dir.join(format!("nms_load_test_{}.nms", std::process::id()));
std::fs::write(&path, "(defun dbl (x) (* x 2))\n(dbl 21)\n").unwrap();
let outcome = session.handle_file(path.to_str().unwrap()).await;
std::fs::remove_file(&path).ok();
ResponsePayload::Value(Value::String(s)) => {
assert!(s.contains("loaded"), "summary: {s}");
assert!(s.contains("2 forms"), "summary: {s}");
other => panic!("expected a load summary string, got: {other:?}"),
async fn handle_file_missing_path_errors() {
let outcome = session.handle_file("/no/such/nms/file.nms").await;
"got: {:?}",
async fn handle_file_aborts_on_a_bad_form() {
let path = dir.join(format!("nms_load_bad_{}.nms", std::process::id()));
std::fs::write(&path, "(+ 1 2)\n(undefined-symbol-here)\n").unwrap();
"a bad form must abort the load, got: {:?}",
async fn handle_file_honours_interrupt_armed_before_load() {
// A `C-g` that lands before/during the synchronous read+parse must abort
// the load (the pre-read latch check), not be ignored until the first
// form reaches the Wasm call.
let path = dir.join(format!("nms_load_intr_{}.nms", std::process::id()));
std::fs::write(&path, "(+ 1 2)\n(+ 3 4)\n").unwrap();
assert_eq!(code.as_symbol(), ErrorCode::INTERRUPTED, "got: {code:?}");
other => panic!("interrupt should abort the load, got: {other:?}"),
async fn handle_request_surfaces_error_payload() {
let outcome = session.handle_request("does-not-exist").await;
"payload should be an Error, got: {:?}",
async fn handle_request_clears_output_between_calls() {
// The buffer must not leak across requests: a print then a pure value.
let _ = session.handle_request("(print \"first\")").await;
let second = session.handle_request("(+ 1 1)").await;
second.output.is_empty(),
"output leaked from prior request: {:?}",
second.output
async fn returns_value_for_literal_form() {
let response = handle_form_smoke("(:id 9 :form 42)").await;
assert_eq!(response, "(:id 9 :value 42)");
async fn returns_value_for_string_literal() {
let response = handle_form_smoke("(:id 9 :form \"hello\")").await;
assert_eq!(response, "(:id 9 :value \"hello\")");
#[test]
fn round_trips_bytes_through_eval() {
let value = parse_to_value("'#u8(1 2 3)").unwrap();
assert_eq!(value, Value::Bytes(vec![1, 2, 3]));
async fn bad_envelope_emits_envelope_error() {
let response = handle_form_smoke("(:form (+ 1 2))").await;
assert!(response.contains(":code args"));
assert!(response.contains(":id 0"));
async fn malformed_envelope_emits_parse_error() {
let response = handle_form_smoke("(((((").await;
assert!(response.contains(":code parse"));
async fn undefined_symbol_emits_compile_error() {
let response = handle_form_smoke("(:id 7 :form does-not-exist)").await;
assert!(response.contains(":id 7"));
assert!(response.contains(":code compile"));
async fn user_function_arity_violation_emits_args_error() {
let _ = session
.handle_form("(:id 1 :form (defun id-fn (x) x))")
.await;
let response = session.handle_form("(:id 2 :form (id-fn))").await;
assert!(response.contains(":id 2"));