iwcore 0.1.24

IntelliWallet Core - Password manager library with AES-256 encryption
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
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
//! Integration tests for iwcore
//!
//! These tests use the obfuscated test database in testdata/nswallet.dat

use std::path::PathBuf;
use std::fs;
use iwcore::{Wallet, BackupManager};
use tempfile::TempDir;

/// Hardcoded password for the test database
const TEST_PASSWORD: &str = "KuiperBelt30au";

/// Get the path to the test database
fn get_test_db_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("testdata")
        .join("nswallet.dat")
}

/// Copy test database to temp directory and return wallet
fn setup_test_wallet() -> (Wallet, TempDir) {
    let source_db = get_test_db_path();
    assert!(source_db.exists(), "Test database not found: {:?}", source_db);

    let temp_dir = TempDir::new().unwrap();
    let dest_db = temp_dir.path().join("nswallet.dat");
    fs::copy(&source_db, &dest_db).expect("Failed to copy test database");

    let wallet = Wallet::open(temp_dir.path()).expect("Failed to open wallet");
    (wallet, temp_dir)
}

#[test]
fn test_open_and_unlock() {
    let (mut wallet, _temp_dir) = setup_test_wallet();

    // Verify unlock works with correct password
    let unlock_result = wallet.unlock(TEST_PASSWORD).unwrap();
    assert!(unlock_result, "Failed to unlock with correct password");
    assert!(wallet.is_unlocked());

    wallet.close();
}

#[test]
fn test_wrong_password() {
    let (mut wallet, _temp_dir) = setup_test_wallet();

    // Wrong password should fail
    let unlock_result = wallet.unlock("WrongPassword123!").unwrap();
    assert!(!unlock_result, "Should not unlock with wrong password");
    assert!(!wallet.is_unlocked());

    wallet.close();
}

#[test]
fn test_read_items() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Get all items
    let items = wallet.get_items().unwrap();
    println!("Found {} items", items.len());

    // There should be items (including root)
    assert!(!items.is_empty(), "Database should contain items");

    // Count folders and items
    let folder_count = items.iter().filter(|i| i.folder && i.item_id != "__ROOT__").count();
    let item_count = items.iter().filter(|i| !i.folder).count();
    println!("  Folders: {}, Items: {}", folder_count, item_count);

    wallet.close();
}

#[test]
fn test_read_fields() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Get all fields
    let fields = wallet.get_fields().unwrap();
    println!("Found {} fields", fields.len());

    // Should have fields
    assert!(!fields.is_empty(), "Database should contain fields");

    // Count by type
    let mut type_counts: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
    for field in fields {
        *type_counts.entry(field.field_type.clone()).or_default() += 1;
    }

    for (field_type, count) in &type_counts {
        println!("  {}: {}", field_type, count);
    }

    wallet.close();
}

#[test]
fn test_properties() {
    let (wallet, _temp_dir) = setup_test_wallet();

    // Get properties (doesn't require unlock)
    let props = wallet.get_properties().unwrap();

    println!("Database properties:");
    println!("  Version: {}", props.version);
    println!("  Language: {}", props.lang);
    println!("  Encryption count: {}", props.encryption_count);

    // Verify version is current (matches iwcore::DB_VERSION).
    assert_eq!(props.version, iwcore::DB_VERSION);
}

#[test]
fn test_labels() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Get all labels
    let labels = wallet.get_labels().unwrap();
    println!("Found {} labels", labels.len());

    // Should have at least system labels
    assert!(labels.len() >= 19, "Should have at least 19 system labels");

    // Count system vs custom
    let system_count = labels.iter().filter(|l| l.system).count();
    let custom_count = labels.iter().filter(|l| !l.system).count();
    println!("  System: {}, Custom: {}", system_count, custom_count);

    wallet.close();
}

#[test]
fn test_search() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Search for common pattern (items are named "Item 001", "Folder 001", etc.)
    let results = wallet.search("Item").unwrap();
    println!("Search for 'Item' found {} results", results.len());

    assert!(!results.is_empty(), "Search should find items");

    wallet.close();
}

#[test]
fn test_get_items_by_parent() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Get root items
    let root_items = wallet.get_items_by_parent("__ROOT__").unwrap();
    println!("Found {} items at root level", root_items.len());

    assert!(!root_items.is_empty(), "Should have items at root level");

    wallet.close();
}

#[test]
fn test_get_fields_by_item() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Find first non-folder item
    let items = wallet.get_items().unwrap().to_vec();
    let first_item = items.iter().find(|i| !i.folder);

    if let Some(item) = first_item {
        let fields = wallet.get_fields_by_item(&item.item_id).unwrap();
        println!("Item '{}' has {} fields", item.name, fields.len());
    }

    wallet.close();
}

#[test]
fn test_full_backup_restore_cycle() {
    // Create a new wallet
    let temp_dir = TempDir::new().unwrap();
    let wallet_dir = temp_dir.path().join("wallet");
    let backup_dir = temp_dir.path().join("backups");

    let mut wallet = Wallet::create(&wallet_dir, "TestPassword123!", "en").unwrap();

    // Add some items
    let folder_id = wallet.add_item("Test Folder", "folder", true, None).unwrap();
    let item_id = wallet.add_item("Test Item", "document", false, Some(&folder_id)).unwrap();
    wallet.add_field(&item_id, "MAIL", "test@example.com", None).unwrap();
    wallet.add_field(&item_id, "PASS", "secretpassword", None).unwrap();

    // Create backup
    let backup_mgr = BackupManager::new(&backup_dir);
    let backup_path = backup_mgr.create_backup(wallet.database().unwrap(), true).unwrap();

    // Close wallet
    wallet.close();

    // Restore to new location
    let restore_dir = temp_dir.path().join("restored");
    backup_mgr.extract_backup(&backup_path, &restore_dir).unwrap();

    // Open restored wallet
    let mut restored = Wallet::open(&restore_dir).unwrap();
    assert!(restored.unlock("TestPassword123!").unwrap());

    // Get items and clone to avoid borrow issues
    let items = restored.get_items().unwrap().to_vec();
    let folder = items.iter().find(|i| i.name == "Test Folder").unwrap();
    assert!(folder.folder);

    let item = items.iter().find(|i| i.name == "Test Item").unwrap();
    assert!(!item.folder);

    // Get item_id before getting fields
    let item_id = item.item_id.clone();

    // Verify fields
    let fields = restored.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields.len(), 2);

    let email_field = fields.iter().find(|f| f.field_type == "MAIL").unwrap();
    assert_eq!(email_field.value, "test@example.com");

    let pass_field = fields.iter().find(|f| f.field_type == "PASS").unwrap();
    assert_eq!(pass_field.value, "secretpassword");

    restored.close();
}

#[test]
fn test_modify_and_verify() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Add a new item
    let new_item_id = wallet.add_item("New Test Item", "document", false, None).unwrap();

    // Add fields to it
    wallet.add_field(&new_item_id, "USER", "testuser", None).unwrap();
    wallet.add_field(&new_item_id, "PASS", "testpass", None).unwrap();

    // Verify it exists
    let items = wallet.get_items().unwrap();
    let new_item = items.iter().find(|i| i.item_id == new_item_id);
    assert!(new_item.is_some(), "New item should exist");
    assert_eq!(new_item.unwrap().name, "New Test Item");

    // Verify fields
    let fields = wallet.get_fields_by_item(&new_item_id).unwrap();
    assert_eq!(fields.len(), 2);

    // Delete the item
    wallet.delete_item(&new_item_id).unwrap();

    // Verify it's gone (soft deleted, won't appear in get_items)
    let items_after = wallet.get_items().unwrap();
    let deleted_item = items_after.iter().find(|i| i.item_id == new_item_id);
    assert!(deleted_item.is_none(), "Deleted item should not appear");

    wallet.close();
}

