mps-rs 1.6.1

MPS — plain-text personal productivity CLI (Rust)
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
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
//! Integration tests — mirrors Ruby's test/integration_test.rb and test/store_test.rb.
//! Uses real fixture files from tests/fixtures/*.mps.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use chrono::NaiveDate;
use indexmap::IndexMap;
use mps::{elements::*, parser, ref_resolver::RefResolver, store::Store};

// ── Fixture helpers ──────────────────────────────────────────────────────────

fn fixtures_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("tests").join("fixtures")
}

fn fixture_path(name: &str) -> PathBuf {
    fixtures_dir().join(name)
}

fn parse_fixture(name: &str) -> IndexMap<String, Element> {
    parser::parse_file(&fixture_path(name)).expect("fixture parse failed")
}

fn non_root(elements: &IndexMap<String, Element>) -> Vec<&Element> {
    elements.values()
        .filter(|e| !e.is_mps_group() && !e.is_unknown())
        .collect()
}

fn fixture_store() -> (tempfile::TempDir, Store) {
    let dir = tempfile::tempdir().unwrap();
    for entry in std::fs::read_dir(fixtures_dir()).unwrap() {
        let entry = entry.unwrap();
        let dst = dir.path().join(entry.file_name());
        std::fs::copy(entry.path(), dst).unwrap();
    }
    let store = Store::new(dir.path());
    (dir, store)
}

// ── Fixtures available ───────────────────────────────────────────────────────
//   20260101.1000000001.mps — bare elements, no tags
//   20260102.1000000002.mps — tags, nested tasks, mps block
//   20260201.1000000010.mps — sprint planning (SPRINT_FILE)
//   20260215.1000000020.mps — rust/learning day  (RUST_FILE)
//   20260220.1000000030.mps — release day        (RELEASE_FILE)

const SPRINT_FILE:  &str = "20260201.1000000010.mps";
const RUST_FILE:    &str = "20260215.1000000020.mps";
const RELEASE_FILE: &str = "20260220.1000000030.mps";

// ── Bare elements (20260101) ─────────────────────────────────────────────────

#[test]
fn test_bare_file_has_task_note_reminder_log() {
    let els = parse_fixture("20260101.1000000001.mps");
    let leafs = non_root(&els);
    assert!(leafs.iter().any(|e| e.kind() == ElementKind::Task));
    assert!(leafs.iter().any(|e| e.kind() == ElementKind::Note));
    assert!(leafs.iter().any(|e| e.kind() == ElementKind::Reminder));
    assert!(leafs.iter().any(|e| e.kind() == ElementKind::Log));
}

#[test]
fn test_bare_file_task_is_open() {
    let els = parse_fixture("20260101.1000000001.mps");
    let task = els.values().find(|e| e.kind() == ElementKind::Task).unwrap();
    if let Element::Task { data, .. } = task {
        assert!(data.is_open());
    } else {
        panic!("not a task");
    }
}

// ── Tagged nested file (20260102) ────────────────────────────────────────────

#[test]
fn test_tagged_file_task_has_tags() {
    let els = parse_fixture("20260102.1000000002.mps");
    // Parser inserts in close-order (depth-first), so check all tasks for the expected tags.
    let has_tags = els.values()
        .filter(|e| e.kind() == ElementKind::Task)
        .any(|e| e.tags().contains(&"x".to_string()) && e.tags().contains(&"y".to_string()));
    assert!(has_tags, "expected a task with tags x and y");
}

#[test]
fn test_tagged_file_has_nested_task() {
    let els = parse_fixture("20260102.1000000002.mps");
    // Root wrapper + top-level task (with nested task) + reminder + log + mps group + nested elements
    let task_count = els.values().filter(|e| e.kind() == ElementKind::Task).count();
    assert!(task_count >= 2, "should have at least a top-level and nested task");
}

#[test]
fn test_tagged_file_reminder_at_5pm() {
    let els = parse_fixture("20260102.1000000002.mps");
    let reminder = els.values().find(|e| e.kind() == ElementKind::Reminder).unwrap();
    if let Element::Reminder { data, .. } = reminder {
        assert_eq!(data.at.as_deref(), Some("5pm"));
    } else {
        panic!("not a reminder");
    }
}

#[test]
fn test_tagged_file_mps_group_has_task_and_note() {
    let els = parse_fixture("20260102.1000000002.mps");
    // The @mps group should contain a nested task and note
    let nested_task = els.values()
        .any(|e| e.kind() == ElementKind::Task && e.body_str().contains("nested task"));
    let nested_note = els.values()
        .any(|e| e.kind() == ElementKind::Note && e.body_str().contains("Nested note"));
    assert!(nested_task, "nested task inside @mps expected");
    assert!(nested_note, "nested note inside @mps expected");
}

// ── Sprint planning fixture (20260201) ───────────────────────────────────────

#[test]
fn test_sprint_tasks_count() {
    let els = parse_fixture(SPRINT_FILE);
    let tasks: Vec<&Element> = els.values().filter(|e| e.kind() == ElementKind::Task).collect();
    assert_eq!(tasks.len(), 4);
}

#[test]
fn test_sprint_open_tasks() {
    let els = parse_fixture(SPRINT_FILE);
    let open: Vec<&Element> = els.values()
        .filter(|e| matches!(e, Element::Task { data, .. } if data.is_open()))
        .collect();
    assert_eq!(open.len(), 3);
}

#[test]
fn test_sprint_done_task_body() {
    let els = parse_fixture(SPRINT_FILE);
    let done: Vec<&Element> = els.values()
        .filter(|e| matches!(e, Element::Task { data, .. } if data.is_done()))
        .collect();
    assert_eq!(done.len(), 1);
    assert!(done[0].body_str().contains("Set up the PostgreSQL schema"));
}

#[test]
fn test_sprint_log_duration_210_minutes() {
    let els = parse_fixture(SPRINT_FILE);
    let log = els.values().find(|e| e.kind() == ElementKind::Log).unwrap();
    if let Element::Log { data, .. } = log {
        assert_eq!(data.duration_minutes(), Some(210)); // 3h30m
        assert_eq!(data.duration_str(), Some("3h30m".into()));
    } else {
        panic!("not a log");
    }
}

#[test]
fn test_sprint_reminder_at_3pm() {
    let els = parse_fixture(SPRINT_FILE);
    let rem = els.values().find(|e| e.kind() == ElementKind::Reminder).unwrap();
    if let Element::Reminder { data, .. } = rem {
        assert_eq!(data.at.as_deref(), Some("3pm"));
    } else {
        panic!("not a reminder");
    }
}

