rlsp-yaml-parser 0.6.1

Spec-faithful streaming YAML 1.2 parser
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
// SPDX-License-Identifier: MIT
//
// Tests for the YAML 1.2 §8.2.2 / §7.4.3 limit: implicit mapping keys must
// not exceed 1024 Unicode characters.  Groups A–G cover block context;
// groups H–N cover flow context (both §7.4.3 [154] and [155]).

#![expect(
    clippy::unwrap_used,
    clippy::expect_used,
    missing_docs,
    reason = "test code"
)]

use rlsp_yaml_parser::{load, parse_events};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn has_parse_error(input: &str) -> bool {
    parse_events(input).any(|r| r.is_err())
}

fn parses_clean(input: &str) -> bool {
    parse_events(input).all(|r| r.is_ok())
}

fn first_error_message(input: &str) -> Option<String> {
    parse_events(input)
        .find_map(std::result::Result::err)
        .map(|e| e.message)
}

// ===========================================================================
// Group A: Boundary acceptance — plain ASCII keys
// ===========================================================================

#[test]
fn a1_1024_ascii_plain_key_parses_successfully() {
    let key = "a".repeat(1024);
    let input = format!("{key}: value\n");
    assert!(
        parses_clean(&input),
        "1024-char ASCII key should parse without error"
    );
}

#[test]
fn a2_1025_ascii_plain_key_produces_error() {
    let key = "a".repeat(1025);
    let input = format!("{key}: value\n");
    assert!(
        has_parse_error(&input),
        "1025-char ASCII key should produce a parse error"
    );
    let msg = first_error_message(&input).unwrap();
    assert!(
        msg.contains("1024"),
        "error message should mention '1024', got: {msg}"
    );
    assert!(
        msg.contains("§8.2.2"),
        "error message should cite §8.2.2, got: {msg}"
    );
}

#[test]
fn a3_short_key_parses_successfully() {
    let input = "k: v\n";
    assert!(parses_clean(input), "short key should parse without error");
}

#[test]
fn a4_empty_key_parses_successfully() {
    // Bare `: value` — zero-char key.
    let input = ": value\n";
    assert!(
        parses_clean(input),
        "empty (zero-char) key should parse without error"
    );
}

// ===========================================================================
// Group B: Unicode / multibyte boundary
// ===========================================================================

#[test]
fn b1_1024_two_byte_chars_parse_successfully() {
    // 1024 × 'é' (U+00E9, 2 bytes each) = 2048 bytes but only 1024 chars.
    let key = "é".repeat(1024);
    let input = format!("{key}: value\n");
    assert!(
        parses_clean(&input),
        "1024 two-byte chars (2048 bytes) should parse successfully — limit is chars not bytes"
    );
}

#[test]
fn b2_1025_two_byte_chars_produce_error() {
    // 1025 × 'é' = 1025 chars → over limit.
    let key = "é".repeat(1025);
    let input = format!("{key}: value\n");
    assert!(
        has_parse_error(&input),
        "1025 two-byte chars should produce a parse error"
    );
    let msg = first_error_message(&input).unwrap();
    assert!(
        msg.contains("1024"),
        "error message should mention '1024', got: {msg}"
    );
}

#[test]
fn b3_1024_three_byte_chars_parse_successfully() {
    // 1024 × '中' (U+4E2D, 3 bytes each) = 3072 bytes but only 1024 chars.
    let key = "".repeat(1024);
    let input = format!("{key}: value\n");
    assert!(
        parses_clean(&input),
        "1024 three-byte chars (3072 bytes) should parse successfully"
    );
}

#[test]
fn b4_1025_four_byte_chars_produce_error() {
    // 1025 × '𝄞' (U+1D11E, 4 bytes each) = 4100 bytes but 1025 chars → over limit.
    let key = "\u{1D11E}".repeat(1025);
    let input = format!("{key}: value\n");
    assert!(
        has_parse_error(&input),
        "1025 four-byte chars should produce a parse error"
    );
}

// ===========================================================================
// Group C: Quoted implicit keys
//
// The check measures trimmed[..colon_offset].chars().count(), which for a
// double-quoted key like `"<content>": value` includes the opening `"`,
// the content, and the closing `"` — so a 1024-content-char quoted key has
// colon_offset at position 1026 (quote + 1024 chars + quote), making
// trimmed[..colon_offset].chars().count() == 1026.
//
// A key whose content is 1022 chars will produce a slice of 1024 chars
// (1022 content + 2 quotes) — right at the limit and accepted.
// A key whose content is 1023 chars produces a slice of 1025 chars — over.
//
// All assertions below are written in terms of the *slice* char count,
// not the content char count.
// ===========================================================================

