use crate::e2e::codegen::field_skip::FieldSkip;
use crate::e2e::codegen::inert_example::{self, InertCause};
use crate::e2e::codegen::{fail_on_unavailable_field_markers, take_skip_records};
use crate::e2e::config::E2eConfig;
use crate::e2e::fixture::{Assertion, AssertionSkip, AssertionSkipDirective, AssertionSkipKind, Fixture, FixtureGroup};
use super::{ensure_capture_declared, snippets};
const UNWIRED_LANGUAGE: &str = "homebrew";
fn mock_total_fixture(id: &str) -> Fixture {
Fixture {
id: id.to_string(),
source: format!("fixtures/{id}.json"),
assertions: vec![Assertion {
assertion_type: "equals".to_string(),
field: Some("mock.requests.total".to_string()),
value: Some(serde_json::json!(3)),
..Default::default()
}],
..Default::default()
}
}
fn included_e2e_config() -> E2eConfig {
let mut config = E2eConfig::default();
config.call.function = "sample_call".to_string();
config
}
fn one_group(fixture: Fixture) -> [FixtureGroup; 1] {
[FixtureGroup {
category: "mock".to_string(),
fixtures: vec![fixture],
}]
}
#[test]
fn the_scenario_language_really_has_no_capture() {
assert_eq!(snippets::mock_capture(UNWIRED_LANGUAGE), Err(snippets::MockCaptureGap));
}
#[test]
fn a_backend_with_no_capture_cannot_silently_green_a_mock_assertion() {
let _ = take_skip_records();
let _ = inert_example::take_inert_examples();
let fixture = mock_total_fixture("mock_total_no_capture");
let body = format!(
" // skipped: {}\n",
FieldSkip::MockCaptureNotSupported.message("mock.requests.total")
);
assert!(
body.contains("requires a capture this backend does not emit"),
"got: {body}"
);
assert_eq!(
FieldSkip::extract_classified(&body),
Some(("mock.requests.total", FieldSkip::MockCaptureNotSupported))
);
fail_on_unavailable_field_markers(&body, UNWIRED_LANGUAGE, &fixture.id, &fixture.assertions);
let refusal = inert_example::inert_verdict(&body, UNWIRED_LANGUAGE, &fixture.id, &fixture.assertions)
.expect("an all-markers body must be refused");
assert_eq!(
refusal.cause,
InertCause::AwaitedOrLimited,
"a GeneratorGap marker must never be misclassified as RenderedNothing"
);
let groups = one_group(fixture.clone());
let error = ensure_capture_declared(&groups, &included_e2e_config(), UNWIRED_LANGUAGE)
.expect_err("generation must fail for an unacknowledged, uncaptured mock.* assertion");
let message = format!("{error:#}");
assert!(
message.contains(&fixture.id),
"error must name the fixture, got: {message}"
);
assert!(message.contains(UNWIRED_LANGUAGE), "got: {message}");
assert!(message.contains("mock.requests.total"), "got: {message}");
let _ = take_skip_records();
let _ = inert_example::take_inert_examples();
}
#[test]
fn the_assertion_type_funnel_does_not_recognise_the_mock_capture_wording() {
use crate::e2e::codegen::assertion_type_skip::AssertionTypeSkip;
let line = format!(
" // skipped: {}",
FieldSkip::MockCaptureNotSupported.message("mock.requests.total")
);
assert_eq!(AssertionTypeSkip::extract_classified(&line), None, "got: {line}");
}
#[test]
fn an_acknowledged_gap_does_not_fail_generation() {
let mut fixture = mock_total_fixture("mock_total_acknowledged");
fixture.assertions[0].skip = Some(AssertionSkip::Scoped(AssertionSkipDirective {
languages: vec![UNWIRED_LANGUAGE.to_string()],
kind: AssertionSkipKind::LanguageLimitation,
reason: Some("homebrew capture not wired yet".to_string()),
}));
let groups = one_group(fixture);
assert!(ensure_capture_declared(&groups, &included_e2e_config(), UNWIRED_LANGUAGE).is_ok());
}
#[test]
fn an_acknowledgement_for_another_language_does_not_excuse_this_one() {
let mut fixture = mock_total_fixture("mock_total_wrong_language_acknowledged");
fixture.assertions[0].skip = Some(AssertionSkip::Scoped(AssertionSkipDirective {
languages: vec!["kotlin".to_string()],
kind: AssertionSkipKind::LanguageLimitation,
reason: Some("kotlin capture not wired yet".to_string()),
}));
let groups = one_group(fixture);
assert!(ensure_capture_declared(&groups, &included_e2e_config(), UNWIRED_LANGUAGE).is_err());
}
#[test]
fn a_wired_language_never_needs_acknowledgement() {
let groups = one_group(mock_total_fixture("mock_total_wired"));
for language in ["rust", "python", "node", "go", "ruby", "php", "java"] {
assert!(ensure_capture_declared(&groups, &included_e2e_config(), language).is_ok());
}
}
#[test]
fn a_fixture_without_a_mock_assertion_never_trips_the_gate() {
let fixture = Fixture {
id: "unrelated_fixture".to_string(),
source: "fixtures/unrelated_fixture.json".to_string(),
assertions: vec![Assertion {
assertion_type: "equals".to_string(),
field: Some("metadata.title".to_string()),
value: Some(serde_json::json!("x")),
..Default::default()
}],
..Default::default()
};
let groups = one_group(fixture);
assert!(ensure_capture_declared(&groups, &included_e2e_config(), UNWIRED_LANGUAGE).is_ok());
}