1
use sqlx::PgPool;
2

            
3
use super::load_form;
4
use super::test_account_edit::{loaded, seeded};
5
use super::test_edit::{create, fx};
6
use super::test_seed::submit_edit;
7
use super::tests_transaction_tags::{TRIP, stored, tagged};
8
use crate::forms::{detail, submit, submit_noticed};
9
use forms::ACCOUNT_EDIT;
10

            
11
const POSTED: &str = "2026-10-27T12:00:00Z";
12

            
13
#[sqlx::test(migrator = "server::db::MIGRATOR")]
14
async fn a_padded_stored_note_is_read_trimmed(pool: PgPool) -> anyhow::Result<()> {
15
    let _guard = server::db::install_test_pool(pool.clone());
16
    let fx = fx(&pool).await?;
17
    let tx = create(&fx, POSTED, &[fx.row("10", "")]).await?;
18
    sqlx::query("UPDATE tags SET tag_name = 'note ', tag_value = ' n\t' WHERE tag_value = 'n'")
19
        .execute(&pool)
20
        .await?;
21

            
22
    let shown = detail(fx.user, tx).await?;
23
    assert_eq!(shown.note.as_deref(), Some("n"));
24
    let reply = load_form(fx.user, "transaction-edit", &tx.to_string()).await?;
25
    assert!(reply.contains(":note \"n\""), "{reply}");
26
    Ok(())
27
}
28

            
29
#[sqlx::test(migrator = "server::db::MIGRATOR")]
30
async fn resubmitting_a_padded_stored_name_is_not_a_rename(pool: PgPool) -> anyhow::Result<()> {
31
    let _guard = server::db::install_test_pool(pool.clone());
32
    let fx = seeded(&pool).await?;
33
    sqlx::query("UPDATE tags SET tag_value = ' Cash ' WHERE id IN (SELECT tag_id FROM account_tags WHERE account_id = $1) AND tag_name = 'name'")
34
        .bind(fx.account)
35
        .execute(&pool)
36
        .await?;
37
    super::test_seed::seed_child_account(fx.user, "Cash", fx.parent).await?;
38
    let values = loaded(fx.user, fx.account).await?;
39

            
40
    let (_, notice) = submit_noticed(fx.user, &ACCOUNT_EDIT, &values).await?;
41
    assert_eq!(notice, None);
42
    Ok(())
43
}
44

            
45
2
async fn resubmits_unchanged(pool: PgPool, padding: &'static str) -> anyhow::Result<()> {
46
2
    let _guard = server::db::install_test_pool(pool.clone());
47
2
    let fx = fx(&pool).await?;
48
2
    let tx = create(&fx, POSTED, &[fx.row("10", "")]).await?;
49
2
    sqlx::query(padding).execute(&pool).await?;
50
2
    let payload = load_form(fx.user, "transaction-edit", &tx.to_string()).await?;
51
2
    submit_edit(fx.user, &payload).await?;
52
2
    Ok(())
53
2
}
54

            
55
#[sqlx::test(migrator = "server::db::MIGRATOR")]
56
async fn a_padded_note_value_edits_without_a_stale_revision(pool: PgPool) -> anyhow::Result<()> {
57
    resubmits_unchanged(
58
        pool,
59
        "UPDATE tags SET tag_value = ' n\t' WHERE tag_value = 'n'",
60
    )
61
    .await
62
}
63

            
64
#[sqlx::test(migrator = "server::db::MIGRATOR")]
65
async fn a_padded_note_name_edits_without_a_stale_revision(pool: PgPool) -> anyhow::Result<()> {
66
    resubmits_unchanged(
67
        pool,
68
        "UPDATE tags SET tag_name = 'note ' WHERE tag_value = 'n'",
69
    )
70
    .await
71
}
72

            
73
2
async fn padded_tx_tag_resubmits_unchanged(
74
2
    pool: PgPool,
75
2
    padding: &'static str,
76
2
) -> anyhow::Result<()> {
77
2
    let _guard = server::db::install_test_pool(pool.clone());
78
2
    let fx = fx(&pool).await?;
79
2
    let tx = tagged(&fx, &[TRIP]).await?;
80
2
    sqlx::query(padding).execute(&pool).await?;
81
2
    let before = stored(&pool, tx).await?;
82
2
    let payload = load_form(fx.user, "transaction-edit", &tx.to_string()).await?;
83

            
84
2
    submit_edit(fx.user, &payload).await?;
85

            
86
2
    assert_eq!(stored(&pool, tx).await?, before);
87
2
    Ok(())
88
2
}
89

            
90
#[sqlx::test(migrator = "server::db::MIGRATOR")]
91
async fn a_padded_tag_name_is_kept_by_a_no_op_edit(pool: PgPool) -> anyhow::Result<()> {
92
    padded_tx_tag_resubmits_unchanged(
93
        pool,
94
        "UPDATE tags SET tag_name = 'trip ' WHERE tag_name = 'trip'",
95
    )
96
    .await
97
}
98

            
99
#[sqlx::test(migrator = "server::db::MIGRATOR")]
100
async fn a_padded_tag_value_is_kept_by_a_no_op_edit(pool: PgPool) -> anyhow::Result<()> {
101
    padded_tx_tag_resubmits_unchanged(
102
        pool,
103
        "UPDATE tags SET tag_value = ' japan\t' WHERE tag_value = 'japan'",
104
    )
105
    .await
106
}
107

            
108
4
async fn account_tags_raw(pool: &PgPool, account: uuid::Uuid) -> anyhow::Result<Vec<String>> {
109
4
    Ok(sqlx::query_scalar(
110
4
        "SELECT t.id || ':' || t.tag_name || '=' || t.tag_value FROM account_tags at \
111
4
         JOIN tags t ON t.id = at.tag_id WHERE at.account_id = $1 ORDER BY 1",
112
4
    )
113
4
    .bind(account)
114
4
    .fetch_all(pool)
115
4
    .await?)
116
4
}
117

            
118
2
async fn padded_account_tag_resubmits_unchanged(
119
2
    pool: PgPool,
120
2
    padding: &'static str,
121
2
) -> anyhow::Result<()> {
122
2
    let _guard = server::db::install_test_pool(pool.clone());
123
2
    let fx = seeded(&pool).await?;
124
2
    sqlx::query(padding).execute(&pool).await?;
125
2
    let before = account_tags_raw(&pool, fx.account).await?;
126
2
    let values = loaded(fx.user, fx.account).await?;
127

            
128
2
    submit(fx.user, &ACCOUNT_EDIT, &values).await?;
129

            
130
2
    assert_eq!(account_tags_raw(&pool, fx.account).await?, before);
131
2
    Ok(())
132
2
}
133

            
134
#[sqlx::test(migrator = "server::db::MIGRATOR")]
135
async fn a_padded_account_tag_value_is_not_rewritten_by_a_no_op_edit(
136
    pool: PgPool,
137
) -> anyhow::Result<()> {
138
    padded_account_tag_resubmits_unchanged(
139
        pool,
140
        "UPDATE tags SET tag_value = ' red ' WHERE tag_name = 'color'",
141
    )
142
    .await
143
}
144

            
145
#[sqlx::test(migrator = "server::db::MIGRATOR")]
146
async fn a_padded_account_tag_name_is_not_rewritten_by_a_no_op_edit(
147
    pool: PgPool,
148
) -> anyhow::Result<()> {
149
    padded_account_tag_resubmits_unchanged(
150
        pool,
151
        "UPDATE tags SET tag_name = 'color ' WHERE tag_name = 'color'",
152
    )
153
    .await
154
}
155

            
156
const SHARED: &str = "(:name \"trip\" :value \"japan\" :description \"D2\")";
157

            
158
/// Relink `tx` from the trimmed `trip=japan` row to a padded twin described `D1`.
159
1
async fn pad_link(pool: &PgPool, tx: uuid::Uuid) -> anyhow::Result<()> {
160
1
    sqlx::query(
161
1
        "WITH padded AS (INSERT INTO tags (id, tag_name, tag_value, description) \
162
1
         VALUES (gen_random_uuid(), 'trip ', 'japan', 'D1') RETURNING id) \
163
1
         UPDATE transaction_tags SET tag_id = (SELECT id FROM padded) \
164
1
         WHERE tx_id = $1 AND tag_id = (SELECT id FROM tags WHERE tag_name = 'trip')",
165
1
    )
