pubky-app-specs 0.5.2

Pubky.app Data Model Specifications
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
use crate::{
    common::{sanitize_url, validate_crockford_id},
    limits::VALIDATION_LIMITS,
    traits::{HasIdPath, TimestampId, Validatable},
    types::PubkyId,
    APP_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};
use std::{fmt, str::FromStr};
use url::Url;

// Reserved keyword used by the system to mark deleted posts with relationships
const RESERVED_CONTENT_DELETED: &str = "[DELETED]";

#[cfg(target_arch = "wasm32")]
use crate::traits::Json;
#[cfg(target_arch = "wasm32")]
use tsify_next::Tsify;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "openapi")]
use utoipa::ToSchema;

/// Represents the type of pubky-app posted data
/// Used primarily to best display the content in UI
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub enum PubkyAppPostKind {
    #[default]
    Short,
    Long,
    Image,
    Video,
    Link,
    File,
    Collection,
    #[serde(other)]
    Unknown,
}

impl fmt::Display for PubkyAppPostKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let string_repr = serde_json::to_value(self)
            .ok()
            .and_then(|v| v.as_str().map(String::from))
            .unwrap_or_default();
        write!(f, "{}", string_repr)
    }
}

impl FromStr for PubkyAppPostKind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "short" => Ok(PubkyAppPostKind::Short),
            "long" => Ok(PubkyAppPostKind::Long),
            "image" => Ok(PubkyAppPostKind::Image),
            "video" => Ok(PubkyAppPostKind::Video),
            "link" => Ok(PubkyAppPostKind::Link),
            "file" => Ok(PubkyAppPostKind::File),
            "collection" => Ok(PubkyAppPostKind::Collection),
            _ => Err(format!("Invalid content kind: {}", s)),
        }
    }
}

impl PubkyAppPostKind {
    /// Returns `true` for every spec-recognized variant, `false` for `Unknown`.
    ///
    /// `Unknown` is the forwards-compat catch-all variant (via `#[serde(other)]`)
    /// that captures any post-kind string this version of the spec doesn't
    /// recognize yet. Most consumers — indexers, stream filters, search ranking —
    /// want to skip such posts, and this helper lets them write
    /// `if kind.is_known() { ... }` rather than
    /// `if !matches!(kind, PubkyAppPostKind::Unknown) { ... }`.
    pub fn is_known(&self) -> bool {
        !matches!(self, PubkyAppPostKind::Unknown)
    }
}

/// Represents embedded content within a post
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct PubkyAppPostEmbed {
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub kind: PubkyAppPostKind, // Kind of the embedded content
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub uri: String, // URI of the embedded content
}

#[cfg(target_arch = "wasm32")]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl PubkyAppPostEmbed {
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(constructor))]
    pub fn new(uri: String, kind: PubkyAppPostKind) -> Self {
        PubkyAppPostEmbed { uri, kind }
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn kind(&self) -> String {
        match self.kind {
            PubkyAppPostKind::Short => "Short".to_string(),
            PubkyAppPostKind::Long => "Long".to_string(),
            PubkyAppPostKind::Image => "Image".to_string(),
            PubkyAppPostKind::Video => "Video".to_string(),
            PubkyAppPostKind::Link => "Link".to_string(),
            PubkyAppPostKind::File => "File".to_string(),
            PubkyAppPostKind::Collection => "Collection".to_string(),
            PubkyAppPostKind::Unknown => "Unknown".to_string(),
        }
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn uri(&self) -> String {
        self.uri.clone()
    }
}

/// Typed JSON envelope stored in `PubkyAppPost::content` when `kind == Collection`.
///
/// A collection post curates an ordered list of URIs (via `items`)
/// under a `name` and optional `description`. The envelope is parsed and validated
/// by the spec but never re-serialized as a top-level homeserver object.
///
/// **Construction**: this struct is deserialized from the post's `content` JSON
/// envelope during validation and is **not** intended to be constructed by
/// callers directly. It is re-exported publicly (via `lib.rs`) so SDK consumers
/// can inspect the envelope shape (OpenAPI schema, type definitions), but the
/// authoritative way to produce a Collection post is to author a `PubkyAppPost`
/// with `kind: Collection` and a `content` string that JSON-parses into this
/// shape.
///
/// Forward-compat: `#[serde(deny_unknown_fields)]` is intentionally NOT used so
/// future minor versions can add fields (e.g. `cover_image`) without breaking
/// older parsers. New fields must be additive and ignorable.
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(target_arch = "wasm32", derive(Tsify))]
#[serde(rename_all = "snake_case")]
pub struct PubkyAppCollectionContent {
    /// Display name of the collection. Length bounded by
    /// `VALIDATION_LIMITS.collection_name_{min,max}_length` (unicode scalars).
    /// Whitespace-only names are rejected separately by the validator.
    pub name: String,
    /// Optional human-readable description. Length bounded by
    /// `VALIDATION_LIMITS.collection_description_max_length` (unicode scalars).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Ordered list of Post URIs this collection curates. Count bounded by
    /// `VALIDATION_LIMITS.collection_items_max_count`; each URI must be in
    /// exact canonical form (see `validate_collection_item_uri`).
    #[serde(default)]
    pub items: Vec<String>,
    /// Optional hero/cover image URL. Length bounded by
    /// `VALIDATION_LIMITS.post_attachment_url_max_length`; protocol must be in
    /// `VALIDATION_LIMITS.post_allowed_attachment_protocols`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cover_image: Option<String>,
}

/// Represents raw post in homeserver with content and kind
/// URI: /pub/pubky.app/posts/:post_id
/// Where post_id is CrockfordBase32 encoding of timestamp
///
/// Example URI:
///
/// `/pub/pubky.app/posts/00321FCW75ZFY`
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct PubkyAppPost {
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub content: String,
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub kind: PubkyAppPostKind,
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub parent: Option<String>, // If a reply, the URI of the parent post.
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub embed: Option<PubkyAppPostEmbed>,
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub attachments: Option<Vec<String>>,
}

#[cfg(target_arch = "wasm32")]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl PubkyAppPost {
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn content(&self) -> String {
        self.content.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn kind(&self) -> String {
        match self.kind {
            PubkyAppPostKind::Short => "Short".to_string(),
            PubkyAppPostKind::Long => "Long".to_string(),
            PubkyAppPostKind::Image => "Image".to_string(),
            PubkyAppPostKind::Video => "Video".to_string(),
            PubkyAppPostKind::Link => "Link".to_string(),
            PubkyAppPostKind::File => "File".to_string(),
            PubkyAppPostKind::Collection => "Collection".to_string(),
            PubkyAppPostKind::Unknown => "Unknown".to_string(),
        }
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn parent(&self) -> Option<String> {
        self.parent.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn embed(&self) -> Option<PubkyAppPostEmbed> {
        self.embed.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn attachments(&self) -> Option<Vec<String>> {
        self.attachments.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = fromJson))]
    pub fn from_json(js_value: &JsValue) -> Result<Self, String> {
        Self::import_json(js_value)
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toJson))]
    pub fn to_json(&self) -> Result<JsValue, String> {
        self.export_json()
    }
}

