use super::*;
use crate::core::config::e2e::CallConfig;
use crate::core::ir::{FieldDef, FunctionDef, TypeDef, TypeRef};
fn docs_fixture(shows: &[&str]) -> Fixture {
serde_json::from_value(serde_json::json!({
"id": "sample_fixture",
"description": "Sample fixture",
"input": {"source": "irrelevant"},
"docs": {
"topic": "guides",
"stem": "sample_fixture",
"shows": shows,
}
}))
.expect("fixture must parse")
}
fn config() -> E2eConfig {
E2eConfig {
call: CallConfig {
function: "process".into(),
result_var: "result".into(),
..CallConfig::default()
},
result_fields: ["data".to_string()].into_iter().collect(),
..E2eConfig::default()
}
}
fn field(name: &str, ty: TypeRef, optional: bool) -> FieldDef {
FieldDef {
name: name.to_string(),
ty,
optional,
..FieldDef::default()
}
}
fn process_returning(type_name: &str) -> Vec<FunctionDef> {
vec![FunctionDef {
name: "process".to_string(),
return_type: TypeRef::Named(type_name.to_string()),
..FunctionDef::default()
}]
}
fn type_defs() -> Vec<TypeDef> {
vec![
TypeDef {
name: "ProcessResult".to_string(),
fields: vec![field("data", TypeRef::Named("DataNode".to_string()), true)],
..TypeDef::default()
},
TypeDef {
name: "DataNode".to_string(),
fields: vec![
field("kind", TypeRef::String, false),
field(
"children",
TypeRef::Vec(Box::new(TypeRef::Named("DataNode".to_string()))),
false,
),
],
..TypeDef::default()
},
]
}
fn shown_expressions(language: &str, shows: &[&str]) -> Vec<String> {
resolve(
&docs_fixture(shows),
&config(),
language,
&type_defs(),
&process_returning("ProcessResult"),
)
.into_iter()
.map(|operation| operation.expression)
.collect()
}
#[test]
fn wasm_shows_the_bare_optional_field_unguarded() {
assert_eq!(shown_expressions("wasm", &["data"]), vec!["result.data".to_string()]);
}
#[test]
fn wasm_chains_through_the_ir_only_optional_field() {
assert_eq!(
shown_expressions("wasm", &["data.kind"]),
vec!["result.data?.kind".to_string()],
);
assert_eq!(
shown_expressions("wasm", &["data.children"]),
vec!["result.data?.children".to_string()],
);
}
#[test]
fn node_and_wasm_agree_on_the_ir_only_optional_field() {
assert_eq!(
shown_expressions("node", &["data.kind"]),
shown_expressions("wasm", &["data.kind"]),
);
}
#[test]
fn wasm_does_not_guard_a_required_nested_field() {
let type_defs = vec![
TypeDef {
name: "ProcessResult".to_string(),
fields: vec![field("data", TypeRef::Named("DataNode".to_string()), false)],
..TypeDef::default()
},
TypeDef {
name: "DataNode".to_string(),
fields: vec![field("kind", TypeRef::String, false)],
..TypeDef::default()
},
];
let expressions: Vec<String> = resolve(
&docs_fixture(&["data.kind"]),
&config(),
"wasm",
&type_defs,
&process_returning("ProcessResult"),
)
.into_iter()
.map(|operation| operation.expression)
.collect();
assert_eq!(expressions, vec!["result.data.kind".to_string()]);
}