hyalo-core 0.13.2

Core library for hyalo — frontmatter parsing, querying, and mutation for Markdown 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
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
#![allow(clippy::missing_errors_doc)]

mod parse;
mod types;

use anyhow::Context as _;

pub use parse::{body_only, hyalo_options, read_frontmatter, skip_frontmatter, write_frontmatter};
pub use types::{infer_type, parse_value};

/// Read a file's modification time and size for TOCTOU detection.
///
/// Returns `(mtime, file_size_bytes)`. Using both components strengthens the
/// fingerprint: on filesystems with coarse mtime granularity (e.g. FAT32 with
/// 2 s resolution), a file that is written and reverted within the same clock
/// tick would have the same mtime but a different size, and vice versa.
pub fn read_mtime(path: &std::path::Path) -> anyhow::Result<(std::time::SystemTime, u64)> {
    let meta =
        std::fs::metadata(path).with_context(|| format!("failed to stat {}", path.display()))?;
    let mtime = meta
        .modified()
        .with_context(|| format!("mtime not available for {}", path.display()))?;
    Ok((mtime, meta.len()))
}

/// Verify that a file's fingerprint (mtime + size) hasn't changed since `expected`.
/// Returns an error if the file was modified concurrently.
pub fn check_mtime(
    path: &std::path::Path,
    expected: (std::time::SystemTime, u64),
) -> anyhow::Result<()> {
    let current = read_mtime(path)?;
    if current != expected {
        anyhow::bail!(
            "file {} was modified by another process during operation",
            path.display()
        );
    }
    Ok(())
}

/// A typed error for frontmatter parse and structural failures.
///
/// Covers bad YAML, unclosed `---` delimiters, oversized frontmatter blocks, and
/// any other condition where the file content itself is the problem. These errors
/// can be safely skipped (with a warning) when processing multiple files.
///
/// I/O errors are **not** represented here — they propagate as plain `anyhow::Error`
/// wrapping `std::io::Error`, so callers can still distinguish them via
/// [`is_parse_error`] or by downcasting directly.
#[derive(Debug)]
pub struct FrontmatterError(pub(crate) String);

impl std::fmt::Display for FrontmatterError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for FrontmatterError {}

/// Returns `true` if the error originates from a frontmatter parse or structural problem
/// (bad YAML, unclosed delimiter, oversized frontmatter) rather than an I/O failure.
///
/// Parse errors can be safely skipped when processing multiple files; I/O errors
/// should be propagated to the caller.
pub fn is_parse_error(err: &anyhow::Error) -> bool {
    err.chain()
        .any(|cause| cause.downcast_ref::<FrontmatterError>().is_some())
}

#[cfg(test)]
mod tests {
    use super::*;
    use parse::{Document, detect_list_indent_style, read_frontmatter_from_reader};
    use serde_json::Value;
    use std::fmt::Write as _;
    use std::path::Path;

    macro_rules! md {
        ($s:expr) => {
            $s.strip_prefix('\n').unwrap_or($s)
        };
    }

