whi 0.5.0

Stupid simple PATH management
Documentation
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
use std::env;
use std::fs;
use std::io::ErrorKind;
use std::path::PathBuf;

#[cfg(unix)]
use std::os::unix::fs::DirBuilderExt;

use crate::system;

/// Get or create session directory (user-specific, secure)
fn get_session_dir() -> Result<PathBuf, String> {
    // Try XDG_RUNTIME_DIR first (standard for user-specific runtime files)
    let base_dir = env::var("XDG_RUNTIME_DIR")
        .or_else(|_| env::var("TMPDIR"))
        .unwrap_or_else(|_| "/tmp".to_string());

    // Use UID for additional security
    let uid = system::get_user_id().map_err(|e| format!("Failed to get user ID: {e}"))?;

    let session_dir = PathBuf::from(format!("{base_dir}/whi-{uid}"));

    // Create directory with restrictive permissions (0700) if it doesn't exist
    if !session_dir.exists() {
        #[cfg(unix)]
        {
            if let Err(e) = fs::DirBuilder::new().mode(0o700).create(&session_dir) {
                if e.kind() != ErrorKind::AlreadyExists {
                    return Err(format!("Failed to create session dir: {e}"));
                }
            }
        }

        #[cfg(not(unix))]
        {
            if let Err(e) = fs::create_dir_all(&session_dir) {
                if e.kind() != ErrorKind::AlreadyExists {
                    return Err(format!("Failed to create session dir: {}", e));
                }
            }
        }
    }

    Ok(session_dir)
}

/// Get path to session log file for given `PID`
pub fn get_session_file(pid: u32) -> Result<PathBuf, String> {
    let session_dir = get_session_dir()?;
    Ok(session_dir.join(format!("session_{pid}.log")))
}

/// Write `PATH` snapshot to session log
pub fn write_path_snapshot(pid: u32, path_string: &str) -> Result<(), String> {
    crate::history::HistoryContext::global(pid)?.write_snapshot(path_string)
}

/// Read all `PATH` snapshots from session log
pub fn read_path_snapshots(pid: u32) -> Result<Vec<String>, String> {
    crate::history::HistoryContext::global(pid)?.read_snapshots()
}

/// Get the initial `PATH` snapshot (first snapshot in session)
pub fn get_initial_path(pid: u32) -> Result<Option<String>, String> {
    crate::history::HistoryContext::global(pid)?.initial_snapshot()
}

/// Truncate snapshots to keep only the first `keep_count` snapshots
/// This is used by undo/reset to discard "future" snapshots from abandoned timelines
pub fn truncate_snapshots(pid: u32, keep_count: usize) -> Result<(), String> {
    crate::history::HistoryContext::global(pid)?.truncate(keep_count)
}

/// Get cursor file path for given `PID`
/// Get current cursor position (index into snapshots)
/// Returns `None` if at end of history (no cursor file = at latest)
pub fn get_cursor(pid: u32) -> Result<Option<usize>, String> {
    crate::history::HistoryContext::global(pid)?.get_cursor()
}

/// Set cursor position (index into snapshots)
pub fn set_cursor(pid: u32, position: usize) -> Result<(), String> {
    crate::history::HistoryContext::global(pid)?.set_cursor(position)
}

/// Clear cursor (move to end of history)
pub fn clear_cursor(pid: u32) -> Result<(), String> {
    crate::history::HistoryContext::global(pid)?.clear_cursor()
}

/// Get current `PATH` snapshot based on cursor position
pub fn get_current_snapshot(pid: u32) -> Result<Option<String>, String> {
    crate::history::HistoryContext::global(pid)?.current_snapshot()
}

/// Clear the session log for given `PID`
pub fn clear_session(pid: u32) -> Result<(), String> {
    crate::history::HistoryContext::global(pid)?.clear_history()
}

/// Get all session files in the session directory
fn get_all_session_files() -> Result<Vec<(PathBuf, std::time::SystemTime)>, String> {
    let session_dir = get_session_dir()?;

    if !session_dir.exists() {
        return Ok(Vec::new());
    }

    let entries =
        fs::read_dir(&session_dir).map_err(|e| format!("Failed to read session directory: {e}"))?;

    let mut session_files = Vec::new();

    for entry in entries.flatten() {
        let path = entry.path();
        if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
            if name.starts_with("session_") && path.extension().is_some_and(|ext| ext == "log") {
                if let Ok(metadata) = entry.metadata() {
                    if let Ok(modified) = metadata.modified() {
                        session_files.push((path, modified));
                    }
                }
            }
        }
    }

    Ok(session_files)
}

