cdx-core 0.7.1

Core library for reading, writing, and validating Codex Document Format (.cdx) files
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
#[cfg(test)]
mod tests {
    use crate::content::{Block, Content, Mark, Text};
    use crate::document::verification::ExtensionValidationReport;
    use crate::{Document, DocumentState};

    #[test]
    fn test_builder_basic() {
        let doc = Document::builder()
            .title("Test Document")
            .creator("Test Author")
            .add_heading(1, "Introduction")
            .add_paragraph("This is the first paragraph.")
            .build()
            .unwrap();

        assert_eq!(doc.title(), "Test Document");
        assert_eq!(doc.creators(), vec!["Test Author"]);
        assert_eq!(doc.content().len(), 2);
        assert_eq!(doc.state(), DocumentState::Draft);
    }

    #[test]
    fn test_builder_with_description() {
        let doc = Document::builder()
            .title("Report")
            .creator("Author")
            .description("A detailed report")
            .language("en")
            .build()
            .unwrap();

        assert_eq!(doc.dublin_core().description(), Some("A detailed report"));
        assert_eq!(doc.dublin_core().language(), Some("en"));
    }

    #[test]
    fn test_document_round_trip() {
        let original = Document::builder()
            .title("Round Trip Test")
            .creator("Tester")
            .add_heading(1, "Title")
            .add_paragraph("Content")
            .build()
            .unwrap();

        // Write to bytes
        let bytes = original.to_bytes().unwrap();

        // Read back
        let loaded = Document::from_bytes(bytes).unwrap();

        assert_eq!(loaded.title(), "Round Trip Test");
        assert_eq!(loaded.creators(), vec!["Tester"]);
        assert_eq!(loaded.content().len(), 2);
    }

    #[test]
    fn test_compute_id() {
        let doc = Document::builder()
            .title("ID Test")
            .creator("Author")
            .add_paragraph("Test content")
            .build()
            .unwrap();

        let id = doc.compute_id().unwrap();
        assert!(!id.is_pending());
        assert_eq!(id.algorithm(), crate::HashAlgorithm::Sha256);
    }

    #[test]
    fn test_verification() {
        let doc = Document::builder()
            .title("Verify Test")
            .creator("Author")
            .add_paragraph("Content")
            .build()
            .unwrap();

        // Fresh documents with pending hashes should verify
        let report = doc.verify().unwrap();
        assert!(report.is_valid());
    }

    // Extension validation tests

    #[test]
    fn test_extension_validation_no_extensions() {
        let doc = Document::builder()
            .title("Simple Doc")
            .creator("Author")
            .add_paragraph("Just plain text")
            .build()
            .unwrap();

        let report = doc.validate_extensions();
        assert!(report.is_valid());
        assert!(report.used_namespaces.is_empty());
        assert!(report.undeclared.is_empty());
    }

    #[test]
    fn test_extension_validation_with_extension_block() {
        use crate::extensions::ExtensionBlock;

        let ext_block = Block::Extension(
            ExtensionBlock::new("forms", "textInput")
                .with_id("name-field")
                .with_attributes(serde_json::json!({"label": "Name"})),
        );

        let content = Content::new(vec![
            Block::paragraph(vec![Text::plain("Fill out this form:")]),
            ext_block,
        ]);

        let doc = Document::builder()
            .title("Form Doc")
            .creator("Author")
            .with_content(content)
            .build()
            .unwrap();

        let report = doc.validate_extensions();
        assert!(!report.is_valid()); // undeclared extension
        assert_eq!(report.used_namespaces, vec!["forms"]);
        assert_eq!(report.undeclared, vec!["forms"]);
        assert!(report.warnings[0].contains("forms"));
    }

    #[test]
    fn test_extension_validation_with_declared_extension() {
        use crate::extensions::ExtensionBlock;
        use crate::manifest::Extension;

        let ext_block = Block::Extension(
            ExtensionBlock::new("semantic", "citation")
                .with_attributes(serde_json::json!({"refs": ["smith2023"]})),
        );

        let content = Content::new(vec![
            Block::paragraph(vec![Text::plain("According to research")]),
            ext_block,
        ]);

        let mut doc = Document::builder()
            .title("Academic Doc")
            .creator("Author")
            .with_content(content)
            .build()
            .unwrap();

        // Declare the extension
        doc.manifest_mut()
            .extensions
            .push(Extension::required("codex.semantic", "0.1"));

        let report = doc.validate_extensions();
        assert!(report.is_valid());
        assert_eq!(report.used_namespaces, vec!["semantic"]);
        assert!(report.undeclared.is_empty());
    }

