genson-core 0.2.5

Core library for JSON schema inference using genson-rs
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
// genson-core/src/tests/schema.rs
use super::*;
use crate::debug;
use predicates::prelude::*;
use serde_json::json;

#[test]
fn test_reorder_unions_string_float_null() {
    // Unordered union: string, float, null
    let mut schema = json!({
        "type": ["string", "float", "null"]
    });

    reorder_unions(&mut schema);

    // After reordering, null should come first, then float/number, then string
    assert_eq!(
        schema,
        json!({
            "type": ["null", "float", "string"]
        })
    );
}

#[test]
fn test_basic_schema_inference() {
    let json_strings = vec![
        r#"{"name": "Alice", "age": 30}"#.to_string(),
        r#"{"name": "Bob", "age": 25, "city": "NYC"}"#.to_string(),
    ];

    let result = infer_json_schema_from_strings(&json_strings, SchemaInferenceConfig::default())
        .expect("Schema inference should succeed");

    // Test processed count
    assert_eq!(result.processed_count, 2);

    // Use predicates to test schema structure
    let schema_str = result.schema.to_string();

    predicate::str::contains("\"type\"")
        .and(predicate::str::contains("object"))
        .eval(&schema_str);

    predicate::str::contains("\"properties\"").eval(&schema_str);

    // Check that both name and age properties are present
    predicate::str::contains("\"name\"")
        .and(predicate::str::contains("\"age\""))
        .eval(&schema_str);

    println!(
        "✅ Generated schema: {}",
        serde_json::to_string_pretty(&result.schema).unwrap()
    );
}

#[test]
fn test_empty_input() {
    let json_strings = vec![];
    let result = infer_json_schema_from_strings(&json_strings, SchemaInferenceConfig::default());

    assert!(result.is_err());

    let error_msg = result.unwrap_err();
    predicate::str::contains("No JSON strings provided").eval(&error_msg);

    println!("✅ Empty input correctly rejected with: {}", error_msg);
}

