omni-dev 0.26.0

A powerful Git commit message analysis and amendment toolkit
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
//! ADF per-context mark allow-lists and per-mark attribute schemas.
//!
//! Validates the `marks` array on each ADF node against:
//!
//! 1. **Allow-list by context.** Marks on an *inline* node (text, hardBreak,
//!    mention, …) are checked against the *parent* container's inline-mark
//!    allow-list — e.g. `code` on text inside `paragraph` is fine, on text
//!    inside `heading` it is not. Marks on a *block* node (paragraph,
//!    heading, tableCell, …) are checked against that node type's
//!    block-mark allow-list.
//!
//! 2. **Per-mark attribute schema.** Re-uses the
//!    [`crate::atlassian::adf_attr_schema::AttrSchema`] /
//!    [`crate::atlassian::adf_attr_schema::AttrType`] machinery from the
//!    second sub-PR. `link.href` must parse as a URL, `subsup.type` must
//!    be `sub` or `sup`, etc.
//!
//! # Source of truth
//!
//! Lists are transcribed from
//! `packages/adf-schema/src/schema/marks/<mark>.ts` and the per-node
//! `inlineContent` / `marks` declarations in the upstream tarball pinned by
//! [`crate::atlassian::adf_schema::SCHEMA_VERSION`]. Mark groups (e.g.
//! `formatting`) are flattened into per-context allow-lists for direct
//! lookup; the trade-off (a slightly larger table vs. a runtime group
//! resolver) is the same one ADR-0023 made for content models.
//!
//! # Forward compatibility
//!
//! - Unknown parent / node types: no mark validation runs (permissive).
//! - Unknown mark types under known parents: flagged as
//!   [`crate::atlassian::adf_schema::AdfSchemaViolation::DisallowedMark`].
//!   `unsupportedMark` (the round-trip preservation wrapper for marks) is
//!   accepted everywhere via the same escape-hatch convention as
//!   `unsupportedBlock` / `unsupportedInline`.

use std::collections::HashMap;
use std::sync::LazyLock;

use serde_json::Value;

use crate::atlassian::adf_attr_schema::{AttrPresence, AttrSchema, AttrType};
use crate::atlassian::adf_schema::AdfSchemaViolation;

// -----------------------------------------------------------------------------
// Mark allow-lists by context
// -----------------------------------------------------------------------------

/// Inline marks shared by most inline-content containers (paragraph,
/// taskItem, decisionItem, caption).
const STD_INLINE_MARKS: &[&str] = &[
    "annotation",
    "backgroundColor",
    "code",
    "em",
    "link",
    "strike",
    "strong",
    "subsup",
    "textColor",
    "underline",
];

/// Heading inline marks — same as STD_INLINE_MARKS minus `code` (upstream
/// `heading` content model excludes code marks since the heading text is
/// styled by the heading itself).
const HEADING_INLINE_MARKS: &[&str] = &[
    "annotation",
    "backgroundColor",
    "em",
    "link",
    "strike",
    "strong",
    "subsup",
    "textColor",
    "underline",
];

/// `codeBlock` text accepts no marks — code blocks are literal text.
const CODE_BLOCK_INLINE_MARKS: &[&str] = &[];

/// `caption` — narrower than std (no `code`, no `annotation`).
const CAPTION_INLINE_MARKS: &[&str] = &[
    "backgroundColor",
    "em",
    "link",
    "strike",
    "strong",
    "subsup",
    "textColor",
    "underline",
];

/// Block-level marks per block node type.
const PARAGRAPH_BLOCK_MARKS: &[&str] = &["alignment", "indentation"];
const HEADING_BLOCK_MARKS: &[&str] = &["alignment", "indentation"];
const TABLE_CELL_BLOCK_MARKS: &[&str] = &["backgroundColor", "border"];
const TABLE_HEADER_BLOCK_MARKS: &[&str] = &["backgroundColor", "border"];

const INLINE_MARKS_ENTRIES: &[(&str, &[&str])] = &[
    ("caption", CAPTION_INLINE_MARKS),
    ("codeBlock", CODE_BLOCK_INLINE_MARKS),
    ("decisionItem", STD_INLINE_MARKS),
    ("heading", HEADING_INLINE_MARKS),
    ("paragraph", STD_INLINE_MARKS),
    ("taskItem", STD_INLINE_MARKS),
];