#[test]
fn test_sprint_nested_tagged_tasks() {
    let els = parse_fixture(SPRINT_FILE);
    let backend_or_frontend = els.values()
        .filter(|e| e.kind() == ElementKind::Task)
        .filter(|e| e.tags().iter().any(|t| t == "backend" || t == "frontend"))
        .count();
    assert!(backend_or_frontend >= 3, "expected ≥ 3 backend/frontend tasks");
}

#[test]
fn test_sprint_ref_resolver_top_level() {
    let els = parse_fixture(SPRINT_FILE);
    let r = RefResolver::new(&els);
    // The @mps group at top level should be mps-1
    let mps_refs: Vec<_> = els.keys()
        .filter(|k| {
            let e = &els[*k];
            e.is_mps_group() && k.split('.').count() == 2
        })
        .collect();
    for epoch_ref in mps_refs {
        let human = r.to_human(epoch_ref).expect("mps group should be mapped");
        assert!(human.starts_with("mps-"));
    }
}

#[test]
fn test_sprint_ref_resolver_nested_refs() {
    let els = parse_fixture(SPRINT_FILE);
    let r = RefResolver::new(&els);
    // All tasks inside the @mps group should get nested refs like mps-1.1, mps-1.2, ...
    for (epoch_ref, el) in &els {
        if el.kind() == ElementKind::Task && epoch_ref.split('.').count() == 3 {
            let human = r.to_human(epoch_ref);
            assert!(human.is_some(), "nested task {} should have a human ref", epoch_ref);
            let h = human.unwrap();
            assert!(h.contains('.'), "nested human ref should contain a dot: {}", h);
        }
    }
}

// ── Rust/learning fixture (20260215) ─────────────────────────────────────────

#[test]
fn test_rust_done_reading_tasks() {
    let els = parse_fixture(RUST_FILE);
    let done_reading: Vec<_> = els.values()
        .filter(|e| matches!(e, Element::Task { data, .. } if data.is_done())
            && e.tags().contains(&"reading".to_string()))
        .collect();
    assert_eq!(done_reading.len(), 2);
}

#[test]
fn test_rust_open_reading_task_body_contains_rustlings() {
    let els = parse_fixture(RUST_FILE);
    let open_reading = els.values().find(|e| {
        matches!(e, Element::Task { data, .. } if data.is_open())
            && e.tags().contains(&"reading".to_string())
    }).unwrap();
    assert!(open_reading.body_str().contains("Rustlings"));
}

#[test]
fn test_rust_habit_tasks() {
    let els = parse_fixture(RUST_FILE);
    let habit_tasks: Vec<_> = els.values()
        .filter(|e| e.kind() == ElementKind::Task && e.tags().contains(&"habits".to_string()))
        .collect();
    assert_eq!(habit_tasks.len(), 3);
    let done_habits = habit_tasks.iter()
        .filter(|e| matches!(e, Element::Task { data, .. } if data.is_done()))
        .count();
    assert_eq!(done_habits, 2);
}

#[test]
fn test_rust_log_duration_90_minutes() {
    let els = parse_fixture(RUST_FILE);
    let log = els.values().find(|e| e.kind() == ElementKind::Log).unwrap();
    if let Element::Log { data, .. } = log {
        assert_eq!(data.duration_minutes(), Some(90)); // 1h30m
        assert_eq!(data.duration_str(), Some("1h30m".into()));
    }
}

#[test]
fn test_rust_all_tasks_have_human_refs() {
    let els = parse_fixture(RUST_FILE);
    let r = RefResolver::new(&els);
    for (epoch_ref, el) in &els {
        if el.kind() == ElementKind::Task {
            let human = r.to_human(epoch_ref);
            assert!(human.is_some(), "task {} should have a human ref", epoch_ref);
        }
    }
}

// ── Release fixture (20260220) ────────────────────────────────────────────────

#[test]
fn test_release_done_tasks() {
    let els = parse_fixture(RELEASE_FILE);
    let done: Vec<_> = els.values()
        .filter(|e| matches!(e, Element::Task { data, .. } if data.is_done()))
        .collect();
    assert_eq!(done.len(), 2);
}

#[test]
fn test_release_total_log_time_360_minutes() {
    let els = parse_fixture(RELEASE_FILE);
    let total: i64 = els.values()
        .filter_map(|e| if let Element::Log { data, .. } = e { data.duration_minutes() } else { None })
        .sum();
    assert_eq!(total, 360); // 4h + 2h
}

#[test]
fn test_release_nested_notes_in_retrospective() {
    let els = parse_fixture(RELEASE_FILE);
    let notes_count = els.values().filter(|e| e.kind() == ElementKind::Note).count();
    assert!(notes_count >= 3, "expected ≥ 3 notes in release day");
}

#[test]
fn test_release_has_open_v11_task() {
    let els = parse_fixture(RELEASE_FILE);
    let v11_task = els.values().find(|e| {
        matches!(e, Element::Task { data, .. } if data.is_open())
            && e.body_str().contains("v1.1")
    });
    assert!(v11_task.is_some(), "expected an open task mentioning v1.1");
}

#[test]
fn test_release_tags_include_work_and_release() {
    let els = parse_fixture(RELEASE_FILE);
    let all_tags: Vec<String> = els.values()
        .filter(|e| !e.is_mps_group() && !e.is_unknown())
        .flat_map(|e| e.tags().to_vec())
        .collect();
    assert!(all_tags.contains(&"work".to_string()));
    assert!(all_tags.contains(&"release".to_string()));
}

// ── RefResolver correctness ───────────────────────────────────────────────────

#[test]
fn test_ref_resolver_roundtrip_on_all_fixtures() {
    for fixture in &[SPRINT_FILE, RUST_FILE, RELEASE_FILE] {
        let els = parse_fixture(fixture);
        let r = RefResolver::new(&els);
        for epoch_ref in els.keys() {
            if let Some(human) = r.to_human(epoch_ref) {
                let back = r.to_epoch(human).unwrap_or("none");
                assert_eq!(back, epoch_ref.as_str(), "roundtrip failed for {}", epoch_ref);
            }
        }
    }
}

#[test]
fn test_ref_resolver_skips_unknown_elements() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("20260101.1000000001.mps");
    std::fs::write(&path, "@custom_type{ body }\n@task{ real task }").unwrap();
    let els = parser::parse_file(&path).unwrap();
    let r = RefResolver::new(&els);

    // Unknown element at .1 should be skipped
    assert!(r.to_human("20260101.1").is_none());
    // Task at .2 should be task-1 (counter resets per-type, Unknown doesn't consume)
    assert_eq!(r.to_human("20260101.2"), Some("task-1"));
}