#[test]
fn test_invalid_json_variants() {
    let test_cases = vec![
        (
            r#"{"name": "Alice"}"#,
            r#"{"invalid": json}"#,
            "unquoted value",
        ),
        (
            r#"{"valid": "json"}"#,
            r#"{"incomplete":"#,
            "incomplete string",
        ),
        (r#"{"good": "data"}"#, r#"{"trailing":,"#, "trailing comma"),
        (
            r#"{"working": true}"#,
            r#"{invalid: "json"}"#,
            "unquoted key",
        ),
        (
            r#"{"normal": "object"}"#,
            r#"{"nested": {"broken": json}}"#,
            "nested broken JSON",
        ),
    ];

    for (valid_json, invalid_json, description) in test_cases {
        println!("🧪 Testing: {}", description);
        println!("   Valid JSON: {}", valid_json);
        println!("   Invalid JSON: {}", invalid_json);

        let json_strings = vec![valid_json.to_string(), invalid_json.to_string()];

        let result =
            infer_json_schema_from_strings(&json_strings, SchemaInferenceConfig::default());

        // Should return an error instead of panicking
        assert!(result.is_err(), "Expected error for case: {}", description);

        let error_msg = result.unwrap_err();

        // Use predicates to verify error message content
        predicate::str::contains("Invalid JSON input at position").eval(&error_msg);

        // For short JSON strings, verify the content is included
        if invalid_json.len() <= MAX_JSON_ERROR_LENGTH {
            predicate::str::contains(invalid_json).eval(&error_msg);
        } else {
            // For long JSON, just check that truncation happened
            predicate::str::contains("truncated").eval(&error_msg);
        }

        // Ensure we don't have panic-related messages
        predicate::str::contains("panicked").not().eval(&error_msg);

        predicate::str::contains("SIGABRT").not().eval(&error_msg);

        println!("   ❌ Correctly failed with: {}", error_msg);
        println!();
    }
}

#[test]
fn test_mixed_valid_and_empty_strings() {
    let json_strings = vec![
        r#"{"name": "Alice", "age": 30}"#.to_string(),
        "".to_string(),    // Empty string should be skipped
        "   ".to_string(), // Whitespace-only should be skipped
        r#"{"name": "Bob", "age": 25}"#.to_string(),
    ];

    let result = infer_json_schema_from_strings(&json_strings, SchemaInferenceConfig::default())
        .expect("Should succeed with valid JSON, skipping empty strings");

    // Should process only the 2 valid JSON strings
    assert_eq!(result.processed_count, 2);

    let schema_str = result.schema.to_string();
    predicate::str::contains("\"name\"")
        .and(predicate::str::contains("\"age\""))
        .eval(&schema_str);

    println!(
        "✅ Processed {} valid strings, skipped empty ones",
        result.processed_count
    );
}

#[test]
fn test_schema_config_variations() {
    let json_strings = vec![r#"[{"item": "first"}, {"item": "second"}]"#.to_string()];

    // Test with ignore_outer_array = false
    let config_array = SchemaInferenceConfig {
        ignore_outer_array: false,
        ..Default::default()
    };

    let result = infer_json_schema_from_strings(&json_strings, config_array)
        .expect("Should handle array schema");

    let schema_str = result.schema.to_string();
    predicate::str::contains("\"type\"")
        .and(predicate::str::contains("array"))
        .eval(&schema_str);

    println!(
        "✅ Array schema: {}",
        serde_json::to_string_pretty(&result.schema).unwrap()
    );

    // Test with ignore_outer_array = true (default)
    let config_object = SchemaInferenceConfig {
        ignore_outer_array: true,
        ..Default::default()
    };

    let result = infer_json_schema_from_strings(&json_strings, config_object)
        .expect("Should handle object schema from array items");

    let schema_str = result.schema.to_string();
    predicate::str::contains("\"type\"")
        .and(predicate::str::contains("object"))
        .eval(&schema_str);

    predicate::str::contains("\"item\"").eval(&schema_str);

    println!(
        "✅ Object schema from array items: {}",
        serde_json::to_string_pretty(&result.schema).unwrap()
    );
}

#[test]
fn test_very_long_invalid_json() {
    // Create a very long invalid JSON string
    let long_value = "x".repeat(500); // 500 char string
    let long_invalid_json = format!(
        r#"{{"field1": "{}", "field2": "{}", "field3": "{}", "field4": "{}", "invalid_syntax": }}"#,
        long_value, long_value, long_value, long_value
    );

    let json_strings = vec![
        r#"{"valid": "json"}"#.to_string(),
        long_invalid_json.clone(),
    ];

    println!(
        "🧪 Testing very long invalid JSON ({} chars)",
        long_invalid_json.len()
    );

    let result = infer_json_schema_from_strings(&json_strings, SchemaInferenceConfig::default());

    assert!(result.is_err(), "Expected error for very long invalid JSON");

    let error_msg = result.unwrap_err();

    println!("The error message was: {}", error_msg);

    // Should contain truncation indicator
    predicate::str::contains("truncated").eval(&error_msg);

    // Should contain position information
    predicate::str::contains("Invalid JSON input at position 2").eval(&error_msg);

    // Error message should be reasonable length (much shorter than original JSON)
    assert!(
        error_msg.len() < long_invalid_json.len() / 2,
        "Error message should be much shorter than original JSON"
    );

    println!(
        "   ❌ Correctly truncated long JSON in error: {}",
        error_msg
    );

    // Verify the error message doesn't exceed a reasonable length
    assert!(
        error_msg.len() < 500,
        "Error message should be under 500 chars, got: {}",
        error_msg.len()
    );
}

#[test]
fn test_complex_nested_schema() {
    let json_strings = vec![
        json!({
            "user": {
                "id": 123,
                "profile": {
                    "name": "Alice",
                    "preferences": ["dark_mode", "notifications"]
                }
            },
            "metadata": {
                "created_at": "2024-01-01",
                "version": 1
            }
        })
        .to_string(),
        json!({
            "user": {
                "id": 456,
                "profile": {
                    "name": "Bob",
                    "preferences": ["light_mode"]
                }
            },
            "metadata": {
                "created_at": "2024-01-02",
                "version": 2
            }
        })
        .to_string(),
    ];

    let result = infer_json_schema_from_strings(&json_strings, SchemaInferenceConfig::default())
        .expect("Should handle complex nested schema");

    assert_eq!(result.processed_count, 2);

    let schema_str = result.schema.to_string();

    // Check for nested structure
    predicate::str::contains("\"user\"")
        .and(predicate::str::contains("\"metadata\""))
        .and(predicate::str::contains("\"profile\""))
        .and(predicate::str::contains("\"preferences\""))
        .eval(&schema_str);

    println!("✅ Complex nested schema generated successfully");
    println!(
        "Schema: {}",
        serde_json::to_string_pretty(&result.schema).unwrap()
    );
}

#[test]
fn test_ndjson_parsing() {
    // Two valid JSON objects separated by newlines (NDJSON format)
    let ndjson_input = r#"
{"name": "Alice", "age": 30}
{"name": "Bob", "age": 25, "city": "NYC"}
{"name": "Charlie"}
"#;

    let json_strings = vec![ndjson_input.to_string()];

    let config = SchemaInferenceConfig {
        delimiter: Some(b'\n'),
        ..Default::default()
    };

    let result = infer_json_schema_from_strings(&json_strings, config)
        .expect("NDJSON schema inference should succeed");

    // All 3 objects should be processed
    assert_eq!(result.processed_count, 1,
    "NDJSON should be counted as a single input string but parsed into multiple rows internally"
);

    let schema_str = result.schema.to_string();

    // The schema should include properties from all lines
    assert!(!schema_str.contains("Alice")); // values are not in schema
    assert!(schema_str.contains("\"name\""));
    assert!(schema_str.contains("\"age\""));
    assert!(schema_str.contains("\"city\""));

    println!(
        "✅ NDJSON schema generated: {}",
        serde_json::to_string_pretty(&result.schema).unwrap()
    );
}

#[test]
fn test_invalid_ndjson_line() {
    // Second line is malformed
    let ndjson_input = r#"
{"valid": true}
{"invalid": json}
{"also_valid": 123}
"#;

    let json_strings = vec![ndjson_input.to_string()];

    let config = SchemaInferenceConfig {
        delimiter: Some(b'\n'),
        ..Default::default()
    };

    let result = infer_json_schema_from_strings(&json_strings, config.clone());

    assert!(result.is_err(), "Expected error for malformed NDJSON line");

    let err_msg = result.unwrap_err();
    debug!(config, "Got error: {}", err_msg);
    assert!(
        err_msg.contains("Invalid JSON input at index 1: expected value at line 1 column 13"),
        "Error message should report the failing line"
    );
    println!("✅ Correctly rejected malformed NDJSON: {}", err_msg);
}

/// Two objects with varying keys and homogeneous string values (low map threshold)
#[test]
fn test_map_threshold_rewrite() {
    let json_strings = vec![
        r#"{"labels": {"en": "Hello", "fr": "Bonjour"}}"#.to_string(),
        r#"{"labels": {"de": "Hallo", "es": "Hola"}}"#.to_string(),
    ];

    let config = SchemaInferenceConfig {
        map_threshold: 2,
        ..Default::default()
    };
    let result = infer_json_schema_from_strings(&json_strings, config).unwrap();

    let labels = &result.schema["properties"]["labels"];
    assert_eq!(labels["type"], "object");
    assert!(labels.get("additionalProperties").is_some());
    assert!(labels.get("properties").is_none());
}

/// Two objects with varying keys and homogeneous string values (default map threshold)
#[test]
fn test_map_threshold_as_record() {
    let json_strings = vec![
        r#"{"labels": {"en": "Hello", "fr": "Bonjour"}}"#.to_string(),
        r#"{"labels": {"de": "Hallo", "es": "Hola"}}"#.to_string(),
    ];

    let result =
        infer_json_schema_from_strings(&json_strings, SchemaInferenceConfig::default()).unwrap();

    let labels = &result.schema["properties"]["labels"];
    assert!(labels.get("properties").is_some());
    assert!(labels.get("additionalProperties").is_none());
}

#[test]
fn test_wrap_root_inserts_single_required_field() {
    let json_strings = vec![
        r#"{"en":{"language":"en","value":"Hello"},"fr":{"language":"fr","value":"Bonjour"}}"#
            .to_string(),
    ];

    let cfg = SchemaInferenceConfig {
        wrap_root: Some("labels".to_string()),
        ..Default::default()
    };

    let out = infer_json_schema_from_strings(&json_strings, cfg).unwrap();
    let sch = out.schema;

    assert_eq!(sch["type"], "object");
    assert_eq!(sch["required"], serde_json::json!(["labels"]));
    assert!(sch["properties"]["labels"].is_object());
}

#[test]
fn test_rewrite_objects_map_of_records() {
    use serde_json::json;

    // Schema that genson-rs would roughly emit for {"en": {...}, "fr": {...}}
    let mut schema = json!({
        "type": "object",
        "properties": {
            "en": {
                "type": "object",
                "properties": {
                    "language": { "type": "string" },
                    "value": { "type": "string" }
                },
                "required": ["language", "value"]
            },
            "fr": {
                "type": "object",
                "properties": {
                    "language": { "type": "string" },
                    "value": { "type": "string" }
                },
                "required": ["language", "value"]
            }
        },
        "required": ["en","fr"]
    });

    let cfg = SchemaInferenceConfig {
        map_threshold: 2, // force detection at 2 keys
        ..Default::default()
    };

    rewrite_objects(&mut schema, None, &cfg);

    // After rewrite, we should have additionalProperties instead of fixed properties
    assert_eq!(schema["type"], "object");
    assert!(schema.get("properties").is_none());
    assert!(schema.get("required").is_none());

    // additionalProperties should carry the inner record shape
    let ap = schema
        .get("additionalProperties")
        .expect("should insert additionalProperties");

    assert_eq!(ap["type"], "object");
    assert_eq!(ap["properties"]["language"], json!({ "type": "string" }));
    assert_eq!(ap["properties"]["value"], json!({ "type": "string" }));
}

#[test]
fn test_map_of_strings_not_promoted_to_records() {
    let schema = json!({
        "type": "object",
        "properties": {
            "labels": {
                "type": "object",
                "properties": {
                    "en": { "type": "string" },
                    "fr": { "type": "string" }
                },
                "required": ["en", "fr"]
            }
        },
        "required": ["labels"]
    });

    let mut sch = schema.clone();
    let cfg = SchemaInferenceConfig {
        map_threshold: 2,
        ..Default::default()
    };
    rewrite_objects(&mut sch, None, &cfg);

    assert_eq!(
        sch["properties"]["labels"]["additionalProperties"]["type"],
        "string"
    );
}

#[test]
fn test_rewrite_objects_respects_map_max_required_keys() {
    let mut schema = json!({
        "type": "object",
        "properties": {
            "field1": {"type": "string"},
            "field2": {"type": "string"}
        },
        "required": ["field1", "field2"]
    });

    let config = SchemaInferenceConfig {
        map_threshold: 2,
        map_max_required_keys: Some(1), // Max 1 required key for maps
        ..Default::default()
    };

    rewrite_objects(&mut schema, None, &config);

    // Should remain as record because 2 required keys > 1
    assert_eq!(schema["type"], "object");
    assert!(schema.get("properties").is_some());
    assert!(schema.get("additionalProperties").is_none());
}

#[test]
fn test_rewrite_objects_allows_map_with_few_required_keys() {
    let mut schema = json!({
        "type": "object",
        "properties": {
            "field1": {"type": "string"},
            "field2": {"type": "string"}
        },
        "required": ["field1"] // Only 1 required key
    });

    let config = SchemaInferenceConfig {
        map_threshold: 2,
        map_max_required_keys: Some(1), // Max 1 required key for maps
        ..Default::default()
    };

    rewrite_objects(&mut schema, None, &config);

    // Should become map because 1 required key ≤ 1
    assert_eq!(schema["type"], "object");
    assert!(schema.get("additionalProperties").is_some());
    assert!(schema.get("properties").is_none());
}

#[test]
fn test_rewrite_objects_none_max_required_keys_preserves_behavior() {
    let mut schema = json!({
        "type": "object",
        "properties": {
            "field1": {"type": "string"},
            "field2": {"type": "string"},
            "field3": {"type": "string"}
        },
        "required": ["field1", "field2", "field3"]
    });

    let config = SchemaInferenceConfig {
        map_threshold: 2,
        map_max_required_keys: None, // No gating
        ..Default::default()
    };

    rewrite_objects(&mut schema, None, &config);

    // Should become map because None means no gating (old behavior)
    assert_eq!(schema["type"], "object");
    assert!(schema.get("additionalProperties").is_some());
    assert!(schema.get("properties").is_none());
}

#[test]
fn test_rewrite_objects_zero_max_required_keys() {
    let mut schema = json!({
        "type": "object",
        "properties": {
            "field1": {"type": "string"},
            "field2": {"type": "string"}
        },
        "required": ["field1"]
    });

    let config = SchemaInferenceConfig {
        map_threshold: 2,
        map_max_required_keys: Some(0), // Only allow maps with 0 required keys
        ..Default::default()
    };

    rewrite_objects(&mut schema, None, &config);

    // Should remain as record because 1 required key > 0
    assert_eq!(schema["type"], "object");
    assert!(schema.get("properties").is_some());
    assert!(schema.get("additionalProperties").is_none());
}

#[test]
fn test_rewrite_objects_zero_required_keys_allowed() {
    let mut schema = json!({
        "type": "object",
        "properties": {
            "field1": {"type": "string"},
            "field2": {"type": "string"}
        }
        // No required array = 0 required keys
    });

    let config = SchemaInferenceConfig {
        map_threshold: 2,
        map_max_required_keys: Some(0), // Only allow maps with 0 required keys
        ..Default::default()
    };

    rewrite_objects(&mut schema, None, &config);

    // Should become map because 0 required keys ≤ 0
    assert_eq!(schema["type"], "object");
    assert!(schema.get("additionalProperties").is_some());
    assert!(schema.get("properties").is_none());
}

#[test]
fn test_rewrite_objects_force_override_wins() {
    let mut schema = json!({
        "type": "object",
        "properties": {
            "field1": {"type": "string"},
            "field2": {"type": "string"}
        },
        "required": ["field1", "field2"]
    });

    let mut force_types = std::collections::HashMap::new();
    force_types.insert("test_field".to_string(), "map".to_string());

    let config = SchemaInferenceConfig {
        map_threshold: 2,
        map_max_required_keys: Some(0), // Would normally block this
        force_field_types: force_types,
        ..Default::default()
    };

    // Apply with field name that matches force override
    rewrite_objects(&mut schema, Some("test_field"), &config);

    // Should become map despite having required keys due to force override
    assert_eq!(schema["type"], "object");
    assert!(schema.get("additionalProperties").is_some());
    assert!(schema.get("properties").is_none());
}

#[test]
fn test_rewrite_objects_non_homogeneous_values_not_rewritten() {
    let mut schema = json!({
        "type": "object",
        "properties": {
            "field1": {"type": "string"},
            "field2": {"type": "integer"} // Non-homogeneous
        },
        "required": []
    });

    let config = SchemaInferenceConfig {
        map_threshold: 2,
        map_max_required_keys: Some(5), // Would allow this
        ..Default::default()
    };

    rewrite_objects(&mut schema, None, &config);

    // Should remain as record because values are not homogeneous
    assert_eq!(schema["type"], "object");
    assert!(schema.get("properties").is_some());
    assert!(schema.get("additionalProperties").is_none());
}

#[test]
fn test_rewrite_objects_below_threshold_not_rewritten() {
    let mut schema = json!({
        "type": "object",
        "properties": {
            "field1": {"type": "string"}
        },
        "required": []
    });

    let config = SchemaInferenceConfig {
        map_threshold: 5,               // Above the key count
        map_max_required_keys: Some(5), // Would allow this
        ..Default::default()
    };

    rewrite_objects(&mut schema, None, &config);

    // Should remain as record because below threshold
    assert_eq!(schema["type"], "object");
    assert!(schema.get("properties").is_some());
    assert!(schema.get("additionalProperties").is_none());
}

#[test]
fn test_map_max_required_keys_with_wrap_root() {
    let json_strings = vec![
        r#"{"en":{"language":"en","value":"Hello"},"fr":{"language":"fr","value":"Bonjour"}}"#
            .to_string(),
    ];

    let cfg = SchemaInferenceConfig {
        wrap_root: Some("labels".to_string()),
        map_threshold: 2,
        map_max_required_keys: Some(1), // Should allow the wrapped content
        ..Default::default()
    };

    let out = infer_json_schema_from_strings(&json_strings, cfg).unwrap();
    let sch = out.schema;

    assert_eq!(sch["type"], "object");
    assert_eq!(sch["required"], serde_json::json!(["labels"]));

    // The wrapped labels should have the map logic applied
    let labels_content = &sch["properties"]["labels"];
    assert!(labels_content.is_object());

    println!("✅ map_max_required_keys works with wrap_root");
}

#[test]
fn test_wrap_scalars_in_map_of_records() {
    let json_strings = vec![
        // "root" field is a map, all map keys have a record{id,value},
        // for map keys A/B value is a record{hello} and for map key C value is a string
        // Row 1: value is an object
        r#"{"root": {"A": {"id": 1, "value": {"hello": "world"}}}}"#.to_string(),
        // Row 2: value is an object, again
        r#"{"root": {"B": {"id": 2, "value": {"hello": "foo"}}}}"#.to_string(),
        // Row 3: value is just a string
        r#"{"root": {"C": {"id": 3, "value": "bar"}}}"#.to_string(),
    ];

    let cfg = SchemaInferenceConfig {
        map_threshold: 3,
        map_max_required_keys: Some(0),
        unify_maps: true,
        wrap_scalars: true,
        ..Default::default()
    };

    let result = infer_json_schema_from_strings(&json_strings, cfg)
        .expect("Schema inference should succeed with wrap_scalars");

    let sch = result.schema;

    // Navigate to root map’s value type
    let root_additional = &sch["properties"]["root"]["additionalProperties"];
    assert_eq!(root_additional["type"], "object");

    // The "value" field should be promoted and contain both object and scalar string form
    let value_schema = &root_additional["properties"]["value"];
    assert!(value_schema.is_object());

    // Should contain the synthetic key for the wrapped scalar
    assert!(
        value_schema["properties"]
            .as_object()
            .unwrap()
            .keys()
            .any(|k| k.contains("__string")),
        "Expected synthetic scalar wrapper key inside value schema, got: {}",
        serde_json::to_string_pretty(value_schema).unwrap()
    );

    println!(
        "✅ wrap_scalars promoted scalar inside map-of-records: {}",
        serde_json::to_string_pretty(value_schema).unwrap()
    );
}