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

170 lines
5.7 KiB
Rust

//! The Rust legacy importer against the TypeScript (R5.2): the committed `legacy-import/`
//! folder byte for byte, the intermediate values frozen in `oracles/r5/pipeline.json`, the
//! raw report against the pre-Prettier digest of the ledger, two runs byte-equal, and the
//! synthetic cases of `oracles/r5/legacy-synthetic.json` (the generic contract, refusals
//! included). Every expectation was written by the TypeScript pipeline.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use faunosteo_core::curation::ids::sha256_hex;
use faunosteo_core::curation::legacy::report::render_import_report;
use faunosteo_core::curation::legacy::{
EquineLegacyInput, ImportResult, SvgSource, import_equine_legacy, render_artifacts,
};
use faunosteo_core::json::to_js_compact;
use serde_json::Value;
const SOURCES: &str = "supabase/seed/anatomical-sheets/equine/legacy-sources";
const LEGACY: &str = "supabase/seed/anatomical-sheets/equine/legacy-import";
fn bytes(relative: &str) -> Vec<u8> {
std::fs::read(common::repo_path(relative)).unwrap()
}
fn equine_input() -> EquineLegacyInput {
EquineLegacyInput {
main_svg: SvgSource {
file_name: "Horse.svg".into(),
bytes: bytes(&format!("{SOURCES}/Horse.svg")),
},
head_svg: SvgSource {
file_name: "Horse_head.svg".into(),
bytes: bytes(&format!("{SOURCES}/Horse_head.svg")),
},
catalog: bytes(&format!("{SOURCES}/pathologies.json")),
vertebrae: bytes(&format!("{SOURCES}/vertebrae.ts")),
}
}
fn oracle(name: &str) -> Value {
serde_json::from_str(&common::repo_file(&format!(
"crates/core/tests/oracles/r5/{name}"
)))
.unwrap()
}
/// The parts of a result the TypeScript `ImportResult` holds besides the two documents.
fn parts(result: &ImportResult) -> Value {
let mut value = serde_json::to_value(result).unwrap();
let map = value.as_object_mut().unwrap();
map.remove("document");
map.remove("legacyIdMap");
value
}
fn assert_same(actual: &Value, expected: &Value, what: &str) {
assert_eq!(
to_js_compact(actual).unwrap(),
to_js_compact(expected).unwrap(),
"{what}"
);
}
#[test]
fn the_committed_legacy_import_folder_is_reproduced_byte_for_byte() {
let result = import_equine_legacy(&equine_input()).unwrap();
let files = render_artifacts(&result).unwrap();
let names: Vec<&str> = files.iter().map(|(name, _)| name.as_str()).collect();
assert_eq!(
names,
[
"document.json",
"legacy-id-map.json",
"assets.json",
"IMPORT-REPORT.md"
]
);
for (name, text) in &files {
assert!(
*text == common::repo_file(&format!("{LEGACY}/{name}")),
"{name} differs"
);
}
for asset in &result.assets {
let copy = bytes(&format!("{LEGACY}/assets/{}", asset.file));
assert_eq!(copy, bytes(&format!("{SOURCES}/{}", asset.file)));
assert_eq!(sha256_hex(&copy), asset.sha256);
}
}
#[test]
fn the_intermediate_values_match_the_frozen_typescript_run() {
let result = import_equine_legacy(&equine_input()).unwrap();
let frozen = oracle("pipeline.json");
let legacy = frozen["legacy"].as_object().unwrap();
let actual = parts(&result);
for (key, expected) in legacy {
assert_same(&actual[key], expected, key);
}
assert_eq!(result.sheet_id, legacy["sheetId"].as_str().unwrap());
}
#[test]
fn the_raw_report_is_the_text_prettier_received() {
let result = import_equine_legacy(&equine_input()).unwrap();
let raw = render_import_report(&result).unwrap();
let frozen = oracle("pipeline.json");
let row = frozen["artifacts"]
.as_array()
.unwrap()
.iter()
.find(|row| row["folder"] == LEGACY && row["file"] == "IMPORT-REPORT.md")
.unwrap();
assert_eq!(
sha256_hex(raw.as_bytes()),
row["before_prettier_sha256"].as_str().unwrap()
);
}
#[test]
fn two_runs_emit_the_same_bytes() {
let first = render_artifacts(&import_equine_legacy(&equine_input()).unwrap()).unwrap();
let second = render_artifacts(&import_equine_legacy(&equine_input()).unwrap()).unwrap();
assert_eq!(first, second);
}
fn synthetic_input(case: &Value) -> EquineLegacyInput {
let text = |key: &str| case[key].as_str().unwrap().as_bytes().to_vec();
EquineLegacyInput {
main_svg: SvgSource {
file_name: "Main.svg".into(),
bytes: text("main"),
},
head_svg: SvgSource {
file_name: "Head.svg".into(),
bytes: text("head"),
},
catalog: text("catalog"),
vertebrae: text("vertebrae"),
}
}
#[test]
fn the_synthetic_cases_match_the_typescript_or_are_refused_alike() {
let frozen = oracle("legacy-synthetic.json");
for case in frozen["cases"].as_array().unwrap() {
let name = case["name"].as_str().unwrap();
let outcome = import_equine_legacy(&synthetic_input(case));
if !case["ok"].as_bool().unwrap() {
assert!(
outcome.is_err(),
"{name}: the TypeScript refused, the port accepted"
);
continue;
}
let result = outcome.unwrap_or_else(|error| panic!("{name}: {error}"));
for (file, text) in render_artifacts(&result).unwrap() {
assert_eq!(
text,
case["files"][&file].as_str().unwrap(),
"{name} {file}"
);
}
let actual = parts(&result);
for (key, expected) in case["parts"].as_object().unwrap() {
assert_same(&actual[key], expected, &format!("{name} {key}"));
}
}
}