const BLOCK_MARKS_ENTRIES: &[(&str, &[&str])] = &[
    ("heading", HEADING_BLOCK_MARKS),
    ("paragraph", PARAGRAPH_BLOCK_MARKS),
    ("tableCell", TABLE_CELL_BLOCK_MARKS),
    ("tableHeader", TABLE_HEADER_BLOCK_MARKS),
];

static INLINE_MARKS: LazyLock<HashMap<&'static str, &'static [&'static str]>> =
    LazyLock::new(|| INLINE_MARKS_ENTRIES.iter().copied().collect());

static BLOCK_MARKS: LazyLock<HashMap<&'static str, &'static [&'static str]>> =
    LazyLock::new(|| BLOCK_MARKS_ENTRIES.iter().copied().collect());

/// Inline node types whose marks are validated against the *parent*'s
/// inline-mark allow-list (rather than the node's own block-mark
/// allow-list). Sorted alphabetically.
const INLINE_NODE_TYPES: &[&str] = &[
    "date",
    "emoji",
    "hardBreak",
    "inlineCard",
    "inlineExtension",
    "mediaInline",
    "mention",
    "placeholder",
    "status",
    "text",
];

/// Returns the allowed inline marks under an inline-content container, or
/// `None` if the container is not registered (permissive on unknown
/// parents).
#[must_use]
pub fn allowed_inline_marks(parent: &str) -> Option<&'static [&'static str]> {
    INLINE_MARKS.get(parent).copied()
}

/// Returns the allowed block-level marks for a block node type, or `None`
/// if the node has no registered block-mark allow-list.
#[must_use]
pub fn allowed_block_marks(node_type: &str) -> Option<&'static [&'static str]> {
    BLOCK_MARKS.get(node_type).copied()
}

/// True when `node_type` is an inline node (whose marks should be checked
/// against the parent's inline-mark allow-list, not its own block-mark
/// allow-list).
#[must_use]
pub fn is_inline_node(node_type: &str) -> bool {
    INLINE_NODE_TYPES.contains(&node_type)
}

/// True for the round-trip preservation wrapper. Accepted under any
/// context.
fn is_unsupported_mark(mark_type: &str) -> bool {
    mark_type == "unsupportedMark" || mark_type == "unsupportedNodeAttribute"
}

// -----------------------------------------------------------------------------
// Per-mark attribute schemas
// -----------------------------------------------------------------------------

const ENUM_SUBSUP_TYPE: &[&str] = &["sub", "sup"];
const ENUM_ALIGNMENT_ALIGN: &[&str] = &["start", "end", "center", "right", "left"];
const ENUM_BREAKOUT_MODE: &[&str] = &["wide", "full-width"];

