monarch-mcp 0.4.2

Monarch Money MCP server — an agentic budgeting companion (read + categorize only)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
//! Pure aggregation logic for the `budget_review` tool.
//!
//! Computes mid-month budget pacing per expense category: given how many days
//! have elapsed in the month, are we spending faster or slower than a straight-
//! line burn rate would predict?
//!
//! All arithmetic lives here, separated from I/O, so it can be unit-tested
//! without standing up a mock server. The tool handler in `tools.rs` fetches
//! data then delegates to [`compute_budget_review`].
//!
//! See ADR 0013 for the pace taxonomy, tolerance choice, and income/transfer
//! exclusion rationale.

use crate::client::{Budget, Transaction};
use crate::spending_report::transaction_spend_magnitude;
use serde::Serialize;
use std::collections::HashMap;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Tolerance (in percentage points) applied symmetrically around the straight-
/// line pace fraction. A category is `on_track` when:
///   pace_fraction - PACE_TOLERANCE_PP ≤ percent_spent ≤ pace_fraction + PACE_TOLERANCE_PP
///
/// 10 pp gives a ±10% band around the expected burn rate. For example, on day
/// 15 of 30 (50% pace), a category is `on_track` when 40%–60% of its budget
/// has been spent.
///
/// See ADR 0013 for the full rationale.
pub const PACE_TOLERANCE_PP: f64 = 10.0;

// ---------------------------------------------------------------------------
// Output types
// ---------------------------------------------------------------------------

/// Pace status for a single expense category relative to a straight-line burn.
#[derive(Debug, Serialize, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
pub enum PaceStatus {
    /// Spending is below the lower tolerance bound: spent% < pace - tolerance.
    Under,
    /// Spending is within the tolerance band around the pace fraction.
    OnTrack,
    /// Spending exceeds the upper tolerance bound but has not yet exceeded
    /// the full budget: spent% > pace + tolerance AND spent < budget.
    Over,
    /// Spending has met or exceeded the full budget amount.
    OverBudget,
}

/// Pacing result for one budgeted expense category.
#[derive(Debug, Serialize, PartialEq)]
pub struct CategoryPacing {
    /// Budget magnitude (positive, in the currency of the account).
    pub budget: f64,
    /// Actual spend so far this month (positive magnitude).
    pub spent: f64,
    /// `budget − spent` (may be negative when over budget).
    pub remaining: f64,
    /// `(spent / budget) * 100`, rounded to the nearest whole percent.
    /// `None` when budget is zero (avoids division-by-zero).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub percent_spent: Option<i64>,
    /// Pace signal relative to a straight-line burn rate.
    pub pace_status: PaceStatus,
}

/// Rollup counters across all paced categories.
#[derive(Debug, Serialize, PartialEq)]
pub struct PaceRollup {
    /// Sum of all expense budget magnitudes (positive).
    pub total_budget: f64,
    /// Sum of all expense spending so far (positive).
    pub total_spent: f64,
    /// Number of categories with `pace_status == OnTrack`.
    pub on_track_count: usize,
    /// Number of categories with `pace_status == Over`.
    pub over_count: usize,
    /// Number of categories with `pace_status == OverBudget`.
    pub over_budget_count: usize,
    /// Number of categories with `pace_status == Under`.
    pub under_count: usize,
}

/// The full budget review payload.
#[derive(Debug, Serialize, PartialEq)]
pub struct BudgetReview {
    /// Per-category pacing data (expense categories only).
    pub by_category: HashMap<String, CategoryPacing>,
    /// Rollup across all paced categories.
    pub rollup: PaceRollup,
}

// ---------------------------------------------------------------------------
// Pure computation — no I/O
// ---------------------------------------------------------------------------

