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
31pub mod ast_grep_lang;
47pub mod backup;
48pub mod bash_background;
49pub mod bash_permissions;
50pub mod bash_rewrite;
51pub mod callgraph;
52pub mod calls;
53pub mod checkpoint;
54pub mod commands;
55pub mod compress;
56pub mod config;
57pub mod context;
58pub mod edit;
59pub mod error;
60pub mod extract;
61pub mod format;
62pub mod fuzzy_match;
63pub mod imports;
64pub mod indent;
65pub mod language;
66pub mod lsp;
67pub mod lsp_hints;
68pub mod parser;
69pub mod protocol;
70pub mod search_index;
71pub mod semantic_index;
72pub mod symbols;
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use config::Config;
78 use error::AftError;
79 use protocol::{RawRequest, Response};
80
81 #[test]
84 fn raw_request_deserializes_ping() {
85 let json = r#"{"id":"1","command":"ping"}"#;
86 let req: RawRequest = serde_json::from_str(json).unwrap();
87 assert_eq!(req.id, "1");
88 assert_eq!(req.command, "ping");
89 assert!(req.lsp_hints.is_none());
90 }
91
92 #[test]
93 fn raw_request_deserializes_echo_with_params() {
94 let json = r#"{"id":"2","command":"echo","message":"hello"}"#;
95 let req: RawRequest = serde_json::from_str(json).unwrap();
96 assert_eq!(req.id, "2");
97 assert_eq!(req.command, "echo");
98 assert_eq!(req.params["message"], "hello");
100 }
101
102 #[test]
103 fn raw_request_preserves_unknown_fields() {
104 let json = r#"{"id":"3","command":"ping","future_field":"abc","nested":{"x":1}}"#;
105 let req: RawRequest = serde_json::from_str(json).unwrap();
106 assert_eq!(req.params["future_field"], "abc");
107 assert_eq!(req.params["nested"]["x"], 1);
108 }
109
110 #[test]
111 fn raw_request_with_lsp_hints() {
112 let json = r#"{"id":"4","command":"ping","lsp_hints":{"completions":["foo","bar"]}}"#;
113 let req: RawRequest = serde_json::from_str(json).unwrap();
114 assert!(req.lsp_hints.is_some());
115 let hints = req.lsp_hints.unwrap();
116 assert_eq!(hints["completions"][0], "foo");
117 }
118
119 #[test]
120 fn response_success_round_trip() {
121 let resp = Response::success("42", serde_json::json!({"command": "pong"}));
122 let json_str = serde_json::to_string(&resp).unwrap();
123 let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
124 assert_eq!(v["id"], "42");
125 assert_eq!(v["success"], true);
126 assert_eq!(v["command"], "pong");
127 }
128
129 #[test]
130 fn response_error_round_trip() {
131 let resp = Response::error("99", "unknown_command", "unknown command: foo");
132 let json_str = serde_json::to_string(&resp).unwrap();
133 let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
134 assert_eq!(v["id"], "99");
135 assert_eq!(v["success"], false);
136 assert_eq!(v["code"], "unknown_command");
137 assert_eq!(v["message"], "unknown command: foo");
138 }
139
140 #[test]
143 fn error_display_symbol_not_found() {
144 let err = AftError::SymbolNotFound {
145 name: "foo".into(),
146 file: "bar.rs".into(),
147 };
148 assert_eq!(err.to_string(), "symbol 'foo' not found in bar.rs");
149 assert_eq!(err.code(), "symbol_not_found");
150 }
151
152 #[test]
153 fn error_display_ambiguous_symbol() {
154 let err = AftError::AmbiguousSymbol {
155 name: "Foo".into(),
156 candidates: vec!["a.rs:10".into(), "b.rs:20".into()],
157 };
158 let s = err.to_string();
159 assert!(s.contains("Foo"));
160 assert!(s.contains("a.rs:10, b.rs:20"));
161 }
162
163 #[test]
164 fn error_display_parse_error() {
165 let err = AftError::ParseError {
166 message: "unexpected token".into(),
167 };
168 assert_eq!(err.to_string(), "parse error: unexpected token");
169 }
170
171 #[test]
172 fn error_display_file_not_found() {
173 let err = AftError::FileNotFound {
174 path: "/tmp/missing.rs".into(),
175 };
176 assert_eq!(err.to_string(), "file not found: /tmp/missing.rs");
177 }
178
179 #[test]
180 fn error_display_invalid_request() {
181 let err = AftError::InvalidRequest {
182 message: "missing field".into(),
183 };
184 assert_eq!(err.to_string(), "invalid request: missing field");
185 }
186
187 #[test]
188 fn error_display_checkpoint_not_found() {
189 let err = AftError::CheckpointNotFound {
190 name: "pre-refactor".into(),
191 };
192 assert_eq!(err.to_string(), "checkpoint not found: pre-refactor");
193 assert_eq!(err.code(), "checkpoint_not_found");
194 }
195
196 #[test]
197 fn error_display_no_undo_history() {
198 let err = AftError::NoUndoHistory {
199 path: "src/main.rs".into(),
200 };
201 assert_eq!(err.to_string(), "no undo history for: src/main.rs");
202 assert_eq!(err.code(), "no_undo_history");
203 }
204
205 #[test]
206 fn error_display_ambiguous_match() {
207 let err = AftError::AmbiguousMatch {
208 pattern: "TODO".into(),
209 count: 5,
210 };
211 assert_eq!(
212 err.to_string(),
213 "pattern 'TODO' matches 5 occurrences, expected exactly 1"
214 );
215 assert_eq!(err.code(), "ambiguous_match");
216 }
217
218 #[test]
219 fn error_display_project_too_large() {
220 let err = AftError::ProjectTooLarge {
221 count: 20001,
222 max: 20000,
223 };
224 assert_eq!(
225 err.to_string(),
226 "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."
227 );
228 assert_eq!(err.code(), "project_too_large");
229 }
230
231 #[test]
232 fn error_to_json_has_code_and_message() {
233 let err = AftError::FileNotFound { path: "/x".into() };
234 let j = err.to_error_json();
235 assert_eq!(j["code"], "file_not_found");
236 assert!(j["message"].as_str().unwrap().contains("/x"));
237 }
238
239 #[test]
242 fn config_default_values() {
243 let cfg = Config::default();
244 assert!(cfg.project_root.is_none());
245 assert_eq!(cfg.validation_depth, 1);
246 assert_eq!(cfg.checkpoint_ttl_hours, 24);
247 assert_eq!(cfg.max_symbol_depth, 10);
248 assert_eq!(cfg.formatter_timeout_secs, 10);
249 assert_eq!(cfg.type_checker_timeout_secs, 30);
250 assert_eq!(cfg.max_callgraph_files, 20_000);
251 }
252}