#[test]
fn test_change_password() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Get initial item count
    let items_before = wallet.get_items().unwrap().to_vec();
    let fields_before = wallet.get_fields().unwrap().to_vec();

    // Change password
    let new_password = "NewSecurePassword123!";
    assert!(wallet.change_password(new_password).unwrap());

    // Lock and try old password
    wallet.lock();
    assert!(!wallet.unlock(TEST_PASSWORD).unwrap(), "Old password should not work");

    // Unlock with new password
    assert!(wallet.unlock(new_password).unwrap(), "New password should work");

    // Verify all data is still accessible
    let items_after = wallet.get_items().unwrap().len();
    let fields_after = wallet.get_fields().unwrap().len();
    assert_eq!(items_before.len(), items_after, "Item count should match");
    assert_eq!(fields_before.len(), fields_after, "Field count should match");

    wallet.close();
}

#[test]
fn test_update_item_name() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item
    let item_id = wallet.add_item("Original Name", "document", false, None).unwrap();

    // Update name
    wallet.update_item_name(&item_id, "Updated Name").unwrap();

    // Verify
    let item = wallet.get_item(&item_id).unwrap().unwrap();
    assert_eq!(item.name, "Updated Name");

    wallet.close();
}

#[test]
fn test_update_item_icon() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item
    let item_id = wallet.add_item("Test Item", "document", false, None).unwrap();

    // Update icon
    wallet.update_item_icon(&item_id, "folder").unwrap();

    // Verify
    let item = wallet.get_item(&item_id).unwrap().unwrap();
    assert_eq!(item.icon, "folder");

    wallet.close();
}

#[test]
fn test_update_field_value() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item with field
    let item_id = wallet.add_item("Test Item", "document", false, None).unwrap();
    let field_id = wallet.add_field(&item_id, "NOTE", "Original value", None).unwrap();

    // Update field — returns new field_id
    let new_field_id = wallet.update_field(&field_id, "Updated value", None).unwrap();
    assert_ne!(new_field_id, field_id);

    // New field has updated value
    let fields = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields.len(), 1);
    assert_eq!(fields[0].field_id, new_field_id);
    assert_eq!(fields[0].value, "Updated value");

    // Old value in deleted pool
    let deleted = wallet.get_deleted_fields().unwrap();
    let old = deleted.iter().find(|f| f.field_id == field_id).unwrap();
    assert_eq!(old.value, "Original value");

    wallet.close();
}

#[test]
fn test_delete_field() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item with field
    let item_id = wallet.add_item("Test Item", "document", false, None).unwrap();
    let field_id = wallet.add_field(&item_id, "PASS", "secret", None).unwrap();

    // Verify field exists
    let fields = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields.len(), 1);

    // Delete field
    wallet.delete_field(&item_id, &field_id).unwrap();

    // Verify deleted
    let fields_after = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields_after.len(), 0);

    wallet.close();
}

#[test]
fn test_copy_item() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item with fields
    let item_id = wallet.add_item("Original Item", "document", false, None).unwrap();
    wallet.add_field(&item_id, "MAIL", "test@example.com", None).unwrap();
    wallet.add_field(&item_id, "PASS", "password123", None).unwrap();

    // Copy item
    let copy_id = wallet.copy_item(&item_id).unwrap();

    // Verify copy
    let copy = wallet.get_item(&copy_id).unwrap().unwrap();
    assert_eq!(copy.name, "Copy of Original Item");

    // Verify fields copied
    let copy_fields = wallet.get_fields_by_item(&copy_id).unwrap();
    assert_eq!(copy_fields.len(), 2);

    wallet.close();
}

#[test]
fn test_copy_folder() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create folder
    let folder_id = wallet.add_item("Original Folder", "folder", true, None).unwrap();

    // Copy folder
    let copy_id = wallet.copy_item(&folder_id).unwrap();

    // Verify
    let copy = wallet.get_item(&copy_id).unwrap().unwrap();
    assert_eq!(copy.name, "Copy of Original Folder");
    assert!(copy.folder);

    wallet.close();
}

#[test]
fn test_move_item() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create folder and item
    let folder_id = wallet.add_item("Target Folder", "folder", true, None).unwrap();
    let item_id = wallet.add_item("Item to Move", "document", false, None).unwrap();

    // Move item to folder
    wallet.move_item(&item_id, &folder_id).unwrap();

    // Verify
    let item = wallet.get_item(&item_id).unwrap().unwrap();
    assert_eq!(item.parent_id.as_deref(), Some(folder_id.as_str()));

    wallet.close();
}

#[test]
fn test_copy_field() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create two items
    let item1_id = wallet.add_item("Item 1", "document", false, None).unwrap();
    let item2_id = wallet.add_item("Item 2", "document", false, None).unwrap();
    let field_id = wallet.add_field(&item1_id, "MAIL", "copy@test.com", None).unwrap();

    // Copy field to item2
    wallet.copy_field(&item1_id, &field_id, &item2_id).unwrap();

    // Verify original still exists
    let fields1 = wallet.get_fields_by_item(&item1_id).unwrap();
    assert_eq!(fields1.len(), 1);

    // Verify copy exists
    let fields2 = wallet.get_fields_by_item(&item2_id).unwrap();
    assert_eq!(fields2.len(), 1);
    assert_eq!(fields2[0].value, "copy@test.com");

    wallet.close();
}

#[test]
fn test_move_field() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create two items
    let item1_id = wallet.add_item("Item 1", "document", false, None).unwrap();
    let item2_id = wallet.add_item("Item 2", "document", false, None).unwrap();
    let field_id = wallet.add_field(&item1_id, "MAIL", "move@test.com", None).unwrap();

    // Move field to item2
    wallet.move_field(&item1_id, &field_id, &item2_id).unwrap();

    // Verify gone from item1
    let fields1 = wallet.get_fields_by_item(&item1_id).unwrap();
    assert_eq!(fields1.len(), 0);

    // Verify in item2
    let fields2 = wallet.get_fields_by_item(&item2_id).unwrap();
    assert_eq!(fields2.len(), 1);
    assert_eq!(fields2[0].value, "move@test.com");

    wallet.close();
}

#[test]
fn test_create_delete_label() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let initial_count = wallet.get_labels().unwrap().len();

    // Create custom label
    let label_id = wallet.add_label("Custom Label", "labelcalendar", "date").unwrap();

    // Verify created
    let labels = wallet.get_labels().unwrap();
    assert_eq!(labels.len(), initial_count + 1);
    let label = labels.iter().find(|l| l.field_type == label_id).unwrap();
    assert_eq!(label.name, "Custom Label");
    assert!(!label.system);

    // Delete label
    wallet.delete_label(&label_id).unwrap();

    // Verify deleted
    let labels_after = wallet.get_labels().unwrap();
    assert_eq!(labels_after.len(), initial_count);

    wallet.close();
}

#[test]
fn test_update_label() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create label
    let label_id = wallet.add_label("Original Label", "labelcalendar", "text").unwrap();

    // Update name
    wallet.update_label_name(&label_id, "Renamed Label").unwrap();

    // Update icon
    wallet.update_label_icon(&label_id, "labellink").unwrap();

    // Verify
    let labels = wallet.get_labels().unwrap();
    let label = labels.iter().find(|l| l.field_type == label_id).unwrap();
    assert_eq!(label.name, "Renamed Label");
    assert_eq!(label.icon, "labellink");

    wallet.close();
}

