fix(domain): refuse three non-canonical v3 shapes, R5.5 LOW fixes, R5.6 plan

check_v3_shape at read_report_document v3 and parse_envelope (sheet, practitioner
as non-objects, severity as non-string); Protocol errors final; DEV address must be
an own unicast address. SQL confirm function and DEV keys characterised, no change.
This commit is contained in:
bchanot committed 2026-10-24 03:34:38 +02:00
1 parent 13db5539a3
commit d502dfa923
11 files changed
+369 -37

No files matched your search

@@ -1,14 +1,15 @@
//! Characterisation, not a requirement (owner 2026-10-23, R5.5 brief §7): the serde derives of
//! the R2 contracts read a struct from a positional array and a unit enum from a one-key map,
//! where the frozen Zod reader refused both. These cases pin today's Rust verdict so that any
//! change to the gated readers is visible; they are to be turned around, not extended, if the
//! owner decides a strict deserialisation (`docs/development/RUST.md` §11.16).
//! 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::report::Report;
use serde::Deserialize;
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 {
@@ -18,41 +19,90 @@ fn demo() -> Value {
.unwrap()
}
fn read(value: &Value) -> Result<Report, serde_json::Error> {
Report::deserialize(value)
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_accepted_where_zod_refused() {
let mut sheet = demo();
let parts = [
sheet["sheet"]["sheetId"].clone(),
Value::Null,
sheet["sheet"]["documentSha256"].clone(),
];
sheet["sheet"] = json!(parts);
let mut practitioner = demo();
let p = practitioner["practitioner"].clone();
practitioner["practitioner"] = json!([p["name"], p["cabinet"], p["contact"], p["siret"]]);
assert!(read(&sheet).is_ok());
assert!(read(&practitioner).is_ok());
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_accepted_where_zod_refused() {
let mut report = demo();
report["observations"][0]["severity"] = json!({ "mild": null });
assert!(read(&report).is_ok());
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_unknown_field_is_accepted_as_the_non_strict_zod_objects_did() {
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());
}
}