// ── Store integration across multiple fixture files ───────────────────────────

#[test]
fn test_store_all_files_sorted() {
    let (_dir, store) = fixture_store();
    let files = store.all_files().unwrap();
    let names: Vec<String> = files.iter()
        .map(|p| p.file_name().unwrap().to_str().unwrap().to_string())
        .collect();
    let mut sorted = names.clone();
    sorted.sort();
    assert_eq!(names, sorted, "files should be sorted chronologically");
}

#[test]
fn test_store_tag_counts_across_fixtures() {
    let (_dir, store) = fixture_store();
    let mut all_tags: HashMap<String, usize> = HashMap::new();
    for file in store.all_files().unwrap() {
        let date_str = file.file_name().unwrap().to_str().unwrap()[..8].to_string();
        let d = NaiveDate::parse_from_str(&date_str, "%Y%m%d").unwrap();
        let els = store.parse_date(d).unwrap();
        for el in els.values().filter(|e| !e.is_mps_group() && !e.is_unknown()) {
            for tag in el.tags() {
                *all_tags.entry(tag.clone()).or_insert(0) += 1;
            }
        }
    }
    assert!(all_tags.get("work").copied().unwrap_or(0) >= 3, "work tag appears across multiple files");
    assert!(all_tags.get("reading").copied().unwrap_or(0) >= 3, "reading tag from rust day");
    assert!(all_tags.get("release").copied().unwrap_or(0) >= 2, "release tag from release day");
}

#[test]
fn test_store_search_authentication_across_fixtures() {
    let (_dir, store) = fixture_store();
    let results = store.search("authentication", None, None, None).unwrap();
    assert!(!results.is_empty(), "should find 'authentication' in some fixture");
    assert!(results.iter().all(|r| !r.element.is_unknown()));
}

#[test]
fn test_store_search_type_filter_log() {
    let (_dir, store) = fixture_store();
    let results = store.search("", None, Some("log"), None).unwrap();
    assert!(results.iter().all(|r| r.element.kind() == ElementKind::Log));
}

#[test]
fn test_store_search_excludes_mps_and_unknown() {
    let (_dir, store) = fixture_store();
    let results = store.search("", None, None, None).unwrap();
    for r in &results {
        assert!(!r.element.is_mps_group(), "search should not return @mps groups");
        assert!(!r.element.is_unknown(), "search should not return Unknown elements");
    }
}

#[test]
fn test_store_all_file_dates_unique_and_sorted() {
    let (_dir, store) = fixture_store();
    let dates = store.all_file_dates().unwrap();
    let mut sorted = dates.clone();
    sorted.sort();
    sorted.dedup();
    assert_eq!(dates, sorted, "dates should be unique and sorted");
}

#[test]
fn test_store_files_since_sprint_date() {
    let (_dir, store) = fixture_store();
    let since = NaiveDate::from_ymd_opt(2026, 2, 1).unwrap();
    let files = store.files_since(since).unwrap();
    for f in &files {
        let date_str = &f.file_name().unwrap().to_str().unwrap()[..8];
        assert!(date_str >= "20260201", "file {} is before since date", date_str);
    }
    // Files before 2026-02-01 (the January fixtures) should be excluded
    let jan_files: Vec<_> = files.iter().filter(|f| {
        f.file_name().unwrap().to_str().unwrap().starts_with("202601")
    }).collect();
    assert!(jan_files.is_empty(), "January fixtures should be excluded");
}

// ── Store::rewrite_element ────────────────────────────────────────────────────

#[test]
fn test_rewrite_element_marks_task_done() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("20260101.1000000001.mps");
    std::fs::write(&path, "@task[work]{\n  Ship it\n}\n").unwrap();

    let store = Store::new(dir.path());
    let date  = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();

    let mut attrs = HashMap::new();
    attrs.insert("status".to_string(), "done".to_string());

    // Use human ref "task-1"
    let ok = store.rewrite_element("task-1", &attrs, date).unwrap();
    assert!(ok, "rewrite should succeed");

    let content = std::fs::read_to_string(&path).unwrap();
    assert!(content.contains("status: done"), "file should now contain status: done");
    assert!(content.contains("work"), "tag 'work' should be preserved");
}

#[test]
fn test_rewrite_element_by_epoch_ref() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("20260101.1000000001.mps");
    std::fs::write(&path, "@task[work]{\n  Ship it\n}\n").unwrap();

    let store = Store::new(dir.path());
    let date  = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();

    // Parse to get the epoch ref
    let els = store.parse_date(date).unwrap();
    let epoch_ref = els.keys().find(|k| {
        let e = &els[*k];
        e.kind() == ElementKind::Task
    }).unwrap().clone();

    let mut attrs = HashMap::new();
    attrs.insert("status".to_string(), "done".to_string());

    let ok = store.rewrite_element(&epoch_ref, &attrs, date).unwrap();
    assert!(ok);

    let content = std::fs::read_to_string(&path).unwrap();
    assert!(content.contains("status: done"));
}

#[test]
fn test_rewrite_element_nonexistent_ref_returns_false() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("20260101.1000000001.mps");
    std::fs::write(&path, "@task{\n  Only task\n}\n").unwrap();

    let store = Store::new(dir.path());
    let date  = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();

    let mut attrs = HashMap::new();
    attrs.insert("status".to_string(), "done".to_string());

    // task-99 doesn't exist
    let ok = store.rewrite_element("task-99", &attrs, date).unwrap();
    assert!(!ok, "should return false for missing ref");
}

#[test]
fn test_rewrite_element_preserves_file_content() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("20260101.1000000001.mps");
    let original = "@task[work]{\n  Task one\n}\n\n@note{\n  A note\n}\n";
    std::fs::write(&path, original).unwrap();

    let store = Store::new(dir.path());
    let date  = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();

    let mut attrs = HashMap::new();
    attrs.insert("status".to_string(), "done".to_string());

    store.rewrite_element("task-1", &attrs, date).unwrap();

    let content = std::fs::read_to_string(&path).unwrap();
    // The note should be untouched
    assert!(content.contains("A note"), "note body should be preserved");
    // The task should have the updated status
    assert!(content.contains("status: done"));
}

