bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
use super::*;
use rustyline::history::MemHistory;

// ===== RED PHASE: Unit Tests (These should FAIL initially) =====

#[test]
fn test_REPL_006_001_completer_new() {
    let completer = ReplCompleter::new();

    assert_eq!(completer.commands.len(), 11);
    assert_eq!(completer.modes.len(), 5);
    assert!(completer.commands.contains(&"mode".to_string()));
    assert!(completer.commands.contains(&"load".to_string()));
    assert!(completer.commands.contains(&"source".to_string()));
    assert!(completer.modes.contains(&"normal".to_string()));
}

#[test]
fn test_REPL_006_001_complete_command_prefix() {
    let completer = ReplCompleter::new();

    let completions = completer.complete_command("mo");

    assert_eq!(completions.len(), 1);
    assert_eq!(completions[0].replacement, ":mode");
}

#[test]
fn test_REPL_006_001_complete_command_multiple_matches() {
    let completer = ReplCompleter::new();

    let completions = completer.complete_command("p");

    assert_eq!(completions.len(), 2); // "parse" and "purify"
    let replacements: Vec<_> = completions.iter().map(|p| p.replacement.as_str()).collect();
    assert!(replacements.contains(&":parse"));
    assert!(replacements.contains(&":purify"));
}

#[test]
fn test_REPL_006_001_complete_command_no_match() {
    let completer = ReplCompleter::new();

    let completions = completer.complete_command("xyz");

    assert_eq!(completions.len(), 0);
}

#[test]
fn test_REPL_006_001_complete_mode_prefix() {
    let completer = ReplCompleter::new();

    let completions = completer.complete_mode("pur");

    assert_eq!(completions.len(), 1);
    assert_eq!(completions[0].replacement, "purify");
}

#[test]
fn test_REPL_006_001_complete_mode_multiple_matches() {
    let completer = ReplCompleter::new();

    let completions = completer.complete_mode("l");

    assert_eq!(completions.len(), 1); // Only "lint"
    assert_eq!(completions[0].replacement, "lint");
}

#[test]
fn test_REPL_006_001_complete_mode_empty_shows_all() {
    let completer = ReplCompleter::new();

    let completions = completer.complete_mode("");

    assert_eq!(completions.len(), 5); // All modes
}

#[test]
fn test_REPL_006_001_complete_bash_construct_parameter_expansion() {
    let completer = ReplCompleter::new();

    let completions = completer.complete_bash_construct("${var:");

    assert!(completions.len() >= 4); // At least :-,  :=, :?, :+
}

#[test]
fn test_REPL_006_001_complete_bash_construct_for_loop() {
    let completer = ReplCompleter::new();

    let completions = completer.complete_bash_construct("for");

    assert_eq!(completions.len(), 1);
    assert_eq!(completions[0].replacement, "for i in");
}

#[test]
fn test_REPL_006_001_complete_full_line_command() {
    let completer = ReplCompleter::new();
    let history = MemHistory::new();
    let ctx = Context::new(&history);

    let (start, completions) = completer.complete(":mo", 3, &ctx).unwrap();

    assert_eq!(start, 0);
    assert_eq!(completions.len(), 1);
    assert_eq!(completions[0].replacement, ":mode");
}

#[test]
fn test_REPL_006_001_complete_full_line_mode_name() {
    let completer = ReplCompleter::new();
    let history = MemHistory::new();
    let ctx = Context::new(&history);

    let (start, completions) = completer.complete(":mode pur", 9, &ctx).unwrap();

    assert_eq!(start, 6); // Start after ":mode "
    assert_eq!(completions.len(), 1);
    assert_eq!(completions[0].replacement, "purify");
}

#[test]
fn test_REPL_006_001_complete_bash_in_normal_line() {
    let completer = ReplCompleter::new();
    let history = MemHistory::new();
    let ctx = Context::new(&history);

    let (start, completions) = completer.complete("for", 3, &ctx).unwrap();

    assert_eq!(start, 0);
    assert_eq!(completions.len(), 1);
    assert_eq!(completions[0].replacement, "for i in");
}

#[test]
fn test_REPL_006_001_complete_case_insensitive_command() {
    let completer = ReplCompleter::new();

    let completions = completer.complete_command("MO");

    assert_eq!(completions.len(), 1);
    assert_eq!(completions[0].replacement, ":mode");
}