/// Cleanup old session files (round robin at >30 files)
/// Returns the number of files cleaned up
pub fn cleanup_old_sessions() -> Result<usize, String> {
    let mut session_files = get_all_session_files()?;

    if session_files.len() <= 30 {
        return Ok(0);
    }

    // Sort by modification time (oldest first)
    session_files.sort_by(|a, b| a.1.cmp(&b.1));

    // Delete oldest files until we have 30 or fewer
    let files_to_delete = session_files.len() - 30;
    let mut deleted_count = 0;

    for (path, _) in session_files.iter().take(files_to_delete) {
        if fs::remove_file(path).is_ok() {
            deleted_count += 1;
        }
    }

    Ok(deleted_count)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use std::sync::{Mutex, MutexGuard, OnceLock};
    use tempfile::TempDir;

    static TEST_ENV_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

    struct SessionTempDir {
        _dir: TempDir,
        old_tmp: Option<String>,
        _guard: MutexGuard<'static, ()>,
    }

    impl SessionTempDir {
        fn new() -> Self {
            let guard = TEST_ENV_LOCK
                .get_or_init(|| Mutex::new(()))
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            let dir = TempDir::new().unwrap();
            let old_xdg = env::var("XDG_RUNTIME_DIR").ok();
            env::set_var("XDG_RUNTIME_DIR", dir.path());
            Self {
                _dir: dir,
                old_tmp: old_xdg,
                _guard: guard,
            }
        }
    }

    impl Drop for SessionTempDir {
        fn drop(&mut self) {
            if let Some(ref value) = self.old_tmp {
                env::set_var("XDG_RUNTIME_DIR", value);
            } else {
                env::remove_var("XDG_RUNTIME_DIR");
            }
        }
    }

    #[test]
    fn test_session_file_path() {
        let _guard = SessionTempDir::new();
        let path = get_session_file(12345).unwrap();
        let path_str = path.to_string_lossy();
        assert!(
            path_str.contains("whi-")
                && path.extension().is_some_and(|ext| ext == "log")
                && path_str.contains("session_12345"),
            "Session file path should be in user-specific directory: {}",
            path_str
        );
    }

    #[test]
    fn test_session_dir_creation() {
        let _guard = SessionTempDir::new();
        // Session directory should be created on first access
        let dir = get_session_dir().unwrap();
        assert!(dir.exists(), "Session directory should exist");

        // Verify it's a directory
        assert!(dir.is_dir(), "Session path should be a directory");
    }

    #[cfg(unix)]
    #[test]
    fn test_session_file_permissions() {
        use std::os::unix::fs::PermissionsExt;

        let _guard = SessionTempDir::new();
        let pid = 999001;
        let _ = clear_session(pid);

        // Write to session file
        write_path_snapshot(pid, "/test/path").unwrap();

        // Check file permissions
        let file_path = get_session_file(pid).unwrap();
        let metadata = fs::metadata(&file_path).unwrap();
        let permissions = metadata.permissions();

        // Should be 0600 (user read/write only)
        let mode = permissions.mode() & 0o777;
        assert_eq!(
            mode, 0o600,
            "Session file should have 0600 permissions, got {:o}",
            mode
        );

        // Cleanup
        let _ = clear_session(pid);
    }

    #[cfg(unix)]
    #[test]
    fn test_session_dir_permissions() {
        use std::os::unix::fs::PermissionsExt;

        let _guard = SessionTempDir::new();
        // Get or create session directory
        let dir = get_session_dir().unwrap();

        // Check if directory exists and get its metadata
        assert!(dir.exists(), "Session directory should exist");
        let metadata = fs::metadata(&dir).unwrap();
        let permissions = metadata.permissions();

        // Should be 0700 (user rwx only)
        let mode = permissions.mode() & 0o777;
        assert_eq!(
            mode, 0o700,
            "Session directory should have 0700 permissions, got {:o}",
            mode
        );
    }

    #[test]
    fn test_write_and_read_snapshots() {
        let _guard = SessionTempDir::new();
        let pid = 999002;
        let _ = clear_session(pid);

        // Write snapshots
        write_path_snapshot(pid, "/a:/b:/c").unwrap();
        write_path_snapshot(pid, "/b:/c:/a").unwrap();
        write_path_snapshot(pid, "/c:/a:/b").unwrap();

        // Read back
        let snapshots = read_path_snapshots(pid).unwrap();

        assert_eq!(snapshots.len(), 3);
        assert_eq!(snapshots[0], "/a:/b:/c");
        assert_eq!(snapshots[1], "/b:/c:/a");
        assert_eq!(snapshots[2], "/c:/a:/b");

        // Cleanup
        let _ = clear_session(pid);
    }

    #[test]
    fn test_snapshot_truncation() {
        let _guard = SessionTempDir::new();
        let pid = 999003;
        let _ = clear_session(pid);

        // Write 5 snapshots
        write_path_snapshot(pid, "/initial").unwrap();
        write_path_snapshot(pid, "/snap1").unwrap();
        write_path_snapshot(pid, "/snap2").unwrap();
        write_path_snapshot(pid, "/snap3").unwrap();
        write_path_snapshot(pid, "/snap4").unwrap();

        // Verify all 5
        let snapshots = read_path_snapshots(pid).unwrap();
        assert_eq!(snapshots.len(), 5);

        // Truncate to keep only first 2
        truncate_snapshots(pid, 2).unwrap();

        // Verify only 2 remain
        let snapshots = read_path_snapshots(pid).unwrap();
        assert_eq!(snapshots.len(), 2);
        assert_eq!(snapshots[0], "/initial");
        assert_eq!(snapshots[1], "/snap1");

        // Cleanup
        let _ = clear_session(pid);
    }

    #[test]
    fn test_rolling_window_cleanup() {
        let _guard = SessionTempDir::new();
        let pid = 999004;
        let _ = clear_session(pid);

        // Write initial snapshot
        write_path_snapshot(pid, "/initial").unwrap();

        // Write 10 more snapshots
        for i in 1..=10 {
            write_path_snapshot(pid, &format!("/snapshot{}", i)).unwrap();
        }

        let snapshots = read_path_snapshots(pid).unwrap();
        assert_eq!(snapshots.len(), 11);

        // Manually call cleanup with max=5
        // Should keep snapshot 0 + last 4 (indices 7, 8, 9, 10)
        crate::history::HistoryContext::global(pid)
            .unwrap()
            .truncate_keep_initial_and_tail(5)
            .unwrap();

        let snapshots = read_path_snapshots(pid).unwrap();
        assert_eq!(snapshots.len(), 5);
        assert_eq!(snapshots[0], "/initial");
        assert_eq!(snapshots[1], "/snapshot7");
        assert_eq!(snapshots[2], "/snapshot8");
        assert_eq!(snapshots[3], "/snapshot9");
        assert_eq!(snapshots[4], "/snapshot10");

        // Cleanup
        let _ = clear_session(pid);
    }

    #[test]
    fn test_get_initial_path() {
        let _guard = SessionTempDir::new();
        let pid = 999005;
        let _ = clear_session(pid);

        // No snapshots yet
        assert_eq!(get_initial_path(pid).unwrap(), None);

        // Write snapshots
        write_path_snapshot(pid, "/first").unwrap();
        write_path_snapshot(pid, "/second").unwrap();

        // Get initial should return first
        assert_eq!(get_initial_path(pid).unwrap(), Some("/first".to_string()));

        // Cleanup
        let _ = clear_session(pid);
    }

    #[test]
    fn test_duplicate_init_handled() {
        let _guard = SessionTempDir::new();
        let pid = 999006;
        let _ = clear_session(pid);

        // Simulate double-source scenario (user sources integration script twice)
        // This should be prevented by shell integration guards, but test resilience
        write_path_snapshot(pid, "/usr/bin:/bin").unwrap();
        write_path_snapshot(pid, "/usr/bin:/bin").unwrap(); // Duplicate init

        // Initial should still return first snapshot
        assert_eq!(
            get_initial_path(pid).unwrap(),
            Some("/usr/bin:/bin".to_string())
        );

        // Verify we can read both snapshots (not ideal, but handled)
        let snapshots = read_path_snapshots(pid).unwrap();
        assert_eq!(snapshots.len(), 2);
        assert_eq!(snapshots[0], "/usr/bin:/bin");
        assert_eq!(snapshots[1], "/usr/bin:/bin");

        // Verify undo behavior - should go back to first duplicate, then error
        // (This demonstrates the phantom operation issue the guard prevents)
        assert_eq!(snapshots.len() - 1, 1); // Only 1 operation to undo

        // Cleanup
        let _ = clear_session(pid);
    }
}