Lines
100 %
Functions
91.67 %
Branches
//! The reference preflight: ambiguous and bare names are listed, clean data
//! passes and the tenant is left as it was.
use server::user::User;
use sqlx::PgPool;
use uuid::Uuid;
use super::test_edit::{Fx, create, fx};
use super::test_seed::{seed_account, seed_child_account};
use crate::references::{
Source, Subject, TenantReport, check_references, check_tenant, load_sources as load,
};
const POSTED: &str = "2026-10-27T12:00:00Z";
async fn template(user: Uuid, source: &str) -> anyhow::Result<Uuid> {
Ok(User { id: user }
.create_template(source.to_string(), Some("t".to_string()))
.await?)
}
fn reasons(report: &TenantReport) -> Vec<&str> {
report.findings.iter().map(|f| f.reason.as_str()).collect()
fn source(code: &str) -> anyhow::Result<Source> {
let dir = tempfile::tempdir()?;
std::fs::write(dir.path().join("x.nms"), code)?;
let (mut sources, findings) = load(dir.path());
anyhow::ensure!(findings.is_empty(), "{findings:?}");
sources.pop().ok_or_else(|| anyhow::anyhow!("no source"))
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn clean_references_pass_and_are_counted(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let fx = fx(&pool).await?;
let by_id = format!("(get-account \"{}\")", fx.cash);
template(
fx.user,
&format!("{by_id}(get-account \"Food\")(get-commodity \"usd\")"),
)
.await?;
let report = check_tenant(fx.user, &[]).await;
assert_eq!(report.findings, []);
assert_eq!(
(
report.coverage.templates,
report.coverage.lookups,
report.coverage.resolved
),
(1, 3, 3)
);
Ok(())
async fn an_ambiguous_name_is_listed(pool: PgPool) -> anyhow::Result<()> {
seed_account(fx.user, "Dup").await?;
let id = template(fx.user, "(get-account \"Dup\")").await?;
reasons(&report),
["get-account: account 'Dup' is ambiguous (2 matches)"]
report.findings[0].subject,
Subject::Template(id, Some("t".to_string()))
assert_eq!(report.coverage.resolved, 0);
async fn a_bare_name_is_listed_with_its_path(pool: PgPool) -> anyhow::Result<()> {
seed_child_account(fx.user, "Cafe", fx.food).await?;
"(get-account \"Cafe\")(get-account \"Cafe\")(get-account \"Food:Cafe\")",
["get-account: account 'Cafe' is a bare name: use the path 'Food:Cafe' or the id"]
assert_eq!((report.coverage.lookups, report.coverage.resolved), (3, 1));
async fn an_unknown_commodity_and_a_broken_template_are_listed(pool: PgPool) -> anyhow::Result<()> {
template(fx.user, "(get-commodity \"XXX\")").await?;
template(fx.user, "(set-draft-note").await?;
assert_eq!(report.findings.len(), 2, "{:?}", report.findings);
assert!(
reasons(&report)
.iter()
.any(|r| r.contains("commodity 'XXX' matches no entity"))
async fn a_source_is_judged_by_its_lookup_keys(pool: PgPool) -> anyhow::Result<()> {
let declared = [source(
"(string= (split-account-name s) \"Cafe\")(get-account \"Cash\")(get-account \"Food:Cafe\")\
(get-commodity \"usd\")(get-account \"Cafe\")",
)?];
let report = check_tenant(fx.user, &declared).await;
Subject::Source(declared[0].name.clone())
report.coverage.sources,
(1, 4, 3)
async fn a_run_over_the_directory_is_clean_or_fails(pool: PgPool) -> anyhow::Result<()> {
std::fs::write(dir.path().join("ok.nms"), "(get-account \"Cash\")")?;
let admin = Uuid::new_v4();
let clean = check_references(admin, Some(dir.path())).await?;
std::fs::write(dir.path().join("bad.nms"), "(get-account \"Nope\")")?;
let failing = check_references(admin, Some(dir.path())).await?;
assert_eq!(clean.findings().count(), 0);
assert_eq!(clean.tenants.len(), 1);
assert_eq!(clean.tenants[0].user, fx.user);
assert!(clean.trigger_imports > 0);
let found: Vec<_> = failing.findings().map(|f| f.reason.as_str()).collect();
found,
["get-account: account 'Nope' matches no entity of the user"]
async fn dump(pool: &PgPool) -> anyhow::Result<Vec<String>> {
Ok(sqlx::query_scalar(
"SELECT t::text FROM (SELECT 'tx' AS k, id::text FROM transactions \
UNION ALL SELECT 'ac', id::text FROM accounts \
UNION ALL SELECT 'tg', id::text || tag_value FROM tags \
UNION ALL SELECT 'ar', id::text FROM artifacts) t ORDER BY 1",
.fetch_all(pool)
async fn a_logical_row_key_must_be_an_id(pool: PgPool) -> anyhow::Result<()> {
let code = format!(
"(get-account \"Cash\")(g '(:from \"{}\" :to \"Food\" :from-commodity \"USD\" \
:to-commodity \"{}\"))",
fx.cash, fx.eur
let declared = [source(&code)?];
let mut found = reasons(&report);
found.sort_unstable();
[
format!("row key account 'Food' must be an id: use '{}'", fx.food),
format!("row key commodity 'USD' must be an id: use '{}'", fx.usd),
]
assert_eq!(report.coverage.resolved, 3);
async fn seeded(pool: &PgPool) -> anyhow::Result<Fx> {
let fx = fx(pool).await?;
create(&fx, POSTED, &[fx.row("10", "")]).await?;
template(fx.user, "(get-account \"Cafe\")(get-commodity \"USD\")").await?;
Ok(fx)
async fn the_tenant_is_unchanged(pool: PgPool) -> anyhow::Result<()> {
let fx = seeded(&pool).await?;
let before = dump(&pool).await?;
let report = check_tenant(fx.user, &[source("(get-account \"Nope\")")?]).await;
assert_eq!(report.findings.len(), 2);
assert_eq!(dump(&pool).await?, before);
assert!(before.len() > 4);