166
1
    .bind(tx)
167
1
    .execute(pool)
168
1
    .await?;
169
1
    Ok(())
170
1
}
171

            
172
2
async fn tag_rows(pool: &PgPool) -> anyhow::Result<Vec<String>> {
173
2
    Ok(sqlx::query_scalar(
174
2
        "SELECT id || ':' || tag_name || '=' || tag_value || ' ' || coalesce(description, '-') \
175
2
         FROM tags ORDER BY 1",
176
2
    )
177
2
    .fetch_all(pool)
178
2
    .await?)
179
2
}
180

            
181
#[sqlx::test(migrator = "server::db::MIGRATOR")]
182
async fn a_no_op_edit_keeps_a_padded_row_and_the_shared_description(
183
    pool: PgPool,
184
) -> anyhow::Result<()> {
185
    let _guard = server::db::install_test_pool(pool.clone());
186
    let fx = fx(&pool).await?;
187
    let a = tagged(&fx, &[SHARED]).await?;
188
    tagged(&fx, &[SHARED]).await?;
189
    pad_link(&pool, a).await?;
190
    let rows = tag_rows(&pool).await?;
191
    let links = stored(&pool, a).await?;
192
    let content = crate::preflight::content(fx.user, a).await;
193
    let payload = load_form(fx.user, "transaction-edit", &a.to_string()).await?;
194

            
195
    submit_edit(fx.user, &payload).await?;
196

            
197
    assert_eq!(tag_rows(&pool).await?, rows);
198
    assert_eq!(stored(&pool, a).await?, links);
199
    assert_eq!(crate::preflight::content(fx.user, a).await, content);
200
    Ok(())
201
}