type MarkAttrEntry = (&'static str, AttrSchema);

const MARK_ATTR_ENTRIES: &[MarkAttrEntry] = &[
    // alignment — marks/alignment.ts
    (
        "alignment",
        AttrSchema {
            fields: &[(
                "align",
                AttrType::Enum(ENUM_ALIGNMENT_ALIGN),
                AttrPresence::Required,
            )],
        },
    ),
    // annotation — marks/annotation.ts
    (
        "annotation",
        AttrSchema {
            fields: &[
                ("id", AttrType::String, AttrPresence::Required),
                ("annotationType", AttrType::String, AttrPresence::Required),
            ],
        },
    ),
    // backgroundColor — marks/backgroundColor.ts
    // upstream: { color: hex string } (must look like #RRGGBB or #RRGGBBAA)
    (
        "backgroundColor",
        AttrSchema {
            fields: &[("color", AttrType::String, AttrPresence::Required)],
        },
    ),
    // border — marks/border.ts
    (
        "border",
        AttrSchema {
            fields: &[
                ("color", AttrType::String, AttrPresence::Required),
                ("size", AttrType::IntRange(1, 3), AttrPresence::Required),
            ],
        },
    ),
    // breakout — marks/breakout.ts
    (
        "breakout",
        AttrSchema {
            fields: &[(
                "mode",
                AttrType::Enum(ENUM_BREAKOUT_MODE),
                AttrPresence::Required,
            )],
        },
    ),
    // code — marks/code.ts (no attrs upstream)
    ("code", AttrSchema { fields: &[] }),
    // em — marks/em.ts (no attrs)
    ("em", AttrSchema { fields: &[] }),
    // indentation — marks/indentation.ts
    (
        "indentation",
        AttrSchema {
            fields: &[("level", AttrType::IntRange(1, 6), AttrPresence::Required)],
        },
    ),
    // link — marks/link.ts
    // upstream: { href: string (URI), title?: string, id?: string,
    //             collection?: string, occurrenceKey?: string }
    (
        "link",
        AttrSchema {
            fields: &[
                ("href", AttrType::Url, AttrPresence::Required),
                ("title", AttrType::String, AttrPresence::Optional),
                ("id", AttrType::String, AttrPresence::Optional),
                ("collection", AttrType::String, AttrPresence::Optional),
                ("occurrenceKey", AttrType::String, AttrPresence::Optional),
            ],
        },
    ),
    // strike — marks/strike.ts (no attrs)
    ("strike", AttrSchema { fields: &[] }),
    // strong — marks/strong.ts (no attrs)
    ("strong", AttrSchema { fields: &[] }),
    // subsup — marks/subsup.ts
    (
        "subsup",
        AttrSchema {
            fields: &[(
                "type",
                AttrType::Enum(ENUM_SUBSUP_TYPE),
                AttrPresence::Required,
            )],
        },
    ),
    // textColor — marks/textColor.ts
    // upstream: { color: hex string }
    (
        "textColor",
        AttrSchema {
            fields: &[("color", AttrType::String, AttrPresence::Required)],
        },
    ),
    // underline — marks/underline.ts (no attrs)
    ("underline", AttrSchema { fields: &[] }),
];

static MARK_ATTR_SCHEMAS: LazyLock<HashMap<&'static str, &'static AttrSchema>> =
    LazyLock::new(|| {
        MARK_ATTR_ENTRIES
            .iter()
            .map(|(mark_type, schema)| (*mark_type, schema))
            .collect()
    });

/// Returns the attribute schema for a mark type, or `None` if not
/// registered.
///
/// Permissive on unknown marks — they will still be flagged by the
/// allow-list check if they appear in a context that doesn't permit
/// them.
#[must_use]
pub fn mark_attr_schema(mark_type: &str) -> Option<&'static AttrSchema> {
    MARK_ATTR_SCHEMAS.get(mark_type).copied()
}

// -----------------------------------------------------------------------------
// Validation entry point
// -----------------------------------------------------------------------------

/// Validates the marks on a single node, appending any violations to `out`.
///
/// `parent_type` is the parent of `node`. `path` is the index path from
/// the document root to `node` (the same path used by the
/// `DisallowedChild` / `Arity` checks).
///
/// Mark validation is structured as:
///
/// 1. Determine the active allow-list:
///     - inline node → inline-marks for `parent_type`
///     - block node  → block-marks for `node.node_type`
///       Unknown contexts produce no allow-list and skip the check.
///
/// 2. For each mark on the node:
///     - If `mark_type` is `unsupported{Mark,NodeAttribute}`, accept it
///       (round-trip preservation wrapper).
///     - If the allow-list doesn't include the mark, emit
///       `DisallowedMark`.
///    - Validate the mark's `attrs` against `mark_attr_schema(mark_type)`,
///      emitting `InvalidMarkAttr` per problem.
pub fn validate_marks(
    parent_type: &str,
    node: &crate::atlassian::adf::AdfNode,
    path: &[usize],
    out: &mut Vec<AdfSchemaViolation>,
) {
    let Some(marks) = node.marks.as_ref() else {
        return;
    };
    if marks.is_empty() {
        return;
    }

    let node_type = node.node_type.as_str();
    let allowed = if is_inline_node(node_type) {
        allowed_inline_marks(parent_type)
    } else {
        allowed_block_marks(node_type)
    };

    for (mark_idx, mark) in marks.iter().enumerate() {
        let mark_type = mark.mark_type.as_str();

        if is_unsupported_mark(mark_type) {
            continue;
        }

        if let Some(allowed) = allowed {
            if !allowed.contains(&mark_type) {
                out.push(AdfSchemaViolation::DisallowedMark {
                    mark_type: mark_type.to_string(),
                    parent_type: if is_inline_node(node_type) {
                        parent_type.to_string()
                    } else {
                        node_type.to_string()
                    },
                    inline_index: if is_inline_node(node_type) {
                        Some(*path.last().unwrap_or(&0))
                    } else {
                        None
                    },
                    path: path.to_vec(),
                });
                // Don't validate attrs for a mark that isn't even allowed
                // here — the schema lookup might still succeed but the
                // mark is structurally rejected.
                continue;
            }
        }

        if let Some(schema) = mark_attr_schema(mark_type) {
            validate_mark_attrs_against(
                schema,
                mark_type,
                mark.attrs.as_ref(),
                mark_idx,
                path,
                out,
            );
        }
    }
}