#[test]
fn c1_double_quoted_key_at_limit_parses_successfully() {
    // Content: 1022 × 'a' → slice = `"` + 1022 + `"` = 1024 chars → at limit, accepted.
    let content = "a".repeat(1022);
    let input = format!("\"{content}\": value\n");
    assert!(
        parses_clean(&input),
        "double-quoted key with 1024-char slice should parse successfully"
    );
}

#[test]
fn c2_double_quoted_key_over_limit_produces_error() {
    // Content: 1023 × 'a' → slice = `"` + 1023 + `"` = 1025 chars → over limit.
    let content = "a".repeat(1023);
    let input = format!("\"{content}\": value\n");
    assert!(
        has_parse_error(&input),
        "double-quoted key with 1025-char slice should produce a parse error"
    );
    let msg = first_error_message(&input).unwrap();
    assert!(
        msg.contains("1024"),
        "error message should mention '1024', got: {msg}"
    );
}

#[test]
fn c3_single_quoted_key_at_limit_parses_successfully() {
    // Content: 1022 × 'a' → slice = `'` + 1022 + `'` = 1024 chars → at limit, accepted.
    let content = "a".repeat(1022);
    let input = format!("'{content}': value\n");
    assert!(
        parses_clean(&input),
        "single-quoted key with 1024-char slice should parse successfully"
    );
}

#[test]
fn c4_single_quoted_key_over_limit_produces_error() {
    // Content: 1023 × 'a' → slice = `'` + 1023 + `'` = 1025 chars → over limit.
    let content = "a".repeat(1023);
    let input = format!("'{content}': value\n");
    assert!(
        has_parse_error(&input),
        "single-quoted key with 1025-char slice should produce a parse error"
    );
}

// ===========================================================================
// Group D: Explicit key exemption
// ===========================================================================

#[test]
fn d1_explicit_key_indicator_with_long_key_parses_successfully() {
    // `? <1025-char key>` — explicit `?` form is not subject to the limit.
    let key = "a".repeat(1025);
    let input = format!("? {key}\n: value\n");
    assert!(
        parses_clean(&input),
        "explicit '?' key with >1024 chars should not be limited"
    );
}

// ===========================================================================
// Group E: Error position and message
// ===========================================================================

#[test]
fn e1_error_position_points_to_colon_indicator() {
    // 1025-char key at column 0; `:` is at column 1025, byte offset 1025.
    let key = "a".repeat(1025);
    let input = format!("{key}: value\n");
    let err = parse_events(&input)
        .find_map(std::result::Result::err)
        .expect("expected a parse error");
    assert_eq!(
        err.pos.byte_offset, 1025,
        "error byte_offset should point to the ':' at byte 1025"
    );
    assert_eq!(
        err.pos.column, 1025,
        "error column should point to the ':' at column 1025"
    );
}

#[test]
fn e2_error_message_contains_expected_substrings() {
    let key = "a".repeat(1025);
    let input = format!("{key}: value\n");
    let msg = first_error_message(&input).expect("expected a parse error");
    assert!(
        msg.contains("1024 Unicode characters"),
        "error message should contain '1024 Unicode characters', got: {msg}"
    );
    assert!(
        msg.contains("§8.2.2"),
        "error message should cite §8.2.2, got: {msg}"
    );
}

// ===========================================================================
// Group F: Integration via load()
// ===========================================================================

#[test]
fn f1_load_with_overlong_key_returns_err() {
    let key = "a".repeat(1025);
    let input = format!("{key}: value\n");
    let result = load(&input);
    assert!(
        result.is_err(),
        "load() should return Err for overlong implicit key"
    );
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("1024"),
        "load() error should mention '1024', got: {err_msg}"
    );
}

#[test]
fn f2_load_nested_overlong_key_returns_err_at_correct_position() {
    // Nested mapping — the overlong key is on line 2 (1-indexed).
    let key = "a".repeat(1025);
    let input = format!("outer:\n  {key}: value\n");
    let result = load(&input);
    assert!(
        result.is_err(),
        "load() should return Err for overlong implicit key in nested mapping"
    );
    // Confirm the error is not on line 1 (the outer key is fine).
    let err_str = result.unwrap_err().to_string();
    assert!(
        err_str.contains("1024"),
        "nested overlong key error should mention '1024', got: {err_str}"
    );
}

// ===========================================================================
// Group G: Interaction with single-line restriction
// ===========================================================================

