Files
faunosteo/crates/core/tests/characterisation_serde_tolerance.rs

131 lines
4.4 KiB
Rust

//! The serde tolerance of the R2 report contract (RUST.md §11.16): serde's derives read a struct
//! from a positional array and a unit enum from a one-key map, where the frozen Zod reader
//! refused both. Owner go 2026-10-24: the three v3 shapes found are refused at the readers'
//! entry (`check_v3_shape`), with a controlled `ReadFailure::Invalid`; the canonical forms and
//! the non-strict unknown fields are read as before (RUST.md §11.18).
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use faunosteo_core::domain::archive::parse_envelope;
use faunosteo_core::domain::report::read::check_v3_shape;
use faunosteo_core::domain::report::{ReadFailure, ReportMigrationMap, read_report_document};
use serde_json::{Value, json};
fn demo() -> Value {
serde_json::from_str(&common::repo_file(
"apps/app/tests/fixtures/report-v3.demo.json",
))
.unwrap()
}
fn map() -> ReportMigrationMap {
let (source, _) = common::zod_oracle("migration-map.draft.json");
serde_json::from_str(&source).unwrap()
}
fn read(value: &Value) -> Result<(), ReadFailure> {
read_report_document(value, &map()).map(|_| ())
}
fn sheet_as_array() -> Value {
let mut report = demo();
let parts = [
report["sheet"]["sheetId"].clone(),
Value::Null,
report["sheet"]["documentSha256"].clone(),
];
report["sheet"] = json!(parts);
report
}
fn practitioner_as_array() -> Value {
let mut report = demo();
let p = report["practitioner"].clone();
report["practitioner"] = json!([p["name"], p["cabinet"], p["contact"], p["siret"]]);
report
}
fn severity_as_map() -> Value {
let mut report = demo();
report["observations"][0]["severity"] = json!({ "mild": null });
report
}
fn refused_with(value: &Value, what: &str) {
match read(value) {
Err(ReadFailure::Invalid(message)) => assert!(message.starts_with(what), "{message}"),
other => panic!("{what}: {other:?}"),
}
}
#[test]
fn the_canonical_report_is_read() {
assert!(read(&demo()).is_ok());
assert!(check_v3_shape(&demo()).is_ok());
}
#[test]
fn a_struct_written_as_a_positional_array_is_refused() {
refused_with(&sheet_as_array(), "sheet: expected an object");
refused_with(&practitioner_as_array(), "practitioner: expected an object");
}
#[test]
fn a_unit_enum_written_as_a_one_key_map_is_refused() {
refused_with(
&severity_as_map(),
"observations[0].severity: expected a string",
);
}
#[test]
fn an_absent_severity_and_an_unknown_field_are_read_as_before() {
let mut report = demo();
report["observations"][0]
.as_object_mut()
.unwrap()
.remove("severity");
report["extra"] = json!(1);
assert!(read(&report).is_ok());
// `severity` is optional, never nullable: null stays refused, as Zod refused it.
report["observations"][0]["severity"] = Value::Null;
refused_with(&report, "observations[0].severity: expected a string");
}
#[test]
fn the_envelope_entry_refuses_the_same_shapes() {
let envelope: Value = serde_json::from_str(&common::repo_file(
"apps/app/tests/fixtures/goldens/r2/envelope.demo.fr.nologos.json",
))
.unwrap();
assert!(parse_envelope(&envelope).is_ok());
for bad in [sheet_as_array(), practitioner_as_array(), severity_as_map()] {
let mut wrapped = envelope.clone();
wrapped["report"] = bad;
assert!(parse_envelope(&wrapped).is_err());
}
}
#[test]
fn a_positional_parent_does_not_carry_a_refused_shape_past_the_check() {
let mut report = demo();
let o = report["observations"][0].clone();
report["observations"][0] = json!([
o["id"], o["zones"], o["findings"], o["freeTextFindings"], { "mild": null }, o["comment"]
]);
refused_with(&report, "observations[0]: expected an object");
let envelope: Value = serde_json::from_str(&common::repo_file(
"apps/app/tests/fixtures/goldens/r2/envelope.demo.fr.nologos.json",
))
.unwrap();
let fields = envelope.as_object().unwrap();
let mut positional: Vec<Value> = fields.values().cloned().collect();
let at = fields.keys().position(|key| key == "report").unwrap();
positional[at] = sheet_as_array();
assert!(parse_envelope(&Value::Array(positional)).is_err());
let mut wrapped = envelope.clone();
wrapped["report"] = json!([1, 2, 3]);
assert!(parse_envelope(&wrapped).is_err());
}