#![cfg(test)]
#![expect(clippy::panic, reason = "test utilities")]
use std::fmt::Write;
use std::str::FromStr;
use cedar_policy::{RequestEnv, Schema};
use cedar_policy_core::ast::Expr;
use super::env::SymEnv;
pub(crate) fn deep_chain_schema(depth: usize) -> Schema {
let mut src = String::new();
for i in 0..depth {
let next_ty = if i + 1 < depth {
format!("Deep{}", i + 1)
} else {
"String".to_string()
};
let _ = writeln!(src, "type Deep{i} = {{ next?: {next_ty} }};");
}
src += "entity User { deep: Deep0 };\n";
src += "action View appliesTo { principal: [User], resource: [User] };\n";
Schema::from_cedarschema_str(&src)
.unwrap_or_else(|e| panic!("{:?}", miette::Report::new(e)))
.0
}
pub(crate) fn deep_chain_sym_env(depth: usize) -> SymEnv {
SymEnv::new(
&deep_chain_schema(depth),
&RequestEnv::new(
"User".parse().unwrap(),
"Action::\"View\"".parse().unwrap(),
"User".parse().unwrap(),
),
)
.expect("Malformed sym env.")
}
pub(crate) fn deep_has_chain_expr(depth: usize) -> Expr {
let path = format!("deep{}", ".next".repeat(depth));
let expr_str = format!("(if (principal has {path}) then principal else principal) has {path}");
Expr::from_str(&expr_str)
.unwrap_or_else(|e| panic!("Could not parse expression: {expr_str}: {e}"))
}