Lines
100 %
Functions
Branches
//! Phase 7 of `doc/ops/0010-common-lisp-booleans.org`: `(eq? nil v)` / `(eq?
//! t v)` against a Money-typed `v` (a type `nil_repr` says is never `t` or
//! `nil`) must evaluate `v` once and yield `nil`, not the old
//! "= expects numeric arguments, got nil" compile error.
//!
//! Index and Ratio operands are covered without a database in
//! `scripting/nms/tests/comparison.rs`; a Money-typed runtime value only
//! exists behind the DB-backed `account-balance` host fn, so that case lives
//! here end to end: nomiscript form -> `rpc::Session` -> `server::command::*`
//! -> real Postgres.
//! Gated on the `db` feature. Run via:
//! DATABASE_URL=postgres://… cargo test -p tests-integration --features db
#![cfg(feature = "db")]
use rpc::{ScriptCtx, Session};
use server::db::install_test_pool;
use sqlx::PgPool;
use supp_macro::local_db_sqlx_test;
use uuid::Uuid;
async fn setup() {}
async fn insert_test_user(pool: &PgPool, id: Uuid) -> anyhow::Result<()> {
sqlx::query!(
"INSERT INTO users (
id, user_name, email, photo, verified, user_password,
user_role, database_name, created_at
) VALUES (
$1, 'rpc-test-user', 'rpc-test@example.com', 'default.png',
TRUE, 'irrelevant', 'user', 'rpc-test', NOW()
)",
id
)
.execute(pool)
.await?;
Ok(())
}
fn extract_id_field(response: &str) -> Option<String> {
let needle = ":value \"";
let start = response.find(needle)? + needle.len();
let end = start + response[start..].find('"')?;
Some(response[start..end].to_string())
/// Builds a fresh user with one account holding a real Money balance (a
/// single split of `100` in a fresh commodity), so `(account-balance
/// "<acct>")` produces a genuine runtime `Commodity` value rather than the
/// "no commodity yet" error.
async fn account_with_balance(pool: &PgPool) -> anyhow::Result<(Session, String)> {
let user_id = Uuid::new_v4();
insert_test_user(pool, user_id).await?;
let mut session = Session::new(ScriptCtx::new(user_id)).expect("Session::new");
let resp = session
.handle_form("(:id 1 :form (create-commodity \"BAR\" \"Bar Coin\"))")
.await;
let commodity = extract_id_field(&resp).expect("commodity id");
.handle_form("(:id 2 :form (create-account \"Bar-A\" \"\"))")
let acct_a = extract_id_field(&resp).expect("account A id");
.handle_form("(:id 3 :form (create-account \"Bar-B\" \"\"))")
let acct_b = extract_id_field(&resp).expect("account B id");
let tx = format!(
"(:id 4 :form (create-transaction \"(:post-date \\\"2026-02-01T00:00:00Z\\\" \
:note \\\"bar-tx\\\" \
:splits ((:account-id \\\"{acct_a}\\\" :commodity-id \\\"{commodity}\\\" :value -100) \
(:account-id \\\"{acct_b}\\\" :commodity-id \\\"{commodity}\\\" :value 100)))\"))"
);
session.handle_form(&tx).await;
Ok((session, acct_a))
#[local_db_sqlx_test]
async fn eq_nil_against_money_is_nil(pool: PgPool) -> anyhow::Result<()> {
let (mut session, acct) = account_with_balance(&pool).await?;
let response = session
.handle_form(&format!(
"(:id 5 :form (eq? nil (account-balance \"{acct}\")))"
))
assert!(response.contains(":id 5"), "got: {response}");
assert!(
response.contains(":value nil)"),
"expected nil, got: {response}"
async fn eq_money_against_nil_is_nil_reversed_order(pool: PgPool) -> anyhow::Result<()> {
"(:id 6 :form (eq? (account-balance \"{acct}\") nil))"
assert!(response.contains(":id 6"), "got: {response}");
async fn eq_t_against_money_is_nil(pool: PgPool) -> anyhow::Result<()> {
"(:id 7 :form (eq? t (account-balance \"{acct}\")))"
assert!(response.contains(":id 7"), "got: {response}");
/// The Money operand's real effect (a live DB lookup) must run — `eq?`
/// evaluating it for effect only, then discarding the value, must not skip
/// the call. `account-balance` on an unknown account id is an error, so if
/// the operand's evaluation were dropped this would wrongly succeed as nil.
async fn eq_nil_against_money_still_runs_the_operand(pool: PgPool) -> anyhow::Result<()> {
insert_test_user(&pool, user_id).await?;
.handle_form(
"(:id 8 :form (eq? nil (account-balance \"00000000-0000-0000-0000-000000000000\")))",
response.contains(":error") || response.contains(":code"),
"expected the account-balance lookup to still run and fail, got: {response}"
/// The exact shape found in review: the Money operand isn't the bare host-fn
/// call but a `(setf <defparameter> (account-balance …))` — a compile-time
/// (non-`WasmLocal`-backed) place assigned a runtime RHS. Before the SETF
/// fix (`compile_const_place_setf_rhs`), the const place's RHS rode
/// `eval_value` alone, which never emits wasm, so `account-balance` was
/// never actually called and this wrongly reported `nil` instead of the
/// missing-account error.
async fn eq_nil_against_setf_of_money_still_runs_the_operand(pool: PgPool) -> anyhow::Result<()> {
"(:id 9 :form (progn (defparameter p7x 0) \
(eq? nil (setf p7x (account-balance \"00000000-0000-0000-0000-000000000000\")))))",
"expected the account-balance lookup (via SETF) to still run and fail, got: {response}"