#[test]
fn test_rewrite_element_log_start_end() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("20260101.1000000001.mps");
    std::fs::write(&path, "@log[work]{\n  Deep work session\n}\n").unwrap();

    let store = Store::new(dir.path());
    let date  = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();

    let mut attrs = HashMap::new();
    attrs.insert("start".to_string(), "09:00".to_string());
    attrs.insert("end".to_string(), "12:00".to_string());

    let ok = store.rewrite_element("log-1", &attrs, date).unwrap();
    assert!(ok);

    let content = std::fs::read_to_string(&path).unwrap();
    assert!(content.contains("start: 09:00"));
    assert!(content.contains("end: 12:00"));
    assert!(content.contains("work"), "tag preserved");
}

// ── Store: epoch-less filenames (e.g. 20241103.mps) ──────────────────────────

#[test]
fn test_store_epoch_less_filename_included_in_all_files() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("20260101.1000000001.mps"), "@task{ A }").unwrap();
    std::fs::write(dir.path().join("20260102.mps"), "@note{ B }").unwrap();

    let store = Store::new(dir.path());
    let files = store.all_files().unwrap();
    let names: Vec<String> = files.iter()
        .map(|p| p.file_name().unwrap().to_str().unwrap().to_string())
        .collect();

    assert!(names.contains(&"20260101.1000000001.mps".to_string()));
    assert!(names.contains(&"20260102.mps".to_string()));
}

#[test]
fn test_store_epoch_less_filename_found_by_find_file() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("20260101.mps"), "@task{ A }").unwrap();

    let store = Store::new(dir.path());
    let date  = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();
    assert!(store.find_file(date).is_some());
}

// ── Store: non-date files are ignored ────────────────────────────────────────

#[test]
fn test_store_non_date_filenames_ignored() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("20260101.1000000001.mps"), "@task{ A }").unwrap();
    std::fs::write(dir.path().join("weird_name.mps"), "@task{ B }").unwrap();

    let store = Store::new(dir.path());
    let files = store.all_files().unwrap();
    let names: Vec<String> = files.iter()
        .map(|p| p.file_name().unwrap().to_str().unwrap().to_string())
        .collect();

    assert!(names.contains(&"20260101.1000000001.mps".to_string()));
    assert!(!names.contains(&"weird_name.mps".to_string()));
}

// ── Search edge cases ─────────────────────────────────────────────────────────

#[test]
fn test_search_empty_query_returns_all_non_mps() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(
        dir.path().join("20260101.1000000001.mps"),
        "@task{ do something }\n@note{ remember this }\n",
    ).unwrap();

    let store = Store::new(dir.path());
    // Empty query string should still work (matches everything)
    let results = store.search("", None, None, None).unwrap();
    assert_eq!(results.len(), 2);
}

// ── Parser: unknown element type ──────────────────────────────────────────────

#[test]
fn test_parser_unknown_element_in_search() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(
        dir.path().join("20260101.1000000001.mps"),
        "@custom_widget{ some body }\n@task{ real task }",
    ).unwrap();

    let store = Store::new(dir.path());
    let results = store.search("", None, None, None).unwrap();
    // Unknown element should be excluded; only the task should appear
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].element.kind(), ElementKind::Task);
}

// ── Date parsing ──────────────────────────────────────────────────────────────

#[test]
fn test_date_parse_all_formats() {
    use mps::date_parse::parse_date;
    use chrono::Local;

    let today = Local::now().date_naive();

    assert_eq!(parse_date("today").unwrap(), today);
    assert_eq!(parse_date("yesterday").unwrap(), today - chrono::Duration::days(1));
    assert_eq!(parse_date("last week").unwrap(), today - chrono::Duration::days(7));
    assert_eq!(parse_date("3 days ago").unwrap(), today - chrono::Duration::days(3));
    assert_eq!(parse_date("20260101").unwrap(), NaiveDate::from_ymd_opt(2026, 1, 1).unwrap());
    assert_eq!(parse_date("2026-01-01").unwrap(), NaiveDate::from_ymd_opt(2026, 1, 1).unwrap());
    assert!(parse_date("not a date").is_err());
}

// ── RefResolver: deeper nesting ───────────────────────────────────────────────

#[test]
fn test_ref_resolver_three_levels_deep() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("20260101.1000000001.mps");
    std::fs::write(&path, "@mps{\n  @mps{\n    @task{ Deep }\n  }\n}").unwrap();

    let els = parser::parse_file(&path).unwrap();
    let r = RefResolver::new(&els);

    // @mps at depth 1 → mps-1
    // @mps at depth 2 → mps-1.1
    // @task at depth 3 → mps-1.1.1
    assert_eq!(r.to_human("20260101.1"), Some("mps-1"));
    assert_eq!(r.to_human("20260101.1.1"), Some("mps-1.1"));
    assert_eq!(r.to_human("20260101.1.1.1"), Some("mps-1.1.1"));
}

// ── Config ────────────────────────────────────────────────────────────────────

