wanderlust 0.2.5

A powerful Windows PATH cleaner and healer. Automatically discovers tools, removes duplicates, and ensures system stability.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use anyhow::Result;
use std::path::Path;
use windows_registry::{CURRENT_USER, LOCAL_MACHINE};

/// Abstraction for System interactions (Registry, File System, Environment).
/// This allows production code to be isolated from dangerous Windows Registry interactions.
pub trait SystemOps {
    /// Read the current PATH from the Registry (User scope).
    fn read_user_path_registry(&self) -> Result<String>;

    /// Write the new PATH to the Registry (User scope).
    fn write_user_path_registry(&self, new_path: &str) -> Result<()>;

    /// Restore a previously read User PATH value. The default preserves the existing write behavior.
    fn restore_user_path_registry(&self, old_path: &str) -> Result<()> {
        self.write_user_path_registry(old_path)
    }

    /// Broadcast the "Environment Changed" message to the system.
    fn broadcast_environment_change(&self) -> Result<()>;

    /// Check if a directory exists on the file system.
    fn path_exists(&self, path: &Path) -> bool;

    /// Write a backup file to disk.
    fn write_backup_file(&self, path: &Path, content: &str) -> Result<()>;

    /// Run system verification probes (cmd, powershell) to ensure PATH is valid.
    fn verify_environment_health(&self) -> bool;

    /// Verify a planned effective PATH.
    fn verify_effective_path(&self, _effective_path: &str) -> Result<()> {
        if self.verify_environment_health() {
            Ok(())
        } else {
            Err(anyhow::anyhow!("environment health verification failed"))
        }
    }

    /// Read the System PATH from the Registry (Machine scope - HKLM).
    fn read_system_path_registry(&self) -> Result<String>;

    /// Write the System PATH to the Registry (Machine scope - HKLM).
    /// Requires Admin privileges.
    fn write_system_path_registry(&self, new_path: &str) -> Result<()>;
}

/// The Real System implementation (Production).
pub struct WindowsSystem;

impl SystemOps for WindowsSystem {
    fn read_user_path_registry(&self) -> Result<String> {
        let key = CURRENT_USER.open("Environment")?;
        let path_val = key.get_string("Path")?;
        Ok(path_val)
    }

    fn write_user_path_registry(&self, new_path: &str) -> Result<()> {
        let key = CURRENT_USER.create("Environment")?;
        key.set_string("Path", new_path)?;
        Ok(())
    }

    fn broadcast_environment_change(&self) -> Result<()> {
        use windows::Win32::Foundation::{LPARAM, WPARAM};
        use windows::Win32::UI::WindowsAndMessaging::{
            HWND_BROADCAST, SMTO_ABORTIFHUNG, SendMessageTimeoutA, WM_SETTINGCHANGE,
        };

        unsafe {
            let env_str = std::ffi::CString::new("Environment").unwrap();
            let mut result: usize = 0;
            SendMessageTimeoutA(
                HWND_BROADCAST,
                WM_SETTINGCHANGE,
                WPARAM(0),
                LPARAM(env_str.as_ptr() as isize),
                SMTO_ABORTIFHUNG,
                5000,
                Some(&mut result),
            );
        }
        Ok(())
    }

    fn path_exists(&self, path: &Path) -> bool {
        path.exists()
    }

    fn write_backup_file(&self, path: &Path, content: &str) -> Result<()> {
        use std::io::Write;
        let mut f = std::fs::File::create(path)?;
        f.write_all(content.as_bytes())?;
        Ok(())
    }

    fn verify_environment_health(&self) -> bool {
        let probes = vec!["cmd.exe /C ver", "powershell.exe -v", "whoami"];

        let mut success_count = 0;
        for cmd in &probes {
            let parts: Vec<&str> = cmd.split_whitespace().collect();
            let status = std::process::Command::new(parts[0])
                .args(&parts[1..])
                .output();
            if status.map(|s| s.status.success()).unwrap_or(false) {
                success_count += 1;
            }
        }

        success_count >= 2
    }