#[cfg(target_arch = "wasm32")]
impl Json for PubkyAppPost {}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl PubkyAppPost {
    /// Creates a new `PubkyAppPost` instance and sanitizes it.
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(constructor))]
    pub fn new(
        content: String,
        kind: PubkyAppPostKind,
        parent: Option<String>,
        embed: Option<PubkyAppPostEmbed>,
        attachments: Option<Vec<String>>,
    ) -> Self {
        let post = PubkyAppPost {
            content,
            kind,
            parent,
            embed,
            attachments,
        };
        post.sanitize()
    }
}

impl TimestampId for PubkyAppPost {}

impl HasIdPath for PubkyAppPost {
    const PATH_SEGMENT: &'static str = "posts/";

    fn create_path(id: &str) -> String {
        [PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT, id].concat()
    }
}

impl Validatable for PubkyAppPost {
    fn sanitize(self) -> Self {
        // Sanitize content: trim whitespace only
        let content = self.content.trim().to_string();

        // Sanitize parent URI if present
        let parent = self.parent.map(|uri_str| sanitize_url(&uri_str));

        // Sanitize embed if present
        let embed = self.embed.map(|e| PubkyAppPostEmbed {
            kind: e.kind,
            uri: sanitize_url(&e.uri),
        });

        // Sanitize attachments
        let attachments = self.attachments.map(|attachments_vec| {
            attachments_vec
                .into_iter()
                .map(|url_str| sanitize_url(&url_str))
                .collect()
        });

        PubkyAppPost {
            content,
            kind: self.kind,
            parent,
            embed,
            attachments,
        }
    }

    fn validate(&self, id: Option<&str>) -> Result<(), String> {
        // Validate the post ID
        if let Some(id) = id {
            self.validate_id(id)?;
        }

        // Validate that post has meaningful content (at least one of: content, embed, or attachments)
        if self.content.trim().is_empty() && self.embed.is_none() && self.attachments.is_none() {
            return Err(
                "Validation Error: Post must have content, an embed, or attachments".into(),
            );
        }

        // We use content keyword `[DELETED]` for deleted posts from a homeserver that still have relationships
        // placed by other users (replies, tags, etc). This content is exactly matched by the client to apply effects to deleted content.
        // Placing posts with content `[DELETED]` is not allowed.
        if self.content == RESERVED_CONTENT_DELETED {
            return Err(
                "Validation Error: Content cannot be the reserved keyword '[DELETED]'".into(),
            );
        }

        // Reject posts whose kind couldn't be matched against any known variant.
        // `Unknown` is a serde catch-all for forwards-compat: older binaries can
        // deserialize events from newer clients without panicking, but such posts
        // must never pass spec validation. Same reasoning for `embed.kind`.
        if !self.kind.is_known() {
            return Err("Validation Error: post kind is unknown".into());
        }
        if let Some(ref embed) = self.embed {
            if !embed.kind.is_known() {
                return Err("Validation Error: embed kind is unknown".into());
            }
        }

        if matches!(self.kind, PubkyAppPostKind::Collection) {
            if self.parent.is_some() || self.embed.is_some() {
                return Err(
                    "Validation Error: Collection posts cannot have parent or embed".into(),
                );
            }
            // Anti-misuse guard: items belong in the envelope, not in
            // `post.attachments`.
            if matches!(&self.attachments, Some(a) if !a.is_empty()) {
                return Err(
                    "Validation Error: Collection posts must not use post.attachments — items belong in the content envelope"
                        .into(),
                );
            }
            if self.content.chars().count() > VALIDATION_LIMITS.collection_content_max_length {
                return Err(format!(
                    "Validation Error: Collection content exceeds max length {}",
                    VALIDATION_LIMITS.collection_content_max_length
                ));
            }
            let envelope: PubkyAppCollectionContent =
                serde_json::from_str(&self.content).map_err(|e| {
                    format!(
                        "Validation Error: Collection content must be a valid JSON envelope: {}",
                        e
                    )
                })?;
            if envelope.name.trim().is_empty() {
                return Err(
                    "Validation Error: Collection name must contain non-whitespace characters"
                        .into(),
                );
            }
            let name_chars = envelope.name.chars().count();
            let name_min = VALIDATION_LIMITS.collection_name_min_length;
            let name_max = VALIDATION_LIMITS.collection_name_max_length;
            if !(name_min..=name_max).contains(&name_chars) {
                return Err(format!(
                    "Validation Error: Collection name must be {}..={} characters",
                    name_min, name_max
                ));
            }
            if let Some(desc) = &envelope.description {
                if desc.chars().count() > VALIDATION_LIMITS.collection_description_max_length {
                    return Err(format!(
                        "Validation Error: Collection description exceeds {} characters",
                        VALIDATION_LIMITS.collection_description_max_length
                    ));
                }
            }
            if let Some(cover) = &envelope.cover_image {
                if cover.chars().count() > VALIDATION_LIMITS.post_attachment_url_max_length {
                    return Err(format!(
                        "Validation Error: Collection cover_image URL exceeds {} characters",
                        VALIDATION_LIMITS.post_attachment_url_max_length
                    ));
                }
                let parsed = Url::parse(cover).map_err(|_| {
                    "Validation Error: Collection cover_image must be a valid URL".to_string()
                })?;
                if !VALIDATION_LIMITS
                    .post_allowed_attachment_protocols
                    .contains(&parsed.scheme())
                {
                    let allowed = VALIDATION_LIMITS
                        .post_allowed_attachment_protocols
                        .iter()
                        .map(|p| format!("{p}://"))
                        .collect::<Vec<_>>()
                        .join(", ");
                    return Err(format!(
                        "Validation Error: Collection cover_image must use one of the allowed protocols: {allowed}"
                    ));
                }
            }
            if envelope.items.len() > VALIDATION_LIMITS.collection_items_max_count {
                return Err(format!(
                    "Validation Error: Collection cannot have more than {} items",
                    VALIDATION_LIMITS.collection_items_max_count
                ));
            }
            for (index, uri) in envelope.items.iter().enumerate() {
                validate_collection_item_uri(uri).map_err(|e| {
                    format!("Validation Error: Collection item at index {index}: {e}")
                })?;
            }
            return Ok(());
        }

        // Validate content length based on post kind
        let (max_length, kind_name) = match self.kind {
            PubkyAppPostKind::Short => (VALIDATION_LIMITS.post_short_content_max_length, "Short"),
            PubkyAppPostKind::Long => (VALIDATION_LIMITS.post_long_content_max_length, "Long"),
            PubkyAppPostKind::Image
            | PubkyAppPostKind::Video
            | PubkyAppPostKind::Link
            | PubkyAppPostKind::File => (
                VALIDATION_LIMITS.post_short_content_max_length,
                "Image/Video/Link/File",
            ),
            PubkyAppPostKind::Collection | PubkyAppPostKind::Unknown => {
                unreachable!("guarded by early-return above")
            }
        };

        if self.content.chars().count() > max_length {
            return Err(format!(
                "Validation Error: Post content exceeds maximum length for {} kind (max: {} characters)",
                kind_name, max_length
            ));
        }

        // Validate parent URI format if present
        if let Some(ref parent_uri) = self.parent {
            Url::parse(parent_uri).map_err(|_| {
                format!(
                    "Validation Error: Invalid parent URI format: {}",
                    parent_uri
                )
            })?;
        }

        // Validate embed URI format if present
        if let Some(ref embed) = self.embed {
            Url::parse(&embed.uri).map_err(|_| {
                format!("Validation Error: Invalid embed URI format: {}", embed.uri)
            })?;
        }

        // Validate attachments
        if let Some(attachments) = &self.attachments {
            if attachments.len() > VALIDATION_LIMITS.post_attachments_max_count {
                return Err(format!(
                    "Validation Error: Too many attachments (max: {})",
                    VALIDATION_LIMITS.post_attachments_max_count
                ));
            }

            for (index, url) in attachments.iter().enumerate() {
                if url.trim().is_empty() {
                    return Err(format!(
                        "Validation Error: Attachment URL at index {} cannot be empty",
                        index
                    ));
                }
                if url.chars().count() > VALIDATION_LIMITS.post_attachment_url_max_length {
                    return Err(format!(
                        "Validation Error: Attachment URL at index {} exceeds maximum length (max: {} characters)",
                        index, VALIDATION_LIMITS.post_attachment_url_max_length
                    ));
                }
                // Validate URL format and ensure it uses an allowed protocol
                let parsed_url = Url::parse(url).map_err(|_| {
                    format!(
                        "Validation Error: Invalid attachment URL format at index {}",
                        index
                    )
                })?;

                // Ensure the URL uses an allowed protocol
                if !VALIDATION_LIMITS
                    .post_allowed_attachment_protocols
                    .contains(&parsed_url.scheme())
                {
                    let allowed_protocols = VALIDATION_LIMITS
                        .post_allowed_attachment_protocols
                        .iter()
                        .map(|p| format!("{}://", p))
                        .collect::<Vec<_>>()
                        .join(", ");
                    return Err(format!(
                        "Validation Error: Attachment URL at index {} must use one of the allowed protocols: {}",
                        index, allowed_protocols
                    ));
                }
            }
        }

        Ok(())
    }
}