#[test]
fn test_cyrillic_data() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item with Cyrillic name
    let cyrillic_name = "Тестовый элемент 123";
    let item_id = wallet.add_item(cyrillic_name, "document", false, None).unwrap();

    // Add field with Cyrillic value
    let cyrillic_value = "Пароль: секретный!";
    wallet.add_field(&item_id, "NOTE", cyrillic_value, None).unwrap();

    // Verify
    let item = wallet.get_item(&item_id).unwrap().unwrap();
    assert_eq!(item.name, cyrillic_name);

    let fields = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields[0].value, cyrillic_value);

    wallet.close();
}

#[test]
fn test_special_characters() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item with special characters
    let special_name = "Test !@#$%^&*()_+-=[]{}|;':\",./<>?";
    let item_id = wallet.add_item(special_name, "document", false, None).unwrap();

    // Verify
    let item = wallet.get_item(&item_id).unwrap().unwrap();
    assert_eq!(item.name, special_name);

    wallet.close();
}

#[test]
fn test_reopen_after_modifications() {
    let (mut wallet, temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Add item
    let item_id = wallet.add_item("Persistent Item", "document", false, None).unwrap();
    wallet.add_field(&item_id, "MAIL", "persist@test.com", None).unwrap();

    // Close wallet
    wallet.close();

    // Reopen same database
    let mut wallet2 = iwcore::Wallet::open(temp_dir.path()).unwrap();
    wallet2.unlock(TEST_PASSWORD).unwrap();

    // Verify data persisted
    let item = wallet2.get_item(&item_id).unwrap().unwrap();
    assert_eq!(item.name, "Persistent Item");

    let fields = wallet2.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields.len(), 1);
    assert_eq!(fields[0].value, "persist@test.com");

    wallet2.close();
}

#[test]
fn test_search_by_item_name() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create items with searchable names
    wallet.add_item("Banking Account", "document", false, None).unwrap();
    wallet.add_item("Email Login", "document", false, None).unwrap();
    wallet.add_item("Social Media", "document", false, None).unwrap();

    // Search for "Bank"
    let results = wallet.search("Bank").unwrap();
    assert!(!results.is_empty(), "Should find 'Banking Account'");
    assert!(results.iter().any(|r| r.item.name.contains("Bank")));

    // Search for "Login"
    let results2 = wallet.search("Login").unwrap();
    assert!(!results2.is_empty(), "Should find 'Email Login'");

    wallet.close();
}

#[test]
fn test_search_by_field_value() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item with searchable field values
    let item_id = wallet.add_item("My Account", "document", false, None).unwrap();
    wallet.add_field(&item_id, "MAIL", "john.doe@example.com", None).unwrap();
    wallet.add_field(&item_id, "USER", "johndoe123", None).unwrap();

    // Search for email
    let results = wallet.search("john.doe").unwrap();
    assert!(!results.is_empty(), "Should find by email");

    // Search for username
    let results2 = wallet.search("johndoe").unwrap();
    assert!(!results2.is_empty(), "Should find by username");

    wallet.close();
}

#[test]
fn test_search_case_insensitive() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item
    let item_id = wallet.add_item("TestItem", "document", false, None).unwrap();
    wallet.add_field(&item_id, "NOTE", "SecretValue", None).unwrap();

    // Search with different cases
    let results_lower = wallet.search("testitem").unwrap();
    let results_upper = wallet.search("TESTITEM").unwrap();
    let results_mixed = wallet.search("TeStItEm").unwrap();

    assert!(!results_lower.is_empty(), "Should find with lowercase");
    assert!(!results_upper.is_empty(), "Should find with uppercase");
    assert!(!results_mixed.is_empty(), "Should find with mixed case");

    wallet.close();
}

#[test]
fn test_search_no_results() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Search for non-existent term
    let results = wallet.search("xyznonexistent123").unwrap();
    assert!(results.is_empty(), "Should return empty for non-existent term");

    wallet.close();
}

#[test]
fn test_search_cyrillic() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item with Cyrillic
    let item_id = wallet.add_item("Банковский счёт", "document", false, None).unwrap();
    wallet.add_field(&item_id, "NOTE", "Секретный пароль", None).unwrap();

    // Search Cyrillic
    let results = wallet.search("Банк").unwrap();
    assert!(!results.is_empty(), "Should find Cyrillic item name");

    let results2 = wallet.search("Секрет").unwrap();
    assert!(!results2.is_empty(), "Should find Cyrillic field value");

    wallet.close();
}

// =========================================================================
// Password Generator Tests
// =========================================================================

#[test]
fn test_password_generator_basic() {
    use iwcore::{generate_password, PasswordOptions};

    let options = PasswordOptions {
        lowercase: true,
        uppercase: true,
        digits: true,
        special: false,
        avoid_ambiguous: false,
        length: 16,
    };

    let password = generate_password(&options);
    assert_eq!(password.len(), 16);
    assert!(password.chars().all(|c| c.is_ascii_alphanumeric()));
}

#[test]
fn test_password_generator_all_types() {
    use iwcore::{generate_password, PasswordOptions};

    let options = PasswordOptions {
        lowercase: true,
        uppercase: true,
        digits: true,
        special: true,
        avoid_ambiguous: false,
        length: 32,
    };

    let password = generate_password(&options);
    assert_eq!(password.len(), 32);
}

#[test]
fn test_password_generator_lowercase_only() {
    use iwcore::{generate_password, PasswordOptions};

    let options = PasswordOptions {
        lowercase: true,
        uppercase: false,
        digits: false,
        special: false,
        avoid_ambiguous: false,
        length: 20,
    };

    let password = generate_password(&options);
    assert_eq!(password.len(), 20);
    assert!(password.chars().all(|c| c.is_ascii_lowercase()));
}

#[test]
fn test_password_generator_digits_only() {
    use iwcore::{generate_password, PasswordOptions};

    let options = PasswordOptions {
        lowercase: false,
        uppercase: false,
        digits: true,
        special: false,
        avoid_ambiguous: false,
        length: 10,
    };

    let password = generate_password(&options);
    assert_eq!(password.len(), 10);
    assert!(password.chars().all(|c| c.is_ascii_digit()));
}

#[test]
fn test_password_generator_uniqueness() {
    use iwcore::{generate_password, PasswordOptions};

    let options = PasswordOptions::default();

    // Generate multiple passwords and ensure they're different
    let p1 = generate_password(&options);
    let p2 = generate_password(&options);
    let p3 = generate_password(&options);

    // With 16 chars from 62+ char pool, collision probability is negligible
    assert_ne!(p1, p2);
    assert_ne!(p2, p3);
    assert_ne!(p1, p3);
}

#[test]
fn test_clever_password_pattern() {
    use iwcore::generate_clever_password;

    // Pattern: uppercase + lowercase + digits
    let password = generate_clever_password("Aaaa0000");
    assert_eq!(password.len(), 8);

    let chars: Vec<char> = password.chars().collect();
    assert!(chars[0].is_ascii_uppercase());
    assert!(chars[1].is_ascii_lowercase());
    assert!(chars[2].is_ascii_lowercase());
    assert!(chars[3].is_ascii_lowercase());
    assert!(chars[4].is_ascii_digit());
    assert!(chars[5].is_ascii_digit());
    assert!(chars[6].is_ascii_digit());
    assert!(chars[7].is_ascii_digit());
}

#[test]
fn test_clever_password_mixed_pattern() {
    use iwcore::generate_clever_password;

    // More complex pattern
    let password = generate_clever_password("Aa0@Aa0@");
    assert_eq!(password.len(), 8);
}

// =========================================================================
// Export Types Tests
// =========================================================================

