Lines
100 %
Functions
Branches
//! The template test answers from the `template-test` query form, and its
//! verdict agrees with the loader of `transaction-create`.
use axum::{Router, http::StatusCode, routing::post};
use web::model::User;
use web::pages::template::draft::{TEST_PATH, template_test};
use crate::common::create_mock_user;
use crate::common::entity_edit::{get_from, post_to};
use crate::common::new_page::Reply;
use crate::common::seed::{named_account, named_commodity};
use crate::common::tenant::provision_tenant;
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)
"#;
const EXTRA_LEG: &str = r#"(draft-split (get-account "Expenses:Food") (get-commodity "USD") 1/1)"#;
fn tagged_legs(first: &str, second: &str) -> String {
format!(
"{GROCERIES}(draft-split-tag 0 \"memo\" \"{first}\")(draft-split-tag 1 \"memo\" \"{second}\")"
)
}
async fn seeded_user() -> User {
let user = create_mock_user();
provision_tenant(user.id).await;
named_commodity(user.id, "USD").await;
named_account(user.id, "Assets:Checking").await;
named_account(user.id, "Expenses:Food").await;
user
fn router() -> Router<std::sync::Arc<web::AppState>> {
Router::new().route(TEST_PATH, post(template_test))
async fn test_source(user: &User, source: &str) -> Reply {
let pairs = [("source".to_string(), source.to_string())];
post_to(router(), user, TEST_PATH, &pairs).await
async fn loads(user: &User, source: &str) -> bool {
let id = server::user::User { id: user.id }
.create_template(source.to_string(), None)
.await
.expect("create template");
rpc::forms::load(user.id, &forms::TRANSACTION_CREATE, &id.to_string())
.is_ok()
#[supp_macro::shared_runtime_test]
async fn a_good_template_shows_its_draft_with_labels() {
let user = seeded_user().await;
let reply = test_source(&user, GROCERIES).await;
assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
for shown in [
"Groceries",
"2026-06-15",
"Assets:Checking",
"Expenses:Food",
"USD",
"-50",
"category=food",
"Ready to pre-fill",
] {
assert!(reply.body.contains(shown), "{shown}: {}", reply.body);
async fn a_draft_that_cannot_prefill_names_the_reason() {
let reply = test_source(&user, &format!("{GROCERIES}{EXTRA_LEG}")).await;
assert!(reply.body.contains("can't pre-fill"), "{}", reply.body);
assert!(
reply.body.contains("odd number of splits"),
"{}",
reply.body
);
async fn a_compile_error_is_a_422_at_the_source() {
let reply = test_source(&user, "(unclosed").await;
assert_eq!(
reply.status,
StatusCode::UNPROCESSABLE_ENTITY,
reply
.headers
.get("hx-retarget")
.and_then(|v| v.to_str().ok()),
Some("#template-test-result")
reply.body.contains("data-field=\"source\""),
async fn a_render_error_is_a_422_at_the_source() {
let reply = test_source(&user, "(error 'my-code \"boom\")").await;
assert!(reply.body.contains("boom"), "{}", reply.body);
async fn an_empty_source_is_a_422_at_the_source() {
let reply = test_source(&user, "").await;
async fn an_unreachable_tenant_is_a_503() {
let reply = test_source(&user, "(get-commodity \"USD\")").await;
StatusCode::SERVICE_UNAVAILABLE,
async fn the_test_agrees_with_the_loader() {
let fraction = GROCERIES
.replace("-50/1)", "-1/3)")
.replace(" 50/1)", " 1/3)");
let cases = [
(GROCERIES.to_string(), true),
(fraction, true),
(GROCERIES.replace("2026-06-15", "15th of June"), false),
(format!("{GROCERIES}{EXTRA_LEG}"), false),
(tagged_legs("a", "b"), true),
(tagged_legs("a", "a"), true),
];
for (source, expected) in cases {
let reply = test_source(&user, &source).await;
assert_eq!(reply.status, StatusCode::OK, "{source}: {}", reply.body);
reply.body.contains("Ready to pre-fill"),
expected,
"{source}: {}",
assert_eq!(loads(&user, &source).await, expected, "{source}");
async fn the_source_travels_in_the_body_not_the_url() {
let query = serde_urlencoded::to_string([("source", GROCERIES)]).expect("query");
let by_get = get_from(router(), &user, &format!("{TEST_PATH}?{query}")).await;
by_get.status,
StatusCode::METHOD_NOT_ALLOWED,
by_get.body
assert_eq!(test_source(&user, GROCERIES).await.status, StatusCode::OK);
async fn a_source_longer_than_8_kb_is_tested() {
let padding = "; padding padding padding padding\n".repeat(400);
let source = format!("{GROCERIES}{padding}");
assert!(source.len() > 8 * 1024);
assert!(reply.body.contains("Ready to pre-fill"), "{}", reply.body);