#[test]
fn test_config_loads_default_command_and_aliases() {
    use mps::config::Config;

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join(".mps_config.yaml");

    // Write a YAML that mirrors what the Ruby gem generates (symbol keys)
    std::fs::write(&path, r#"
mps_dir: /tmp/mps-test
storage_dir: /tmp/mps-test/mps
log_file: /tmp/mps-test/mps.log
git_remote: origin
git_branch: master
default_command: list
aliases:
  t: task
  n: note
"#).unwrap();

    let cfg = Config::load(&path).unwrap();
    assert_eq!(cfg.default_command, "list");
    assert_eq!(cfg.type_aliases.get("t").map(|s| s.as_str()), Some("task"));
    assert_eq!(cfg.type_aliases.get("n").map(|s| s.as_str()), Some("note"));
}

#[test]
fn test_config_defaults_when_fields_absent() {
    use mps::config::Config;

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join(".mps_config.yaml");

    // Minimal YAML — omit optional fields
    std::fs::write(&path, r#"
mps_dir: /tmp/mps-test
storage_dir: /tmp/mps-test/mps
log_file: /tmp/mps-test/mps.log
"#).unwrap();

    let cfg = Config::load(&path).unwrap();
    assert_eq!(cfg.default_command, "open");
    assert!(cfg.type_aliases.is_empty());
    assert_eq!(cfg.git_remote, "origin");
    assert_eq!(cfg.git_branch, "master");
}

#[test]
fn test_config_loads_ruby_symbol_keys() {
    use mps::config::Config;

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join(".mps_config.yaml");

    // Ruby-style YAML with symbol keys (leading colon)
    std::fs::write(&path, r#":mps_dir: /tmp/mps-test
:storage_dir: /tmp/mps-test/mps
:log_file: /tmp/mps-test/mps.log
:git_remote: origin
:git_branch: master
:default_command: list
"#).unwrap();

    let cfg = Config::load(&path).unwrap();
    assert_eq!(cfg.default_command, "list");
    assert_eq!(cfg.git_remote, "origin");
}

#[test]
fn test_config_loads_type_aliases_new_key() {
    use mps::config::Config;

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join(".mps_config.yaml");

    std::fs::write(&path, r#"
mps_dir: /tmp/mps-test
storage_dir: /tmp/mps-test/mps
log_file: /tmp/mps-test/mps.log
type_aliases:
  t: task
  c: character
"#).unwrap();

    let cfg = Config::load(&path).unwrap();
    assert_eq!(cfg.type_aliases.get("t").map(|s| s.as_str()), Some("task"));
    assert_eq!(cfg.type_aliases.get("c").map(|s| s.as_str()), Some("character"));
}

#[test]
fn test_config_loads_command_aliases() {
    use mps::config::Config;

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join(".mps_config.yaml");

    std::fs::write(&path, r#"
mps_dir: /tmp/mps-test
storage_dir: /tmp/mps-test/mps
log_file: /tmp/mps-test/mps.log
command_aliases:
  a: append
  "+": append
  l: list
"#).unwrap();

    let cfg = Config::load(&path).unwrap();
    assert_eq!(cfg.command_aliases.get("a").map(|s| s.as_str()), Some("append"));
    assert_eq!(cfg.command_aliases.get("+").map(|s| s.as_str()), Some("append"));
    assert_eq!(cfg.command_aliases.get("l").map(|s| s.as_str()), Some("list"));
}

#[test]
fn test_config_aliases_backward_compat() {
    use mps::config::Config;

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join(".mps_config.yaml");

    // Old configs used 'aliases:' key — must still load into type_aliases
    std::fs::write(&path, r#"
mps_dir: /tmp/mps-test
storage_dir: /tmp/mps-test/mps
log_file: /tmp/mps-test/mps.log
aliases:
  t: task
  n: note
  r: reminder
"#).unwrap();

    let cfg = Config::load(&path).unwrap();
    assert_eq!(cfg.type_aliases.get("t").map(|s| s.as_str()), Some("task"));
    assert_eq!(cfg.type_aliases.get("n").map(|s| s.as_str()), Some("note"));
    assert_eq!(cfg.type_aliases.get("r").map(|s| s.as_str()), Some("reminder"));
    assert!(cfg.command_aliases.is_empty());
}

// ── character element ────────────────────────────────────────────────────────

#[test]
fn test_character_append_and_parse() {
    let (_dir, store) = fixture_store();
    let date = NaiveDate::from_ymd_opt(2026, 6, 15).unwrap();
    store.append(
        "character",
        "He has done me a great favor",
        &["mahfuz-vai".to_string(), "favor".to_string()],
        &[("name", "Mahfuz Vai")],
        date,
    ).unwrap();
    let els = store.parse_date(date).unwrap();
    let el = els.values().find(|e| e.kind() == ElementKind::Character)
        .expect("character element should exist");
    if let Element::Character { data, .. } = el {
        assert_eq!(data.name.as_deref(), Some("Mahfuz Vai"));
        assert!(data.tags.contains(&"favor".to_string()));
    }
    assert!(el.body_str().contains("great favor"));
}

#[test]
fn test_character_append_no_name() {
    let (_dir, store) = fixture_store();
    let date = NaiveDate::from_ymd_opt(2026, 6, 15).unwrap();
    store.append("character", "Anonymous observation", &[], &[], date).unwrap();
    let els = store.parse_date(date).unwrap();
    let el = els.values().find(|e| e.kind() == ElementKind::Character)
        .expect("character element should exist");
    if let Element::Character { data, .. } = el {
        assert!(data.name.is_none());
    }
}

#[test]
fn test_character_type_filter_excludes_task() {
    use mps::commands::{visible, DisplayOpts};
    let (_dir, store) = fixture_store();
    let date = NaiveDate::from_ymd_opt(2026, 6, 15).unwrap();
    store.append("task",      "do the thing",  &["work".to_string()], &[], date).unwrap();
    store.append("character", "person entry",  &[], &[("name", "Someone")], date).unwrap();
    let els = store.parse_date(date).unwrap();

    let opts = DisplayOpts {
        type_filter:   Some("character".to_string()),
        tag_filter:    None,
        status_filter: None,
        name_filter:   None,
    };
    let visible_kinds: Vec<_> = els.values()
        .filter(|e| !e.is_mps_group() && !e.is_unknown() && visible(e, &opts))
        .map(|e| e.kind())
        .collect();
    assert!(visible_kinds.iter().all(|k| *k == ElementKind::Character),
        "only character elements should be visible with type=character");
    assert!(!visible_kinds.is_empty());
}

#[test]
fn test_character_tag_filter() {
    use mps::commands::{visible, DisplayOpts};
    let (_dir, store) = fixture_store();
    let date = NaiveDate::from_ymd_opt(2026, 6, 15).unwrap();
    store.append("character", "entry one", &["mahfuz-vai".to_string()], &[("name", "Mahfuz Vai")], date).unwrap();
    store.append("character", "entry two", &["other-person".to_string()], &[("name", "Other")], date).unwrap();
    let els = store.parse_date(date).unwrap();

    let opts = DisplayOpts {
        type_filter:   None,
        tag_filter:    Some("mahfuz-vai".to_string()),
        status_filter: None,
        name_filter:   None,
    };
    let matched: Vec<_> = els.values()
        .filter(|e| !e.is_mps_group() && !e.is_unknown() && visible(e, &opts))
        .collect();
    assert_eq!(matched.len(), 1);
    assert_eq!(matched[0].kind(), ElementKind::Character);
}

#[test]
fn test_character_name_filter() {
    use mps::commands::{visible, DisplayOpts};
    let (_dir, store) = fixture_store();
    let date = NaiveDate::from_ymd_opt(2026, 6, 15).unwrap();
    store.append("character", "about mahfuz", &["mahfuz-vai".to_string()], &[("name", "Mahfuz Vai")], date).unwrap();
    store.append("character", "about alice",  &["alice".to_string()],      &[("name", "Alice")],      date).unwrap();
    let els = store.parse_date(date).unwrap();

    let opts = DisplayOpts {
        type_filter:   None,
        tag_filter:    None,
        status_filter: None,
        name_filter:   Some("Mahfuz Vai".to_string()),
    };
    let matched: Vec<_> = els.values()
        .filter(|e| !e.is_mps_group() && !e.is_unknown() && visible(e, &opts))
        .collect();
    assert_eq!(matched.len(), 1);
    if let Element::Character { data, .. } = matched[0] {
        assert_eq!(data.name.as_deref(), Some("Mahfuz Vai"));
    }
}

#[test]
fn test_character_search_by_body() {
    let (_dir, store) = fixture_store();
    let date = NaiveDate::from_ymd_opt(2026, 6, 15).unwrap();
    store.append("character", "He offered me a fancy breakfast", &[], &[("name", "Mahfuz Vai")], date).unwrap();
    let results = store.search("fancy breakfast", None, None, None).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].element.kind(), ElementKind::Character);
}

#[test]
fn test_character_search_type_filter() {
    // Use a fresh store (no fixtures) so search results are fully predictable.
    let dir = tempfile::tempdir().unwrap();
    let store = Store::new(dir.path());
    let date = NaiveDate::from_ymd_opt(2026, 6, 15).unwrap();
    store.append("note",      "favor note",       &[], &[], date).unwrap();
    store.append("character", "favor character",  &[], &[("name", "Mahfuz Vai")], date).unwrap();
    let results = store.search("favor", Some("character"), None, None).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].element.kind(), ElementKind::Character);
}

#[test]
fn test_character_counted_in_store_parse() {
    let (_dir, store) = fixture_store();
    let date = NaiveDate::from_ymd_opt(2026, 6, 15).unwrap();
    store.append("character", "first",  &[], &[("name", "A")], date).unwrap();
    store.append("character", "second", &[], &[("name", "B")], date).unwrap();
    store.append("task",      "a task", &[], &[], date).unwrap();
    let els = store.parse_date(date).unwrap();
    let char_count = els.values().filter(|e| e.kind() == ElementKind::Character).count();
    assert_eq!(char_count, 2);
    let task_count = els.values().filter(|e| e.kind() == ElementKind::Task).count();
    assert_eq!(task_count, 1);
}

#[test]
fn test_character_not_excluded_from_search() {
    let (_dir, store) = fixture_store();
    let date = NaiveDate::from_ymd_opt(2026, 6, 15).unwrap();
    store.append("character", "unique phrase xyz", &[], &[("name", "Test")], date).unwrap();
    let results = store.search("unique phrase xyz", None, None, None).unwrap();
    assert_eq!(results.len(), 1);
}

#[test]
fn test_character_export_name_in_typed_attrs() {
    let (_dir, store) = fixture_store();
    let date = NaiveDate::from_ymd_opt(2026, 6, 15).unwrap();
    store.append("character", "body text", &[], &[("name", "Export Test")], date).unwrap();
    let els = store.parse_date(date).unwrap();
    let el = els.values().find(|e| e.kind() == ElementKind::Character).unwrap();
    let attrs = el.typed_attrs();
    let name_attr = attrs.iter().find(|(k, _)| k == "name");
    assert!(name_attr.is_some());
    assert_eq!(name_attr.unwrap().1, "Export Test");
}

// ── Meta + config merge ───────────────────────────────────────────────────────

#[test]
fn test_meta_shared_path_is_in_storage_dir() {
    use mps::meta::MetaShared;
    let dir = tempfile::tempdir().unwrap();
    let path = MetaShared::path(dir.path());
    assert_eq!(path, dir.path().join(".mps.meta"));
}

#[test]
fn test_meta_local_path_is_in_storage_dir() {
    use mps::meta::MetaLocal;
    let dir = tempfile::tempdir().unwrap();
    let path = MetaLocal::path(dir.path());
    assert_eq!(path, dir.path().join(".mps.local"));
}

#[test]
fn test_meta_shared_roundtrip_with_all_fields() {
    use mps::meta::MetaShared;
    use std::collections::HashMap;

    let dir = tempfile::tempdir().unwrap();
    let mut m = MetaShared::default();
    m.version = 1;
    m.config.type_aliases    = HashMap::from([("t".into(), "task".into())]);
    m.config.command_aliases = HashMap::from([("a".into(), "append".into())]);
    m.config.default_command = Some("list".into());
    m.config.custom_tags     = vec!["work".into(), "urgent".into()];
    m.config.notify.window_minutes = 10;
    m.config.notify.task_notify_at = Some("8am".into());
    m.save(dir.path()).unwrap();

    let m2 = MetaShared::load(dir.path());
    assert_eq!(m2.version, 1);
    assert_eq!(m2.config.type_aliases.get("t").map(|s| s.as_str()), Some("task"));
    assert_eq!(m2.config.command_aliases.get("a").map(|s| s.as_str()), Some("append"));
    assert_eq!(m2.config.default_command.as_deref(), Some("list"));
    assert_eq!(m2.config.custom_tags, vec!["work", "urgent"]);
    assert_eq!(m2.config.notify.window_minutes, 10);
    assert_eq!(m2.config.notify.task_notify_at.as_deref(), Some("8am"));
}

#[test]
fn test_config_merge_meta_unions_type_aliases() {
    use mps::config::{Config, NotifyConfig};
    use mps::meta::MetaConfig;
    use std::collections::HashMap;

    let dir = tempfile::tempdir().unwrap();
    let mut cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     dir.path().to_path_buf(),
        log_file:        dir.path().join("mps.log"),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::from([("t".into(), "task".into())]),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify:          NotifyConfig::default(),
    };

    let mut meta = MetaConfig::default();
    meta.type_aliases.insert("c".into(), "character".into());
    meta.type_aliases.insert("t".into(), "note".into()); // conflict — YAML wins
    meta.command_aliases.insert("a".into(), "append".into());
    meta.default_command = Some("list".into());
    meta.custom_tags = vec!["work".into()];

    cfg.merge_meta(&meta);

    assert_eq!(cfg.type_aliases.get("t").map(|s| s.as_str()), Some("task")); // YAML wins
    assert_eq!(cfg.type_aliases.get("c").map(|s| s.as_str()), Some("character")); // meta adds
    assert_eq!(cfg.command_aliases.get("a").map(|s| s.as_str()), Some("append"));
    assert_eq!(cfg.default_command, "list"); // meta wins
    assert!(cfg.custom_tags.contains(&"work".to_string()));
}

#[test]
fn test_config_merge_meta_does_not_clobber_paths() {
    use mps::config::{Config, NotifyConfig};
    use mps::meta::MetaConfig;
    use std::collections::HashMap;

    let dir = tempfile::tempdir().unwrap();
    let storage = dir.path().join("mps");
    std::fs::create_dir_all(&storage).unwrap();

    let mut cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     storage.clone(),
        log_file:        dir.path().join("mps.log"),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify:          NotifyConfig::default(),
    };

    let meta = MetaConfig::default();
    cfg.merge_meta(&meta);

    // Machine-specific paths must survive the merge unchanged.
    assert_eq!(cfg.storage_dir, storage);
    assert_eq!(cfg.git_remote, "origin");
    assert_eq!(cfg.git_branch, "master");
}

