Files
faunosteo/crates/core/tests/interior_probe.rs
T
bchanot 86dce16b09 fix(anatomy): V1-2.2 model arcs as browsers store them
Second security review: endpoints equal in f32 draw no arc, as in
browsers; an arc carries the spread of its f32 variants (endpoint or
radius moved one ulp) in its deviation and their extremes in its
bounds. Probe and mutants extended.
2026-11-01 13:04:43 +01:00

252 lines
8.4 KiB
Rust

//! The browser proof of the interior rule (owner brief « V1-2.2 : fermeture du dernier MEDIUM »,
//! 2026-11-01): `apps/app/e2e/fixtures/interior-probe.json`, read by
//! `apps/app/e2e/ui/interior-clicks.spec.ts` in Chromium, Firefox and WebKit. Each case is a
//! sheet whose main view, framed `0 0 286 310` (one unit per CSS px on the phone preview), holds
//! one shape: the canvas the editor gets (`view.canvas`), the verdict's code, and the widest disc
//! the core measures. The equine draft's shapes give their discs too. The file is generated
//! here and compared byte for byte; `MAKE_INTERIOR_PROBE=1` writes it.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use common::sheet_readers::{DETAIL_ASSET, MAIN_ASSET, OPEN, document, drawing};
use faunosteo_core::anatomy::interior::{
Budget, LAYER_FILL_RULE, Search, geometry_outline, widest_disc,
};
use faunosteo_core::domain::sheet::preview::{PreviewInput, PreviewSheet, PreviewStatus};
use faunosteo_core::domain::sheet::{AnatomicalSheetDocument, Annotation, Geometry};
use faunosteo_core::facade::call_with_preview;
use serde_json::{Value, json};
const FIXTURE: &str = "apps/app/e2e/fixtures/interior-probe.json";
const THIN: &str = "annotation.geometry.interior_too_small";
const UNREADABLE: &str = "annotation.geometry.out_of_frame";
const TOO_SMALL: &str = "annotation.geometry.too_small";
const PHONE: &str = "0 0 286 310";
/// The widest disc of a shape, measured to a thousandth of a unit: centre and radius; none for
/// path data the core does not read.
fn widest(geometry: &Geometry) -> Value {
let Some(outline) = geometry_outline(geometry, 0.001) else {
return Value::Null;
};
let search = Search {
floor: 0.0,
enough: f64::INFINITY,
precision: 0.001,
};
match widest_disc(&outline, LAYER_FILL_RULE, &search, &mut Budget::default()) {
Some(disc) => json!({
"x": round(disc.centre.0),
"y": round(disc.centre.1),
"r": round(disc.radius),
}),
None => Value::Null,
}
}
fn round(value: f64) -> f64 {
(value * 1000.0).round() / 1000.0
}
/// One case: the canvas, the frame codes, the widest disc.
fn case(name: &str, geometry: Value, root: &str) -> Value {
let mut doc: Value = serde_json::from_str(&document()).unwrap();
doc["annotations"][0]["id"] = json!("shape");
doc["annotations"][0]["kind"] = json!("zone");
doc["annotations"][0]["geometry"] = geometry.clone();
let main = OPEN.replace(
"viewBox=\"0 0 10 10\"",
&format!("{root}viewBox=\"{PHONE}\""),
) + "<path d=\"M0 0h1\"/></svg>";
let detail = OPEN.to_owned() + "<path d=\"M0 0h1\"/></svg>";
let input = PreviewInput {
status: PreviewStatus::Draft,
document: doc.to_string(),
fingerprint: None,
};
let preview = PreviewSheet::load(
input,
vec![drawing(MAIN_ASSET, &main), drawing(DETAIL_ASSET, &detail)],
)
.unwrap();
let codes: Vec<String> = preview
.state()
.unwrap()
.report
.issues
.iter()
.filter(|i| i.code.starts_with("annotation.geometry."))
.map(|i| i.code.clone())
.collect();
let canvas: Value = serde_json::from_str(&call_with_preview(
&preview,
"view.canvas",
&json!({ "viewId": "main" }).to_string(),
))
.unwrap();
assert_eq!(canvas["ok"], json!(true), "{name}: {canvas}");
let typed: Geometry = serde_json::from_value(geometry).unwrap();
json!({
"name": name,
"accepted": codes.is_empty(),
"codes": codes,
"widest": widest(&typed),
"svg": canvas["value"]["svg"],
})
}
fn polygon(points: &[[f64; 2]]) -> Value {
json!({ "type": "polygon", "points": points })
}
fn path(d: &str) -> Value {
json!({ "type": "path", "d": d })
}
fn ell(x: f64, y: f64, w: f64) -> Value {
polygon(&[
[x, y],
[x + 40.0, y],
[x + 40.0, y + w],
[x + w, y + w],
[x + w, y + 40.0],
[x, y + 40.0],
])
}
fn star(cx: f64, cy: f64, r: f64) -> Value {
let points: Vec<[f64; 2]> = (0..5)
.map(|i| {
let angle =
-std::f64::consts::FRAC_PI_2 + f64::from(i * 2) * std::f64::consts::TAU / 5.0;
[cx + r * angle.cos(), cy + r * angle.sin()]
})
.collect();
polygon(&points)
}
fn cases() -> Vec<Value> {
let plain = "";
vec![
// Accepted: small shapes that fill.
case(
"square-6",
polygon(&[[40.0, 40.0], [46.0, 40.0], [46.0, 46.0], [40.0, 46.0]]),
plain,
),
case(
"flat-triangle",
polygon(&[[40.0, 40.0], [100.0, 40.0], [70.0, 44.6]]),
plain,
),
case(
"arc-disc-6",
path("M40 50a3 3 0 1 0 6 0a3 3 0 1 0 -6 0Z"),
plain,
),
case(
"cubic-lens",
path("M40 50C50 44 70 44 80 50C70 56 50 56 40 50Z"),
plain,
),
case("ell-3.6", ell(40.0, 40.0, 3.6), plain),
// Winding 2 at the centre: filled by the layer's nonzero rule, whatever the drawing says.
case(
"pentagram-evenodd-root",
star(60.0, 60.0, 10.0),
"fill-rule=\"evenodd\" ",
),
// Refused: no interior a tap reaches on the phone.
case("forth-and-back", path("M40 40L90 90L40 40"), plain),
case(
"square-twice-opposite",
path("M40 40h40v40h-40zM40 40v40h40v-40z"),
plain,
),
case(
"diagonal-sliver",
polygon(&[[40.0, 40.0], [90.0, 90.0], [90.0, 88.6]]),
plain,
),
case(
"cubic-there-and-back",
path("M40 80C50 40 80 40 90 80C80 40 50 40 40 80Z"),
plain,
),
case(
"arc-there-and-back",
path("M40 80A25 25 0 0 1 90 80A25 25 0 0 0 40 80Z"),
plain,
),
case(
"flat-ellipse-45",
path("M40 40A35 1 45 0 1 90 90A35 1 45 0 1 40 40Z"),
plain,
),
case("ell-3.3", ell(40.0, 40.0, 3.3), plain),
case(
"triangle-6.6",
polygon(&[[40.0, 45.716], [46.6, 45.716], [43.3, 40.0]]),
plain,
),
// A radius browsers read as zero (`f32`), where `f64` would scale it to a half disc.
case(
"arc-radius-1e-49",
path("M40 50A1e-49 1e-49 0 0 1 90 50Z"),
plain,
),
// Endpoints one `f32` point: browsers draw no arc, `f64` would draw a full circle.
case(
"arc-endpoints-equal-in-f32",
path("M40 50A20 20 0 1 1 40.000001 50Z"),
plain,
),
]
}
/// The equine draft's polygons and paths: their widest disc, in the frame's units.
fn equine() -> Vec<Value> {
let text = common::repo_file("supabase/seed/anatomical-sheets/equine/draft/document.json");
let doc: AnatomicalSheetDocument = serde_json::from_str(&text).unwrap();
doc.annotations
.iter()
.filter_map(|a| {
let geometry = match a {
Annotation::Observable(o) => o.geometry.as_ref(),
Annotation::DetailTrigger(t) => t.geometry.as_ref(),
}?;
if matches!(geometry, Geometry::Circle(_)) {
return None;
}
Some(json!({ "id": a.id(), "view": a.view_id(), "widest": widest(geometry) }))
})
.collect()
}
#[test]
fn the_interior_probe_is_current() {
let cases = cases();
for c in &cases {
let refused = [THIN, UNREADABLE, TOO_SMALL]
.iter()
.any(|code| c["codes"] == json!([code]));
assert!(c["accepted"] == json!(true) || refused, "{}", c["name"]);
}
let probe = json!({
"generatedBy": "crates/core/tests/interior_probe.rs",
"frame": { "width": 286, "height": 310 },
"cases": cases,
"equine": equine(),
});
let text = serde_json::to_string_pretty(&probe).unwrap() + "\n";
let path = format!("{}/../../{FIXTURE}", env!("CARGO_MANIFEST_DIR"));
if std::env::var("MAKE_INTERIOR_PROBE").is_ok_and(|v| v == "1") {
std::fs::write(&path, &text).unwrap();
}
assert!(
common::repo_file(FIXTURE) == text,
"{FIXTURE} is stale: MAKE_INTERIOR_PROBE=1 cargo test --test interior_probe"
);
}