fn validate_mark_attrs_against(
    schema: &AttrSchema,
    mark_type: &str,
    attrs: Option<&Value>,
    mark_idx: usize,
    path: &[usize],
    out: &mut Vec<AdfSchemaViolation>,
) {
    // Reuse the per-node validate_attrs by calling its underlying logic
    // through a small adapter that translates Missing/Invalid attr
    // violations into the mark-specific variants.
    let mut tmp: Vec<AdfSchemaViolation> = Vec::new();
    crate::atlassian::adf_attr_schema::validate_attrs(
        // The shared validate_attrs uses node_type as a *lookup key*. To
        // reuse its body without lookup, pass a sentinel that has no
        // schema and validate inline below. Easier: replicate the small
        // loop here so we control variant emission.
        "<__adf_mark_inline__>",
        attrs,
        path,
        &mut tmp,
    );
    debug_assert!(
        tmp.is_empty(),
        "sentinel must not match a registered schema"
    );

    // Inline replication of the schema walk so we emit the mark variants.
    let attr_obj = match attrs {
        Some(Value::Object(map)) => Some(map),
        Some(Value::Null) | None => None,
        Some(_other) => {
            for (field, _ty, presence) in schema.fields {
                if *presence == AttrPresence::Required {
                    out.push(AdfSchemaViolation::DisallowedMark {
                        mark_type: mark_type.to_string(),
                        parent_type: format!("<malformed attrs for mark '{mark_type}'>"),
                        inline_index: Some(mark_idx),
                        path: path.to_vec(),
                    });
                    let _ = field;
                    return;
                }
            }
            return;
        }
    };

    for (field, ty, presence) in schema.fields {
        let value = attr_obj.and_then(|m| m.get(*field));
        let value = match value {
            Some(Value::Null) | None => None,
            Some(v) => Some(v),
        };

        match (value, *presence) {
            (None, AttrPresence::Required) => {
                out.push(AdfSchemaViolation::InvalidMarkAttr {
                    mark_type: mark_type.to_string(),
                    attr_name: (*field).to_string(),
                    problem: crate::atlassian::adf_attr_schema::AttrProblem::WrongType {
                        expected: "present",
                    },
                    inline_index: Some(mark_idx),
                    path: path.to_vec(),
                });
            }
            (None, AttrPresence::Optional) => {}
            (Some(v), _) => {
                if let Some(problem) = crate::atlassian::adf_attr_schema::check_value(ty, v) {
                    out.push(AdfSchemaViolation::InvalidMarkAttr {
                        mark_type: mark_type.to_string(),
                        attr_name: (*field).to_string(),
                        problem,
                        inline_index: Some(mark_idx),
                        path: path.to_vec(),
                    });
                }
            }
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::needless_collect)]
mod tests {
    use super::*;
    use crate::atlassian::adf::{AdfMark, AdfNode};

    fn text_with_marks(text: &str, marks: Vec<AdfMark>) -> AdfNode {
        AdfNode {
            node_type: "text".to_string(),
            attrs: None,
            content: None,
            text: Some(text.to_string()),
            marks: Some(marks),
            local_id: None,
            parameters: None,
        }
    }

    fn paragraph_with_marks(marks: Vec<AdfMark>, content: Vec<AdfNode>) -> AdfNode {
        AdfNode {
            node_type: "paragraph".to_string(),
            attrs: None,
            content: if content.is_empty() {
                None
            } else {
                Some(content)
            },
            text: None,
            marks: Some(marks),
            local_id: None,
            parameters: None,
        }
    }

    fn mark(mark_type: &str, attrs: Option<serde_json::Value>) -> AdfMark {
        AdfMark {
            mark_type: mark_type.to_string(),
            attrs,
        }
    }

    fn run_inline(parent: &str, child: AdfNode) -> Vec<AdfSchemaViolation> {
        let mut out = Vec::new();
        validate_marks(parent, &child, &[0_usize], &mut out);
        out
    }

    fn run_block(node: AdfNode) -> Vec<AdfSchemaViolation> {
        let mut out = Vec::new();
        validate_marks("doc", &node, &[0_usize], &mut out);
        out
    }

    // ---- Inline marks: allow-list ---------------------------------------

    #[test]
    fn paragraph_allows_code_mark_on_text() {
        let node = text_with_marks("hi", vec![mark("code", None)]);
        assert!(run_inline("paragraph", node).is_empty());
    }

    #[test]
    fn heading_rejects_code_mark_on_text() {
        let node = text_with_marks("hi", vec![mark("code", None)]);
        let v = run_inline("heading", node);
        assert_eq!(v.len(), 1, "got: {v:?}");
        match &v[0] {
            AdfSchemaViolation::DisallowedMark {
                mark_type,
                parent_type,
                ..
            } => {
                assert_eq!(mark_type, "code");
                assert_eq!(parent_type, "heading");
            }
            other => panic!("expected DisallowedMark, got {other:?}"),
        }
    }

    #[test]
    fn code_block_rejects_any_mark_on_text() {
        let node = text_with_marks("hi", vec![mark("strong", None)]);
        let v = run_inline("codeBlock", node);
        assert_eq!(v.len(), 1);
    }

    #[test]
    fn unknown_parent_skips_mark_validation() {
        let node = text_with_marks("hi", vec![mark("madeUp", None)]);
        assert!(run_inline("madeUpParent", node).is_empty());
    }

    #[test]
    fn unsupported_mark_accepted_anywhere() {
        let node = text_with_marks(
            "hi",
            vec![
                mark("unsupportedMark", None),
                mark("unsupportedNodeAttribute", None),
            ],
        );
        assert!(run_inline("heading", node).is_empty());
    }

    // ---- Block marks ----------------------------------------------------

    #[test]
    fn paragraph_block_allows_alignment() {
        let node = paragraph_with_marks(
            vec![mark(
                "alignment",
                Some(serde_json::json!({"align": "center"})),
            )],
            vec![AdfNode::text("x")],
        );
        assert!(run_block(node).is_empty());
    }

    #[test]
    fn paragraph_block_rejects_border() {
        // border is a tableCell-only block mark.
        let node = paragraph_with_marks(
            vec![mark(
                "border",
                Some(serde_json::json!({"color": "#ff0000", "size": 1})),
            )],
            vec![AdfNode::text("x")],
        );
        let v = run_block(node);
        let disallowed: Vec<_> = v
            .iter()
            .filter(|v| matches!(v, AdfSchemaViolation::DisallowedMark { .. }))
            .collect();
        assert_eq!(disallowed.len(), 1, "got: {v:?}");
    }

    #[test]
    fn table_cell_allows_border() {
        let cell = AdfNode {
            node_type: "tableCell".to_string(),
            attrs: None,
            content: None,
            text: None,
            marks: Some(vec![mark(
                "border",
                Some(serde_json::json!({"color": "#ff0000", "size": 2})),
            )]),
            local_id: None,
            parameters: None,
        };
        assert!(run_block(cell).is_empty());
    }

    // ---- Mark attr validation -------------------------------------------

    #[test]
    fn link_mark_with_valid_href_validates() {
        let node = text_with_marks(
            "hi",
            vec![mark(
                "link",
                Some(serde_json::json!({"href": "https://x.com"})),
            )],
        );
        assert!(run_inline("paragraph", node).is_empty());
    }

    #[test]
    fn link_mark_with_invalid_href_flagged() {
        let node = text_with_marks(
            "hi",
            vec![mark("link", Some(serde_json::json!({"href": "not a url"})))],
        );
        let v = run_inline("paragraph", node);
        let invalid: Vec<_> = v
            .iter()
            .filter(|v| matches!(v, AdfSchemaViolation::InvalidMarkAttr { .. }))
            .collect();
        assert_eq!(invalid.len(), 1, "got: {v:?}");
    }

    #[test]
    fn link_mark_missing_href_flagged() {
        let node = text_with_marks("hi", vec![mark("link", Some(serde_json::json!({})))]);
        let v = run_inline("paragraph", node);
        let invalid: Vec<_> = v
            .iter()
            .filter(|v| matches!(v, AdfSchemaViolation::InvalidMarkAttr { .. }))
            .collect();
        assert_eq!(invalid.len(), 1);
    }

    #[test]
    fn subsup_known_type_validates() {
        for t in ["sub", "sup"] {
            let node = text_with_marks(
                "hi",
                vec![mark("subsup", Some(serde_json::json!({"type": t})))],
            );
            assert!(run_inline("paragraph", node).is_empty());
        }
    }

    #[test]
    fn subsup_unknown_type_flagged() {
        let node = text_with_marks(
            "hi",
            vec![mark("subsup", Some(serde_json::json!({"type": "side"})))],
        );
        let v = run_inline("paragraph", node);
        let invalid: Vec<_> = v
            .iter()
            .filter(|v| matches!(v, AdfSchemaViolation::InvalidMarkAttr { .. }))
            .collect();
        assert_eq!(invalid.len(), 1);
    }

    #[test]
    fn indentation_level_in_range() {
        let node = paragraph_with_marks(
            vec![mark("indentation", Some(serde_json::json!({"level": 3})))],
            vec![AdfNode::text("x")],
        );
        assert!(run_block(node).is_empty());
    }

    #[test]
    fn indentation_level_out_of_range_flagged() {
        let node = paragraph_with_marks(
            vec![mark("indentation", Some(serde_json::json!({"level": 10})))],
            vec![AdfNode::text("x")],
        );
        let v = run_block(node);
        let invalid: Vec<_> = v
            .iter()
            .filter(|v| matches!(v, AdfSchemaViolation::InvalidMarkAttr { .. }))
            .collect();
        assert_eq!(invalid.len(), 1);
    }

    #[test]
    fn border_with_size_too_large_flagged() {
        let cell = AdfNode {
            node_type: "tableCell".to_string(),
            attrs: None,
            content: None,
            text: None,
            marks: Some(vec![mark(
                "border",
                Some(serde_json::json!({"color": "#ff0000", "size": 5})),
            )]),
            local_id: None,
            parameters: None,
        };
        let v = run_block(cell);
        let invalid: Vec<_> = v
            .iter()
            .filter(|v| matches!(v, AdfSchemaViolation::InvalidMarkAttr { .. }))
            .collect();
        assert_eq!(invalid.len(), 1);
    }

    #[test]
    fn empty_marks_array_no_violations() {
        let node = text_with_marks("hi", vec![]);
        assert!(run_inline("paragraph", node).is_empty());
    }

    #[test]
    fn no_marks_field_no_violations() {
        let node = AdfNode::text("hi");
        assert!(run_inline("paragraph", node).is_empty());
    }

    // ── malformed attrs path on a mark ─────────────────────────────────

    #[test]
    fn link_mark_with_array_attrs_flagged_as_disallowed_mark() {
        // attrs is present but not an object (array). The link schema has
        // a required `href`, so the malformed-attrs branch fires and emits
        // a DisallowedMark with a sentinel parent_type marker.
        let node = text_with_marks(
            "click",
            vec![mark("link", Some(serde_json::json!([1, 2, 3])))],
        );
        let v = run_inline("paragraph", node);
        let disallowed: Vec<_> = v
            .iter()
            .filter(|v| matches!(v, AdfSchemaViolation::DisallowedMark { .. }))
            .collect();
        assert_eq!(disallowed.len(), 1, "got: {v:?}");
        match disallowed[0] {
            AdfSchemaViolation::DisallowedMark {
                mark_type,
                parent_type,
                ..
            } => {
                assert_eq!(mark_type, "link");
                assert!(
                    parent_type.contains("malformed attrs"),
                    "expected malformed-attrs sentinel, got: {parent_type}"
                );
            }
            other => panic!("expected DisallowedMark, got {other:?}"),
        }
    }

    #[test]
    fn code_mark_with_array_attrs_no_violation() {
        // `code` mark schema has no required fields, so the malformed-attrs
        // branch should fall through without emitting anything (covers the
        // bare `return;` arm at the bottom of the malformed-attrs match).
        let node = text_with_marks("x", vec![mark("code", Some(serde_json::json!([1, 2, 3])))]);
        assert!(run_inline("paragraph", node).is_empty());
    }

    // ── inline node under a parent without an inline-mark allow-list ──

    #[test]
    fn inline_node_under_unknown_parent_skips_mark_check() {
        // Parent has no inline-mark allow-list, so the mark is neither
        // accepted-by-allow-list nor flagged. Validates that the
        // `if let Some(allowed) = allowed { ... }` guard short-circuits
        // cleanly when `allowed` is `None`, but the per-mark attr schema
        // still runs (link.href validation here).
        let node = text_with_marks(
            "x",
            vec![mark("link", Some(serde_json::json!({"href": "not a url"})))],
        );
        let v = run_inline("madeUpParent", node);
        // No DisallowedMark (no allow-list to violate).
        assert!(v
            .iter()
            .all(|v| !matches!(v, AdfSchemaViolation::DisallowedMark { .. })));
        // But the mark-attr validation still fires.
        assert!(v
            .iter()
            .any(|v| matches!(v, AdfSchemaViolation::InvalidMarkAttr { .. })));
    }
}