use std::path::PathBuf;
use krypt_core::include::{IncludeError, load_with_includes};
fn fixture(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fixtures")
.join("include")
.join(name)
}
#[test]
fn simple_include_merges_links() {
let cfg = load_with_includes(fixture("simple_root.toml")).expect("should expand");
assert_eq!(cfg.links.len(), 2, "expected root + inc1 links");
assert_eq!(cfg.links[0].dst, "/tmp/root.conf");
assert_eq!(cfg.links[1].dst, "/tmp/inc1.conf");
assert!(cfg.include.is_empty(), "include list must be cleared");
}
#[test]
fn glob_include_expands_all_matching_files() {
let cfg = load_with_includes(fixture("glob_root.toml")).expect("should expand");
assert_eq!(cfg.links.len(), 4, "expected 4 links total");
assert_eq!(cfg.links[0].dst, "/tmp/root.conf");
assert_eq!(cfg.links[1].dst, "/tmp/part_a.conf");
assert_eq!(cfg.links[2].dst, "/tmp/part_b.conf");
assert_eq!(cfg.links[3].dst, "/tmp/part_c.conf");
assert!(cfg.include.is_empty());
}
#[test]
fn nested_include_three_levels() {
let cfg = load_with_includes(fixture("nested/a.toml")).expect("should expand");
assert_eq!(cfg.links.len(), 3, "expected a + b + c links");
assert_eq!(cfg.links[0].dst, "/tmp/a.conf");
assert_eq!(cfg.links[1].dst, "/tmp/b.conf");
assert_eq!(cfg.links[2].dst, "/tmp/c.conf");
assert!(cfg.include.is_empty());
}
#[test]
fn cycle_is_detected() {
let err = load_with_includes(fixture("cycle/a.toml")).expect_err("should detect cycle");
match err {
IncludeError::Cycle { chain } => {
assert!(
chain.contains("a.toml") || chain.contains("b.toml"),
"chain should mention a.toml or b.toml; got: {chain}"
);
}
other => panic!("expected Cycle, got: {other:?}"),
}
}
#[test]
fn depth_exceeded() {
let err = load_with_includes(fixture("depth/d0.toml")).expect_err("should exceed depth");
match err {
IncludeError::DepthExceeded { max, .. } => {
assert_eq!(max, 8);
}
other => panic!("expected DepthExceeded, got: {other:?}"),
}
}
#[test]
fn map_merge_later_wins_on_conflict() {
let cfg = load_with_includes(fixture("map_merge_root.toml")).expect("should expand");
assert_eq!(cfg.paths["HOME"], "/b", "later include should win");
assert_eq!(cfg.paths["EXTRA"], "keep", "root-only key must survive");
}
#[test]
fn vec_merge_appends_in_order() {
let cfg = load_with_includes(fixture("vec_merge_root.toml")).expect("should expand");
assert_eq!(cfg.links.len(), 3, "expected 2 root + 1 include links");
assert_eq!(cfg.links[0].dst, "/tmp/root1.conf");
assert_eq!(cfg.links[1].dst, "/tmp/root2.conf");
assert_eq!(cfg.links[2].dst, "/tmp/inc1.conf");
}