wsb 0.81.223364

Workspace - A tool suite for file operations, version management, and development workflow automation
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
414
415
416
417
418
419
420
use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::{NamedTempFile, TempDir};
use std::io::Write;
use std::fs;

/// Test the code command help functionality
#[test]
fn test_code_command_help() {
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&["code", "--help"]);
    
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("AST-based code analysis and transformation"))
        .stdout(predicate::str::contains("search"))
        .stdout(predicate::str::contains("transform"))
        .stdout(predicate::str::contains("patterns"))
        .stdout(predicate::str::contains("analyze"));
}

/// Test code search functionality
#[test]
fn test_code_search_basic() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "fn main() {{").unwrap();
    writeln!(temp_file, "    println!(\"Hello, world!\");").unwrap();
    writeln!(temp_file, "    let x = 42;").unwrap();
    writeln!(temp_file, "    println!(\"Value: {{}}\", x);").unwrap();
    writeln!(temp_file, "}}").unwrap();
    temp_file.flush().unwrap();

    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "search", 
        "println",
        "--files", temp_file.path().to_str().unwrap(),
        "--language", "rust"
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("println"));
}

/// Test code search with JSON output
#[test]
fn test_code_search_json_output() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "console.log('Hello, world!');").unwrap();
    writeln!(temp_file, "const x = 42;").unwrap();
    writeln!(temp_file, "console.log('Value:', x);").unwrap();
    temp_file.flush().unwrap();

    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "search",
        "console.log",
        "--files", temp_file.path().to_str().unwrap(),
        "--language", "javascript",
        "--format", "json"
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("{"));
}

/// Test code transform functionality (dry run)
#[test]
fn test_code_transform_dry_run() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "fn main() {{").unwrap();
    writeln!(temp_file, "    let result = some_operation().unwrap();").unwrap();
    writeln!(temp_file, "    println!(\"Result: {{}}\", result);").unwrap();
    writeln!(temp_file, "}}").unwrap();
    temp_file.flush().unwrap();

    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "transform",
        "unwrap",
        "expect(\"Expected value\")",
        "--files", temp_file.path().to_str().unwrap(),
        "--language", "rust",
        "--dry-run"
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("dry run"));
}

/// Test code transform with actual file modification
#[test]
fn test_code_transform_actual() {
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("test.js");
    
    fs::write(&file_path, "var x = 42;\nvar y = 'hello';\n").unwrap();

    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "transform",
        "var",
        "let",
        "--files", file_path.to_str().unwrap(),
        "--language", "javascript",
        "--no-backup"
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("changes applied"));

    // Verify the file was actually modified
    let content = fs::read_to_string(&file_path).unwrap();
    assert!(content.contains("let"));
}

/// Test code patterns for different languages
#[test]
fn test_code_patterns_rust() {
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&["code", "patterns", "rust", "--category", "transform"]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Common transform patterns for rust"));
}

#[test]
fn test_code_patterns_javascript() {
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&["code", "patterns", "javascript", "--category", "transform"]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Common transform patterns for javascript"));
}

#[test]
fn test_code_patterns_unsupported_language() {
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&["code", "patterns", "cobol"]);

    cmd.assert()
        .success()
        .stderr(predicate::str::contains("Unsupported language"));
}

/// Test code analyze functionality
#[test]
fn test_code_analyze_basic() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "fn main() {{").unwrap();
    writeln!(temp_file, "    println!(\"Hello, world!\");").unwrap();
    writeln!(temp_file, "    let x = 42;").unwrap();
    writeln!(temp_file, "}}").unwrap();
    temp_file.flush().unwrap();

    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "analyze",
        "--files", temp_file.path().to_str().unwrap(),
        "--analysis-type", "structure"
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Code Analysis"))
        .stdout(predicate::str::contains("lines"))
        .stdout(predicate::str::contains("characters"));
}

/// Test code analyze with JSON output
#[test]
fn test_code_analyze_json() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "print('Hello, Python!')").unwrap();
    writeln!(temp_file, "x = 42").unwrap();
    temp_file.flush().unwrap();

    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "analyze",
        "--files", temp_file.path().to_str().unwrap(),
        "--language", "python",
        "--format", "json"
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("\"file\":"))
        .stdout(predicate::str::contains("\"lines\":"))
        .stdout(predicate::str::contains("\"chars\":"));
}

