Skip to main content

smix_core_conformance/
lib.rs

1#![doc = include_str!("../README.md")]
2#![deny(missing_docs)]
3
4//! smix-core-conformance — cross-SDK conformance harness lib.
5//!
6//! Provides [`Fixture`] type + [`load_fixture`] loader for use by both
7//! the binary `fixture-runner` and per-backend tests.
8
9use serde::{Deserialize, Serialize};
10use serde_json::Value;
11use std::path::PathBuf;
12
13/// One conformance fixture loaded from `fixtures/<id>.json`.
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct Fixture {
16    /// Unique fixture id (filename root).
17    pub id: String,
18    /// One-line human description.
19    pub description: String,
20    /// A11yNode wire JSON form (kept as `Value` so this crate stays
21    /// pure to wire-level — the FFI backend re-serializes when calling).
22    pub tree: Value,
23    /// Selector wire JSON form (same rationale).
24    pub selector: Value,
25    /// Expected resolved node ids (`Vec<String>`).
26    pub expected: Vec<String>,
27}
28
29/// Load a fixture by id from `crates/smix-core-conformance/fixtures/<id>-*.json`.
30///
31/// The directory is resolved relative to the workspace root via the
32/// `CARGO_MANIFEST_DIR` env baked into the binary/test at compile time.
33///
34/// # Errors
35/// Returns an [`anyhow::Error`] when the fixture file does not exist or
36/// fails JSON parse.
37pub fn load_fixture(id: &str) -> anyhow::Result<Fixture> {
38    let manifest_dir = env!("CARGO_MANIFEST_DIR");
39    let fixtures_dir = PathBuf::from(manifest_dir).join("fixtures");
40    let entries = std::fs::read_dir(&fixtures_dir)?;
41    for entry in entries {
42        let entry = entry?;
43        let path = entry.path();
44        let stem = path
45            .file_stem()
46            .and_then(|s| s.to_str())
47            .unwrap_or_default();
48        // accept either exact `<id>` or `<id>-<rest>` filename root
49        if stem == id || stem.starts_with(&format!("{id}-")) {
50            let body = std::fs::read_to_string(&path)?;
51            let fixture: Fixture = serde_json::from_str(&body)?;
52            return Ok(fixture);
53        }
54    }
55    Err(anyhow::anyhow!(
56        "fixture id '{id}' not found in {fixtures_dir:?}"
57    ))
58}