#[test]
fn test_export_item_type() {
    use iwcore::ExportItemType;

    assert_eq!(ExportItemType::Item.to_string(), "Item");
    assert_eq!(ExportItemType::Folder.to_string(), "Folder");
    assert_eq!(ExportItemType::Field.to_string(), "Field");
}

#[test]
fn test_pdf_item_model_creation() {
    use iwcore::{PDFItemModel, ExportItemType};

    let model = PDFItemModel::new("Test Item", "document", ExportItemType::Item, "/Banking/");
    assert_eq!(model.name, "Test Item");
    assert_eq!(model.image, "document");
    assert_eq!(model.item_type, ExportItemType::Item);
    assert_eq!(model.path, "/Banking/");
}

#[test]
fn test_pdf_item_model_helpers() {
    use iwcore::PDFItemModel;

    let item = PDFItemModel::item("My Item", "document", "/path/");
    assert!(item.is_item());
    assert!(!item.is_folder());
    assert!(!item.is_field());

    let folder = PDFItemModel::folder("My Folder", "folder", "/");
    assert!(!folder.is_item());
    assert!(folder.is_folder());
    assert!(!folder.is_field());

    let field = PDFItemModel::field("Email", "mail", "/Banking/Visa/");
    assert!(!field.is_item());
    assert!(!field.is_folder());
    assert!(field.is_field());
}

#[test]
fn test_pdf_item_model_serialization() {
    use iwcore::PDFItemModel;

    let model = PDFItemModel::item("Test", "icon", "/path/");
    let json = serde_json::to_string(&model).unwrap();

    assert!(json.contains("\"name\":\"Test\""));
    assert!(json.contains("\"image\":\"icon\""));
    assert!(json.contains("\"item_type\":\"Item\""));
    assert!(json.contains("\"path\":\"/path/\""));

    // Deserialize back
    let deserialized: PDFItemModel = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized.name, "Test");
    assert!(deserialized.is_item());
}

// =========================================================================
// Search Edge Cases Tests
// =========================================================================

#[test]
fn test_search_minimum_length() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create items with short names
    wallet.add_item("A", "document", false, None).unwrap();
    wallet.add_item("AB", "document", false, None).unwrap();

    // Search with phrase shorter than minimum (2 chars) should return empty
    let results = wallet.search("A").unwrap();
    assert!(results.is_empty(), "Search with <2 chars should return empty");

    // Search with exactly 2 chars should work and match the AB item
    let results2 = wallet.search("AB").unwrap();
    assert!(!results2.is_empty(), "Search with 2 chars should return matches");

    wallet.close();
}

#[test]
fn test_search_excludes_folders_from_name_match() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create folder and item with same name pattern
    wallet.add_item("Banking Folder", "folder", true, None).unwrap();
    wallet.add_item("Banking Account", "document", false, None).unwrap();

    // Search should only match the item, not the folder
    let results = wallet.search("Banking").unwrap();

    // Count matches by type
    let folder_matches = results.iter().filter(|r| r.item.folder).count();
    let item_matches = results.iter().filter(|r| !r.item.folder).count();

    assert_eq!(folder_matches, 0, "Folders should not be matched by name");
    assert!(item_matches >= 1, "Items should be matched by name");

    wallet.close();
}

// =========================================================================
// Gap #1: Undelete Items & Fields
// =========================================================================

#[test]
fn test_undelete_item() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let item_id = wallet.add_item("Recoverable Item", "document", false, None).unwrap();
    wallet.add_field(&item_id, "MAIL", "recover@test.com", None).unwrap();

    // Delete (cascades to fields)
    wallet.delete_item(&item_id).unwrap();
    assert!(wallet.get_item(&item_id).unwrap().is_none());

    // Undelete item only
    wallet.undelete_item(&item_id).unwrap();

    // Verify item is restored
    let item = wallet.get_item(&item_id).unwrap().unwrap();
    assert_eq!(item.name, "Recoverable Item");
    assert!(!item.deleted);

    // Fields stay deleted after item undelete (must be individually restored)
    let fields = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields.len(), 0, "Fields should stay deleted after item undelete");

    // Undelete field individually
    let deleted_fields = wallet.get_deleted_fields().unwrap();
    let field = deleted_fields.iter().find(|f| f.item_id == item_id).unwrap();
    wallet.undelete_field(&item_id, &field.field_id).unwrap();

    let restored_fields = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(restored_fields.len(), 1);
    assert_eq!(restored_fields[0].value, "recover@test.com");

    wallet.close();
}

#[test]
fn test_database_stats() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Get stats on the legacy test database
    let stats = wallet.get_database_stats().unwrap();

    // Basic sanity checks on the legacy database
    assert!(stats.total_labels >= 19, "Should have at least 19 system labels");
    assert!(stats.file_size_bytes > 0, "File size should be positive");

    // Add some items and verify counts change
    let item_id = wallet.add_item("Stats Test", "document", false, None).unwrap();
    wallet.add_field(&item_id, "MAIL", "stats@test.com", None).unwrap();

    let stats2 = wallet.get_database_stats().unwrap();
    assert_eq!(stats2.total_items, stats.total_items + 1);
    assert_eq!(stats2.total_fields, stats.total_fields + 1);

    // Delete and check deleted counts
    wallet.delete_item(&item_id).unwrap();
    let stats3 = wallet.get_database_stats().unwrap();
    assert_eq!(stats3.deleted_items, stats.deleted_items + 1);
    assert_eq!(stats3.deleted_fields, stats.deleted_fields + 1); // cascade-deleted field

    wallet.close();
}

#[test]
fn test_undelete_field() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let item_id = wallet.add_item("Item", "document", false, None).unwrap();
    let field_id = wallet.add_field(&item_id, "PASS", "secret_value", None).unwrap();

    // Delete field
    wallet.delete_field(&item_id, &field_id).unwrap();
    let fields = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields.len(), 0);

    // Undelete field
    wallet.undelete_field(&item_id, &field_id).unwrap();

    // Verify restored with correct data
    let fields = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields.len(), 1);
    assert_eq!(fields[0].value, "secret_value");
    assert_eq!(fields[0].field_type, "PASS");

    wallet.close();
}

#[test]
fn test_undelete_item_not_found() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let result = wallet.undelete_item("NONEXISTENT");
    assert!(result.is_err());

    wallet.close();
}

#[test]
fn test_undelete_active_item_errors() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let item_id = wallet.add_item("Active Item", "document", false, None).unwrap();

    // Undelete an item that's not deleted should fail
    let result = wallet.undelete_item(&item_id);
    assert!(result.is_err());

    wallet.close();
}

#[test]
fn test_undelete_multiple_fields() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let item_id = wallet.add_item("Item", "document", false, None).unwrap();
    let f1 = wallet.add_field(&item_id, "MAIL", "a@test.com", None).unwrap();
    let f2 = wallet.add_field(&item_id, "PASS", "pass1", None).unwrap();
    let f3 = wallet.add_field(&item_id, "NOTE", "note1", None).unwrap();

    // Delete all
    wallet.delete_field(&item_id, &f1).unwrap();
    wallet.delete_field(&item_id, &f2).unwrap();
    wallet.delete_field(&item_id, &f3).unwrap();
    assert_eq!(wallet.get_fields_by_item(&item_id).unwrap().len(), 0);

    // Undelete two
    wallet.undelete_field(&item_id, &f1).unwrap();
    wallet.undelete_field(&item_id, &f3).unwrap();

    let fields = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields.len(), 2);
    let values: Vec<&str> = fields.iter().map(|f| f.value.as_str()).collect();
    assert!(values.contains(&"a@test.com"));
    assert!(values.contains(&"note1"));

    // f2 should still be in deleted list
    let deleted = wallet.get_deleted_fields().unwrap();
    assert!(deleted.iter().any(|f| f.field_id == f2));

    wallet.close();
}

