pictor-runtime 0.1.0

Inference runtime, sampling, tokenizer, and server for Pictor
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
//! Integration tests for the JSON Schema → BNF Grammar compiler.
//!
//! Tests cover: all primitive types, composite types, `enum`, `anyOf`,
//! `oneOf`, `allOf`, `$ref` / `$defs`, depth limit, unsupported keywords,
//! error cases, and end-to-end round-trips via `GrammarConstraint`.

use std::sync::Arc;

use pictor_runtime::constrained_decoding::TokenConstraint;
use pictor_runtime::grammar::{
    compile_json_schema, compile_json_schema_str, EarleyRecognizer, GrammarConstraint,
    JsonSchemaCompileError,
};

// ─────────────────────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────────────────────

/// Build a `GrammarConstraint` with a byte-level ASCII vocab (token id == byte value).
fn ascii_constraint_from_schema(schema_json: &str) -> GrammarConstraint {
    let grammar = compile_json_schema_str(schema_json).expect("schema must compile");
    GrammarConstraint::new(
        grammar,
        |id| if id < 128 { vec![id as u8] } else { vec![] },
        128,
    )
}

/// Feed an entire string into a `GrammarConstraint` advancing token-by-token.
/// Returns `true` iff all tokens were accepted.
fn feed_str_constraint(c: &mut GrammarConstraint, s: &str) -> bool {
    for b in s.bytes() {
        if !c.advance(b as u32) {
            return false;
        }
    }
    true
}

/// Build a normalised `EarleyRecognizer` from a compiled JSON schema.
fn recognizer_from_schema(schema_json: &str) -> EarleyRecognizer {
    let mut g = compile_json_schema_str(schema_json).expect("schema must compile");
    g.normalise_terminals();
    EarleyRecognizer::new(Arc::new(g))
}