    fn read_system_path_registry(&self) -> Result<String> {
        let key =
            LOCAL_MACHINE.open(r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")?;
        let path_val = key.get_string("Path")?;
        Ok(path_val)
    }

    fn write_system_path_registry(&self, new_path: &str) -> Result<()> {
        let key = LOCAL_MACHINE
            .create(r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")?;
        key.set_string("Path", new_path)?;
        Ok(())
    }
}

/// Test-only system operation failure points.
#[cfg(test)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum MockOperation {
    ReadUserPath,
    WriteUserPath,
    RestoreUserPath,
    BroadcastEnvironmentChange,
    WriteBackup,
    VerifyEnvironment,
    VerifyEffectivePath,
    ReadSystemPath,
    WriteSystemPath,
}

/// Test-only record of a call made through [`MockSystem`].
#[cfg(test)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum MockCall {
    ReadUserPath,
    WriteUserPath(String),
    RestoreUserPath(String),
    BroadcastEnvironmentChange,
    WriteBackup {
        path: std::path::PathBuf,
        content: String,
    },
    VerifyEnvironment,
    VerifyEffectivePath(String),
    ReadSystemPath,
    WriteSystemPath(String),
}

/// A configurable, in-memory SystemOps implementation for isolated unit tests.
#[cfg(test)]
#[derive(Debug, Default)]
pub struct MockSystem {
    pub registry: std::sync::Mutex<std::collections::HashMap<String, String>>,
    pub file_system: std::sync::Mutex<Vec<std::path::PathBuf>>,
    pub broadcast_called: std::sync::Mutex<bool>,
    pub(crate) calls: std::sync::Mutex<Vec<MockCall>>,
    pub(crate) failures: std::sync::Mutex<
        std::collections::HashMap<MockOperation, std::collections::BTreeSet<usize>>,
    >,
    pub(crate) operation_counts: std::sync::Mutex<std::collections::HashMap<MockOperation, usize>>,
}

#[cfg(test)]
impl MockSystem {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_registry(key: &str, value: &str) -> Self {
        let mut map = std::collections::HashMap::new();
        map.insert(key.to_string(), value.to_string());
        Self {
            registry: std::sync::Mutex::new(map),
            ..Default::default()
        }
    }

    /// Fail the next invocation of `operation`.
    pub fn fail_next(&self, operation: MockOperation) {
        let next = self
            .operation_counts
            .lock()
            .unwrap()
            .get(&operation)
            .copied()
            .unwrap_or(0)
            + 1;
        self.fail_on_nth(operation, next);
    }

    /// Fail a specific one-based invocation of `operation`.
    pub fn fail_on_nth(&self, operation: MockOperation, invocation: usize) {
        assert!(invocation > 0, "mock operation invocations are one-based");
        self.failures
            .lock()
            .unwrap()
            .entry(operation)
            .or_default()
            .insert(invocation);
    }

    pub fn calls(&self) -> Vec<MockCall> {
        self.calls.lock().unwrap().clone()
    }

    pub fn call_count(&self, operation: MockOperation) -> usize {
        self.operation_counts
            .lock()
            .unwrap()
            .get(&operation)
            .copied()
            .unwrap_or(0)
    }

    fn record(&self, call: MockCall) {
        self.calls.lock().unwrap().push(call);
    }

    fn fail_if_configured(&self, operation: MockOperation) -> Result<()> {
        let invocation = {
            let mut counts = self.operation_counts.lock().unwrap();
            let count = counts.entry(operation).or_insert(0);
            *count += 1;
            *count
        };
        if self
            .failures
            .lock()
            .unwrap()
            .get(&operation)
            .is_some_and(|calls| calls.contains(&invocation))
        {
            Err(anyhow::anyhow!(
                "configured mock failure for {operation:?} invocation {invocation}"
            ))
        } else {
            Ok(())
        }
    }
}

#[cfg(test)]
impl SystemOps for MockSystem {
    fn read_user_path_registry(&self) -> Result<String> {
        self.record(MockCall::ReadUserPath);
        self.fail_if_configured(MockOperation::ReadUserPath)?;
        let map = self.registry.lock().unwrap();
        map.get("Path")
            .cloned()
            .ok_or_else(|| anyhow::anyhow!("Path not found in mock registry"))
    }

    fn write_user_path_registry(&self, new_path: &str) -> Result<()> {
        self.record(MockCall::WriteUserPath(new_path.to_string()));
        self.fail_if_configured(MockOperation::WriteUserPath)?;
        self.registry
            .lock()
            .unwrap()
            .insert("Path".to_string(), new_path.to_string());
        Ok(())
    }

    fn restore_user_path_registry(&self, old_path: &str) -> Result<()> {
        self.record(MockCall::RestoreUserPath(old_path.to_string()));
        self.fail_if_configured(MockOperation::RestoreUserPath)?;
        self.registry
            .lock()
            .unwrap()
            .insert("Path".to_string(), old_path.to_string());
        Ok(())
    }

