#![allow(unused_imports)]
use super::commands::*;
use super::dispatch::*;
use super::helpers::*;
use super::helpers_state::*;
use super::helpers_time::*;
use super::init::*;
use crate::core::types::ProvenanceEvent;
use crate::core::{codegen, executor, migrate, parser, planner, resolver, secrets, state, types};
use crate::transport;
use crate::tripwire::{anomaly, drift, eventlog, tracer};
use std::path::{Path, PathBuf};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fj017_init() {
let dir = tempfile::tempdir().unwrap();
let sub = dir.path().join("test-project");
std::fs::create_dir_all(&sub).unwrap();
cmd_init(&sub).unwrap();
assert!(sub.join("forjar.yaml").exists());
assert!(sub.join("state").is_dir());
}
#[test]
fn test_fj017_init_already_exists() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("forjar.yaml"), "exists").unwrap();
let result = cmd_init(dir.path());
assert!(result.is_err());
}
#[test]
fn test_fj017_dispatch_init() {
let dir = tempfile::tempdir().unwrap();
let sub = dir.path().join("dispatch-test");
std::fs::create_dir_all(&sub).unwrap();
dispatch(Commands::Init(InitArgs { path: sub.clone() }), 0, true).unwrap();
assert!(sub.join("forjar.yaml").exists());
}
#[test]
fn test_fj017_fmt_check_unformatted() {
let dir = tempfile::tempdir().unwrap();
let config = dir.path().join("forjar.yaml");
std::fs::write(
&config,
r#"version: "1.0"
name: my-infra
machines:
m1:
hostname: box
addr: 1.2.3.4
resources:
pkg:
type: package
machine: m1
provider: apt
packages: [curl]
"#,
)
.unwrap();
let result = cmd_fmt(&config, true);
assert!(result.is_err(), "unformatted file should fail check mode");
}
#[test]
fn test_fj017_fmt_write_then_check() {
let dir = tempfile::tempdir().unwrap();
let config = dir.path().join("forjar.yaml");
std::fs::write(
&config,
r#"version: "1.0"
name: my-infra
machines:
m1:
hostname: box
addr: 1.2.3.4
resources:
pkg:
type: package
machine: m1
provider: apt
packages: [curl]
"#,
)
.unwrap();
cmd_fmt(&config, false).unwrap();
cmd_fmt(&config, true).unwrap();
}
#[test]
fn test_fj017_init_creates_state_dir() {
let dir = tempfile::tempdir().unwrap();
let project = dir.path().join("new-project");
std::fs::create_dir_all(&project).unwrap();
cmd_init(&project).unwrap();
assert!(project.join("forjar.yaml").exists());
assert!(project.join("state").exists());
}
#[test]
fn test_fj017_init_template_is_valid() {
let dir = tempfile::tempdir().unwrap();
let project = dir.path().join("valid-init");
std::fs::create_dir_all(&project).unwrap();
cmd_init(&project).unwrap();
let content = std::fs::read_to_string(project.join("forjar.yaml")).unwrap();
let config: types::ForjarConfig = serde_yaml_ng::from_str(&content).unwrap();
assert_eq!(config.version, "1.0");
assert_eq!(config.name, "my-infrastructure");
}
#[test]
fn test_fj132_cmd_init_creates_project() {
let dir = tempfile::tempdir().unwrap();
cmd_init(dir.path()).unwrap();
assert!(dir.path().join("forjar.yaml").exists());
assert!(dir.path().join("state").is_dir());
let content = std::fs::read_to_string(dir.path().join("forjar.yaml")).unwrap();
let _config: types::ForjarConfig = serde_yaml_ng::from_str(&content).unwrap();
}
#[test]
fn test_fj132_cmd_init_refuses_existing() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("forjar.yaml"), "version: '1.0'").unwrap();
let result = cmd_init(dir.path());
assert!(result.is_err());
assert!(result.unwrap_err().contains("already exists"));
}
#[test]
fn test_fj132_cmd_fmt_already_formatted() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("test.yaml");
let yaml = r#"version: "1.0"
name: test
machines: {}
resources: {}
"#;
let config: types::ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
let formatted = serde_yaml_ng::to_string(&config).unwrap();
std::fs::write(&file, &formatted).unwrap();
cmd_fmt(&file, false).unwrap();
}
#[test]
fn test_fj132_cmd_fmt_check_mode() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("test.yaml");
let yaml = r#"version: "1.0"
name: test
machines: {}
resources: {}
"#;
let config: types::ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
let formatted = serde_yaml_ng::to_string(&config).unwrap();
std::fs::write(&file, &formatted).unwrap();
cmd_fmt(&file, true).unwrap();
}
#[test]
fn test_fj132_cmd_fmt_formats_unformatted() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("messy.yaml");
let yaml = "version: '1.0'\nname: test\nmachines: {}\nresources: {}\n";
std::fs::write(&file, yaml).unwrap();
cmd_fmt(&file, false).unwrap();
let content = std::fs::read_to_string(&file).unwrap();
assert!(content.contains("version"));
}
#[test]
fn test_fj036_cmd_init_creates_state_dir() {
let dir = tempfile::tempdir().unwrap();
let project = dir.path().join("fj036-project");
std::fs::create_dir_all(&project).unwrap();
cmd_init(&project).unwrap();
assert!(
project.join("state").is_dir(),
"cmd_init must create state/ directory"
);
assert!(
project.join("forjar.yaml").exists(),
"cmd_init must create forjar.yaml"
);
let content = std::fs::read_to_string(project.join("forjar.yaml")).unwrap();
let config: types::ForjarConfig = serde_yaml_ng::from_str(&content).unwrap();
assert_eq!(config.version, "1.0");
}
#[test]
fn test_fj017_cmd_fmt_check_valid() {
let dir = tempfile::tempdir().unwrap();
let config_path = dir.path().join("forjar.yaml");
let yaml = r#"
version: "1.0"
name: test
machines:
m1:
hostname: m1
addr: 1.1.1.1
resources:
pkg:
type: package
machine: m1
provider: apt
packages: [curl]
"#;
let parsed: types::ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
let formatted = serde_yaml_ng::to_string(&parsed).unwrap();
std::fs::write(&config_path, &formatted).unwrap();
let result = cmd_fmt(&config_path, true);
assert!(
result.is_ok(),
"cmd_fmt check should succeed on already-formatted config"
);
}
#[test]
fn test_fj253_completion_bash() {
let result = std::thread::Builder::new()
.stack_size(16 * 1024 * 1024)
.spawn(|| cmd_completion(CompletionShell::Bash))
.expect("failed to spawn thread")
.join()
.expect("completion thread panicked");
assert!(result.is_ok());
}
#[test]
fn test_fj253_completion_zsh() {
let result = std::thread::Builder::new()
.stack_size(16 * 1024 * 1024)
.spawn(|| cmd_completion(CompletionShell::Zsh))
.expect("failed to spawn thread")
.join()
.expect("completion thread panicked");
assert!(result.is_ok());
}
#[test]
fn test_fj253_completion_fish() {
let result = std::thread::Builder::new()
.stack_size(16 * 1024 * 1024)
.spawn(|| cmd_completion(CompletionShell::Fish))
.expect("failed to spawn thread")
.join()
.expect("completion thread panicked");
assert!(result.is_ok());
}
#[test]
fn test_fj253_completion_shell_enum_debug() {
let bash = CompletionShell::Bash;
let debug = format!("{bash:?}");
assert_eq!(debug, "Bash");
}
#[test]
fn test_fj253_completion_shell_clone() {
let orig = CompletionShell::Zsh;
let cloned = orig.clone();
assert_eq!(format!("{cloned:?}"), "Zsh");
}
#[test]
fn test_fj264_schema_dispatch() {
let result = dispatch(Commands::Schema, 0, true);
assert!(result.is_ok());
}
#[test]
fn test_fj264_schema_valid_json() {
let result = cmd_schema();
assert!(result.is_ok());
}
#[test]
fn test_fj264_schema_has_required_fields() {
let machine_schema = serde_json::json!({
"type": "object",
"required": ["hostname", "addr"]
});
assert_eq!(
machine_schema["required"][0], "hostname",
"machine schema should require hostname"
);
}
#[test]
fn test_fj264_schema_resource_types() {
let types: [&str; 11] = [
"package", "file", "service", "mount", "user", "docker", "cron", "network", "pepita",
"model", "gpu",
];
assert_eq!(types.len(), 11, "should support 11 resource types");
}
#[test]
fn test_fj264_schema_policy_defaults() {
let policy = serde_json::json!({
"failure": "stop_on_first",
"parallel_machines": false,
"tripwire": true,
"lock_file": true,
"ssh_retries": 1
});
assert_eq!(policy["ssh_retries"], 1);
assert_eq!(policy["tripwire"], true);
}
}