1
//! A raise detail decodes as contents, so a cyclic or too deep value carries
2
//! the depth placeholder instead of losing the whole detail.
3

            
4
use scripting::HookError;
5
use scripting::executor::ScriptExecutor;
6
use scripting::format::{ContextType, EntityType};
7
use scripting::runtime::{EngineError, classify_runtime_error};
8
use scripting::serializer::MemorySerializer;
9

            
10
use nomiscript::{Compiler, Program, Reader, SymbolTable, Value, format_value};
11

            
12
3
fn raised_detail(src: &str) -> Option<String> {
13
3
    let program: Program = Reader::parse(src).expect("parse script source");
14
3
    let mut compiler = Compiler::with_host_fns(Vec::new());
15
3
    let mut symbols = SymbolTable::with_builtins_for_wasm();
16
3
    let wasm = compiler
17
3
        .compile(&program, &mut symbols)
18
3
        .expect("script compiles");
19
3
    let mut ser = MemorySerializer::new();
20
3
    ser.set_context(ContextType::BatchProcess, EntityType::Transaction);
21
3
    let input = ser.finalize(4096);
22
3
    let executor = ScriptExecutor::try_new().expect("baseline engine");
23
3
    let Err(HookError::WASM(err)) = executor.execute(&wasm, &input, Some(4096)) else {
24
        panic!("uncaught (error) must surface as a wasm error");
25
    };
26
3
    match classify_runtime_error(&err) {
27
3
        EngineError::ScriptRaised { detail, .. } => detail.map(|d| format_value(&Value::from(d))),
28
        other => panic!("expected ScriptRaised, got {other:?}"),
29
    }
30
3
}
31

            
32
3
fn assert_placeholder(detail: Option<String>) {
33
3
    let detail = detail.expect("the raise carries a detail");
34
3
    assert!(detail.contains("#<...>"), "{detail}");
35
3
    assert!(!detail.contains("undecodable"), "{detail}");
36
3
}
37

            
38
#[test]
39
1
fn a_cyclic_struct_detail_prints_the_placeholder() {
40
1
    assert_placeholder(raised_detail(
41
1
        r#"(defstruct node next) (defvar *n* (make-node)) (setf (node-next *n*) *n*)
42
1
           (error 'boom "c" *n*)"#,
43
1
    ));
44
1
}
45

            
46
#[test]
47
1
fn a_deep_struct_chain_detail_prints_the_placeholder() {
48
1
    assert_placeholder(raised_detail(
49
1
        r#"(defstruct node next)
50
1
           (defun chain (n acc) (if (= n 0) acc (chain (- n 1) (make-node :next acc))))
51
1
           (error 'boom "c" (chain 63 nil))"#,
52
1
    ));
53
1
}
54

            
55
#[test]
56
1
fn a_deep_list_detail_prints_the_placeholder() {
57
1
    assert_placeholder(raised_detail(
58
1
        r#"(let ((x (list 1 "a"))) (do ((i 0 (+ i 1))) ((= i 63)) (setf x (list x))) (error 'boom "c" x))"#,
59
1
    ));
60
1
}