#[test]
fn test_config_merge_meta_custom_tags_deduped() {
    use mps::config::{Config, NotifyConfig};
    use mps::meta::MetaConfig;
    use std::collections::HashMap;

    let dir = tempfile::tempdir().unwrap();
    let mut cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     dir.path().to_path_buf(),
        log_file:        dir.path().join("mps.log"),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec!["work".into()],
        notify:          NotifyConfig::default(),
    };

    let mut meta = MetaConfig::default();
    meta.custom_tags = vec!["work".into(), "urgent".into()]; // "work" duplicate

    cfg.merge_meta(&meta);

    let work_count = cfg.custom_tags.iter().filter(|t| t.as_str() == "work").count();
    assert_eq!(work_count, 1, "duplicate tags must be removed");
    assert!(cfg.custom_tags.contains(&"urgent".to_string()));
}

// ── Daemon unit file template substitution ────────────────────────────────────

#[test]
fn test_daemon_service_template_has_placeholders() {
    // The service template is private — verify behaviour indirectly by checking
    // that the install path writes a non-empty file (we cannot call install in tests
    // without a real systemd, so we test the template constants via the source).
    // Instead, assert the expected substrings are present in the embedded strings.
    // We do this by checking that `mps daemon run` doesn't panic on unknown subcommand.
    use mps::config::{Config, NotifyConfig};
    use std::collections::HashMap;

    let dir = tempfile::tempdir().unwrap();
    let storage = dir.path().join("mps");
    std::fs::create_dir_all(&storage).unwrap();
    // Create a dummy log file so ensure_dirs-style checks don't fail.
    std::fs::write(dir.path().join("mps.log"), "").unwrap();

    let cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     storage,
        log_file:        dir.path().join("mps.log"),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify:          NotifyConfig::default(),
    };

    // "run" subcommand should execute notify::run which does nothing when disabled.
    let mut cfg2 = cfg.clone();
    cfg2.notify.enabled = false;
    let result = mps::commands::daemon::run(&cfg2, "run");
    assert!(result.is_ok());

    // Unknown subcommand returns an error.
    let err = mps::commands::daemon::run(&cfg, "bogus");
    assert!(err.is_err());
}

