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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
//! Pure computation for the `progress_vs_goals` tool.
//!
//! Measures actual finances against the household's stored goals and classifies
//! each as `on track`, `drifting`, or `off` using a single shared banding
//! function. Only goals the household has actually set are reported.

use crate::client::{Account, Cashflow};
use crate::goals::Goals;
use crate::net_worth_trend::AccountTypeSnapshot;
use serde::Serialize;

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

/// The result payload returned as JSON by `progress_vs_goals`.
/// Only fields for goals that have been set are present.
/// `guidance` is set when no goals of any kind are configured.
/// This prevents the payload from ever serializing to a silent empty object `{}`.
#[derive(Debug, Serialize, PartialEq)]
pub struct GoalsProgress {
    /// Savings-rate goal progress — present only when the goal is set.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub savings_rate: Option<GoalStatus>,

    /// Emergency-fund runway goal progress — present only when the goal is set.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub emergency_fund: Option<GoalStatus>,

    /// Debt-payoff progress — present only when the goal is set.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub debt_payoff: Option<DebtPayoffStatus>,

    /// Set when no goals of any kind are configured — explains next steps.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub guidance: Option<String>,
}

/// Classification of a single goal.
#[derive(Debug, Serialize, PartialEq)]
pub struct GoalStatus {
    /// One of `"on track"`, `"drifting"`, or `"off"`.
    pub status: String,
}

/// Enriched debt-payoff progress combining plan-based and pace-based signals.
#[derive(Debug, Serialize, PartialEq)]
pub struct DebtPayoffStatus {
    /// Overall worst-of status: `"on track"`, `"drifting"`, `"off"`, or `"unknown"`.
    pub status: String,
    /// Total debt owed across all credit and loan accounts (positive magnitude).
    pub total_debt: f64,
    /// Whole months from today to the target payoff date. May be ≤ 0 if past.
    /// `None` when `target_date` is malformed (not parseable as `YYYY-MM…`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub months_to_target: Option<i64>,
    /// Monthly payment required to clear total_debt by the target date.
    /// None when months_to_target ≤ 0 or total_debt == 0.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required_monthly: Option<f64>,
    /// The household's planned monthly payment from the goal config.
    /// None when monthly_payment is not set.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub planned_monthly: Option<f64>,
    /// Plan-based signal: is the planned payment sufficient?
    /// None when planned_monthly or required_monthly is unavailable.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub on_schedule: Option<String>,
    /// Actual debt reduction last month (prior_debt − current_debt).
    /// None when no prior-month snapshot is available.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actual_paydown: Option<f64>,
    /// Pace-based signal: did the household actually pay down enough?
    /// None when actual_paydown or planned_monthly is unavailable.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub on_pace: Option<String>,
}

/// Account types that represent debt liabilities.
const DEBT_TYPES: &[&str] = &["credit", "loan"];

/// Compute whole months from (today_year, today_month) to the leading YYYY-MM of target_date.
///
/// Returns `Some(months)` when `target_date` has a parseable `YYYY-MM` prefix with a valid
/// year and a month in `1..=12`. Returns `None` for any malformed input (empty string,
/// year-only, non-numeric parts, month out of range).
///
/// Returns a negative value when the target date is in the past.
/// Only the year and month portions of target_date are used; the day is ignored.
pub fn months_to_target_from_date(
    target_date: &str,
    today_year: i64,
    today_month: u32,
) -> Option<i64> {
    let mut parts = target_date.splitn(3, '-');
    let target_year: i64 = parts.next()?.parse().ok()?;
    let target_month: i64 = parts.next()?.parse().ok()?;
    if !(1..=12).contains(&target_month) {
        return None;
    }
    Some((target_year - today_year) * 12 + target_month - today_month as i64)
}

// ---------------------------------------------------------------------------
// Banding — shared classifier for all goal types
// ---------------------------------------------------------------------------

/// Classify `actual` against `goal` using the standard three-band rule:
///
/// - `"on track"`: actual ≥ goal
/// - `"off"`:      actual < goal / 2
/// - `"drifting"`: anything in between (goal/2 ≤ actual < goal)
pub fn classify_goal(actual: f64, goal: f64) -> &'static str {
    if actual >= goal {
        "on track"
    } else if actual < goal / 2.0 {
        "off"
    } else {
        "drifting"
    }
}

// ---------------------------------------------------------------------------
// Actuals computation helpers
// ---------------------------------------------------------------------------

/// Rank status strings for worst-of blending: higher = worse.
fn status_rank(status: &str) -> u8 {
    match status {
        "on track" => 0,
        "drifting" => 1,
        _ => 2, // "off" and anything unexpected
    }
}

/// Compute enriched debt-payoff progress from all inputs.
///
/// `today_year`/`today_month` come from the caller's date seam so this
/// function stays hermetic (no clock reads). `prior_month` and
/// `current_month` are `"YYYY-MM"` strings used to look up snapshot rows.
#[allow(clippy::too_many_arguments)]
pub fn compute_debt_payoff(
    goal: &crate::goals::DebtPayoffGoal,
    accounts: &[Account],
    snapshots: &[AccountTypeSnapshot],
    prior_month: &str,
    current_month: &str,
    today_year: i64,
    today_month: u32,
) -> DebtPayoffStatus {
    let total_debt = total_debt_from_accounts(accounts);
    let months_to_target = months_to_target_from_date(&goal.target_date, today_year, today_month);
    let planned_monthly = goal.monthly_payment;

    // Signal 1 — on schedule?
    let required_monthly = if total_debt == 0.0 {
        Some(0.0)
    } else {
        match months_to_target {
            Some(m) if m > 0 => Some(total_debt / m as f64),
            _ => None,
        }
    };

    let on_schedule: Option<String> = match (planned_monthly, required_monthly) {
        (Some(planned), Some(required)) => Some(classify_goal(planned, required).to_string()),
        _ => None,
    };

    // Signal 2 — on pace?
    let actual_paydown = actual_paydown_from_snapshots(snapshots, prior_month, current_month);

    let on_pace: Option<String> = match (actual_paydown, planned_monthly) {
        (Some(paydown), Some(planned)) if paydown > 0.0 => {
            Some(classify_goal(paydown, planned).to_string())
        }
        (Some(_), Some(_)) => Some("off".to_string()), // debt grew or stayed flat
        _ => None,
    };

    // Overall status: worst of computable signals; "unknown" when none.
    // A malformed target_date (months_to_target == None) does NOT force "off" —
    // it falls through to the sub-status blend, yielding "unknown" when no other
    // signal is available.
    let status = if total_debt == 0.0 {
        "on track".to_string()
    } else if matches!(months_to_target, Some(m) if m <= 0) && total_debt > 0.0 {
        // Valid target date has passed with debt remaining — genuinely off
        "off".to_string()
    } else {
        let sub_statuses: Vec<&str> = [on_schedule.as_deref(), on_pace.as_deref()]
            .iter()
            .flatten()
            .copied()
            .collect();
        if sub_statuses.is_empty() {
            "unknown".to_string()
        } else {
            sub_statuses
                .iter()
                .max_by_key(|s| status_rank(s))
                .copied()
                .unwrap_or("unknown")
                .to_string()
        }
    };

    DebtPayoffStatus {
        status,
        total_debt,
        months_to_target,
        required_monthly,
        planned_monthly,
        on_schedule,
        actual_paydown,
        on_pace,
    }
}