#[test]
fn g1_multiline_flow_collection_key_fires_multiline_error_not_length_error() {
    // A block mapping whose nested key is a multi-line flow collection.
    // `key:\n  [a\n  b]: value` — the `[a\n  b]` spans two lines, which is
    // illegal as an implicit mapping key (YAML 1.2 §7.4.2).  The multi-line
    // flow collection error must fire, not the new block key length error.
    let input = "key:\n  [a\n  b]: value\n";
    assert!(
        has_parse_error(input),
        "multi-line flow collection as implicit key should produce a parse error"
    );
    let msg = first_error_message(input).expect("expected an error");
    assert!(
        msg.contains("multi-line flow collection"),
        "error should mention 'multi-line flow collection', not the length limit; got: {msg}"
    );
    assert!(
        !msg.contains("1024"),
        "length-limit error must not fire for a short multi-line key; got: {msg}"
    );
}

// ===========================================================================
// Group H: Flow-mapping boundary acceptance — plain ASCII keys
//          `{key: value}` form (YAML 1.2 §7.4.3 [154])
// ===========================================================================

#[test]
fn h1_flow_map_1024_ascii_plain_key_parses_successfully() {
    let key = "a".repeat(1024);
    let input = format!("{{{key}: value}}");
    assert!(
        parses_clean(&input),
        "1024-char ASCII key in flow mapping should parse without error"
    );
}

#[test]
fn h2_flow_map_1025_ascii_plain_key_produces_error() {
    let key = "a".repeat(1025);
    let input = format!("{{{key}: value}}");
    assert!(
        has_parse_error(&input),
        "1025-char ASCII key in flow mapping should produce a parse error"
    );
    let msg = first_error_message(&input).unwrap();
    assert!(
        msg.contains("1024"),
        "error message should mention '1024', got: {msg}"
    );
    assert!(
        msg.contains("§7.4.3"),
        "error message should cite §7.4.3, got: {msg}"
    );
}

#[test]
fn h3_flow_map_short_key_parses_successfully() {
    assert!(
        parses_clean("{k: v}"),
        "short key in flow mapping should parse"
    );
}

#[test]
fn h4_flow_map_empty_key_parses_successfully() {
    assert!(
        parses_clean("{: v}"),
        "empty (zero-char) key in flow mapping should parse"
    );
}

// ===========================================================================
// Group I: Flow-sequence single-pair boundary — plain ASCII keys
//          `[key: value]` form (YAML 1.2 §7.4.3 [154])
// ===========================================================================

#[test]
fn i1_flow_seq_1024_ascii_plain_key_parses_successfully() {
    let key = "a".repeat(1024);
    let input = format!("[{key}: value]");
    assert!(
        parses_clean(&input),
        "1024-char ASCII key in flow sequence should parse without error"
    );
}

#[test]
fn i2_flow_seq_1025_ascii_plain_key_produces_error() {
    let key = "a".repeat(1025);
    let input = format!("[{key}: value]");
    assert!(
        has_parse_error(&input),
        "1025-char ASCII key in flow sequence should produce a parse error"
    );
    let msg = first_error_message(&input).unwrap();
    assert!(
        msg.contains("1024"),
        "error message should mention '1024', got: {msg}"
    );
    assert!(
        msg.contains("§7.4.3"),
        "error message should cite §7.4.3, got: {msg}"
    );
}

#[test]
fn i3_flow_seq_short_key_parses_successfully() {
    assert!(
        parses_clean("[k: v]"),
        "short key in flow sequence should parse"
    );
}

// ===========================================================================
// Group J: Flow context Unicode / multibyte boundary
// ===========================================================================

#[test]
fn j1_flow_map_1024_two_byte_chars_parse_successfully() {
    let key = "é".repeat(1024);
    let input = format!("{{{key}: value}}");
    assert!(
        parses_clean(&input),
        "1024 two-byte chars in flow mapping should parse — limit is chars not bytes"
    );
}

#[test]
fn j2_flow_map_1025_two_byte_chars_produce_error() {
    let key = "é".repeat(1025);
    let input = format!("{{{key}: value}}");
    assert!(
        has_parse_error(&input),
        "1025 two-byte chars in flow mapping should produce a parse error"
    );
    let msg = first_error_message(&input).unwrap();
    assert!(
        msg.contains("1024"),
        "error message should mention '1024', got: {msg}"
    );
}

#[test]
fn j3_flow_seq_1024_three_byte_chars_parse_successfully() {
    let key = "".repeat(1024);
    let input = format!("[{key}: value]");
    assert!(
        parses_clean(&input),
        "1024 three-byte chars in flow sequence should parse"
    );
}

#[test]
fn j4_flow_seq_1025_four_byte_chars_produce_error() {
    let key = "\u{1D11E}".repeat(1025);
    let input = format!("[{key}: value]");
    assert!(
        has_parse_error(&input),
        "1025 four-byte chars in flow sequence should produce a parse error"
    );
}