// ── time_parse integration ────────────────────────────────────────────────────

#[test]
fn test_time_parse_used_in_notify_reminder_scan() {
    use mps::time_parse::parse_time;
    use chrono::Timelike;

    // Verify the time parser handles all formats that appear in real reminder fixtures.
    let cases = [("5pm", 17, 0), ("9am", 9, 0), ("3:30pm", 15, 30), ("noon", 12, 0)];
    for (input, exp_h, exp_m) in cases {
        let t = parse_time(input).unwrap_or_else(|_| panic!("parse_time('{}') failed", input));
        assert_eq!(t.hour(),   exp_h, "hour mismatch for '{}'", input);
        assert_eq!(t.minute(), exp_m, "minute mismatch for '{}'", input);
    }
}

// ── notify dry-run (no network/display needed) ────────────────────────────────

#[test]
fn test_notify_dry_run_no_events_when_no_reminders() {
    use mps::config::{Config, NotifyConfig};
    use std::collections::HashMap;

    let dir = tempfile::tempdir().unwrap();
    let storage = dir.path().join("mps");
    std::fs::create_dir_all(&storage).unwrap();
    let log = dir.path().join("mps.log");
    std::fs::write(&log, "").unwrap();

    let cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     storage,
        log_file:        log,
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify:          NotifyConfig::default(),
    };

    // dry_run=true means no actual notify-send; no .mps.local written.
    let result = mps::commands::notify::run(&cfg, true, None, false);
    assert!(result.is_ok());
    // .mps.local must not be written in dry-run mode.
    assert!(!mps::meta::MetaLocal::path(&cfg.storage_dir).exists());
}

// ── Iteration 8: notify cooldown uses task_cooldown_minutes ──────────────────

/// Helper to build a minimal Config pointing to a temp storage dir.
fn make_notify_cfg(storage: &std::path::Path, log: &std::path::Path) -> mps::config::Config {
    use mps::config::NotifyConfig;
    mps::config::Config {
        mps_dir:         storage.parent().unwrap().to_path_buf(),
        storage_dir:     storage.to_path_buf(),
        log_file:        log.to_path_buf(),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify:          NotifyConfig::default(),
    }
}

#[test]
fn test_notify_cooldown_uses_task_cooldown_minutes() {
    // Verify that a reminder that was marked-notified within task_cooldown_minutes
    // is suppressed on the next run (dedup check).
    let dir = tempfile::tempdir().unwrap();
    let storage = dir.path().join("mps");
    std::fs::create_dir_all(&storage).unwrap();
    let log = dir.path().join("mps.log");
    std::fs::write(&log, "").unwrap();

    let cfg = make_notify_cfg(&storage, &log);

    // Pre-populate .mps.local: mark a fake epoch_ref as notified just now.
    let epoch_ref = "20260101.1700000000.1";
    let mut local = mps::meta::MetaLocal::default();
    local.mark_notified(epoch_ref);
    local.save(&storage).unwrap();

    // The cooldown default is 60 min; was_notified(60*60) must return true.
    let local2 = mps::meta::MetaLocal::load(&storage);
    assert!(local2.was_notified(epoch_ref, (cfg.notify.task_cooldown_minutes * 60) as i64),
        "reminder marked now must be within 60-minute cooldown");
    // But the old window_minutes window (5 min = 300s) would also be fresh here —
    // confirm task_cooldown_minutes (3600s) is larger than window_minutes (300s).
    assert!(cfg.notify.task_cooldown_minutes * 60 > cfg.notify.window_minutes * 60,
        "cooldown must exceed window so dedup logic is meaningful");
}

// ── Iteration 9: notify task list truncation — tested via internal unit test ──
// The NotifyEvent type is private; truncation is verified in src/commands/notify.rs tests.

// ── notify_open_tasks merge semantics ────────────────────────────────────────

