Lines
100 %
Functions
Branches
//! Pins the template prefill of the web create page (plan 0016): the template
//! wins over `from_account`, carries note, tags and an explicit date, and
//! refuses a malformed date or an unrepresentable pairing. Since W5 the page
//! is the shared-forms page and its prefill is `load-form
//! "transaction-create"`.
use axum::http::StatusCode;
use forms::Source;
use sqlx::types::Uuid;
use web::model::User;
use crate::common::new_page::{
created_id, get_page, input_value, option_id, post_form, seeded_user, snapshot, textarea,
without_entered,
};
use crate::common::seed::named_account;
const GROCERIES: &str = r#"
(set-draft-note "Groceries")
(set-draft-date "2026-06-15")
(draft-tag "category" "food")
(draft-split (get-account "Assets:Checking") (get-commodity "USD") -50/1)
(draft-split (get-account "Expenses:Food") (get-commodity "USD") 50/1)
"#;
async fn template(user: &User, source: &str) -> Uuid {
server::user::User { id: user.id }
.create_template(source.to_string(), Some("prefill".to_string()))
.await
.expect("create template")
}
#[supp_macro::shared_runtime_test]
async fn a_template_prefill_carries_note_tags_date_and_rows() {
let user = seeded_user().await;
let id = template(&user, GROCERIES).await;
let checking = option_id(&user, Source::Accounts, "Assets:Checking").await;
let food = option_id(&user, Source::Accounts, "Expenses:Food").await;
let reply = get_page(&user, &format!("template={id}")).await;
assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
let html = &reply.body;
assert_eq!(textarea(html, "note"), Some("Groceries"));
assert_eq!(input_value(html, "date"), Some("2026-06-15T12:00:00Z"));
assert_eq!(input_value(html, "tags.0.name"), Some("category"));
assert_eq!(input_value(html, "tags.0.value"), Some("food"));
let id_text = |id: Uuid| id.to_string();
assert_eq!(
input_value(html, "splits.0.from"),
Some(id_text(checking).as_str())
);
input_value(html, "splits.0.to"),
Some(id_text(food).as_str())
input_value(html, "label:splits.0.from"),
Some("Assets:Checking")
input_value(html, "label:splits.0.to"),
Some("Expenses:Food")
assert_eq!(input_value(html, "splits.0.value"), Some("50"));
assert_eq!(input_value(html, "splits.1.from"), None);
/// Flipped in W5: the server drops `from_account` when a template is given
/// (before, both were emitted and only the wasm picked the draft).
async fn a_template_wins_over_from_account() {
let wallet = named_account(user.id, "Assets:Wallet").await;
let reply = get_page(&user, &format!("from_account={wallet}&template={id}")).await;
assert_eq!(textarea(&reply.body, "note"), Some("Groceries"));
input_value(&reply.body, "label:splits.0.from"),
assert!(!reply.body.contains("Assets:Wallet"), "{}", reply.body);
assert!(!reply.body.contains(&wallet.to_string()), "{}", reply.body);
async fn a_template_without_a_date_starts_now() {
let id = template(
&user,
&GROCERIES.replace("(set-draft-date \"2026-06-15\")", ""),
)
.await;
let date = input_value(&reply.body, "date").unwrap_or_default();
assert!(chrono::DateTime::parse_from_rfc3339(date).is_ok(), "{date}");
assert!(date.ends_with('Z'), "{date}");
/// The refusal of a page a browser navigated to is an HTML page in the
/// layout with a way back, never raw JSON.
async fn a_refused_template_is_an_html_page() {
let source = GROCERIES.replace("2026-06-15", "15th of June");
let id = template(&user, &source).await;
assert_refused(reply.status, &reply.body);
let content_type = reply.headers.get("content-type").expect("content type");
assert!(
content_type
.to_str()
.is_ok_and(|t| t.starts_with("text/html"))
reply.body.contains("<title>Error</title>"),
"{}",
reply.body
assert!(reply.body.contains(">Back</a>"), "{}", reply.body);
assert!(!reply.body.contains("\"status\""), "{}", reply.body);
fn assert_refused(status: StatusCode, body: &str) {
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{body}");
assert!(body.contains("cannot be shown as a form"), "{body}");
assert!(!body.contains("nf-transaction-create"), "{body}");
async fn a_template_with_a_malformed_date_is_refused() {
/// Flipped in W5: the `1/3` pairing is exact, so the shared loader shows it;
/// the legacy page refused it as non-terminating.
async fn a_template_with_fractions_prefills_them_exactly() {
let source = GROCERIES
.replace("-50/1)", "-1/3)")
.replace(" 50/1)", " 1/3)");
assert_eq!(input_value(&reply.body, "splits.0.value"), Some("1/3"));
async fn a_template_with_an_unpaired_leg_is_refused() {
let unpaired = format!(
"{GROCERIES}(draft-split (get-account \"Expenses:Food\") (get-commodity \"USD\") 1/1)"
let id = template(&user, &unpaired).await;
async fn a_failing_template_shows_the_authors_error() {
let id = template(&user, "(error 'my-code \"boom\")").await;
assert!(reply.body.contains("boom"), "{}", reply.body);
async fn a_missing_template_is_a_404() {
let reply = get_page(&user, &format!("template={}", Uuid::new_v4())).await;
assert_eq!(reply.status, StatusCode::NOT_FOUND, "{}", reply.body);
!reply.body.contains("nf-transaction-create"),
async fn a_same_currency_template_leaves_the_to_commodity_empty() {
assert_eq!(input_value(html, "splits.0.to-commodity"), Some(""));
assert_eq!(input_value(html, "label:splits.0.to-commodity"), Some(""));
input_value(html, "label:splits.0.from-commodity"),
Some("USD")
const PREFILLED: [&str; 12] = [
"date",
"tags.0.name",
"tags.0.value",
"splits.0.key",
"splits.0.from",
"splits.0.to",
"splits.0.from-commodity",
"splits.0.to-commodity",
"label:splits.0.from-commodity",
"label:splits.0.to-commodity",
"splits.0.value",
"splits.0.to-amount",
];
fn prefilled(html: &str) -> Vec<(&'static str, &str)> {
let mut pairs = vec![
("note", textarea(html, "note").unwrap_or_default()),
("tags", ""),
("splits", ""),
("splits.0.hidden", ""),
("split-tags", ""),
pairs.extend(PREFILLED.map(|name| (name, input_value(html, name).unwrap_or_default())));
pairs
/// The prefilled same-currency row, its to-commodity empty, submits as the
/// plain one-commodity entry with no conversion price.
async fn a_prefilled_same_currency_template_submits_without_a_price() {
let page = get_page(&user, &format!("template={id}")).await;
let pairs = prefilled(&page.body);
let plain: Vec<_> = pairs
.iter()
.copied()
.filter(|(name, _)| !name.ends_with("to-commodity"))
.collect();
let posted = post_form(&user, &pairs).await;
let baseline = post_form(&user, &plain).await;
let facts = snapshot(&user, created_id(&posted)).await;
let before = snapshot(&user, created_id(&baseline)).await;
assert_eq!(page.status, StatusCode::OK, "{}", page.body);
assert!(pairs.contains(&("splits.0.to-commodity", "")), "{pairs:?}");
assert_eq!(posted.status, StatusCode::OK, "{}", posted.body);
assert!(facts.iter().all(|f| !f.starts_with("price ")), "{facts:?}");
assert_eq!(without_entered(facts), without_entered(before));