/// Compute the budget review from already-fetched Monarch data.
///
/// `today_day_of_month` is the 1-based day within the current month (1..=31).
/// `days_in_month` is the total number of days in the current month (28..=31).
///
/// Both parameters are expected to come from the caller who obtains "today" via
/// `today_epoch_day()` (honoring `MONARCH_NOW`) and derives the YMD triple from
/// `epoch_days_to_ymd`. Keeping them as parameters preserves hermeticity.
pub fn compute_budget_review(
    budgets: &[Budget],
    transactions: &[Transaction],
    today_day_of_month: u32,
    days_in_month: u32,
) -> BudgetReview {
    let spending_by_category = aggregate_expense_spending(transactions);
    let by_category = build_category_pacings(
        budgets,
        &spending_by_category,
        today_day_of_month,
        days_in_month,
    );
    let rollup = compute_rollup(&by_category);
    BudgetReview {
        by_category,
        rollup,
    }
}

/// Sum expense magnitudes per category, reusing `transaction_spend_magnitude`
/// from `spending_report` for consistency.
///
/// Income and transfer categories contribute zero magnitude and are excluded
/// from the map entirely (see ADR 0013).
fn aggregate_expense_spending(transactions: &[Transaction]) -> HashMap<String, f64> {
    let mut totals: HashMap<String, f64> = HashMap::new();
    for txn in transactions {
        let magnitude = transaction_spend_magnitude(txn);
        // Only expense categories (and unknown-group fallback) appear in the map.
        // Income and transfer categories are excluded from pacing entirely.
        if matches!(txn.category.group_type.as_deref(), Some("expense") | None) {
            *totals.entry(txn.category.name.clone()).or_insert(0.0) += magnitude;
        }
    }
    totals
}

/// Build per-category pacing entries for all budgeted expense categories.
///
/// In production, `Budget.category.group_type` is always `None` because the
/// `GetJointPlanningData` join path does not fetch group_type (ADR 0013). The
/// exclusion of income/transfer categories is therefore handled via the
/// `aggregate_expense_spending` map: income and transfer transactions contribute
/// zero magnitude and never appear in that map, so their budget categories
/// naturally show `spent = 0` and pace as `Under`. We additionally respect an
/// explicit `Some("income") | Some("transfer")` group_type when it IS present
/// (e.g. in unit tests or future API changes) to keep behavior consistent.
///
/// A budgeted category with zero spending is included (spent = 0.0).
/// A category with spending but no budget is not included (no pacing reference).
fn build_category_pacings(
    budgets: &[Budget],
    spending: &HashMap<String, f64>,
    today_day_of_month: u32,
    days_in_month: u32,
) -> HashMap<String, CategoryPacing> {
    budgets
        .iter()
        .filter_map(|b| {
            // When group_type is explicitly income or transfer, exclude from pacing.
            // In production group_type is None for all budgets (the join path omits it).
            // For None, we include the category and let the spending map drive the result
            // (income/transfer transactions contribute zero via transaction_spend_magnitude,
            // so they naturally produce spent=0 and appear as Under or OnTrack).
            if matches!(
                b.category.group_type.as_deref(),
                Some("income") | Some("transfer")
            ) {
                return None;
            }

            let budget_mag = b.amount.abs();
            // Zero-budget categories: include with OverBudget if any spend,
            // otherwise Under (vacuously). This matches spending_report behavior.
            let spent = *spending.get(&b.category.name).unwrap_or(&0.0);
            let remaining = budget_mag - spent;
            let percent_spent = compute_percent_spent(spent, budget_mag);
            let pace_status = classify_pace(spent, budget_mag, today_day_of_month, days_in_month);

            Some((
                b.category.name.clone(),
                CategoryPacing {
                    budget: budget_mag,
                    spent,
                    remaining,
                    percent_spent,
                    pace_status,
                },
            ))
        })
        .collect()
}

/// Compute `(spent / budget_mag) * 100` rounded to the nearest whole percent.
///
/// Returns `None` when `budget_mag` is zero to avoid division-by-zero.
fn compute_percent_spent(spent: f64, budget_mag: f64) -> Option<i64> {
    if budget_mag == 0.0 {
        return None;
    }
    Some(((spent / budget_mag) * 100.0).round() as i64)
}