    fn broadcast_environment_change(&self) -> Result<()> {
        self.record(MockCall::BroadcastEnvironmentChange);
        self.fail_if_configured(MockOperation::BroadcastEnvironmentChange)?;
        *self.broadcast_called.lock().unwrap() = true;
        Ok(())
    }

    fn path_exists(&self, path: &Path) -> bool {
        self.file_system
            .lock()
            .unwrap()
            .contains(&path.to_path_buf())
    }

    fn write_backup_file(&self, path: &Path, content: &str) -> Result<()> {
        self.record(MockCall::WriteBackup {
            path: path.to_path_buf(),
            content: content.to_string(),
        });
        self.fail_if_configured(MockOperation::WriteBackup)?;
        self.file_system.lock().unwrap().push(path.to_path_buf());
        Ok(())
    }

    fn verify_environment_health(&self) -> bool {
        self.record(MockCall::VerifyEnvironment);
        self.fail_if_configured(MockOperation::VerifyEnvironment)
            .is_ok()
    }

    fn verify_effective_path(&self, effective_path: &str) -> Result<()> {
        self.record(MockCall::VerifyEffectivePath(effective_path.to_string()));
        self.fail_if_configured(MockOperation::VerifyEffectivePath)
    }

    fn read_system_path_registry(&self) -> Result<String> {
        self.record(MockCall::ReadSystemPath);
        self.fail_if_configured(MockOperation::ReadSystemPath)?;
        let map = self.registry.lock().unwrap();
        map.get("SystemPath")
            .cloned()
            .ok_or_else(|| anyhow::anyhow!("SystemPath not found in mock registry"))
    }

    fn write_system_path_registry(&self, new_path: &str) -> Result<()> {
        self.record(MockCall::WriteSystemPath(new_path.to_string()));
        self.fail_if_configured(MockOperation::WriteSystemPath)?;
        self.registry
            .lock()
            .unwrap()
            .insert("SystemPath".to_string(), new_path.to_string());
        Ok(())
    }
}

/// An automatically removed, process-unique temporary directory for isolated tests.
#[cfg(test)]
#[derive(Debug)]
pub struct TestTempDir {
    path: std::path::PathBuf,
}

#[cfg(test)]
impl TestTempDir {
    pub fn new(label: &str) -> std::io::Result<Self> {
        use std::sync::atomic::{AtomicUsize, Ordering};
        static NEXT_ID: AtomicUsize = AtomicUsize::new(0);

        let label: String = label
            .chars()
            .map(|ch| if ch.is_ascii_alphanumeric() { ch } else { '-' })
            .collect();
        let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
        let path =
            std::env::temp_dir().join(format!("wanderlust-{label}-{}-{id}", std::process::id()));
        std::fs::create_dir(&path)?;
        Ok(Self { path })
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn child(&self, name: impl AsRef<Path>) -> std::path::PathBuf {
        self.path.join(name)
    }
}

#[cfg(test)]
impl Drop for TestTempDir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.path);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mock_system_records_configured_failure_points_and_inputs() {
        let system = MockSystem::with_registry("Path", "C:\\old");
        system.fail_on_nth(MockOperation::RestoreUserPath, 1);

        assert_eq!(system.read_user_path_registry().unwrap(), "C:\\old");
        system.write_user_path_registry("C:\\new").unwrap();
        system.broadcast_environment_change().unwrap();
        system.verify_effective_path("C:\\system;C:\\new").unwrap();
        assert!(system.restore_user_path_registry("C:\\old").is_err());

        assert_eq!(
            system.calls(),
            vec![
                MockCall::ReadUserPath,
                MockCall::WriteUserPath("C:\\new".to_string()),
                MockCall::BroadcastEnvironmentChange,
                MockCall::VerifyEffectivePath("C:\\system;C:\\new".to_string()),
                MockCall::RestoreUserPath("C:\\old".to_string()),
            ]
        );
    }

    #[test]
    fn temporary_directory_is_unique_and_removed_on_drop() {
        let path = {
            let temp_dir = TestTempDir::new("system seam").unwrap();
            let cache_file = temp_dir.child("cache.stage");
            std::fs::write(&cache_file, "staged").unwrap();
            assert!(cache_file.exists());
            temp_dir.path().to_path_buf()
        };

        assert!(!path.exists());
    }
}