Lines
100 %
Functions
Branches
//! Session state across forms and the meta / server natives called from source.
use super::*;
use nomiscript::{Fraction, Number};
#[tokio::test]
async fn interrupt_does_not_persist_across_forms() {
let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
session.interrupt_handle().interrupt();
let _ = session.handle_form("(:id 11 :form (+ 1 2))").await;
let response = session.handle_form("(:id 12 :form (+ 1 2))").await;
assert_eq!(response, "(:id 12 :value 3)");
}
async fn session_state_persists_across_forms() {
let defun = session
.handle_form("(:id 1 :form (defun double (x) (* 2 x)))")
.await;
assert!(defun.contains(":id 1"));
let call = session.handle_form("(:id 2 :form (double 21))").await;
assert_eq!(call, "(:id 2 :value 42)");
async fn fraction_results_format_canonically() {
// A fractional (Scalar) literal renders canonically as `n/d`. (Integer
// `(/ 1 4)` is now Index division → 0 per ADR-0028; the canonical
// fraction idiom is the `1/4` Scalar literal.)
let response = handle_form_smoke("(:id 3 :form 1/4)").await;
assert_eq!(response, "(:id 3 :value 1/4)");
#[test]
fn nomi_runtime_value_carries_through() {
let program = Reader::parse("(+ 0.5 0.25)").unwrap();
let value = nomiscript::eval_program(&mut SymbolTable::with_builtins(), &program).unwrap();
assert_eq!(value, Value::Number(Number::Scalar(Fraction::new(3, 4))));
async fn calls_meta_native_from_nomiscript_source() {
let response = handle_form_smoke("(:id 1 :form (rpc-protocol-version))").await;
let expected_version = crate::natives::meta::PROTOCOL_VERSION;
assert_eq!(response, format!("(:id 1 :value {expected_version})"));
async fn calls_server_get_version_from_nomiscript_source() {
let response = handle_form_smoke("(:id 1 :form (get-version))").await;
// GIT_HASH is baked at server crate build time via env!. We don't
// assert its exact value (changes per build) — just that the
// envelope round-trips a non-empty :value string.
assert!(
response.starts_with("(:id 1 :value \""),
"expected string response, got: {response}"
);
assert!(response.ends_with("\")"));
async fn calls_server_get_build_date_from_nomiscript_source() {
let response = handle_form_smoke("(:id 2 :form (get-build-date))").await;
response.starts_with("(:id 2 :value \""),
async fn cons_list_surfaces_as_printable_string() {
// First WasmGC sub-slice: eval-mode `(cons ...)` chains now
// capture through pending_string instead of erroring at compile
// time. The result is a textual `(1 2 3)` value the emacs client
// can (read) back into a real list. Heterogeneous car types ride
// a follow-up slice once Pair/Vector/Closure/Struct share a
// tagged union — today's cons cell stores i32 payloads only.
let response = handle_form_smoke("(:id 12 :form (cons 1 (cons 2 (cons 3 nil))))").await;
response.contains(":value (1 2 3)"),
"expected :value (1 2 3), got: {response}"
async fn count_native_cannot_mix_with_ratio_arithmetic() {
// account-count returns i32 (a count / Index, not a Scalar). Mixing it
// with a fractional Scalar literal must fail to compile — the design
// forbids accidental arithmetic across the Index/Scalar strata. (An
// integer literal like `10` is itself an Index now (ADR-0028), so
// `(+ 10 (account-count))` is valid Index arithmetic; the genuine
// stratum clash needs a fractional `1/2` Scalar operand.) The explicit
// `index->scalar` bridge is the only legal crossing.
let response = handle_form_smoke("(:id 11 :form (+ 1/2 (account-count)))").await;
assert!(response.contains(":code compile"), "got: {response}");
response.contains("scalar") && response.contains("index"),
"expected Index/Scalar stratum-separation error, got: {response}"
async fn get_commodity_with_non_uuid_arg_falls_back_to_symbol_lookup() {
// get-commodity now accepts a uuid OR a symbol (mirroring get-account's
// name fallback), so a non-uuid arg is NO LONGER an "invalid uuid"
// error — it's treated as a symbol and routed to a DB lookup. On this
// no-DB smoke harness that lookup surfaces a runtime DB-access error
// (not a parse error); a DB-backed test
// (`get_commodity_resolves_by_symbol` in tests-integration) covers the
// successful resolution.
let response = handle_form_smoke("(:id 9 :form (get-commodity \"USD\"))").await;
assert!(response.contains(":id 9"), "got: {response}");
response.contains(":code unavailable"),
"expected the unavailable condition (symbol path hits the DB), got: {response}"
!response.contains("invalid uuid"),
"a non-uuid arg must no longer short-circuit as an invalid-uuid error: {response}"
fn meta_native_unknown_in_script_mode_compile() {
// host_fns are only registered in eval-mode contexts; the compiler
// built without with_host_fns shouldn't see them. Sanity that the
// mode flag actually gates the registration.
use nomiscript::CompileMode;
let mut compiler = Compiler::new();
let mut symbols = SymbolTable::with_builtins();
let program = nomiscript::Reader::parse("(rpc-protocol-version)").unwrap();
let result = compiler.compile_with_mode(&program, &mut symbols, CompileMode::Script);
result.is_err(),
"host fn should not be callable when compiler has no specs"
async fn get_config_missing_name_yields_error_envelope() {
let resp = handle_form_smoke("(:id 20 :form (get-config \"\"))").await;
assert!(resp.contains(":id 20"), "{resp}");
assert!(resp.contains(":error"), "{resp}");
assert!(!resp.contains(":value"), "{resp}");
async fn get_config_success_shape_is_config_value() {
// Passes a non-empty name; the DB call fails (no pool) and surfaces
// as an :error envelope — we verify the SUCCESS wire shape is only
// emitted when the command actually succeeds. Here we just confirm
// the argument-validation path (empty string) always errors.
let resp = handle_form_smoke("(:id 21 :form (get-config \"\"))").await;
assert!(resp.contains(":id 21"), "{resp}");
async fn a_printed_number_shows_its_kind() {
assert_values(&[
("(+ 1 1)", "2"),
("(* 1/2 2/1)", "1/1"),
("(* 1/2 1/2)", "1/4"),
("'(2/1 3)", "(2/1 3/1)"),
("(index->scalar 2)", "2/1"),
("(scalar->index 2/1)", "2"),
])