test(config): pin the checked-in schemas of the three trees as the oracle

tests/schemas.rs reads schema.json and schema.asc.json of the Windows tree, komorebi-for-kde and komorebi-for-mac by path and checks that every file is in the canonical form the generator prints (so a regenerated schema compares with == on text) and that each keeps today's keys: 53/37/33 properties, 60/52/47 definitions, the six definitions of the applications schema. Nothing is generated from the crate yet, so at this point the test guards the three files against accidental edits; each following stage adds a comparison of the types it moves with these definitions.

Verified: native cargo test -p komorebi-config: 2 passed, 0 failed (tier 1). The three checked-in files were byte-identical to their regenerated forms before this stage (Windows 141169/5945 bytes, KDE 127164/5051, macOS 120057/5051; S/step3/regen.sh).
This commit is contained in:
LGUG2Z committed 2026-10-31 01:37:53 -07:00
1 parent 67c1189bd8
commit 06436c98fe
1 file changed
+111
+111
View File
@@ -0,0 +1,111 @@
//! The oracle of the crate: the schemas that are checked in next to the three daemons.
//!
//! `schema.json` and `schema.asc.json` of the Windows tree (the repository root), of
//! `komorebi-for-kde` and of `komorebi-for-mac` are what users, the docs and the nix options
//! are generated from, so they must not change by accident while the data types move into this
//! crate. The files are read by their path relative to the repository.
//!
//! What is checked at this stage:
//! - every checked-in file is in the canonical form the generator prints (pretty JSON, one
//! trailing newline), so a regenerated schema can be compared with `==` on the text;
//! - the files keep the keys they have today (53/37/33 properties, 60/52/47 definitions and
//! the six definitions of the applications schema): a step that drops or adds a key of
//! `komorebi.json` for one platform fails here.
use std::path::PathBuf;
use serde_json::Value;
struct Tree {
name: &'static str,
dir: &'static str,
properties: usize,
defs: usize,
}
const TREES: [Tree; 3] = [
Tree {
name: "windows",
dir: "",
properties: 53,
defs: 60,
},
Tree {
name: "kde",
dir: "komorebi-for-kde",
properties: 37,
defs: 52,
},
Tree {
name: "macos",
dir: "komorebi-for-mac",
properties: 33,
defs: 47,
},
];
fn checked_in(tree: &Tree, file: &str) -> String {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../..")
.join(tree.dir)
.join(file);
std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()))
}
#[test]
fn the_checked_in_schemas_are_in_the_form_the_generator_prints() {
for tree in &TREES {
for file in ["schema.json", "schema.asc.json"] {
let text = checked_in(tree, file);
let value: Value = serde_json::from_str(&text).expect("a schema is JSON");
let mut again = serde_json::to_string_pretty(&value).expect("JSON");
again.push('\n');
assert!(
text == again,
"{} {file} is not in canonical form",
tree.name
);
}
}
}
#[test]
fn the_checked_in_schemas_have_the_keys_of_each_platform() {
for tree in &TREES {
let schema: Value = serde_json::from_str(&checked_in(tree, "schema.json")).unwrap();
assert_eq!(schema["title"], "StaticConfig", "{}", tree.name);
assert_eq!(
schema["properties"].as_object().unwrap().len(),
tree.properties,
"{} properties",
tree.name
);
assert_eq!(
schema["$defs"].as_object().unwrap().len(),
tree.defs,
"{} definitions",
tree.name
);
let asc: Value = serde_json::from_str(&checked_in(tree, "schema.asc.json")).unwrap();
let defs: Vec<&str> = asc["$defs"]
.as_object()
.unwrap()
.keys()
.map(String::as_str)
.collect();
assert_eq!(
defs,
[
"ApplicationIdentifier",
"AscApplicationRules",
"AscApplicationRulesOrSchema",
"IdWithIdentifier",
"MatchingRule",
"MatchingStrategy"
],
"{} applications schema",
tree.name
);
}
}