#[test]
fn test_REPL_006_001_complete_case_insensitive_mode() {
    let completer = ReplCompleter::new();

    let completions = completer.complete_mode("PUR");

    assert_eq!(completions.len(), 1);
    assert_eq!(completions[0].replacement, "purify");
}

#[test]
fn test_REPL_006_001_default_trait() {
    let completer = ReplCompleter::default();

    assert_eq!(completer.commands.len(), 11);
    assert_eq!(completer.modes.len(), 5);
}

// ===== REPL-009-002: File Path Completion Tests =====

#[test]
fn test_REPL_009_002_complete_file_path_current_dir() {
    use std::io::Write;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.sh");
    let mut file = std::fs::File::create(&test_file).unwrap();
    writeln!(file, "#!/bin/bash").unwrap();

    // Change to temp directory
    let original_dir = std::env::current_dir().unwrap();
    std::env::set_current_dir(temp_dir.path()).unwrap();

    let completer = ReplCompleter::new();
    let completions = completer.complete_file_path("te");

    // Restore original directory
    std::env::set_current_dir(original_dir).unwrap();

    assert!(!completions.is_empty());
    assert!(completions.iter().any(|p| p.replacement == "test.sh"));
}

#[test]
fn test_REPL_009_002_complete_file_path_with_directory() {
    use std::io::Write;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let sub_dir = temp_dir.path().join("examples");
    std::fs::create_dir(&sub_dir).unwrap();
    let test_file = sub_dir.join("script.sh");
    let mut file = std::fs::File::create(&test_file).unwrap();
    writeln!(file, "#!/bin/bash").unwrap();

    let completer = ReplCompleter::new();
    let path_str = format!("{}/scr", sub_dir.display());
    let completions = completer.complete_file_path(&path_str);

    assert!(!completions.is_empty());
    assert!(completions.iter().any(|p| p.display.contains("script.sh")));
}

#[test]
fn test_REPL_009_002_complete_file_path_directories_first() {
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let dir1 = temp_dir.path().join("dir_a");
    let dir2 = temp_dir.path().join("dir_b");
    let file1 = temp_dir.path().join("file_a.sh");
    std::fs::create_dir(&dir1).unwrap();
    std::fs::create_dir(&dir2).unwrap();
    std::fs::File::create(&file1).unwrap();

    let completer = ReplCompleter::new();
    let path_str = format!("{}/", temp_dir.path().display());
    let completions = completer.complete_file_path(&path_str);

    // Directories should come before files
    let dir_positions: Vec<usize> = completions
        .iter()
        .enumerate()
        .filter(|(_, p)| p.display.ends_with('/'))
        .map(|(i, _)| i)
        .collect();

    let file_positions: Vec<usize> = completions
        .iter()
        .enumerate()
        .filter(|(_, p)| !p.display.ends_with('/'))
        .map(|(i, _)| i)
        .collect();

    if !dir_positions.is_empty() && !file_positions.is_empty() {
        assert!(dir_positions.iter().max().unwrap() < file_positions.iter().min().unwrap());
    }
}

#[test]
fn test_REPL_009_002_complete_full_line_load_command() {
    use std::io::Write;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("example.sh");
    let mut file = std::fs::File::create(&test_file).unwrap();
    writeln!(file, "#!/bin/bash").unwrap();

    let completer = ReplCompleter::new();
    let history = MemHistory::new();
    let ctx = Context::new(&history);

    let path_str = format!("{}/ex", temp_dir.path().display());
    let line = format!(":load {}", path_str);
    let (start, completions) = completer.complete(&line, line.len(), &ctx).unwrap();

    assert_eq!(start, 6); // Position after ":load "
    assert!(!completions.is_empty());
    assert!(completions.iter().any(|p| p.display.contains("example.sh")));
}

#[test]
fn test_REPL_009_002_complete_full_line_source_command() {
    use std::io::Write;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("script.sh");
    let mut file = std::fs::File::create(&test_file).unwrap();
    writeln!(file, "#!/bin/bash").unwrap();

    let completer = ReplCompleter::new();
    let history = MemHistory::new();
    let ctx = Context::new(&history);

    let path_str = format!("{}/scr", temp_dir.path().display());
    let line = format!(":source {}", path_str);
    let (start, completions) = completer.complete(&line, line.len(), &ctx).unwrap();

    assert_eq!(start, 8); // Position after ":source "
    assert!(!completions.is_empty());
    assert!(completions.iter().any(|p| p.display.contains("script.sh")));
}

