use alef::core::config::NewAlefConfig;
use alef::e2e::codegen::E2eCodegen;
use alef::e2e::codegen::go::GoCodegen;
use alef::e2e::fixture::{Assertion, Fixture, FixtureGroup};
fn build_config() -> NewAlefConfig {
let toml_src = r#"
[workspace]
languages = ["go"]
[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]
[crates.e2e]
fixtures = "fixtures"
output = "e2e"
languages = ["go"]
result_fields = ["action_results", "final_url", "metrics"]
fields_array = ["action_results"]
[crates.e2e.call]
function = "interact"
module = "github.com/example/testlib"
result_var = "result"
returns_result = true
args = [
{ name = "url", field = "url", type = "mock_url" },
]
"#;
toml::from_str(toml_src).expect("config parses")
}
fn build_fixture_group() -> FixtureGroup {
FixtureGroup {
category: "interaction".to_string(),
fixtures: vec![Fixture {
id: "namespaced_array_field".to_string(),
category: Some("interaction".to_string()),
description: "Namespace-prefixed array field assertion".to_string(),
input: serde_json::json!({ "url": "/page1" }),
assertions: vec![
Assertion {
assertion_type: "not_error".to_string(),
..Assertion::default()
},
Assertion {
assertion_type: "contains".to_string(),
field: Some("interaction.action_results".to_string()),
value: Some(serde_json::json!("click")),
..Assertion::default()
},
],
source: "test.json".to_string(),
..Fixture::default()
}],
}
}
fn generate_category_test() -> String {
let cfg = build_config();
let resolved = cfg.clone().resolve().expect("config resolves").remove(0);
let e2e = cfg.crates[0].e2e.clone().expect("e2e config present");
let files = GoCodegen
.generate(&[build_fixture_group()], &e2e, &resolved, &[], &[], &[], &[])
.expect("go generation succeeds");
files
.iter()
.find(|f| f.path.to_string_lossy().ends_with("interaction_test.go"))
.unwrap_or_else(|| {
panic!(
"interaction_test.go is emitted; got {:?}",
files.iter().map(|f| f.path.display().to_string()).collect::<Vec<_>>()
)
})
.content
.clone()
}
#[test]
fn namespace_prefixed_array_field_uses_the_slice_stringifier() {
let generated = generate_category_test();
assert!(
generated.contains("jsonString(result.ActionResults)"),
"a slice-valued contains must serialize the slice; got:\n{generated}"
);
}
#[test]
fn namespace_prefixed_array_field_does_not_emit_a_scalar_string_conversion() {
let generated = generate_category_test();
assert!(
!generated.contains("string(result.ActionResults)"),
"`string(...)` on a slice does not compile; got:\n{generated}"
);
}