/// Test multiple file processing
#[test]
fn test_code_search_multiple_files() {
    let temp_dir = TempDir::new().unwrap();
    
    // Create first file
    let file1 = temp_dir.path().join("file1.rs");
    fs::write(&file1, "fn main() { println!(\"File 1\"); }").unwrap();
    
    // Create second file
    let file2 = temp_dir.path().join("file2.rs");
    fs::write(&file2, "fn helper() { println!(\"File 2\"); }").unwrap();

    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "search",
        "println",
        "--files", file1.to_str().unwrap(),
        "--files", file2.to_str().unwrap(),
        "--language", "rust"
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("println"));
}

/// Test search with context lines
#[test]
fn test_code_search_with_context() {
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("test.rs");
    
    fs::write(&file_path, 
        "// This is a comment\n\
         fn main() {\n\
             println!(\"Target line\");\n\
             let x = 42;\n\
         }\n").unwrap();

    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "search",
        "Target line",
        "--files", file_path.to_str().unwrap(),
        "--context", "2"
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Target line"));
}

/// Test code tree default functionality  
#[test]
fn test_code_tree_default() {
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.arg("code");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Codebase Structure"))
        .stdout(predicate::str::contains("Project Root:"))
        .stdout(predicate::str::contains("Current Location:"));
}

/// Test code tree with options
#[test]
fn test_code_tree_with_options() {
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "tree",
        "--depth", "2",
        "--sizes",
        "--extensions", "rs,toml"
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Codebase Structure"))
        .stdout(predicate::str::contains("Project Root:"));
}

/// Test tree respects gitignore by default
#[test]
fn test_code_tree_respects_gitignore() {
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&["code", "tree", "--depth", "2"]);

    let output = cmd.output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    
    // Should not show the target directory (which is gitignored)
    assert!(!stdout.contains("target"), "target directory should be filtered by gitignore");
    
    // Should show source files that are not ignored
    assert!(stdout.contains("src"), "src directory should be shown");
}

/// Test tree --no-ignore shows all files
#[test]
fn test_code_tree_no_ignore_shows_all() {
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&["code", "tree", "--depth", "1", "--no-ignore"]);

    let output = cmd.output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    
    // With --no-ignore, should show directories that would normally be ignored
    // Note: target might not exist in all test environments, so we test for structure
    assert!(stdout.contains("Codebase Structure"), "Should show codebase structure");
}

/// Test transform with max changes limit
#[test]
fn test_code_transform_max_changes() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "var a = 1;").unwrap();
    writeln!(temp_file, "var b = 2;").unwrap();
    writeln!(temp_file, "var c = 3;").unwrap();
    writeln!(temp_file, "var d = 4;").unwrap();
    temp_file.flush().unwrap();

    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "transform",
        "var",
        "let",
        "--files", temp_file.path().to_str().unwrap(),
        "--language", "javascript",
        "--max-changes", "2",
        "--dry-run"
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("changes applied"));
}

/// Test error handling for non-existent files
#[test]
fn test_code_search_nonexistent_file() {
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "search",
        "test",
        "--files", "/nonexistent/file.rs"
    ]);

    // Should handle the error gracefully, not crash
    cmd.assert()
        .success(); // The command should not crash, even if file doesn't exist
}

/// Test language auto-detection
#[test]
fn test_code_search_language_autodetect() {
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("test.py");
    fs::write(&file_path, "print('Hello, Python!')").unwrap();

    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "search",
        "print",
        "--files", file_path.to_str().unwrap()
        // No explicit language specified - should auto-detect from extension
    ]);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("print"));
}

/// Integration test - full workflow
#[test]
fn test_code_full_workflow() {
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("workflow.js");
    
    // Create initial file with var declarations
    fs::write(&file_path, 
        "var username = 'john';\n\
         var age = 25;\n\
         console.log('User:', username);\n"
    ).unwrap();

    // Step 1: Search for var declarations
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "search",
        "var",
        "--files", file_path.to_str().unwrap(),
        "--language", "javascript"
    ]);
    cmd.assert().success();

    // Step 2: Transform var to let
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "transform",
        "var",
        "let",
        "--files", file_path.to_str().unwrap(),
        "--language", "javascript",
        "--no-backup"
    ]);
    cmd.assert().success();

    // Step 3: Analyze the transformed file
    let mut cmd = Command::cargo_bin("wsb").unwrap();
    cmd.args(&[
        "code", "analyze",
        "--files", file_path.to_str().unwrap()
    ]);
    cmd.assert().success();

    // Verify transformation worked
    let content = fs::read_to_string(&file_path).unwrap();
    assert!(content.contains("let"));
    assert!(!content.contains("var"));
}