#![cfg(feature = "server")]
use symforge::cli::InitClient;
use symforge::cli::init::{
merge_hooks_into_settings, register_codex_mcp_server, register_kilo_mcp_server,
run_init_with_context,
};
use tempfile::TempDir;
const FAKE_BINARY: &str = "/usr/local/bin/symforge";
fn fake_binary_path() -> std::path::PathBuf {
std::path::PathBuf::from(FAKE_BINARY)
}
fn read_settings(dir: &TempDir) -> serde_json::Value {
let path = dir.path().join("settings.json");
let settings_json = std::fs::read_to_string(&path).expect("settings.json must exist");
serde_json::from_str(&settings_json).expect("settings.json must be valid JSON")
}
fn read_text(path: &std::path::Path) -> String {
std::fs::read_to_string(path).expect("text file must exist")
}
#[test]
fn test_init_writes_hooks() {
let dir = TempDir::new().unwrap();
let settings_path = dir.path().join("settings.json");
merge_hooks_into_settings(&settings_path, &fake_binary_path())
.expect("merge_hooks_into_settings must succeed");
let settings = read_settings(&dir);
let post = settings["hooks"]["PostToolUse"]
.as_array()
.expect("PostToolUse must be an array");
let session = settings["hooks"]["SessionStart"]
.as_array()
.expect("SessionStart must be an array");
let prompt = settings["hooks"]["UserPromptSubmit"]
.as_array()
.expect("UserPromptSubmit must be an array");
assert_eq!(
post.len(),
1,
"PostToolUse must have 1 entry (single stdin-routed entry)"
);
assert_eq!(session.len(), 1, "SessionStart must have 1 entry");
assert_eq!(prompt.len(), 1, "UserPromptSubmit must have 1 entry");
let all_commands: Vec<&str> = post
.iter()
.chain(session.iter())
.flat_map(|e| e["hooks"].as_array().unwrap())
.filter_map(|h| h["command"].as_str())
.collect();
for cmd in &all_commands {
assert!(
cmd.contains("symforge hook"),
"command must contain 'symforge hook': {cmd}"
);
assert!(
cmd.contains(FAKE_BINARY),
"command must contain binary path {FAKE_BINARY}: {cmd}"
);
}
let matcher = post[0]["matcher"].as_str().unwrap();
assert_eq!(
matcher, "Read|Edit|Write|Grep",
"matcher must cover all tools"
);
let has_session = all_commands
.iter()
.any(|c| c.ends_with("hook session-start"));
assert!(has_session, "SessionStart hook must be present");
let has_prompt_submit = prompt
.iter()
.flat_map(|e| e["hooks"].as_array().unwrap())
.filter_map(|h| h["command"].as_str())
.any(|c| c.ends_with("hook prompt-submit"));
assert!(has_prompt_submit, "UserPromptSubmit hook must be present");
}
#[test]
fn test_init_idempotent() {
let dir = TempDir::new().unwrap();
let settings_path = dir.path().join("settings.json");
merge_hooks_into_settings(&settings_path, &fake_binary_path())
.expect("first merge must succeed");
let after_first = std::fs::read_to_string(&settings_path).unwrap();
merge_hooks_into_settings(&settings_path, &fake_binary_path())
.expect("second merge must succeed");
let after_second = std::fs::read_to_string(&settings_path).unwrap();
assert_eq!(
after_first, after_second,
"running merge_hooks_into_settings twice must produce identical output (idempotent)"
);
let settings = read_settings(&dir);
let post_count = settings["hooks"]["PostToolUse"].as_array().unwrap().len();
assert_eq!(post_count, 1, "second merge must not add duplicate entries");
}
#[test]
fn test_init_preserves_other_hooks() {
let dir = TempDir::new().unwrap();
let settings_path = dir.path().join("settings.json");
let initial = serde_json::json!({
"hooks": {
"PostToolUse": [
{
"matcher": "Bash",
"hooks": [{"type": "command", "command": "/some/other/hook bash", "timeout": 10}]
}
]
}
});
std::fs::write(
&settings_path,
serde_json::to_string_pretty(&initial).unwrap(),
)
.unwrap();
merge_hooks_into_settings(&settings_path, &fake_binary_path()).expect("merge must succeed");
let settings = read_settings(&dir);
let post = settings["hooks"]["PostToolUse"]
.as_array()
.expect("PostToolUse must be an array");
assert_eq!(post.len(), 2, "existing hook + 1 symforge hook = 2 entries");
let has_bash_hook = post.iter().any(|e| {
e["hooks"][0]["command"]
.as_str()
.map(|c| c == "/some/other/hook bash")
.unwrap_or(false)
});
assert!(
has_bash_hook,
"non-symforge hook must be preserved after merge"
);
}
#[test]
fn test_init_registers_mcp_server() {
let dir = TempDir::new().unwrap();
let claude_json_path = dir.path().join(".claude.json");
let binary_path = "/usr/local/bin/symforge";
symforge::cli::init::register_mcp_server(&claude_json_path, binary_path)
.expect("register_mcp_server must succeed");
let config_json = std::fs::read_to_string(&claude_json_path).unwrap();
let config: serde_json::Value = serde_json::from_str(&config_json).unwrap();
let tok = &config["mcpServers"]["symforge"];
let expected_command = if cfg!(windows) {
binary_path.replace('/', "\\")
} else {
binary_path.to_string()
};
assert_eq!(tok["command"], expected_command);
assert_eq!(tok["disabled"], false, "disabled must be false");
assert_eq!(
tok["env"]["SYMFORGE_SURFACE"].as_str(),
Some("full"),
"Claude init must pin the full surface"
);
assert!(
tok["alwaysAllow"].is_array(),
"alwaysAllow must be an array"
);
let always_allow = tok["alwaysAllow"].as_array().unwrap();
assert!(
always_allow.iter().any(|v| v.as_str() == Some("health")),
"alwaysAllow must include health"
);
assert!(
always_allow
.iter()
.any(|v| v.as_str() == Some("search_symbols")),
"alwaysAllow must include search_symbols"
);
assert!(
always_allow
.iter()
.any(|v| v.as_str() == Some("replace_symbol_body")),
"alwaysAllow must include replace_symbol_body"
);
}
#[test]
fn test_init_mcp_registration_idempotent() {
let dir = TempDir::new().unwrap();
let claude_json_path = dir.path().join(".claude.json");
let binary_path = "/usr/local/bin/symforge";
symforge::cli::init::register_mcp_server(&claude_json_path, binary_path).unwrap();
let first = std::fs::read_to_string(&claude_json_path).unwrap();
symforge::cli::init::register_mcp_server(&claude_json_path, binary_path).unwrap();
let second = std::fs::read_to_string(&claude_json_path).unwrap();
assert_eq!(first, second, "register_mcp_server must be idempotent");
}
#[test]
fn test_init_mcp_registration_preserves_other_servers() {
let dir = TempDir::new().unwrap();
let claude_json_path = dir.path().join(".claude.json");
let initial = serde_json::json!({
"mcpServers": {
"other-server": {"type": "stdio", "command": "other-binary"}
}
});
std::fs::write(
&claude_json_path,
serde_json::to_string_pretty(&initial).unwrap(),
)
.unwrap();
symforge::cli::init::register_mcp_server(&claude_json_path, "/usr/local/bin/symforge").unwrap();
let config_json = std::fs::read_to_string(&claude_json_path).unwrap();
let config: serde_json::Value = serde_json::from_str(&config_json).unwrap();
assert!(
config["mcpServers"]["other-server"].is_object(),
"other MCP server must be preserved"
);
assert!(
config["mcpServers"]["symforge"].is_object(),
"symforge must be added"
);
}
#[test]
fn test_init_registers_codex_mcp_server() {
let dir = TempDir::new().unwrap();
let codex_config_path = dir.path().join(".codex").join("config.toml");
let binary_path = r"C:\Users\user\.symforge\bin\symforge.exe";
register_codex_mcp_server(&codex_config_path, binary_path)
.expect("register_codex_mcp_server must succeed");
let config_toml = std::fs::read_to_string(&codex_config_path).unwrap();
assert!(
config_toml.contains("[mcp_servers.symforge]"),
"config must contain a symforge MCP table: {config_toml}"
);
assert!(
config_toml.contains(binary_path),
"config must contain the Windows binary path: {config_toml}"
);
assert!(
config_toml.contains("startup_timeout_sec"),
"config must tune Codex MCP startup timeout: {config_toml}"
);
assert!(
config_toml.contains("tool_timeout_sec"),
"config must tune Codex MCP tool timeout: {config_toml}"
);
assert!(
config_toml.contains("project_doc_fallback_filenames"),
"config must configure project doc fallbacks: {config_toml}"
);
assert!(
config_toml.contains("CLAUDE.md"),
"config must include CLAUDE.md as a project doc fallback: {config_toml}"
);
}
#[test]
fn test_init_codex_registration_idempotent() {
let dir = TempDir::new().unwrap();
let codex_config_path = dir.path().join(".codex").join("config.toml");
let binary_path = r"C:\Users\user\.symforge\bin\symforge.exe";
register_codex_mcp_server(&codex_config_path, binary_path).unwrap();
let first = std::fs::read_to_string(&codex_config_path).unwrap();
register_codex_mcp_server(&codex_config_path, binary_path).unwrap();
let second = std::fs::read_to_string(&codex_config_path).unwrap();
assert_eq!(
first, second,
"register_codex_mcp_server must be idempotent"
);
}
#[test]
fn test_init_codex_registration_preserves_other_config() {
let dir = TempDir::new().unwrap();
let codex_dir = dir.path().join(".codex");
let codex_config_path = codex_dir.join("config.toml");
std::fs::create_dir_all(&codex_dir).unwrap();
std::fs::write(
&codex_config_path,
r#"# keep this comment
model = "gpt-5.4"
project_doc_fallback_filenames = ["README.agent.md"]
[mcp_servers.other]
command = "other.exe"
"#,
)
.unwrap();
register_codex_mcp_server(
&codex_config_path,
r"C:\Users\user\.symforge\bin\symforge.exe",
)
.unwrap();
let config_toml = std::fs::read_to_string(&codex_config_path).unwrap();
assert!(
config_toml.contains("# keep this comment"),
"existing comments should survive"
);
assert!(
config_toml.contains("model = \"gpt-5.4\""),
"existing config should survive"
);
assert!(
config_toml.contains("[mcp_servers.other]"),
"other MCP servers should survive"
);
assert!(
config_toml.contains("README.agent.md"),
"existing project doc fallbacks should survive"
);
assert!(
config_toml.contains("CLAUDE.md"),
"SymForge should merge CLAUDE.md into project doc fallbacks"
);
}
#[test]
fn test_run_init_grok_preserves_toml_and_is_idempotent() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let grok_dir = home.path().join(".grok");
let grok_config = grok_dir.join("config.toml");
std::fs::create_dir_all(&grok_dir).unwrap();
std::fs::write(
&grok_config,
r#"# keep this comment
theme = "dark"
[mcp_servers.other]
command = "other.exe"
enabled = false
[mcp_servers.symforge]
command = "stale.exe" # keep command comment
args = ["--stale"]
enabled = false
custom = "keep"
[mcp_servers.symforge.env]
KEEP_ME = "yes"
RUST_LOG = "debug" # keep env comment
SYMFORGE_SURFACE = "compact"
SYMFORGE_WORKSPACE_ROOT = "stale-root"
"#,
)
.unwrap();
let binary_path = fake_binary_path();
let client = <InitClient as clap::ValueEnum>::from_str("grok", true)
.expect("grok must be a supported init client");
run_init_with_context(client, home.path(), cwd.path(), &binary_path)
.expect("grok init must succeed");
let first = read_text(&grok_config);
run_init_with_context(client, home.path(), cwd.path(), &binary_path)
.expect("repeated grok init must succeed");
let second = read_text(&grok_config);
assert_eq!(first, second, "Grok registration must be idempotent");
assert!(first.contains("# keep this comment"));
assert!(first.contains("theme = \"dark\""));
assert!(first.contains("[mcp_servers.other]"));
assert!(first.contains("custom = \"keep\""));
assert!(first.contains("# keep command comment"));
assert!(first.contains("# keep env comment"));
let config = first.parse::<toml_edit::DocumentMut>().unwrap();
let symforge = &config["mcp_servers"]["symforge"];
let expected_command = if cfg!(windows) {
FAKE_BINARY.replace('/', "\\")
} else {
FAKE_BINARY.to_string()
};
assert_eq!(
symforge["command"].as_str(),
Some(expected_command.as_str())
);
assert_eq!(symforge["args"].as_array().map(|args| args.len()), Some(0));
assert_eq!(symforge["enabled"].as_bool(), Some(true));
assert_eq!(symforge["env"]["RUST_LOG"].as_str(), Some("off"));
assert_eq!(
symforge["env"]["SYMFORGE_WORKSPACE_ROOT"].as_str(),
Some(cwd.path().display().to_string().as_str())
);
assert_eq!(symforge["env"]["KEEP_ME"].as_str(), Some("yes"));
assert_eq!(
symforge["env"]["SYMFORGE_SURFACE"].as_str(),
Some("compact"),
"unmanaged Grok env values must be preserved"
);
assert!(!home.path().join(".codex").join("config.toml").exists());
assert!(!home.path().join(".claude.json").exists());
assert!(!home.path().join(".gemini").join("settings.json").exists());
assert!(!cwd.path().join(".kilocode").join("mcp.json").exists());
}
#[test]
fn test_run_init_grok_preserves_inline_tables() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let grok_dir = home.path().join(".grok");
let grok_config = grok_dir.join("config.toml");
std::fs::create_dir_all(&grok_dir).unwrap();
std::fs::write(
&grok_config,
r#"mcp_servers = { other = { command = "x" }, symforge = { command = "stale", custom = "keep", env = { KEEP_ME = "yes" } } }
"#,
)
.unwrap();
let binary_path = fake_binary_path();
let client = <InitClient as clap::ValueEnum>::from_str("grok", true).unwrap();
run_init_with_context(client, home.path(), cwd.path(), &binary_path).unwrap();
let first = read_text(&grok_config);
run_init_with_context(client, home.path(), cwd.path(), &binary_path).unwrap();
let second = read_text(&grok_config);
assert_eq!(first, second);
let config = first.parse::<toml_edit::DocumentMut>().unwrap();
let servers = &config["mcp_servers"];
let symforge = &servers["symforge"];
assert_eq!(servers["other"]["command"].as_str(), Some("x"));
assert_eq!(symforge["custom"].as_str(), Some("keep"));
assert_eq!(symforge["env"]["KEEP_ME"].as_str(), Some("yes"));
assert_eq!(symforge["args"].as_array().map(|args| args.len()), Some(0));
assert_eq!(symforge["enabled"].as_bool(), Some(true));
assert_eq!(symforge["env"]["RUST_LOG"].as_str(), Some("off"));
assert_eq!(
symforge["env"]["SYMFORGE_WORKSPACE_ROOT"].as_str(),
Some(cwd.path().display().to_string().as_str())
);
}
#[test]
fn test_init_registers_kilo_mcp_server() {
let dir = TempDir::new().unwrap();
let kilo_config_path = dir.path().join(".kilocode").join("mcp.json");
let binary_path = r"C:\\Users\\user\\.symforge\\bin\\symforge.exe";
register_kilo_mcp_server(&kilo_config_path, binary_path)
.expect("register_kilo_mcp_server must succeed");
let config_json = std::fs::read_to_string(&kilo_config_path).unwrap();
let config: serde_json::Value = serde_json::from_str(&config_json).unwrap();
let symforge = &config["mcpServers"]["symforge"];
assert_eq!(symforge["command"], binary_path);
assert_eq!(symforge["disabled"], false, "disabled must be false");
assert_eq!(
symforge["args"],
serde_json::json!([]),
"args must be empty"
);
assert_eq!(
symforge["env"]["SYMFORGE_SURFACE"].as_str(),
Some("full"),
"Kilo env must make the full surface explicit"
);
let always_allow = symforge["alwaysAllow"]
.as_array()
.expect("alwaysAllow must be an array");
let names: Vec<&str> = always_allow.iter().filter_map(|v| v.as_str()).collect();
for name in [
"symforge",
"symforge_edit",
"status",
"search_symbols",
"replace_symbol_body",
] {
assert!(
names.contains(&name),
"alwaysAllow must grant full-surface name {name}: {names:?}"
);
}
}
#[test]
fn test_init_kilo_registration_preserves_other_servers() {
let dir = TempDir::new().unwrap();
let kilo_config_path = dir.path().join(".kilocode").join("mcp.json");
let initial = serde_json::json!({
"mcpServers": {
"other-server": {
"command": "other-binary",
"args": ["stdio"]
}
}
});
std::fs::create_dir_all(kilo_config_path.parent().unwrap()).unwrap();
std::fs::write(
&kilo_config_path,
serde_json::to_string_pretty(&initial).unwrap(),
)
.unwrap();
register_kilo_mcp_server(&kilo_config_path, "/usr/local/bin/symforge").unwrap();
let config_json = std::fs::read_to_string(&kilo_config_path).unwrap();
let config: serde_json::Value = serde_json::from_str(&config_json).unwrap();
assert!(
config["mcpServers"]["other-server"].is_object(),
"other MCP servers must be preserved"
);
assert!(
config["mcpServers"]["symforge"].is_object(),
"symforge must be added"
);
}
#[test]
fn test_run_init_codex_only_updates_codex_files() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
run_init_with_context(InitClient::Codex, home.path(), cwd.path(), &binary_path)
.expect("codex init must succeed");
assert!(
home.path().join(".codex").join("config.toml").exists(),
"Codex config must be created"
);
assert!(
home.path().join(".codex").join("AGENTS.md").exists(),
"Codex global AGENTS guidance must be created"
);
assert!(
!home.path().join(".claude.json").exists(),
"Claude MCP config must not be created for codex-only init"
);
assert!(
!home.path().join(".claude").join("settings.json").exists(),
"Claude hooks config must not be created for codex-only init"
);
assert!(
!home.path().join(".claude").join("CLAUDE.md").exists(),
"Claude memory file must not be created for codex-only init"
);
assert!(
cwd.path().join(".symforge").exists(),
"runtime directory must still be created"
);
}
#[test]
fn project_aware_init_reconciles_existing_root_gitignore() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
std::fs::create_dir(cwd.path().join(".git")).expect("create git metadata marker");
std::fs::write(cwd.path().join(".gitignore"), b"target/\n").expect("write root gitignore");
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
run_init_with_context(InitClient::Codex, home.path(), cwd.path(), &binary_path)
.expect("project-aware init must succeed");
run_init_with_context(InitClient::Codex, home.path(), cwd.path(), &binary_path)
.expect("repeated project-aware init must succeed");
assert_eq!(
std::fs::read(cwd.path().join(".gitignore")).expect("read reconciled gitignore"),
b"target/\n/.symforge/\n",
"init must reconcile once, preserve newline style, and remain idempotent"
);
}
#[test]
fn test_run_init_claude_only_updates_claude_files() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
run_init_with_context(InitClient::Claude, home.path(), cwd.path(), &binary_path)
.expect("claude init must succeed");
assert!(
home.path().join(".claude.json").exists(),
"Claude MCP config must be created"
);
assert!(
home.path().join(".claude").join("settings.json").exists(),
"Claude hooks config must be created"
);
assert!(
home.path().join(".claude").join("CLAUDE.md").exists(),
"Claude guidance memory must be created"
);
assert!(
!home.path().join(".codex").join("config.toml").exists(),
"Codex config must not be created for claude-only init"
);
assert!(
!home.path().join(".codex").join("AGENTS.md").exists(),
"Codex AGENTS guidance must not be created for claude-only init"
);
assert!(
!home.path().join(".gemini").join("settings.json").exists(),
"Gemini config must not be created for claude-only init"
);
assert!(
!cwd.path().join(".kilocode").join("mcp.json").exists(),
"Kilo config must not be created for claude-only init"
);
}
#[test]
fn test_run_init_all_updates_both_clients() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let stable_bin_dir = std::env::current_dir()
.unwrap()
.join("target")
.join("test-global-bin")
.join(format!("init-all-{}", std::process::id()));
std::fs::create_dir_all(&stable_bin_dir).unwrap();
let binary_path = stable_bin_dir.join(if cfg!(windows) {
"symforge.exe"
} else {
"symforge"
});
std::fs::write(&binary_path, b"").unwrap();
let claude_desktop_config = if cfg!(windows) {
home.path()
.join("AppData")
.join("Roaming")
.join("Claude")
.join("claude_desktop_config.json")
} else if cfg!(target_os = "macos") {
home.path()
.join("Library")
.join("Application Support")
.join("Claude")
.join("claude_desktop_config.json")
} else {
home.path()
.join(".config")
.join("Claude")
.join("claude_desktop_config.json")
};
run_init_with_context(InitClient::All, home.path(), cwd.path(), &binary_path)
.expect("all-client init must succeed");
assert!(
home.path().join(".codex").join("config.toml").exists(),
"Codex config must be created"
);
assert!(
home.path().join(".grok").join("config.toml").exists(),
"Grok config must be created"
);
assert!(
home.path().join(".claude.json").exists(),
"Claude MCP config must be created"
);
assert!(
home.path().join(".claude").join("settings.json").exists(),
"Claude hooks config must be created"
);
assert!(
home.path().join(".claude").join("CLAUDE.md").exists(),
"Claude guidance memory must be created"
);
assert!(
home.path().join(".codex").join("AGENTS.md").exists(),
"Codex AGENTS guidance must be created"
);
assert!(
home.path().join(".gemini").join("settings.json").exists(),
"Gemini config must be created"
);
assert!(
home.path().join(".gemini").join("GEMINI.md").exists(),
"Gemini guidance must be created"
);
assert!(
cwd.path().join(".kilocode").join("mcp.json").exists(),
"Kilo config must be created"
);
assert!(
cwd.path()
.join(".kilocode")
.join("rules")
.join("symforge.md")
.exists(),
"Kilo guidance rules must be created"
);
assert!(
claude_desktop_config.exists(),
"Claude Desktop config must be created under the injected home"
);
let claude_desktop_config_json = read_text(&claude_desktop_config);
let claude_desktop_config_value: serde_json::Value =
serde_json::from_str(&claude_desktop_config_json).unwrap();
let claude_desktop_command = claude_desktop_config_value["mcpServers"]["symforge"]["command"]
.as_str()
.unwrap();
#[cfg(windows)]
{
let desktop_config_dir = claude_desktop_config
.parent()
.expect("desktop config has a parent dir");
assert!(
claude_desktop_command.contains(&desktop_config_dir.display().to_string()),
"Claude Desktop command must point at the wrapper in the config dir: {claude_desktop_command}"
);
let wrapper_script = read_text(&desktop_config_dir.join("symforge-desktop.cmd"));
assert!(
wrapper_script.contains(&stable_bin_dir.display().to_string()),
"the wrapper must launch the stable binary by absolute path: {wrapper_script}"
);
}
#[cfg(not(windows))]
assert!(
claude_desktop_command.contains(&stable_bin_dir.display().to_string()),
"Claude Desktop command must be the stable binary path on non-Windows: {claude_desktop_command}"
);
let codex_config = read_text(&home.path().join(".codex").join("config.toml"));
assert!(
codex_config.contains(&binary_path.display().to_string()),
"Codex config must use stable binary path: {codex_config}"
);
}
#[test]
fn test_run_init_codex_writes_symforge_agents_guidance() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
run_init_with_context(InitClient::Codex, home.path(), cwd.path(), &binary_path)
.expect("codex init must succeed");
let agents_path = home.path().join(".codex").join("AGENTS.md");
let raw = read_text(&agents_path);
assert!(
raw.contains("SYMFORGE START"),
"Codex AGENTS guidance must include a SymForge marker block: {raw}"
);
assert!(
raw.contains("SymForge MCP"),
"Codex AGENTS guidance must mention SymForge MCP: {raw}"
);
assert!(
raw.contains("get_file_context"),
"Codex AGENTS guidance must include tool guidance: {raw}"
);
assert!(
raw.contains("validate_file_syntax"),
"Codex AGENTS guidance must include config validation guidance: {raw}"
);
assert!(
raw.contains("| Task | Call |"),
"Codex AGENTS guidance must carry the task-to-tool map: {raw}"
);
assert!(
!raw.contains("## Agent Directives: Mechanical Overrides")
&& !raw.contains("## Tooling Preference"),
"removed guidance sections must not ship again: {raw}"
);
assert!(
raw.contains("\n<!-- SYMFORGE END -->"),
"end marker must sit alone on its own line, nothing fused before it: {raw}"
);
}
#[test]
fn test_run_init_codex_preserves_existing_agents_content_and_is_idempotent() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
let codex_dir = home.path().join(".codex");
std::fs::create_dir_all(&codex_dir).unwrap();
let agents_path = codex_dir.join("AGENTS.md");
std::fs::write(&agents_path, "# Existing guidance\n\nKeep this line.\n").unwrap();
run_init_with_context(InitClient::Codex, home.path(), cwd.path(), &binary_path)
.expect("first codex init must succeed");
let first = read_text(&agents_path);
run_init_with_context(InitClient::Codex, home.path(), cwd.path(), &binary_path)
.expect("second codex init must succeed");
let second = read_text(&agents_path);
assert!(
second.contains("Keep this line."),
"existing Codex guidance must survive"
);
assert_eq!(first, second, "Codex AGENTS guidance must be idempotent");
}
#[test]
fn test_run_init_codex_never_duplicates_external_overrides_heading() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
let codex_dir = home.path().join(".codex");
std::fs::create_dir_all(&codex_dir).unwrap();
let agents_path = codex_dir.join("AGENTS.md");
std::fs::write(
&agents_path,
"# Existing guidance\n\n## Agent Directives: Mechanical Overrides\n\nKeep external copy.\n",
)
.unwrap();
run_init_with_context(InitClient::Codex, home.path(), cwd.path(), &binary_path)
.expect("codex init must succeed");
let raw = read_text(&agents_path);
assert_eq!(
raw.matches("## Agent Directives: Mechanical Overrides")
.count(),
1,
"Codex AGENTS guidance must not duplicate an external overrides block: {raw}"
);
}
#[test]
fn test_run_init_claude_writes_symforge_memory_guidance() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
run_init_with_context(InitClient::Claude, home.path(), cwd.path(), &binary_path)
.expect("claude init must succeed");
let memory_path = home.path().join(".claude").join("CLAUDE.md");
let raw = read_text(&memory_path);
assert!(
raw.contains("SYMFORGE START"),
"Claude memory guidance must include a SymForge marker block: {raw}"
);
assert!(
raw.contains("SymForge MCP"),
"Claude memory guidance must mention SymForge MCP: {raw}"
);
assert!(
raw.contains("get_file_context"),
"Claude memory guidance must include tool guidance: {raw}"
);
assert!(
raw.contains("| Task | Call |"),
"Claude memory guidance must carry the task-to-tool map: {raw}"
);
assert!(
raw.contains("validate_file_syntax"),
"Claude memory guidance must include config validation guidance: {raw}"
);
assert!(
!raw.contains("## Agent Directives: Mechanical Overrides")
&& !raw.contains("## Tooling Preference"),
"removed guidance sections must not ship again: {raw}"
);
}
#[test]
fn test_run_init_claude_preserves_existing_memory_content_and_is_idempotent() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
let claude_dir = home.path().join(".claude");
std::fs::create_dir_all(&claude_dir).unwrap();
let memory_path = claude_dir.join("CLAUDE.md");
std::fs::write(&memory_path, "# Existing memory\n\nKeep this line.\n").unwrap();
run_init_with_context(InitClient::Claude, home.path(), cwd.path(), &binary_path)
.expect("first claude init must succeed");
let first = read_text(&memory_path);
run_init_with_context(InitClient::Claude, home.path(), cwd.path(), &binary_path)
.expect("second claude init must succeed");
let second = read_text(&memory_path);
assert!(
second.contains("Keep this line."),
"existing Claude memory must survive"
);
assert_eq!(first, second, "Claude memory guidance must be idempotent");
}
#[test]
fn test_run_init_claude_never_duplicates_external_overrides_heading() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
let claude_dir = home.path().join(".claude");
std::fs::create_dir_all(&claude_dir).unwrap();
let memory_path = claude_dir.join("CLAUDE.md");
std::fs::write(
&memory_path,
"# Existing memory\n\n## Agent Directives: Mechanical Overrides\n\nKeep external copy.\n",
)
.unwrap();
run_init_with_context(InitClient::Claude, home.path(), cwd.path(), &binary_path)
.expect("claude init must succeed");
let raw = read_text(&memory_path);
assert_eq!(
raw.matches("## Agent Directives: Mechanical Overrides")
.count(),
1,
"Claude memory guidance must not duplicate an external overrides block: {raw}"
);
}
#[test]
fn test_run_init_gemini_only_updates_gemini_files() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
run_init_with_context(InitClient::Gemini, home.path(), cwd.path(), &binary_path)
.expect("gemini init must succeed");
assert!(
home.path().join(".gemini").join("settings.json").exists(),
"Gemini config must be created"
);
assert!(
home.path().join(".gemini").join("GEMINI.md").exists(),
"Gemini guidance must be created"
);
assert!(
!home.path().join(".codex").join("config.toml").exists(),
"Codex config must not be created for gemini-only init"
);
assert!(
!home.path().join(".claude.json").exists(),
"Claude config must not be created for gemini-only init"
);
assert!(
!cwd.path().join(".kilocode").join("mcp.json").exists(),
"Kilo config must not be created for gemini-only init"
);
}
#[test]
fn test_run_init_gemini_writes_full_symforge_guidance() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
run_init_with_context(InitClient::Gemini, home.path(), cwd.path(), &binary_path)
.expect("gemini init must succeed");
let guidance_path = home.path().join(".gemini").join("GEMINI.md");
let raw = read_text(&guidance_path);
assert!(
raw.contains("| Task | Call |"),
"Gemini guidance must carry the task-to-tool map: {raw}"
);
assert!(
raw.contains("validate_file_syntax"),
"Gemini guidance must include config validation guidance: {raw}"
);
assert!(
raw.contains("Raw reads (`get_file_content`/Read) remain correct"),
"Gemini guidance must keep the raw-read fallback rule: {raw}"
);
assert!(
!raw.contains("## Agent Directives: Mechanical Overrides"),
"Gemini guidance must not include Claude/Codex-only mechanical overrides: {raw}"
);
}
#[test]
fn test_run_init_kilo_only_updates_kilo_files() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
run_init_with_context(InitClient::KiloCode, home.path(), cwd.path(), &binary_path)
.expect("kilo init must succeed");
assert!(
cwd.path().join(".kilocode").join("mcp.json").exists(),
"Kilo config must be created"
);
assert!(
cwd.path()
.join(".kilocode")
.join("rules")
.join("symforge.md")
.exists(),
"Kilo guidance rules must be created"
);
assert!(
!home.path().join(".codex").join("config.toml").exists(),
"Codex config must not be created for kilo-only init"
);
assert!(
!home.path().join(".gemini").join("settings.json").exists(),
"Gemini config must not be created for kilo-only init"
);
}
#[test]
fn test_run_init_kilo_writes_symforge_rules_guidance() {
let home = TempDir::new().unwrap();
let cwd = TempDir::new().unwrap();
let binary_path = std::path::PathBuf::from(FAKE_BINARY);
run_init_with_context(InitClient::KiloCode, home.path(), cwd.path(), &binary_path)
.expect("kilo init must succeed");
let rules_path = cwd
.path()
.join(".kilocode")
.join("rules")
.join("symforge.md");
let raw = read_text(&rules_path);
assert!(
raw.contains("SymForge MCP"),
"Kilo rules guidance must mention SymForge MCP: {raw}"
);
assert!(
raw.contains("| Task | Call |"),
"Kilo rules guidance must carry the task-to-tool map: {raw}"
);
assert!(
raw.contains("validate_file_syntax"),
"Kilo rules guidance must include config validation guidance: {raw}"
);
assert!(
!raw.contains("## Agent Directives: Mechanical Overrides"),
"Kilo guidance must not include Claude/Codex-only mechanical overrides: {raw}"
);
}