//! Shared helpers of the core integration tests: repository files and the Zod oracles. #![allow( dead_code, clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used )] use faunosteo_core::domain::js::{find_lone_surrogate, parse_json}; use serde_json::Value; /// A repository file, by path from the repository root. pub fn repo_file(relative: &str) -> String { let path = format!("{}/../../{relative}", env!("CARGO_MANIFEST_DIR")); std::fs::read_to_string(&path).unwrap_or_else(|error| panic!("{path}: {error}")) } /// A frozen oracle file (`crates/core/tests/oracles/`): its text and its value. The /// TypeScript outputs it freezes keep the lone surrogate escapes `JSON.parse` kept (the L1 /// cases), which the core refuses to read; each one is read here as literal text (`\\uD800` /// spelt out), never replaced, so the raw text stays the evidence of what TypeScript did. pub fn oracle_file(name: &str) -> (String, Value) { let text = repo_file(&format!("crates/core/tests/oracles/{name}")); let mut spelt = String::with_capacity(text.len()); let mut rest = text.as_str(); while let Some(at) = find_lone_surrogate(rest) { spelt.push_str(&rest[..at]); spelt.push('\\'); spelt.push_str(&rest[at..at + 6]); rest = &rest[at + 6..]; } spelt.push_str(rest); let value = parse_json(&spelt).unwrap(); (text, value) } /// A Zod oracle (`crates/core/tests/oracles/`): its source file and the Zod output in /// the JavaScript pretty form. pub fn zod_oracle(name: &str) -> (String, String) { let oracle: Value = serde_json::from_str(&repo_file(&format!("crates/core/tests/oracles/{name}"))).unwrap(); let source = oracle["source"].as_str().unwrap().to_owned(); let output = faunosteo_core::json::to_js_pretty(&oracle["output"]).unwrap(); (repo_file(&source), output) }