use super::sample_urls::{only_snippet_content, url_e2e_config, url_fixture};
use super::*;
const HOSTED_SAMPLE_URL: &str = "https://samples.example.org";
fn url_fixture_named(id: &str) -> Fixture {
let mut fixture = url_fixture();
fixture.id = id.to_string();
fixture
}
fn url_fixture_hosted_at(id: &str, sample_url: &str) -> Fixture {
let mut fixture = url_fixture_named(id);
fixture.docs = Some(crate::e2e::fixture::FixtureDocs {
sample_url: Some(sample_url.to_string()),
..fixture.docs.expect("url_fixture always carries docs")
});
fixture
}
fn python_report(mock_only: bool, fixtures: &[Fixture]) -> Result<SnippetGenerationReport> {
let (e2e, crate_config) = url_e2e_config();
let snippet_config = SnippetConfig {
output: "docs/snippets".into(),
mock_only,
..SnippetConfig::default()
};
let context = SnippetRenderContext {
e2e: &e2e,
crate_config: &crate_config,
type_defs: &[],
enums: &[],
functions: &[],
errors: &[],
};
generate_snippet_report_with_extensions(fixtures, &["python".into()], &snippet_config, &context, &[])
}
#[test]
fn a_corpus_without_mock_only_warns_exactly_as_it_did_before() {
let report = python_report(false, &[url_fixture()]).expect("python snippet report renders");
assert_eq!(
report.placeholder_sample_url_fixtures,
vec!["extract_uri".to_string()],
"an unconfigured corpus must still name the fixture whose snippet cannot be run as published"
);
}
#[test]
fn a_mock_only_corpus_emits_no_reserved_domain_warning() {
let report = python_report(true, &[url_fixture()]).expect("python snippet report renders");
assert!(
report.placeholder_sample_url_fixtures.is_empty(),
"a corpus that declares itself mock-only has nothing left to configure, so there is \
nothing to report: {:?}",
report.placeholder_sample_url_fixtures
);
}
#[test]
fn a_mock_only_corpus_publishes_exactly_what_an_unconfigured_corpus_publishes() {
let mock_only = python_report(true, &[url_fixture()]).expect("python snippet report renders");
let unconfigured = python_report(false, &[url_fixture()]).expect("python snippet report renders");
assert_eq!(
only_snippet_content(&mock_only),
only_snippet_content(&unconfigured),
"declaring a corpus mock-only must not change a single byte of generated documentation"
);
assert!(
only_snippet_content(&mock_only).contains("https://example.com/pdf/report.pdf"),
"the illustrative reserved-domain address is still what a mock-only snippet shows:\n{}",
only_snippet_content(&mock_only)
);
}
#[test]
fn a_per_fixture_sample_url_under_mock_only_resolves_normally() {
let report = python_report(true, &[url_fixture_hosted_at("extract_uri", HOSTED_SAMPLE_URL)])
.expect("python snippet report renders");
let content = only_snippet_content(&report);
assert!(
content.contains("https://samples.example.org/pdf/report.pdf"),
"the fixture's own declared host must reach the published snippet, with its \
mock-relative path appended:\n{content}"
);
assert!(
!content.contains("example.com"),
"no trace of the reserved placeholder may survive a fixture-level declaration:\n{content}"
);
assert!(
report.placeholder_sample_url_fixtures.is_empty(),
"a fixture publishing a real address has nothing to warn about"
);
}
#[test]
fn a_broken_per_fixture_sample_url_is_still_warned_about_under_mock_only() {
let report = python_report(
true,
&[url_fixture_hosted_at("extract_uri", "https://example.com/hosted")],
)
.expect("python snippet report renders");
assert_eq!(
report.placeholder_sample_url_fixtures,
vec!["extract_uri".to_string()],
"a fixture that claims a public address and publishes the reserved documentation domain \
anyway is reporting a broken URL, not the absence of one -- mock_only must not cover it"
);
}
#[test]
fn mock_only_silences_the_fixture_that_inherited_it_and_not_the_one_that_claimed_a_url() {
let report = python_report(
true,
&[
url_fixture_named("inherits_mock_only"),
url_fixture_hosted_at("claims_a_broken_url", "https://example.com/hosted"),
],
)
.expect("python snippet report renders");
assert_eq!(
report.placeholder_sample_url_fixtures,
vec!["claims_a_broken_url".to_string()],
"exactly the fixture that claimed an address must be named: the other one has nothing \
to configure, and this one has something broken to fix"
);
}
#[test]
fn a_per_fixture_sample_url_overrides_a_configured_corpus_base_for_that_fixture_alone() {
let (e2e, crate_config) = url_e2e_config();
let snippet_config = SnippetConfig {
output: "docs/snippets".into(),
sample_base_url: Some("https://corpus.example.org".to_string()),
..SnippetConfig::default()
};
let context = SnippetRenderContext {
e2e: &e2e,
crate_config: &crate_config,
type_defs: &[],
enums: &[],
functions: &[],
errors: &[],
};
let fixtures = [
url_fixture_named("uses_corpus_base"),
url_fixture_hosted_at("uses_own_url", "https://its-own.example.org"),
];
let report = generate_snippet_report_with_extensions(&fixtures, &["python".into()], &snippet_config, &context, &[])
.expect("python snippet report renders");
let bodies: BTreeMap<&str, &str> = report
.snippets
.iter()
.map(|snippet| (snippet.fixture_id.as_str(), snippet.file.content.as_str()))
.collect();
assert!(
bodies["uses_corpus_base"].contains("https://corpus.example.org/pdf/report.pdf"),
"the undeclared fixture stays on the corpus base:\n{}",
bodies["uses_corpus_base"]
);
assert!(
bodies["uses_own_url"].contains("https://its-own.example.org/pdf/report.pdf"),
"the declaring fixture uses its own address:\n{}",
bodies["uses_own_url"]
);
assert!(
!bodies["uses_own_url"].contains("corpus.example.org"),
"the corpus base must not leak into a fixture that overrode it:\n{}",
bodies["uses_own_url"]
);
}
#[test]
fn mock_only_alongside_a_configured_sample_base_url_fails_the_run() {
let (e2e, crate_config) = url_e2e_config();
let snippet_config = SnippetConfig {
output: "docs/snippets".into(),
mock_only: true,
sample_base_url: Some(HOSTED_SAMPLE_URL.to_string()),
..SnippetConfig::default()
};
let context = SnippetRenderContext {
e2e: &e2e,
crate_config: &crate_config,
type_defs: &[],
enums: &[],
functions: &[],
errors: &[],
};
let error =
generate_snippet_report_with_extensions(&[url_fixture()], &["python".into()], &snippet_config, &context, &[])
.expect_err("two contradictory claims about one corpus cannot both be honoured");
let message = format!("{error:#}");
assert!(
message.contains("mock_only") && message.contains("sample_base_url"),
"the failure must name both halves of the contradiction: {message}"
);
}
#[test]
fn an_unusable_per_fixture_sample_url_fails_the_run_naming_the_fixture() {
let error = python_report(true, &[url_fixture_hosted_at("extract_uri", "samples.example.org")])
.expect_err("a scheme-less fixture address cannot form a public URL");
let message = format!("{error:#}");
assert!(
message.contains("extract_uri"),
"the failure must name the offending fixture: {message}"
);
assert!(
message.contains("docs.sample_url"),
"the failure must name the key its author actually wrote: {message}"
);
}