#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
if !ok {
eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
}
ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
c
}
use std::{
io::Write,
process::{Command, Stdio},
};
#[allow(dead_code)]
#[path = "../src/check/json.rs"]
mod json;
fn call(nodes: &str, scope: &str) -> (bool, json::J) {
let raw =
format!(r#"{{"schema":"instinct.check-dependencies/v1","nodes":{nodes},"scope":{scope}}}"#);
let mut c = Command::new(env!("CARGO_BIN_EXE_instinct"))
.args(["check", "dependencies", "--input", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
c.stdin.take().unwrap().write_all(raw.as_bytes()).ok();
let o = c.wait_with_output().unwrap();
(o.status.success(), json::parse(&o.stdout).unwrap())
}
fn get<'a>(v: &'a json::J, k: &str) -> &'a json::J {
let json::J::Obj(fields) = v else {
panic!("object")
};
&fields.iter().find(|(key, _)| key == k).unwrap().1
}
fn node(id: &str, parents: &str, value: Option<bool>, scope: &str) -> String {
let facts = value
.map(|v| format!("{{\"value\":{v},\"truth\":true}}"))
.unwrap_or("{}".into());
format!(
r#"{{"id":"{id}","depends_on":{parents},"request":{{"check":{{"schema":"instinct.check/v1","id":"test","revision":1,"inputs":{{"value":"bool","truth":"bool"}},"claim":{{"op":"eq","left":{{"fact":"value"}},"right":{{"fact":"truth"}}}},"scope":{scope}}},"facts":{facts}}}}}"#
)
}
#[test]
fn input_order_does_not_override_dependency_order() {
let a = node("a", "[]", Some(true), "{}");
let b = node("b", r#"["a"]"#, Some(true), "{}");
let (ok, v) = call(&format!("[{b},{a}]"), "{}");
assert!(ok, "{v:?}");
assert_eq!(get(&v, "verdict"), &json::J::Str("pass".into()));
assert_eq!(get(&v, "evaluations"), &json::J::Num("2".into()));
let json::J::Arr(rows) = get(&v, "nodes") else {
panic!("rows")
};
assert_eq!(get(&rows[0], "id"), &json::J::Str("a".into()));
}
#[test]
fn every_parent_state_obeys_dependency_truth_table() {
for a in [Some(true), Some(false), None] {
for b in [Some(true), Some(false), None] {
let nodes = format!(
"[{},{}]",
node("a", "[]", a, "{}"),
node("b", r#"["a"]"#, b, "{}")
);
let (ok, v) = call(&nodes, "{}");
assert!(ok);
let expected = if a == Some(false) {
"fail"
} else if a.is_none() {
"abstain"
} else {
match b {
Some(true) => "pass",
Some(false) => "fail",
None => "abstain",
}
};
assert_eq!(get(&v, "verdict"), &json::J::Str(expected.into()));
assert_eq!(
get(&v, "evaluations"),
&json::J::Num(if a == Some(true) { "2" } else { "1" }.into())
);
}
}
}
#[test]
fn cycles_unknown_parents_and_duplicate_ids_reject() {
for nodes in [
format!("[{}]", node("a", r#"["a"]"#, Some(true), "{}")),
format!(
"[{},{}]",
node("a", r#"["b"]"#, Some(true), "{}"),
node("b", r#"["a"]"#, Some(true), "{}")
),
format!("[{}]", node("a", r#"["missing"]"#, Some(true), "{}")),
format!(
"[{},{}]",
node("a", "[]", Some(true), "{}"),
node("a", "[]", Some(true), "{}")
),
] {
assert!(!call(&nodes, "{}").0);
}
}
#[test]
fn aggregate_scope_cannot_widen_and_conflicts_reject() {
let scoped = node("a", "[]", Some(true), r#"{"project":"lab"}"#);
let (ok, v) = call(&format!("[{scoped}]"), "{}");
assert!(ok);
assert_eq!(get(&v, "verdict"), &json::J::Str("abstain".into()));
assert_eq!(get(&v, "evaluations"), &json::J::Num("0".into()));
let other = node("b", "[]", Some(true), r#"{"project":"other"}"#);
assert!(!call(&format!("[{scoped},{other}]"), r#"{"project":"lab"}"#).0);
}
#[test]
fn malformed_blocked_node_is_still_validated() {
let a = node("a", "[]", Some(false), "{}");
let b = node("b", r#"["a"]"#, Some(true), "{}").replace("\"eq\"", "\"shell\"");
assert!(!call(&format!("[{a},{b}]"), "{}").0);
}
#[test]
fn diamond_evaluates_each_node_once() {
let nodes = format!(
"[{},{},{},{}]",
node("d", r#"["b","c"]"#, Some(true), "{}"),
node("c", r#"["a"]"#, Some(true), "{}"),
node("b", r#"["a"]"#, Some(true), "{}"),
node("a", "[]", Some(true), "{}")
);
let (ok, v) = call(&nodes, "{}");
assert!(ok);
assert_eq!(get(&v, "verdict"), &json::J::Str("pass".into()));
assert_eq!(get(&v, "evaluations"), &json::J::Num("4".into()));
}
#[test]
fn size_and_duplicate_edge_limits_reject() {
let nodes = (0..33)
.map(|i| node(&format!("n{i}"), "[]", Some(true), "{}"))
.collect::<Vec<_>>()
.join(",");
assert!(!call(&format!("[{nodes}]"), "{}").0);
let nodes = format!(
"[{},{}]",
node("a", "[]", Some(true), "{}"),
node("b", r#"["a","a"]"#, Some(true), "{}")
);
assert!(!call(&nodes, "{}").0);
}
#[test]
fn changed_facts_are_recomputed() {
let a = node("a", "[]", Some(true), "{}");
let b = node("b", r#"["a"]"#, Some(true), "{}");
let raw = format!("[{a},{b}]");
assert_eq!(
get(&call(&raw, "{}").1, "verdict"),
&json::J::Str("pass".into())
);
let changed = format!("[{},{}]", node("a", "[]", Some(false), "{}"), b);
let (ok, v) = call(&changed, "{}");
assert!(ok);
assert_eq!(get(&v, "verdict"), &json::J::Str("fail".into()));
assert_eq!(get(&v, "cached"), &json::J::Bool(false));
}