Lines
42.86 %
Functions
Branches
100 %
use super::*;
const GROCERIES_SCRIPT_WASM: &[u8] =
include_bytes!("../../../../web/static/wasm/groceries_markup.wasm");
const TAG_SYNC_SCRIPT_WASM: &[u8] = include_bytes!("../../../../web/static/wasm/tag_sync.wasm");
#[local_db_sqlx_test]
async fn test_create_transaction_with_all_scripts_completes(pool: PgPool) -> anyhow::Result<()> {
let user = USER.get().unwrap();
user.commit()
.await
.expect("Failed to commit user to database");
let mut conn = user.get_connection().await?;
let groceries_script_id = user
.create_script(GROCERIES_SCRIPT_WASM.to_vec(), None)
.await?;
let tag_sync_script_id = user
.create_script(TAG_SYNC_SCRIPT_WASM.to_vec(), None)
let commodity_result = CreateCommodity::new()
.symbol("TST".to_string())
.name("Test Commodity".to_string())
.user_id(user.id)
.run()
let commodity_id = if let Some(CmdResult::String(id)) = commodity_result {
uuid::Uuid::parse_str(&id)?
} else {
panic!("Expected commodity ID string result");
};
let account1 = if let Some(CmdResult::Entity(FinanceEntity::Account(account))) =
CreateAccount::new()
.name("Account 1".to_string())
.await?
{
account
panic!("Expected account entity result");
let account2 = if let Some(CmdResult::Entity(FinanceEntity::Account(account))) =
.name("Account 2".to_string())
let tx_id = Uuid::new_v4();
let split1_id = Uuid::new_v4();
let split2_id = Uuid::new_v4();
let split1 = Split {
id: split1_id,
tx_id,
account_id: account1.id,
commodity_id,
value_num: -5000,
value_denom: 100,
reconcile_state: None,
reconcile_date: None,
lot_id: None,
let split2 = Split {
id: split2_id,
account_id: account2.id,
value_num: 5000,
let splits = vec![FinanceEntity::Split(split1), FinanceEntity::Split(split2)];
let now = Utc::now();
// A liveness guard, not a performance budget: it exists so a script that
// deadlocks fails the suite instead of hanging it. The bound has to hold
// on the slowest machine that runs this, which is a 4-vCPU CI node under
// -Cinstrument-coverage executing two wasm scripts — 10s failed there
// while passing everywhere else, which is a flaky test, not a finding.
let result = tokio::time::timeout(
std::time::Duration::from_secs(120),
CreateTransaction::new()
.splits(splits)
.id(tx_id)
.post_date(now)
.enter_date(now)
.note("groceries".to_string())
.run(),
)
.await;
assert!(
result.is_ok(),
"Transaction creation with scripts hung (>120s) — a script or lock is stuck"
);
result.unwrap()?;
let split1_tags = sqlx::query_file!("sql/select/tags/by_split.sql", split1_id)
.fetch_all(&mut *conn)
let split1_has_category = split1_tags
.iter()
.any(|t| t.tag_name == "category" && t.tag_value == "groceries");
split1_has_category,
"Split 1 should have category=groceries tag from groceries script"
user.delete_script(groceries_script_id).await?;
user.delete_script(tag_sync_script_id).await?;
}
async fn test_create_transaction_with_script(pool: PgPool) -> anyhow::Result<()> {
// Insert the groceries script into the database
let script_id = user
// Create commodity and accounts
// Create transaction with note="groceries" - this should trigger the script
// Verify that the script added "category=groceries" tags to both splits
let split2_tags = sqlx::query_file!("sql/select/tags/by_split.sql", split2_id)
// Check split1 has the category tag
"Split 1 should have category=groceries tag from script. Tags: {:?}",
split1_tags
.map(|t| format!("{}={}", t.tag_name, t.tag_value))
.collect::<Vec<_>>()
// Check split2 has the category tag
let split2_has_category = split2_tags
split2_has_category,
"Split 2 should have category=groceries tag from script. Tags: {:?}",
split2_tags
// Clean up the script
user.delete_script(script_id).await?;
async fn test_create_transaction_script_skips_non_matching(pool: PgPool) -> anyhow::Result<()> {
// Create transaction with note="other" - this should NOT trigger the script
.note("other".to_string())
// Verify that the script did NOT add any tags to splits
// Neither split should have the category tag
!split1_has_category,
"Split 1 should NOT have category tag for non-groceries transaction"
!split2_has_category,
"Split 2 should NOT have category tag for non-groceries transaction"