use super::*;
use serde_json::json;
use crate::infer_json_schema_from_strings;
#[test]
fn test_scalar_unification_ndjson_mixed_nullable_formats() {
let ndjson_input = r#"
{"colors": {"a": {"hex": {"r": "ff"}}, "b": {"hex": {"g": "00"}}}}
{"colors": {"c": {"hex": {"b": "cc"}}, "a": {"hex": {"g": "aa"}}}}
{"colors": {"b": {"rgb": 255}}}
"#;
let config = SchemaInferenceConfig {
delimiter: Some(b'\n'),
map_threshold: 1,
unify_maps: true,
debug: true,
..Default::default()
};
let result = infer_json_schema_from_strings(&[ndjson_input.to_string()], config)
.expect("Should handle NDJSON with nested maps triggering scalar unification");
let colors_schema = &result.schema["properties"]["colors"];
assert!(colors_schema.get("additionalProperties").is_some());
let color_record = &colors_schema["additionalProperties"];
let hex_schema = &color_record["properties"]["hex"];
assert!(hex_schema.get("additionalProperties").is_some());
assert!(hex_schema.get("properties").is_none());
let hex_values = &hex_schema["additionalProperties"];
assert_eq!(hex_values["type"], json!(["null", "string"]));
}
#[test]
fn test_scalar_unification_with_old_nullable_format() {
let config = SchemaInferenceConfig {
map_threshold: 1,
unify_maps: true,
..Default::default()
};
let schemas = vec![
json!({"type": "string"}), json!({"type": ["null", "string"]}), json!(["null", {"type": ["null", "string"]}]), ];
let result = check_unifiable_schemas(&schemas, "test", &config);
assert!(result.is_some());
let unified = result.unwrap();
assert_eq!(unified["type"], json!(["null", "string"]));
}
#[test]
fn test_is_scalar_schema_with_mixed_formats() {
assert!(is_scalar_schema(&json!({"type": "string"})));
assert!(is_scalar_schema(&json!({"type": ["null", "string"]})));
assert!(is_scalar_schema(&json!(["null", {"type": "string"}])));
assert!(is_scalar_schema(&json!(["null", {"type": ["null", "string"]}])));
assert!(!is_scalar_schema(&json!({"type": "object", "properties": {}})));
assert!(!is_scalar_schema(&json!({"type": "array", "items": {}})));
}