Skip to main content

aft/
lib.rs

1#![allow(
2    clippy::collapsible_if,
3    clippy::double_ended_iterator_last,
4    clippy::int_plus_one,
5    clippy::large_enum_variant,
6    clippy::len_without_is_empty,
7    clippy::let_and_return,
8    clippy::manual_contains,
9    clippy::manual_pattern_char_comparison,
10    clippy::manual_repeat_n,
11    clippy::manual_strip,
12    clippy::manual_unwrap_or_default,
13    clippy::map_clone,
14    clippy::needless_borrow,
15    clippy::needless_borrows_for_generic_args,
16    clippy::needless_range_loop,
17    clippy::new_without_default,
18    clippy::obfuscated_if_else,
19    clippy::ptr_arg,
20    clippy::question_mark,
21    clippy::same_item_push,
22    clippy::should_implement_trait,
23    clippy::single_match,
24    clippy::too_many_arguments,
25    clippy::type_complexity,
26    clippy::unnecessary_cast,
27    clippy::unnecessary_lazy_evaluations,
28    clippy::unnecessary_map_or
29)]
30
31// ## Note on `.unwrap()` / `.expect()` usage
32//
33// The remaining `.unwrap()` and `.expect()` calls in `src/` are in:
34// - **Tree-sitter query operations** (parser.rs, zoom.rs, extract.rs, inline.rs,
35//   outline.rs): These operate on AFT's own compiled grammars and query patterns, which
36//   are compile-time constants. Pattern captures and node kinds are guaranteed to exist.
37// - **Checkpoint serialization** (checkpoint.rs): serde_json::to_value on known-good
38//   HashMap<PathBuf, String> types cannot fail.
39// - **lib.rs main loop**: JSON parsing of stdin lines — a malformed line is logged and
40//   skipped, not unwrapped.
41//
42// All production command handlers that process user/agent input return Result or
43// Response::error instead of panicking. Confirmed zero .unwrap()/.expect() in
44// production error paths as of v0.6.3 audit.
45
46pub mod ast_grep_hints;
47pub mod ast_grep_lang;
48pub mod backup;
49pub mod bash_background;
50pub mod bash_permissions;
51pub mod bash_rewrite;
52pub mod callgraph;
53pub mod calls;
54pub mod checkpoint;
55pub mod commands;
56pub mod compress;
57pub mod config;
58pub mod context;
59pub mod edit;
60pub mod error;
61pub mod extract;
62pub mod format;
63pub mod fuzzy_match;
64pub mod imports;
65pub mod indent;
66pub mod language;
67pub mod log_ctx;
68pub mod lsp;
69pub mod lsp_hints;
70pub mod parser;
71pub mod protocol;
72pub mod search_index;
73pub mod semantic_index;
74pub mod symbol_cache_disk;
75pub mod symbols;
76// Compiled on all platforms so cross-platform unit tests in
77// `commands::bash::try_spawn_with_fallback` can exercise the retry
78// decision logic without a real Windows runtime. The module itself only
79// uses portable APIs; only its callers are Windows-gated.
80pub mod windows_shell;
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85    use config::Config;
86    use error::AftError;
87    use protocol::{RawRequest, Response};
88
89    // --- Protocol serialization ---
90
91    #[test]
92    fn raw_request_deserializes_ping() {
93        let json = r#"{"id":"1","command":"ping"}"#;
94        let req: RawRequest = serde_json::from_str(json).unwrap();
95        assert_eq!(req.id, "1");
96        assert_eq!(req.command, "ping");
97        assert!(req.lsp_hints.is_none());
98    }
99
100    #[test]
101    fn raw_request_deserializes_echo_with_params() {
102        let json = r#"{"id":"2","command":"echo","message":"hello"}"#;
103        let req: RawRequest = serde_json::from_str(json).unwrap();
104        assert_eq!(req.id, "2");
105        assert_eq!(req.command, "echo");
106        // "message" is captured in the flattened params
107        assert_eq!(req.params["message"], "hello");
108    }
109
110    #[test]
111    fn raw_request_preserves_unknown_fields() {
112        let json = r#"{"id":"3","command":"ping","future_field":"abc","nested":{"x":1}}"#;
113        let req: RawRequest = serde_json::from_str(json).unwrap();
114        assert_eq!(req.params["future_field"], "abc");
115        assert_eq!(req.params["nested"]["x"], 1);
116    }
117
118    #[test]
119    fn raw_request_with_lsp_hints() {
120        let json = r#"{"id":"4","command":"ping","lsp_hints":{"completions":["foo","bar"]}}"#;
121        let req: RawRequest = serde_json::from_str(json).unwrap();
122        assert!(req.lsp_hints.is_some());
123        let hints = req.lsp_hints.unwrap();
124        assert_eq!(hints["completions"][0], "foo");
125    }
126
127    #[test]
128    fn response_success_round_trip() {
129        let resp = Response::success("42", serde_json::json!({"command": "pong"}));
130        let json_str = serde_json::to_string(&resp).unwrap();
131        let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
132        assert_eq!(v["id"], "42");
133        assert_eq!(v["success"], true);
134        assert_eq!(v["command"], "pong");
135    }
136
137    #[test]
138    fn response_error_round_trip() {
139        let resp = Response::error("99", "unknown_command", "unknown command: foo");
140        let json_str = serde_json::to_string(&resp).unwrap();
141        let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
142        assert_eq!(v["id"], "99");
143        assert_eq!(v["success"], false);
144        assert_eq!(v["code"], "unknown_command");
145        assert_eq!(v["message"], "unknown command: foo");
146    }
147
148    // --- Error formatting ---
149
150    #[test]
151    fn error_display_symbol_not_found() {
152        let err = AftError::SymbolNotFound {
153            name: "foo".into(),
154            file: "bar.rs".into(),
155        };
156        assert_eq!(err.to_string(), "symbol 'foo' not found in bar.rs");
157        assert_eq!(err.code(), "symbol_not_found");
158    }
159
160    #[test]
161    fn error_display_ambiguous_symbol() {
162        let err = AftError::AmbiguousSymbol {
163            name: "Foo".into(),
164            candidates: vec!["a.rs:10".into(), "b.rs:20".into()],
165        };
166        let s = err.to_string();
167        assert!(s.contains("Foo"));
168        assert!(s.contains("a.rs:10, b.rs:20"));
169    }
170
171    #[test]
172    fn error_display_parse_error() {
173        let err = AftError::ParseError {
174            message: "unexpected token".into(),
175        };
176        assert_eq!(err.to_string(), "parse error: unexpected token");
177    }
178
179    #[test]
180    fn error_display_file_not_found() {
181        let err = AftError::FileNotFound {
182            path: "/tmp/missing.rs".into(),
183        };
184        assert_eq!(err.to_string(), "file not found: /tmp/missing.rs");
185    }
186
187    #[test]
188    fn error_display_invalid_request() {
189        let err = AftError::InvalidRequest {
190            message: "missing field".into(),
191        };
192        assert_eq!(err.to_string(), "invalid request: missing field");
193    }
194
195    #[test]
196    fn error_display_checkpoint_not_found() {
197        let err = AftError::CheckpointNotFound {
198            name: "pre-refactor".into(),
199        };
200        assert_eq!(err.to_string(), "checkpoint not found: pre-refactor");
201        assert_eq!(err.code(), "checkpoint_not_found");
202    }
203
204    #[test]
205    fn error_display_no_undo_history() {
206        let err = AftError::NoUndoHistory {
207            path: "src/main.rs".into(),
208        };
209        assert_eq!(err.to_string(), "no undo history for: src/main.rs");
210        assert_eq!(err.code(), "no_undo_history");
211    }
212
213    #[test]
214    fn error_display_ambiguous_match() {
215        let err = AftError::AmbiguousMatch {
216            pattern: "TODO".into(),
217            count: 5,
218        };
219        assert_eq!(
220            err.to_string(),
221            "pattern 'TODO' matches 5 occurrences, expected exactly 1"
222        );
223        assert_eq!(err.code(), "ambiguous_match");
224    }
225
226    #[test]
227    fn error_display_project_too_large() {
228        let err = AftError::ProjectTooLarge {
229            count: 20001,
230            max: 20000,
231        };
232        assert_eq!(
233            err.to_string(),
234            "project has 20001 source files, exceeding max_callgraph_files=20000. Call-graph operations (callers, trace_to, trace_data, impact) are disabled for this root. Open a specific subdirectory or raise max_callgraph_files in config."
235        );
236        assert_eq!(err.code(), "project_too_large");
237    }
238
239    #[test]
240    fn error_to_json_has_code_and_message() {
241        let err = AftError::FileNotFound { path: "/x".into() };
242        let j = err.to_error_json();
243        assert_eq!(j["code"], "file_not_found");
244        assert!(j["message"].as_str().unwrap().contains("/x"));
245    }
246
247    // --- Config defaults ---
248
249    #[test]
250    fn config_default_values() {
251        let cfg = Config::default();
252        assert!(cfg.project_root.is_none());
253        assert_eq!(cfg.validation_depth, 1);
254        assert_eq!(cfg.checkpoint_ttl_hours, 24);
255        assert_eq!(cfg.max_symbol_depth, 10);
256        assert_eq!(cfg.formatter_timeout_secs, 10);
257        assert_eq!(cfg.type_checker_timeout_secs, 30);
258        assert_eq!(cfg.max_callgraph_files, 5_000);
259    }
260}