// ===========================================================================
// Group K: Quoted (JSON-key) implicit keys in flow context
//          Both `{"key": value}` (double-quoted) and `{'key': value}` (single-quoted)
//          forms (YAML 1.2 §7.4.3 [155]).
//
// Quote-inclusive measurement: `"content"` counts as len(content)+2 chars.
// A 1022-content-char quoted key has a 1024-char slice — at the limit.
// A 1023-content-char quoted key has a 1025-char slice — over the limit.
// ===========================================================================

#[test]
fn k1_flow_map_double_quoted_key_at_limit_parses_successfully() {
    let content = "a".repeat(1022);
    let input = format!("{{\"{content}\": value}}");
    assert!(
        parses_clean(&input),
        "double-quoted key with 1024-char slice in flow mapping should parse"
    );
}

#[test]
fn k2_flow_map_double_quoted_key_over_limit_produces_error() {
    let content = "a".repeat(1023);
    let input = format!("{{\"{content}\": value}}");
    assert!(
        has_parse_error(&input),
        "double-quoted key with 1025-char slice in flow mapping should produce a parse error"
    );
    let msg = first_error_message(&input).unwrap();
    assert!(
        msg.contains("1024"),
        "error message should mention '1024', got: {msg}"
    );
}

#[test]
fn k3_flow_map_single_quoted_key_at_limit_parses_successfully() {
    let content = "a".repeat(1022);
    let input = format!("{{'{content}': value}}");
    assert!(
        parses_clean(&input),
        "single-quoted key with 1024-char slice in flow mapping should parse"
    );
}

#[test]
fn k4_flow_map_single_quoted_key_over_limit_produces_error() {
    let content = "a".repeat(1023);
    let input = format!("{{'{content}': value}}");
    assert!(
        has_parse_error(&input),
        "single-quoted key with 1025-char slice in flow mapping should produce a parse error"
    );
}

#[test]
fn k5_flow_seq_double_quoted_key_at_limit_parses_successfully() {
    let content = "a".repeat(1022);
    let input = format!("[\"{content}\": value]");
    assert!(
        parses_clean(&input),
        "double-quoted key with 1024-char slice in flow sequence should parse"
    );
}

#[test]
fn k6_flow_seq_double_quoted_key_over_limit_produces_error() {
    let content = "a".repeat(1023);
    let input = format!("[\"{content}\": value]");
    assert!(
        has_parse_error(&input),
        "double-quoted key with 1025-char slice in flow sequence should produce a parse error"
    );
    let msg = first_error_message(&input).unwrap();
    assert!(
        msg.contains("1024"),
        "error message should mention '1024', got: {msg}"
    );
}

#[test]
fn k7_flow_seq_single_quoted_key_at_limit_parses_successfully() {
    let content = "a".repeat(1022);
    let input = format!("['{content}': value]");
    assert!(
        parses_clean(&input),
        "single-quoted key with 1024-char slice in flow sequence should parse"
    );
}

#[test]
fn k8_flow_seq_single_quoted_key_over_limit_produces_error() {
    let content = "a".repeat(1023);
    let input = format!("['{content}': value]");
    assert!(
        has_parse_error(&input),
        "single-quoted key with 1025-char slice in flow sequence should produce a parse error"
    );
}

// ===========================================================================
// Group L: Explicit key exemption in flow context
// ===========================================================================

#[test]
fn l1_explicit_key_in_flow_mapping_with_long_key_parses_successfully() {
    // `{? <1025-char key>: value}` — explicit `?` form is not subject to the limit.
    let key = "a".repeat(1025);
    let input = format!("{{? {key}: value}}");
    assert!(
        parses_clean(&input),
        "explicit '?' key in flow mapping with >1024 chars should not be limited"
    );
}

#[test]
fn l2_explicit_key_in_flow_sequence_with_long_key_parses_successfully() {
    // `[? <1025-char key>: value]` — explicit `?` form is not subject to the limit.
    let key = "a".repeat(1025);
    let input = format!("[? {key}: value]");
    assert!(
        parses_clean(&input),
        "explicit '?' key in flow sequence with >1024 chars should not be limited"
    );
}

// ===========================================================================
// Group M: Error position and message in flow context
// ===========================================================================