/// Classify the pace status for a category.
///
/// Taxonomy (in priority order):
/// 1. `OverBudget` — spent ≥ budget magnitude (regardless of time elapsed).
/// 2. `Over` — percent_spent > pace_fraction_pct + PACE_TOLERANCE_PP.
/// 3. `Under` — percent_spent < pace_fraction_pct - PACE_TOLERANCE_PP.
/// 4. `OnTrack` — within the tolerance band.
///
/// `pace_fraction_pct` = `(today_day_of_month / days_in_month) * 100`.
/// On day 1 of 30 this is 3.33%; on day 15 of 30 it is 50%; on day 30 it is 100%.
///
/// Note: The band comparison (Over/Under/OnTrack classification) is computed
/// using the RAW f64 ratio `(spent / budget) * 100`, NOT the rounded i64 value.
/// The rounded `percent_spent` field is used only for display. This ensures
/// classification matches ADR 0013, which defines the tolerance band on the
/// raw ratio.
///
/// For zero-budget categories `percent_spent` is `None`; the category is
/// classified `OverBudget` if any spend exists (matches `spending_report`
/// over-budget logic for zero-budget), `Under` if no spend.
fn classify_pace(
    spent: f64,
    budget_mag: f64,
    today_day_of_month: u32,
    days_in_month: u32,
) -> PaceStatus {
    // OverBudget: spending meets or exceeds the budget magnitude.
    if spent >= budget_mag && budget_mag > 0.0 {
        return PaceStatus::OverBudget;
    }
    // Zero-budget special case: any spend → OverBudget; no spend → Under.
    if budget_mag == 0.0 {
        return if spent > 0.0 {
            PaceStatus::OverBudget
        } else {
            PaceStatus::Under
        };
    }

    // Compute the raw ratio for band comparison (NOT the rounded display percent).
    // This ensures classification matches ADR 0013.
    let percent_f = (spent / budget_mag) * 100.0;

    let pace_pct = (today_day_of_month as f64 / days_in_month as f64) * 100.0;

    if percent_f > pace_pct + PACE_TOLERANCE_PP {
        PaceStatus::Over
    } else if percent_f < pace_pct - PACE_TOLERANCE_PP {
        PaceStatus::Under
    } else {
        PaceStatus::OnTrack
    }
}

/// Compute rollup totals and counts from per-category pacings.
fn compute_rollup(by_category: &HashMap<String, CategoryPacing>) -> PaceRollup {
    let mut total_budget = 0.0;
    let mut total_spent = 0.0;
    let mut on_track_count = 0;
    let mut over_count = 0;
    let mut over_budget_count = 0;
    let mut under_count = 0;

    for pacing in by_category.values() {
        total_budget += pacing.budget;
        total_spent += pacing.spent;
        match pacing.pace_status {
            PaceStatus::OnTrack => on_track_count += 1,
            PaceStatus::Over => over_count += 1,
            PaceStatus::OverBudget => over_budget_count += 1,
            PaceStatus::Under => under_count += 1,
        }
    }

    PaceRollup {
        total_budget,
        total_spent,
        on_track_count,
        over_count,
        over_budget_count,
        under_count,
    }
}