    #[test]
    fn test_extension_validation_with_extension_marks() {
        use crate::content::ExtensionMark;

        let citation_mark = Mark::Extension(ExtensionMark::citation("smith2023"));
        let text_with_citation = Text::with_marks("important finding", vec![citation_mark]);

        let content = Content::new(vec![Block::paragraph(vec![text_with_citation])]);

        let doc = Document::builder()
            .title("Cited Doc")
            .creator("Author")
            .with_content(content)
            .build()
            .unwrap();

        let report = doc.validate_extensions();
        assert!(!report.is_valid()); // undeclared
        assert_eq!(report.used_namespaces, vec!["semantic"]);
        assert_eq!(report.undeclared, vec!["semantic"]);
    }

    #[test]
    fn test_extension_validation_mixed() {
        use crate::content::ExtensionMark;
        use crate::extensions::ExtensionBlock;
        use crate::manifest::Extension;

        // Create content with multiple extensions
        let citation_mark = Mark::Extension(ExtensionMark::citation("smith2023"));
        let entity_mark =
            Mark::Extension(ExtensionMark::entity("https://wikidata.org/Q937", "person"));

        let form_block = Block::Extension(
            ExtensionBlock::new("forms", "textInput")
                .with_id("email")
                .with_attributes(serde_json::json!({"label": "Email"})),
        );

        let content = Content::new(vec![
            Block::paragraph(vec![
                Text::with_marks("Einstein", vec![entity_mark]),
                Text::plain(" published his theory "),
                Text::with_marks("(ref)", vec![citation_mark]),
            ]),
            form_block,
        ]);

        let mut doc = Document::builder()
            .title("Mixed Extensions")
            .creator("Author")
            .with_content(content)
            .build()
            .unwrap();

        // Only declare semantic, not forms
        doc.manifest_mut()
            .extensions
            .push(Extension::required("codex.semantic", "0.1"));

        let report = doc.validate_extensions();
        assert!(!report.is_valid()); // forms not declared
        assert!(report.used_namespaces.contains(&"semantic".to_string()));
        assert!(report.used_namespaces.contains(&"forms".to_string()));
        assert_eq!(report.undeclared, vec!["forms"]);
        assert!(report.warnings.len() == 1);
    }

    #[test]
    fn test_extension_validation_report_methods() {
        let mut report = ExtensionValidationReport::default();
        assert!(report.is_valid());
        assert!(!report.has_warnings());

        report.undeclared.push("test".to_string());
        report.warnings.push("Test warning".to_string());
        assert!(!report.is_valid());
        assert!(report.has_warnings());
    }

    // ===== Extension File I/O Tests =====

    #[test]
    fn test_comments_round_trip() {
        use crate::extensions::{Collaborator, Comment, CommentThread};

        let mut doc = Document::builder()
            .title("Comments Test")
            .creator("Author")
            .add_paragraph("Content to comment on")
            .build()
            .unwrap();

        // Create a comment thread
        let mut thread = CommentThread::new();
        let author = Collaborator::new("Alice");
        let comment = Comment::new("c1", "block-1", author, "This is a test comment");
        thread.add(comment);

        // Set comments
        doc.set_comments(thread).unwrap();
        assert!(doc.has_comments());

        // Round-trip through bytes
        let bytes = doc.to_bytes().unwrap();
        let loaded = Document::from_bytes(bytes).unwrap();

        assert!(loaded.has_comments());
        let loaded_thread = loaded.comments().unwrap();
        assert_eq!(loaded_thread.comments.len(), 1);
        assert_eq!(loaded_thread.comments[0].id, "c1");
        assert_eq!(loaded_thread.comments[0].content, "This is a test comment");
    }

    #[test]
    fn test_phantom_clusters_round_trip() {
        use crate::anchor::ContentAnchor;
        use crate::extensions::{
            Phantom, PhantomCluster, PhantomClusters, PhantomContent, PhantomPosition, PhantomScope,
        };

        let mut doc = Document::builder()
            .title("Phantoms Test")
            .creator("Author")
            .add_paragraph("Content with phantoms")
            .build()
            .unwrap();

        // Create phantom clusters
        let mut clusters = PhantomClusters::new();
        let position = PhantomPosition::new(100.0, 200.0);
        let content = PhantomContent::paragraph("Alternative text");
        let phantom = Phantom::new("phantom-1", position, content);
        let cluster =
            PhantomCluster::new("cluster-1", ContentAnchor::block("block-1"), "Test cluster")
                .with_phantom(phantom)
                .with_scope(PhantomScope::Shared);
        clusters.add_cluster(cluster);

        // Set phantom clusters
        doc.set_phantom_clusters(clusters).unwrap();
        assert!(doc.has_phantom_clusters());

        // Round-trip through bytes
        let bytes = doc.to_bytes().unwrap();
        let loaded = Document::from_bytes(bytes).unwrap();

        assert!(loaded.has_phantom_clusters());
        let loaded_clusters = loaded.phantom_clusters().unwrap();
        assert_eq!(loaded_clusters.len(), 1);
        assert_eq!(loaded_clusters.clusters[0].id, "cluster-1");
    }

