1use std::path::PathBuf;
2
3pub mod agents;
4mod support;
5use agents::{
6 install_amp_hook, install_claude_hook, install_claude_hook_config, install_claude_hook_scripts,
7 install_claude_project_hooks, install_cline_rules, install_codex_hook, install_copilot_hook,
8 install_crush_hook, install_cursor_hook, install_cursor_hook_config,
9 install_cursor_hook_scripts, install_gemini_hook, install_gemini_hook_config,
10 install_gemini_hook_scripts, install_hermes_hook, install_jetbrains_hook, install_kiro_hook,
11 install_opencode_hook, install_pi_hook, install_windsurf_rules,
12};
13use support::{
14 ensure_codex_hooks_enabled, install_codex_instruction_docs, install_named_json_server,
15 upsert_lean_ctx_codex_hook_entries,
16};
17
18fn mcp_server_quiet_mode() -> bool {
19 std::env::var_os("LEAN_CTX_MCP_SERVER").is_some()
20 || matches!(std::env::var("LEAN_CTX_QUIET"), Ok(value) if value.trim() == "1")
21}
22
23pub fn refresh_installed_hooks() {
26 let Some(home) = crate::core::home::resolve_home_dir() else {
27 return;
28 };
29
30 let claude_dir = crate::setup::claude_config_dir(&home);
31 let claude_hooks = claude_dir.join("hooks/lean-ctx-rewrite.sh").exists()
32 || claude_dir.join("settings.json").exists()
33 && std::fs::read_to_string(claude_dir.join("settings.json"))
34 .unwrap_or_default()
35 .contains("lean-ctx");
36
37 if claude_hooks {
38 install_claude_hook_scripts(&home);
39 install_claude_hook_config(&home);
40 }
41
42 let cursor_hooks = home.join(".cursor/hooks/lean-ctx-rewrite.sh").exists()
43 || home.join(".cursor/hooks.json").exists()
44 && std::fs::read_to_string(home.join(".cursor/hooks.json"))
45 .unwrap_or_default()
46 .contains("lean-ctx");
47
48 if cursor_hooks {
49 install_cursor_hook_scripts(&home);
50 install_cursor_hook_config(&home);
51 }
52
53 let gemini_rewrite = home.join(".gemini/hooks/lean-ctx-rewrite-gemini.sh");
54 let gemini_legacy = home.join(".gemini/hooks/lean-ctx-hook-gemini.sh");
55 if gemini_rewrite.exists() || gemini_legacy.exists() {
56 install_gemini_hook_scripts(&home);
57 install_gemini_hook_config(&home);
58 }
59
60 let codex_hooks = home.join(".codex/hooks/lean-ctx-rewrite-codex.sh").exists()
61 || home.join(".codex/hooks.json").exists()
62 && std::fs::read_to_string(home.join(".codex/hooks.json"))
63 .unwrap_or_default()
64 .contains("lean-ctx");
65
66 if codex_hooks {
67 install_codex_hook();
68 }
69}
70
71fn resolve_binary_path() -> String {
72 if is_lean_ctx_in_path() {
73 return "lean-ctx".to_string();
74 }
75 crate::core::portable_binary::resolve_portable_binary()
76}
77
78fn is_lean_ctx_in_path() -> bool {
79 let which_cmd = if cfg!(windows) { "where" } else { "which" };
80 std::process::Command::new(which_cmd)
81 .arg("lean-ctx")
82 .stdout(std::process::Stdio::null())
83 .stderr(std::process::Stdio::null())
84 .status()
85 .is_ok_and(|s| s.success())
86}
87
88fn resolve_binary_path_for_bash() -> String {
89 let path = resolve_binary_path();
90 to_bash_compatible_path(&path)
91}
92
93pub fn to_bash_compatible_path(path: &str) -> String {
94 let path = match crate::core::pathutil::strip_verbatim_str(path) {
95 Some(stripped) => stripped,
96 None => path.replace('\\', "/"),
97 };
98 if path.len() >= 2 && path.as_bytes()[1] == b':' {
99 let drive = (path.as_bytes()[0] as char).to_ascii_lowercase();
100 format!("/{drive}{}", &path[2..])
101 } else {
102 path
103 }
104}
105
106pub fn normalize_tool_path(path: &str) -> String {
110 let mut p = match crate::core::pathutil::strip_verbatim_str(path) {
111 Some(stripped) => stripped,
112 None => path.to_string(),
113 };
114
115 if p.len() >= 3
117 && p.starts_with('/')
118 && p.as_bytes()[1].is_ascii_alphabetic()
119 && p.as_bytes()[2] == b'/'
120 {
121 let drive = p.as_bytes()[1].to_ascii_uppercase() as char;
122 p = format!("{drive}:{}", &p[2..]);
123 }
124
125 p = p.replace('\\', "/");
126
127 while p.contains("//") && !p.starts_with("//") {
129 p = p.replace("//", "/");
130 }
131
132 if p.len() > 1 && p.ends_with('/') && !p.ends_with(":/") {
134 p.pop();
135 }
136
137 p
138}
139
140pub fn generate_rewrite_script(binary: &str) -> String {
141 let case_pattern = crate::rewrite_registry::bash_case_pattern();
142 format!(
143 r#"#!/usr/bin/env bash
144# lean-ctx PreToolUse hook — rewrites bash commands to lean-ctx equivalents
145set -euo pipefail
146
147LEAN_CTX_BIN="{binary}"
148
149INPUT=$(cat)
150TOOL=$(echo "$INPUT" | grep -oE '"tool_name":"([^"\\]|\\.)*"' | head -1 | sed 's/^"tool_name":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g')
151
152if [ "$TOOL" != "Bash" ] && [ "$TOOL" != "bash" ]; then
153 exit 0
154fi
155
156CMD=$(echo "$INPUT" | grep -oE '"command":"([^"\\]|\\.)*"' | head -1 | sed 's/^"command":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g')
157
158if [ -z "$CMD" ] || echo "$CMD" | grep -qE "^(lean-ctx |$LEAN_CTX_BIN )"; then
159 exit 0
160fi
161
162case "$CMD" in
163 {case_pattern})
164 # Shell-escape then JSON-escape (two passes)
165 SHELL_ESC=$(printf '%s' "$CMD" | sed 's/\\/\\\\/g;s/"/\\"/g')
166 REWRITE="$LEAN_CTX_BIN -c \"$SHELL_ESC\""
167 JSON_CMD=$(printf '%s' "$REWRITE" | sed 's/\\/\\\\/g;s/"/\\"/g')
168 printf '{{"hookSpecificOutput":{{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{{"command":"%s"}}}}}}' "$JSON_CMD"
169 ;;
170 *) exit 0 ;;
171esac
172"#
173 )
174}
175
176pub fn generate_compact_rewrite_script(binary: &str) -> String {
177 let case_pattern = crate::rewrite_registry::bash_case_pattern();
178 format!(
179 r#"#!/usr/bin/env bash
180# lean-ctx hook — rewrites shell commands
181set -euo pipefail
182LEAN_CTX_BIN="{binary}"
183INPUT=$(cat)
184CMD=$(echo "$INPUT" | grep -oE '"command":"([^"\\]|\\.)*"' | head -1 | sed 's/^"command":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g' 2>/dev/null || echo "")
185if [ -z "$CMD" ] || echo "$CMD" | grep -qE "^(lean-ctx |$LEAN_CTX_BIN )"; then exit 0; fi
186case "$CMD" in
187 {case_pattern})
188 SHELL_ESC=$(printf '%s' "$CMD" | sed 's/\\/\\\\/g;s/"/\\"/g')
189 REWRITE="$LEAN_CTX_BIN -c \"$SHELL_ESC\""
190 JSON_CMD=$(printf '%s' "$REWRITE" | sed 's/\\/\\\\/g;s/"/\\"/g')
191 printf '{{"hookSpecificOutput":{{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{{"command":"%s"}}}}}}' "$JSON_CMD" ;;
192 *) exit 0 ;;
193esac
194"#
195 )
196}
197
198const REDIRECT_SCRIPT_CLAUDE: &str = r"#!/usr/bin/env bash
199# lean-ctx PreToolUse hook — all native tools pass through
200# Read/Grep/ListFiles are allowed so Edit (which requires native Read) works.
201# The MCP instructions guide the AI to prefer ctx_read/ctx_search/ctx_tree.
202exit 0
203";
204
205const REDIRECT_SCRIPT_GENERIC: &str = r"#!/usr/bin/env bash
206# lean-ctx hook — all native tools pass through
207exit 0
208";
209
210pub fn install_project_rules() {
211 if crate::core::config::Config::load().rules_scope_effective()
212 == crate::core::config::RulesScope::Global
213 {
214 return;
215 }
216
217 let cwd = std::env::current_dir().unwrap_or_default();
218
219 if !is_inside_git_repo(&cwd) {
220 eprintln!(
221 " Skipping project files: not inside a git repository.\n \
222 Run this command from your project root to create CLAUDE.md / AGENTS.md."
223 );
224 return;
225 }
226
227 let home = crate::core::home::resolve_home_dir().unwrap_or_default();
228 if cwd == home {
229 eprintln!(
230 " Skipping project files: current directory is your home folder.\n \
231 Run this command from a project directory instead."
232 );
233 return;
234 }
235
236 ensure_project_agents_integration(&cwd);
237
238 let cursorrules = cwd.join(".cursorrules");
239 if !cursorrules.exists()
240 || !std::fs::read_to_string(&cursorrules)
241 .unwrap_or_default()
242 .contains("lean-ctx")
243 {
244 let content = CURSORRULES_TEMPLATE;
245 if cursorrules.exists() {
246 let mut existing = std::fs::read_to_string(&cursorrules).unwrap_or_default();
247 if !existing.ends_with('\n') {
248 existing.push('\n');
249 }
250 existing.push('\n');
251 existing.push_str(content);
252 write_file(&cursorrules, &existing);
253 } else {
254 write_file(&cursorrules, content);
255 }
256 println!("Created/updated .cursorrules in project root.");
257 }
258
259 let claude_rules_dir = cwd.join(".claude").join("rules");
260 let claude_rules_file = claude_rules_dir.join("lean-ctx.md");
261 if !claude_rules_file.exists()
262 || !std::fs::read_to_string(&claude_rules_file)
263 .unwrap_or_default()
264 .contains(crate::rules_inject::RULES_VERSION_STR)
265 {
266 let _ = std::fs::create_dir_all(&claude_rules_dir);
267 write_file(
268 &claude_rules_file,
269 crate::rules_inject::rules_dedicated_markdown(),
270 );
271 println!("Created .claude/rules/lean-ctx.md (Claude Code project rules).");
272 }
273
274 install_claude_project_hooks(&cwd);
275
276 let kiro_dir = cwd.join(".kiro");
277 if kiro_dir.exists() {
278 let steering_dir = kiro_dir.join("steering");
279 let steering_file = steering_dir.join("lean-ctx.md");
280 if !steering_file.exists()
281 || !std::fs::read_to_string(&steering_file)
282 .unwrap_or_default()
283 .contains("lean-ctx")
284 {
285 let _ = std::fs::create_dir_all(&steering_dir);
286 write_file(&steering_file, KIRO_STEERING_TEMPLATE);
287 println!("Created .kiro/steering/lean-ctx.md (Kiro steering).");
288 }
289 }
290}
291
292const PROJECT_LEAN_CTX_MD_MARKER: &str = "<!-- lean-ctx-owned: PROJECT-LEAN-CTX.md v1 -->";
293const PROJECT_LEAN_CTX_MD: &str = "LEAN-CTX.md";
294const PROJECT_AGENTS_MD: &str = "AGENTS.md";
295const AGENTS_BLOCK_START: &str = "<!-- lean-ctx -->";
296const AGENTS_BLOCK_END: &str = "<!-- /lean-ctx -->";
297
298fn ensure_project_agents_integration(cwd: &std::path::Path) {
299 let lean_ctx_md = cwd.join(PROJECT_LEAN_CTX_MD);
300 let desired = format!(
301 "{PROJECT_LEAN_CTX_MD_MARKER}\n{}\n",
302 crate::rules_inject::rules_dedicated_markdown()
303 );
304
305 if !lean_ctx_md.exists() {
306 write_file(&lean_ctx_md, &desired);
307 } else if std::fs::read_to_string(&lean_ctx_md)
308 .unwrap_or_default()
309 .contains(PROJECT_LEAN_CTX_MD_MARKER)
310 {
311 let current = std::fs::read_to_string(&lean_ctx_md).unwrap_or_default();
312 if !current.contains(crate::rules_inject::RULES_VERSION_STR) {
313 write_file(&lean_ctx_md, &desired);
314 }
315 }
316
317 let block = format!(
318 "{AGENTS_BLOCK_START}\n\
319## lean-ctx\n\n\
320Prefer lean-ctx MCP tools over native equivalents for token savings.\n\
321Full rules: @{PROJECT_LEAN_CTX_MD}\n\
322{AGENTS_BLOCK_END}\n"
323 );
324
325 let agents_md = cwd.join(PROJECT_AGENTS_MD);
326 if !agents_md.exists() {
327 let content = format!("# Agent Instructions\n\n{block}");
328 write_file(&agents_md, &content);
329 println!("Created AGENTS.md in project root (lean-ctx reference only).");
330 return;
331 }
332
333 let existing = std::fs::read_to_string(&agents_md).unwrap_or_default();
334 if existing.contains(AGENTS_BLOCK_START) {
335 let updated = replace_marked_block(&existing, AGENTS_BLOCK_START, AGENTS_BLOCK_END, &block);
336 if updated != existing {
337 write_file(&agents_md, &updated);
338 }
339 return;
340 }
341
342 if existing.contains("lean-ctx") && existing.contains(PROJECT_LEAN_CTX_MD) {
343 return;
344 }
345
346 let mut out = existing;
347 if !out.ends_with('\n') {
348 out.push('\n');
349 }
350 out.push('\n');
351 out.push_str(&block);
352 write_file(&agents_md, &out);
353 println!("Updated AGENTS.md (added lean-ctx reference block).");
354}
355
356fn replace_marked_block(content: &str, start: &str, end: &str, replacement: &str) -> String {
357 let s = content.find(start);
358 let e = content.find(end);
359 match (s, e) {
360 (Some(si), Some(ei)) if ei >= si => {
361 let after_end = ei + end.len();
362 let before = &content[..si];
363 let after = &content[after_end..];
364 let mut out = String::new();
365 out.push_str(before.trim_end_matches('\n'));
366 out.push('\n');
367 out.push('\n');
368 out.push_str(replacement.trim_end_matches('\n'));
369 out.push('\n');
370 out.push_str(after.trim_start_matches('\n'));
371 out
372 }
373 _ => content.to_string(),
374 }
375}
376
377const CURSORRULES_TEMPLATE: &str = "\
378# lean-ctx — Context Engineering Layer
379
380PREFER lean-ctx MCP tools over native equivalents for token savings:
381
382| PREFER | OVER | Why |
383|--------|------|-----|
384| `ctx_read(path)` | `Read` | Cached, 10 compression modes |
385| `ctx_shell(command)` | `Shell` | Pattern compression |
386| `ctx_search(pattern, path)` | `Grep` | Compact results |
387| `ctx_tree(path, depth)` | `ls` / `find` | Directory maps |
388| `ctx_edit(path, old_string, new_string)` | `Edit` (when Read unavailable) | Search-and-replace without native Read |
389
390Edit files: use native Edit/StrReplace if available. If Edit requires Read and Read is unavailable, use ctx_edit.
391Write, Delete, Glob — use normally. NEVER loop on Edit failures — switch to ctx_edit immediately.
392";
393
394pub const KIRO_STEERING_TEMPLATE: &str = "\
395---
396inclusion: always
397---
398
399# lean-ctx — Context Engineering Layer
400
401The workspace has the `lean-ctx` MCP server installed. You MUST prefer lean-ctx tools over native equivalents for token efficiency and caching.
402
403## Mandatory Tool Preferences
404
405| Use this | Instead of | Why |
406|----------|-----------|-----|
407| `mcp_lean_ctx_ctx_read` | `readFile`, `readCode` | Cached reads, 10 compression modes, re-reads cost ~13 tokens |
408| `mcp_lean_ctx_ctx_multi_read` | `readMultipleFiles` | Batch cached reads in one call |
409| `mcp_lean_ctx_ctx_shell` | `executeBash` | Pattern compression for git/npm/test output |
410| `mcp_lean_ctx_ctx_search` | `grepSearch` | Compact, .gitignore-aware results |
411| `mcp_lean_ctx_ctx_tree` | `listDirectory` | Compact directory maps with file counts |
412
413## When to use native Kiro tools instead
414
415- `fsWrite` / `fsAppend` — always use native (lean-ctx doesn't write files)
416- `strReplace` — always use native (precise string replacement)
417- `semanticRename` / `smartRelocate` — always use native (IDE integration)
418- `getDiagnostics` — always use native (language server diagnostics)
419- `deleteFile` — always use native
420
421## Session management
422
423- At the start of a long task, call `mcp_lean_ctx_ctx_preload` with a task description to warm the cache
424- Use `mcp_lean_ctx_ctx_compress` periodically in long conversations to checkpoint context
425- Use `mcp_lean_ctx_ctx_knowledge` to persist important discoveries across sessions
426
427## Rules
428
429- NEVER loop on edit failures — switch to `mcp_lean_ctx_ctx_edit` immediately
430- For large files, use `mcp_lean_ctx_ctx_read` with `mode: \"signatures\"` or `mode: \"map\"` first
431- For re-reading a file you already read, just call `mcp_lean_ctx_ctx_read` again (cache hit = ~13 tokens)
432- When running tests or build commands, use `mcp_lean_ctx_ctx_shell` for compressed output
433";
434
435pub fn install_agent_hook(agent: &str, global: bool) {
436 let home = crate::core::home::resolve_home_dir().unwrap_or_default();
437 match agent {
438 "claude" | "claude-code" => install_claude_hook(global),
439 "cursor" => install_cursor_hook(global),
440 "gemini" | "antigravity" => install_gemini_hook(),
441 "codex" => install_codex_hook(),
442 "windsurf" => install_windsurf_rules(global),
443 "cline" | "roo" => install_cline_rules(global),
444 "copilot" => install_copilot_hook(global),
445 "pi" => install_pi_hook(global),
446 "qwen" => install_mcp_json_agent(
447 "Qwen Code",
448 "~/.qwen/mcp.json",
449 &home.join(".qwen/mcp.json"),
450 ),
451 "trae" => install_mcp_json_agent("Trae", "~/.trae/mcp.json", &home.join(".trae/mcp.json")),
452 "amazonq" => install_mcp_json_agent(
453 "Amazon Q Developer",
454 "~/.aws/amazonq/mcp.json",
455 &home.join(".aws/amazonq/mcp.json"),
456 ),
457 "jetbrains" => install_jetbrains_hook(),
458 "kiro" => install_kiro_hook(),
459 "verdent" => install_mcp_json_agent(
460 "Verdent",
461 "~/.verdent/mcp.json",
462 &home.join(".verdent/mcp.json"),
463 ),
464 "opencode" => install_opencode_hook(),
465 "aider" => {
466 install_mcp_json_agent("Aider", "~/.aider/mcp.json", &home.join(".aider/mcp.json"));
467 }
468 "amp" => install_amp_hook(),
469 "crush" => install_crush_hook(),
470 "hermes" => install_hermes_hook(global),
471 _ => {
472 eprintln!("Unknown agent: {agent}");
473 eprintln!(" Supported: claude, cursor, gemini, codex, windsurf, cline, roo, copilot, pi, qwen, trae, amazonq, jetbrains, kiro, verdent, opencode, aider, amp, crush, antigravity, hermes");
474 std::process::exit(1);
475 }
476 }
477}
478
479fn write_file(path: &std::path::Path, content: &str) {
480 if let Err(e) = crate::config_io::write_atomic_with_backup(path, content) {
481 tracing::error!("Error writing {}: {e}", path.display());
482 }
483}
484
485fn is_inside_git_repo(path: &std::path::Path) -> bool {
486 let mut p = path;
487 loop {
488 if p.join(".git").exists() {
489 return true;
490 }
491 match p.parent() {
492 Some(parent) => p = parent,
493 None => return false,
494 }
495 }
496}
497
498#[cfg(unix)]
499fn make_executable(path: &PathBuf) {
500 use std::os::unix::fs::PermissionsExt;
501 let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755));
502}
503
504#[cfg(not(unix))]
505fn make_executable(_path: &PathBuf) {}
506
507fn full_server_entry(binary: &str) -> serde_json::Value {
508 let data_dir = crate::core::data_dir::lean_ctx_data_dir()
509 .map(|d| d.to_string_lossy().to_string())
510 .unwrap_or_default();
511 let auto_approve = crate::core::editor_registry::auto_approve_tools();
512 serde_json::json!({
513 "command": binary,
514 "env": { "LEAN_CTX_DATA_DIR": data_dir },
515 "autoApprove": auto_approve
516 })
517}
518
519fn install_mcp_json_agent(name: &str, display_path: &str, config_path: &std::path::Path) {
520 let binary = resolve_binary_path();
521 let entry = full_server_entry(&binary);
522 install_named_json_server(name, display_path, config_path, "mcpServers", entry);
523}
524
525#[cfg(test)]
526mod tests {
527 use super::*;
528
529 #[test]
530 fn bash_path_unix_unchanged() {
531 assert_eq!(
532 to_bash_compatible_path("/usr/local/bin/lean-ctx"),
533 "/usr/local/bin/lean-ctx"
534 );
535 }
536
537 #[test]
538 fn bash_path_home_unchanged() {
539 assert_eq!(
540 to_bash_compatible_path("/home/user/.cargo/bin/lean-ctx"),
541 "/home/user/.cargo/bin/lean-ctx"
542 );
543 }
544
545 #[test]
546 fn bash_path_windows_drive_converted() {
547 assert_eq!(
548 to_bash_compatible_path("C:\\Users\\Fraser\\bin\\lean-ctx.exe"),
549 "/c/Users/Fraser/bin/lean-ctx.exe"
550 );
551 }
552
553 #[test]
554 fn bash_path_windows_lowercase_drive() {
555 assert_eq!(
556 to_bash_compatible_path("D:\\tools\\lean-ctx.exe"),
557 "/d/tools/lean-ctx.exe"
558 );
559 }
560
561 #[test]
562 fn bash_path_windows_forward_slashes() {
563 assert_eq!(
564 to_bash_compatible_path("C:/Users/Fraser/bin/lean-ctx.exe"),
565 "/c/Users/Fraser/bin/lean-ctx.exe"
566 );
567 }
568
569 #[test]
570 fn bash_path_bare_name_unchanged() {
571 assert_eq!(to_bash_compatible_path("lean-ctx"), "lean-ctx");
572 }
573
574 #[test]
575 fn normalize_msys2_path() {
576 assert_eq!(
577 normalize_tool_path("/c/Users/game/Downloads/project"),
578 "C:/Users/game/Downloads/project"
579 );
580 }
581
582 #[test]
583 fn normalize_msys2_drive_d() {
584 assert_eq!(
585 normalize_tool_path("/d/Projects/app/src"),
586 "D:/Projects/app/src"
587 );
588 }
589
590 #[test]
591 fn normalize_backslashes() {
592 assert_eq!(
593 normalize_tool_path("C:\\Users\\game\\project\\src"),
594 "C:/Users/game/project/src"
595 );
596 }
597
598 #[test]
599 fn normalize_mixed_separators() {
600 assert_eq!(
601 normalize_tool_path("C:\\Users/game\\project/src"),
602 "C:/Users/game/project/src"
603 );
604 }
605
606 #[test]
607 fn normalize_double_slashes() {
608 assert_eq!(
609 normalize_tool_path("/home/user//project///src"),
610 "/home/user/project/src"
611 );
612 }
613
614 #[test]
615 fn normalize_trailing_slash() {
616 assert_eq!(
617 normalize_tool_path("/home/user/project/"),
618 "/home/user/project"
619 );
620 }
621
622 #[test]
623 fn normalize_root_preserved() {
624 assert_eq!(normalize_tool_path("/"), "/");
625 }
626
627 #[test]
628 fn normalize_windows_root_preserved() {
629 assert_eq!(normalize_tool_path("C:/"), "C:/");
630 }
631
632 #[test]
633 fn normalize_unix_path_unchanged() {
634 assert_eq!(
635 normalize_tool_path("/home/user/project/src/main.rs"),
636 "/home/user/project/src/main.rs"
637 );
638 }
639
640 #[test]
641 fn normalize_relative_path_unchanged() {
642 assert_eq!(normalize_tool_path("src/main.rs"), "src/main.rs");
643 }
644
645 #[test]
646 fn normalize_dot_unchanged() {
647 assert_eq!(normalize_tool_path("."), ".");
648 }
649
650 #[test]
651 fn normalize_unc_path_preserved() {
652 assert_eq!(
653 normalize_tool_path("//server/share/file"),
654 "//server/share/file"
655 );
656 }
657
658 #[test]
659 fn cursor_hook_config_has_version_and_object_hooks() {
660 let config = serde_json::json!({
661 "version": 1,
662 "hooks": {
663 "preToolUse": [
664 {
665 "matcher": "terminal_command",
666 "command": "lean-ctx hook rewrite"
667 },
668 {
669 "matcher": "read_file|grep|search|list_files|list_directory",
670 "command": "lean-ctx hook redirect"
671 }
672 ]
673 }
674 });
675
676 let json_str = serde_json::to_string_pretty(&config).unwrap();
677 let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
678
679 assert_eq!(parsed["version"], 1);
680 assert!(parsed["hooks"].is_object());
681 assert!(parsed["hooks"]["preToolUse"].is_array());
682 assert_eq!(parsed["hooks"]["preToolUse"].as_array().unwrap().len(), 2);
683 assert_eq!(
684 parsed["hooks"]["preToolUse"][0]["matcher"],
685 "terminal_command"
686 );
687 }
688
689 #[test]
690 fn cursor_hook_detects_old_format_needs_migration() {
691 let old_format = r#"{"hooks":[{"event":"preToolUse","command":"lean-ctx hook rewrite"}]}"#;
692 let has_correct =
693 old_format.contains("\"version\"") && old_format.contains("\"preToolUse\"");
694 assert!(
695 !has_correct,
696 "Old format should be detected as needing migration"
697 );
698 }
699
700 #[test]
701 fn gemini_hook_config_has_type_command() {
702 let binary = "lean-ctx";
703 let rewrite_cmd = format!("{binary} hook rewrite");
704 let redirect_cmd = format!("{binary} hook redirect");
705
706 let hook_config = serde_json::json!({
707 "hooks": {
708 "BeforeTool": [
709 {
710 "hooks": [{
711 "type": "command",
712 "command": rewrite_cmd
713 }]
714 },
715 {
716 "hooks": [{
717 "type": "command",
718 "command": redirect_cmd
719 }]
720 }
721 ]
722 }
723 });
724
725 let parsed = hook_config;
726 let before_tool = parsed["hooks"]["BeforeTool"].as_array().unwrap();
727 assert_eq!(before_tool.len(), 2);
728
729 let first_hook = &before_tool[0]["hooks"][0];
730 assert_eq!(first_hook["type"], "command");
731 assert_eq!(first_hook["command"], "lean-ctx hook rewrite");
732
733 let second_hook = &before_tool[1]["hooks"][0];
734 assert_eq!(second_hook["type"], "command");
735 assert_eq!(second_hook["command"], "lean-ctx hook redirect");
736 }
737
738 #[test]
739 fn gemini_hook_old_format_detected() {
740 let old_format = r#"{"hooks":{"BeforeTool":[{"command":"lean-ctx hook rewrite"}]}}"#;
741 let has_new = old_format.contains("hook rewrite")
742 && old_format.contains("hook redirect")
743 && old_format.contains("\"type\"");
744 assert!(!has_new, "Missing 'type' field should trigger migration");
745 }
746
747 #[test]
748 fn rewrite_script_uses_registry_pattern() {
749 let script = generate_rewrite_script("/usr/bin/lean-ctx");
750 assert!(script.contains(r"git\ *"), "script missing git pattern");
751 assert!(script.contains(r"cargo\ *"), "script missing cargo pattern");
752 assert!(script.contains(r"npm\ *"), "script missing npm pattern");
753 assert!(
754 !script.contains(r"rg\ *"),
755 "script should not contain rg pattern"
756 );
757 assert!(
758 script.contains("LEAN_CTX_BIN=\"/usr/bin/lean-ctx\""),
759 "script missing binary path"
760 );
761 }
762
763 #[test]
764 fn compact_rewrite_script_uses_registry_pattern() {
765 let script = generate_compact_rewrite_script("/usr/bin/lean-ctx");
766 assert!(script.contains(r"git\ *"), "compact script missing git");
767 assert!(script.contains(r"cargo\ *"), "compact script missing cargo");
768 assert!(
769 !script.contains(r"rg\ *"),
770 "compact script should not contain rg"
771 );
772 }
773
774 #[test]
775 fn rewrite_scripts_contain_all_registry_commands() {
776 let script = generate_rewrite_script("lean-ctx");
777 let compact = generate_compact_rewrite_script("lean-ctx");
778 for entry in crate::rewrite_registry::REWRITE_COMMANDS {
779 if entry.category == crate::rewrite_registry::Category::Search {
780 continue;
781 }
782 let pattern = if entry.command.contains('-') {
783 format!("{}*", entry.command.replace('-', r"\-"))
784 } else {
785 format!(r"{}\ *", entry.command)
786 };
787 assert!(
788 script.contains(&pattern),
789 "rewrite_script missing '{}' (pattern: {})",
790 entry.command,
791 pattern
792 );
793 assert!(
794 compact.contains(&pattern),
795 "compact_rewrite_script missing '{}' (pattern: {})",
796 entry.command,
797 pattern
798 );
799 }
800 }
801}