Lines
100 %
Functions
Branches
use rpc::Session;
use sqlx::PgPool;
use super::*;
use crate::render::reparse_list;
use crate::transaction::tests::seeded_session;
const GROCERIES: &str = r#"(set-draft-note "Groceries")(set-draft-date "2026-06-15")
(draft-tag "category" "food")
(draft-split (get-account "Cash") (get-commodity "USD") -5/1)
(draft-split (get-account "Food") (get-commodity "USD") 5/1)
(draft-split-tag 0 "memo" "lunch")"#;
const ODD: &str = r#"(draft-split (get-account "Cash") (get-commodity "USD") -5/1)"#;
fn option(id: &str, label: &str) -> FormOption {
FormOption {
id: id.to_string(),
label: label.to_string(),
alias: label.to_string(),
commodity: None,
}
fn draft_of(text: &str) -> anyhow::Result<Value> {
Ok(reparse_list(text)?)
async fn listed(session: &mut Session) -> anyhow::Result<Vec<String>> {
Ok(list(session).await?)
#[test]
fn a_draft_prints_labels_and_the_prefill_verdict() -> anyhow::Result<()> {
let draft = draft_of(
r#"(:NOTE "Lunch" :DATE "2026-06-15" :SPLITS
((:ACCOUNT "a1" :AMOUNT "-5" :COMMODITY "c1" :TAGS ((:NAME "memo" :VALUE "x")))
(:ACCOUNT "a2" :AMOUNT "5" :COMMODITY "c1" :TAGS nil))
:TAGS ((:NAME "kind" :VALUE "food")) :PREFILLABLE nil :REASON "it has an odd number of splits")"#,
)?;
let accounts = [option("a1", "Assets:Cash"), option("a2", "Food")];
let lines = render_draft(&draft, &accounts, &[option("c1", "USD")])?;
assert_eq!(
lines,
[
"note: Lunch",
"date: 2026-06-15",
"split: Assets:Cash -5 USD [memo=x]",
"split: Food 5 USD",
"tags: kind=food",
"prefillable: no: it has an odd number of splits",
]
);
Ok(())
fn an_empty_draft_prints_only_the_verdict() -> anyhow::Result<()> {
let draft = draft_of("(:NOTE nil :DATE nil :SPLITS nil :TAGS nil :PREFILLABLE t :REASON nil)")?;
assert_eq!(render_draft(&draft, &[], &[])?, ["prefillable: yes"]);
fn a_reply_that_is_not_a_draft_is_malformed() -> anyhow::Result<()> {
let draft = draft_of("(:SPLITS (1))")?;
assert!(matches!(
render_draft(&draft, &[], &[]),
Err(CommandError::Command(_))
));
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn create_then_list_shows_the_id_and_name(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let mut session = seeded_session(&pool).await?;
let id = create(&mut session, Some("Groceries"), GROCERIES).await?;
assert_eq!(listed(&mut session).await?, [format!("{id} Groceries")]);
assert_eq!(stored_source(&mut session, &id).await?, GROCERIES);
async fn edit_keeps_the_fields_it_is_not_given(pool: PgPool) -> anyhow::Result<()> {
edit(&mut session, &id, None, Some(ODD)).await?;
assert_eq!(stored_source(&mut session, &id).await?, ODD);
edit(&mut session, &id, Some("Renamed"), None).await?;
assert_eq!(listed(&mut session).await?, [format!("{id} Renamed")]);
async fn a_source_that_does_not_compile_is_a_field_error(pool: PgPool) -> anyhow::Result<()> {
let created = create(&mut session, None, "(unclosed").await.map(|_| ());
let tested = test(&mut session, "(unclosed").await.map(|_| ());
for failed in [created, tested] {
match failed {
Err(CommandError::FormInvalid(lines)) => {
assert!(
lines.iter().any(|line| line.starts_with("source: ")
&& line.contains("template compile error")),
"{lines:?}"
)
other => anyhow::bail!("expected a field error, got {other:?}"),
assert!(listed(&mut session).await?.is_empty());
async fn test_prints_the_draft_without_saving(pool: PgPool) -> anyhow::Result<()> {
let lines = test(&mut session, GROCERIES).await?;
"note: Groceries",
"split: Cash -5 USD [memo=lunch]",
"tags: category=food",
"prefillable: yes",
async fn test_names_why_a_draft_cannot_prefill(pool: PgPool) -> anyhow::Result<()> {
let lines = test(&mut session, ODD).await?;
"split: Cash -5 USD",
"prefillable: no: it has an odd number of splits"