Files
alkane-os/third-party/flare-ui/crates/wschema-cli/tests/cli.rs
T
x ad31cfbafa Vendor flare-ui v0.2.9 and prove the schema-driven UI in an image
Spike, not the final UI: it answers whether flare-ui can drive Alkane OS's
on-box pages, and what it costs.

What works:

- schema/alkane-bb.json authors the sidebar as data (3 sections, 14 tabs);
- src/bin/ui-spike.rs renders it through flare_ui::nav + flare_ui::shell, so
  the unified sidebar, ordering and surface filtering come from the schema
  rather than from hand-written HTML;
- the render uses the shared bf- design tokens, the same ones the rest of the
  suite uses.

Size, measured rather than assumed (armv7, release, stripped):

  ui-baseline   323 KiB   (maud + serde_json, no flare-ui)
  ui-spike      771 KiB   (flare-ui + wschema + sha2)
  marginal      448 KiB

Vendoring: third-party/flare-ui is the full v0.2.9 tree pinned in
flare-ui/flare-ui.lock (a partial archive does not compile, since the crate
include_str!s assets from dash-editor/). vendor/ is re-generated as a cargo
directory source with the new registry dependencies (maud, sha2, typenum,
digest, generic-array, block-buffer, crypto-common, cfg-if, etc.) so the build
still works offline. third-party/README.md records how to re-pin.
2026-11-04 14:06:25 -06:00

169 lines
5.5 KiB
Rust

//! End-to-end tests of the `wschema-cli` binary.
use std::path::PathBuf;
use std::process::{Command, Output};
const FRAGMENT: &str = r#"{
"format": 1, "kind": "fragment", "id": "production",
"requires": {"display_api": 1, "checks_api": 1},
"settings": [{
"path": "apps.production.fluid", "label": "Fluid", "help": "Primary produced fluid.",
"kind": {"t": "enum", "values": [{"v": "gas", "label": "Gas"}, {"v": "oil", "label": "Oil"}]},
"default": {"value": "gas"}, "owner": "synced", "guarded": false,
"section": "apps.production", "order": 10,
"apply": {"class": "none"}, "display": {"t": "dropdown"}}],
"menus": [
{"id": "production", "slug": "production", "title": "Production", "band": 2, "order": 0},
{"id": "production/settings", "slug": "settings", "title": "Settings", "order": 30,
"page": "production.settings"}],
"pages": [{"id": "production.settings",
"blocks": [{"kind": "card", "order": 10, "section": "apps.production"}]}]
}"#;
fn scratch(name: &str, text: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("wschema-cli-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let p = dir.join(name);
std::fs::write(&p, text).unwrap();
p
}
fn run(args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_wschema-cli"))
.args(args)
.output()
.unwrap()
}
#[test]
fn lint_passes_a_clean_fragment() {
let f = scratch("clean.json", FRAGMENT);
let out = run(&["lint", f.to_str().unwrap()]);
assert_eq!(
out.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&out.stdout)
);
assert!(out.stdout.is_empty());
}
#[test]
fn lint_fails_with_codes_on_a_bad_fragment() {
let bad = FRAGMENT.replace("apps.production.fluid", "apps.other.fluid");
let f = scratch("bad.json", &bad);
let out = run(&["lint", f.to_str().unwrap()]);
assert_eq!(out.status.code(), Some(1));
assert!(String::from_utf8_lossy(&out.stdout).contains("L15"));
}
#[test]
fn canon_is_stable_and_hash_matches_sha256_of_it() {
let f = scratch("canon.json", FRAGMENT);
let a = run(&["canon", f.to_str().unwrap()]);
let b = run(&["canon", f.to_str().unwrap()]);
assert_eq!(a.status.code(), Some(0));
assert_eq!(a.stdout, b.stdout);
assert!(a.stdout.starts_with(b"{\"format\":1,\"id\":\"production\""));
let h = run(&["hash", f.to_str().unwrap()]);
let want = wschema::sha256_hex(&a.stdout);
assert_eq!(String::from_utf8(h.stdout).unwrap().trim(), want);
}
#[test]
fn canon_raw_sorts_keys() {
let f = scratch("raw.json", r#"{"b": 1, "a": [2, {"d": 0, "c": 1}]}"#);
let out = run(&["canon", "--raw", f.to_str().unwrap()]);
assert_eq!(
String::from_utf8(out.stdout).unwrap(),
r#"{"a":[2,{"c":1,"d":0}],"b":1}"#
);
}
#[test]
fn bad_input_exits_2() {
let out = run(&["lint", "/nonexistent/file.json"]);
assert_eq!(out.status.code(), Some(2));
let f = scratch("junk.json", "{not json");
assert_eq!(run(&["canon", f.to_str().unwrap()]).status.code(), Some(2));
assert_eq!(run(&[]).status.code(), Some(2));
}
fn corpus_file(name: &str) -> String {
format!("{}/../../corpus/{name}", env!("CARGO_MANIFEST_DIR"))
}
#[test]
fn validate_runs_the_corpus() {
let base = corpus_file("schema/warden-base.json");
let out = run(&[
"validate",
"--schema",
&base,
"--corpus",
&corpus_file("generic.jsonl"),
"--corpus",
&corpus_file("flare-ported.jsonl"),
]);
assert_eq!(out.status.code(), Some(0), "{out:?}");
assert!(String::from_utf8(out.stderr)
.unwrap()
.ends_with("case(s) passed\n"));
}
#[test]
fn validate_reports_mismatches_and_resolves_fragment_ids() {
let base = corpus_file("schema/warden-base.json");
let frag = scratch("production.json", FRAGMENT);
let cases = scratch(
"cases.jsonl",
concat!(
r#"{"schema":"production","origin":"flare","path":"apps.production.fluid","value":"oil","expect":{"ok":true}}"#,
"\n",
r#"{"schema":"production","origin":"flare","path":"apps.production.fluid","value":"water","expect":{"ok":true}}"#,
"\n",
r#"{"schema":"base","origin":"flare","path":"apps.production.fluid","value":"oil","expect":{"code":"V_PATH"}}"#,
"\n",
),
);
let out = run(&[
"validate",
"--schema",
&base,
"--fragment",
frag.to_str().unwrap(),
"--corpus",
cases.to_str().unwrap(),
]);
assert_eq!(out.status.code(), Some(1), "{out:?}");
let stdout = String::from_utf8(out.stdout).unwrap();
assert_eq!(stdout.lines().count(), 1, "{stdout}");
assert!(stdout.contains("line 2: apps.production.fluid: expected ok, got V_ENUM"));
assert_eq!(
String::from_utf8(out.stderr).unwrap(),
"1 of 3 case(s) mismatched\n"
);
}
#[test]
fn validate_usage_and_input_errors() {
let base = corpus_file("schema/warden-base.json");
assert_eq!(run(&["validate", "--schema", &base]).status.code(), Some(2));
assert_eq!(
run(&["validate", "--schema", &base, "--schema", &base, "--corpus", "x"])
.status
.code(),
Some(2)
);
let junk = scratch("junk.jsonl", "{\"schema\":\"base\"}\n");
let out = run(&[
"validate",
"--schema",
&base,
"--corpus",
junk.to_str().unwrap(),
]);
assert_eq!(out.status.code(), Some(2));
assert!(String::from_utf8(out.stderr).unwrap().contains("line 1"));
}