    #[test]
    fn test_form_data_round_trip() {
        use crate::extensions::FormData;

        let mut doc = Document::builder()
            .title("Form Data Test")
            .creator("Author")
            .add_paragraph("Form content")
            .build()
            .unwrap();

        // Create form data
        let mut form_data = FormData::new();
        form_data.set("name", serde_json::json!("John Doe"));
        form_data.set("email", serde_json::json!("john@example.com"));
        form_data.set("age", serde_json::json!(30));

        // Set form data
        doc.set_form_data(form_data).unwrap();
        assert!(doc.has_form_data());

        // Round-trip through bytes
        let bytes = doc.to_bytes().unwrap();
        let loaded = Document::from_bytes(bytes).unwrap();

        assert!(loaded.has_form_data());
        let loaded_form = loaded.form_data().unwrap();
        assert_eq!(
            loaded_form.get("name"),
            Some(&serde_json::json!("John Doe"))
        );
        assert_eq!(
            loaded_form.get("email"),
            Some(&serde_json::json!("john@example.com"))
        );
        assert_eq!(loaded_form.get("age"), Some(&serde_json::json!(30)));
    }

    #[test]
    fn test_bibliography_round_trip() {
        use crate::extensions::{Bibliography, BibliographyEntry, CitationStyle, EntryType};

        let mut doc = Document::builder()
            .title("Bibliography Test")
            .creator("Author")
            .add_paragraph("Content with citations")
            .build()
            .unwrap();

        // Create bibliography
        let mut bibliography = Bibliography::new(CitationStyle::Apa);
        let entry = BibliographyEntry::new("smith2023", EntryType::Article, "Test Article");
        bibliography.add_entry(entry);

        // Set bibliography
        doc.set_bibliography(bibliography).unwrap();
        assert!(doc.has_bibliography());

        // Round-trip through bytes
        let bytes = doc.to_bytes().unwrap();
        let loaded = Document::from_bytes(bytes).unwrap();

        assert!(loaded.has_bibliography());
        let loaded_bib = loaded.bibliography().unwrap();
        assert_eq!(loaded_bib.len(), 1);
        assert_eq!(loaded_bib.style, CitationStyle::Apa);
        assert!(loaded_bib.contains("smith2023"));
    }

    #[test]
    fn test_jsonld_round_trip() {
        use crate::extensions::JsonLdMetadata;
        use serde_json::json;

        let mut doc = Document::builder()
            .title("JSON-LD Test")
            .creator("Author")
            .add_paragraph("Content with structured data")
            .build()
            .unwrap();

        // Create JSON-LD metadata
        let mut jsonld = JsonLdMetadata::new();
        jsonld.add_node(json!({
            "@type": "Article",
            "name": "Test Article",
            "author": {
                "@type": "Person",
                "name": "Test Author"
            }
        }));

        // Set JSON-LD metadata
        doc.set_jsonld_metadata(jsonld).unwrap();
        assert!(doc.has_jsonld_metadata());

        // Round-trip through bytes
        let bytes = doc.to_bytes().unwrap();
        let loaded = Document::from_bytes(bytes).unwrap();

        assert!(loaded.has_jsonld_metadata());
        let loaded_jsonld = loaded.jsonld_metadata().unwrap();
        assert_eq!(loaded_jsonld.graph.len(), 1);
        assert!(loaded_jsonld
            .context
            .contains(&"https://schema.org".to_string()));
    }

    #[test]
    fn test_clear_extension_data() {
        use crate::extensions::{Bibliography, CitationStyle, CommentThread, FormData};

        let mut doc = Document::builder()
            .title("Clear Test")
            .creator("Author")
            .add_paragraph("Content")
            .build()
            .unwrap();

        // Set all extension data
        doc.set_comments(CommentThread::new()).unwrap();
        doc.set_form_data(FormData::new()).unwrap();
        doc.set_bibliography(Bibliography::new(CitationStyle::Chicago))
            .unwrap();

        assert!(doc.has_comments());
        assert!(doc.has_form_data());
        assert!(doc.has_bibliography());

        // Clear each
        doc.clear_comments().unwrap();
        doc.clear_form_data().unwrap();
        doc.clear_bibliography().unwrap();

        assert!(!doc.has_comments());
        assert!(!doc.has_form_data());
        assert!(!doc.has_bibliography());
    }