#[test]
fn m1_flow_map_error_position_points_to_colon_indicator() {
    // `{` at byte 0, 1025-char key at bytes 1–1025, `:` at byte 1026.
    let key = "a".repeat(1025);
    let input = format!("{{{key}: value}}");
    let err = parse_events(&input)
        .find_map(std::result::Result::err)
        .expect("expected a parse error");
    assert_eq!(
        err.pos.byte_offset, 1026,
        "error byte_offset should point to the ':' at byte 1026"
    );
    assert_eq!(
        err.pos.column, 1026,
        "error column should point to the ':' at column 1026"
    );
}

#[test]
fn m2_flow_seq_error_position_points_to_colon_indicator() {
    // `[` at byte 0, 1025-char key at bytes 1–1025, `:` at byte 1026.
    let key = "a".repeat(1025);
    let input = format!("[{key}: value]");
    let err = parse_events(&input)
        .find_map(std::result::Result::err)
        .expect("expected a parse error");
    assert_eq!(
        err.pos.byte_offset, 1026,
        "error byte_offset should point to the ':' at byte 1026"
    );
}

#[test]
fn m3_flow_error_message_contains_expected_substrings() {
    let key = "a".repeat(1025);
    let input = format!("{{{key}: value}}");
    let msg = first_error_message(&input).expect("expected a parse error");
    assert!(
        msg.contains("1024 Unicode characters"),
        "error message should contain '1024 Unicode characters', got: {msg}"
    );
    assert!(
        msg.contains("§7.4.3"),
        "error message should cite §7.4.3, got: {msg}"
    );
}

// ===========================================================================
// Group N: Integration via load() in flow context
// ===========================================================================

#[test]
fn n1_load_flow_map_overlong_key_returns_err() {
    let key = "a".repeat(1025);
    let input = format!("{{{key}: value}}");
    let result = load(&input);
    assert!(
        result.is_err(),
        "load() should return Err for overlong implicit key in flow mapping"
    );
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("1024"),
        "load() error should mention '1024', got: {err_msg}"
    );
}

#[test]
fn n2_load_flow_seq_overlong_key_returns_err() {
    let key = "a".repeat(1025);
    let input = format!("[{key}: value]");
    let result = load(&input);
    assert!(
        result.is_err(),
        "load() should return Err for overlong implicit key in flow sequence"
    );
}

// ===========================================================================
// Group H (continued): Interaction with other flow-context restrictions
// ===========================================================================

#[test]
fn h5_flow_seq_single_line_restriction_fires_before_length_check() {
    // Key `aaa` ends on line 1; `:` is on line 2 with no continuation on line 2.
    // The single-line restriction must fire, not the length-limit error.
    let input = "[aaa\n: v]";
    assert!(
        has_parse_error(input),
        "flow-seq key with colon on a different line should produce a parse error"
    );
    let msg = first_error_message(input).expect("expected an error");
    assert!(
        msg.contains("single line"),
        "error should mention 'single line', got: {msg}"
    );
    assert!(
        !msg.contains("1024"),
        "length-limit error must not fire for a short key; got: {msg}"
    );
}

#[test]
fn h6_second_key_in_multi_entry_flow_map_also_checked() {
    // First key is short and valid; second key is overlong — the error must fire.
    let long_key = "a".repeat(1025);
    let input = format!("{{ok: v, {long_key}: v}}");
    assert!(
        has_parse_error(&input),
        "overlong second key in multi-entry flow mapping should produce a parse error"
    );
    let msg = first_error_message(&input).unwrap();
    assert!(
        msg.contains("1024"),
        "error message should mention '1024', got: {msg}"
    );
}

#[test]
fn h7_nested_flow_map_inner_overlong_key_caught() {
    // Outer key is short; inner key is overlong — the inner error must fire.
    let long_key = "a".repeat(1025);
    let input = format!("{{outer: {{{long_key}: v}}}}");
    assert!(
        has_parse_error(&input),
        "overlong key in nested flow mapping should produce a parse error"
    );
    let msg = first_error_message(&input).unwrap();
    assert!(
        msg.contains("1024"),
        "error message should mention '1024', got: {msg}"
    );
}

#[test]
fn h8_flow_seq_overlong_multiline_key_fires_single_line_error_not_length_error() {
    // A 1025-char key ends on line 1; `:` is on line 2.
    // The single-line restriction must fire before the length check so the error
    // message cites the line restriction, not the character limit.
    let key = "a".repeat(1025);
    let input = format!("[{key}\n: v]");
    assert!(
        has_parse_error(&input),
        "overlong flow-seq key with colon on next line should produce a parse error"
    );
    let msg = first_error_message(&input).expect("expected an error");
    assert!(
        msg.contains("single line"),
        "error should cite the single-line restriction, got: {msg}"
    );
    assert!(
        !msg.contains("1024"),
        "length-limit error must not fire when single-line restriction fires first; got: {msg}"
    );
}