// =========================================================================
// Gap #2: get_deleted_items() / get_deleted_fields()
// =========================================================================

#[test]
fn test_get_deleted_items_list() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create and delete items
    let id1 = wallet.add_item("Deleted A", "document", false, None).unwrap();
    let id2 = wallet.add_item("Deleted B", "document", false, None).unwrap();
    wallet.add_item("Keep Active", "document", false, None).unwrap();

    wallet.delete_item(&id1).unwrap();
    wallet.delete_item(&id2).unwrap();

    let deleted = wallet.get_deleted_items().unwrap();
    let deleted_names: Vec<&str> = deleted.iter().map(|i| i.name.as_str()).collect();

    // Our two deleted items should be in the list
    assert!(deleted_names.contains(&"Deleted A"));
    assert!(deleted_names.contains(&"Deleted B"));
    // Active item should NOT be in deleted list
    assert!(!deleted_names.contains(&"Keep Active"));

    wallet.close();
}

#[test]
fn test_get_deleted_fields_list() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let item_id = wallet.add_item("Item", "document", false, None).unwrap();
    let f1 = wallet.add_field(&item_id, "MAIL", "deleted@test.com", None).unwrap();
    wallet.add_field(&item_id, "PASS", "active_secret", None).unwrap();

    wallet.delete_field(&item_id, &f1).unwrap();

    let deleted = wallet.get_deleted_fields().unwrap();
    let deleted_values: Vec<&str> = deleted.iter().map(|f| f.value.as_str()).collect();
    assert!(deleted_values.contains(&"deleted@test.com"));
    assert!(!deleted_values.contains(&"active_secret"));

    wallet.close();
}

#[test]
fn test_get_deleted_items_empty_when_none_deleted() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    wallet.add_item("Active Only", "document", false, None).unwrap();

    // Only newly created items (no deletes) — but the legacy DB may have deleted records,
    // so we just verify it doesn't error out
    let deleted = wallet.get_deleted_items();
    assert!(deleted.is_ok());

    wallet.close();
}

// =========================================================================
// Gap #3: Change Password + Deleted Records
// =========================================================================

#[test]
fn test_change_password_deleted_items_still_accessible() {
    let temp_dir = TempDir::new().unwrap();
    let mut wallet = Wallet::create(temp_dir.path(), "OldPassword1", "en").unwrap();

    // Create and delete an item
    let item_id = wallet.add_item("Deleted Before PW Change", "document", false, None).unwrap();
    wallet.delete_item(&item_id).unwrap();

    // Change password
    assert!(wallet.change_password("NewPassword2").unwrap());

    // Deleted item should still be accessible
    let deleted = wallet.get_deleted_items().unwrap();
    assert_eq!(deleted.len(), 1);
    assert_eq!(deleted[0].name, "Deleted Before PW Change");

    // Verify after lock/unlock with new password
    wallet.lock();
    assert!(wallet.unlock("NewPassword2").unwrap());
    let deleted2 = wallet.get_deleted_items().unwrap();
    assert_eq!(deleted2.len(), 1);
    assert_eq!(deleted2[0].name, "Deleted Before PW Change");

    wallet.close();
}

#[test]
fn test_change_password_deleted_fields_still_accessible() {
    let temp_dir = TempDir::new().unwrap();
    let mut wallet = Wallet::create(temp_dir.path(), "OldPassword1", "en").unwrap();

    let item_id = wallet.add_item("Item", "document", false, None).unwrap();
    let field_id = wallet.add_field(&item_id, "PASS", "secret_before_change", None).unwrap();
    wallet.delete_field(&item_id, &field_id).unwrap();

    assert!(wallet.change_password("NewPassword2").unwrap());

    let deleted = wallet.get_deleted_fields().unwrap();
    assert_eq!(deleted.len(), 1);
    assert_eq!(deleted[0].value, "secret_before_change");

    // Lock/unlock cycle
    wallet.lock();
    assert!(wallet.unlock("NewPassword2").unwrap());
    let deleted2 = wallet.get_deleted_fields().unwrap();
    assert_eq!(deleted2.len(), 1);
    assert_eq!(deleted2[0].value, "secret_before_change");

    wallet.close();
}

#[test]
fn test_change_password_mixed_active_and_deleted() {
    let temp_dir = TempDir::new().unwrap();
    let mut wallet = Wallet::create(temp_dir.path(), "Pass1", "en").unwrap();

    // Active records
    let active_item = wallet.add_item("Active Item", "document", false, None).unwrap();
    wallet.add_field(&active_item, "MAIL", "active@test.com", None).unwrap();

    // Deleted records
    let del_item = wallet.add_item("Deleted Item", "document", false, None).unwrap();
    let del_field = wallet.add_field(&active_item, "PASS", "deleted_pass", None).unwrap();
    wallet.delete_item(&del_item).unwrap();
    wallet.delete_field(&active_item, &del_field).unwrap();

    // Change password
    assert!(wallet.change_password("Pass2").unwrap());

    // Active still works
    let item = wallet.get_item(&active_item).unwrap().unwrap();
    assert_eq!(item.name, "Active Item");
    let fields = wallet.get_fields_by_item(&active_item).unwrap();
    assert_eq!(fields[0].value, "active@test.com");

    // Deleted still works
    let del_items = wallet.get_deleted_items().unwrap();
    assert!(del_items.iter().any(|i| i.name == "Deleted Item"));
    let del_fields = wallet.get_deleted_fields().unwrap();
    assert!(del_fields.iter().any(|f| f.value == "deleted_pass"));

    wallet.close();
}

#[test]
fn test_change_password_twice_deleted_survive() {
    let temp_dir = TempDir::new().unwrap();
    let mut wallet = Wallet::create(temp_dir.path(), "Pass1", "en").unwrap();

    let item_id = wallet.add_item("Survivor", "document", false, None).unwrap();
    let field_id = wallet.add_field(&item_id, "NOTE", "persistent_note", None).unwrap();
    wallet.delete_item(&item_id).unwrap();
    wallet.delete_field(&item_id, &field_id).unwrap();

    // First password change
    assert!(wallet.change_password("Pass2").unwrap());
    // Second password change
    assert!(wallet.change_password("Pass3").unwrap());

    wallet.lock();
    assert!(!wallet.unlock("Pass1").unwrap());
    assert!(!wallet.unlock("Pass2").unwrap());
    assert!(wallet.unlock("Pass3").unwrap());

    let del_items = wallet.get_deleted_items().unwrap();
    assert!(del_items.iter().any(|i| i.name == "Survivor"));
    let del_fields = wallet.get_deleted_fields().unwrap();
    assert!(del_fields.iter().any(|f| f.value == "persistent_note"));

    wallet.close();
}

// =========================================================================
// Gap #4: Compact
// =========================================================================

#[test]
fn test_compact_purges_deleted_items() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let id1 = wallet.add_item("Purge Me 1", "document", false, None).unwrap();
    let id2 = wallet.add_item("Purge Me 2", "document", false, None).unwrap();

    wallet.delete_item(&id1).unwrap();
    wallet.delete_item(&id2).unwrap();

    let deleted_before = wallet.get_deleted_items().unwrap();
    let our_deleted = deleted_before.iter()
        .filter(|i| i.name == "Purge Me 1" || i.name == "Purge Me 2")
        .count();
    assert_eq!(our_deleted, 2);

    let (items_purged, _fields_purged) = wallet.compact().unwrap();
    assert!(items_purged >= 2);

    // Our items should be gone from deleted list
    let deleted_after = wallet.get_deleted_items().unwrap();
    assert!(!deleted_after.iter().any(|i| i.name == "Purge Me 1"));
    assert!(!deleted_after.iter().any(|i| i.name == "Purge Me 2"));

    wallet.close();
}