/// Compute the savings rate as a percentage from cashflow data.
/// savings_rate = (income - spending) / income * 100
/// Returns 0.0 when income is zero to avoid division by zero.
pub fn actual_savings_rate_pct(cashflow: &Cashflow) -> f64 {
    if cashflow.income == 0.0 {
        return 0.0;
    }
    (cashflow.income - cashflow.spending) / cashflow.income * 100.0
}

/// Compute actual debt paydown from monthly snapshots.
///
/// Sums debt-type balances for prior_month and current_month, converts each to
/// owed magnitude, and returns `prior_owed − current_owed`. Returns `None` when
/// no prior-month snapshot exists (first data point — can't compute a delta).
/// A negative result means debt grew.
pub fn actual_paydown_from_snapshots(
    snapshots: &[AccountTypeSnapshot],
    prior_month: &str,
    current_month: &str,
) -> Option<f64> {
    let prior_owed: f64 = snapshots
        .iter()
        .filter(|s| s.month == prior_month && DEBT_TYPES.contains(&s.account_type.as_str()))
        .map(|s| (-s.balance).max(0.0))
        .sum();

    let has_prior = snapshots
        .iter()
        .any(|s| s.month == prior_month && DEBT_TYPES.contains(&s.account_type.as_str()));

    if !has_prior {
        return None;
    }

    let current_owed: f64 = snapshots
        .iter()
        .filter(|s| s.month == current_month && DEBT_TYPES.contains(&s.account_type.as_str()))
        .map(|s| (-s.balance).max(0.0))
        .sum();

    Some(prior_owed - current_owed)
}

/// Sum the owed amount across all debt-type accounts.
///
/// Monarch stores liability balances as negative numbers. Owed per account =
/// `(-current_balance).max(0.0)` so a positive balance (overpaid credit card)
/// contributes 0, not a negative debt.
pub fn total_debt_from_accounts(accounts: &[Account]) -> f64 {
    accounts
        .iter()
        .filter(|a| DEBT_TYPES.contains(&a.account_type.name.as_str()))
        .map(|a| (-a.current_balance).max(0.0))
        .sum()
}

/// Compute months of expenses covered by liquid cash-equivalent accounts.
///
/// `reserves_months = total_liquid_balance / monthly_spending`
/// Returns 0.0 when monthly spending is zero.
///
/// Liquid cash-equivalent types (included):
///   - `"savings"`      — traditional savings / HYSA
///   - `"checking"`     — checking / demand-deposit accounts
///   - `"depository"`   — Monarch's catch-all for bank deposit accounts
///   - `"money_market"` — money-market accounts / funds
///
/// Excluded: `"brokerage"`, `"investment"`, `"retirement"`, `"credit"`, `"loan"`,
/// and any other type not in the above list.  Investment/retirement balances are
/// not immediately liquid; liability balances would distort the runway figure.
///
/// Semantics rationale: an emergency fund is the cash you can tap in 1–3 business
/// days without selling assets or incurring penalties.  Brokerage accounts require
/// a T+2 settlement cycle and market-risk exposure; retirement accounts carry
/// withdrawal penalties.  Only deposit-type accounts qualify.
pub fn actual_reserve_months(accounts: &[Account], cashflow: &Cashflow) -> f64 {
    if cashflow.spending == 0.0 {
        return 0.0;
    }
    const LIQUID_TYPES: &[&str] = &["savings", "checking", "depository", "money_market"];
    let liquid_balance: f64 = accounts
        .iter()
        .filter(|a| LIQUID_TYPES.contains(&a.account_type.name.as_str()))
        .map(|a| a.current_balance)
        .sum();
    liquid_balance / cashflow.spending
}

// ---------------------------------------------------------------------------
// Pure aggregation — no I/O
// ---------------------------------------------------------------------------

/// Compute goal progress from already-fetched Monarch data and loaded goals.
///
/// `snapshots` are monthly per-type debt history; pass an empty slice when no
/// debt-payoff goal is configured to avoid an unnecessary fetch.
/// `prior_month` / `current_month` are `"YYYY-MM"` strings for snapshot lookup.
/// `today` is `(year, month)` from the caller's date seam (honours `MONARCH_NOW`).
///
/// When no goals have been configured, returns a `guidance` message directing
/// the household to create a goals.toml — consistent with the soft re-auth
/// pattern (a successful MCP result whose body explains next steps).
pub fn compute_progress(
    goals: &Goals,
    accounts: &[Account],
    cashflow: &Cashflow,
    snapshots: &[AccountTypeSnapshot],
    prior_month: &str,
    current_month: &str,
    today: (i64, u32),
) -> GoalsProgress {
    let savings_rate = goals.savings_rate.as_ref().map(|g| {
        let actual = actual_savings_rate_pct(cashflow);
        GoalStatus {
            status: classify_goal(actual, g.target_percent).to_string(),
        }
    });

    let emergency_fund = goals.emergency_fund.as_ref().map(|g| {
        let actual = actual_reserve_months(accounts, cashflow);
        GoalStatus {
            status: classify_goal(actual, g.target_months).to_string(),
        }
    });

    let debt_payoff = goals.debt_payoff.as_ref().map(|g| {
        compute_debt_payoff(
            g,
            accounts,
            snapshots,
            prior_month,
            current_month,
            today.0,
            today.1,
        )
    });

    // guidance fires only when no goals of any kind are configured, so the
    // payload is never a silent empty object.
    let has_no_goals = savings_rate.is_none() && emergency_fund.is_none() && debt_payoff.is_none();
    let guidance = has_no_goals.then(|| {
        "No goals configured yet. Add a goals.toml file and set \
         MONARCH_GOALS_FILE to its path to start tracking progress."
            .to_string()
    });

    GoalsProgress {
        savings_rate,
        emergency_fund,
        debt_payoff,
        guidance,
    }
}

