Skip to main content

oxicode/foundation/
fixtures.rs

1//! Shared cross-host fixture loader.
2//!
3//! The cross-host fixture set lives under
4//! `tests/fixtures/oxi-foundation/v1/` and is byte-identical across
5//! oxicode / oxibrain / oxios. Loading here is intentionally dumb
6//! (`include_str!`-style — never network-fetched, never mutated).
7
8use std::path::PathBuf;
9
10/// Resolve the fixture root by walking the current CARGO_MANIFEST_DIR
11/// upward. Returns `None` when the layout can't be found (e.g. when
12/// the crate is consumed as a dependency without the fixtures
13/// directory).
14pub fn fixture_root() -> Option<PathBuf> {
15    let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
16    let candidate = manifest
17        .join("..")
18        .join("..")
19        .join("tests")
20        .join("fixtures")
21        .join("oxi-foundation")
22        .join("v1");
23    if candidate.is_dir() {
24        Some(candidate)
25    } else {
26        None
27    }
28}
29
30/// Read a profile fixture by name (without extension).
31pub fn profile(name: &str) -> Option<String> {
32    let root = fixture_root()?;
33    let path = root.join("profiles").join(format!("{name}.json"));
34    std::fs::read_to_string(path).ok()
35}
36
37/// Read a package fixture by name (without extension).
38pub fn package(name: &str) -> Option<String> {
39    let root = fixture_root()?;
40    let path = root.join("packages").join(format!("{name}.json"));
41    std::fs::read_to_string(path).ok()
42}
43
44/// Read the canonical `foundation.json` fixture.
45pub fn foundation() -> Option<String> {
46    let root = fixture_root()?;
47    let path = root.join("foundation.json");
48    std::fs::read_to_string(path).ok()
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn fixture_root_resolves() {
57        // The fixture directory may not exist in this test build if
58        // the fixtures have not been written yet. We just check the
59        // path computation is stable.
60        let root = fixture_root();
61        assert!(root.is_none() || root.unwrap().ends_with("oxi-foundation/v1"));
62    }
63}