#[test]
fn test_compact_purges_deleted_fields() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let item_id = wallet.add_item("Item With Fields", "document", false, None).unwrap();
    let f1 = wallet.add_field(&item_id, "MAIL", "purge_this@test.com", None).unwrap();
    wallet.add_field(&item_id, "PASS", "keep_this_active", None).unwrap();

    wallet.delete_field(&item_id, &f1).unwrap();

    wallet.compact().unwrap();

    // Deleted field should be purged
    let deleted = wallet.get_deleted_fields().unwrap();
    assert!(!deleted.iter().any(|f| f.value == "purge_this@test.com"));

    // Active field should survive
    let active = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(active.len(), 1);
    assert_eq!(active[0].value, "keep_this_active");

    wallet.close();
}

#[test]
fn test_compact_returns_counts() {
    let temp_dir = TempDir::new().unwrap();
    let mut wallet = Wallet::create(temp_dir.path(), "TestPass", "en").unwrap();

    let item_id = wallet.add_item("Item", "document", false, None).unwrap();
    let f1 = wallet.add_field(&item_id, "MAIL", "a@a.com", None).unwrap();
    wallet.add_field(&item_id, "PASS", "secret", None).unwrap();

    // Explicitly delete one field, then delete item (cascade-deletes the other)
    wallet.delete_field(&item_id, &f1).unwrap();
    wallet.delete_item(&item_id).unwrap();

    let (items_purged, fields_purged) = wallet.compact().unwrap();
    assert_eq!(items_purged, 1);
    assert_eq!(fields_purged, 2); // f1 explicitly deleted + PASS cascade-deleted

    wallet.close();
}

#[test]
fn test_compact_idempotent() {
    let temp_dir = TempDir::new().unwrap();
    let mut wallet = Wallet::create(temp_dir.path(), "TestPass", "en").unwrap();

    let item_id = wallet.add_item("Gone", "document", false, None).unwrap();
    wallet.delete_item(&item_id).unwrap();

    let (count1, _) = wallet.compact().unwrap();
    assert_eq!(count1, 1);

    // Second compact: nothing left
    let (count2, count3) = wallet.compact().unwrap();
    assert_eq!(count2, 0);
    assert_eq!(count3, 0);

    wallet.close();
}

// =========================================================================
// Gap #5: check_password()
// =========================================================================

#[test]
fn test_check_password_correct() {
    let (wallet, _temp_dir) = setup_test_wallet();

    // check_password doesn't require unlock
    assert!(wallet.check_password(TEST_PASSWORD).unwrap());
}

#[test]
fn test_check_password_wrong() {
    let (wallet, _temp_dir) = setup_test_wallet();

    assert!(!wallet.check_password("WrongPassword").unwrap());
}

#[test]
fn test_check_password_after_change() {
    let temp_dir = TempDir::new().unwrap();
    let mut wallet = Wallet::create(temp_dir.path(), "OldPass", "en").unwrap();

    assert!(wallet.check_password("OldPass").unwrap());
    assert!(!wallet.check_password("NewPass").unwrap());

    wallet.change_password("NewPass").unwrap();

    assert!(!wallet.check_password("OldPass").unwrap());
    assert!(wallet.check_password("NewPass").unwrap());

    wallet.close();
}

#[test]
fn test_check_password_while_locked() {
    let (mut wallet, _temp_dir) = setup_test_wallet();

    // Lock explicitly (wallet starts locked for setup_test_wallet)
    // check_password should work regardless of lock state
    assert!(wallet.check_password(TEST_PASSWORD).unwrap());

    wallet.unlock(TEST_PASSWORD).unwrap();
    assert!(wallet.check_password(TEST_PASSWORD).unwrap());

    wallet.lock();
    assert!(wallet.check_password(TEST_PASSWORD).unwrap());

    wallet.close();
}

// =========================================================================
// Gap #6: Cascade Delete (folder with children)
// =========================================================================

#[test]
fn test_cascade_delete_folder_with_children() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let folder = wallet.add_item("Parent Folder", "folder", true, None).unwrap();
    let child1 = wallet.add_item("Child 1", "document", false, Some(&folder)).unwrap();
    let child2 = wallet.add_item("Child 2", "document", false, Some(&folder)).unwrap();
    wallet.add_field(&child1, "MAIL", "child1@test.com", None).unwrap();

    // Delete folder — should cascade to children
    wallet.delete_item(&folder).unwrap();

    // All gone from active
    assert!(wallet.get_item(&folder).unwrap().is_none());
    assert!(wallet.get_item(&child1).unwrap().is_none());
    assert!(wallet.get_item(&child2).unwrap().is_none());

    // All present in deleted list
    let deleted = wallet.get_deleted_items().unwrap();
    let deleted_ids: Vec<&str> = deleted.iter().map(|i| i.item_id.as_str()).collect();
    assert!(deleted_ids.contains(&folder.as_str()));
    assert!(deleted_ids.contains(&child1.as_str()));
    assert!(deleted_ids.contains(&child2.as_str()));

    wallet.close();
}

#[test]
fn test_cascade_delete_deep_nesting() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let l1 = wallet.add_item("Level 1", "folder", true, None).unwrap();
    let l2 = wallet.add_item("Level 2", "folder", true, Some(&l1)).unwrap();
    let l3 = wallet.add_item("Level 3", "folder", true, Some(&l2)).unwrap();
    let l4 = wallet.add_item("Deep Item", "document", false, Some(&l3)).unwrap();

    wallet.delete_item(&l1).unwrap();

    // All 4 levels should be deleted
    let deleted = wallet.get_deleted_items().unwrap();
    let deleted_ids: Vec<&str> = deleted.iter().map(|i| i.item_id.as_str()).collect();
    assert!(deleted_ids.contains(&l1.as_str()));
    assert!(deleted_ids.contains(&l2.as_str()));
    assert!(deleted_ids.contains(&l3.as_str()));
    assert!(deleted_ids.contains(&l4.as_str()));

    wallet.close();
}

#[test]
fn test_cascade_delete_then_undelete_child() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let folder = wallet.add_item("Folder", "folder", true, None).unwrap();
    let child = wallet.add_item("Child", "document", false, Some(&folder)).unwrap();

    wallet.delete_item(&folder).unwrap();

    // Undelete only the child — it should land at root
    wallet.undelete_item(&child).unwrap();

    let restored = wallet.get_item(&child).unwrap().unwrap();
    assert_eq!(restored.name, "Child");
    assert_eq!(restored.parent_id.as_deref(), Some("__ROOT__"));

    // Parent still deleted
    assert!(wallet.get_item(&folder).unwrap().is_none());

    wallet.close();
}

// =========================================================================
// Gap #6b: Cascade Delete — Fields at all nesting levels
// =========================================================================

