use gram_codec::parse_to_ast;
use serde_json::json;
#[test]
fn test_json_format_matches_gram_hs() {
let input = "(alice:Person {name: \"Alice\", age: 30})";
let ast = parse_to_ast(input).expect("Failed to parse");
let json_output = serde_json::to_value(&ast).expect("Failed to serialize");
let expected = json!({
"subject": {
"identity": "alice",
"labels": ["Person"],
"properties": {
"name": "Alice",
"age": 30 }
},
"elements": []
});
assert_eq!(
json_output, expected,
"JSON format should match gram-hs canonical format"
);
}
#[test]
fn test_lowercase_type_discriminators() {
let input = r#"(node {range: 1..10, body: md`# Heading`})"#;
let ast = parse_to_ast(input).expect("Failed to parse");
let json_output = serde_json::to_value(&ast).expect("Failed to serialize");
let properties = &json_output["subject"]["properties"];
assert_eq!(properties["range"]["type"], "range");
assert!(properties["range"]["lower"].is_number());
assert!(properties["range"]["upper"].is_number());
assert_eq!(properties["range"]["lower"].as_f64().unwrap(), 1.0);
assert_eq!(properties["range"]["upper"].as_f64().unwrap(), 10.0);
assert_eq!(properties["body"]["type"], "tagged");
assert_eq!(properties["body"]["tag"], "md");
assert_eq!(properties["body"]["content"], "# Heading");
}
#[test]
fn test_native_json_numbers() {
let input = "(data {count: 42, temperature: 98.6})";
let ast = parse_to_ast(input).expect("Failed to parse");
let json_output = serde_json::to_value(&ast).expect("Failed to serialize");
let properties = &json_output["subject"]["properties"];
assert!(properties["count"].is_number());
assert_eq!(properties["count"], 42);
assert!(properties["temperature"].is_number());
assert_eq!(properties["temperature"], 98.6);
assert!(properties["count"].get("type").is_none());
assert!(properties["temperature"].get("type").is_none());
}
#[test]
fn test_field_names_match_gram_hs() {
let input = "(alice:Person)";
let ast = parse_to_ast(input).expect("Failed to parse");
let json_output = serde_json::to_value(&ast).expect("Failed to serialize");
assert!(json_output.get("subject").is_some());
assert!(json_output.get("elements").is_some());
let subject = &json_output["subject"];
assert!(subject.get("identity").is_some());
assert!(subject.get("labels").is_some());
assert!(subject.get("properties").is_some());
assert_eq!(subject["identity"], "alice");
}