#[test]
fn test_merge_meta_notify_open_tasks_false_in_meta_wins() {
    use mps::config::{Config, NotifyConfig};
    use mps::meta::MetaConfig;

    let dir = tempfile::tempdir().unwrap();
    let mut cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     dir.path().to_path_buf(),
        log_file:        dir.path().join("mps.log"),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify:          NotifyConfig::default(), // notify_open_tasks=true
    };

    let mut meta = MetaConfig::default();
    meta.notify.notify_open_tasks = false;

    cfg.merge_meta(&meta);
    assert!(!cfg.notify.notify_open_tasks,
        "meta.notify_open_tasks=false must disable open task briefing");
}

#[test]
fn test_merge_meta_open_task_tags_from_meta() {
    use mps::config::{Config, NotifyConfig};
    use mps::meta::MetaConfig;

    let dir = tempfile::tempdir().unwrap();
    let mut cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     dir.path().to_path_buf(),
        log_file:        dir.path().join("mps.log"),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify:          NotifyConfig::default(), // open_task_tags empty
    };

    let mut meta = MetaConfig::default();
    meta.notify.open_task_tags = vec!["urgent".into(), "work".into()];

    cfg.merge_meta(&meta);
    assert_eq!(cfg.notify.open_task_tags, vec!["urgent", "work"],
        "meta open_task_tags must be applied when non-empty");
}

#[test]
fn test_merge_meta_yaml_open_task_tags_not_clobbered_by_empty_meta() {
    use mps::config::{Config, NotifyConfig};
    use mps::meta::MetaConfig;

    let dir = tempfile::tempdir().unwrap();
    let mut cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     dir.path().to_path_buf(),
        log_file:        dir.path().join("mps.log"),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify: NotifyConfig {
            open_task_tags: vec!["personal".into()],
            ..NotifyConfig::default()
        },
    };

    // Meta has empty open_task_tags (default).
    let meta = MetaConfig::default();
    cfg.merge_meta(&meta);

    // YAML's tags must survive when meta has empty list.
    assert_eq!(cfg.notify.open_task_tags, vec!["personal"],
        "YAML open_task_tags must survive when meta is empty");
}

// ── Iteration 10: field-by-field notify merge — YAML window survives ──────────

#[test]
fn test_merge_meta_notify_yaml_window_minutes_survives() {
    use mps::config::{Config, NotifyConfig};
    use mps::meta::MetaConfig;

    let dir = tempfile::tempdir().unwrap();
    let mut cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     dir.path().to_path_buf(),
        log_file:        dir.path().join("mps.log"),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify: NotifyConfig {
            window_minutes: 10, // YAML sets a non-default window
            ..NotifyConfig::default()
        },
    };

    // Meta only sets task_notify_at (non-default), nothing else.
    let mut meta = MetaConfig::default();
    meta.notify.task_notify_at = Some("9am".into());

    cfg.merge_meta(&meta);

    // window_minutes from YAML must survive — meta's value is the default (5).
    assert_eq!(cfg.notify.window_minutes, 10,
        "YAML window_minutes must survive when meta has default value");
    // task_notify_at from meta must be applied.
    assert_eq!(cfg.notify.task_notify_at.as_deref(), Some("9am"),
        "meta task_notify_at must be merged");
}

#[test]
fn test_merge_meta_notify_disabled_in_meta_wins() {
    use mps::config::{Config, NotifyConfig};
    use mps::meta::MetaConfig;

    let dir = tempfile::tempdir().unwrap();
    let mut cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     dir.path().to_path_buf(),
        log_file:        dir.path().join("mps.log"),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify:          NotifyConfig::default(), // enabled=true
    };

    let mut meta = MetaConfig::default();
    meta.notify.enabled = false; // meta turns off notifications

    cfg.merge_meta(&meta);

    assert!(!cfg.notify.enabled, "meta.notify.enabled=false must override YAML enabled=true");
}

#[test]
fn test_merge_meta_notify_overdue_days_from_meta() {
    use mps::config::{Config, NotifyConfig};
    use mps::meta::MetaConfig;

    let dir = tempfile::tempdir().unwrap();
    let mut cfg = Config {
        mps_dir:         dir.path().to_path_buf(),
        storage_dir:     dir.path().to_path_buf(),
        log_file:        dir.path().join("mps.log"),
        git_remote:      "origin".into(),
        git_branch:      "master".into(),
        default_command: "open".into(),
        type_aliases:    HashMap::new(),
        command_aliases: HashMap::new(),
        custom_tags:     vec![],
        notify:          NotifyConfig::default(), // overdue_days=7
    };

    let mut meta = MetaConfig::default();
    meta.notify.overdue_days = 14; // meta wants 2 weeks

    cfg.merge_meta(&meta);

    assert_eq!(cfg.notify.overdue_days, 14,
        "non-default meta overdue_days must override YAML default");
}

// ── Iteration 15: open_task_tags filter ──────────────────────────────────────

#[test]
fn test_notify_enabled_false_returns_immediately() {
    let dir = tempfile::tempdir().unwrap();
    let storage = dir.path().join("mps");
    std::fs::create_dir_all(&storage).unwrap();
    let log = dir.path().join("mps.log");
    std::fs::write(&log, "").unwrap();

    let mut cfg = make_notify_cfg(&storage, &log);
    cfg.notify.enabled = false;

    // Append a task so there would be something to notify about if enabled.
    let store = mps::store::Store::new(&storage);
    let today = chrono::Local::now().date_naive();
    store.append("task", "Important task", &[], &[], today).unwrap();

    // Should succeed quietly with no notifications.
    let result = mps::commands::notify::run(&cfg, true, None, false);
    assert!(result.is_ok());
}

#[test]
fn test_notify_open_tasks_tag_filter_excludes_untagged() {
    let dir = tempfile::tempdir().unwrap();
    let storage = dir.path().join("mps");
    std::fs::create_dir_all(&storage).unwrap();
    let log = dir.path().join("mps.log");
    std::fs::write(&log, "").unwrap();

    let mut cfg = make_notify_cfg(&storage, &log);
    // Only tasks tagged "urgent" should appear.
    cfg.notify.open_task_tags = vec!["urgent".into()];
    // Set task_notify_at to "now" so the briefing fires (within window).
    // Since we use dry_run, we just verify the function succeeds.
    cfg.notify.task_notify_at = Some("9am".into());
    cfg.notify.notify_open_tasks = true;

    let store = mps::store::Store::new(&storage);
    let today = chrono::Local::now().date_naive();
    // Append a task without the "urgent" tag.
    store.append("task", "Non-urgent task", &[], &[], today).unwrap();

    let result = mps::commands::notify::run(&cfg, true, None, false);
    assert!(result.is_ok(), "notify with tag filter must not error");
}