    #[test]
    fn parse_valid_frontmatter() {
        let content = md!(r"
---
title: Hello
status: draft
---
Body text here.
");
        let doc = Document::parse(content).unwrap();
        assert_eq!(doc.properties().len(), 2);
        assert_eq!(
            doc.get_property("title"),
            Some(&Value::String("Hello".into()))
        );
        assert_eq!(doc.body(), "Body text here.\n");
    }

    #[test]
    fn parse_no_frontmatter() {
        let content = "Just a regular markdown file.\n";
        let doc = Document::parse(content).unwrap();
        assert!(doc.properties().is_empty());
        assert_eq!(doc.body(), content);
    }

    #[test]
    fn parse_empty_frontmatter() {
        let content = md!(r"
---
---
Body.
");
        let doc = Document::parse(content).unwrap();
        assert!(doc.properties().is_empty());
        assert_eq!(doc.body(), "Body.\n");
    }

    #[test]
    fn parse_malformed_frontmatter() {
        // Missing closing delimiter — now returns an error to prevent corruption on write
        let content = md!(r"
---
title: Broken
No closing delimiter.
");
        let err = Document::parse(content).unwrap_err();
        assert!(err.to_string().contains("unclosed frontmatter"));
    }

    #[test]
    fn infer_type_text() {
        assert_eq!(infer_type(&Value::String("hello".into())), "text");
    }

    #[test]
    fn infer_type_number() {
        assert_eq!(infer_type(&Value::Number(42.into())), "number");
    }

    #[test]
    fn infer_type_bool() {
        assert_eq!(infer_type(&Value::Bool(true)), "checkbox");
    }

    #[test]
    fn infer_type_date() {
        assert_eq!(infer_type(&Value::String("2026-03-20".into())), "date");
    }

    #[test]
    fn infer_type_datetime() {
        assert_eq!(
            infer_type(&Value::String("2026-03-20T14:30:00".into())),
            "datetime"
        );
    }

    #[test]
    fn infer_type_list() {
        assert_eq!(
            infer_type(&Value::Array(vec![Value::String("a".into())])),
            "list"
        );
    }

    #[test]
    fn infer_type_null() {
        assert_eq!(infer_type(&Value::Null), "null");
    }

    #[test]
    fn roundtrip_preserves_body() {
        let content = md!(r"
---
title: Test
priority: 5
---
# Heading

Paragraph content.
");
        let doc = Document::parse(content).unwrap();
        let serialized = doc.serialize().unwrap();
        let doc2 = Document::parse(&serialized).unwrap();
        assert_eq!(doc.properties(), doc2.properties());
        assert_eq!(doc.body(), doc2.body());
    }

    #[test]
    fn serialize_no_properties_no_frontmatter() {
        let doc = Document::parse("Just body.\n").unwrap();
        let serialized = doc.serialize().unwrap();
        assert_eq!(serialized, "Just body.\n");
    }

    #[test]
    fn set_and_remove_property() {
        let mut doc = Document::parse(md!(r"
---
title: Hi
---
Body
"))
        .unwrap();
        doc.set_property("status".into(), Value::String("done".into()));
        assert!(doc.get_property("status").is_some());
        doc.remove_property("status");
        assert!(doc.get_property("status").is_none());
    }

    #[test]
    fn parse_value_infer() {
        // Number
        match parse_value("42", None).unwrap() {
            Value::Number(n) => assert_eq!(n.as_i64(), Some(42)),
            other => panic!("expected number, got {other:?}"),
        }
        // Bool
        assert_eq!(parse_value("true", None).unwrap(), Value::Bool(true));
        // Text
        match parse_value("hello", None).unwrap() {
            Value::String(s) => assert_eq!(s, "hello"),
            other => panic!("expected string, got {other:?}"),
        }
    }

    #[test]
    fn parse_value_forced_type() {
        // Force text even for number-like string
        match parse_value("42", Some("text")).unwrap() {
            Value::String(s) => assert_eq!(s, "42"),
            other => panic!("expected string, got {other:?}"),
        }
        // Force list
        match parse_value("a, b, c", Some("list")).unwrap() {
            Value::Array(items) => assert_eq!(items.len(), 3),
            other => panic!("expected array, got {other:?}"),
        }
    }

    #[test]
    fn file_with_only_frontmatter() {
        let content = md!(r"
---
title: Only FM
---
");
        let doc = Document::parse(content).unwrap();
        assert_eq!(doc.properties().len(), 1);
        assert_eq!(doc.body(), "");
    }

    // --- Streaming reader tests ---

    #[test]
    fn streaming_valid_frontmatter() {
        let input = md!("
---
title: Hello
status: draft
---
# Body that should not be read
Lots of content here.
");
        let props = read_frontmatter_from_reader(input.as_bytes()).unwrap();
        assert_eq!(props.len(), 2);
        assert_eq!(props.get("title"), Some(&Value::String("Hello".into())));
        assert_eq!(props.get("status"), Some(&Value::String("draft".into())));
    }

    #[test]
    fn streaming_no_frontmatter() {
        let input = md!("
Just a regular file.
No frontmatter.
");
        let props = read_frontmatter_from_reader(input.as_bytes()).unwrap();
        assert!(props.is_empty());
    }

    #[test]
    fn streaming_empty_frontmatter() {
        let input = md!("
---
---
Body.
");
        let props = read_frontmatter_from_reader(input.as_bytes()).unwrap();
        assert!(props.is_empty());
    }

    #[test]
    fn streaming_no_closing_delimiter() {
        // No closing `---` must always error — even if the YAML content is valid.
        let input = md!("
---
title: Broken
Not valid yaml line
");
        let result = read_frontmatter_from_reader(input.as_bytes());
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("unclosed frontmatter"),
            "expected unclosed frontmatter error"
        );

        // Also errors even when the content happens to be valid YAML
        let input2 = md!("
---
title: Works
status: ok
");
        let result2 = read_frontmatter_from_reader(input2.as_bytes());
        assert!(result2.is_err());
        assert!(
            result2
                .unwrap_err()
                .to_string()
                .contains("unclosed frontmatter"),
            "expected unclosed frontmatter error for valid-YAML-but-unclosed file"
        );
    }

    #[test]
    fn streaming_solo_dash_is_no_frontmatter() {
        // A file whose entire content is exactly `---` (no trailing newline) must be
        // treated as "no frontmatter" — consistent with `extract_frontmatter`.
        let result = read_frontmatter_from_reader("---".as_bytes());
        assert!(
            result.is_ok(),
            "expected Ok for bare `---`, got: {result:?}"
        );
        assert!(
            result.unwrap().is_empty(),
            "expected empty map for bare `---`"
        );

        // Same for `---\n` — indistinguishable from `---` in a line-based reader.
        let result = read_frontmatter_from_reader("---\n".as_bytes());
        assert!(result.is_ok(), "expected Ok for `---\\n`, got: {result:?}");
        assert!(
            result.unwrap().is_empty(),
            "expected empty map for `---\\n`"
        );

        // A file with `---` followed by actual content but no closing delimiter
        // must still error (not silently become "no frontmatter").
        let result = read_frontmatter_from_reader("---\ntitle: X\n".as_bytes());
        assert!(
            result.is_err(),
            "expected Err for unclosed frontmatter with content"
        );
    }

    #[test]
    fn streaming_matches_full_parse() {
        let content = md!(r"
---
title: Test
priority: 5
tags:
  - a
  - b
---
# Heading

Body.
");
        let doc = Document::parse(content).unwrap();
        let streamed = read_frontmatter_from_reader(content.as_bytes()).unwrap();
        assert_eq!(doc.properties(), &streamed);
    }

    // --- Budget boundary tests for skip_frontmatter ---

    fn make_frontmatter_with_n_lines(n: usize) -> String {
        // Each content line is "k: v\n" (6 bytes). The closing --- is appended.
        let mut s = String::from("---\n");
        for i in 0..n {
            let _ = writeln!(s, "k{i}: v");
        }
        s.push_str("---\n");
        s
    }

    #[test]
    fn streaming_budget_boundary_lines_at_limit() {
        // Exactly 200 content lines — must succeed
        let input = make_frontmatter_with_n_lines(200);
        let mut reader = input.as_bytes();
        // Read and discard the opening "---\n" line, then call skip_frontmatter
        let mut first_line = String::new();
        std::io::BufRead::read_line(&mut reader, &mut first_line).unwrap();
        let first = first_line.trim_end_matches(['\n', '\r']);
        let result = skip_frontmatter(&mut reader, first);
        assert!(
            result.is_ok(),
            "200 content lines should succeed: {result:?}"
        );
    }

    #[test]
    fn streaming_budget_boundary_lines_over_limit() {
        // 201 content lines — must error
        let input = make_frontmatter_with_n_lines(201);
        let mut reader = input.as_bytes();
        let mut first_line = String::new();
        std::io::BufRead::read_line(&mut reader, &mut first_line).unwrap();
        let first = first_line.trim_end_matches(['\n', '\r']);
        let result = skip_frontmatter(&mut reader, first);
        assert!(result.is_err(), "201 content lines should fail");
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("frontmatter too large")
        );
    }

    #[test]
    fn streaming_budget_boundary_bytes_at_limit() {
        // Build frontmatter whose content is just under or equal to 8192 bytes.
        // skip_frontmatter counts raw bytes from read_line (including \n).
        // Use a single long line of exactly 8192 bytes (including the \n).
        // "x: " (3 bytes) + value + "\n" = 8192 → value = 8188 bytes of 'a'
        let value = "a".repeat(8188);
        let input = format!("---\nx: {value}\n---\n");
        let mut reader = input.as_bytes();
        let mut first_line = String::new();
        std::io::BufRead::read_line(&mut reader, &mut first_line).unwrap();
        let first = first_line.trim_end_matches(['\n', '\r']);
        let result = skip_frontmatter(&mut reader, first);
        assert!(
            result.is_ok(),
            "8192-byte content should succeed: {result:?}"
        );
    }

    #[test]
    fn streaming_budget_boundary_bytes_over_limit() {
        // Content line of 8193 bytes (including \n) — must error
        let value = "a".repeat(8189); // "x: " (3) + 8189 + "\n" = 8193
        let input = format!("---\nx: {value}\n---\n");
        let mut reader = input.as_bytes();
        let mut first_line = String::new();
        std::io::BufRead::read_line(&mut reader, &mut first_line).unwrap();
        let first = first_line.trim_end_matches(['\n', '\r']);
        let result = skip_frontmatter(&mut reader, first);
        assert!(result.is_err(), "8193-byte content should fail");
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("frontmatter too large")
        );
    }

    #[test]
    fn is_parse_error_true_for_yaml_error() {
        let err =
            read_frontmatter_from_reader("---\n: invalid [[[{\n---\n".as_bytes()).unwrap_err();
        assert!(is_parse_error(&err), "expected parse error: {err}");
    }

    #[test]
    fn is_parse_error_true_for_unclosed_frontmatter() {
        let err = read_frontmatter_from_reader("---\ntitle: Broken\n".as_bytes()).unwrap_err();
        assert!(
            is_parse_error(&err),
            "unclosed frontmatter should be a parse error: {err}"
        );
    }

    #[test]
    fn is_parse_error_false_for_io_error() {
        let err = read_frontmatter(Path::new("/nonexistent/path/file.md")).unwrap_err();
        assert!(!is_parse_error(&err), "expected I/O error: {err}");
    }

    #[test]
    fn frontmatter_error_is_directly_downcastable() {
        let err =
            read_frontmatter_from_reader("---\n: invalid [[[{\n---\n".as_bytes()).unwrap_err();
        let found = err
            .chain()
            .any(|cause| cause.downcast_ref::<FrontmatterError>().is_some());
        assert!(
            found,
            "FrontmatterError should be downcastable from anyhow::Error: {err}"
        );
    }

    #[test]
    fn infer_value_list_basic() {
        match parse_value("[a, b, c]", None).unwrap() {
            Value::Array(items) => {
                assert_eq!(items.len(), 3);
                assert_eq!(items[0], Value::String("a".to_owned()));
                assert_eq!(items[1], Value::String("b".to_owned()));
                assert_eq!(items[2], Value::String("c".to_owned()));
            }
            other => panic!("expected array, got {other:?}"),
        }
    }

    #[test]
    fn infer_value_list_empty() {
        match parse_value("[]", None).unwrap() {
            Value::Array(items) => assert!(items.is_empty()),
            other => panic!("expected empty sequence, got {other:?}"),
        }
    }

    #[test]
    fn infer_value_list_single_item() {
        match parse_value("[single]", None).unwrap() {
            Value::Array(items) => {
                assert_eq!(items.len(), 1);
                assert_eq!(items[0], Value::String("single".to_owned()));
            }
            other => panic!("expected array, got {other:?}"),
        }
    }

    #[test]
    fn infer_value_not_a_list() {
        // Value that contains brackets but doesn't start with [ should remain string
        match parse_value("not [a list]", None).unwrap() {
            Value::String(s) => assert_eq!(s, "not [a list]"),
            other => panic!("expected string, got {other:?}"),
        }
    }

    #[test]
    fn infer_value_list_whitespace_trimmed() {
        match parse_value("[  a , b ,  c  ]", None).unwrap() {
            Value::Array(items) => {
                assert_eq!(items[0], Value::String("a".to_owned()));
                assert_eq!(items[1], Value::String("b".to_owned()));
                assert_eq!(items[2], Value::String("c".to_owned()));
            }
            other => panic!("expected array, got {other:?}"),
        }
    }

    // --- Hardened parser option tests ---

    #[test]
    fn rejects_deeply_nested_yaml() {
        // Depth > 20 must be rejected by the budget
        let mut yaml = String::from("---\n");
        for i in 0..25 {
            yaml.push_str(&"  ".repeat(i));
            let _ = writeln!(yaml, "l{i}:");
        }
        yaml.push_str(&"  ".repeat(25));
        yaml.push_str("val: 1\n");
        yaml.push_str("---\nBody\n");

        let err = Document::parse(&yaml);
        assert!(err.is_err(), "deeply nested YAML should be rejected");
    }

    #[test]
    fn rejects_yaml_with_aliases() {
        // Aliases (max_aliases: 0) must be rejected
        let content = "---\nanchor: &a value\nalias: *a\n---\nBody\n";
        let err = Document::parse(content);
        assert!(err.is_err(), "YAML with aliases should be rejected");
    }

    #[test]
    fn rejects_duplicate_keys() {
        let content = "---\ntitle: First\ntitle: Second\n---\nBody\n";
        let err = Document::parse(content).unwrap_err();
        let chain = format!("{err:?}").to_lowercase();
        assert!(
            chain.contains("duplicate"),
            "error chain should mention duplicate, got: {err:?}"
        );
    }

    #[test]
    fn strict_booleans_yes_is_string() {
        // With strict_booleans, `yes` must parse as a string, not a boolean
        let content = "---\nflag: yes\n---\nBody\n";
        let doc = Document::parse(content).unwrap();
        assert_eq!(
            doc.get_property("flag"),
            Some(&Value::String("yes".into())),
            "`yes` should be parsed as string with strict booleans"
        );
    }

    #[test]
    fn strict_booleans_no_is_string() {
        let content = "---\nflag: no\n---\nBody\n";
        let doc = Document::parse(content).unwrap();
        assert_eq!(
            doc.get_property("flag"),
            Some(&Value::String("no".into())),
            "`no` should be parsed as string with strict booleans"
        );
    }

    #[test]
    fn strict_booleans_on_off_are_strings() {
        let content = "---\na: on\nb: off\n---\nBody\n";
        let doc = Document::parse(content).unwrap();
        assert_eq!(doc.get_property("a"), Some(&Value::String("on".into())));
        assert_eq!(doc.get_property("b"), Some(&Value::String("off".into())));
    }

    #[test]
    fn strict_booleans_true_false_still_booleans() {
        let content = "---\na: true\nb: false\n---\nBody\n";
        let doc = Document::parse(content).unwrap();
        assert_eq!(doc.get_property("a"), Some(&Value::Bool(true)));
        assert_eq!(doc.get_property("b"), Some(&Value::Bool(false)));
    }

    #[test]
    fn streaming_rejects_duplicate_keys() {
        let input = "---\ntitle: First\ntitle: Second\n---\nBody\n";
        let result = read_frontmatter_from_reader(input.as_bytes());
        assert!(
            result.is_err(),
            "streaming parser should reject duplicate keys"
        );
    }

    #[test]
    fn streaming_strict_booleans() {
        let input = "---\nflag: yes\n---\nBody\n";
        let props = read_frontmatter_from_reader(input.as_bytes()).unwrap();
        assert_eq!(
            props.get("flag"),
            Some(&Value::String("yes".into())),
            "streaming parser should treat `yes` as string"
        );
    }

    // --- Key order preservation tests ---

    #[test]
    fn roundtrip_preserves_key_order() {
        let content = md!(r"
---
title: Hello
type: iteration
date: 2026-03-27
status: planned
branch: iter-54/test
---
Body.
");
        let doc = Document::parse(content).unwrap();
        let serialized = doc.serialize().unwrap();
        let doc2 = Document::parse(&serialized).unwrap();
        let keys: Vec<&str> = doc2.properties().keys().map(String::as_str).collect();
        assert_eq!(
            keys,
            vec!["title", "type", "date", "status", "branch"],
            "key order should be preserved through roundtrip"
        );
    }

    #[test]
    fn roundtrip_preserves_key_order_after_mutation() {
        let content = md!(r"
---
title: Hello
type: iteration
date: 2026-03-27
status: planned
---
Body.
");
        let mut doc = Document::parse(content).unwrap();
        doc.set_property("status".into(), Value::String("completed".into()));
        let serialized = doc.serialize().unwrap();
        let doc2 = Document::parse(&serialized).unwrap();
        let keys: Vec<&str> = doc2.properties().keys().map(String::as_str).collect();
        assert_eq!(
            keys,
            vec!["title", "type", "date", "status"],
            "key order should be preserved after mutation"
        );
    }

    // --- List indent style detection tests ---

    #[test]
    fn detect_compact_list_style() {
        let yaml = "title: Test\ntags:\n- a\n- b\n";
        assert!(
            detect_list_indent_style(yaml),
            "flush `- item` should be detected as compact"
        );
    }

    #[test]
    fn detect_indented_list_style() {
        let yaml = "title: Test\ntags:\n  - a\n  - b\n";
        assert!(
            !detect_list_indent_style(yaml),
            "indented `  - item` should be detected as non-compact"
        );
    }

    #[test]
    fn detect_indented_list_with_comment_after_key() {
        let yaml = "tags: # my tags\n  - a\n  - b\n";
        assert!(
            !detect_list_indent_style(yaml),
            "comment after key colon should still detect indented style"
        );
    }

    #[test]
    fn detect_indented_list_with_blank_line_between() {
        let yaml = "tags:\n\n  - a\n  - b\n";
        assert!(
            !detect_list_indent_style(yaml),
            "blank line between key and sequence should not break detection"
        );
    }

    #[test]
    fn detect_indented_list_with_comment_line_between() {
        let yaml = "tags:\n  # note\n  - a\n  - b\n";
        assert!(
            !detect_list_indent_style(yaml),
            "comment line between key and sequence should not break detection"
        );
    }

    #[test]
    fn detect_no_sequences_defaults_to_non_compact() {
        let yaml = "title: Test\nstatus: draft\n";
        assert!(
            !detect_list_indent_style(yaml),
            "no sequences should default to non-compact"
        );
    }

    #[test]
    fn roundtrip_compact_list_style() {
        let content = md!(r"
---
title: Test
tags:
- a
- b
---
Body.
");
        let doc = Document::parse(content).unwrap();
        let serialized = doc.serialize().unwrap();
        assert!(
            serialized.contains("tags:\n- a\n- b"),
            "compact list style should be preserved: {serialized}"
        );
    }

    #[test]
    fn roundtrip_indented_list_style() {
        let content = md!(r"
---
title: Test
tags:
  - a
  - b
---
Body.
");
        let doc = Document::parse(content).unwrap();
        let serialized = doc.serialize().unwrap();
        assert!(
            serialized.contains("tags:\n  - a\n  - b"),
            "indented list style should be preserved: {serialized}"
        );
    }
}