/// Feed bytes into an `EarleyRecognizer`; returns false if any byte is rejected.
fn feed_str_recognizer(rec: &mut EarleyRecognizer, s: &str) -> bool {
    for b in s.bytes() {
        if !rec.feed_byte(b) {
            return false;
        }
    }
    true
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 1: compile_empty_object_schema
//
// An empty schema `{}` has no "type" — we treat it as "any JSON value".
// This produces a grammar with at least one rule (it succeeds, not an error).
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_empty_object_schema() {
    // `{}` has no "type"; compiler treats it as any-value (non-error).
    let result = compile_json_schema_str("{}");
    assert!(
        result.is_ok(),
        "empty schema should compile successfully, got: {:?}",
        result.err()
    );
    let g = result.unwrap();
    assert!(!g.rules.is_empty(), "compiled grammar must have rules");
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 2: compile_string_type
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_string_type() {
    let g = compile_json_schema_str(r#"{"type":"string"}"#).expect("should compile");
    assert!(!g.rules.is_empty());
    // Start NT must have rules.
    let start_rules: Vec<_> = g.rules_for(g.start()).collect();
    assert!(
        !start_rules.is_empty(),
        "start NT must have at least one rule"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 3: compile_integer_type
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_integer_type() {
    let mut rec = recognizer_from_schema(r#"{"type":"integer"}"#);
    assert!(
        feed_str_recognizer(&mut rec, "42"),
        "integer 42 should be accepted"
    );
    assert!(rec.is_accepting(), "42 is a complete integer");

    // Negative integer.
    let mut rec2 = recognizer_from_schema(r#"{"type":"integer"}"#);
    assert!(feed_str_recognizer(&mut rec2, "-7"));
    assert!(rec2.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 4: compile_number_type
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_number_type() {
    let mut rec = recognizer_from_schema(r#"{"type":"number"}"#);
    assert!(
        feed_str_recognizer(&mut rec, "3"),
        "whole number should be accepted"
    );
    assert!(rec.is_accepting());

    // Float.
    let mut rec2 = recognizer_from_schema(r#"{"type":"number"}"#);
    assert!(feed_str_recognizer(&mut rec2, "3.14"));
    assert!(rec2.is_accepting(), "3.14 must be accepted as a number");

    // Negative float.
    let mut rec3 = recognizer_from_schema(r#"{"type":"number"}"#);
    assert!(feed_str_recognizer(&mut rec3, "-0.5"));
    assert!(rec3.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 5: compile_boolean_type
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_boolean_type() {
    let mut rec_true = recognizer_from_schema(r#"{"type":"boolean"}"#);
    assert!(feed_str_recognizer(&mut rec_true, "true"));
    assert!(rec_true.is_accepting());

    let mut rec_false = recognizer_from_schema(r#"{"type":"boolean"}"#);
    assert!(feed_str_recognizer(&mut rec_false, "false"));
    assert!(rec_false.is_accepting());

    // Invalid.
    let mut rec_bad = recognizer_from_schema(r#"{"type":"boolean"}"#);
    let ok = feed_str_recognizer(&mut rec_bad, "yes");
    assert!(!ok || !rec_bad.is_accepting(), "\"yes\" is not a boolean");
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 6: compile_null_type
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_null_type() {
    let mut rec = recognizer_from_schema(r#"{"type":"null"}"#);
    assert!(feed_str_recognizer(&mut rec, "null"));
    assert!(rec.is_accepting());

    let mut rec_bad = recognizer_from_schema(r#"{"type":"null"}"#);
    let ok = feed_str_recognizer(&mut rec_bad, "undefined");
    assert!(!ok || !rec_bad.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 7: compile_object_with_required_props
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_object_with_required_props() {
    let schema = r#"{
        "type": "object",
        "properties": {
            "x": {"type": "integer"},
            "y": {"type": "string"}
        },
        "required": ["x", "y"]
    }"#;
    let g = compile_json_schema_str(schema).expect("should compile");

    // Grammar must reference NTs for integer and string.
    // At minimum: start NT + object NT + integer NT + string NT + digit NT etc.
    assert!(
        g.nt_count >= 3,
        "must have multiple NTs for object with properties"
    );

    // Check grammar has at least 2 rules (object body + value NTs).
    assert!(g.rules.len() >= 2);
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 8: compile_array_of_integers
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_array_of_integers() {
    let schema = r#"{"type":"array","items":{"type":"integer"}}"#;
    let mut rec = recognizer_from_schema(schema);

    // Empty array.
    assert!(feed_str_recognizer(&mut rec, "[]"));
    assert!(rec.is_accepting());

    // Single-element array.
    let mut rec2 = recognizer_from_schema(schema);
    assert!(feed_str_recognizer(&mut rec2, "[42]"));
    assert!(rec2.is_accepting());

    // Multi-element array.
    let mut rec3 = recognizer_from_schema(schema);
    assert!(feed_str_recognizer(&mut rec3, "[1,2,3]"));
    assert!(rec3.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 9: compile_nested_object_array (3 levels deep)
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_nested_object_array() {
    let schema = r#"{
        "type": "object",
        "properties": {
            "items": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "id": {"type": "integer"}
                    },
                    "required": ["id"]
                }
            }
        },
        "required": ["items"]
    }"#;
    let g = compile_json_schema_str(schema).expect("nested schema should compile");
    // 3 levels → object, array, inner object, integer NTs all present.
    assert!(g.nt_count >= 4, "expected multiple NTs for 3-level nesting");
    assert!(!g.rules.is_empty());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 10: compile_enum_strings
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_enum_strings() {
    let schema = r#"{"enum":["foo","bar","baz"]}"#;
    let mut c = ascii_constraint_from_schema(schema);

    // '"' starts a string literal — should be the only allowed first byte.
    let mask = c.allowed_tokens(&[], 128).unwrap();
    assert!(
        mask[b'"' as usize],
        "quote should be allowed at start of enum string"
    );
    assert!(
        !mask[b'x' as usize],
        "'x' not a valid start for any enum value"
    );

    // Feed '"foo"' — must be accepted.
    assert!(feed_str_constraint(&mut c, r#""foo""#));
    assert!(c.is_complete());

    // '"qux"' should be rejected (not in enum).
    let mut c2 = ascii_constraint_from_schema(schema);
    let ok = feed_str_constraint(&mut c2, r#""qux""#);
    // Either rejected mid-stream or not accepting at end.
    assert!(!ok || !c2.is_complete());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 11: compile_enum_integers
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_enum_integers() {
    let schema = r#"{"enum":[1,2,3]}"#;
    let mut rec = recognizer_from_schema(schema);

    assert!(feed_str_recognizer(&mut rec, "2"));
    assert!(rec.is_accepting());

    // 4 is not in the enum.
    let mut rec2 = recognizer_from_schema(schema);
    let ok = feed_str_recognizer(&mut rec2, "4");
    assert!(!ok || !rec2.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 12: compile_any_of
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_any_of() {
    let schema = r#"{"anyOf":[{"type":"string"},{"type":"integer"}]}"#;
    let mut rec_str = recognizer_from_schema(schema);

    // String value.
    assert!(feed_str_recognizer(&mut rec_str, r#""hello""#));
    assert!(rec_str.is_accepting());

    // Integer value.
    let mut rec_int = recognizer_from_schema(schema);
    assert!(feed_str_recognizer(&mut rec_int, "99"));
    assert!(rec_int.is_accepting());

    // Boolean — not in anyOf.
    let mut rec_bad = recognizer_from_schema(schema);
    let ok = feed_str_recognizer(&mut rec_bad, "true");
    assert!(!ok || !rec_bad.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 13: compile_one_of (identical behaviour to anyOf)
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_one_of() {
    let schema = r#"{"oneOf":[{"type":"boolean"},{"type":"null"}]}"#;
    let mut rec_true = recognizer_from_schema(schema);
    assert!(feed_str_recognizer(&mut rec_true, "true"));
    assert!(rec_true.is_accepting());

    let mut rec_null = recognizer_from_schema(schema);
    assert!(feed_str_recognizer(&mut rec_null, "null"));
    assert!(rec_null.is_accepting());

    // Integer — not in oneOf.
    let mut rec_int = recognizer_from_schema(schema);
    let ok = feed_str_recognizer(&mut rec_int, "42");
    assert!(!ok || !rec_int.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 14: compile_ref_to_defs
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_ref_to_defs() {
    let schema = r##"{
        "$defs": {
            "MyString": {"type": "string"}
        },
        "$ref": "#/$defs/MyString"
    }"##;
    let g = compile_json_schema_str(schema).expect("$ref to $defs must compile");
    assert!(!g.rules.is_empty());
    // The compiled grammar should resolve the reference.
    let mut rec = EarleyRecognizer::new({
        let mut g2 = g;
        g2.normalise_terminals();
        Arc::new(g2)
    });
    assert!(feed_str_recognizer(&mut rec, r#""hello""#));
    assert!(rec.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 15: compile_recursive_ref
//
// A linked-list style schema:
// Node: { "value": integer, "next": { "$ref": "#/$defs/Node" } | null }
//
// We model this as:
//   $defs.Node: object with required ["value","next"]
//   where "next" is anyOf [$ref Node, null]
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_recursive_ref() {
    // Self-referential: Node has a required "value" integer field.
    // The compiler must survive this without infinite recursion.
    let schema = r##"{
        "$defs": {
            "Node": {
                "type": "object",
                "properties": {
                    "value": {"type": "integer"},
                    "next": {
                        "anyOf": [
                            {"$ref": "#/$defs/Node"},
                            {"type": "null"}
                        ]
                    }
                },
                "required": ["value", "next"]
            }
        },
        "$ref": "#/$defs/Node"
    }"##;
    // Must compile without panic or DepthExceeded.
    let result = compile_json_schema_str(schema);
    // The recursive $ref is handled by the pre-allocation in pass 1 — the
    // body references the already-allocated NT id for "Node", so there is
    // no infinite expansion during compilation.  The runtime Earley parser
    // handles the recursive grammar correctly.
    assert!(
        result.is_ok(),
        "recursive $ref schema must compile, got: {:?}",
        result.err()
    );
    let g = result.unwrap();
    assert!(!g.rules.is_empty());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 16: unsupported_keyword_not
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn unsupported_keyword_not() {
    let schema = r#"{"not": {"type": "string"}}"#;
    let err = compile_json_schema_str(schema).expect_err("'not' should not be supported");
    assert!(
        matches!(err, JsonSchemaCompileError::UnsupportedKeyword(ref kw) if kw == "not"),
        "expected UnsupportedKeyword(\"not\"), got: {err:?}"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 17: unsupported_keyword_if_then
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn unsupported_keyword_if_then() {
    let schema = r#"{"if": {"type": "string"}, "then": {"type": "integer"}}"#;
    let err = compile_json_schema_str(schema).expect_err("'if'/'then' should not be supported");
    assert!(
        matches!(err, JsonSchemaCompileError::UnsupportedKeyword(_)),
        "expected UnsupportedKeyword, got: {err:?}"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 18: invalid_json_str
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn invalid_json_str() {
    let err = compile_json_schema_str("not json at all }{").expect_err("invalid JSON must fail");
    assert!(
        matches!(err, JsonSchemaCompileError::InvalidJson(_)),
        "expected InvalidJson, got: {err:?}"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 19: dangling_ref
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn dangling_ref() {
    // $ref to a definition that doesn't exist in $defs.
    let schema = r##"{"$ref": "#/$defs/Missing"}"##;
    let err = compile_json_schema_str(schema).expect_err("dangling $ref must fail");
    assert!(
        matches!(err, JsonSchemaCompileError::DanglingRef(_)),
        "expected DanglingRef, got: {err:?}"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 20: depth_exceeded
//
// Build a schema nested 33 levels deep — one more than the limit of 32.
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn depth_exceeded() {
    // Build a schema with 33 levels of anyOf nesting.
    let mut schema = serde_json::json!({"type": "integer"});
    for _ in 0..33 {
        schema = serde_json::json!({"anyOf": [schema]});
    }
    let schema_str = serde_json::to_string(&schema).unwrap();
    let err =
        compile_json_schema_str(&schema_str).expect_err("depth > 32 must return DepthExceeded");
    assert!(
        matches!(err, JsonSchemaCompileError::DepthExceeded { limit: 32 }),
        "expected DepthExceeded {{ limit: 32 }}, got: {err:?}"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 21: all_of_merges_objects
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn all_of_merges_objects() {
    let schema = r#"{
        "allOf": [
            {
                "type": "object",
                "properties": {"x": {"type": "integer"}},
                "required": ["x"]
            },
            {
                "type": "object",
                "properties": {"y": {"type": "string"}},
                "required": ["y"]
            }
        ]
    }"#;
    let g = compile_json_schema_str(schema).expect("allOf of objects must compile");
    assert!(!g.rules.is_empty(), "merged object must produce rules");

    // The merged object schema should include rules for both x (integer) and y (string).
    // We verify by checking that the NT names include both integer and string NTs.
    let has_integer_nt = g.nt_names.values().any(|n| n.contains("integer"));
    let has_string_nt = g.nt_names.values().any(|n| n.contains("string"));
    assert!(has_integer_nt, "merged schema should contain integer NT");
    assert!(has_string_nt, "merged schema should contain string NT");
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 22: end_to_end_grammar_constraint_string_type
//
// Compile string schema → GrammarConstraint → verify allowed_tokens:
//   - '"' (0x22) must be allowed as first byte.
//   - digit bytes ('0'..'9') must NOT be allowed as first byte.
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn end_to_end_grammar_constraint_string_type() {
    let schema = r#"{"type":"string"}"#;
    let grammar = compile_json_schema_str(schema).expect("string schema must compile");

    // Byte-level vocab where token id == byte value (0..128).
    let c = GrammarConstraint::new(
        grammar,
        |id| if id < 128 { vec![id as u8] } else { vec![] },
        128,
    );

    let mask = c
        .allowed_tokens(&[], 128)
        .expect("allowed_tokens must return Some");

    // '"' (0x22 = 34) must be allowed — it starts any string value.
    assert!(
        mask[b'"' as usize],
        "quote character (0x22) must be allowed at start of string type"
    );

    // Digit tokens must NOT be allowed (digits don't start a JSON string).
    for d in b'0'..=b'9' {
        assert!(
            !mask[d as usize],
            "digit '{}' should NOT be allowed at start of string type",
            d as char
        );
    }

    // Boolean and null starters must also be rejected.
    assert!(!mask[b't' as usize], "'t' should not start a string");
    assert!(!mask[b'f' as usize], "'f' should not start a string");
    assert!(!mask[b'n' as usize], "'n' should not start a string");
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 23: end_to_end_integer_constraint_advance
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn end_to_end_integer_constraint_advance() {
    let schema = r#"{"type":"integer"}"#;
    let mut c = ascii_constraint_from_schema(schema);

    // Digits must be allowed at start.
    let mask = c.allowed_tokens(&[], 128).unwrap();
    for d in b'0'..=b'9' {
        assert!(
            mask[d as usize],
            "digit '{}' must be allowed at start",
            d as char
        );
    }
    // '-' is also valid at start (negative integer).
    assert!(
        mask[b'-' as usize],
        "'-' must be allowed at start of integer"
    );

    // Feed "123" and check we are accepting.
    assert!(feed_str_constraint(&mut c, "123"));
    assert!(c.is_complete(), "123 is a complete integer");
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 24: enum_bool_null values
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_enum_bool_null() {
    let schema = r#"{"enum":[true,false,null]}"#;
    let mut rec_true = recognizer_from_schema(schema);
    assert!(feed_str_recognizer(&mut rec_true, "true"));
    assert!(rec_true.is_accepting());

    let mut rec_false = recognizer_from_schema(schema);
    assert!(feed_str_recognizer(&mut rec_false, "false"));
    assert!(rec_false.is_accepting());

    let mut rec_null = recognizer_from_schema(schema);
    assert!(feed_str_recognizer(&mut rec_null, "null"));
    assert!(rec_null.is_accepting());

    // "maybe" is not in the enum.
    let mut rec_bad = recognizer_from_schema(schema);
    let ok = feed_str_recognizer(&mut rec_bad, "maybe");
    assert!(!ok || !rec_bad.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 25: object_empty_required
//
// An object with no required properties emits "{}" grammar.
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn object_empty_required() {
    let schema = r#"{"type":"object","properties":{"x":{"type":"integer"}}}"#;
    let mut rec = recognizer_from_schema(schema);
    // Only empty object is valid (no required properties → only `{}` rule).
    assert!(feed_str_recognizer(&mut rec, "{}"));
    assert!(rec.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 26: compile_definitions_alias
//
// Use `definitions` (older JSON Schema draft) instead of `$defs`.
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_definitions_alias() {
    let schema = r##"{
        "definitions": {
            "Num": {"type": "integer"}
        },
        "$ref": "#/definitions/Num"
    }"##;
    let g = compile_json_schema_str(schema).expect("'definitions' alias must compile");
    assert!(!g.rules.is_empty());

    let mut rec = EarleyRecognizer::new({
        let mut g2 = g;
        g2.normalise_terminals();
        Arc::new(g2)
    });
    assert!(feed_str_recognizer(&mut rec, "7"));
    assert!(rec.is_accepting());
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 27: allOf_rejects_non_object
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn all_of_rejects_non_object() {
    let schema = r#"{"allOf":[{"type":"string"},{"type":"integer"}]}"#;
    let err = compile_json_schema_str(schema).expect_err("allOf of non-objects must fail");
    assert!(
        matches!(err, JsonSchemaCompileError::UnsupportedKeyword(_)),
        "expected UnsupportedKeyword for allOf of non-objects, got: {err:?}"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 28: unsupported_keyword_pattern
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn unsupported_keyword_pattern() {
    let schema = r#"{"type":"string","pattern":"^[a-z]+"}"#;
    let err = compile_json_schema_str(schema).expect_err("'pattern' should not be supported");
    assert!(
        matches!(err, JsonSchemaCompileError::UnsupportedKeyword(ref kw) if kw == "pattern"),
        "expected UnsupportedKeyword(\"pattern\"), got: {err:?}"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 29: grammar_has_correct_start_nt
//
// Verify that the start NT returned by compile_json_schema is consistently
// the root of the compiled language.
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn grammar_has_correct_start_nt() {
    let g = compile_json_schema_str(r#"{"type":"boolean"}"#).expect("must compile");
    let start = g.start();
    // Start NT must have at least one rule.
    let start_rules: Vec<_> = g.rules_for(start).collect();
    assert!(
        !start_rules.is_empty(),
        "start NT {start} must have at least one rule, got 0"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Test 30: compile_json_schema_value_api
//
// Test the `compile_json_schema(&Value)` API directly (not the str variant).
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn compile_json_schema_value_api() {
    let schema_val = serde_json::json!({"type": "null"});
    let g = compile_json_schema(&schema_val).expect("must compile from Value");
    assert!(!g.rules.is_empty());

    let mut rec = EarleyRecognizer::new({
        let mut g2 = g;
        g2.normalise_terminals();
        Arc::new(g2)
    });
    assert!(feed_str_recognizer(&mut rec, "null"));
    assert!(rec.is_accepting());
}