bc-envelope-pattern 0.14.0

Pattern matcher for Gordian Envelope
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
mod common;

use bc_envelope::prelude::*;
use bc_envelope_pattern::{DCBORPattern, Matcher, Pattern, format_paths};
use indoc::indoc;

#[test]
fn test_bool_pattern() {
    // Does not match non-boolean subjects.
    let envelope = Envelope::new(42);
    assert!(!Pattern::any_bool().matches(&envelope));
    assert!(!Pattern::bool(true).matches(&envelope));
    assert!(!Pattern::bool(false).matches(&envelope));

    // Matches a bare subject that is a boolean.
    let envelope = Envelope::new(true);
    assert!(Pattern::any_bool().matches(&envelope));
    assert!(Pattern::bool(true).matches(&envelope));
    assert!(!Pattern::bool(false).matches(&envelope));
    assert!(Pattern::cbor(true).matches(&envelope));
    assert!(!Pattern::cbor(false).matches(&envelope));

    // Matches a subject that is a boolean with an assertion.
    let envelope = envelope.add_assertion("an", "assertion");
    assert!(Pattern::any_bool().matches(&envelope));
    assert!(Pattern::bool(true).matches(&envelope));
    assert!(!Pattern::bool(false).matches(&envelope));
    assert!(Pattern::cbor(true).matches(&envelope));
    assert!(!Pattern::cbor(false).matches(&envelope));

    // The matched paths include the assertion. In other words, the
    // path just includes the envelope itself as its only element.
    let paths = Pattern::any_bool().paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        50d1019e NODE true [ "an": "assertion" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Matching a boolean and the subject in a traversal returns a path
    // where the first element is the original envelope and the second
    // element is the subject.
    let paths =
        Pattern::traverse(vec![Pattern::any_bool(), Pattern::any_subject()])
            .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        50d1019e NODE true [ "an": "assertion" ]
            27abdedd LEAF true
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn test_number_pattern() {
    // Does not match non-number subjects.
    let envelope = Envelope::new("string");
    assert!(!Pattern::any_number().matches(&envelope));
    assert!(!Pattern::number(42).matches(&envelope));

    // Matches a bare subject that is a number.
    let envelope = Envelope::new(42);
    assert!(Pattern::any_number().matches(&envelope));
    assert!(Pattern::number(42).matches(&envelope));
    assert!(!Pattern::number(43).matches(&envelope));
    assert!(Pattern::cbor(42).matches(&envelope));
    assert!(!Pattern::cbor(43).matches(&envelope));

    assert!(Pattern::number_range(40..=50).matches(&envelope));
    assert!(!Pattern::number_range(43..=50).matches(&envelope));
    assert!(Pattern::number_greater_than(41).matches(&envelope));
    assert!(!Pattern::number_greater_than(42).matches(&envelope));
    assert!(Pattern::number_less_than(43).matches(&envelope));
    assert!(!Pattern::number_less_than(42).matches(&envelope));
    assert!(Pattern::number_greater_than_or_equal(42).matches(&envelope));
    assert!(!Pattern::number_greater_than_or_equal(43).matches(&envelope));

    // Matches a subject that is a number with an assertion.
    let envelope = envelope.add_assertion("an", "assertion");
    assert!(Pattern::any_number().matches(&envelope));
    assert!(Pattern::number(42).matches(&envelope));

    // The matched paths include the assertion.
    let paths = Pattern::any_number().paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        6cb2ea4a NODE 42 [ "an": "assertion" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Matching a number and the subject in a traversal returns a path
    // where the first element is the original envelope and the second
    // element is the subject.
    let paths =
        Pattern::traverse(vec![Pattern::any_number(), Pattern::any_subject()])
            .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        6cb2ea4a NODE 42 [ "an": "assertion" ]
            7f83f7bd LEAF 42
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn test_text_pattern() {
    // Does not match non-text subjects.
    let envelope = Envelope::new(42);
    assert!(!Pattern::any_text().matches(&envelope));
    assert!(!Pattern::text("hello").matches(&envelope));

    // Matches a bare subject that is text.
    let envelope = Envelope::new("hello");
    assert!(Pattern::any_text().matches(&envelope));
    assert!(Pattern::text("hello").matches(&envelope));
    assert!(!Pattern::text("world").matches(&envelope));
    assert!(Pattern::cbor("hello").matches(&envelope));
    assert!(!Pattern::cbor("world").matches(&envelope));
    let regex = regex::Regex::new(r"^h.*o$").unwrap();
    assert!(Pattern::text_regex(regex.clone()).matches(&envelope));

    // Matches a subject that is text with an assertion.
    let envelope = envelope.add_assertion("greeting", "world");
    assert!(Pattern::any_text().matches(&envelope));
    assert!(Pattern::text("hello").matches(&envelope));
    assert!(!Pattern::text("world").matches(&envelope));
    assert!(Pattern::text_regex(regex).matches(&envelope));

    // The matched paths include the assertion.
    let paths = Pattern::any_text().paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        80a8c700 NODE "hello" [ "greeting": "world" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Matching a text and the subject in a traversal returns a path
    // where the first element is the original envelope and the second
    // element is the subject.
    let paths =
        Pattern::traverse(vec![Pattern::any_text(), Pattern::any_subject()])
            .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        80a8c700 NODE "hello" [ "greeting": "world" ]
            cb835593 LEAF "hello"
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn test_date_pattern() {
    // Does not match non-date subjects.
    let envelope = Envelope::new("2023-12-25");
    assert!(!Pattern::any_date().matches(&envelope));

    // Create a date envelope
    let date = Date::from_ymd(2023, 12, 25);
    let envelope = Envelope::new(date);

    // Matches a bare subject that is a date.
    assert!(Pattern::any_date().matches(&envelope));
    assert!(Pattern::date(date).matches(&envelope));
    assert!(!Pattern::date(Date::from_ymd(2023, 12, 24)).matches(&envelope));
    assert!(Pattern::cbor(date).matches(&envelope));
    assert!(!Pattern::cbor(Date::from_ymd(2023, 12, 24)).matches(&envelope));

    // Test date range matching
    let start = Date::from_ymd(2023, 12, 20);
    let end = Date::from_ymd(2023, 12, 30);
    assert!(Pattern::date_range(start..=end).matches(&envelope));

    let start = Date::from_ymd(2023, 12, 26);
    let end = Date::from_ymd(2023, 12, 30);
    assert!(!Pattern::date_range(start..=end).matches(&envelope));

    // Test ISO-8601 string matching
    assert!(Pattern::date_iso8601("2023-12-25").matches(&envelope));
    assert!(!Pattern::date_iso8601("2023-12-24").matches(&envelope));

    // Test regex matching
    let regex = regex::Regex::new(r"^2023-.*").unwrap();
    assert!(Pattern::date_regex(regex).matches(&envelope));

    let regex = regex::Regex::new(r"^2024-.*").unwrap();
    assert!(!Pattern::date_regex(regex).matches(&envelope));

    // Matches a subject that is a date with an assertion.
    let envelope = envelope.add_assertion("type", "christmas");
    assert!(Pattern::any_date().matches(&envelope));
    assert!(Pattern::date(date).matches(&envelope));

    // The matched paths include the assertion.
    let paths = Pattern::any_date().paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        20f45d77 NODE 2023-12-25 [ "type": "christmas" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Matching a date and the subject in a traversal returns a path
    // where the first element is the original envelope and the second
    // element is the subject.
    let paths =
        Pattern::traverse(vec![Pattern::any_date(), Pattern::any_subject()])
            .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        20f45d77 NODE 2023-12-25 [ "type": "christmas" ]
            3854ff69 LEAF 2023-12-25
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Test with date-time (not just date)
    let date_with_time = Date::from_ymd_hms(2023, 12, 25, 15, 30, 45);
    let envelope_with_time = Envelope::new(date_with_time);

    assert!(Pattern::any_date().matches(&envelope_with_time));
    assert!(
        Pattern::date_iso8601("2023-12-25T15:30:45Z")
            .matches(&envelope_with_time)
    );

    let regex = regex::Regex::new(r".*T15:30:45Z$").unwrap();
    assert!(Pattern::date_regex(regex).matches(&envelope_with_time));
}

#[test]
fn test_known_value_pattern() {
    use known_values;

    // Does not match non-known-value subjects.
    let envelope = Envelope::new("test");
    assert!(!Pattern::any_known_value().matches(&envelope));
    assert!(!Pattern::known_value(known_values::DATE).matches(&envelope));
    assert!(!Pattern::known_value_named("date").matches(&envelope));

    // Matches a bare subject that is a known value.
    let envelope = Envelope::new(known_values::DATE);
    assert!(Pattern::any_known_value().matches(&envelope));
    assert!(Pattern::known_value(known_values::DATE).matches(&envelope));
    assert!(!Pattern::known_value(known_values::LANGUAGE).matches(&envelope));
    assert!(Pattern::cbor(known_values::DATE).matches(&envelope));
    assert!(!Pattern::cbor(known_values::LANGUAGE).matches(&envelope));
    assert!(Pattern::known_value_named("date").matches(&envelope));
    assert!(!Pattern::known_value_named("language").matches(&envelope));

    // Matches a subject that is a known value with an assertion.
    let envelope = envelope.add_assertion("meaning", "timestamp");
    assert!(Pattern::any_known_value().matches(&envelope));
    assert!(Pattern::known_value(known_values::DATE).matches(&envelope));
    assert!(Pattern::known_value_named("date").matches(&envelope));
    assert!(!Pattern::known_value(known_values::LANGUAGE).matches(&envelope));
    assert!(!Pattern::known_value_named("language").matches(&envelope));

    // The matched paths include the assertion. In other words, the
    // path just includes the envelope itself as its only element.
    let paths = Pattern::any_known_value().paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        813f39cd NODE 'date' [ "meaning": "timestamp" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Test matching by name
    let paths = Pattern::known_value_named("date").paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        813f39cd NODE 'date' [ "meaning": "timestamp" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Matching a known value and the subject in a traversal returns a path
    // where the first element is the original envelope and the second
    // element is the subject.
    let paths = Pattern::traverse(vec![
        Pattern::any_known_value(),
        Pattern::any_subject(),
    ])
    .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        813f39cd NODE 'date' [ "meaning": "timestamp" ]
            2e40139b KNOWN_VALUE 'date'
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Test with unknown name should not match
    assert!(!Pattern::known_value_named("unknown_name").matches(&envelope));
}

#[test]
fn test_known_value_regex_pattern() {
    use regex::Regex;

    let value = known_values::DATE;
    let envelope =
        Envelope::new(value.clone()).add_assertion("meaning", "timestamp");

    // Test regex that matches "date" - Pattern should match
    let regex = Regex::new(r"^da.*").unwrap();
    assert!(Pattern::known_value_regex(regex).matches(&envelope));

    // Test regex that matches names ending with "te" - Pattern should match
    let regex = Regex::new(r".*te$").unwrap();
    assert!(Pattern::known_value_regex(regex).matches(&envelope));

    // Test case-insensitive regex - Pattern should match
    let regex = Regex::new(r"^date$").unwrap();
    assert!(Pattern::known_value_regex(regex).matches(&envelope));

    // Test regex that doesn't match - Pattern should not match
    let regex = Regex::new(r"^lang.*").unwrap();
    assert!(!Pattern::known_value_regex(regex).matches(&envelope));

    // Test with a different known value
    let language_value = known_values::LANGUAGE;
    let language_envelope = Envelope::new(language_value.clone());

    // This regex should match "language"
    let regex = Regex::new(r"lang.*").unwrap();
    assert!(Pattern::known_value_regex(regex).matches(&language_envelope));

    // This regex should not match "language"
    let regex = Regex::new(r"^da.*").unwrap();
    assert!(!Pattern::known_value_regex(regex).matches(&language_envelope));

    // Test with non-known-value envelope - Pattern should not match
    let text_envelope = Envelope::new("test");
    let regex = Regex::new(r".*").unwrap();
    assert!(!Pattern::known_value_regex(regex).matches(&text_envelope));

    // Test regex pattern in traversal patterns
    let regex = Regex::new(r".*te$").unwrap();
    let paths = Pattern::traverse(vec![
        Pattern::known_value_regex(regex),
        Pattern::any_subject(),
    ])
    .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        813f39cd NODE 'date' [ "meaning": "timestamp" ]
            2e40139b KNOWN_VALUE 'date'
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn test_byte_string_pattern() {
    // Does not match non-byte-string subjects.
    let envelope = Envelope::new("string");
    assert!(!Pattern::any_byte_string().matches(&envelope));
    assert!(!Pattern::byte_string(vec![1, 2, 3]).matches(&envelope));

    // Test with a byte string "Hello"
    let hello_bytes = vec![0x48, 0x65, 0x6c, 0x6c, 0x6f]; // "Hello"
    let envelope = Envelope::new(CBOR::to_byte_string(hello_bytes.clone()));

    // Matches any byte string
    assert!(Pattern::any_byte_string().matches(&envelope));

    // Matches exact byte string
    assert!(Pattern::byte_string(hello_bytes.clone()).matches(&envelope));
    assert!(
        Pattern::cbor(CBOR::to_byte_string(hello_bytes.clone()))
            .matches(&envelope)
    );

    assert!(!Pattern::byte_string(vec![1, 2, 3]).matches(&envelope));

    // Matches binary regex
    let regex = regex::bytes::Regex::new(r"^He.*o$").unwrap();
    assert!(Pattern::byte_string_binary_regex(regex).matches(&envelope));

    let non_matching_regex = regex::bytes::Regex::new(r"^World").unwrap();
    assert!(
        !Pattern::byte_string_binary_regex(non_matching_regex)
            .matches(&envelope)
    );

    // Matches a subject that is a byte string with an assertion.
    let envelope = envelope.add_assertion("type", "greeting");
    assert!(Pattern::any_byte_string().matches(&envelope));
    assert!(Pattern::byte_string(hello_bytes.clone()).matches(&envelope));

    let regex = regex::bytes::Regex::new(r".*llo.*").unwrap();
    assert!(Pattern::byte_string_binary_regex(regex).matches(&envelope));

    // The matched paths include the assertion.
    let paths = Pattern::any_byte_string().paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        19c2bef3 NODE Bytes(5) [ "type": "greeting" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Matching a byte string and the subject in a traversal returns a path
    // where the first element is the original envelope and the second
    // element is the subject.
    let paths = Pattern::traverse(vec![
        Pattern::any_byte_string(),
        Pattern::any_subject(),
    ])
    .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        19c2bef3 NODE Bytes(5) [ "type": "greeting" ]
            3a91d2eb LEAF Bytes(5)
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Test with binary regex patterns
    //
    // IMPORTANT: Binary regex patterns must use the (?s-u) flags to work
    // correctly with arbitrary bytes:
    // - (?s) allows '.' to match newline characters (like 0x0a)
    // - (?-u) disables unicode mode so '.' matches any byte, not just valid
    //   UTF-8 sequences
    //
    // Example matching any byte string ending with h'0001020304':
    // ```
    // (?s-u).*\x00\x01\x02\x03\x04$
    // ```

    let binary_data = vec![0x00, 0x01, 0xFF, 0xAB, 0xCD];
    let binary_envelope =
        Envelope::new(CBOR::to_byte_string(binary_data.clone()));

    // Test pattern that matches any byte containing 0xFF
    let regex = regex::bytes::Regex::new(r"(?s-u).*\xFF.*").unwrap();
    assert!(Pattern::byte_string_binary_regex(regex).matches(&binary_envelope));

    // Test pattern that matches specific sequence
    let regex = regex::bytes::Regex::new(r"(?s-u)\x00\x01").unwrap();
    assert!(Pattern::byte_string_binary_regex(regex).matches(&binary_envelope));

    // Test pattern that matches any 5 bytes (should match our binary data)
    let regex = regex::bytes::Regex::new(r"(?s-u)^.{5}$").unwrap();
    assert!(Pattern::byte_string_binary_regex(regex).matches(&binary_envelope));

    // Test pattern that doesn't match (starts with 0xFF)
    let regex = regex::bytes::Regex::new(r"(?s-u)^\xFF").unwrap();
    assert!(
        !Pattern::byte_string_binary_regex(regex).matches(&binary_envelope)
    );

    // Test pattern matching high bit bytes
    let regex = regex::bytes::Regex::new(r"(?s-u)[\x80-\xFF]").unwrap();
    assert!(Pattern::byte_string_binary_regex(regex).matches(&binary_envelope));
}

#[test]
fn test_array_pattern() {
    // Does not match non-array subjects.
    let envelope = Envelope::new("string");
    assert!(!Pattern::any_array().matches(&envelope));
    assert!(!Pattern::array_with_count(3).matches(&envelope));

    // Test with a CBOR array
    let array = vec![1, 2, 3];
    let envelope = Envelope::new(array.clone());

    // Matches the exact CBOR array
    assert!(Pattern::cbor(array).matches(&envelope));

    // Matches any array
    assert!(Pattern::any_array().matches(&envelope));

    // Matches exact count
    assert!(Pattern::array_with_count(3).matches(&envelope));
    assert!(!Pattern::array_with_count(5).matches(&envelope));

    // Matches count range
    assert!(Pattern::array_with_range(2..=4).matches(&envelope));
    assert!(!Pattern::array_with_range(5..=10).matches(&envelope));

    // Test with assertions
    let envelope = envelope.add_assertion("type", "list");
    assert!(Pattern::any_array().matches(&envelope));
    assert!(Pattern::array_with_count(3).matches(&envelope));

    // The matched paths include the assertion
    let paths = Pattern::any_array().paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        70db8c16 NODE [1, 2, 3] [ "type": "list" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Test with empty array
    let empty_array = Vec::<i32>::new().to_cbor();
    let empty_envelope = Envelope::new(empty_array);
    assert!(Pattern::any_array().matches(&empty_envelope));
    assert!(Pattern::array_with_count(0).matches(&empty_envelope));
    assert!(!Pattern::array_with_count(1).matches(&empty_envelope));

    // Test traversal patterns
    let paths =
        Pattern::traverse(vec![Pattern::any_array(), Pattern::any_subject()])
            .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        70db8c16 NODE [1, 2, 3] [ "type": "list" ]
            4abc3113 LEAF [1, 2, 3]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn test_map_pattern() {
    // Does not match non-map subjects.
    let envelope = Envelope::new("string");
    assert!(!Pattern::any_map().matches(&envelope));
    assert!(!Pattern::map_with_count(2).matches(&envelope));

    // Test with a CBOR map
    let mut map = Map::new();
    map.insert("key1", "value1");
    map.insert("key2", "value2");
    let envelope = Envelope::new(map.clone());

    // Matches the exact CBOR map
    assert!(Pattern::cbor(map).matches(&envelope));

    // Matches any map
    assert!(Pattern::any_map().matches(&envelope));

    // Matches exact count
    assert!(Pattern::map_with_count(2).matches(&envelope));
    assert!(!Pattern::map_with_count(3).matches(&envelope));

    // Matches count range
    assert!(Pattern::map_with_range(1..=3).matches(&envelope));
    assert!(!Pattern::map_with_range(5..=10).matches(&envelope));

    // Test with assertions
    let envelope = envelope.add_assertion("type", "dictionary");
    assert!(Pattern::any_map().matches(&envelope));
    assert!(Pattern::map_with_count(2).matches(&envelope));

    // The matched paths include the assertion
    let paths = Pattern::any_map().paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        1d96ee45 NODE {"key1": "value1", "key2": "value2"} [ "type": "dictionary" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Test with empty map
    let empty_map = Map::new();
    let empty_envelope = Envelope::new(empty_map);
    assert!(Pattern::any_map().matches(&empty_envelope));
    assert!(Pattern::map_with_count(0).matches(&empty_envelope));
    assert!(!Pattern::map_with_count(1).matches(&empty_envelope));

    // Test traversal patterns
    let paths =
        Pattern::traverse(vec![Pattern::any_map(), Pattern::any_subject()])
            .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        1d96ee45 NODE {"key1": "value1", "key2": "value2"} [ "type": "dictionary" ]
            0e16f9b4 LEAF {"key1": "value1", "key2": "value2"}
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn test_null_pattern() {
    // Does not match non-null subjects.
    let envelope = Envelope::new("string");
    assert!(!Pattern::null().matches(&envelope));

    // Matches a null subject.
    let envelope = Envelope::null();
    assert!(Pattern::null().matches(&envelope));

    // Matches a CBOR null value.
    assert!(Pattern::cbor(CBOR::null()).matches(&envelope));

    // Matches a subject that is null with an assertion.
    let envelope = envelope.add_assertion("type", "null_value");
    assert!(Pattern::null().matches(&envelope));

    // The matched paths include the assertion.
    let paths = Pattern::null().paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        a72948d7 NODE null [ "type": "null_value" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Matching a null and the subject in a traversal returns a path
    // where the first element is the original envelope and the second
    // element is the subject.
    let paths =
        Pattern::traverse(vec![Pattern::null(), Pattern::any_subject()])
            .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        a72948d7 NODE null [ "type": "null_value" ]
            b0b2988b LEAF null
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn test_tag_pattern() {
    // Does not match non-tagged subjects.
    let envelope = Envelope::new("string");
    assert!(!Pattern::any_tag().matches(&envelope));
    assert!(!Pattern::tagged(100, DCBORPattern::any()).matches(&envelope));

    // Test with a tagged CBOR value
    let tagged_cbor = CBOR::to_tagged_value(100, "tagged_content");
    let envelope = Envelope::new(tagged_cbor.clone());

    // Matches a tagged CBOR value
    assert!(Pattern::cbor(tagged_cbor).matches(&envelope));

    // Matches any tag
    assert!(Pattern::any_tag().matches(&envelope));

    // Matches specific tag value
    assert!(Pattern::tagged(100, DCBORPattern::any()).matches(&envelope));
    assert!(!Pattern::tagged(200, DCBORPattern::any()).matches(&envelope));

    // Matches specific tag object
    let tag = Tag::with_value(100);
    assert!(Pattern::tagged(tag, DCBORPattern::any()).matches(&envelope));

    let different_tag = Tag::with_value(200);
    assert!(
        !Pattern::tagged(different_tag, DCBORPattern::any()).matches(&envelope)
    );

    // Test with assertions
    let envelope = envelope.add_assertion("format", "tagged");
    assert!(Pattern::any_tag().matches(&envelope));
    assert!(Pattern::tagged(100, DCBORPattern::any()).matches(&envelope));

    // The matched paths include the assertion
    let paths = Pattern::any_tag().paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        b9457c8d NODE 100("tagged_content") [ "format": "tagged" ]
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);

    // Test traversal patterns
    let paths =
        Pattern::traverse(vec![Pattern::any_tag(), Pattern::any_subject()])
            .paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        b9457c8d NODE 100("tagged_content") [ "format": "tagged" ]
            a8e58a0d LEAF 100("tagged_content")
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn test_tag_pattern_named() {
    // Ensure tags are registered for testing
    bc_envelope::register_tags();

    // Test with registered tag (date tag = 1)
    let tagged_cbor = Date::from_ymd(2023, 12, 25).to_cbor();
    let envelope = Envelope::new(tagged_cbor);

    // Should match by name
    assert!(
        Pattern::tagged_name("date", DCBORPattern::any()).matches(&envelope)
    );

    // Should not match with wrong name
    assert!(
        !Pattern::tagged_name("unknown_tag", DCBORPattern::any())
            .matches(&envelope)
    );

    // Test with unregistered tag
    let unregistered_tagged_cbor =
        CBOR::to_tagged_value(999, "unregistered_content");
    let unregistered_envelope = Envelope::new(unregistered_tagged_cbor);

    // Should not match by any name since tag 999 is not registered
    assert!(
        !Pattern::tagged_name("date", DCBORPattern::any())
            .matches(&unregistered_envelope)
    );
    assert!(
        !Pattern::tagged_name("unknown_tag", DCBORPattern::any())
            .matches(&unregistered_envelope)
    );

    // Test with non-tagged content
    let text_envelope = Envelope::new("just text");
    assert!(
        !Pattern::tagged_name("date", DCBORPattern::any())
            .matches(&text_envelope)
    );

    // Test paths for matched envelope
    let paths =
        Pattern::tagged_name("date", DCBORPattern::any()).paths(&envelope);
    // expected-text-output-rubric:
    #[rustfmt::skip]
    let expected = indoc! {r#"
        3854ff69 LEAF 2023-12-25
    "#}.trim();
    assert_actual_expected!(format_paths(&paths), expected);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].len(), 1);
    assert_eq!(paths[0][0], envelope);

    // Test in traversal pattern
    let paths = Pattern::traverse(vec![
        Pattern::tagged_name("date", DCBORPattern::any()),
        Pattern::any_subject(),
    ])
    .paths(&envelope);
    assert_actual_expected!(format_paths(&paths), expected);
}

#[test]
fn test_tag_pattern_regex() {
    // Ensure tags are registered for testing
    bc_envelope::register_tags();

    // Test with registered tag (date tag = 1)
    let tagged_cbor = Date::from_ymd(2023, 12, 25).to_cbor();
    let envelope = Envelope::new(tagged_cbor);

    // Regex that should match "date"
    let regex = regex::Regex::new(r"^da.*").unwrap();
    assert!(
        Pattern::tagged_regex(regex.clone(), DCBORPattern::any())
            .matches(&envelope)
    );

    // Regex that should match names ending with "te"
    let regex = regex::Regex::new(r".*te$").unwrap();
    assert!(
        Pattern::tagged_regex(regex.clone(), DCBORPattern::any())
            .matches(&envelope)
    );

    // Regex that should not match "date"
    let regex = regex::Regex::new(r"^time.*").unwrap();
    assert!(
        !Pattern::tagged_regex(regex.clone(), DCBORPattern::any())
            .matches(&envelope)
    );

    // Test with unregistered tag
    let unregistered_tagged_cbor =
        CBOR::to_tagged_value(999, "unregistered_content");
    let unregistered_envelope = Envelope::new(unregistered_tagged_cbor);

    // Should not match any regex since tag 999 has no name in registry
    let regex = regex::Regex::new(r".*").unwrap(); // Match everything
    assert!(
        !Pattern::tagged_regex(regex.clone(), DCBORPattern::any())
            .matches(&unregistered_envelope)
    );

    // Test with non-tagged content
    let text_envelope = Envelope::new("just text");
    let regex = regex::Regex::new(r".*").unwrap();
    assert!(
        !Pattern::tagged_regex(regex.clone(), DCBORPattern::any())
            .matches(&text_envelope)
    );

    // Test paths for matched envelope
    let regex = regex::Regex::new(r"^da.*").unwrap();
    let paths =
        Pattern::tagged_regex(regex, DCBORPattern::any()).paths(&envelope);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].len(), 1);
    assert_eq!(paths[0][0], envelope);

    // Test in traversal pattern
    let regex = regex::Regex::new(r".*te$").unwrap();
    let paths = Pattern::traverse(vec![
        Pattern::tagged_regex(regex, DCBORPattern::any()),
        Pattern::any_subject(),
    ])
    .paths(&envelope);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].len(), 1);
    assert_eq!(paths[0][0], envelope);
}

#[test]
fn test_tag_pattern_with_bc_components_tags() {
    // Ensure all tags are registered
    bc_envelope::register_tags();

    // Test with a bc-components tag (e.g., digest tag)
    let digest_tag_value = 40001u64; // TAG_DIGEST from bc-tags
    let tagged_cbor =
        CBOR::to_tagged_value(digest_tag_value, [1u8, 2, 3, 4].as_slice());
    let envelope = Envelope::new(tagged_cbor);

    // Test tag name matching (assuming digest tag is registered with name
    // "digest") This will verify if the tag is properly registered in the
    // global registry
    let pattern = Pattern::tagged_name("digest", DCBORPattern::any());
    let matches = pattern.matches(&envelope);

    // Also test regex matching for digest-related tags
    let regex = regex::Regex::new(r".*digest.*").unwrap();
    let pattern_regex = Pattern::tagged_regex(regex, DCBORPattern::any());
    let matches_regex = pattern_regex.matches(&envelope);

    // At minimum, the specific tag value should work
    assert!(
        Pattern::tagged(digest_tag_value, DCBORPattern::any())
            .matches(&envelope)
    );
    assert!(Pattern::any_tag().matches(&envelope));

    // Print debug info for manual verification
    println!(
        "Digest tag {} matches by name: {}",
        digest_tag_value, matches
    );
    println!(
        "Digest tag {} matches by regex: {}",
        digest_tag_value, matches_regex
    );
}