/// Strict canonical post-URI check for Collection items. Accepts only the
/// exact form `pubky://<pubky-id>/pub/pubky.app/posts/<post-id>`.
///
/// Deliberately avoids `Url::parse`: it silently strips userinfo and collapses
/// `..` path segments, smuggling non-canonical strings past a parse-and-recheck
/// approach. Splitting the raw string and delegating to `PubkyId::try_from`
/// (52-char z-base-32) and `validate_crockford_id` (13-char Crockford) enforces
/// the canonical 94-char form structurally.
fn validate_collection_item_uri(uri: &str) -> Result<(), String> {
    const PREFIX: &str = "pubky://";
    const MIDDLE: &str = "/pub/pubky.app/posts/";
    let rest = uri
        .strip_prefix(PREFIX)
        .ok_or_else(|| format!("must start with pubky://: {uri}"))?;
    let (host, post_id) = rest
        .split_once(MIDDLE)
        .ok_or_else(|| format!("must be a canonical post URI: {uri}"))?;
    PubkyId::try_from(host).map_err(|e| format!("invalid pubky-id in host: {e}"))?;
    validate_crockford_id(post_id).map_err(|e| format!("invalid post id: {e}"))?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    const TEST_PUBKY_ID: &str = "operrr8wsbpr3ue9d4qj41ge1kcc6r7fdiy6o3ugjrrhi4y77rdo";
    use crate::{traits::Validatable, APP_PATH, PUBLIC_PATH};

    #[test]
    fn test_create_id() {
        let post = PubkyAppPost::new(
            "Hello World!".to_string(),
            PubkyAppPostKind::Short,
            None,
            None,
            None,
        );

        let post_id = post.create_id();
        println!("Generated Post ID: {}", post_id);

        // Assert that the post ID is 13 characters long
        assert_eq!(post_id.len(), 13);
    }

    #[test]
    fn test_new() {
        let content = "This is a test post".to_string();
        let kind = PubkyAppPostKind::Short;
        let post = PubkyAppPost::new(content.clone(), kind.clone(), None, None, None);

        assert_eq!(post.content, content);
        assert_eq!(post.kind, kind);
        assert!(post.parent.is_none());
        assert!(post.embed.is_none());
        assert!(post.attachments.is_none());
    }

    #[test]
    fn test_create_path() {
        let post = PubkyAppPost::new(
            "Test post".to_string(),
            PubkyAppPostKind::Short,
            None,
            None,
            None,
        );

        let post_id = post.create_id();
        let path = PubkyAppPost::create_path(&post_id);

        // Check if the path starts with the expected prefix
        let prefix = format!("{}{}posts/", PUBLIC_PATH, APP_PATH);
        assert!(path.starts_with(&prefix));

        let expected_path_len = prefix.len() + post_id.len();
        assert_eq!(path.len(), expected_path_len);
    }

    #[test]
    fn test_sanitize() {
        let content = "  This is a test post with extra whitespace   ".to_string();
        let post = PubkyAppPost::new(
            content.clone(),
            PubkyAppPostKind::Short,
            Some("  pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/posts/0034A0X7NJ52G  ".to_string()),
            Some(PubkyAppPostEmbed {
                kind: PubkyAppPostKind::Link,
                uri: "  pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/files/0034A0X7Q3D80  ".to_string(),
            }),
            Some(vec![
                "pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/files/0034A0X7NJ52G".to_string(),
                "  pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/files/0034A0X7Q3D80  ".to_string(), // Should be trimmed
            ]),
        );

        let sanitized_post = post.sanitize();
        assert_eq!(sanitized_post.content, content.trim());

        // Parent URI should be trimmed
        assert!(sanitized_post.parent.is_some());
        let parent = sanitized_post.parent.unwrap();
        assert!(!parent.starts_with("  "));
        assert!(!parent.ends_with("  "));
        assert!(parent.starts_with("pubky://"));

        // Embed URI should be trimmed
        assert!(sanitized_post.embed.is_some());
        let embed = sanitized_post.embed.unwrap();
        assert!(!embed.uri.starts_with("  "));
        assert!(!embed.uri.ends_with("  "));
        assert!(embed.uri.starts_with("pubky://"));

        // Attachments should be trimmed
        assert!(sanitized_post.attachments.is_some());
        let attachments = sanitized_post.attachments.unwrap();
        assert_eq!(attachments.len(), 2);
        assert!(attachments[0].starts_with("pubky://"));
        assert!(attachments[1].starts_with("pubky://"));
        // Check that whitespace was trimmed
        assert!(!attachments[1].starts_with("  pubky://"));
        assert!(!attachments[1].ends_with("  "));
    }

    #[test]
    fn test_sanitize_trims_parent_and_embed() {
        let valid_parent_uri = "  pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/posts/0034A0X7NJ52G  ".to_string();
        let valid_embed_uri = "  pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/files/0034A0X7Q3D80  ".to_string();

        let post = PubkyAppPost::new(
            "Test content".to_string(),
            PubkyAppPostKind::Short,
            Some(valid_parent_uri.clone()),
            Some(PubkyAppPostEmbed {
                kind: PubkyAppPostKind::Link,
                uri: valid_embed_uri.clone(),
            }),
            None,
        );

        let sanitized_post = post.sanitize();

        // Check that parent URI was trimmed and normalized
        assert!(sanitized_post.parent.is_some());
        let parent = sanitized_post.parent.unwrap();
        assert!(!parent.starts_with("  "));
        assert!(!parent.ends_with("  "));
        assert!(parent.starts_with("pubky://"));

        // Check that embed URI was trimmed and normalized
        assert!(sanitized_post.embed.is_some());
        let embed = sanitized_post.embed.unwrap();
        assert!(!embed.uri.starts_with("  "));
        assert!(!embed.uri.ends_with("  "));
        assert!(embed.uri.starts_with("pubky://"));
    }

    #[test]
    fn test_validate() {
        let post = PubkyAppPost::new(
            "Valid content".to_string(),
            PubkyAppPostKind::Short,
            None,
            None,
            None,
        );

        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_invalid_id() {
        let post = PubkyAppPost::new(
            "Valid content".to_string(),
            PubkyAppPostKind::Short,
            None,
            None,
            None,
        );

        let invalid_id = "INVALIDID12345";
        let result = post.validate(Some(invalid_id));
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_invalid_parent_uri() {
        let post = PubkyAppPost::new(
            "Valid content".to_string(),
            PubkyAppPostKind::Short,
            Some("invalid uri".to_string()),
            None,
            None,
        );

        let id = post.create_id();
        let sanitized = post.sanitize();
        let result = sanitized.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Invalid parent URI format"));
    }

    #[test]
    fn test_validate_invalid_embed_uri() {
        let post = PubkyAppPost::new(
            "Valid content".to_string(),
            PubkyAppPostKind::Short,
            None,
            Some(PubkyAppPostEmbed {
                kind: PubkyAppPostKind::Link,
                uri: "invalid uri".to_string(),
            }),
            None,
        );

        let id = post.create_id();
        let sanitized = post.sanitize();
        let result = sanitized.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Invalid embed URI format"));
    }

    #[test]
    fn test_validate_invalid_attachment_uri() {
        let post = PubkyAppPost::new(
            "Valid content".to_string(),
            PubkyAppPostKind::Image,
            None,
            None,
            Some(vec![
                "pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/files/0034A0X7NJ52G".to_string(),
                "invalid uri".to_string(),
            ]),
        );

        let id = post.create_id();
        let sanitized = post.sanitize();
        let result = sanitized.validate(Some(&id));
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .contains("Invalid attachment URL format"));
    }

    #[test]
    fn test_try_from_valid() {
        let post_json = r#"
        {
            "content": "Hello World!",
            "kind": "short",
            "parent": null,
            "embed": null,
            "attachments": null
        }
        "#;

        let id = PubkyAppPost::new(
            "Hello World!".to_string(),
            PubkyAppPostKind::Short,
            None,
            None,
            None,
        )
        .create_id();

        let blob = post_json.as_bytes();
        let post = <PubkyAppPost as Validatable>::try_from(blob, &id).unwrap();

        assert_eq!(post.content, "Hello World!");
    }

    #[test]
    fn test_validate_reserved_keyword() {
        let post = PubkyAppPost::new(
            "[DELETED]".to_string(),
            PubkyAppPostKind::Short,
            None,
            None,
            None,
        );

        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("reserved keyword"));
    }

    #[test]
    fn test_try_from_invalid_content() {
        let content = "[DELETED]".to_string();
        let post_json = format!(
            r#"{{
                "content": "{}",
                "kind": "short",
                "parent": null,
                "embed": null,
                "attachments": null
            }}"#,
            content
        );

        let id = PubkyAppPost::new(content.clone(), PubkyAppPostKind::Short, None, None, None)
            .create_id();

        let blob = post_json.as_bytes();
        let result = <PubkyAppPost as Validatable>::try_from(blob, &id);

        // Should fail validation because [DELETED] is a reserved keyword
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("reserved keyword"));
    }

    #[test]
    fn test_validate_attachments_valid_protocols() {
        // Test allowed protocols (limited to post_attachments_max_count)
        let protocols = vec![
            "pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/files/0034A0X7NJ52G".to_string(),
            "https://example.com/file.png".to_string(),
            "http://example.com/file.jpg".to_string(),
        ];
        assert!(
            protocols.len() <= VALIDATION_LIMITS.post_attachments_max_count,
            "Test uses more than post_attachments_max_count"
        );

        let post = PubkyAppPost::new(
            "Valid content".to_string(),
            PubkyAppPostKind::Image,
            None,
            None,
            Some(protocols),
        );

        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_attachments_all_allowed_protocols() {
        // Test each allowed protocol individually to ensure all are accepted
        let allowed_protocols = vec![
            "pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/files/0034A0X7NJ52G",
            "http://example.com/file.jpg",
            "https://example.com/file.png",
        ];

        for protocol_url in allowed_protocols {
            let post = PubkyAppPost::new(
                "Valid content".to_string(),
                PubkyAppPostKind::Image,
                None,
                None,
                Some(vec![protocol_url.to_string()]),
            );

            let id = post.create_id();
            let result = post.validate(Some(&id));
            assert!(result.is_ok(), "Should accept protocol: {}", protocol_url);
        }
    }

    #[test]
    fn test_validate_attachments_too_many() {
        let mut attachments = Vec::new();
        for i in 0..VALIDATION_LIMITS.post_attachments_max_count + 1 {
            attachments.push(format!(
                "pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/files/{}",
                i
            ));
        }

        let post = PubkyAppPost::new(
            "Valid content".to_string(),
            PubkyAppPostKind::Image,
            None,
            None,
            Some(attachments),
        );

        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Too many attachments"));
    }

    #[test]
    fn test_validate_attachments_invalid_protocol() {
        // Test that disallowed protocols are rejected
        let invalid_protocols = vec!["ftp://example.com/file", "file:///path/to/file"];

        for invalid_url in invalid_protocols {
            let post = PubkyAppPost {
                content: "Valid content".to_string(),
                kind: PubkyAppPostKind::Image,
                parent: None,
                embed: None,
                attachments: Some(vec![invalid_url.to_string()]),
            };

            let id = post.create_id();
            let result = post.validate(Some(&id));
            assert!(result.is_err(), "Should reject protocol: {}", invalid_url);
            assert!(result.unwrap_err().contains("protocol"));
        }
    }

    #[test]
    fn test_validate_attachments_invalid_url_format() {
        // Create post directly without sanitization to test validation logic
        let post = PubkyAppPost {
            content: "Valid content".to_string(),
            kind: PubkyAppPostKind::Image,
            parent: None,
            embed: None,
            attachments: Some(vec!["not a valid url".to_string()]),
        };

        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .contains("Invalid attachment URL format"));
    }

    #[test]
    fn test_validate_attachments_url_too_long() {
        // Create a URL that exceeds post_attachment_url_max_length (200)
        // Base URL structure: "pubky://<52-char-user-id>/pub/pubky.app/files/" = ~80 chars
        // So we need a file ID that makes the total exceed 200
        let long_file_id = "a".repeat(150); // This will make total > 200
        let long_url = format!(
            "pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/files/{}",
            long_file_id
        );

        // Verify the URL is actually too long
        assert!(
            long_url.chars().count() > VALIDATION_LIMITS.post_attachment_url_max_length,
            "URL length {} should exceed {}",
            long_url.chars().count(),
            VALIDATION_LIMITS.post_attachment_url_max_length
        );

        let post = PubkyAppPost::new(
            "Valid content".to_string(),
            PubkyAppPostKind::Image,
            None,
            None,
            Some(vec![long_url]),
        );

        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("exceeds maximum length"));
    }

    #[test]
    fn test_validate_attachments_empty_url() {
        // Create post directly without sanitization to test validation logic
        let post = PubkyAppPost {
            content: "Valid content".to_string(),
            kind: PubkyAppPostKind::Image,
            parent: None,
            embed: None,
            attachments: Some(vec!["   ".to_string()]), // Whitespace only
        };

        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("cannot be empty"));
    }

    #[test]
    fn test_sanitize_attachments_preserves_all() {
        // Sanitize should preserve all attachments (just trim), validation rejects invalid
        let post = PubkyAppPost::new(
            "Valid content".to_string(),
            PubkyAppPostKind::Image,
            None,
            None,
            Some(vec![
                "pubky://6mfxozzqmb36rc9rgy3rykoyfghfao74n8igt5tf1boehproahoy/pub/pubky.app/files/0034A0X7NJ52G".to_string(),
                "https://example.com/file.jpg".to_string(),
                "  invalid url  ".to_string(), // Should be trimmed but preserved
            ]),
        );

        let id = post.create_id();
        let sanitized = post.sanitize();
        assert!(sanitized.attachments.is_some());
        let attachments = sanitized.attachments.as_ref().unwrap();
        assert_eq!(attachments.len(), 3); // All URLs should be preserved
        assert!(attachments[0].starts_with("pubky://"));
        assert!(attachments[1].starts_with("https://"));
        assert_eq!(attachments[2], "invalid url"); // Trimmed but preserved

        // Validation should reject the invalid URL
        let result = sanitized.validate(Some(&id));
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .contains("Invalid attachment URL format"));
    }

    #[test]
    fn test_sanitize_attachments_with_all_invalid_preserved() {
        // Sanitize should preserve all attachments, validation rejects invalid
        let post = PubkyAppPost::new(
            "Valid content".to_string(),
            PubkyAppPostKind::Image,
            None,
            None,
            Some(vec!["invalid url".to_string(), "not a url".to_string()]),
        );

        let id = post.create_id();
        let sanitized = post.sanitize();
        assert!(sanitized.attachments.is_some()); // Attachments preserved
        let attachments = sanitized.attachments.as_ref().unwrap();
        assert_eq!(attachments.len(), 2);

        // Validation should reject the invalid URLs
        let result = sanitized.validate(Some(&id));
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .contains("Invalid attachment URL format"));
    }

    #[test]
    fn test_validate_empty_post_rejected() {
        // Post with empty content, no embed, and no attachments should be rejected
        let post = PubkyAppPost::new("".to_string(), PubkyAppPostKind::Short, None, None, None);

        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .contains("must have content, an embed, or attachments"));
    }

    #[test]
    fn test_validate_empty_content_with_embed_accepted() {
        // Post with empty content but with embed should be valid
        let post = PubkyAppPost::new(
            "".to_string(),
            PubkyAppPostKind::Short,
            None,
            Some(PubkyAppPostEmbed {
                kind: PubkyAppPostKind::Short,
                uri: "pubky://user123/pub/pubky.app/posts/0033SSE3B1FQ0".to_string(),
            }),
            None,
        );

        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(
            result.is_ok(),
            "Post with embed but no content should be valid"
        );
    }

    #[test]
    fn test_validate_empty_content_with_attachments_accepted() {
        // Post with empty content but with attachments should be valid
        let post = PubkyAppPost::new(
            "".to_string(),
            PubkyAppPostKind::Image,
            None,
            None,
            Some(vec![
                "pubky://user123/pub/pubky.app/files/0034A0X7NJ52G".to_string()
            ]),
        );

        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(
            result.is_ok(),
            "Post with attachments but no content should be valid"
        );
    }

    // ----- v0.4.5 forwards-compat shim: PubkyAppPostKind::Unknown -----

    #[test]
    fn test_postkind_deserializes_unknown_kind_as_unknown() {
        // A future spec version adds a new kind that this binary doesn't know about.
        // The serde catch-all `Unknown` variant lets old binaries deserialize without panicking.
        let post_json = r#"
        {
            "content": "Hello",
            "kind": "totally-new-kind",
            "parent": null,
            "embed": null,
            "attachments": null
        }
        "#;

        let post: PubkyAppPost = serde_json::from_str(post_json).unwrap();
        assert_eq!(post.kind, PubkyAppPostKind::Unknown);
    }

    #[test]
    fn test_postkind_existing_variants_unchanged_after_unknown_added() {
        // Regression guard: adding Unknown with #[serde(other)] must not break round-tripping
        // any of the six existing lowercase string forms.
        for (s, expected) in [
            ("short", PubkyAppPostKind::Short),
            ("long", PubkyAppPostKind::Long),
            ("image", PubkyAppPostKind::Image),
            ("video", PubkyAppPostKind::Video),
            ("link", PubkyAppPostKind::Link),
            ("file", PubkyAppPostKind::File),
        ] {
            let json = format!(
                r#"{{"content":"x","kind":"{}","parent":null,"embed":null,"attachments":null}}"#,
                s
            );
            let post: PubkyAppPost = serde_json::from_str(&json).unwrap();
            assert_eq!(post.kind, expected, "kind={} did not round-trip", s);
            // re-serialize and ensure the lowercase string survives
            let re = serde_json::to_value(&post.kind).unwrap();
            assert_eq!(re.as_str(), Some(s));
        }
    }

    #[test]
    fn test_postkind_unknown_rejected_by_validator() {
        let post = PubkyAppPost {
            content: "x".to_string(),
            kind: PubkyAppPostKind::Unknown,
            parent: None,
            embed: None,
            attachments: None,
        };
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(
            result.unwrap_err().to_lowercase().contains("unknown"),
            "validator should mention 'unknown' in the error"
        );
    }

    #[test]
    fn test_postkind_unknown_displays_as_lowercase() {
        assert_eq!(PubkyAppPostKind::Unknown.to_string(), "unknown");
    }

    #[test]
    fn test_postkind_fromstr_rejects_unknown_strings() {
        // FromStr stays strict: it does NOT produce Unknown for arbitrary input.
        // Unknown is exclusively a serde catch-all.
        assert!(PubkyAppPostKind::from_str("foobar").is_err());
        assert!(PubkyAppPostKind::from_str("totally-new-kind").is_err());
    }

    #[test]
    fn test_is_known_returns_true_for_all_recognized_variants() {
        use PubkyAppPostKind::*;
        for k in [Short, Long, Image, Video, Link, File, Collection] {
            assert!(k.is_known(), "{k:?} should be known");
        }
    }

    #[test]
    fn test_is_known_returns_false_for_unknown() {
        assert!(!PubkyAppPostKind::Unknown.is_known());
    }

    #[test]
    fn test_post_deserializes_embed_with_unknown_kind_as_unknown() {
        // Embed kinds get the same forwards-compat treatment as top-level kinds:
        // an unrecognized embed.kind deserializes to Unknown rather than failing.
        let post_json = r#"
        {
            "content": "x",
            "kind": "short",
            "parent": null,
            "embed": {"kind": "totally-new-embed-kind", "uri": "pubky://x/pub/pubky.app/posts/01"},
            "attachments": null
        }
        "#;
        let post: PubkyAppPost = serde_json::from_str(post_json).unwrap();
        assert_eq!(post.embed.unwrap().kind, PubkyAppPostKind::Unknown);
    }

    #[test]
    fn test_postkind_unknown_embed_kind_rejected_by_validator() {
        // Counterpart to `test_postkind_unknown_rejected_by_validator`:
        // an Unknown embed.kind also fails validation, so the spec stays as
        // strict as before for posts that reach validation.
        let post = PubkyAppPost {
            content: "x".to_string(),
            kind: PubkyAppPostKind::Short,
            parent: None,
            embed: Some(PubkyAppPostEmbed {
                kind: PubkyAppPostKind::Unknown,
                uri: "pubky://x/pub/pubky.app/posts/01".to_string(),
            }),
            attachments: None,
        };
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        let err = result.unwrap_err().to_lowercase();
        assert!(
            err.contains("embed") && err.contains("unknown"),
            "validator should mention 'embed' and 'unknown' in the error, got: {}",
            err
        );
    }

    #[cfg(target_arch = "wasm32")]
    #[wasm_bindgen_test::wasm_bindgen_test]
    fn test_postkind_unknown_wasm_getter() {
        let post = PubkyAppPost {
            content: "x".to_string(),
            kind: PubkyAppPostKind::Unknown,
            parent: None,
            embed: None,
            attachments: None,
        };
        assert_eq!(post.kind(), "Unknown");
    }

    // ----- v0.5.0 Collection variant + PubkyAppCollectionContent envelope -----

    fn collection_envelope_json(name: &str, description: Option<&str>, items: &[String]) -> String {
        serde_json::to_string(&PubkyAppCollectionContent {
            name: name.to_string(),
            description: description.map(|d| d.to_string()),
            items: items.to_vec(),
            cover_image: None,
        })
        .unwrap()
    }

    fn make_collection_post(
        name: &str,
        description: Option<&str>,
        items: Option<Vec<String>>,
    ) -> PubkyAppPost {
        let items = items.unwrap_or_default();
        PubkyAppPost::new(
            collection_envelope_json(name, description, &items),
            PubkyAppPostKind::Collection,
            None,
            None,
            None,
        )
    }

    fn make_collection_post_with_cover(cover_image: Option<&str>) -> PubkyAppPost {
        let envelope = PubkyAppCollectionContent {
            name: "X".to_string(),
            description: None,
            items: vec![],
            cover_image: cover_image.map(|s| s.to_string()),
        };
        let content = serde_json::to_string(&envelope).expect("envelope serialization");
        PubkyAppPost::new(content, PubkyAppPostKind::Collection, None, None, None)
    }

    #[test]
    fn test_collection_post_roundtrip_valid() {
        let post = make_collection_post(
            "AI papers",
            Some("Best stuff"),
            Some(vec![
                format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/0034A0X7NJ52A"),
                format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/0034A0X7NJ52B"),
                format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/0034A0X7NJ52C"),
            ]),
        );
        let id = post.create_id();
        let blob = serde_json::to_vec(&post).unwrap();
        let parsed = <PubkyAppPost as Validatable>::try_from(&blob, &id).unwrap();
        assert_eq!(parsed.kind, PubkyAppPostKind::Collection);
        assert!(parsed.attachments.is_none());
        let envelope: PubkyAppCollectionContent = serde_json::from_str(&parsed.content).unwrap();
        assert_eq!(envelope.items.len(), 3);
    }

    #[test]
    fn test_collection_post_rejects_malformed_envelope() {
        let post = PubkyAppPost::new(
            "this is not JSON".to_string(),
            PubkyAppPostKind::Collection,
            None,
            None,
            None,
        );
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.contains("JSON envelope"),
            "expected JSON envelope error, got: {}",
            err
        );
    }

    #[test]
    fn test_collection_post_rejects_empty_name() {
        let post = make_collection_post("", None, None);
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("name"));
    }

    #[test]
    fn test_collection_post_rejects_oversized_name() {
        // 101 grapheme-ish chars; mix in emoji to confirm we count by unicode scalars, not bytes.
        let oversized = "a".repeat(99) + "🚀🚀";
        assert_eq!(oversized.chars().count(), 101);
        let post = make_collection_post(&oversized, None, None);
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("name"));
    }

    #[test]
    fn test_collection_post_accepts_max_name() {
        let exactly_100 = "a".repeat(100);
        assert_eq!(exactly_100.chars().count(), 100);
        let post = make_collection_post(&exactly_100, None, None);
        let id = post.create_id();
        assert!(post.validate(Some(&id)).is_ok());
    }

    #[test]
    fn test_collection_post_rejects_whitespace_only_name() {
        // Whitespace-only names pass `min_length=1` purely by char count, so
        // we reject them with a dedicated guard. Without that guard, a name
        // of `"    "` would be a 4-char valid name with no meaningful content.
        let post = make_collection_post("    ", None, None);
        let id = post.create_id();
        let err = post
            .validate(Some(&id))
            .expect_err("whitespace-only name must fail validation");
        assert!(
            err.contains("whitespace"),
            "error should mention whitespace, got: {err}"
        );
    }

    #[test]
    fn test_collection_post_counts_whitespace_in_name_length() {
        // Regression guard: the validator does NOT trim before counting. A
        // 99-char name padded with one space on each side is 101 chars and
        // must fail max=100. With the previous trim-then-count behavior this
        // would have been 99 chars and passed.
        let padded = format!(" {} ", "a".repeat(99));
        assert_eq!(padded.chars().count(), 101);
        let post = make_collection_post(&padded, None, None);
        let id = post.create_id();
        let err = post
            .validate(Some(&id))
            .expect_err("101-char padded name must fail max length");
        assert!(
            err.contains("1..=100"),
            "error should report the length range, got: {err}"
        );
    }

    #[test]
    fn test_collection_post_accepts_cover_image_pubky_uri() {
        let cover = format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/files/0034A0X7NJ52A");
        let post = make_collection_post_with_cover(Some(&cover));
        let id = post.create_id();
        assert!(post.validate(Some(&id)).is_ok());
    }

    #[test]
    fn test_collection_post_accepts_cover_image_https() {
        let post = make_collection_post_with_cover(Some("https://example.com/cover.png"));
        let id = post.create_id();
        assert!(post.validate(Some(&id)).is_ok());
    }

    #[test]
    fn test_collection_post_rejects_cover_image_invalid_url() {
        let post = make_collection_post_with_cover(Some("not a url"));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(
            err.contains("cover_image must be a valid URL"),
            "got: {err}"
        );
    }

    #[test]
    fn test_collection_post_rejects_cover_image_disallowed_protocol() {
        let post = make_collection_post_with_cover(Some("ftp://example.com/cover.png"));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(
            err.contains("cover_image must use one of the allowed protocols"),
            "got: {err}"
        );
    }

    #[test]
    fn test_collection_post_rejects_cover_image_too_long() {
        // post_attachment_url_max_length is 200; this URL exceeds it.
        let too_long = format!("https://example.com/{}", "a".repeat(200));
        let post = make_collection_post_with_cover(Some(&too_long));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(err.contains("cover_image URL exceeds"), "got: {err}");
    }

    #[test]
    fn test_collection_post_rejects_oversized_description() {
        let too_long = "a".repeat(501);
        let post = make_collection_post("X", Some(&too_long), None);
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("description"));
    }

    #[test]
    fn test_collection_post_accepts_empty_description() {
        // Explicit empty-string description is valid (the field is optional and
        // 0..=500 chars allowed).
        let post = make_collection_post("X", Some(""), None);
        let id = post.create_id();
        assert!(post.validate(Some(&id)).is_ok());
    }

    #[test]
    fn test_collection_post_accepts_max_description() {
        let exactly_500 = "a".repeat(500);
        assert_eq!(exactly_500.chars().count(), 500);
        let post = make_collection_post("X", Some(&exactly_500), None);
        let id = post.create_id();
        assert!(post.validate(Some(&id)).is_ok());
    }

    #[test]
    fn test_collection_post_rejects_missing_name() {
        // Envelope JSON without a `name` field at all (description-only).
        // Distinct from `test_collection_post_rejects_empty_name`, which sends
        // an empty string; this sends a missing key entirely.
        let envelope = r#"{ "description": "no name here" }"#.to_string();
        let post = PubkyAppPost::new(envelope, PubkyAppPostKind::Collection, None, None, None);
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.contains("name") || err.to_lowercase().contains("missing"),
            "expected name-required error, got: {err}"
        );
    }

    #[test]
    fn test_collection_post_rejects_parent() {
        let post = PubkyAppPost::new(
            collection_envelope_json("X", None, &[]),
            PubkyAppPostKind::Collection,
            Some("pubky://userA/pub/pubky.app/posts/0034A0X7NJ52A".to_string()),
            None,
            None,
        );
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.contains("parent or embed"),
            "expected parent-or-embed error, got: {}",
            err
        );
    }

    #[test]
    fn test_collection_post_rejects_embed() {
        let post = PubkyAppPost::new(
            collection_envelope_json("X", None, &[]),
            PubkyAppPostKind::Collection,
            None,
            Some(PubkyAppPostEmbed {
                kind: PubkyAppPostKind::Short,
                uri: "pubky://userA/pub/pubky.app/posts/0034A0X7NJ52A".to_string(),
            }),
            None,
        );
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("parent or embed"));
    }

    #[test]
    fn test_collection_post_accepts_100_items() {
        let items: Vec<String> = (0..100)
            .map(|i| format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/{:013}", i))
            .collect();
        let post = make_collection_post("Big list", None, Some(items));
        let id = post.create_id();
        assert!(post.validate(Some(&id)).is_ok());
    }

    #[test]
    fn test_collection_post_rejects_101_items() {
        let items: Vec<String> = (0..101)
            .map(|i| format!("pubky://userA/pub/pubky.app/posts/{:013}", i))
            .collect();
        let post = make_collection_post("Too big", None, Some(items));
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("100 items"));
    }

    #[test]
    fn test_postkind_collection_display_lowercase() {
        assert_eq!(PubkyAppPostKind::Collection.to_string(), "collection");
    }

    #[test]
    fn test_postkind_fromstr_collection() {
        assert_eq!(
            PubkyAppPostKind::from_str("collection").unwrap(),
            PubkyAppPostKind::Collection
        );
    }

    #[test]
    fn test_collection_post_accepts_zero_items() {
        // Curators may create a draft and add items later via edits.
        let post = make_collection_post("Drafts", None, None);
        let id = post.create_id();
        assert!(post.validate(Some(&id)).is_ok());
    }

    #[test]
    fn test_collection_envelope_tolerates_extra_fields() {
        // Forward-compat: the envelope intentionally does NOT use deny_unknown_fields,
        // so future minor versions can add fields without breaking older parsers.
        // Use a deliberately-fictional canary field name so this test stays
        // meaningful even after real fields land.
        let envelope_json = r#"{"name":"X","_forward_compat_canary":"future-only"}"#;
        let post = PubkyAppPost::new(
            envelope_json.to_string(),
            PubkyAppPostKind::Collection,
            None,
            None,
            None,
        );
        let id = post.create_id();
        assert!(
            post.validate(Some(&id)).is_ok(),
            "unknown envelope fields must be tolerated"
        );
    }

    #[test]
    fn test_collection_post_rejects_non_post_uri() {
        let post =
            make_collection_post("X", None, Some(vec!["ftp://example.com/file".to_string()]));
        let id = post.create_id();
        let result = post.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Collection item"));
    }

    #[test]
    fn test_collection_post_rejects_post_uri_with_invalid_post_id() {
        // 13 chars but not valid Crockford: contains hyphens which aren't in the alphabet.
        let uri = format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/abc-def-ghi-j");
        let post = make_collection_post("X", None, Some(vec![uri]));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(err.contains("invalid post id"), "got: {err}");
    }

    #[test]
    fn test_collection_post_rejects_post_uri_with_extra_path_segment() {
        // Extra segment lands inside the post-id slot, failing the 13-char
        // Crockford check.
        let uri = format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/0034A0X7NJ52A/extra");
        let post = make_collection_post("X", None, Some(vec![uri]));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(err.contains("invalid post id"), "got: {err}");
    }

    #[test]
    fn test_collection_post_rejects_post_uri_with_query_string() {
        // Query string lands inside the post-id slot and fails the Crockford
        // check (`?` and `=` aren't in the alphabet, and the length is wrong).
        let uri = format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/0034A0X7NJ52A?foo=bar");
        let post = make_collection_post("X", None, Some(vec![uri]));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(err.contains("invalid post id"), "got: {err}");
    }

    #[test]
    fn test_collection_post_rejects_post_uri_with_fragment() {
        // Same as the query-string case: `#` and the fragment body land in the
        // post-id slot and fail Crockford.
        let uri = format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/0034A0X7NJ52A#frag");
        let post = make_collection_post("X", None, Some(vec![uri]));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(err.contains("invalid post id"), "got: {err}");
    }

    #[test]
    fn test_collection_post_rejects_post_uri_with_trailing_slash() {
        // A trailing slash bloats the post-id past 13 chars.
        let uri = format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/0034A0X7NJ52A/");
        let post = make_collection_post("X", None, Some(vec![uri]));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(err.contains("invalid post id"), "got: {err}");
    }

    #[test]
    fn test_collection_post_rejects_post_uri_with_empty_post_id() {
        // Empty post-id segment fails the 13-char Crockford check.
        let uri = format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/");
        let post = make_collection_post("X", None, Some(vec![uri]));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(err.contains("invalid post id"), "got: {err}");
    }

    #[test]
    fn test_collection_post_rejects_userinfo_padding_bypass() {
        // `Url::parse(...).host_str()` strips userinfo, which would smuggle
        // arbitrary bytes past a parse-and-recheck approach. The strict
        // validator keeps the `JUNK@` in the host slot, failing the 52-char
        // PubkyId length check.
        let uri = format!(
            "pubky://AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@{TEST_PUBKY_ID}/pub/pubky.app/posts/0034A0X7NJ52A"
        );
        let post = make_collection_post("X", None, Some(vec![uri]));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(err.contains("invalid pubky-id"), "got: {err}");
    }

    #[test]
    fn test_collection_post_rejects_dot_dot_path_bypass() {
        // `Url::parse(...)` collapses `..` segments before path inspection,
        // which would smuggle a non-canonical raw path past a parse-and-recheck
        // approach. The strict validator splits the raw string, so the extra
        // segments land in the host slot and fail PubkyId.
        let uri = format!("pubky://{TEST_PUBKY_ID}/aa/bb/../../pub/pubky.app/posts/0034A0X7NJ52A");
        let post = make_collection_post("X", None, Some(vec![uri]));
        let id = post.create_id();
        let err = post.validate(Some(&id)).unwrap_err();
        assert!(err.contains("invalid pubky-id"), "got: {err}");
    }

    #[test]
    fn test_collection_post_accepts_canonical_max_length_uri() {
        // Success-side boundary: the longest valid canonical post URI is
        // pubky://<52-char-pubky-id>/pub/pubky.app/posts/<13-char-crockford>
        // which is exactly 94 chars. Validator must accept this.
        let uri = format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/0034A0X7NJ52A");
        assert_eq!(uri.chars().count(), 94);
        let post = make_collection_post("X", None, Some(vec![uri]));
        let id = post.create_id();
        assert!(post.validate(Some(&id)).is_ok());
    }

    #[test]
    fn test_collection_post_rejects_non_empty_attachments() {
        let post = PubkyAppPost::new(
            collection_envelope_json("X", None, &[]),
            PubkyAppPostKind::Collection,
            None,
            None,
            Some(vec![
                "pubky://userA/pub/pubky.app/posts/0034A0X7NJ52A".to_string()
            ]),
        );
        let id = post.create_id();
        let err = post
            .validate(Some(&id))
            .expect_err("Collection with non-empty post.attachments must be rejected");
        assert!(
            err.contains("post.attachments"),
            "expected anti-misuse error, got: {err}"
        );
    }

    #[test]
    fn test_collection_post_accepts_missing_items_field() {
        let envelope_json = r#"{"name":"X"}"#;
        let post = PubkyAppPost::new(
            envelope_json.to_string(),
            PubkyAppPostKind::Collection,
            None,
            None,
            None,
        );
        let id = post.create_id();
        assert!(
            post.validate(Some(&id)).is_ok(),
            "missing `items` field must deserialize as empty list via serde(default)"
        );
    }

    #[test]
    fn test_collection_post_envelope_at_max_size() {
        // 100 distinct valid pubky post URIs (max-count). Each exactly 94 chars.
        let items: Vec<String> = (0..VALIDATION_LIMITS.collection_items_max_count)
            .map(|i| format!("pubky://{TEST_PUBKY_ID}/pub/pubky.app/posts/{:013}", i))
            .collect();
        let max_name = "a".repeat(VALIDATION_LIMITS.collection_name_max_length);
        let max_desc = "b".repeat(VALIDATION_LIMITS.collection_description_max_length);
        let post = make_collection_post(&max_name, Some(&max_desc), Some(items));
        assert!(
            post.content.chars().count() < VALIDATION_LIMITS.collection_content_max_length,
            "envelope at max field sizes must fit under collection_content_max_length"
        );
        let id = post.create_id();
        assert!(post.validate(Some(&id)).is_ok());
    }

    #[test]
    fn test_existing_post_kinds_unchanged_with_collection() {
        // Regression: each of the six legacy lowercase kinds still round-trips after
        // adding Collection. Catches accidental ordering / serde changes.
        for s in ["short", "long", "image", "video", "link", "file"] {
            let json = format!(
                r#"{{"content":"x","kind":"{}","parent":null,"embed":null,"attachments":null}}"#,
                s
            );
            let post: PubkyAppPost = serde_json::from_str(&json).unwrap();
            let re = serde_json::to_value(&post.kind).unwrap();
            assert_eq!(re.as_str(), Some(s), "kind={} did not round-trip", s);
        }
    }

    #[cfg(target_arch = "wasm32")]
    #[wasm_bindgen_test::wasm_bindgen_test]
    fn test_postkind_collection_wasm_getter() {
        let post = PubkyAppPost {
            content: collection_envelope_json("X", None, &[]),
            kind: PubkyAppPostKind::Collection,
            parent: None,
            embed: None,
            attachments: None,
        };
        assert_eq!(post.kind(), "Collection");
    }

    #[cfg(target_arch = "wasm32")]
    #[wasm_bindgen_test::wasm_bindgen_test]
    fn test_create_collection_post_wasm_builder() {
        // End-to-end via the JS-facing builder:
        //   PubkySpecsBuilder.createCollectionPost(name, description?, attachments?)
        // builds the {name, description} envelope internally, packages it
        // into a kind=Collection PubkyAppPost, and returns a PostResult
        // ready to ship to the homeserver. JS callers don't have to
        // JSON-stringify the envelope themselves.
        use crate::PubkySpecsBuilder;
        let pubky_id = "operrr8wsbpr3ue9d4qj41ge1kcc6r7fdiy6o3ugjrrhi4y77rdo".to_string();
        let builder = PubkySpecsBuilder::new(pubky_id).expect("Failed to construct builder");
        let result = builder
            .create_collection_post(
                "My favorites".to_string(),
                Some("Best things".to_string()),
                Some(vec![
                    "pubky://operrr8wsbpr3ue9d4qj41ge1kcc6r7fdiy6o3ugjrrhi4y77rdo/pub/pubky.app/posts/0034A0X7NJ52A".to_string(),
                ]),
                Some("https://example.com/cover.png".to_string()),
            )
            .expect("createCollectionPost should succeed");

        let post = result.post();
        assert_eq!(post.kind, PubkyAppPostKind::Collection);
        assert!(post.attachments.is_none());
        let envelope: PubkyAppCollectionContent = serde_json::from_str(&post.content)
            .expect("Collection content must deserialize as PubkyAppCollectionContent");
        assert_eq!(envelope.name, "My favorites");
        assert_eq!(envelope.description.as_deref(), Some("Best things"));
        assert_eq!(envelope.items.len(), 1);
        assert_eq!(
            envelope.cover_image.as_deref(),
            Some("https://example.com/cover.png")
        );
    }
}