    #[test]
    fn test_extension_data_mutable_access() {
        use crate::extensions::{Collaborator, Comment, CommentThread};

        let mut doc = Document::builder()
            .title("Mutable Test")
            .creator("Author")
            .add_paragraph("Content")
            .build()
            .unwrap();

        // Set initial comments
        let mut thread = CommentThread::new();
        let author = Collaborator::new("Alice");
        thread.add(Comment::new("c1", "block-1", author.clone(), "First"));
        doc.set_comments(thread).unwrap();

        // Modify through mutable reference
        if let Some(comments) = doc.comments_mut().unwrap() {
            comments.add(Comment::new("c2", "block-2", author, "Second"));
        }

        assert_eq!(doc.comments().unwrap().comments.len(), 2);
    }

    #[cfg(any(feature = "signatures", feature = "encryption"))]
    #[test]
    fn test_write_to_clears_stale_security_ref() {
        use crate::manifest::SecurityRef;

        // Create a document and add a signature
        let mut doc = Document::builder()
            .title("Security Ref Test")
            .creator("Author")
            .add_paragraph("Content")
            .build()
            .unwrap();

        // Manually set a security reference as if it had signatures before
        doc.manifest_mut().security = Some(SecurityRef {
            signatures: Some("security/signatures.json".to_string()),
            encryption: None,
        });

        // Save and reload — signature_file is None so the security ref should be cleared
        let bytes = doc.to_bytes().unwrap();
        let loaded = Document::from_bytes(bytes).unwrap();
        assert!(
            loaded.manifest().security.is_none(),
            "security ref should be None when no signatures or encryption exist"
        );
    }

    #[test]
    fn test_root_document_can_freeze_after_set_lineage() {
        let mut doc = Document::builder()
            .title("Root Doc")
            .creator("Author")
            .add_paragraph("Content")
            .build()
            .unwrap();

        // Set root lineage
        doc.set_lineage(None, 1, None).unwrap();
        assert!(doc.manifest().lineage.is_some());
    }

    #[cfg(feature = "signatures")]
    #[test]
    fn test_freeze_lineage_error_message_mentions_set_lineage() {
        let mut doc = Document::builder()
            .title("Lineage Error Test")
            .creator("Author")
            .add_paragraph("Content")
            .build()
            .unwrap();

        doc.submit_for_review().unwrap();

        // Add a dummy signature so the signature check passes
        #[cfg(feature = "signatures")]
        {
            use crate::security::{Signature, SignatureAlgorithm, SignerInfo};
            let sig = Signature::new(
                "sig-1",
                SignatureAlgorithm::ES256,
                SignerInfo::new("test@example.com"),
                "dummysig",
            );
            doc.add_signature(sig).unwrap();
        }

        let err = doc.freeze().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("set_lineage"),
            "error message should mention set_lineage, got: {msg}"
        );
    }

    // Phase 2: Document mutation consistency tests

    #[test]
    fn test_set_extension_updates_modified() {
        use crate::extensions::Bibliography;

        let mut doc = Document::builder()
            .title("Extension Modified Test")
            .creator("Author")
            .add_paragraph("Content")
            .build()
            .unwrap();

        let before = doc.manifest().modified;
        std::thread::sleep(std::time::Duration::from_millis(10));

        doc.set_bibliography(Bibliography::default()).unwrap();
        assert!(
            doc.manifest().modified > before,
            "modified timestamp should update after set_bibliography"
        );
    }

    #[test]
    fn test_clear_extension_updates_modified() {
        use crate::extensions::Bibliography;

        let mut doc = Document::builder()
            .title("Clear Extension Modified Test")
            .creator("Author")
            .add_paragraph("Content")
            .build()
            .unwrap();

        doc.set_bibliography(Bibliography::default()).unwrap();
        let before = doc.manifest().modified;
        std::thread::sleep(std::time::Duration::from_millis(10));

        doc.clear_bibliography().unwrap();
        assert!(
            doc.manifest().modified > before,
            "modified timestamp should update after clear_bibliography"
        );
    }

    #[cfg(feature = "encryption")]
    #[test]
    fn test_set_encryption_updates_modified() {
        use crate::security::{EncryptionAlgorithm, EncryptionMetadata};

        let mut doc = Document::builder()
            .title("Encryption Modified Test")
            .creator("Author")
            .add_paragraph("Content")
            .build()
            .unwrap();

        let before = doc.manifest().modified;
        std::thread::sleep(std::time::Duration::from_millis(10));

        let meta = EncryptionMetadata {
            algorithm: EncryptionAlgorithm::Aes256Gcm,
            kdf: None,
            wrapped_key: None,
            key_management: None,
            recipients: Vec::new(),
        };
        doc.set_encryption(meta).unwrap();
        assert!(
            doc.manifest().modified > before,
            "modified timestamp should update after set_encryption"
        );
    }
}