Lines
97.96 %
Functions
100 %
Branches
//! The Accounts tab lists accounts by path label, then id, like `form-options`.
use crate::app::App;
use crate::route::{Route, RouteCtx};
use crate::tabs::fetch::Fetch;
use crate::view::ViewId;
const A: &str = "00000000-0000-0000-0000-00000000000a";
const B: &str = "00000000-0000-0000-0000-00000000000b";
const C: &str = "00000000-0000-0000-0000-00000000000c";
const D: &str = "00000000-0000-0000-0000-00000000000d";
fn account(id: &str, name: &str, parent: &str) -> String {
format!(r#"(:account :id "{id}" :name "{name}" :parent "{parent}")"#)
}
fn deliver(app: &mut App, accounts: &[String]) {
let wire = format!("(:id 1 :value ({}))", accounts.join(" "));
app.deliver_reply(
Route {
target: Some(app.window_of(ViewId::Accounts)),
ctx: RouteCtx::None,
},
&wire,
);
fn names(app: &App) -> Vec<String> {
match &app.accounts().state {
Fetch::Loaded(rows) => rows.iter().map(|row| row.cells[0].clone()).collect(),
_ => Vec::new(),
#[test]
fn order_does_not_follow_the_activity_order_of_the_reply() {
let (assets, cash, bank) = (
account(B, "Assets", ""),
account(A, "Cash", B),
account(C, "Bank", ""),
let mut app = crate::test_support::app();
deliver(&mut app, &[bank.clone(), cash.clone(), assets.clone()]);
let first = names(&app);
deliver(&mut app, &[assets, bank, cash]);
assert_eq!(names(&app), first);
assert_eq!(first, ["Assets", "Cash", "Bank"]);
fn a_new_account_lands_in_path_order_and_the_rest_keep_theirs() {
let base = [account(B, "Assets", ""), account(C, "Bank", "")];
deliver(&mut app, &base);
let created = account(D, "Cash", B);
deliver(&mut app, &[created, base[1].clone(), base[0].clone()]);
assert_eq!(names(&app), ["Assets", "Cash", "Bank"]);
async fn eval(session: &mut rpc::Session, form: &str) -> String {
session.handle_form(&format!("(:id 1 :form {form})")).await
fn unquote(wire: &str) -> String {
wire.split('"').nth(1).unwrap_or_default().to_string()
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn tab_order_equals_the_form_options_order(pool: sqlx::PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let fixture = server::lossless::seed(&pool).await?;
let mut session = rpc::Session::new(rpc::ScriptCtx::new(fixture.user))?;
let root = unquote(&eval(&mut session, r#"(create-account "Assets" "")"#).await);
for (name, parent) in [
("Zed", root.as_str()),
("Cash", root.as_str()),
("Bank", ""),
] {
eval(
&mut session,
&format!(r#"(create-account "{name}" "{parent}")"#),
)
.await;
let listed = eval(&mut session, "(list-accounts)").await;
let options = eval(&mut session, r#"(form-options "accounts")"#).await;
&listed,
let Fetch::Loaded(rows) = &app.accounts().state else {
anyhow::bail!("accounts not loaded: {listed}");
};
let tab: Vec<String> = rows.iter().filter_map(|row| row.id.clone()).collect();
let picker: Vec<String> = cli_core::forms::parse_form_options(&options)
.into_iter()
.map(|option| option.id)
.collect();
assert_eq!(tab.len(), 6);
assert_eq!(tab, picker);
Ok(())
async fn tab_order_follows_the_final_labels_of_clashing_names(
pool: sqlx::PgPool,
) -> anyhow::Result<()> {
for name in ["Cash", "Cash Box", "Cash", "A|B", "Cash"] {
eval(&mut session, &format!(r#"(create-account "{name}" "")"#)).await;
assert_eq!(tab.len(), 7);