//! The palette's own gates, plus the guard that keeps `palette.json` — the //! record's JSON rendering, for the readers that are not Rust (the Mac's icon //! scripts, the site's tokens) — equal to what this module declares. //! //! Ported from what the retired TypeScript palette's own tests and the retired //! `rust-gen.test.ts`'s Swift section asserted about this record, and extended //! with the totality the TypeScript could not state. use super::*; use std::collections::BTreeSet; use std::path::PathBuf; /// The committed JSON the surviving TypeScript reads. Inside `crates/`, so /// nothing under `crates/` reaches outside it (step 5, R12). fn json_path() -> PathBuf { // Run time, not `env!` — a cached test binary carries a checkout that is gone (#403). PathBuf::from( std::env::var("CARGO_MANIFEST_DIR") .expect("cargo sets CARGO_MANIFEST_DIR for every `cargo test`"), ) .join("src/design/palette.json") } fn hex_is_well_formed(hex: &str) -> bool { let digits = &hex[1..]; hex.starts_with('#') && (digits.len() == 6 || digits.len() == 8) && digits.chars().all(|ch| ch.is_ascii_hexdigit()) } #[test] fn every_slot_of_both_palettes_is_a_colour() { // Both records — the app's PAPER and the brand's fixed GRAPHITE — even // though only one of them is ever resolved to. for palette in [PAPER, GRAPHITE] { for slot in PaletteSlot::ALL { let hex = palette.slot(slot); assert!( hex_is_well_formed(hex), "{}.{} is {hex:?}, which is not #RRGGBB or #RRGGBBAA", palette.name, slot.as_str() ); } } for slot in StatusSlot::ALL { let hex = STATUS.slot(slot); assert!( hex_is_well_formed(hex), "status {} is {hex:?}", slot.as_str() ); } for slot in SidebarSlot::ALL { let hex = SIDEBAR.slot(slot); assert!( hex_is_well_formed(hex), "sidebar {} is {hex:?}", slot.as_str() ); } } #[test] fn the_catalogue_names_every_slot_once() { let sets = colorsets(); assert_eq!( sets.len(), PaletteSlot::ALL.len() + StatusSlot::ALL.len() + SidebarSlot::ALL.len() ); let names: BTreeSet<&str> = sets.iter().map(|(name, _)| *name).collect(); assert_eq!(names.len(), sets.len(), "two colorsets share a name"); } // ── The interval guard (step 5, R18's pattern) ──────────────────────────── // // `packages/ui` and its TypeScript copy of the palette are gone (5i). The JSON // stays, because two readers that are not Rust want the record — the Mac's // icon scripts (`apps/cotton/scripts/`) and the site's tokens — and this test // is what keeps the committed file equal to the declaration: the Swift the // apps compile is `cotton-gen`'s, rendered from THIS module, never from the // JSON. fn palette_json() -> String { let mut root = serde_json::Map::new(); let palette_object = |palette: &Palette| { let mut object = serde_json::Map::new(); object.insert("name".into(), palette.name.into()); for slot in PaletteSlot::ALL { object.insert(slot.as_str().into(), palette.slot(slot).into()); } serde_json::Value::Object(object) }; root.insert("paper".into(), palette_object(&PAPER)); root.insert("graphite".into(), palette_object(&GRAPHITE)); let status_object = |status: &Status| { let mut object = serde_json::Map::new(); for slot in StatusSlot::ALL { object.insert(slot.as_str().into(), status.slot(slot).into()); } serde_json::Value::Object(object) }; root.insert("status".into(), status_object(&STATUS)); format!( "{}\n", serde_json::to_string_pretty(&serde_json::Value::Object(root)).expect("the record is JSON") ) } #[test] fn the_emitted_json_matches_the_committed_one() { let path = json_path(); let wanted = palette_json(); let found = std::fs::read_to_string(&path).ok(); if found.as_deref() == Some(wanted.as_str()) { return; } if std::env::var("COTTON_DESIGN_WRITE_JSON").as_deref() == Ok("1") { // The writing run PASSES, on purpose. Writing the file and then failing // anyway reads as "the rewrite did not work", and the honest answer is // that this run no longer knows: it compared the OLD bytes. The next // run compares the new ones, which is what the line asks for. std::fs::write(&path, &wanted).expect("write the palette JSON"); println!( "{} rewritten from `design::palette` — re-run without \ COTTON_DESIGN_WRITE_JSON to confirm, then commit it.", path.display() ); return; } panic!( "{} is not what `design::palette` declares. Run \ `COTTON_DESIGN_WRITE_JSON=1 cargo test -p cotton-core design::palette` and commit it; \ the Mac's icon scripts and the site's tokens read this file, and the apps' Swift is \ `cotton-gen`'s rendering of the same declaration.", path.display() ); } #[test] fn the_committed_json_holds_every_slot_of_both_records() { // Anti-vacuity: an empty or truncated JSON would let the TypeScript sweep // pass over nothing, which is the failure mode AGENTS.md names for a guard // that reads a file by path. let text = std::fs::read_to_string(json_path()) .unwrap_or_else(|error| panic!("{}: {error}", json_path().display())); for slot in PaletteSlot::ALL { assert!(text.contains(slot.as_str()), "{} is missing", slot.as_str()); } for slot in StatusSlot::ALL { assert!(text.contains(slot.as_str()), "{} is missing", slot.as_str()); } }