#[test]
fn test_cascade_delete_deep_nesting_fields_at_all_levels() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let l1 = wallet.add_item("Level 1", "folder", true, None).unwrap();
    let l2 = wallet.add_item("Level 2", "folder", true, Some(&l1)).unwrap();
    let l3 = wallet.add_item("Level 3", "folder", true, Some(&l2)).unwrap();
    let item_l1 = wallet.add_item("Item in L1", "document", false, Some(&l1)).unwrap();
    let item_l2 = wallet.add_item("Item in L2", "document", false, Some(&l2)).unwrap();
    let item_l3 = wallet.add_item("Item in L3", "document", false, Some(&l3)).unwrap();

    wallet.add_field(&item_l1, "MAIL", "l1@test.com", None).unwrap();
    wallet.add_field(&item_l2, "PASS", "l2secret", None).unwrap();
    wallet.add_field(&item_l2, "NOTE", "l2note", None).unwrap();
    wallet.add_field(&item_l3, "MAIL", "l3@test.com", None).unwrap();

    // Delete top-level folder
    wallet.delete_item(&l1).unwrap();

    // Fields at all levels are cascade-deleted
    assert_eq!(wallet.get_fields_by_item(&item_l1).unwrap().len(), 0);
    assert_eq!(wallet.get_fields_by_item(&item_l2).unwrap().len(), 0);
    assert_eq!(wallet.get_fields_by_item(&item_l3).unwrap().len(), 0);

    // All 4 fields appear in deleted list
    let deleted = wallet.get_deleted_fields().unwrap();
    let our_deleted: Vec<_> = deleted.iter()
        .filter(|f| f.item_id == item_l1 || f.item_id == item_l2 || f.item_id == item_l3)
        .collect();
    assert_eq!(our_deleted.len(), 4);

    // Compact purges everything
    let (items_purged, fields_purged) = wallet.compact().unwrap();
    assert!(items_purged >= 6); // 3 folders + 3 items
    assert!(fields_purged >= 4); // 4 fields

    wallet.close();
}

#[test]
fn test_explicit_field_delete_then_item_delete_intermediate_state() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let item_id = wallet.add_item("Item", "document", false, None).unwrap();
    let f1 = wallet.add_field(&item_id, "MAIL", "explicit@test.com", None).unwrap();
    let f2 = wallet.add_field(&item_id, "PASS", "cascade_me", None).unwrap();
    wallet.add_field(&item_id, "NOTE", "also_cascade", None).unwrap();

    // Explicitly delete f1 first
    wallet.delete_field(&item_id, &f1).unwrap();

    // Intermediate: 2 active, 1 deleted
    assert_eq!(wallet.get_fields_by_item(&item_id).unwrap().len(), 2);
    let deleted_mid = wallet.get_deleted_fields().unwrap();
    let our_deleted_mid: Vec<_> = deleted_mid.iter().filter(|f| f.item_id == item_id).collect();
    assert_eq!(our_deleted_mid.len(), 1);
    assert_eq!(our_deleted_mid[0].field_id, f1);

    // Now delete item — f2 and NOTE cascade, f1 stays as-is
    wallet.delete_item(&item_id).unwrap();

    // All 3 fields now in deleted list
    let deleted_after = wallet.get_deleted_fields().unwrap();
    let our_deleted_after: Vec<_> = deleted_after.iter().filter(|f| f.item_id == item_id).collect();
    assert_eq!(our_deleted_after.len(), 3);

    // f2 specifically became cascade-deleted
    assert!(our_deleted_after.iter().any(|f| f.field_id == f2));

    wallet.close();
}

#[test]
fn test_change_password_with_cascade_deleted_fields() {
    let temp_dir = TempDir::new().unwrap();
    let mut wallet = Wallet::create(temp_dir.path(), "OldPass", "en").unwrap();

    let item_id = wallet.add_item("Item", "document", false, None).unwrap();
    wallet.add_field(&item_id, "MAIL", "cascade@test.com", None).unwrap();
    wallet.add_field(&item_id, "PASS", "cascade_secret", None).unwrap();

    // Delete item — fields become cascade-deleted
    wallet.delete_item(&item_id).unwrap();

    // Change password
    assert!(wallet.change_password("NewPass").unwrap());

    // Cascade-deleted fields still accessible
    let deleted = wallet.get_deleted_fields().unwrap();
    let our_fields: Vec<_> = deleted.iter().filter(|f| f.item_id == item_id).collect();
    assert_eq!(our_fields.len(), 2);
    let values: Vec<&str> = our_fields.iter().map(|f| f.value.as_str()).collect();
    assert!(values.contains(&"cascade@test.com"));
    assert!(values.contains(&"cascade_secret"));

    // Deleted item also still accessible
    let deleted_items = wallet.get_deleted_items().unwrap();
    assert!(deleted_items.iter().any(|i| i.item_id == item_id));

    // Lock/unlock cycle with new password
    wallet.lock();
    assert!(!wallet.unlock("OldPass").unwrap());
    assert!(wallet.unlock("NewPass").unwrap());

    // Still accessible after reopen
    let deleted2 = wallet.get_deleted_fields().unwrap();
    let our_fields2: Vec<_> = deleted2.iter().filter(|f| f.item_id == item_id).collect();
    assert_eq!(our_fields2.len(), 2);

    wallet.close();
}

// =========================================================================
// Gap #7: Backup Verify / Cleanup
// =========================================================================

#[test]
fn test_backup_verify() {
    let temp_dir = TempDir::new().unwrap();
    let wallet_dir = temp_dir.path().join("wallet");
    let backup_dir = temp_dir.path().join("backups");

    let wallet = Wallet::create(&wallet_dir, "TestPass", "en").unwrap();
    let backup_mgr = BackupManager::new(&backup_dir);

    let backup_path = backup_mgr.create_backup(wallet.database().unwrap(), true).unwrap();

    // Verify the backup is valid
    let valid = backup_mgr.verify_backup(&backup_path).unwrap();
    assert!(valid);
}

#[test]
fn test_backup_list_and_get_latest() {
    let temp_dir = TempDir::new().unwrap();
    let wallet_dir = temp_dir.path().join("wallet");
    let backup_dir = temp_dir.path().join("backups");

    let wallet = Wallet::create(&wallet_dir, "TestPass", "en").unwrap();
    let backup_mgr = BackupManager::new(&backup_dir);

    // No backups yet
    assert!(backup_mgr.list_backups().unwrap().is_empty());
    assert!(backup_mgr.get_latest_backup().unwrap().is_none());

    // Create two backups
    let _b1 = backup_mgr.create_backup(wallet.database().unwrap(), true).unwrap();
    std::thread::sleep(std::time::Duration::from_millis(1100)); // filenames use seconds
    let b2 = backup_mgr.create_backup(wallet.database().unwrap(), true).unwrap();

    let backups = backup_mgr.list_backups().unwrap();
    assert_eq!(backups.len(), 2);

    let latest = backup_mgr.get_latest_backup().unwrap().unwrap();
    assert_eq!(latest.path, b2);
}

#[test]
fn test_backup_cleanup_old() {
    let temp_dir = TempDir::new().unwrap();
    let wallet_dir = temp_dir.path().join("wallet");
    let backup_dir = temp_dir.path().join("backups");

    let wallet = Wallet::create(&wallet_dir, "TestPass", "en").unwrap();
    let backup_mgr = BackupManager::new(&backup_dir);

    // Create 4 backups
    for _ in 0..4 {
        backup_mgr.create_backup(wallet.database().unwrap(), true).unwrap();
        std::thread::sleep(std::time::Duration::from_millis(1100));
    }

    assert_eq!(backup_mgr.list_backups().unwrap().len(), 4);

    // Keep only 2
    let deleted = backup_mgr.cleanup_old_backups(2).unwrap();
    assert_eq!(deleted, 2);
    assert_eq!(backup_mgr.list_backups().unwrap().len(), 2);
}

#[test]
fn test_backup_cleanup_auto() {
    let temp_dir = TempDir::new().unwrap();
    let wallet_dir = temp_dir.path().join("wallet");
    let backup_dir = temp_dir.path().join("backups");

    let wallet = Wallet::create(&wallet_dir, "TestPass", "en").unwrap();
    let backup_mgr = BackupManager::new(&backup_dir);

    // Create auto backups (manual=false)
    for _ in 0..3 {
        backup_mgr.create_backup(wallet.database().unwrap(), false).unwrap();
        std::thread::sleep(std::time::Duration::from_millis(1100));
    }

    assert_eq!(backup_mgr.list_backups().unwrap().len(), 3);

    // Cleanup with min_keep=3 and max_age=30 days — all are fresh, none should be deleted
    let deleted = backup_mgr.cleanup_auto_backups(3, 30).unwrap();
    assert_eq!(deleted, 0);
    assert_eq!(backup_mgr.list_backups().unwrap().len(), 3);
}

