use std::collections::{HashMap, HashSet};
use crate::e2e::codegen::c::assertions::{EffectiveConfigSource, FieldConfigSources};
use crate::e2e::field_access::FieldResolver;
use crate::e2e::fixture::{Assertion, Fixture};
fn resolver_with_a_real_result_field() -> FieldResolver {
FieldResolver::new(
&HashMap::new(),
&HashSet::new(),
&HashSet::from(["status".to_string()]),
&HashSet::new(),
&HashSet::new(),
)
}
fn global_sources() -> FieldConfigSources {
FieldConfigSources {
result_fields: EffectiveConfigSource::Global,
fields: EffectiveConfigSource::Global,
}
}
fn mock_assertion(field: &str, assertion_type: &str, value: serde_json::Value) -> Assertion {
Assertion {
assertion_type: assertion_type.to_string(),
field: Some(field.to_string()),
value: Some(value),
..Assertion::default()
}
}
#[test]
fn a_mock_only_fixture_still_takes_the_active_assertion_path() {
let fixture = Fixture {
id: "engine_factory_mock_capture".into(),
description: "asserts against the mock server request log".into(),
assertions: vec![mock_assertion("mock.requests.total", "equals", serde_json::json!(1))],
..Fixture::default()
};
let mut out = String::new();
super::render_engine_factory_test_function(
&mut out,
&fixture,
"sample",
"scrape",
"result",
&resolver_with_a_real_result_field(),
&HashMap::new(),
&HashSet::new(),
"CrawlResult",
"CrawlConfig",
false,
None,
&[],
&global_sources(),
)
.expect("engine-factory mock-only fixture renders");
assert!(
out.contains("assert(result != 0 && \"expected call to succeed\");"),
"a mock.*-only fixture must take the active-assertion path, not the soft null guard:\n{out}"
);
assert!(
out.contains(
"assert(alef_mock_request_count(getenv(\"MOCK_SERVER_URL\"), \"/__alef/requests/total?prefix=\") == 1 && \"equals assertion failed\");"
),
"expected the mock-count assertion to actually render:\n{out}"
);
}
#[test]
fn a_fixture_with_no_assertions_still_takes_the_soft_guard_path() {
let fixture = Fixture {
id: "engine_factory_no_assertions".into(),
description: "no assertions at all".into(),
assertions: vec![],
..Fixture::default()
};
let mut out = String::new();
super::render_engine_factory_test_function(
&mut out,
&fixture,
"sample",
"scrape",
"result",
&resolver_with_a_real_result_field(),
&HashMap::new(),
&HashSet::new(),
"CrawlResult",
"CrawlConfig",
false,
None,
&[],
&global_sources(),
)
.expect("engine-factory no-assertion fixture renders");
assert!(
!out.contains("expected call to succeed"),
"a fixture with no assertions at all must still take the soft null-guard branch:\n{out}"
);
assert!(
out.contains("if (result != 0) sample_crawl_result_free(result);"),
"expected the soft null-guard free, got:\n{out}"
);
}
#[test]
fn a_mock_assertion_survives_extraction_even_when_is_valid_for_result_is_permissive() {
let fixture = Fixture {
id: "engine_factory_mock_capture_permissive".into(),
description: "asserts against the mock server request log, no result_fields configured".into(),
assertions: vec![mock_assertion("mock.requests.total", "equals", serde_json::json!(1))],
..Fixture::default()
};
let mut out = String::new();
super::render_engine_factory_test_function(
&mut out,
&fixture,
"sample",
"scrape",
"result",
&FieldResolver::new(
&HashMap::new(),
&HashSet::new(),
&HashSet::new(),
&HashSet::new(),
&HashSet::new(),
),
&HashMap::new(),
&HashSet::new(),
"CrawlResult",
"CrawlConfig",
false,
None,
&[],
&global_sources(),
)
.expect("engine-factory mock-only fixture renders even with a permissive resolver");
assert!(
out.contains(
"assert(alef_mock_request_count(getenv(\"MOCK_SERVER_URL\"), \"/__alef/requests/total?prefix=\") == 1 && \"equals assertion failed\");"
),
"expected the mock-count assertion to actually render:\n{out}"
);
assert!(
!out.contains("_mock("),
"must not have emitted a bogus accessor call for the 'mock' segment, got:\n{out}"
);
}
#[test]
fn a_mock_assertion_on_a_bytes_returning_call_renders_the_real_comparison() {
let fixture = Fixture {
id: "bytes_mock_capture".into(),
description: "asserts against the mock server request log on a bytes-returning call".into(),
assertions: vec![mock_assertion(
"mock.requests.total",
"greater_than_or_equal",
serde_json::json!(1),
)],
..Fixture::default()
};
let mut out = String::new();
super::render_bytes_test_function(
&mut out,
&fixture,
"sample",
"speech",
"result",
&[],
"",
"SpeechResponse",
"default_client_new",
"Client",
false,
&[],
false,
);
assert!(
out.contains(
"assert(alef_mock_request_count(getenv(\"MOCK_SERVER_URL\"), \"/__alef/requests/total?prefix=\") >= 1 && \"expected greater than or equal\");"
),
"expected the real mock-count comparison, got:\n{out}"
);
assert!(
!out.contains("not meaningful on raw byte buffer"),
"a mock.* assertion must not be treated as an unsupported byte-buffer field, got:\n{out}"
);
}