// ---------------------------------------------------------------------------
// Tests — TDD: RED → GREEN → TRIANGULATE → GREEN → REFACTOR
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::{AccountType, Cashflow};
    use crate::goals::{EmergencyFundGoal, Goals, SavingsRateGoal};

    // -----------------------------------------------------------------------
    // Classifier banding — boundary tests
    // -----------------------------------------------------------------------

    // 9a RED: exactly at goal → on track
    #[test]
    fn classify_at_goal_is_on_track() {
        assert_eq!(classify_goal(20.0, 20.0), "on track");
    }

    // 9a RED: above goal → on track
    #[test]
    fn classify_above_goal_is_on_track() {
        assert_eq!(classify_goal(25.0, 20.0), "on track");
    }

    // 9c TRIANGULATE: exactly at half → drifting (not off)
    #[test]
    fn classify_at_half_goal_is_drifting() {
        assert_eq!(classify_goal(10.0, 20.0), "drifting");
    }

    // 9c TRIANGULATE: just below half → off
    #[test]
    fn classify_just_below_half_is_off() {
        // 9.9 < 20/2=10 → off
        assert_eq!(classify_goal(9.9, 20.0), "off");
    }

    // 9c TRIANGULATE: midrange → drifting
    #[test]
    fn classify_midrange_is_drifting() {
        assert_eq!(classify_goal(17.0, 20.0), "drifting");
    }

    // 9c TRIANGULATE: far below half → off
    #[test]
    fn classify_far_below_half_is_off() {
        assert_eq!(classify_goal(6.0, 20.0), "off");
    }

    // 9c TRIANGULATE: zero → off (unless goal is also zero)
    #[test]
    fn classify_zero_actual_is_off() {
        assert_eq!(classify_goal(0.0, 20.0), "off");
    }

    // 9c TRIANGULATE: emergency-fund banding (6-month target)
    #[test]
    fn classify_emergency_fund_at_goal_is_on_track() {
        assert_eq!(classify_goal(6.0, 6.0), "on track");
    }

    #[test]
    fn classify_emergency_fund_above_goal_is_on_track() {
        assert_eq!(classify_goal(7.0, 6.0), "on track");
    }

    #[test]
    fn classify_emergency_fund_drifting() {
        assert_eq!(classify_goal(4.0, 6.0), "drifting");
    }

    #[test]
    fn classify_emergency_fund_off() {
        assert_eq!(classify_goal(2.0, 6.0), "off");
    }

    // -----------------------------------------------------------------------
    // Savings-rate computation
    // -----------------------------------------------------------------------

    fn cashflow(income: f64, spending: f64) -> Cashflow {
        Cashflow {
            income,
            spending,
            prior_month_spending: 0.0,
        }
    }

    #[test]
    fn savings_rate_pct_is_correct() {
        // (10000 - 7500) / 10000 * 100 = 25%
        assert_eq!(actual_savings_rate_pct(&cashflow(10000.0, 7500.0)), 25.0);
    }

    #[test]
    fn savings_rate_pct_zero_income_returns_zero() {
        assert_eq!(actual_savings_rate_pct(&cashflow(0.0, 0.0)), 0.0);
    }

    #[test]
    fn savings_rate_pct_full_spending_returns_zero() {
        // All income spent → 0% savings
        assert_eq!(actual_savings_rate_pct(&cashflow(10000.0, 10000.0)), 0.0);
    }

    // -----------------------------------------------------------------------
    // Reserve months computation
    // -----------------------------------------------------------------------

    fn savings_account(balance: f64) -> Account {
        Account {
            id: "s1".to_string(),
            display_name: "Emergency Fund".to_string(),
            current_balance: balance,
            balance_was_null: false,
            account_type: AccountType {
                name: "savings".to_string(),
            },
            subtype: None,
            is_hidden: false,
        }
    }

    fn checking_account(balance: f64) -> Account {
        Account {
            id: "c1".to_string(),
            display_name: "Checking".to_string(),
            current_balance: balance,
            balance_was_null: false,
            account_type: AccountType {
                name: "checking".to_string(),
            },
            subtype: None,
            is_hidden: false,
        }
    }

    fn money_market_account(balance: f64) -> Account {
        Account {
            id: "mm1".to_string(),
            display_name: "HYSA".to_string(),
            current_balance: balance,
            balance_was_null: false,
            account_type: AccountType {
                name: "money_market".to_string(),
            },
            subtype: None,
            is_hidden: false,
        }
    }

    fn brokerage_account(balance: f64) -> Account {
        Account {
            id: "b1".to_string(),
            display_name: "Brokerage".to_string(),
            current_balance: balance,
            balance_was_null: false,
            account_type: AccountType {
                name: "brokerage".to_string(),
            },
            subtype: None,
            is_hidden: false,
        }
    }

    fn retirement_account(balance: f64) -> Account {
        Account {
            id: "r1".to_string(),
            display_name: "401k".to_string(),
            current_balance: balance,
            balance_was_null: false,
            account_type: AccountType {
                name: "retirement".to_string(),
            },
            subtype: None,
            is_hidden: false,
        }
    }

    // 9a RED: money-market should count as liquid reserves
    #[test]
    fn reserve_months_counts_money_market() {
        // 20000 money-market / 5000 spending = 4 months
        let accounts = vec![money_market_account(20000.0)];
        let cf = cashflow(6000.0, 5000.0);
        assert_eq!(actual_reserve_months(&accounts, &cf), 4.0);
    }

    // 9a RED: checking should count as liquid reserves
    #[test]
    fn reserve_months_counts_checking() {
        // 10000 checking / 5000 spending = 2 months
        let accounts = vec![checking_account(10000.0)];
        let cf = cashflow(6000.0, 5000.0);
        assert_eq!(actual_reserve_months(&accounts, &cf), 2.0);
    }

    // 9a RED: savings + checking + money_market all combined
    #[test]
    fn reserve_months_combines_all_liquid_types() {
        // 30000 + 10000 + 20000 = 60000 / 5000 = 12 months
        let accounts = vec![
            savings_account(30000.0),
            checking_account(10000.0),
            money_market_account(20000.0),
        ];
        let cf = cashflow(6000.0, 5000.0);
        assert_eq!(actual_reserve_months(&accounts, &cf), 12.0);
    }

    // 9c TRIANGULATE: brokerage must NOT count toward reserves
    #[test]
    fn reserve_months_excludes_brokerage() {
        let accounts = vec![savings_account(30000.0), brokerage_account(100000.0)];
        let cf = cashflow(6000.0, 5000.0);
        assert_eq!(actual_reserve_months(&accounts, &cf), 6.0);
    }

    // 9c TRIANGULATE: retirement must NOT count toward reserves
    #[test]
    fn reserve_months_excludes_retirement() {
        let accounts = vec![savings_account(30000.0), retirement_account(200000.0)];
        let cf = cashflow(6000.0, 5000.0);
        assert_eq!(actual_reserve_months(&accounts, &cf), 6.0);
    }

    #[test]
    fn reserve_months_divides_savings_by_spending() {
        // 30000 savings / 5000 spending = 6 months
        let accounts = vec![savings_account(30000.0)];
        let cf = cashflow(6000.0, 5000.0);
        assert_eq!(actual_reserve_months(&accounts, &cf), 6.0);
    }

    #[test]
    fn reserve_months_ignores_non_liquid_accounts() {
        // Only liquid-cash-equivalent types count; brokerage is excluded
        let accounts = vec![savings_account(30000.0), brokerage_account(10000.0)];
        let cf = cashflow(6000.0, 5000.0);
        assert_eq!(actual_reserve_months(&accounts, &cf), 6.0);
    }

    #[test]
    fn reserve_months_zero_spending_returns_zero() {
        let accounts = vec![savings_account(30000.0)];
        let cf = cashflow(0.0, 0.0);
        assert_eq!(actual_reserve_months(&accounts, &cf), 0.0);
    }

    #[test]
    fn reserve_months_no_liquid_accounts_returns_zero() {
        // Only non-liquid accounts (brokerage) — reserves = 0
        let accounts = vec![brokerage_account(10000.0)];
        let cf = cashflow(6000.0, 5000.0);
        assert_eq!(actual_reserve_months(&accounts, &cf), 0.0);
    }

    // -----------------------------------------------------------------------
    // compute_progress — full integration of goals + actuals
    // -----------------------------------------------------------------------

    fn goals_with_savings_rate(pct: f64) -> Goals {
        Goals {
            savings_rate: Some(SavingsRateGoal {
                target_percent: pct,
            }),
            emergency_fund: None,
            debt_payoff: None,
        }
    }

    fn goals_with_emergency_fund(months: f64) -> Goals {
        Goals {
            savings_rate: None,
            emergency_fund: Some(EmergencyFundGoal {
                target_months: months,
            }),
            debt_payoff: None,
        }
    }

    fn empty_goals() -> Goals {
        Goals {
            savings_rate: None,
            emergency_fund: None,
            debt_payoff: None,
        }
    }

    #[test]
    fn savings_rate_on_track_when_actual_exceeds_goal() {
        let goals = goals_with_savings_rate(20.0);
        // income=10000, spending=7500 → 25% actual, goal 20% → on track
        let cf = cashflow(10000.0, 7500.0);
        let result = compute_progress(&goals, &[], &cf, &[], "2026-04", "2026-05", (2026, 5));
        assert_eq!(result.savings_rate.unwrap().status, "on track");
        assert!(result.emergency_fund.is_none());
    }

    #[test]
    fn savings_rate_drifting_when_between_half_and_goal() {
        let goals = goals_with_savings_rate(20.0);
        // 17% actual, goal 20% → drifting
        let cf = cashflow(10000.0, 8300.0);
        let result = compute_progress(&goals, &[], &cf, &[], "2026-04", "2026-05", (2026, 5));
        assert_eq!(result.savings_rate.unwrap().status, "drifting");
    }

    #[test]
    fn savings_rate_off_when_below_half_goal() {
        let goals = goals_with_savings_rate(20.0);
        // 6% actual (spending=9400), goal 20% → off
        let cf = cashflow(10000.0, 9400.0);
        let result = compute_progress(&goals, &[], &cf, &[], "2026-04", "2026-05", (2026, 5));
        assert_eq!(result.savings_rate.unwrap().status, "off");
    }

    #[test]
    fn emergency_fund_on_track_when_reserves_cover_goal() {
        let goals = goals_with_emergency_fund(6.0);
        let accounts = vec![savings_account(42000.0)]; // 42000/5000 = 8.4 months
        let cf = cashflow(6000.0, 5000.0);
        let result = compute_progress(&goals, &accounts, &cf, &[], "2026-04", "2026-05", (2026, 5));
        assert_eq!(result.emergency_fund.unwrap().status, "on track");
    }

    #[test]
    fn emergency_fund_drifting_when_between_half_and_goal() {
        let goals = goals_with_emergency_fund(6.0);
        let accounts = vec![savings_account(20000.0)]; // 20000/5000 = 4 months
        let cf = cashflow(6000.0, 5000.0);
        let result = compute_progress(&goals, &accounts, &cf, &[], "2026-04", "2026-05", (2026, 5));
        assert_eq!(result.emergency_fund.unwrap().status, "drifting");
    }

    #[test]
    fn emergency_fund_off_when_below_half_target() {
        let goals = goals_with_emergency_fund(6.0);
        let accounts = vec![savings_account(10000.0)]; // 10000/5000 = 2 months
        let cf = cashflow(6000.0, 5000.0);
        let result = compute_progress(&goals, &accounts, &cf, &[], "2026-04", "2026-05", (2026, 5));
        assert_eq!(result.emergency_fund.unwrap().status, "off");
    }

    #[test]
    fn unset_goal_is_not_reported() {
        // No goals set at all → both are None
        let goals = empty_goals();
        let result = compute_progress(
            &goals,
            &[],
            &cashflow(10000.0, 8000.0),
            &[],
            "2026-04",
            "2026-05",
            (2026, 5),
        );
        assert!(result.savings_rate.is_none());
        assert!(result.emergency_fund.is_none());
    }

    // --- RED: empty goals yields a guidance message, not a silent empty payload ---

    #[test]
    fn empty_goals_returns_guidance_message() {
        let goals = empty_goals();
        let result = compute_progress(
            &goals,
            &[],
            &cashflow(10000.0, 8000.0),
            &[],
            "2026-04",
            "2026-05",
            (2026, 5),
        );
        let guidance = result.guidance.as_deref().unwrap_or("");
        assert!(
            guidance.contains("no goals") || guidance.contains("goals.toml"),
            "expected guidance about missing goals, got: {guidance:?}"
        );
    }

    // --- TRIANGULATE: a goal being set means no guidance message ---

    #[test]
    fn goals_present_means_no_guidance() {
        let goals = goals_with_savings_rate(20.0);
        let cf = cashflow(10000.0, 7500.0);
        let result = compute_progress(&goals, &[], &cf, &[], "2026-04", "2026-05", (2026, 5));
        assert!(
            result.guidance.is_none(),
            "guidance should be absent when goals are configured"
        );
    }

    // --- debt-payoff-only config: debt_payoff present, guidance absent (ADR 0007 #22 guard removal) ---
    // Previously this test asserted that guidance mentioning "#27" was present.
    // With real debt-payoff computation implemented (issue #27), a debt-only
    // config now returns a populated debt_payoff field instead of a guidance message.

    #[test]
    fn debt_payoff_only_config_returns_debt_payoff_not_guidance() {
        use crate::goals::DebtPayoffGoal;
        let goals = Goals {
            savings_rate: None,
            emergency_fund: None,
            debt_payoff: Some(DebtPayoffGoal {
                target_date: "2027-12-01".to_string(),
                monthly_payment: Some(500.0),
            }),
        };
        let cf = cashflow(10000.0, 8000.0);
        let result = compute_progress(&goals, &[], &cf, &[], "2026-04", "2026-05", (2026, 5));
        assert!(
            result.debt_payoff.is_some(),
            "debt_payoff must be Some when the goal is configured"
        );
        assert!(
            result.guidance.is_none(),
            "guidance must be absent when debt_payoff goal is set; got: {:?}",
            result.guidance
        );
    }

    // --- savings-only config: guidance absent (savings_rate is computable) ---

    #[test]
    fn savings_rate_only_config_has_no_guidance() {
        let goals = goals_with_savings_rate(20.0);
        let cf = cashflow(10000.0, 8000.0);
        let result = compute_progress(&goals, &[], &cf, &[], "2026-04", "2026-05", (2026, 5));
        assert!(
            result.guidance.is_none(),
            "guidance must be absent when savings_rate goal is set"
        );
    }

    // -----------------------------------------------------------------------
    // months_to_target_from_date — pure, hermetic, takes (today_year, today_month)
    // -----------------------------------------------------------------------

    // 9a RED: basic months calculation
    #[test]
    fn months_to_target_8_months_away() {
        // MONARCH_NOW = 2026-05-15, target = 2027-01-01
        // (2027 - 2026)*12 + (1 - 5) = 12 - 4 = 8
        assert_eq!(months_to_target_from_date("2027-01-01", 2026, 5), Some(8));
    }

    // 9c TRIANGULATE: target in the same month → 0
    #[test]
    fn months_to_target_same_month_is_zero() {
        assert_eq!(months_to_target_from_date("2026-05-01", 2026, 5), Some(0));
    }

    // 9c TRIANGULATE: target in the past → negative
    #[test]
    fn months_to_target_past_date_is_negative() {
        // target = 2026-04-01, today = 2026-05 → -1
        assert_eq!(months_to_target_from_date("2026-04-01", 2026, 5), Some(-1));
    }

    // 9c TRIANGULATE: year boundary
    #[test]
    fn months_to_target_crosses_year_boundary() {
        // target = 2027-12-01, today = 2026-05 → (1)*12 + 7 = 19
        assert_eq!(months_to_target_from_date("2027-12-01", 2026, 5), Some(19));
    }

    // -----------------------------------------------------------------------
    // months_to_target_from_date — Option return: None for malformed dates
    // -----------------------------------------------------------------------

    // 9a RED: garbage string → None (not a confident wrong answer)
    #[test]
    fn months_to_target_garbage_is_none() {
        assert_eq!(months_to_target_from_date("garbage", 2026, 5), None);
    }

    // 9a RED: empty string → None
    #[test]
    fn months_to_target_empty_string_is_none() {
        assert_eq!(months_to_target_from_date("", 2026, 5), None);
    }

    // 9a RED: year only (no month) → None
    #[test]
    fn months_to_target_year_only_is_none() {
        assert_eq!(months_to_target_from_date("2027", 2026, 5), None);
    }

    // 9a RED: month out of range (13) → None
    #[test]
    fn months_to_target_month_13_is_none() {
        assert_eq!(months_to_target_from_date("2027-13", 2026, 5), None);
    }

    // 9a RED: month zero → None
    #[test]
    fn months_to_target_month_zero_is_none() {
        assert_eq!(months_to_target_from_date("2027-00", 2026, 5), None);
    }

    // 9c TRIANGULATE: valid full date with day → Some(months)
    #[test]
    fn months_to_target_valid_full_date_is_some() {
        // "2027-12-01" from today (2026, 5) → Some(19)
        assert_eq!(months_to_target_from_date("2027-12-01", 2026, 5), Some(19));
    }

    // 9c TRIANGULATE: same month → Some(0)
    #[test]
    fn months_to_target_same_month_some_zero() {
        assert_eq!(months_to_target_from_date("2026-05-01", 2026, 5), Some(0));
    }

    // 9c TRIANGULATE: past valid date → Some(negative)
    #[test]
    fn months_to_target_past_valid_date_some_negative() {
        // "2026-04-01" from today (2026, 5) → Some(-1)
        assert_eq!(months_to_target_from_date("2026-04-01", 2026, 5), Some(-1));
    }

    // -----------------------------------------------------------------------
    // total_debt_from_accounts
    // -----------------------------------------------------------------------

    fn credit_account(balance: f64) -> Account {
        Account {
            id: "cc1".to_string(),
            display_name: "Credit Card".to_string(),
            current_balance: balance,
            balance_was_null: false,
            account_type: AccountType {
                name: "credit".to_string(),
            },
            subtype: None,
            is_hidden: false,
        }
    }

    fn loan_account(balance: f64) -> Account {
        Account {
            id: "l1".to_string(),
            display_name: "Car Loan".to_string(),
            current_balance: balance,
            balance_was_null: false,
            account_type: AccountType {
                name: "loan".to_string(),
            },
            subtype: None,
            is_hidden: false,
        }
    }

    // 9a RED: credit account balance -5000 → owed 5000
    #[test]
    fn total_debt_from_negative_credit_balance() {
        let accounts = vec![credit_account(-5000.0)];
        assert_eq!(total_debt_from_accounts(&accounts), 5000.0);
    }

    // 9c TRIANGULATE: loan account also counts
    #[test]
    fn total_debt_includes_loan_accounts() {
        let accounts = vec![credit_account(-3000.0), loan_account(-10000.0)];
        assert_eq!(total_debt_from_accounts(&accounts), 13000.0);
    }

    // 9c TRIANGULATE: overpaid credit card (positive balance) contributes 0
    #[test]
    fn total_debt_overpaid_credit_contributes_zero() {
        let accounts = vec![credit_account(50.0)];
        assert_eq!(total_debt_from_accounts(&accounts), 0.0);
    }

    // 9c TRIANGULATE: savings account is excluded
    #[test]
    fn total_debt_excludes_savings_accounts() {
        let accounts = vec![savings_account(30000.0), credit_account(-2000.0)];
        assert_eq!(total_debt_from_accounts(&accounts), 2000.0);
    }

    // 9c TRIANGULATE: no debt accounts → 0
    #[test]
    fn total_debt_no_debt_accounts_is_zero() {
        let accounts = vec![savings_account(10000.0)];
        assert_eq!(total_debt_from_accounts(&accounts), 0.0);
    }

    // -----------------------------------------------------------------------
    // actual_paydown_from_snapshots
    // -----------------------------------------------------------------------

    fn debt_snap(month: &str, balance: f64) -> AccountTypeSnapshot {
        AccountTypeSnapshot {
            account_type: "credit".to_string(),
            month: month.to_string(),
            balance,
        }
    }

    // 9a RED: prior month had more debt → positive paydown
    #[test]
    fn actual_paydown_positive_when_debt_reduced() {
        // prior: -8000 credit → owed 8000; current: -7000 → owed 7000
        // paydown = 8000 - 7000 = 1000
        let prior_month = "2026-04";
        let current_month = "2026-05";
        let snaps = vec![
            debt_snap(prior_month, -8000.0),
            debt_snap(current_month, -7000.0),
        ];
        let result = actual_paydown_from_snapshots(&snaps, prior_month, current_month);
        assert_eq!(result, Some(1000.0));
    }

    // 9c TRIANGULATE: no prior snapshot → None
    #[test]
    fn actual_paydown_none_when_no_prior_snapshot() {
        let snaps = vec![debt_snap("2026-05", -7000.0)];
        let result = actual_paydown_from_snapshots(&snaps, "2026-04", "2026-05");
        assert_eq!(result, None);
    }

    // 9c TRIANGULATE: debt grew → negative paydown
    #[test]
    fn actual_paydown_negative_when_debt_grew() {
        let snaps = vec![debt_snap("2026-04", -7000.0), debt_snap("2026-05", -8000.0)];
        let result = actual_paydown_from_snapshots(&snaps, "2026-04", "2026-05");
        assert_eq!(result, Some(-1000.0));
    }

    // 9c TRIANGULATE: multiple debt types are summed per month
    #[test]
    fn actual_paydown_sums_multiple_debt_types() {
        let snaps = vec![
            AccountTypeSnapshot {
                account_type: "credit".to_string(),
                month: "2026-04".to_string(),
                balance: -3000.0,
            },
            AccountTypeSnapshot {
                account_type: "loan".to_string(),
                month: "2026-04".to_string(),
                balance: -10000.0,
            },
            AccountTypeSnapshot {
                account_type: "credit".to_string(),
                month: "2026-05".to_string(),
                balance: -2500.0,
            },
            AccountTypeSnapshot {
                account_type: "loan".to_string(),
                month: "2026-05".to_string(),
                balance: -9600.0,
            },
        ];
        // prior_owed = 3000 + 10000 = 13000; current_owed = 2500 + 9600 = 12100
        // paydown = 900
        let result = actual_paydown_from_snapshots(&snaps, "2026-04", "2026-05");
        assert!((result.unwrap() - 900.0).abs() < 0.01);
    }

    // 9c TRIANGULATE: non-debt snapshot types are ignored
    #[test]
    fn actual_paydown_ignores_non_debt_snapshots() {
        let snaps = vec![
            AccountTypeSnapshot {
                account_type: "depository".to_string(),
                month: "2026-04".to_string(),
                balance: 10000.0,
            },
            debt_snap("2026-04", -5000.0),
            AccountTypeSnapshot {
                account_type: "depository".to_string(),
                month: "2026-05".to_string(),
                balance: 11000.0,
            },
            debt_snap("2026-05", -4500.0),
        ];
        let result = actual_paydown_from_snapshots(&snaps, "2026-04", "2026-05");
        assert_eq!(result, Some(500.0));
    }

    // -----------------------------------------------------------------------
    // compute_debt_payoff — full integration of all debt signals
    // -----------------------------------------------------------------------

    use crate::goals::DebtPayoffGoal;

    fn debt_goal(target_date: &str, monthly_payment: Option<f64>) -> DebtPayoffGoal {
        DebtPayoffGoal {
            target_date: target_date.to_string(),
            monthly_payment,
        }
    }

    fn prior_and_current_snaps(prior_owed: f64, current_owed: f64) -> Vec<AccountTypeSnapshot> {
        vec![
            AccountTypeSnapshot {
                account_type: "credit".to_string(),
                month: "2026-04".to_string(),
                balance: -prior_owed,
            },
            AccountTypeSnapshot {
                account_type: "credit".to_string(),
                month: "2026-05".to_string(),
                balance: -current_owed,
            },
        ]
    }

    // 9a RED: on track when both signals are on track
    // today = (2026, 5), target = "2027-01-01" → 8 months, total_debt=7000
    // required = 7000/8 = 875; planned = 1000 ≥ 875 → on_schedule=on track
    // paydown = 8000-7000=1000 ≥ 1000 → on_pace=on track
    #[test]
    fn compute_debt_payoff_on_track_when_both_signals_on_track() {
        let goal = debt_goal("2027-01-01", Some(1000.0));
        let accounts = vec![credit_account(-7000.0)];
        let snaps = prior_and_current_snaps(8000.0, 7000.0);
        let result = compute_debt_payoff(&goal, &accounts, &snaps, "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "on track");
        assert_eq!(result.on_schedule.as_deref(), Some("on track"));
        assert_eq!(result.on_pace.as_deref(), Some("on track"));
    }

    // 9c TRIANGULATE: worst-of blend picks drifting when on_schedule=drifting, on_pace=on track
    // planned=500, required=875: 500 ≥ 875/2=437.5 → drifting (not off)
    // paydown=500 ≥ planned=500 → on_pace=on track
    // worst → drifting
    #[test]
    fn compute_debt_payoff_worst_of_picks_drifting() {
        let goal = debt_goal("2027-01-01", Some(500.0));
        let accounts = vec![credit_account(-7000.0)];
        let snaps = prior_and_current_snaps(7500.0, 7000.0);
        let result = compute_debt_payoff(&goal, &accounts, &snaps, "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "drifting");
        assert_eq!(result.on_schedule.as_deref(), Some("drifting"));
        assert_eq!(result.on_pace.as_deref(), Some("on track"));
    }

    // 9c TRIANGULATE: worst-of picks off when planned < half required
    // planned=200, required=875: 200 < 875/2=437.5 → off
    // paydown=200 = planned=200 → on_pace=on track
    // worst → off
    #[test]
    fn compute_debt_payoff_worst_of_picks_off() {
        let goal = debt_goal("2027-01-01", Some(200.0));
        let accounts = vec![credit_account(-7000.0)];
        let snaps = prior_and_current_snaps(7200.0, 7000.0);
        let result = compute_debt_payoff(&goal, &accounts, &snaps, "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "off");
        assert_eq!(result.on_schedule.as_deref(), Some("off"));
        assert_eq!(result.on_pace.as_deref(), Some("on track"));
    }

    // 9c TRIANGULATE: already paid off → on track, required_monthly=Some(0.0)
    #[test]
    fn compute_debt_payoff_paid_off_is_on_track() {
        let goal = debt_goal("2027-01-01", Some(500.0));
        let accounts: Vec<Account> = vec![];
        let result = compute_debt_payoff(&goal, &accounts, &[], "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "on track");
        assert_eq!(result.total_debt, 0.0);
        assert_eq!(result.required_monthly, Some(0.0));
    }

    // 9c TRIANGULATE: target date passed with debt → on_schedule=off, status=off
    #[test]
    fn compute_debt_payoff_target_passed_with_debt_is_off() {
        let goal = debt_goal("2026-04-01", Some(500.0));
        let accounts = vec![credit_account(-3000.0)];
        let result = compute_debt_payoff(&goal, &accounts, &[], "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "off");
        assert!(result.months_to_target.unwrap_or(0) <= 0);
        assert_eq!(result.required_monthly, None);
    }

    // 9c TRIANGULATE: no monthly_payment → on_schedule/on_pace None; status unknown
    #[test]
    fn compute_debt_payoff_no_monthly_payment_yields_unknown() {
        let goal = debt_goal("2027-01-01", None);
        let accounts = vec![credit_account(-7000.0)];
        let result = compute_debt_payoff(&goal, &accounts, &[], "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "unknown");
        assert!(result.on_schedule.is_none());
        assert!(result.on_pace.is_none());
        // numbers still surface
        assert_eq!(result.total_debt, 7000.0);
        assert_eq!(result.months_to_target, Some(8));
        assert!(result.required_monthly.is_some());
    }

    // 9c TRIANGULATE: no prior snapshot → on_pace None; status driven only by on_schedule
    // planned=500, required=875: 500 ≥ 437.5 → drifting
    #[test]
    fn compute_debt_payoff_no_prior_snapshot_uses_only_on_schedule() {
        let goal = debt_goal("2027-01-01", Some(500.0));
        let accounts = vec![credit_account(-7000.0)];
        let result = compute_debt_payoff(&goal, &accounts, &[], "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "drifting");
        assert_eq!(result.on_schedule.as_deref(), Some("drifting"));
        assert_eq!(result.on_pace, None);
    }

    // 9c TRIANGULATE: no prior snapshot, planned far below required → off
    // planned=200, required=875: 200 < 437.5 → off
    #[test]
    fn compute_debt_payoff_no_prior_snapshot_off_schedule() {
        let goal = debt_goal("2027-01-01", Some(200.0));
        let accounts = vec![credit_account(-7000.0)];
        let result = compute_debt_payoff(&goal, &accounts, &[], "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "off");
        assert_eq!(result.on_schedule.as_deref(), Some("off"));
        assert_eq!(result.on_pace, None);
    }

    // 9c TRIANGULATE: debt grew → on_pace=off
    #[test]
    fn compute_debt_payoff_debt_grew_is_off_pace() {
        let goal = debt_goal("2027-01-01", Some(1000.0));
        let accounts = vec![credit_account(-8000.0)];
        let snaps = prior_and_current_snaps(7000.0, 8000.0); // debt grew
        let result = compute_debt_payoff(&goal, &accounts, &snaps, "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.on_pace.as_deref(), Some("off"));
    }

    // worst-of blend must be driven by on_pace when it is the worse signal.
    // Mirror of the on_schedule-worse case: here on_schedule="on track" but
    // on_pace="off" (debt grew), so overall status must be "off". Guards against
    // a blend that ignores on_pace.
    #[test]
    fn compute_debt_payoff_worst_of_driven_by_on_pace_off() {
        // target 2027-01-01 from (2026,5) = 8 months; debt 8000; required 8000/8=1000;
        // planned 1000 -> on_schedule "on track". Snapshots prior 7000 / current 8000
        // -> paydown -1000 -> on_pace "off".
        let goal = debt_goal("2027-01-01", Some(1000.0));
        let accounts = vec![credit_account(-8000.0)];
        let snaps = prior_and_current_snaps(7000.0, 8000.0);
        let result = compute_debt_payoff(&goal, &accounts, &snaps, "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.on_schedule.as_deref(), Some("on track"));
        assert_eq!(result.on_pace.as_deref(), Some("off"));
        assert_eq!(
            result.status, "off",
            "worst-of must surface on_pace 'off' over on_schedule 'on track'"
        );
    }

    // -----------------------------------------------------------------------
    // Audit gaps
    // -----------------------------------------------------------------------

    // Audit: malformed target_date → months_to_target_from_date returns None (not a fabricated value)
    // These cases are now covered by the dedicated None tests above; this test just
    // documents the previously-panic-free but wrong behavior is now correctly None.
    #[test]
    fn months_to_target_malformed_dates_are_none() {
        assert_eq!(months_to_target_from_date("not-a-date", 2026, 5), None);
        assert_eq!(months_to_target_from_date("", 2026, 5), None);
        assert_eq!(months_to_target_from_date("2027", 2026, 5), None);
    }

    // Audit: months_to_target == 0 with debt > 0 → status "off" (target this month, still owed)
    #[test]
    fn compute_debt_payoff_target_this_month_with_debt_is_off() {
        // target = "2026-05-01" and today = (2026, 5) → months_to_target = Some(0)
        // 0 is treated like a passed deadline with remaining debt → off
        let goal = debt_goal("2026-05-01", Some(500.0));
        let accounts = vec![credit_account(-3000.0)];
        let result = compute_debt_payoff(&goal, &accounts, &[], "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "off");
        assert_eq!(result.months_to_target, Some(0));
        assert_eq!(result.required_monthly, None); // can't compute when months=0
    }

    // -----------------------------------------------------------------------
    // Step 2: malformed target_date propagation — None months_to_target
    // -----------------------------------------------------------------------

    // 9a RED: malformed date + debt + planned payment + no snapshot → "unknown", months_to_target absent
    #[test]
    fn compute_debt_payoff_malformed_date_no_snapshot_is_unknown() {
        let goal = debt_goal("garbage", Some(500.0));
        let accounts = vec![credit_account(-5000.0)];
        let result = compute_debt_payoff(&goal, &accounts, &[], "2026-04", "2026-05", 2026, 5);
        assert_eq!(
            result.status, "unknown",
            "malformed date must not produce 'off'"
        );
        assert_eq!(
            result.months_to_target, None,
            "months_to_target must be absent for malformed date"
        );
        assert_eq!(
            result.required_monthly, None,
            "required_monthly must be None when months unknown"
        );
    }

    // 9a RED: empty date string → same as garbage
    #[test]
    fn compute_debt_payoff_empty_date_no_snapshot_is_unknown() {
        let goal = debt_goal("", Some(500.0));
        let accounts = vec![credit_account(-3000.0)];
        let result = compute_debt_payoff(&goal, &accounts, &[], "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "unknown");
        assert_eq!(result.months_to_target, None);
    }

    // 9a RED: year-only date → same as garbage
    #[test]
    fn compute_debt_payoff_year_only_date_no_snapshot_is_unknown() {
        let goal = debt_goal("2027", Some(500.0));
        let accounts = vec![credit_account(-3000.0)];
        let result = compute_debt_payoff(&goal, &accounts, &[], "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "unknown");
        assert_eq!(result.months_to_target, None);
    }

    // 9c TRIANGULATE: malformed date + good prior-snapshot paydown → on_pace signal drives status
    #[test]
    fn compute_debt_payoff_malformed_date_with_good_paydown_uses_on_pace() {
        // planned=500, actual paydown=600 ≥ 500 → on_pace="on track"; no on_schedule
        // worst of {on_track} = "on track" (not "off")
        let goal = debt_goal("2027", Some(500.0));
        let accounts = vec![credit_account(-7000.0)];
        let snaps = prior_and_current_snaps(7600.0, 7000.0); // paid down 600
        let result = compute_debt_payoff(&goal, &accounts, &snaps, "2026-04", "2026-05", 2026, 5);
        assert_ne!(
            result.status, "off",
            "malformed date must not force 'off' when pace is good"
        );
        assert_eq!(result.months_to_target, None);
        assert_eq!(result.on_schedule, None);
        assert_eq!(result.on_pace.as_deref(), Some("on track"));
        assert_eq!(result.status, "on track");
    }

    // 9c TRIANGULATE: genuinely past valid date with debt → still "off"
    #[test]
    fn compute_debt_payoff_genuinely_past_valid_date_is_off() {
        // "2026-04-01" is a valid date, months_to_target = Some(-1), debt > 0 → "off"
        let goal = debt_goal("2026-04-01", Some(500.0));
        let accounts = vec![credit_account(-3000.0)];
        let result = compute_debt_payoff(&goal, &accounts, &[], "2026-04", "2026-05", 2026, 5);
        assert_eq!(result.status, "off");
        assert_eq!(result.months_to_target, Some(-1));
    }

    #[test]
    fn unset_savings_rate_absent_even_when_emergency_fund_present() {
        let goals = goals_with_emergency_fund(6.0);
        let accounts = vec![savings_account(30000.0)];
        let cf = cashflow(6000.0, 5000.0);
        let result = compute_progress(&goals, &accounts, &cf, &[], "2026-04", "2026-05", (2026, 5));
        assert!(
            result.savings_rate.is_none(),
            "savings_rate should be absent when goal not set"
        );
        assert!(result.emergency_fund.is_some());
    }
}