#[test]
fn test_REPL_009_002_complete_file_path_no_hidden_files() {
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let hidden_file = temp_dir.path().join(".hidden.sh");
    let visible_file = temp_dir.path().join("visible.sh");
    std::fs::File::create(&hidden_file).unwrap();
    std::fs::File::create(&visible_file).unwrap();

    let completer = ReplCompleter::new();
    let path_str = format!("{}/", temp_dir.path().display());
    let completions = completer.complete_file_path(&path_str);

    // Should not include hidden files unless user explicitly types "."
    assert!(completions.iter().any(|p| p.display.contains("visible.sh")));
    assert!(!completions.iter().any(|p| p.display.contains(".hidden.sh")));
}

#[test]
fn test_REPL_009_002_complete_file_path_nonexistent_dir() {
    let completer = ReplCompleter::new();
    let completions = completer.complete_file_path("/nonexistent/path/file");

    // Should return empty vector for nonexistent directories
    assert_eq!(completions.len(), 0);
}

// ===== REPL-015-002-INT: Syntax Highlighting Integration Tests =====

/// Test: REPL-015-002-INT-001 - Highlighter integration basic
#[test]
fn test_REPL_015_002_INT_001_highlighter_basic() {
    use crate::repl::highlighting::strip_ansi_codes;

    let completer = ReplCompleter::new();

    let input = "echo hello";
    let highlighted = completer.highlight(input, 0);

    // Should contain ANSI codes
    assert!(highlighted.contains("\x1b["));

    // Should preserve original text when stripped
    let stripped = strip_ansi_codes(&highlighted);
    assert_eq!(stripped, input);
}

/// Test: REPL-015-002-INT-002 - Highlight with variables
#[test]
fn test_REPL_015_002_INT_002_highlight_variables() {
    let completer = ReplCompleter::new();

    let input = "echo $HOME";
    let highlighted = completer.highlight(input, 0);

    // Should highlight 'echo' as command (cyan)
    assert!(highlighted.contains("\x1b[36mecho\x1b[0m"));

    // Should highlight '$HOME' as variable (yellow)
    assert!(highlighted.contains("\x1b[33m$HOME\x1b[0m"));
}

/// Test: REPL-015-002-INT-003 - Highlight with keywords
#[test]
fn test_REPL_015_002_INT_003_highlight_keywords() {
    let completer = ReplCompleter::new();

    let input = "if [ -f test ]; then echo found; fi";
    let highlighted = completer.highlight(input, 0);

    // Should highlight keywords (blue)
    assert!(highlighted.contains("\x1b[1;34mif\x1b[0m"));
    assert!(highlighted.contains("\x1b[1;34mthen\x1b[0m"));
    assert!(highlighted.contains("\x1b[1;34mfi\x1b[0m"));
}

/// Test: REPL-015-002-INT-004 - Highlight multiline input
#[test]
fn test_REPL_015_002_INT_004_highlight_multiline() {
    let completer = ReplCompleter::new();

    let input = "for i in 1 2 3\ndo echo $i\ndone";
    let highlighted = completer.highlight(input, 0);

    // Should highlight keywords across lines
    assert!(highlighted.contains("\x1b[1;34mfor\x1b[0m"));
    assert!(highlighted.contains("\x1b[1;34mdo\x1b[0m"));
    assert!(highlighted.contains("\x1b[1;34mdone\x1b[0m"));

    // Should highlight variable
    assert!(highlighted.contains("\x1b[33m$i\x1b[0m"));
}

/// Test: REPL-015-002-INT-005 - Empty input
#[test]
fn test_REPL_015_002_INT_005_empty_input() {
    let completer = ReplCompleter::new();

    let highlighted = completer.highlight("", 0);

    // Should handle empty input gracefully
    assert_eq!(highlighted.as_ref(), "");
}

/// Test: REPL-015-002-INT-006 - Special characters
#[test]
fn test_REPL_015_002_INT_006_special_characters() {
    let completer = ReplCompleter::new();

    let input = "echo \"test\" | grep 'pattern' && exit 0";
    let highlighted = completer.highlight(input, 0);

    // Should highlight strings (green)
    assert!(highlighted.contains("\x1b[32m\"test\"\x1b[0m"));
    assert!(highlighted.contains("\x1b[32m'pattern'\x1b[0m"));

    // Should highlight operators (magenta)
    assert!(highlighted.contains("\x1b[35m|\x1b[0m"));
    assert!(highlighted.contains("\x1b[35m&&\x1b[0m"));
}