use std::path::{Path, PathBuf};
use rto_render::okf::conform;
fn fixture(bundle: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/okf-upstream")
.join(bundle)
}
fn scratch(tag: &str) -> PathBuf {
static SEQ: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let root = std::env::temp_dir().join(format!(
"rto-okf-conform-{}-{seq}-{tag}",
std::process::id()
));
let _ = std::fs::remove_dir_all(&root);
root
}
fn bundle(tag: &str, files: &[(&str, &str)]) -> PathBuf {
let root = scratch(tag);
for (rel, content) in files {
let path = root.join(rel);
std::fs::create_dir_all(path.parent().expect("a parent")).expect("create dir");
std::fs::write(&path, content).expect("write concept");
}
root
}
const ROOT_INDEX: &str = "---\nokf_version: \"0.2\"\n---\n\n# Bundle\n";
fn messages(report: &conform::CheckReport, code: Option<&str>) -> Vec<String> {
report
.findings
.iter()
.filter(|f| code.is_none_or(|c| f.code == Some(c)))
.map(|f| f.message.clone())
.collect()
}
#[test]
fn the_published_bundles_carry_no_conformance_error() {
for name in ["acme_retail", "ga4"] {
let report = conform::validate_report(&fixture(name)).expect(name);
assert!(
report.passed(),
"{name} must validate clean; errors: {:?}",
report
.findings
.iter()
.filter(|f| f.severity == "error")
.collect::<Vec<_>>()
);
assert!(
report.concepts > 0,
"and it must have looked at something: {report:?}"
);
}
}
#[test]
fn the_corpus_findings_match_what_upstream_reported() {
let acme = conform::validate_report(&fixture("acme_retail")).expect("acme_retail");
let found = messages(&acme, None);
assert_eq!(
found.iter().filter(|m| m.contains("deprecated")).count(),
2,
"two concepts link to the retired `gross-margin-legacy`: {found:?}"
);
assert_eq!(
found
.iter()
.filter(|m| m.contains("sql_equality.py"))
.count(),
2,
"the trimmed fixture is missing the attester script: {found:?}"
);
assert_eq!(found.len(), 4, "and nothing else: {found:?}");
let ga4 = conform::validate_report(&fixture("ga4")).expect("ga4");
assert!(ga4.passed(), "no errors: {:?}", messages(&ga4, None));
let stale: Vec<String> = messages(&ga4, None)
.into_iter()
.filter(|m| m.contains("no longer exists"))
.collect();
assert_eq!(stale.len(), 2, "{stale:?}");
assert!(
stale.iter().any(|m| m.contains("datasets/index.md"))
&& stale.iter().any(|m| m.contains("references/index.md")),
"{stale:?}"
);
assert!(
!stale.iter().any(|m| m.contains("tables/index.md")),
"`tables/index.md` is present, so reporting it would mean the rule \
cannot tell a missing listing from a live one: {stale:?}"
);
assert!(
messages(&ga4, None)
.iter()
.filter(|m| m.contains("which the bundle does not contain"))
.count()
> 0
);
assert!(
ga4.findings
.iter()
.filter(|f| f.message.contains("which the bundle does not contain"))
.all(|f| f.severity == "info"),
"§6: consumers MUST tolerate broken links: {:?}",
ga4.findings
);
let lint = conform::lint_report(&fixture("acme_retail")).expect("lint acme_retail");
assert_eq!(
lint.findings.len(),
26,
"upstream reports 26 hygiene findings on acme_retail"
);
assert!(
lint.passed(),
"hygiene never gates: nothing here is an error"
);
}
#[test]
fn one_deprecated_target_is_one_finding_however_many_links() {
let report = conform::validate_report(&fixture("acme_retail")).expect("acme_retail");
let from_gross_margin: Vec<_> = report
.findings
.iter()
.filter(|f| f.concept.as_deref() == Some("metrics/gross-margin"))
.collect();
assert_eq!(
from_gross_margin.len(),
1,
"two links, one retired target, one finding: {from_gross_margin:?}"
);
}
#[test]
fn a_heading_with_subheadings_under_it_is_not_empty() {
let report = conform::lint_report(&fixture("ga4")).expect("ga4");
let empty = messages(&report, Some("L4"));
assert!(
empty.is_empty(),
"no heading in ga4 is empty; these are containers: {empty:?}"
);
}
#[test]
fn a_bundle_without_okf_version_is_not_faulted_for_it() {
for name in ["acme_retail", "ga4"] {
let report = conform::validate_report(&fixture(name)).expect(name);
assert!(
!messages(&report, None)
.iter()
.any(|m| m.contains("okf_version")),
"{name}: §8 and §12 both say MAY: {:?}",
messages(&report, None)
);
}
}
#[test]
fn the_error_severities_can_actually_fire() {
let root = bundle(
"errors",
&[
("index.md", ROOT_INDEX),
(
"metrics/untyped.md",
"---\ntitle: Untyped\n---\n\n# Untyped\n",
),
(
"metrics/broken.md",
"---\ntype: [unclosed\n---\n\n# Broken\n",
),
],
);
let report = conform::validate_report(&root).expect("load");
assert!(!report.passed(), "{report:?}");
let errors: Vec<_> = report
.findings
.iter()
.filter(|f| f.severity == "error")
.map(|f| f.message.clone())
.collect();
assert!(
errors.iter().any(|m| m.contains("`type` is missing")),
"{errors:?}"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn circular_derivation_is_an_error_and_names_the_ring() {
let root = bundle(
"cycle",
&[
("index.md", ROOT_INDEX),
(
"metrics/a.md",
"---\ntype: Metric\ntitle: A\nsources:\n - { id: b, resource: /metrics/b.md }\n---\n\n# A\n",
),
(
"metrics/b.md",
"---\ntype: Metric\ntitle: B\nsources:\n - { id: a, resource: /metrics/a.md }\n---\n\n# B\n",
),
],
);
let report = conform::validate_report(&root).expect("load");
let cycles: Vec<_> = report
.findings
.iter()
.filter(|f| f.message.contains("circular derivation"))
.collect();
assert_eq!(cycles.len(), 1, "one ring, reported once: {report:?}");
assert_eq!(cycles[0].severity, "error");
assert!(
cycles[0].message.contains("metrics/a") && cycles[0].message.contains("metrics/b"),
"the ring's members are named, because \"there is a cycle\" is not actionable: {}",
cycles[0].message
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_stale_listing_and_an_unlisted_concept_are_both_found() {
let root = bundle(
"listings",
&[
("index.md", ROOT_INDEX),
(
"metrics/index.md",
"# Metrics\n\n* [Gone](gone.md) - a concept that was deleted.\n",
),
(
"metrics/here.md",
"---\ntype: Metric\ntitle: Here\n---\n\n# Here\n",
),
],
);
let validate = conform::validate_report(&root).expect("validate");
assert!(
messages(&validate, None)
.iter()
.any(|m| m.contains("index lists `gone.md`")),
"{:?}",
messages(&validate, None)
);
let lint = conform::lint_report(&root).expect("lint");
assert!(
messages(&lint, Some("L9"))
.iter()
.any(|m| m.contains("no `index.md` lists this concept")),
"`here.md` exists and nothing lists it: {:?}",
messages(&lint, Some("L9"))
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn portability_is_hygiene_under_our_own_code_not_conformance() {
let root = bundle(
"portability",
&[
("index.md", ROOT_INDEX),
(
"metrics/Not Portable.md",
"---\ntype: Metric\ntitle: NP\n---\n\n# NP\n",
),
],
);
let validate = conform::validate_report(&root).expect("validate");
assert!(
!messages(&validate, None)
.iter()
.any(|m| m.contains("portable")),
"the specification requires no such thing, so validate must not claim it does: {:?}",
messages(&validate, None)
);
let lint = conform::lint_report(&root).expect("lint");
let ours = messages(&lint, Some("R1"));
assert_eq!(ours.len(), 1, "{lint:?}");
assert!(
ours[0].contains("the specification does not forbid it"),
"the finding must own up to being ours: {}",
ours[0]
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn an_incomplete_computation_contract_is_reported_part_by_part() {
let root = bundle(
"computation",
&[
("index.md", ROOT_INDEX),
(
"computations/bare.md",
"---\ntype: Attested Computation\ntitle: Bare\n---\n\n# Bare\n\nNo contract at all.\n",
),
],
);
let report = conform::validate_report(&root).expect("load");
let found = messages(&report, None);
for expected in [
"`runtime` is missing",
"missing `executor`",
"missing `attester`",
] {
assert!(
found.iter().any(|m| m.contains(expected)),
"expected `{expected}` among {found:?}"
);
}
assert!(
report.passed(),
"each is a warning: an incomplete contract is still a readable document"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn staleness_is_never_judged_against_the_clock() {
let root = bundle(
"stale",
&[
("index.md", ROOT_INDEX),
(
"metrics/expired.md",
"---\ntype: Metric\ntitle: Expired\nstale_after: 2000-01-01T00:00:00Z\n---\n\n# Expired\n",
),
(
"metrics/malformed.md",
"---\ntype: Metric\ntitle: Malformed\nstale_after: yesterday\n---\n\n# Malformed\n",
),
],
);
let report = conform::validate_report(&root).expect("load");
let found = messages(&report, None);
assert!(
!found.iter().any(|m| m.contains("stale since")),
"a date long past is not a finding — only the clock could make it one: {found:?}"
);
assert!(
found
.iter()
.any(|m| m.contains("`stale_after` is not an ISO-8601 datetime")),
"but an unparseable one is, because that is a property of the text: {found:?}"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn an_empty_bundle_says_it_examined_nothing() {
let root = bundle("empty", &[("index.md", ROOT_INDEX)]);
let report = conform::validate_report(&root).expect("load");
assert_eq!(report.concepts, 0, "{report:?}");
assert!(report.findings.is_empty());
assert!(
report.passed(),
"nothing wrong, but `concepts` is what tells a reader nothing was looked at"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn findings_are_ordered_by_severity() {
let root = bundle(
"ordering",
&[
("index.md", ROOT_INDEX),
(
"metrics/untyped.md",
"---\ntitle: Untyped\n---\n\n# Untyped\n",
),
],
);
let report = conform::validate_report(&root).expect("load");
let rank: Vec<u8> = report
.findings
.iter()
.map(|f| match f.severity {
"error" => 0,
"warning" => 1,
_ => 2,
})
.collect();
assert!(
rank.windows(2).all(|w| w[0] <= w[1]),
"severity order is what a reader sees first: {:?}",
report
.findings
.iter()
.map(|f| (f.severity, &f.message))
.collect::<Vec<_>>()
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn validate_and_lint_are_different_checks() {
let acme = fixture("acme_retail");
let validate = conform::validate_report(&acme).expect("validate");
let lint = conform::lint_report(&acme).expect("lint");
assert_eq!(validate.check, "validate");
assert_eq!(lint.check, "lint");
assert_ne!(
validate.findings.len(),
lint.findings.len(),
"2 against 26 over this bundle; equal counts would suggest one entry point"
);
assert!(
lint.findings.iter().all(|f| f.code.is_some()),
"every hygiene finding carries its rule"
);
assert!(
validate.findings.iter().all(|f| f.code.is_none()),
"conformance findings carry none: they are the specification's rules, not ours"
);
}
#[test]
fn a_path_that_is_not_a_bundle_is_refused() {
let err = conform::validate_report(Path::new("/no/such/bundle")).expect_err("not a bundle");
assert!(
err.to_string().contains("/no/such/bundle"),
"the refusal names the path the caller gave: {err}"
);
}
#[test]
fn a_concept_that_derives_from_itself_is_a_cycle() {
let root = bundle(
"self-cycle",
&[
("index.md", ROOT_INDEX),
(
"metrics/ouroboros.md",
"---\ntype: Metric\ntitle: O\nsources:\n - { id: me, resource: /metrics/ouroboros.md }\n---\n\n# O\n",
),
],
);
let report = conform::validate_report(&root).expect("load");
let cycles: Vec<_> = report
.findings
.iter()
.filter(|f| f.message.contains("circular derivation"))
.collect();
assert_eq!(cycles.len(), 1, "{report:?}");
assert_eq!(cycles[0].severity, "error");
assert!(!report.passed());
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_resource_that_escapes_the_bundle_is_not_followed() {
let escapes = [
("../outside.md", "unix parent"),
("..\\outside.md", "windows parent"),
("a/../../outside.md", "climb through"),
("C:\\outside.md", "drive letter"),
("./here.md", "current-dir component"),
];
for (target, label) in escapes {
let root = bundle(
"escape",
&[
("index.md", ROOT_INDEX),
(
"metrics/m.md",
&format!("---\ntype: Metric\ntitle: M\nresource: {target}\n---\n\n# M\n"),
),
],
);
let report = conform::validate_report(&root).expect("load");
assert!(
!messages(&report, None)
.iter()
.any(|m| m.contains("which the bundle does not contain")),
"{label} (`{target}`) must not be resolved against the bundle root: {:?}",
messages(&report, None)
);
let _ = std::fs::remove_dir_all(&root);
}
let root = bundle(
"escape-control",
&[
("index.md", ROOT_INDEX),
(
"metrics/m.md",
"---\ntype: Metric\ntitle: M\nresource: /metrics/absent.md\n---\n\n# M\n",
),
],
);
let report = conform::validate_report(&root).expect("load");
assert!(
messages(&report, None)
.iter()
.any(|m| m.contains("which the bundle does not contain")),
"a real bundle-relative path is still checked: {:?}",
messages(&report, None)
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_bundle_with_no_concepts_can_still_fail() {
let root = bundle(
"unparseable",
&[
("index.md", ROOT_INDEX),
(
"metrics/broken.md",
"---\ntype: [unclosed\n---\n\n# Broken\n",
),
],
);
let report = conform::validate_report(&root).expect("load");
assert_eq!(report.concepts, 0, "nothing to examine: {report:?}");
assert!(
!report.passed(),
"but the unreadable root is an error, and `concepts == 0` must not hide it: {report:?}"
);
let _ = std::fs::remove_dir_all(&root);
}