Lines
99.2 %
Functions
100 %
Branches
//! Prelude helpers, constant-list rendering on the eval path and the value-position introspection forms.
use super::*;
use nomiscript::Reader;
#[test]
fn host_prelude_helper_is_loaded_and_compiles() {
// ADR-0029 host-dependent prelude: split:list-for-transaction is loaded
// on the Session path (after register_host_fns) and is callable. We
// compile a form referencing it — proving load + name resolution + the
// qualified native dispatch wire up — WITHOUT running it (it bottoms out
// in the DB-backed list-splits-by-transaction native; execution is
// covered by the db-gated integration test).
let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
assert!(
session.symbols.contains("SPLIT:LIST-FOR-TRANSACTION"),
"host prelude helper not registered"
);
// Compile (not run) a real call: lowering emits the qualified-name
// dispatch + the native import, proving resolution wires up. Host fns
// never execute at compile time, so no DB is touched.
let program = Reader::parse("(split:list-for-transaction (car (list-transactions \"\")))")
.expect("parse");
if let Err(e) = session
.compiler
.compile_eval_with_type(&program, &mut session.symbols)
{
panic!("host prelude helper failed to compile: {e:?}");
}
#[tokio::test]
async fn car_of_quoted_constant_list_compiles_on_eval_path() {
// Regression: CAR/CDR of a quoted CONSTANT list folds on the codegen
// path but used to trap on the eval-with-type (Session) path because
// the stack handler called compile_for_stack on the quoted arg, which
// has no Quote arm. The stack handlers now const-fold first.
let response = handle_form_smoke("(:id 1 :form (car '(1 2 3)))").await;
assert_eq!(response, "(:id 1 :value 1)");
async fn car_of_quoted_heterogeneous_list_compiles_on_eval_path() {
// Mixed number + string quoted list — the element is extracted by fold.
let response = handle_form_smoke("(:id 1 :form (car '(7 \"x\")))").await;
assert_eq!(response, "(:id 1 :value 7)");
async fn car_of_cdr_of_quoted_constant_compiles_on_eval_path() {
// CDR folds to the quoted tail, then CAR extracts its head — exercises
// the cdr fold-first path feeding car.
let response = handle_form_smoke("(:id 1 :form (car (cdr '(1 2 3))))").await;
assert_eq!(response, "(:id 1 :value 2)");
async fn cdr_of_quoted_constant_renders_tail_on_eval_path() {
// The bare-CDR case (no enclosing CAR): folds to a quoted tail and
// renders to its printed form rather than trapping.
assert_eq!(
handle_form_smoke("(:id 1 :form (cdr '(1 2 3)))").await,
"(:id 1 :value (2 3))"
handle_form_smoke("(:id 1 :form (cdr '(1)))").await,
"(:id 1 :value nil)"
async fn car_of_quoted_compound_and_symbol_heads_render_as_data() {
// A compound or symbol head is quoted DATA, not code — a compound renders
// to its printed form, a symbol is a symbol (not resolved as a call /
// variable).
handle_form_smoke("(:id 1 :form (car '((1 2) 3)))").await,
"(:id 1 :value (1 2))"
handle_form_smoke("(:id 1 :form (car '(x y)))").await,
"(:id 1 :value X)"
async fn reverse_of_constant_list_renders_on_eval_path() {
// Regression (same class as CAR/CDR): REVERSE of a constant or
// runtime-builder list folded but the stack handler rejected the
// non-runtime-pair result on the eval-with-type path. Now it renders
// the reversed datum on both surfaces.
handle_form_smoke("(:id 1 :form (reverse '(1 2 3)))").await,
"(:id 1 :value (3 2 1))"
handle_form_smoke("(:id 1 :form (reverse (list 1 2 3)))").await,
// Composition still folds through to the element.
handle_form_smoke("(:id 1 :form (car (reverse '(1 2 3))))").await,
"(:id 1 :value 3)"
async fn cons_onto_constant_list_renders_on_eval_path() {
// Regression: CONS with a constant / runtime-builder list cdr trapped
// in push_pair_cdr on the eval-with-type path. A fully-constant cons
// folds to a list datum, which is the same `$pair` chain as a runtime
// one; a dotted pair has no cell representation (plan 0014 Stage 1).
handle_form_smoke("(:id 1 :form (cons 0 '(1 2 3)))").await,
"(:id 1 :value (0 1 2 3))"
handle_form_smoke("(:id 1 :form (cons 0 (list 1 2 3)))").await,
let response = handle_form_smoke("(:id 1 :form (cons 1 2))").await;
response.contains("'(1 . 2) has no list cell representation"),
"{response}"
async fn append_of_constant_lists_renders_on_eval_path() {
// Regression: all-constant APPEND folds to a quoted list, the same
// `$pair` chain as a runtime one, symbol members included.
handle_form_smoke("(:id 1 :form (append '(1 2) '(3)))").await,
"(:id 1 :value (1 2 3))"
handle_form_smoke("(:id 1 :form (append '(a b) '(c)))").await,
"(:id 1 :value (A B C))"
async fn universal_prelude_helper_runs_end_to_end() {
// The universal prelude is loaded on the Session path too; a math:*
// helper executes through wasm and returns its value. No DB.
let response = handle_form_smoke("(:id 9 :form (math:square 9))").await;
assert_eq!(response, "(:id 9 :value 81)");
async fn pp_form_at_value_position_returns_string() {
// Exercises compile_pp_for_stack — the path nms / emacs see
// when `(pp 42)` is the request form.
let resp = handle_form_smoke("(:id 7 :form (pp 42))").await;
assert!(resp.contains(":id 7"), "{resp}");
assert!(resp.contains("\"42\""), "{resp}");
async fn describe_form_at_value_position_returns_doc() {
// compile_describe_for_stack — same shape.
let resp = handle_form_smoke("(:id 8 :form (describe '+))").await;
assert!(resp.contains(":id 8"), "{resp}");
assert!(!resp.contains(":error"), "{resp}");
async fn apropos_form_at_value_position_returns_list() {
let resp = handle_form_smoke("(:id 9 :form (apropos \"entity\"))").await;
assert!(resp.contains(":id 9"), "{resp}");
async fn deftest_form_at_value_position_returns_quoted_name() {
let resp = handle_form_smoke("(:id 10 :form (deftest sanity (assert-equal 1 1)))").await;
assert!(resp.contains(":id 10"), "{resp}");
async fn assert_equal_pass_form_at_value_position() {
let resp = handle_form_smoke("(:id 11 :form (assert-equal 2 2))").await;
assert!(resp.contains(":id 11"), "{resp}");
async fn assert_equal_fail_surfaces_as_error() {
// assert_equal returns Err on mismatch; Session maps to
// :code compile error envelope (it's a NomiError::Compile).
let resp = handle_form_smoke("(:id 12 :form (assert-equal 1 2))").await;
assert!(resp.contains(":id 12"), "{resp}");
assert!(resp.contains(":error"), "{resp}");
async fn coverage_dump_lists_called_natives() {
let _ = session
.handle_form("(:id 1 :form (rpc-protocol-version))")
.await;
let dump = session.handle_form("(:id 2 :form (coverage-dump))").await;
assert!(dump.contains("RPC-PROTOCOL-VERSION"), "{dump}");
assert!(dump.contains(":id 2"), "{dump}");
async fn coverage_dump_is_empty_of_natives_before_any_call() {
let dump = session.handle_form("(:id 1 :form (coverage-dump))").await;
assert!(dump.contains(":id 1"), "{dump}");
assert!(!dump.contains("RPC-PROTOCOL-VERSION"), "{dump}");