Skip to main content

aft_base/
lib.rs

1#![allow(
2    clippy::collapsible_if,
3    clippy::collapsible_match,
4    clippy::double_ended_iterator_last,
5    clippy::int_plus_one,
6    clippy::large_enum_variant,
7    clippy::len_without_is_empty,
8    clippy::let_and_return,
9    clippy::manual_contains,
10    clippy::manual_pattern_char_comparison,
11    clippy::manual_repeat_n,
12    clippy::manual_strip,
13    clippy::manual_unwrap_or_default,
14    clippy::map_clone,
15    clippy::iter_kv_map,
16    clippy::needless_borrow,
17    clippy::needless_borrows_for_generic_args,
18    clippy::needless_range_loop,
19    clippy::new_without_default,
20    clippy::obfuscated_if_else,
21    clippy::ptr_arg,
22    clippy::question_mark,
23    clippy::same_item_push,
24    clippy::should_implement_trait,
25    clippy::single_match,
26    clippy::too_many_arguments,
27    clippy::type_complexity,
28    clippy::unnecessary_sort_by,
29    clippy::unnecessary_cast,
30    clippy::unnecessary_lazy_evaluations,
31    clippy::unnecessary_map_or
32)]
33// Mirror the main crate's lint policy so moved code retains its lint baseline.
34
35// Private fixture helpers can compile with test-support without their unit tests. Their local
36// non-test dead-code allowances keep them private while preserving diagnostics in the unit harness.
37// Keep one item body and its original production visibility while sharing it with higher-layer tests.
38macro_rules! test_support_visibility {
39    ($(#[$attribute:meta])* $visibility:vis $keyword:ident $($item:tt)*) => {
40        #[cfg(any(test, feature = "test-support"))]
41        $(#[$attribute])*
42        pub $keyword $($item)*
43
44        #[cfg(not(any(test, feature = "test-support")))]
45        $(#[$attribute])*
46        $visibility $keyword $($item)*
47    };
48}
49
50pub mod agent_commands;
51pub mod alert_records;
52pub mod alloc_trace;
53pub mod atomic_replace;
54pub mod bash_env;
55pub mod bounded_io;
56pub mod build_state;
57pub mod cache_freshness;
58pub mod cache_location;
59pub mod clock;
60pub mod cold_build_limiter;
61pub mod compress;
62pub mod config;
63pub mod config_fix;
64pub mod config_resolve;
65pub mod config_sources;
66pub mod cross_volume;
67pub mod developer_tools;
68pub mod dir_identity;
69pub mod disk_floor;
70pub mod durability;
71pub mod durable_dir;
72pub mod effective_path;
73pub mod engram_catalog;
74#[doc(hidden)]
75pub mod environment;
76pub mod error;
77#[cfg(any(test, feature = "test-support"))]
78#[path = "../tests/helpers/executable_cache.rs"]
79pub mod executable_cache;
80pub mod executor;
81pub mod feature_config;
82pub mod file_identity;
83pub mod file_kind;
84pub mod fleet_status;
85pub mod fs_lock;
86pub mod gh_shim_paths;
87pub mod git_head;
88pub mod git_probe;
89pub mod git_revision;
90pub mod git_root_resolution;
91pub mod github_address;
92pub mod grammar_state;
93pub mod harness;
94pub mod harness_partition_guard;
95pub mod host_tool_names;
96pub mod inspect_category;
97pub mod io_accounting;
98pub mod jsonc;
99pub mod jsonc_edit;
100pub mod lang_id;
101pub mod language;
102pub mod large_read_counts;
103#[doc(hidden)]
104pub mod launch_fds;
105pub mod launch_nonce;
106pub mod list_envelope;
107pub mod list_surfaces;
108pub mod live_db_files;
109pub mod lock_diagnostics;
110pub mod log_ctx;
111pub mod log_redact;
112pub mod logging;
113pub mod memory;
114pub mod memory_profile;
115pub mod registration;
116
117pub mod native_stat;
118pub mod ndjson_text;
119pub mod no_follow_file;
120pub mod ort_lifecycle;
121pub mod ort_pin;
122pub mod os_metadata;
123pub mod path_identity;
124pub mod pattern_compile;
125pub mod pending_notices;
126pub mod pending_response;
127pub mod persistence_gate;
128pub mod pinned_dir;
129pub mod pinned_executable;
130pub mod platform_tls;
131pub mod policy_path;
132#[cfg(unix)]
133pub mod posix_spawn;
134pub mod prefrontal;
135pub mod privacy_spawn;
136#[doc(hidden)]
137pub mod private_storage;
138pub mod process_group;
139pub mod process_identity;
140pub mod process_io;
141pub mod process_spawn;
142pub mod production_storage;
143pub mod project_content;
144pub mod project_walk;
145pub mod protocol;
146pub mod pty_render;
147pub mod query_shape;
148pub mod read_policy;
149pub mod runner_status_state;
150pub mod scope_watch_events;
151pub mod secret;
152pub mod secret_proxy;
153pub mod shared_timer;
154pub mod shell_format;
155pub mod stage_timer;
156pub mod storage_path;
157pub mod subc_config;
158#[cfg(any(test, feature = "test-support"))]
159pub mod subc_fake;
160pub mod symbols;
161pub mod system_temp;
162pub mod task_temp_reads;
163pub mod task_watch_lines;
164pub mod task_watches;
165#[cfg(any(test, feature = "test-support"))]
166pub mod test_allocations;
167#[cfg(any(test, feature = "test-support"))]
168pub mod test_env;
169#[cfg(any(test, feature = "test-support"))]
170pub mod test_gate;
171#[cfg(all(any(test, feature = "test-support"), windows))]
172pub mod test_read_denial;
173#[cfg(any(test, feature = "test-support"))]
174pub mod test_storage;
175#[cfg(any(test, feature = "test-support"))]
176pub mod test_support;
177pub mod tool_gate;
178pub mod tool_path;
179pub mod tool_progress;
180pub mod url_fetch;
181pub mod view_storage_reserve;
182pub mod volume;
183pub mod walk_boundary;
184#[cfg(any(test, feature = "test-support"))]
185pub mod walk_profile;
186pub mod watcher_markers;
187pub mod windows_command;
188pub mod windows_path;
189pub mod worker_jail_canary;
190pub mod write_counters;
191pub mod write_intent;
192
193#[cfg(test)]
194mod tests {
195    use super::*;
196    use config::Config;
197    use error::AftError;
198    use protocol::{RawRequest, Response};
199
200    // --- Protocol serialization ---
201
202    #[test]
203    fn raw_request_deserializes_ping() {
204        let json = r#"{"id":"1","command":"ping"}"#;
205        let req: RawRequest = serde_json::from_str(json).unwrap();
206        assert_eq!(req.id, "1");
207        assert_eq!(req.command, "ping");
208        assert!(req.lsp_hints.is_none());
209    }
210
211    #[test]
212    fn raw_request_deserializes_echo_with_params() {
213        let json = r#"{"id":"2","command":"echo","message":"hello"}"#;
214        let req: RawRequest = serde_json::from_str(json).unwrap();
215        assert_eq!(req.id, "2");
216        assert_eq!(req.command, "echo");
217        // "message" is captured in the flattened params
218        assert_eq!(req.params["message"], "hello");
219    }
220
221    #[test]
222    fn raw_request_preserves_unknown_fields() {
223        let json = r#"{"id":"3","command":"ping","future_field":"abc","nested":{"x":1}}"#;
224        let req: RawRequest = serde_json::from_str(json).unwrap();
225        assert_eq!(req.params["future_field"], "abc");
226        assert_eq!(req.params["nested"]["x"], 1);
227    }
228
229    #[test]
230    fn raw_request_with_lsp_hints() {
231        let json = r#"{"id":"4","command":"ping","lsp_hints":{"completions":["foo","bar"]}}"#;
232        let req: RawRequest = serde_json::from_str(json).unwrap();
233        assert!(req.lsp_hints.is_some());
234        let hints = req.lsp_hints.unwrap();
235        assert_eq!(hints["completions"][0], "foo");
236    }
237
238    #[test]
239    fn response_success_round_trip() {
240        let resp = Response::success("42", serde_json::json!({"command": "pong"}));
241        let json_str = serde_json::to_string(&resp).unwrap();
242        let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
243        assert_eq!(v["id"], "42");
244        assert_eq!(v["success"], true);
245        assert_eq!(v["command"], "pong");
246    }
247
248    #[test]
249    fn response_error_round_trip() {
250        let resp = Response::error("99", "unknown_command", "unknown command: foo");
251        let json_str = serde_json::to_string(&resp).unwrap();
252        let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
253        assert_eq!(v["id"], "99");
254        assert_eq!(v["success"], false);
255        assert_eq!(v["code"], "unknown_command");
256        assert_eq!(v["message"], "unknown command: foo");
257    }
258
259    // --- Error formatting ---
260
261    #[test]
262    fn error_display_symbol_not_found() {
263        let err = AftError::SymbolNotFound {
264            name: "foo".into(),
265            file: "bar.rs".into(),
266        };
267        assert_eq!(err.to_string(), "symbol 'foo' not found in bar.rs");
268        assert_eq!(err.code(), "symbol_not_found");
269    }
270
271    #[test]
272    fn error_display_ambiguous_symbol() {
273        let err = AftError::AmbiguousSymbol {
274            name: "Foo".into(),
275            candidates: vec!["a.rs:10".into(), "b.rs:20".into()],
276        };
277        let s = err.to_string();
278        assert!(s.contains("Foo"));
279        assert!(s.contains("a.rs:10, b.rs:20"));
280    }
281
282    #[test]
283    fn error_display_parse_error() {
284        let err = AftError::ParseError {
285            message: "unexpected token".into(),
286        };
287        assert_eq!(err.to_string(), "parse error: unexpected token");
288    }
289
290    #[test]
291    fn error_display_file_not_found() {
292        let err = AftError::FileNotFound {
293            path: "/tmp/missing.rs".into(),
294        };
295        assert_eq!(err.to_string(), "file not found: /tmp/missing.rs");
296    }
297
298    #[test]
299    fn error_display_invalid_request() {
300        let err = AftError::InvalidRequest {
301            message: "missing field".into(),
302        };
303        assert_eq!(err.to_string(), "invalid request: missing field");
304    }
305
306    #[test]
307    fn error_display_checkpoint_not_found() {
308        let err = AftError::CheckpointNotFound {
309            name: "pre-refactor".into(),
310        };
311        assert_eq!(err.to_string(), "checkpoint not found: pre-refactor");
312        assert_eq!(err.code(), "checkpoint_not_found");
313    }
314
315    #[test]
316    fn error_display_no_undo_history() {
317        let err = AftError::NoUndoHistory {
318            path: "src/main.rs".into(),
319        };
320        assert_eq!(err.to_string(), "no undo history for: src/main.rs");
321        assert_eq!(err.code(), "no_undo_history");
322    }
323
324    #[test]
325    fn error_display_ambiguous_match() {
326        let err = AftError::AmbiguousMatch {
327            pattern: "TODO".into(),
328            count: 5,
329        };
330        assert_eq!(
331            err.to_string(),
332            "pattern 'TODO' matches 5 occurrences, expected exactly 1"
333        );
334        assert_eq!(err.code(), "ambiguous_match");
335    }
336
337    #[test]
338    fn error_to_json_has_code_and_message() {
339        let err = AftError::FileNotFound { path: "/x".into() };
340        let j = err.to_error_json();
341        assert_eq!(j["code"], "file_not_found");
342        assert!(j["message"].as_str().unwrap().contains("/x"));
343    }
344
345    // --- Config defaults ---
346
347    #[test]
348    fn config_default_values() {
349        let cfg = Config::default();
350        assert!(cfg.project_root.is_none());
351        assert_eq!(cfg.validation_depth, 1);
352        assert_eq!(cfg.checkpoint_ttl_hours, 24);
353        assert_eq!(cfg.max_symbol_depth, 10);
354        assert_eq!(cfg.formatter_timeout_secs, 10);
355        assert_eq!(cfg.type_checker_timeout_secs, 30);
356    }
357}
358
359#[cfg(any(test, feature = "test-support"))]
360pub mod search_hot_path_measurements;
361
362#[cfg(any(test, feature = "test-support"))]
363#[cfg_attr(not(test), allow(dead_code, unused_imports))]
364pub mod nested_worktree_tests;
365
366#[cfg(all(any(test, feature = "test-support"), unix))]
367#[cfg_attr(not(test), allow(dead_code, unused_imports))]
368pub mod storage_permissions_tests;
369
370#[cfg(all(any(test, feature = "test-support"), target_os = "macos"))]
371#[cfg_attr(not(test), allow(dead_code, unused_imports))]
372pub mod disk_write_hunt;