//! V1-2.2: the server verdict (contract §6.4, §6.5, §6.7). A clean sheet and the equine draft
//! pass; each submission rule fails alone with its closed code; the caps hold at cap − 1 and
//! cap and fail at cap + 1 (labels in astral characters included); the shape is the one the
//! M5 SQL reads; the same input gives the same verdict; a synthetic document at the caps is
//! verified in time.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use faunosteo_core::domain::sheet::fingerprint::sheet_fingerprint;
use faunosteo_core::domain::sheet::verdict::{
DrawingResult, Verdict, VerdictBinding, VerdictDrawing, VerdictInput, VerdictOutcome, codes,
compute_verdict, sha256_hex,
};
use serde_json::{Value, json};
const ASSET: &str = "11111111-2222-4333-8444-555555555555";
/// A drawing in its stored form (`serialize::stored_form`), as the page's cleaning uploads it.
const DRAWING: &str = concat!(
"\n",
r#""#
);
const SEED: &str = "supabase/seed/anatomical-sheets/equine/draft";
const HORSE_ID: &str = "7f767f74-3b36-51a7-8465-52dd7e2ead4a";
fn binding() -> VerdictBinding {
VerdictBinding {
sheet_id: "aaaaaaaa-0000-4000-8000-000000000001".to_owned(),
version_id: "aaaaaaaa-0000-4000-8000-000000000002".to_owned(),
revision: 3,
review_id: "aaaaaaaa-0000-4000-8000-000000000003".to_owned(),
}
}
fn name(text: &str) -> Value {
json!({ "fr": text, "en": text })
}
/// A small valid document drawn on `ASSET`: one view, one point, one zone with a category
/// and a node, everything `to_confirm` (submittable, not yet approvable).
fn document() -> Value {
json!({
"schema_version": 2, "species_id": "canine", "name": name("Chien"),
"description": {}, "scope_disclaimer": { "fr": "Outil", "en": "Tool" },
"attribution": { "authors": ["A"], "license": null, "notes": null },
"vertebral_config": { "regions": [] }, "references": [],
"categories": [{ "id": "c", "name": name("C"), "color": null }],
"views": [{ "id": "main", "kind": "main", "name": name("Vue"), "svg_asset_id": ASSET, "view_box": null }],
"annotations": [{ "id": "a1", "kind": "point", "view_id": "main", "category_id": "c",
"laterality": "unpaired", "geometry": { "type": "circle", "cx": 1, "cy": 1, "r": 1 },
"review_status": "to_confirm", "name": name("P"), "finding_zone_id": "z1", "vertebral_region_code": null }],
"finding_zones": [{ "id": "z1", "name": name("Z"), "categories": [{ "id": "cat", "kind": "dysfunction",
"name": name("D"), "review_status": "to_confirm",
"children": [{ "id": "n1", "name": name("N"), "review_status": "to_confirm" }] }] }]
})
}
fn row(bytes: &[u8]) -> VerdictDrawing<'_> {
VerdictDrawing {
asset_id: ASSET,
sha256: Box::leak(sha256_hex(bytes).into_boxed_str()),
byte_size: bytes.len() as u64,
bytes: Some(bytes),
}
}
fn verdict_of(document: &Value, drawings: Vec>) -> Verdict {
let text = document.to_string();
compute_verdict(&VerdictInput {
binding: binding(),
document: &text,
base_document: None,
drawings,
})
}
fn verdict(document: &Value) -> Verdict {
verdict_of(document, vec![row(DRAWING.as_bytes())])
}
#[test]
fn a_clean_sheet_passes_bound_to_its_round() {
let doc = document();
let verdict = verdict(&doc);
assert_eq!(
verdict.outcome,
VerdictOutcome::Passed,
"{:?}",
verdict.failures
);
assert_eq!(
(verdict.failure_code.as_deref(), verdict.failures.len()),
(None, 0)
);
assert_eq!(
verdict.fingerprint,
Some(sheet_fingerprint(&doc.to_string()).unwrap())
);
let b = binding();
assert_eq!(
(
&verdict.sheet_id,
&verdict.version_id,
verdict.revision,
&verdict.review_id
),
(&b.sheet_id, &b.version_id, b.revision, &b.review_id)
);
assert_eq!(verdict.format, 1);
assert!(verdict.verified_with.starts_with("faunosteo-core "));
assert_eq!(verdict.drawings.len(), 1);
assert_eq!(verdict.drawings[0].result, DrawingResult::Ok);
let keys: Vec<&str> = verdict.groups.iter().map(|g| g.key.as_str()).collect();
assert_eq!(keys, ["general", "view:main", "zone:z1"]);
assert!(
verdict.groups[1..].iter().all(|g| !g.coverable),
"to_confirm left"
);
assert!(verdict.changes.is_none());
let completeness = verdict.completeness.unwrap();
assert_eq!(completeness.statuses.to_confirm, 3);
assert_eq!(completeness.uncited, 2);
}
#[test]
fn the_shape_is_what_the_sql_reads() {
let value = serde_json::to_value(verdict(&document())).unwrap();
assert!(value["fingerprint"].as_str().unwrap().len() == 64);
for group in value["groups"].as_array().unwrap() {
assert!(group["key"].is_string() && group["digest"].is_string());
assert!(group["coverable"].is_boolean());
}
let drawing = &value["drawings"][0];
assert_eq!(drawing["asset_id"], json!(ASSET));
assert_eq!(drawing["sha256"], json!(sha256_hex(DRAWING.as_bytes())));
assert_eq!(value["outcome"], json!("passed"));
assert_eq!(
value["verified_with"].as_str().map(|s| s.len() <= 100),
Some(true)
);
}
#[test]
fn the_equine_draft_passes_with_its_fingerprint_and_thirty_groups() {
let text = common::repo_file(&format!("{SEED}/document.json"));
let horse = std::fs::read(common::repo_path(&format!("{SEED}/assets/horse.svg"))).unwrap();
let verdict = compute_verdict(&VerdictInput {
binding: binding(),
document: &text,
base_document: None,
drawings: vec![VerdictDrawing {
asset_id: HORSE_ID,
sha256: "0ea5202fa3b95bac167b87c12ee969ab0272e085b60c2d34dbca6202c5f08541",
byte_size: 132_997,
bytes: Some(&horse),
}],
});
assert_eq!(
verdict.outcome,
VerdictOutcome::Passed,
"{:?}",
verdict.failures
);
assert_eq!(
verdict.fingerprint.as_deref(),
Some("12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2")
);
assert_eq!(verdict.groups.len(), 30);
assert_eq!(verdict.document_bytes, 184_105);
assert!(
verdict
.issues
.iter()
.all(|i| i.severity != faunosteo_core::domain::sheet::SheetIssueSeverity::Error)
);
}
fn failures(verdict: &Verdict) -> Vec<&str> {
verdict.failures.iter().map(String::as_str).collect()
}
#[test]
fn an_unreadable_document_or_base_fails_as_integrity() {
for text in ["{", "[]", r#"{"schema_version":2}"#] {
let verdict = compute_verdict(&VerdictInput {
binding: binding(),
document: text,
base_document: None,
drawings: vec![],
});
assert_eq!(failures(&verdict), [codes::DOCUMENT], "{text}");
assert!(verdict.fingerprint.is_none() && verdict.groups.is_empty());
}
let mut unknown = document();
unknown["extra"] = json!(1);
assert_eq!(
failures(&verdict(&unknown)),
[codes::DOCUMENT],
"strict shape"
);
let text = document().to_string();
let verdict = compute_verdict(&VerdictInput {
binding: binding(),
document: &text,
base_document: Some("{\"broken\""),
drawings: vec![row(DRAWING.as_bytes())],
});
assert_eq!(failures(&verdict), [codes::BASE]);
}
#[test]
fn each_submission_rule_fails_alone() {
let mut duplicate = document();
let copy = duplicate["annotations"][0].clone();
duplicate["annotations"].as_array_mut().unwrap().push(copy);
let v = verdict(&duplicate);
assert_eq!(failures(&v), [codes::RULES]);
assert!(
v.fingerprint.is_none(),
"no fingerprint with an integrity error"
);
assert!(v.issues.iter().any(|i| i.code == "annotation.id.duplicate"));
let mut undrawn = document();
undrawn["annotations"][0]["geometry"] = Value::Null;
let v = verdict(&undrawn);
assert_eq!(failures(&v), [codes::NO_GEOMETRY]);
assert!(
v.fingerprint.is_some(),
"a missing geometry is no integrity error"
);
assert!(v.issues.iter().any(|i| i.code == "annotation.geometry.missing" && i.path == "annotations[0].geometry"));
}
#[test]
fn each_drawing_fault_fails_with_its_code() {
let doc = document();
let bytes = DRAWING.as_bytes();
let base = row(bytes);
let cases: Vec<(Vec>, &str, DrawingResult)> = vec![
(vec![], codes::DRAWING_MISSING, DrawingResult::Missing),
(
vec![VerdictDrawing {
bytes: None,
..base
}],
codes::DRAWING_MISSING,
DrawingResult::Missing,
),
(
vec![VerdictDrawing {
byte_size: 1,
..base
}],
codes::SIZE_MISMATCH,
DrawingResult::SizeMismatch,
),
(
vec![VerdictDrawing {
sha256: "ab",
..base
}],
codes::OBJECT_CONFLICT,
DrawingResult::HashMismatch,
),
];
for (drawings, code, result) in cases {
let v = verdict_of(&doc, drawings);
assert_eq!(failures(&v), [code]);
assert_eq!(v.drawings[0].result, result);
assert!(v.fingerprint.is_some());
}
let invalid: &[u8] = b"";
let doctype = b"