Make Pheon structural review results self-contained

Capture revision metadata, cell presence and attributes, and structured
diagnostics in owned comparison results. Derive outcomes and render text,
JSON, and hierarchy reports without retaining source documents. Keep derived
descriptions and graph analysis in their renderers.

Use pheon-structural-v1 and PHEON_LAYER_NAMES_V1 consistently, expose helpers
through their defining modules, and remove obsolete re-exports. Update user
guides and report fixtures without changing schema version 1.

Cover dropped inputs, diagnostic ordering, absent and empty cells, offset
independence, and CLI failures. All 92 Rust tests pass in debug and release;
report and hierarchy goldens retain their behavior.
This commit is contained in:
Jan Petykiewicz 2026-11-16 20:45:06 -08:00
commit df62677f5c

View file

@ -1,7 +1,8 @@
use super::*;
use pheon::review::{
DisplayFilter, Scope,
Diagnostic, Outcome, Side,
report::{ReportInputs, ReportOptions, failure_report},
selection::{DisplayFilter, Scope},
};
use serde_json::{Value, json};
@ -15,7 +16,7 @@ fn inputs() -> ReportInputs {
fn report(old: &[u8], new: &[u8], options: &ReportOptions) -> Value {
let a = Document::parse(old).unwrap();
let b = Document::parse(new).unwrap();
Comparison::between(&a, &b).json_report(&a, &b, &inputs(), options)
Comparison::between(&a, &b).json_report(&inputs(), options)
}
fn unknown(geometry: Vec<u8>) -> Vec<u8> {
@ -26,12 +27,109 @@ fn unknown(geometry: Vec<u8>) -> Vec<u8> {
layout(&[("CHIP", vec![object])])
}
#[test]
fn comparison_reports_survive_dropped_documents_and_overwritten_inputs() {
let comparison = {
let mut old_bytes = shared(square());
let mut new_bytes = shared(triangle());
let old = Document::parse(&old_bytes).unwrap();
let new = Document::parse(&new_bytes).unwrap();
let comparison = Comparison::between(&old, &new);
drop(old);
drop(new);
old_bytes.fill(0);
new_bytes.fill(0);
comparison
};
assert_eq!(comparison.outcome(), Outcome::Different);
assert!(comparison.old.present && comparison.new.present);
assert_eq!(comparison.old.units.as_ref().unwrap().len(), 16);
let mut actual = comparison.json_report(
&inputs(),
&ReportOptions {
hierarchy: true,
..Default::default()
},
);
actual["producer"]["version"] = json!("test");
let expected: Value =
serde_json::from_str(include_str!("../golden/report-geometry.json")).unwrap();
assert_eq!(actual, expected);
assert_eq!(
comparison.hierarchy(false, 8),
include_str!("../golden/review-hierarchy.txt")
);
assert_eq!(
comparison.hierarchy(true, 8),
include_str!("../golden/review-hierarchy.mmd")
);
// Display choices affect evidence, not the captured outcome or full graph.
let limited = comparison.json_report(
&inputs(),
&ReportOptions {
hierarchy: true,
max_objects: Some(0),
..Default::default()
},
);
assert_eq!(limited["hierarchy"], actual["hierarchy"]);
assert_eq!(limited["summary"], actual["summary"]);
assert_eq!(limited["exit_code"], 1);
assert!(comparison.text(0).contains("0 of 2 object changes shown"));
}
#[test]
fn comparison_diagnostics_capture_both_sides_and_scope_mismatch_once() {
let comparison = {
let bytes = unknown(square());
let old = Document::parse(&bytes)
.unwrap()
.selected(
&Scope {
cell: Some("CHIP".to_string()),
..Default::default()
},
false,
)
.unwrap();
let mut new = Document::parse(&bytes).unwrap();
let comparison = Comparison::between(&old, &new);
// Changes to source documents cannot alter an existing result.
new.scope = old.scope.clone();
new.diagnostics.clear();
comparison
};
assert_eq!(
comparison.outcome(),
Outcome::Incomplete { different: false }
);
assert_eq!(comparison.diagnostics.len(), 3);
assert_eq!(comparison.diagnostics[0].side, Some(Side::Old));
assert_eq!(comparison.diagnostics[1].side, Some(Side::New));
assert_eq!(comparison.diagnostics[2].code, "scope_mismatch");
assert_eq!(comparison.diagnostics[2].side, None);
let report = comparison.json_report(&inputs(), &ReportOptions::default());
assert_eq!(report["status"], "incomplete");
assert_eq!(report["result"], "unknown");
assert_eq!(report["exit_code"], 2);
assert_eq!(report["scope"]["cell"], "CHIP");
assert_eq!(report["diagnostics"].as_array().unwrap().len(), 3);
assert_eq!(report["diagnostics"][0]["side"], "old");
assert_eq!(report["diagnostics"][1]["side"], "new");
assert_eq!(report["diagnostics"][2]["code"], "scope_mismatch");
let text = comparison.text(0);
let scope = text.find("INCOMPLETE: comparison scopes differ").unwrap();
let old = text.find("INCOMPLETE: old:").unwrap();
let new = text.find("INCOMPLETE: new:").unwrap();
assert!(scope < old && old < new);
}
#[test]
fn reports_preserve_fixture_results_totals_and_replacement_links() {
for pair in corpus() {
let r = report(&pair.old, &pair.new, &ReportOptions::default());
assert_eq!(r["schema_version"], 1);
assert_eq!(r["comparison_profile"], "klamath-object-v2");
assert_eq!(r["comparison_profile"], "pheon-structural-v1");
assert_eq!(r["producer"]["version"], env!("CARGO_PKG_VERSION"));
assert_eq!(r["status"], "complete", "{}", pair.name);
assert_eq!(
@ -110,12 +208,12 @@ fn future_layer_metadata_remains_incomplete_under_geometry_scopes() {
let bytes = if location == "CHIP" {
layout(&[(
"CHIP",
vec![square(), label((900, 7), "KLAMATH_LAYER_NAMES_V2")],
vec![square(), label((900, 7), "PHEON_LAYER_NAMES_V2")],
)])
} else {
layout(&[
("CHIP", vec![square()]),
("legend", vec![label((900, 7), "KLAMATH_LAYER_NAMES_V2")]),
("legend", vec![label((900, 7), "PHEON_LAYER_NAMES_V2")]),
])
};
let source = Document::parse(&bytes).unwrap().diagnostics[0].offset;
@ -159,10 +257,10 @@ fn future_layer_metadata_remains_incomplete_under_geometry_scopes() {
assert_eq!(diagnostic.layer, Some((900, 7)));
assert_eq!(diagnostic.offset, source);
let diff = Comparison::between(&doc, &doc);
assert_eq!(diff.exit_code(), 2);
assert!(!diff.is_equal());
assert_eq!(diff.outcome().exit_code(), 2);
assert!(!diff.outcome().is_equal());
assert!(diff.text(0).contains("INCOMPLETE"));
let json = diff.json_report(&doc, &doc, &inputs(), &ReportOptions::default());
let json = diff.json_report(&inputs(), &ReportOptions::default());
assert_eq!(json["status"], "incomplete");
assert_eq!(json["result"], "unknown");
assert_eq!(json["exit_code"], 2);
@ -339,7 +437,7 @@ fn hierarchy_reports_shared_ancestors_and_symbolic_arrays() {
.unwrap()
.selected(&scope, false)
.unwrap();
let r = Comparison::between(&doc, &doc).json_report(&doc, &doc, &inputs(), &options);
let r = Comparison::between(&doc, &doc).json_report(&inputs(), &options);
assert_eq!(r["scope"]["cell"], "CHIP");
assert_eq!(
r["hierarchy"]["nodes"]
@ -369,6 +467,14 @@ fn library_attributes_empty_cells_units_and_absence_are_explicit() {
let r = report(&a, &b, &ReportOptions::default());
assert_eq!(r["cell_changes"][0]["before"], Value::Null);
assert_eq!(r["cell_changes"][0]["after"]["attributes"], json!([]));
assert_eq!(r["cell_changes"][0]["attributes_changed"], true);
let removed = report(&b, &a, &ReportOptions::default());
assert_eq!(
removed["cell_changes"][0]["before"]["attributes"],
json!([])
);
assert_eq!(removed["cell_changes"][0]["after"], Value::Null);
assert_eq!(removed["cell_changes"][0]["attributes_changed"], true);
let mut b = b;
let end = Document::parse(&b).unwrap().cells["empty"].end_offset;
let mut attribute = Vec::new();
@ -379,15 +485,11 @@ fn library_attributes_empty_cells_units_and_absence_are_explicit() {
r["cell_changes"][0]["after"]["attributes"][0]["value"],
json!([1])
);
assert_eq!(r["cell_changes"][0]["attributes_changed"], true);
assert_eq!(r["inputs"]["old"]["units"]["encoding"], "real8");
let doc = Document::parse(&a).unwrap();
let absent = doc.empty_like();
let r = Comparison::between(&absent, &doc).json_report(
&absent,
&doc,
&inputs(),
&ReportOptions::default(),
);
let r = Comparison::between(&absent, &doc).json_report(&inputs(), &ReportOptions::default());
assert_eq!(r["inputs"]["old"]["present"], false);
assert_eq!(r["inputs"]["old"]["units"], Value::Null);
assert_eq!(
@ -401,11 +503,8 @@ fn library_attributes_empty_cells_units_and_absence_are_explicit() {
.find(|r| r.tag == RTAG_UNITS)
.unwrap()
.offset;
b[units + 12..units + 20].copy_from_slice(
&pheon::basic::encode_real8(2e-9)
.unwrap()
.to_be_bytes(),
);
b[units + 12..units + 20]
.copy_from_slice(&pheon::basic::encode_real8(2e-9).unwrap().to_be_bytes());
let r = report(&a, &b, &ReportOptions::default());
assert_eq!(r["library_changes"][0]["tag"], RTAG_UNITS);
assert_ne!(r["inputs"]["old"]["units"], r["inputs"]["new"]["units"]);
@ -530,9 +629,7 @@ fn machine_reports_match_versioned_goldens() {
&inputs(),
&Scope::default(),
&ReportOptions::default(),
"invalid_input",
"truncated record",
Some("new"),
&Diagnostic::new("invalid_input", "truncated record", Some(Side::New)),
),
),
];
@ -580,6 +677,24 @@ fn cli_emits_json_for_success_input_errors_scope_errors_and_argument_errors() {
);
report
};
let check_text_failure = |report: &Value, extra: &[&str]| {
let output = Command::new(env!("CARGO_BIN_EXE_pheon"))
.arg("diff")
.arg(&a)
.arg(&b)
.args(extra)
.output()
.unwrap();
assert_eq!(output.status.code(), Some(2));
assert!(output.stdout.is_empty());
assert_eq!(
String::from_utf8(output.stderr).unwrap(),
format!(
"pheon: {}\n",
report["diagnostics"][0]["message"].as_str().unwrap()
)
);
};
let full = command(&["--hierarchy", "graph"]);
assert_eq!(full["result"], "different");
assert!(full["hierarchy"].is_object());
@ -597,14 +712,35 @@ fn cli_emits_json_for_success_input_errors_scope_errors_and_argument_errors() {
assert_eq!(failure["status"], "failed");
assert_eq!(failure["result"], "unknown");
assert_eq!(failure["summary"], Value::Null);
if extra.first() == Some(&"--top") {
assert_eq!(failure["diagnostics"][0]["code"], "invalid_scope");
assert_eq!(failure["diagnostics"][0]["side"], Value::Null);
check_text_failure(&failure, extra);
}
}
fs::write(&b, b"invalid").unwrap();
let invalid = command(&[]);
assert_eq!(invalid["diagnostics"][0]["code"], "invalid_input");
assert_eq!(invalid["diagnostics"][0]["side"], "new");
assert_eq!(invalid["inputs"]["new"]["path"], b.to_str().unwrap());
check_text_failure(&invalid, &[]);
fs::write(&a, b"invalid").unwrap();
let invalid = command(&[]);
assert_eq!(invalid["diagnostics"][0]["code"], "invalid_input");
assert_eq!(invalid["diagnostics"][0]["side"], "old");
check_text_failure(&invalid, &[]);
fs::write(&a, shared(square())).unwrap();
fs::remove_file(&b).unwrap();
assert_eq!(command(&[])["diagnostics"][0]["code"], "io_error");
let missing = command(&[]);
assert_eq!(missing["diagnostics"][0]["code"], "io_error");
assert_eq!(missing["diagnostics"][0]["side"], "new");
check_text_failure(&missing, &[]);
fs::remove_file(&a).unwrap();
let missing = command(&[]);
assert_eq!(missing["diagnostics"][0]["code"], "io_error");
assert_eq!(missing["diagnostics"][0]["side"], "old");
check_text_failure(&missing, &[]);
fs::write(&a, shared(square())).unwrap();
fs::write(&b, fs::read(&a).unwrap()).unwrap();
assert_eq!(command(&[])["result"], "equal");
fs::write(&b, unknown(square())).unwrap();
@ -613,7 +749,7 @@ fn cli_emits_json_for_success_input_errors_scope_errors_and_argument_errors() {
&b,
layout(&[
("CHIP", vec![square()]),
("legend", vec![label((900, 7), "KLAMATH_LAYER_NAMES_V2")]),
("legend", vec![label((900, 7), "PHEON_LAYER_NAMES_V2")]),
]),
)
.unwrap();