1
use std::collections::HashMap;
2

            
3
use chrono::{DateTime, Utc};
4
use finance::price::Price;
5
use finance::split::Split;
6
use finance::tag::Tag;
7
use sqlx::types::Uuid;
8

            
9
use super::revision;
10

            
11
539
fn uuid(n: u128) -> Uuid {
12
539
    Uuid::from_u128(n)
13
539
}
14

            
15
108
fn at(text: &str) -> DateTime<Utc> {
16
108
    DateTime::parse_from_rfc3339(text)
17
108
        .expect("valid instant")
18
108
        .with_timezone(&Utc)
19
108
}
20

            
21
111
fn tag(name: &str, value: &str, description: Option<&str>) -> Tag {
22
111
    Tag {
23
111
        id: Uuid::new_v4(),
24
111
        tag_name: name.to_string(),
25
111
        tag_value: value.to_string(),
26
111
        description: description.map(str::to_string),
27
111
    }
28
111
}
29

            
30
54
fn split(n: u128, account: u128, commodity: u128, value: i64) -> Split {
31
54
    Split {
32
54
        id: uuid(n),
33
54
        tx_id: uuid(99),
34
54
        account_id: uuid(account),
35
54
        commodity_id: uuid(commodity),
36
54
        reconcile_state: None,
37
54
        reconcile_date: None,
38
54
        value_num: value,
39
54
        value_denom: 1,
40
54
        lot_id: None,
41
54
    }
42
54
}
43

            
44
type Reconcile = (Option<bool>, Option<&'static str>, Option<u128>);
45

            
46
54
fn price(n: u128, rate: i64, date: &str, from: u128, to: u128) -> Price {
47
54
    Price {
48
54
        id: uuid(n),
49
54
        date: at(date),
50
54
        commodity_id: uuid(5),
51
54
        currency_id: uuid(4),
52
54
        commodity_split: Some(uuid(to)),
53
54
        currency_split: Some(uuid(from)),
54
54
        value_num: rate,
55
54
        value_denom: 9,
56
54
    }
57
54
}
58

            
59
type TagText = (&'static str, &'static str, Option<&'static str>);
60

            
61
#[derive(Clone)]
62
struct State {
63
    date: &'static str,
64
    note: Option<&'static str>,
65
    accounts: [u128; 2],
66
    commodity: u128,
67
    value: i64,
68
    tags: Vec<TagText>,
69
    transaction_tags: Vec<TagText>,
70
    rate: i64,
71
    price_date: &'static str,
72
    links: (u128, u128),
73
    reconcile: Reconcile,
74
}
75

            
76
2
fn base() -> State {
77
2
    State {
78
2
        date: "2026-10-27T12:00:00Z",
79
2
        note: Some("n"),
80
2
        accounts: [11, 12],
81
2
        commodity: 4,
82
2
        value: 10,
83
2
        tags: vec![("memo", "m", Some("d")), ("k", "v", None)],
84
2
        transaction_tags: vec![("trip", "japan", Some("spring")), ("cleared", "yes", None)],
85
2
        rate: 10,
86
2
        price_date: "2026-10-20T10:11:12.345678Z",
87
2
        links: (1, 2),
88
2
        reconcile: (Some(false), Some("2026-10-28T09:00:00.5Z"), Some(30)),
89
2
    }
90
2
}
91

            
92
54
fn tags_of(texts: &[TagText]) -> Vec<Tag> {
93
54
    texts
94
54
        .iter()
95
106
        .map(|(name, value, description)| tag(name, value, *description))
96
        .collect()
97
54
}
98

            
99
27
fn digest(state: &State, reverse: bool) -> String {
100
27
    let (reconcile_state, reconcile_date, lot) = state.reconcile;
101
27
    let mut splits = vec![
102
27
        Split {
103
27
            reconcile_state,
104
27
            reconcile_date: reconcile_date.map(at),
105
27
            lot_id: lot.map(uuid),
106
27
            ..split(1, state.accounts[0], state.commodity, -state.value)
107
27
        },
108
27
        split(2, state.accounts[1], 5, 9),
109
    ];
110
27
    let mut tags = tags_of(&state.tags);
111
27
    let mut transaction_tags = tags_of(&state.transaction_tags);
112
27
    let mut prices = vec![
113
27
        price(
114
27
            7,
115
27
            state.rate,
116
27
            state.price_date,
117
27
            state.links.0,
118
27
            state.links.1,
119
27
        ),
120
27
        price(8, 3, "2026-10-01T00:00:00Z", 1, 2),
121
    ];
122
27
    if reverse {
123
1
        splits.reverse();
124
1
        tags.reverse();
125
1
        transaction_tags.reverse();
126
1
        prices.reverse();
127
26
    }
128
27
    let split_tags = HashMap::from([(uuid(1), tags)]);
129
27
    revision(
130
27
        at(state.date),
131
27
        state.note,
132
27
        &transaction_tags,
133
27
        &splits,
134
27
        &split_tags,
135
27
        &prices,
136
27
    )
137
27
}
138

            
139
#[test]
140
1
fn each_covered_field_changes_the_digest() {
141
1
    let base = base();
142
1
    let original = digest(&base, false);
143
1
    let changes: Vec<(&str, State)> = vec![
144
1
        (
145
1
            "date",
146
1
            State {
147
1
                date: "2026-10-27T12:00:01Z",
148
1
                ..base.clone()
149
1
            },
150
1
        ),
151
1
        (
152
1
            "note",
153
1
            State {
154
1
                note: Some("m"),
155
1
                ..base.clone()
156
1
            },
157
1
        ),
158
1
        (
159
1
            "note removed",
160
1
            State {
161
1
                note: None,
162
1
                ..base.clone()
163
1
            },
164
1
        ),
165
1
        (
166
1
            "account",
167
1
            State {
168
1
                accounts: [11, 13],
169
1
                ..base.clone()
170
1
            },
171
1
        ),
172
1
        (
173
1
            "commodity",
174
1
            State {
175
1
                commodity: 6,
176
1
                ..base.clone()
177
1
            },
178
1
        ),
179
1
        (
180
1
            "value",
181
1
            State {
182
1
                value: 11,
183
1
                ..base.clone()
184
1
            },
185
1
        ),
186
1
        (
187
1
            "tag name",
188
1
            State {
189
1
                tags: vec![("memo2", "m", Some("d")), ("k", "v", None)],
190
1
                ..base.clone()
191
1
            },
192
1
        ),
193
1
        (
194
1
            "tag value",
195
1
            State {
196
1
                tags: vec![("memo", "m2", Some("d")), ("k", "v", None)],
197
1
                ..base.clone()
198
1
            },
199
1
        ),
200
1
        (
201
1
            "tag description",
202
1
            State {
203
1
                tags: vec![("memo", "m", None), ("k", "v", None)],
204
1
                ..base.clone()
205
1
            },
206
1
        ),
207
1
        (
208
1
            "tag removed",
209
1
            State {
210
1
                tags: vec![("k", "v", None)],
211
1
                ..base.clone()
212
1
            },
213
1
        ),
214
1
        (
215
1
            "transaction tag value",
216
1
            State {
217
1
                transaction_tags: vec![("trip", "korea", Some("spring")), ("cleared", "yes", None)],
218
1
                ..base.clone()
219
1
            },
220
1
        ),
221
1
        (
222
1
            "transaction tag description",
223
1
            State {
224
1
                transaction_tags: vec![("trip", "japan", None), ("cleared", "yes", None)],
225
1
                ..base.clone()
226
1
            },
227
1
        ),
228
1
        (
229
1
            "transaction tag removed",
230
1
            State {
231
1
                transaction_tags: vec![("cleared", "yes", None)],
232
1
                ..base.clone()
233
1
            },
234
1
        ),
235
1
        (
236
1
            "split tag moved to the transaction",
237
1
            State {
238
1
                tags: vec![("k", "v", None)],
239
1
                transaction_tags: vec![
240
1
                    ("trip", "japan", Some("spring")),
241
1
                    ("cleared", "yes", None),
242
1
                    ("memo", "m", Some("d")),
243
1
                ],
244
1
                ..base.clone()
245
1
            },
246
1
        ),
247
1
        (
248
1
            "reconcile state",
249
1
            State {
250
1
                reconcile: (Some(true), base.reconcile.1, base.reconcile.2),
251
1
                ..base.clone()
252
1
            },
253
1
        ),
254
1
        (
255
1
            "reconcile state cleared",
256
1
            State {
257
1
                reconcile: (None, base.reconcile.1, base.reconcile.2),
258
1
                ..base.clone()
259
1
            },
260
1
        ),
261
1
        (
262
1
            "reconcile date",
263
1
            State {
264
1
                reconcile: (
265
1
                    base.reconcile.0,
266
1
                    Some("2026-10-28T09:00:00.500001Z"),
267
1
                    base.reconcile.2,
268
1
                ),
269
1
                ..base.clone()
270
1
            },
271
1
        ),
272
1
        (
273
1
            "reconcile date cleared",
274
1
            State {
275
1
                reconcile: (base.reconcile.0, None, base.reconcile.2),
276
1
                ..base.clone()
277
1
            },
278
1
        ),
279
1
        (
280
1
            "lot",
281
1
            State {
282
1
                reconcile: (base.reconcile.0, base.reconcile.1, Some(31)),
283
1
                ..base.clone()
284
1
            },
285
1
        ),
286
1
        (
287
1
            "lot cleared",
288
1
            State {
289
1
                reconcile: (base.reconcile.0, base.reconcile.1, None),
290
1
                ..base.clone()
291
1
            },
292
1
        ),
293
1
        (
294
1
            "rate",
295
1
            State {
296
1
                rate: 11,
297
1
                ..base.clone()
298
1
            },
299
1
        ),
300
1
        (
301
1
            "price date",
302
1
            State {
303
1
                price_date: "2026-10-20T10:11:12.345679Z",
304
1
                ..base.clone()
305
1
            },
306
1
        ),
307
1
        (
308
1
            "price links",
309
1
            State {
310
1
                links: (2, 1),
311
1
                ..base.clone()
312
1
            },
313
1
        ),
314
    ];
315
1
    for (name, changed) in changes {
316
23
        assert_ne!(digest(&changed, false), original, "{name}");
317
    }
318
1
}
319

            
320
#[test]
321
1
fn order_of_splits_tags_and_prices_does_not_matter() {
322
1
    let base = base();
323
1
    assert_eq!(digest(&base, true), digest(&base, false));
324
1
    assert_eq!(digest(&base, false).len(), 64);
325
1
}
326

            
327
#[test]
328
1
fn a_padded_note_name_is_the_note_in_the_stored_revision() {
329
1
    let when = at("2026-03-01T00:00:00Z");
330
1
    let padded = vec![tag(" note\u{a0}", "lunch", None), tag("k", "v", None)];
331
1
    let plain = vec![tag("note", "lunch", None), tag("k", "v", None)];
332

            
333
1
    let stored = super::stored_revision(when, padded, &[], &HashMap::new(), &[]);
334

            
335
1
    assert_eq!(
336
1
        stored,
337
1
        super::stored_revision(when, plain, &[], &HashMap::new(), &[])
338
    );
339
1
    assert_eq!(
340
1
        stored,
341
1
        revision(
342
1
            when,
343
1
            Some("lunch"),
344
1
            &[tag("k", "v", None)],
345
1
            &[],
346
1
            &HashMap::new(),
347
1
            &[]
348
1
        )
349
    );
350
1
}