Skip to main content

tracevault_cli/hooks/
mod.rs

1use std::path::Path;
2use tracevault_protocol::streaming::StreamEventRequest;
3
4// Adapter modules contain stubs for future stream command integration.
5#[allow(dead_code)]
6pub mod claude_code;
7#[allow(dead_code)]
8pub mod codex;
9#[allow(dead_code)]
10pub mod cursor;
11
12#[allow(dead_code)]
13pub trait HookAdapter: Send + Sync {
14    fn tool_name(&self) -> &str;
15    fn parse_event(&self, raw: &str) -> Result<StreamEventRequest, String>;
16    fn parse_transcript(&self, path: &Path) -> Result<Vec<serde_json::Value>, String>;
17}
18
19#[derive(Debug, Clone, Copy)]
20pub enum DetectedTool {
21    ClaudeCode,
22    Cursor,
23    Codex,
24}
25
26impl DetectedTool {
27    pub fn name(&self) -> &str {
28        match self {
29            DetectedTool::ClaudeCode => "claude-code",
30            DetectedTool::Cursor => "cursor",
31            DetectedTool::Codex => "codex",
32        }
33    }
34
35    #[allow(dead_code)]
36    pub fn adapter(&self) -> Box<dyn HookAdapter> {
37        match self {
38            DetectedTool::ClaudeCode => Box::new(claude_code::ClaudeCodeAdapter),
39            DetectedTool::Cursor => Box::new(cursor::CursorAdapter),
40            DetectedTool::Codex => Box::new(codex::CodexAdapter),
41        }
42    }
43}
44
45pub fn detect_tools(cwd: &Path) -> Vec<DetectedTool> {
46    let mut tools = vec![];
47    if cwd.join(".claude").exists() {
48        tools.push(DetectedTool::ClaudeCode);
49    }
50    if cwd.join(".cursor").exists() {
51        tools.push(DetectedTool::Cursor);
52    }
53    if cwd.join(".codex").exists() {
54        tools.push(DetectedTool::Codex);
55    }
56    tools
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62    use std::fs;
63
64    #[test]
65    fn detect_tools_claude_only() {
66        let dir = tempfile::tempdir().unwrap();
67        fs::create_dir(dir.path().join(".claude")).unwrap();
68        let tools = detect_tools(dir.path());
69        assert_eq!(tools.len(), 1);
70        assert!(matches!(tools[0], DetectedTool::ClaudeCode));
71    }
72
73    #[test]
74    fn detect_tools_neither() {
75        let dir = tempfile::tempdir().unwrap();
76        assert!(detect_tools(dir.path()).is_empty());
77    }
78
79    #[test]
80    fn detect_tools_both() {
81        let dir = tempfile::tempdir().unwrap();
82        fs::create_dir(dir.path().join(".claude")).unwrap();
83        fs::create_dir(dir.path().join(".cursor")).unwrap();
84        assert_eq!(detect_tools(dir.path()).len(), 2);
85    }
86
87    #[test]
88    fn detect_tools_codex() {
89        let dir = tempfile::tempdir().unwrap();
90        fs::create_dir(dir.path().join(".codex")).unwrap();
91        let tools = detect_tools(dir.path());
92        assert!(tools.iter().any(|t| matches!(t, DetectedTool::Codex)));
93    }
94
95    #[test]
96    fn codex_adapter_reports_codex_tool_name() {
97        // Guard against the Codex detection borrowing the Claude adapter's
98        // "claude-code" label if `adapter()` is ever wired into the live path.
99        assert_eq!(DetectedTool::Codex.adapter().tool_name(), "codex");
100        assert_eq!(
101            DetectedTool::ClaudeCode.adapter().tool_name(),
102            "claude-code"
103        );
104    }
105}