The 21 rules of validateSheetDocument ported with identical codes, severities, paths and messages (frozen from the TypeScript on 26 conformance fixtures), createEmptySheetDocument, the publish check (strict shape then rules), and the index data React reads once (positions with Map semantics, names, spine ids, finding-path keys). The fixtures also carry the codes private.sheet_document_issues returns: a cargo test generates supabase/tests/011_sheet_conformance.sql and refuses drift (PGlite 12/12 files, real stack 446/446). .prettierignore keeps the byte-exact oracles and generated fixtures out of pnpm format.
152 lines
5.7 KiB
Rust
152 lines
5.7 KiB
Rust
//! Conformance of the integrity rules across the three implementations (review III.4):
|
|
//! the fixtures under `tests/conformance` carry what the TypeScript rules reported (frozen by
|
|
//! `packages/domain/tests/r2-conformance.test.ts`) and the codes the SQL re-check returns.
|
|
//! This suite checks the Rust rules against the first and generates the pgTAP file that checks
|
|
//! SQL against the second (`supabase/tests/011_sheet_conformance.sql`, never edited by hand:
|
|
//! `FAUNOSTEO_WRITE_CONFORMANCE=1 cargo test -p faunosteo-core --test sheet_conformance`).
|
|
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
|
|
|
mod common;
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
use faunosteo_core::domain::js::parse_json;
|
|
use faunosteo_core::domain::sheet::{SheetIssueSeverity, check_sheet_document};
|
|
use serde_json::Value;
|
|
|
|
const SQL_FILE: &str = "supabase/tests/011_sheet_conformance.sql";
|
|
const TAG: &str = "$fixture$";
|
|
|
|
/// The error codes `private.sheet_document_issues` mirrors (codes only, deduplicated).
|
|
const MIRRORED: [&str; 17] = [
|
|
"view.main.count",
|
|
"view.id.duplicate",
|
|
"view.parent.missing",
|
|
"view.unreachable",
|
|
"view.asset.missing",
|
|
"annotation.id.duplicate",
|
|
"annotation.view.missing",
|
|
"annotation.category.missing",
|
|
"annotation.opens_view.invalid",
|
|
"annotation.finding_zone.missing",
|
|
"annotation.region.missing",
|
|
"region.code.duplicate",
|
|
"category.id.duplicate",
|
|
"finding_zone.id.duplicate",
|
|
"reference.id.duplicate",
|
|
"finding.reference.missing",
|
|
"region.count_range.invalid",
|
|
];
|
|
|
|
fn fixtures() -> Vec<(String, Value)> {
|
|
let dir = format!("{}/tests/conformance", env!("CARGO_MANIFEST_DIR"));
|
|
let mut names: Vec<String> = std::fs::read_dir(&dir)
|
|
.unwrap()
|
|
.map(|entry| entry.unwrap().file_name().into_string().unwrap())
|
|
.filter(|name| name.ends_with(".json"))
|
|
.collect();
|
|
names.sort();
|
|
names
|
|
.into_iter()
|
|
.map(|name| {
|
|
let text = common::repo_file(&format!("crates/core/tests/conformance/{name}"));
|
|
(name, parse_json(&text).unwrap())
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn sql_codes(fixture: &Value) -> Vec<String> {
|
|
let codes = fixture["sql"].as_array().unwrap();
|
|
codes
|
|
.iter()
|
|
.map(|c| c.as_str().unwrap().to_owned())
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn the_rust_rules_report_what_the_typescript_rules_reported() {
|
|
let all = fixtures();
|
|
assert!(all.len() >= 25, "every rule family has a fixture");
|
|
for (name, fixture) in all {
|
|
let outcome = check_sheet_document(fixture["document"].clone());
|
|
if fixture["rust"]["shape_error"] == Value::Bool(true) {
|
|
assert!(outcome.is_err(), "{name}: the shape tier must refuse it");
|
|
continue;
|
|
}
|
|
let check = outcome.unwrap_or_else(|e| panic!("{name}: {e}"));
|
|
let issues = serde_json::to_value(&check.issues).unwrap();
|
|
assert_eq!(issues, fixture["rust"]["issues"], "{name}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_sql_expectations_are_the_mirrored_error_codes() {
|
|
for (name, fixture) in fixtures() {
|
|
if fixture["rust"]["shape_error"] == Value::Bool(true) {
|
|
continue;
|
|
}
|
|
let check = check_sheet_document(fixture["document"].clone()).unwrap();
|
|
let mirrored: BTreeSet<String> = check
|
|
.issues
|
|
.iter()
|
|
.filter(|i| i.severity == SheetIssueSeverity::Error)
|
|
.filter(|i| MIRRORED.contains(&i.code.as_str()))
|
|
.map(|i| i.code.clone())
|
|
.collect();
|
|
let expected: Vec<String> = mirrored.into_iter().collect();
|
|
assert_eq!(sql_codes(&fixture), expected, "{name}");
|
|
}
|
|
}
|
|
|
|
fn sql_literal(codes: &[String]) -> String {
|
|
format!("'{{{}}}'::text[]", codes.join(","))
|
|
}
|
|
|
|
fn assertion(name: &str, fixture: &Value) -> String {
|
|
let document = serde_json::to_string(&fixture["document"]).unwrap();
|
|
assert!(
|
|
!document.contains(TAG),
|
|
"{name}: dollar-quote tag in the document"
|
|
);
|
|
let description = fixture["description"].as_str().unwrap().replace('\'', "''");
|
|
let stem = name.trim_end_matches(".json");
|
|
format!(
|
|
"select is(\n array(select c from unnest(private.sheet_document_issues({TAG}{document}{TAG}::jsonb)) as c order by c collate \"C\"),\n {},\n 'conformance {stem}: {description}'\n);\n",
|
|
sql_literal(&sql_codes(fixture))
|
|
)
|
|
}
|
|
|
|
fn generated_sql() -> String {
|
|
let all = fixtures();
|
|
let mut out = String::from(
|
|
"-- GENERATED from crates/core/tests/conformance/*.json by crates/core/tests/sheet_conformance.rs:\n\
|
|
-- FAUNOSTEO_WRITE_CONFORMANCE=1 cargo test -p faunosteo-core --test sheet_conformance\n\
|
|
-- Never edited by hand: cargo fails when this file differs from the fixtures.\n\
|
|
-- The SQL publication re-check returns, for each fixture, the error codes the Rust\n\
|
|
-- integrity rules report that SQL mirrors (codes only, sorted), plus its own\n\
|
|
-- document.shape and reference.url.scheme on documents the shape tier refuses.\n\
|
|
begin;\n",
|
|
);
|
|
out.push_str(&format!("select plan({});\n\n", all.len()));
|
|
for (name, fixture) in &all {
|
|
out.push_str(&assertion(name, fixture));
|
|
out.push('\n');
|
|
}
|
|
out.push_str("select * from finish();\nrollback;\n");
|
|
out
|
|
}
|
|
|
|
#[test]
|
|
fn the_pgtap_file_is_generated_from_the_fixtures() {
|
|
let expected = generated_sql();
|
|
let path = format!("{}/../../{SQL_FILE}", env!("CARGO_MANIFEST_DIR"));
|
|
if std::env::var("FAUNOSTEO_WRITE_CONFORMANCE").as_deref() == Ok("1") {
|
|
std::fs::write(&path, &expected).unwrap();
|
|
}
|
|
let committed = std::fs::read_to_string(&path).unwrap_or_default();
|
|
assert!(
|
|
committed == expected,
|
|
"{SQL_FILE} differs from the fixtures: regenerate it (FAUNOSTEO_WRITE_CONFORMANCE=1)"
|
|
);
|
|
}
|