#[test]
fn test_backup_cleanup_keeps_minimum() {
    let temp_dir = TempDir::new().unwrap();
    let wallet_dir = temp_dir.path().join("wallet");
    let backup_dir = temp_dir.path().join("backups");

    let wallet = Wallet::create(&wallet_dir, "TestPass", "en").unwrap();
    let backup_mgr = BackupManager::new(&backup_dir);

    // Create 2 backups
    backup_mgr.create_backup(wallet.database().unwrap(), true).unwrap();
    std::thread::sleep(std::time::Duration::from_millis(1100));
    backup_mgr.create_backup(wallet.database().unwrap(), true).unwrap();

    // Try to keep 5 (more than we have) — should delete nothing
    let deleted = backup_mgr.cleanup_old_backups(5).unwrap();
    assert_eq!(deleted, 0);
    assert_eq!(backup_mgr.list_backups().unwrap().len(), 2);
}

// =========================================================================
// Gap #8: Legacy Database with Deleted Records
// =========================================================================

#[test]
fn test_legacy_db_get_deleted_items_does_not_crash() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // The legacy test database has deleted items with potentially NULL icon fields.
    // This should not crash — should return Ok with whatever is decryptable.
    let result = wallet.get_deleted_items();
    assert!(result.is_ok(), "get_deleted_items should not crash on legacy DB");

    let deleted = result.unwrap();
    println!("Legacy DB has {} deleted items (decryptable)", deleted.len());

    // All returned items should have valid data
    for item in &deleted {
        assert!(!item.item_id.is_empty());
        assert!(item.deleted);
    }

    wallet.close();
}

#[test]
fn test_legacy_db_get_deleted_fields_does_not_crash() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Legacy DB may have deleted fields encrypted with old passwords.
    // Should silently skip undecryptable ones, not abort.
    let result = wallet.get_deleted_fields();
    assert!(result.is_ok(), "get_deleted_fields should not crash on legacy DB");

    let deleted = result.unwrap();
    println!("Legacy DB has {} deleted fields (decryptable)", deleted.len());

    for field in &deleted {
        assert!(!field.field_id.is_empty());
        assert!(field.deleted);
    }

    wallet.close();
}

#[test]
fn test_legacy_db_change_password_with_deleted_records() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Count what's decryptable before
    let items_before = wallet.get_items().unwrap().len();
    let fields_before = wallet.get_fields().unwrap().len();
    let deleted_items_before = wallet.get_deleted_items().unwrap().len();
    let deleted_fields_before = wallet.get_deleted_fields().unwrap().len();

    // Change password on legacy DB (which has deleted records with old encryption)
    let new_password = "LegacyDBNewPassword!";
    assert!(wallet.change_password(new_password).unwrap());

    // Active records should match
    let items_after = wallet.get_items().unwrap().len();
    let fields_after = wallet.get_fields().unwrap().len();
    assert_eq!(items_before, items_after, "Active items count should match");
    assert_eq!(fields_before, fields_after, "Active fields count should match");

    // Deleted records should be at least as many as before
    // (change_password re-encrypts what it can decrypt)
    let deleted_items_after = wallet.get_deleted_items().unwrap().len();
    let deleted_fields_after = wallet.get_deleted_fields().unwrap().len();
    assert!(deleted_items_after >= deleted_items_before,
        "Deleted items should not decrease after password change");
    assert!(deleted_fields_after >= deleted_fields_before,
        "Deleted fields should not decrease after password change");

    // Verify new password works after lock/unlock
    wallet.lock();
    assert!(wallet.unlock(new_password).unwrap());
    assert!(!wallet.unlock(TEST_PASSWORD).unwrap_or(false));

    wallet.close();
}

// =========================================================================
// Update Field History Preservation
// =========================================================================

#[test]
fn test_update_field_history_preservation() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Create item with field
    let item_id = wallet.add_item("History Test", "document", false, None).unwrap();
    let fid1 = wallet.add_field(&item_id, "NOTE", "version1", None).unwrap();

    // Update twice
    let fid2 = wallet.update_field(&fid1, "version2", None).unwrap();
    let fid3 = wallet.update_field(&fid2, "version3", None).unwrap();

    // Active field has latest value
    let fields = wallet.get_fields_by_item(&item_id).unwrap();
    assert_eq!(fields.len(), 1);
    assert_eq!(fields[0].field_id, fid3);
    assert_eq!(fields[0].value, "version3");

    // 2 deleted entries with correct old values
    let deleted = wallet.get_deleted_fields().unwrap();
    let history: Vec<_> = deleted.iter().filter(|f| f.item_id == item_id).collect();
    assert_eq!(history.len(), 2);
    let values: Vec<&str> = history.iter().map(|f| f.value.as_str()).collect();
    assert!(values.contains(&"version1"));
    assert!(values.contains(&"version2"));

    wallet.close();
}

// =========================================================================
// Undelete after compact — item restores to root even when parent is purged
// =========================================================================

#[test]
fn test_undelete_deep_nested_item_restores_to_root() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let l1 = wallet.add_item("Level 1", "folder", true, None).unwrap();
    let l2 = wallet.add_item("Level 2", "folder", true, Some(&l1)).unwrap();
    let l3 = wallet.add_item("Level 3", "folder", true, Some(&l2)).unwrap();
    let leaf = wallet.add_item("Leaf", "document", false, Some(&l3)).unwrap();

    // Delete top-level folder (cascades all)
    wallet.delete_item(&l1).unwrap();

    // Undelete only the leaf — it should land at root
    wallet.undelete_item(&leaf).unwrap();

    let restored = wallet.get_item(&leaf).unwrap().unwrap();
    assert_eq!(restored.name, "Leaf");
    assert_eq!(restored.parent_id.as_deref(), Some("__ROOT__"));
    assert!(!restored.deleted);

    // Verify it appears in root listing
    let root_items = wallet.get_items_by_parent("__ROOT__").unwrap();
    assert!(root_items.iter().any(|i| i.item_id == leaf));

    wallet.close();
}

#[test]
fn test_export_pdf_produces_valid_pdf() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    let bytes = wallet.export_pdf().unwrap();
    assert!(!bytes.is_empty(), "PDF should not be empty");
    assert!(bytes.starts_with(b"%PDF"), "Output should be a valid PDF");

    wallet.close();
}

#[test]
fn test_export_pdf_with_new_data() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    wallet.unlock(TEST_PASSWORD).unwrap();

    // Add a folder with an item and fields
    let folder_id = wallet.add_item("Test Folder", "folder", true, None).unwrap();
    let item_id = wallet.add_item("Test Entry", "document", false, Some(&folder_id)).unwrap();
    wallet.add_field(&item_id, "MAIL", "test@example.com", Some(0)).unwrap();
    wallet.add_field(&item_id, "PASS", "s3cret!", Some(1)).unwrap();

    let bytes = wallet.export_pdf().unwrap();
    assert!(bytes.starts_with(b"%PDF"));
    // PDF with content should be larger than an empty one
    assert!(bytes.len() > 1000, "PDF with entries should have substantial size");

    wallet.close();
}

#[test]
fn test_export_pdf_requires_unlock() {
    let (mut wallet, _temp_dir) = setup_test_wallet();
    // Don't unlock
    let result = wallet.export_pdf();
    assert!(result.is_err());
}