// ---------------------------------------------------------------------------
// Tests — TDD: RED first, then GREEN
//
// Fixtures use the REAL Monarch sign convention:
//   - expense outflows: NEGATIVE amounts (e.g. -360.0 for dining)
//   - income: POSITIVE amounts (e.g. +5000.0 for a paycheck)
//   - refunds landing in expense categories: POSITIVE
//   - expense budgets: NEGATIVE for outflows (e.g. -400.0 for Restaurants)
//     OR positive (historically both appear; we use .abs() consistently)
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::{Budget, Category, Transaction};

    /// Build an expense transaction (negative amount, group_type = "expense").
    fn make_expense_txn(merchant: &str, amount: f64, category: &str) -> Transaction {
        Transaction {
            id: format!("{merchant}-{amount}"),
            amount,
            date: "2026-05-15".to_string(),
            merchant_name: merchant.to_string(),
            category: Category {
                name: category.to_string(),
                group_type: Some("expense".into()),
            },
            tags: vec![],
            notes: String::new(),
            needs_review: false,
        }
    }

    /// Build an income transaction (positive amount, group_type = "income").
    fn make_income_txn(merchant: &str, amount: f64, category: &str) -> Transaction {
        Transaction {
            id: format!("{merchant}-{amount}"),
            amount,
            date: "2026-05-01".to_string(),
            merchant_name: merchant.to_string(),
            category: Category {
                name: category.to_string(),
                group_type: Some("income".into()),
            },
            tags: vec![],
            notes: String::new(),
            needs_review: false,
        }
    }

    /// Build a transfer transaction (group_type = "transfer").
    fn make_transfer_txn(merchant: &str, amount: f64, category: &str) -> Transaction {
        Transaction {
            id: format!("{merchant}-{amount}"),
            amount,
            date: "2026-05-05".to_string(),
            merchant_name: merchant.to_string(),
            category: Category {
                name: category.to_string(),
                group_type: Some("transfer".into()),
            },
            tags: vec![],
            notes: String::new(),
            needs_review: false,
        }
    }

    /// Build a budget with an expense group_type.
    fn make_expense_budget(category: &str, amount: f64) -> Budget {
        Budget {
            category: Category {
                name: category.to_string(),
                group_type: Some("expense".into()),
            },
            amount,
        }
    }

    /// Build an income budget.
    fn make_income_budget(category: &str, amount: f64) -> Budget {
        Budget {
            category: Category {
                name: category.to_string(),
                group_type: Some("income".into()),
            },
            amount,
        }
    }

    /// Build a transfer budget.
    fn make_transfer_budget(category: &str, amount: f64) -> Budget {
        Budget {
            category: Category {
                name: category.to_string(),
                group_type: Some("transfer".into()),
            },
            amount,
        }
    }

    // -----------------------------------------------------------------------
    // Core pace classification — day 15 of 30 (50% pace)
    // -----------------------------------------------------------------------

    #[test]
    fn category_spent_faster_than_month_elapsed_is_over() {
        // Day 15 of 30 = 50% pace. Restaurants budget 400, spent 360 = 90%.
        // 90% > 50% + 10% tolerance → Over.
        let budgets = vec![make_expense_budget("Restaurants", -400.0)];
        let txns = vec![make_expense_txn("Restaurant Co", -360.0, "Restaurants")];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Restaurants").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::Over, "{cat:?}");
        assert_eq!(cat.remaining, 40.0);
        assert_eq!(cat.spent, 360.0);
        assert_eq!(cat.budget, 400.0);
    }

    #[test]
    fn category_tracking_with_calendar_is_on_track() {
        // Day 15 of 30 = 50% pace. Groceries budget 600, spent 290 ≈ 48%.
        // 48% is within [40%, 60%] tolerance band → OnTrack.
        let budgets = vec![make_expense_budget("Groceries", -600.0)];
        let txns = vec![make_expense_txn("Grocery Store", -290.0, "Groceries")];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Groceries").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::OnTrack, "{cat:?}");
    }

    #[test]
    fn category_already_over_budget_is_flagged_over_budget() {
        // Phone budget 200, spent 210 → spent ≥ budget → OverBudget.
        let budgets = vec![make_expense_budget("Phone", -200.0)];
        let txns = vec![make_expense_txn("Phone Co", -210.0, "Phone")];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Phone").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::OverBudget, "{cat:?}");
        assert!(
            (cat.remaining - (-10.0)).abs() < 0.001,
            "remaining should be -10, got {}",
            cat.remaining
        );
    }

    #[test]
    fn category_exactly_at_budget_is_over_budget() {
        // Spent exactly equals budget → OverBudget (spent >= budget).
        let budgets = vec![make_expense_budget("Utilities", -300.0)];
        let txns = vec![make_expense_txn("Utility Co", -300.0, "Utilities")];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Utilities").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::OverBudget, "{cat:?}");
    }

    // -----------------------------------------------------------------------
    // Tolerance edges
    // -----------------------------------------------------------------------

    #[test]
    fn exactly_at_upper_tolerance_edge_is_on_track() {
        // Day 15 of 30 = 50% pace. Upper edge: 50% + 10% = 60%.
        // 60% spent → still OnTrack (boundary is exclusive: > 60% → Over).
        let budgets = vec![make_expense_budget("Dining", -100.0)];
        let txns = vec![make_expense_txn("Diner", -60.0, "Dining")]; // 60%
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Dining").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::OnTrack, "{cat:?}");
    }

    #[test]
    fn one_cent_above_upper_tolerance_edge_is_over() {
        // Day 15 of 30 = 50% pace. 61% > 60% → Over.
        let budgets = vec![make_expense_budget("Dining", -100.0)];
        let txns = vec![make_expense_txn("Diner", -61.0, "Dining")]; // 61%
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Dining").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::Over, "{cat:?}");
    }

    #[test]
    fn exactly_at_lower_tolerance_edge_is_on_track() {
        // Day 15 of 30 = 50% pace. Lower edge: 50% - 10% = 40%.
        // 40% spent → OnTrack (boundary is exclusive: < 40% → Under).
        let budgets = vec![make_expense_budget("Dining", -100.0)];
        let txns = vec![make_expense_txn("Diner", -40.0, "Dining")]; // 40%
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Dining").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::OnTrack, "{cat:?}");
    }

    #[test]
    fn one_cent_below_lower_tolerance_edge_is_under() {
        // Day 15 of 30 = 50% pace. 39% < 40% → Under.
        let budgets = vec![make_expense_budget("Dining", -100.0)];
        let txns = vec![make_expense_txn("Diner", -39.0, "Dining")]; // 39%
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Dining").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::Under, "{cat:?}");
    }

    // -----------------------------------------------------------------------
    // Day boundary tests (first, last, mid)
    // -----------------------------------------------------------------------

    #[test]
    fn day_1_of_30_pace_is_3_percent() {
        // Day 1 of 30 ≈ 3.33% pace. Lower edge ≈ 3.33% - 10% = -6.67%.
        // Any spend > 0% puts us above the lower bound.
        // Upper edge ≈ 3.33% + 10% = 13.33%.
        // 10% spent → on track (between ~-6.67% and ~13.33%).
        let budgets = vec![make_expense_budget("Groceries", -200.0)];
        let txns = vec![make_expense_txn("Grocery Store", -20.0, "Groceries")]; // 10%
        let result = compute_budget_review(&budgets, &txns, 1, 30);
        let cat = result.by_category.get("Groceries").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::OnTrack, "{cat:?}");
    }

    #[test]
    fn day_1_of_30_with_no_spending_is_on_track() {
        // On day 1 with no spending, lower bound is negative, so 0% is on track.
        let budgets = vec![make_expense_budget("Groceries", -200.0)];
        let result = compute_budget_review(&budgets, &[], 1, 30);
        let cat = result.by_category.get("Groceries").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::OnTrack, "{cat:?}");
    }

    #[test]
    fn last_day_of_30_with_full_spend_is_on_track() {
        // Day 30 of 30 = 100% pace. 100% spent → at upper tolerance edge.
        // 100% is not > 110% → OnTrack.
        let budgets = vec![make_expense_budget("Rent", -1500.0)];
        let txns = vec![make_expense_txn("Landlord", -1500.0, "Rent")];
        // spent == budget → OverBudget (spent >= budget), not OnTrack.
        // Exact budget hit IS over_budget per the priority rule.
        let result = compute_budget_review(&budgets, &txns, 30, 30);
        let cat = result.by_category.get("Rent").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::OverBudget, "{cat:?}");
    }

    #[test]
    fn last_day_of_30_with_99_percent_spend_is_on_track() {
        // Day 30 of 30 = 100% pace. 99% spent → 99% is in [90%, 110%] → OnTrack.
        let budgets = vec![make_expense_budget("Rent", -1500.0)];
        let txns = vec![make_expense_txn("Landlord", -1485.0, "Rent")]; // 99%
        let result = compute_budget_review(&budgets, &txns, 30, 30);
        let cat = result.by_category.get("Rent").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::OnTrack, "{cat:?}");
    }

    // -----------------------------------------------------------------------
    // Income and transfer exclusion
    // -----------------------------------------------------------------------

    #[test]
    fn income_budget_is_excluded_from_pacing() {
        let budgets = vec![
            make_income_budget("Paychecks", 5000.0),
            make_expense_budget("Groceries", -600.0),
        ];
        let txns = vec![
            make_income_txn("Employer", 5000.0, "Paychecks"),
            make_expense_txn("Grocery Store", -300.0, "Groceries"),
        ];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        assert!(
            !result.by_category.contains_key("Paychecks"),
            "income category must not appear in pacing: {:?}",
            result.by_category.keys().collect::<Vec<_>>()
        );
        assert!(result.by_category.contains_key("Groceries"));
    }

    #[test]
    fn transfer_budget_is_excluded_from_pacing() {
        let budgets = vec![
            make_transfer_budget("Credit Card Payment", -2000.0),
            make_expense_budget("Dining", -400.0),
        ];
        let txns = vec![
            make_transfer_txn("Chase CC", -2000.0, "Credit Card Payment"),
            make_expense_txn("Diner", -200.0, "Dining"),
        ];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        assert!(
            !result.by_category.contains_key("Credit Card Payment"),
            "transfer category must not appear in pacing"
        );
        assert!(result.by_category.contains_key("Dining"));
    }

    #[test]
    fn income_and_transfer_transactions_do_not_count_as_spend() {
        // Only the expense transaction contributes; income and transfer are excluded.
        let budgets = vec![make_expense_budget("Groceries", -600.0)];
        let txns = vec![
            make_income_txn("Employer", 5000.0, "Paychecks"),
            make_transfer_txn("Chase CC", -2000.0, "Credit Card Payment"),
            make_expense_txn("Grocery Store", -300.0, "Groceries"),
        ];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Groceries").unwrap();
        assert_eq!(
            cat.spent, 300.0,
            "spent must be 300, not inflated by income/transfer"
        );
    }

    // -----------------------------------------------------------------------
    // Category with spend but no budget
    // -----------------------------------------------------------------------

    #[test]
    fn category_with_spend_but_no_budget_is_not_in_pacing() {
        // Unbudgeted categories are invisible in budget_review (no pacing reference).
        let budgets = vec![make_expense_budget("Groceries", -600.0)];
        let txns = vec![
            make_expense_txn("Airline", -500.0, "Travel"),
            make_expense_txn("Grocery Store", -300.0, "Groceries"),
        ];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        assert!(
            !result.by_category.contains_key("Travel"),
            "unbudgeted Travel must not appear in pacing"
        );
        assert!(result.by_category.contains_key("Groceries"));
    }

    // -----------------------------------------------------------------------
    // Budgeted category with no spend
    // -----------------------------------------------------------------------

    #[test]
    fn budgeted_category_with_zero_spend_is_included_and_on_track_early() {
        // On day 1 of 30, zero spend with lower bound negative → OnTrack.
        let budgets = vec![make_expense_budget("Gym", -50.0)];
        let result = compute_budget_review(&budgets, &[], 1, 30);
        let cat = result.by_category.get("Gym").unwrap();
        assert_eq!(cat.spent, 0.0);
        assert_eq!(cat.budget, 50.0);
        assert_eq!(cat.remaining, 50.0);
    }

    #[test]
    fn budgeted_category_with_zero_spend_mid_month_is_under() {
        // On day 20 of 30 ≈ 67% pace. Lower edge = 57%. 0% < 57% → Under.
        let budgets = vec![make_expense_budget("Gym", -50.0)];
        let result = compute_budget_review(&budgets, &[], 20, 30);
        let cat = result.by_category.get("Gym").unwrap();
        assert_eq!(cat.pace_status, PaceStatus::Under, "{cat:?}");
    }

    // -----------------------------------------------------------------------
    // MONARCH_NOW-driven pace fraction (day derived from today_day_of_month)
    // -----------------------------------------------------------------------

    #[test]
    fn pace_fraction_changes_with_day_of_month() {
        // Same category and spend, different day: on day 10 vs day 20.
        // Budget 100, spent 60 = 60%.
        // Day 10 of 30: pace = 33%. Upper edge = 43%. 60% > 43% → Over.
        // Day 20 of 30: pace = 67%. Lower edge = 57%. 60% is in [57%, 77%] → OnTrack.
        let budgets = vec![make_expense_budget("Misc", -100.0)];
        let txns = vec![make_expense_txn("Misc Co", -60.0, "Misc")];

        let result_day10 = compute_budget_review(&budgets, &txns, 10, 30);
        let cat_day10 = result_day10.by_category.get("Misc").unwrap();
        assert_eq!(
            cat_day10.pace_status,
            PaceStatus::Over,
            "day 10: {cat_day10:?}"
        );

        let result_day20 = compute_budget_review(&budgets, &txns, 20, 30);
        let cat_day20 = result_day20.by_category.get("Misc").unwrap();
        assert_eq!(
            cat_day20.pace_status,
            PaceStatus::OnTrack,
            "day 20: {cat_day20:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Percent_spent field
    // -----------------------------------------------------------------------

    #[test]
    fn percent_spent_rounds_correctly() {
        // 360 / 400 = 90%
        let budgets = vec![make_expense_budget("Restaurants", -400.0)];
        let txns = vec![make_expense_txn("Restaurant Co", -360.0, "Restaurants")];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Restaurants").unwrap();
        assert_eq!(cat.percent_spent, Some(90));
    }

    #[test]
    fn percent_spent_is_none_for_zero_budget() {
        let budgets = vec![make_expense_budget("Misc", 0.0)];
        let result = compute_budget_review(&budgets, &[], 15, 30);
        let cat = result.by_category.get("Misc").unwrap();
        assert_eq!(cat.percent_spent, None);
    }

    #[test]
    fn zero_budget_with_no_spend_is_under_and_finite() {
        // Category with zero budget and no spending should pace as Under (spent 0 < budget 0).
        // See classify_pace: budget_mag == 0.0 && spent == 0.0 → Under.
        let budgets = vec![make_expense_budget("Misc", 0.0)];
        let result = compute_budget_review(&budgets, &[], 15, 30);
        let cat = result.by_category.get("Misc").unwrap();
        assert_eq!(
            cat.pace_status,
            PaceStatus::Under,
            "zero budget with no spend should be Under: {cat:?}"
        );
        assert_eq!(cat.remaining, 0.0);
        assert!(cat.remaining.is_finite(), "remaining should be finite");
    }

    #[test]
    fn zero_budget_with_spend_is_over_budget_and_finite() {
        // Category with zero budget but any spending (e.g., -25.0 expense) should be OverBudget.
        // See classify_pace: budget_mag == 0.0 && spent > 0.0 → OverBudget.
        let budgets = vec![make_expense_budget("Misc", 0.0)];
        let txns = vec![make_expense_txn("Misc Co", -25.0, "Misc")];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Misc").unwrap();
        assert_eq!(
            cat.pace_status,
            PaceStatus::OverBudget,
            "zero budget with any spend should be OverBudget: {cat:?}"
        );
        assert_eq!(
            cat.percent_spent, None,
            "percent_spent must be None for zero budget"
        );
        assert_eq!(cat.spent, 25.0);
        assert_eq!(cat.remaining, -25.0);
        assert!(cat.remaining.is_finite(), "remaining should be finite");
    }

    #[test]
    fn zero_budget_with_spend_counts_as_over_budget_in_rollup() {
        // A zero-budget category with spending should increment over_budget_count in rollup.
        let budgets = vec![make_expense_budget("Misc", 0.0)];
        let txns = vec![make_expense_txn("Misc Co", -25.0, "Misc")];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let rollup = &result.rollup;
        assert_eq!(
            rollup.over_budget_count, 1,
            "zero budget with spend should count in over_budget_count: {rollup:?}"
        );
        assert_eq!(
            rollup.on_track_count, 0,
            "on_track_count should be 0: {rollup:?}"
        );
        assert_eq!(rollup.over_count, 0, "over_count should be 0: {rollup:?}");
        assert_eq!(rollup.under_count, 0, "under_count should be 0: {rollup:?}");
    }

    // -----------------------------------------------------------------------
    // Rollup counts
    // -----------------------------------------------------------------------

    #[test]
    fn rollup_counts_are_correct() {
        // Day 15 of 30 (50% pace, tolerance ±10 → on track: [40%, 60%]).
        // Restaurants: 360/400 = 90% → Over
        // Groceries:   290/600 = 48% → OnTrack
        // Phone:       210/200 → OverBudget
        // Gym:         0/50 = 0% → Under
        let budgets = vec![
            make_expense_budget("Restaurants", -400.0),
            make_expense_budget("Groceries", -600.0),
            make_expense_budget("Phone", -200.0),
            make_expense_budget("Gym", -50.0),
        ];
        let txns = vec![
            make_expense_txn("Restaurant Co", -360.0, "Restaurants"),
            make_expense_txn("Grocery Store", -290.0, "Groceries"),
            make_expense_txn("Phone Co", -210.0, "Phone"),
        ];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let rollup = &result.rollup;
        assert_eq!(rollup.over_count, 1, "over: {:?}", rollup);
        assert_eq!(rollup.on_track_count, 1, "on_track: {:?}", rollup);
        assert_eq!(rollup.over_budget_count, 1, "over_budget: {:?}", rollup);
        assert_eq!(rollup.under_count, 1, "under: {:?}", rollup);
        assert_eq!(rollup.total_budget, 1250.0, "total_budget: {:?}", rollup);
        assert!(
            (rollup.total_spent - 860.0).abs() < 0.001,
            "total_spent: {:?}",
            rollup
        );
    }

    // -----------------------------------------------------------------------
    // Negative budget (loan-repayment style) — .abs() ensures consistency.
    // -----------------------------------------------------------------------

    #[test]
    fn negative_budget_magnitude_is_used_correctly() {
        // Budget -1280 (loan). Spent 1000. Magnitude 1000/1280 = 78%.
        // Day 15/30 = 50% pace. 78% > 60% upper edge → Over.
        let budgets = vec![make_expense_budget("Loan Repayment", -1280.0)];
        let txns = vec![make_expense_txn("Loan Co", -1000.0, "Loan Repayment")];
        let result = compute_budget_review(&budgets, &txns, 15, 30);
        let cat = result.by_category.get("Loan Repayment").unwrap();
        assert_eq!(cat.budget, 1280.0, "budget should be the magnitude");
        assert_eq!(cat.percent_spent, Some(78));
        assert_eq!(cat.pace_status, PaceStatus::Over, "{cat:?}");
    }
}