wasm-slim 0.1.1

WASM bundle size optimizer
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
421
422
423
424
425
426
427
428
//! Integration tests for the CLI binary
//!
//! Tests CLI commands, flag combinations, and output formatting using assert_cmd

// TODO: Migrate to cargo_bin! macro when stable migration path is documented
// https://github.com/assert-rs/assert_cmd/issues/225
#![allow(deprecated)]

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;

mod common;

// ===== Module Organization =====
// Tests are organized into logical groups:
// - Basic CLI: help, version, flags
// - Command Execution: subcommands and their behavior
// - Error Handling: invalid inputs and edge cases
// - Output Formatting: JSON, exit codes, messages

/// Create a minimal test project for CLI testing
fn create_test_project(temp_dir: &TempDir) -> std::path::PathBuf {
    let project_root = temp_dir.path().to_path_buf();

    // Create Cargo.toml
    let cargo_toml = r#"[package]
name = "test-cli-project"
version = "0.1.0"
edition = "2021"

[dependencies]
"#;

    fs::write(project_root.join("Cargo.toml"), cargo_toml).expect("Failed to write Cargo.toml");

    // Create src/lib.rs
    fs::create_dir(project_root.join("src")).expect("Failed to create src directory");

    let lib_rs = "pub fn hello() -> String { \"Hello\".to_string() }";
    fs::write(project_root.join("src/lib.rs"), lib_rs).expect("Failed to write lib.rs");

    project_root
}

// ===== Basic CLI Tests =====

#[test]
fn test_cli_help_flag() {
    // Test that --help flag works and shows usage information
    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("wasm-slim"))
        .stdout(predicate::str::contains("Usage:"));
}

#[test]
fn test_cli_version_flag() {
    // Test that --version flag shows version information
    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("wasm-slim"));
}

#[test]
fn test_cli_help_for_subcommands() {
    // Test that help works for each subcommand
    let subcommands = vec!["analyze", "build", "init"];

    for subcmd in subcommands {
        let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

        cmd.arg(subcmd)
            .arg("--help")
            .assert()
            .success()
            .stdout(predicate::str::contains(subcmd));
    }
}

// ===== Command Execution Tests =====

#[test]
fn test_cli_init_command() {
    // Test that init command creates configuration file
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let project_root = create_test_project(&temp_dir);

    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("init")
        .current_dir(&project_root)
        .assert()
        .success();

    // Verify config file was created
    assert!(
        project_root.join(".wasm-slim.toml").exists(),
        "Config file should be created by init command"
    );
}

#[test]
fn test_cli_init_with_template() {
    // Test init command with template flag
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let project_root = create_test_project(&temp_dir);

    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("init")
        .arg("--template")
        .arg("balanced")
        .current_dir(&project_root)
        .assert()
        .success();

    // Verify config file was created
    assert!(project_root.join(".wasm-slim.toml").exists());
}

#[test]
fn test_cli_analyze_without_project() {
    // Test that analyze fails gracefully without a valid project
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");

    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("analyze")
        .current_dir(temp_dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("Cargo.toml").or(predicate::str::contains("project")));
}

#[test]
fn test_cli_init_command_standalone() {
    // Test that init command works standalone
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let project_root = create_test_project(&temp_dir);

    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    // init should work
    cmd.arg("init")
        .current_dir(&project_root)
        .assert()
        .success();
}

#[test]
fn test_cli_json_output_flag() {
    // Test that --json flag produces valid JSON output (if supported)
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let _project_root = create_test_project(&temp_dir);

    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    // Test with a command that supports JSON output
    let result = cmd.arg("--version").output();

    // Just verify the command runs (JSON support is optional)
    assert!(result.is_ok());
}

#[test]
fn test_cli_init_with_multiple_options() {
    // Test that init command works with multiple options
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let project_root = create_test_project(&temp_dir);

    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("init")
        .arg("--template")
        .arg("balanced")
        .current_dir(&project_root)
        .assert()
        .success();
}

#[test]
fn test_cli_init_overwrites_with_force() {
    // Test that init with --force overwrites existing config
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let project_root = create_test_project(&temp_dir);

    // Create initial config
    let mut cmd1 = Command::cargo_bin("wasm-slim").unwrap();
    cmd1.arg("init")
        .current_dir(&project_root)
        .assert()
        .success();

    // Try to init again with --force (if supported)
    let mut cmd2 = Command::cargo_bin("wasm-slim").unwrap();
    let result = cmd2
        .arg("init")
        .arg("--force")
        .current_dir(&project_root)
        .output();

    // Either succeeds with --force or fails without it
    assert!(result.is_ok());
}

// ===== Error Handling Tests =====

#[test]
fn test_cli_invalid_subcommand() {
    // Test that invalid subcommand produces error
    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("nonexistent-command")
        .assert()
        .failure()
        .stderr(predicate::str::contains("error").or(predicate::str::contains("invalid")));
}

#[test]
fn test_cli_invalid_flag() {
    // Test that invalid flag produces error
    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("--nonexistent-flag").assert().failure();
}

#[test]
fn test_cli_error_messages_are_helpful() {
    // Test that error messages provide useful information
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");

    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("analyze")
        .current_dir(temp_dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::is_empty().not());
}

#[test]
fn test_cli_completions_command() {
    // Test that completions command works for various shells
    let shells = vec!["bash", "zsh", "fish"];

    for shell in shells {
        let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

        let result = cmd.arg("completions").arg(shell).output();

        // Command should at least execute (may not be implemented yet)
        assert!(
            result.is_ok(),
            "Completions command should run for {}",
            shell
        );
    }
}

// ===== Output Formatting Tests =====

#[test]
fn test_cli_output_format_consistency() {
    // Test that output format is consistent across commands
    let mut cmd1 = Command::cargo_bin("wasm-slim").unwrap();
    let output1 = cmd1.arg("--help").output().expect("Failed to run command");

    let mut cmd2 = Command::cargo_bin("wasm-slim").unwrap();
    let output2 = cmd2
        .arg("--version")
        .output()
        .expect("Failed to run command");

    // Both should succeed
    assert!(output1.status.success());
    assert!(output2.status.success());

    // Both should produce output
    assert!(!output1.stdout.is_empty() || !output1.stderr.is_empty());
    assert!(!output2.stdout.is_empty() || !output2.stderr.is_empty());
}

#[test]
fn test_cli_exit_codes() {
    // Test that CLI uses appropriate exit codes
    let mut cmd_success = Command::cargo_bin("wasm-slim").unwrap();
    cmd_success.arg("--version").assert().code(0); // Success should be exit code 0

    let mut cmd_failure = Command::cargo_bin("wasm-slim").unwrap();
    let result = cmd_failure.arg("--nonexistent-flag").assert().failure();

    // Failure should be non-zero exit code
    assert!(result.get_output().status.code().unwrap_or(0) != 0);
}

#[test]
fn test_cli_handles_ctrl_c_gracefully() {
    // Test that CLI can be interrupted (basic test)
    // This is more of a smoke test - actual signal handling tested elsewhere
    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    let result = cmd.arg("--help").output();

    // Command should complete successfully
    assert!(result.is_ok());
}

#[test]
fn test_cli_respects_current_directory() {
    // Test that CLI respects the current working directory
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let project_root = create_test_project(&temp_dir);

    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("init")
        .current_dir(&project_root)
        .assert()
        .success();

    // Config should be created in the current directory
    assert!(project_root.join(".wasm-slim.toml").exists());
}

#[test]
fn test_cli_help_shows_all_subcommands() {
    // Test that help output lists all available subcommands
    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("init"))
        .stdout(predicate::str::contains("analyze"))
        .stdout(predicate::str::contains("build"));
}

#[test]
fn test_cli_version_format() {
    // Test that version output has expected format
    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("wasm-slim"))
        .stdout(predicate::str::is_match(r"\d+\.\d+\.\d+").unwrap());
}

#[test]
fn test_cli_stdin_not_required() {
    // Test that CLI doesn't block waiting for stdin
    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    cmd.arg("--help").assert().success();

    // CLI should complete without waiting for stdin
}

#[test]
fn test_cli_parallel_execution() {
    // Test that multiple CLI instances can run concurrently
    use std::thread;

    let handles: Vec<_> = (0..3)
        .map(|_| {
            thread::spawn(|| {
                let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
                cmd.arg("--version").assert().success();
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread panicked");
    }
}

#[test]
fn test_cli_unicode_in_arguments() {
    // Test that CLI handles Unicode in arguments
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let project_root = create_test_project(&temp_dir);

    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    // Try with Unicode template name (should fail gracefully)
    let result = cmd
        .arg("init")
        .arg("--template")
        .arg("日本語")
        .current_dir(&project_root)
        .output();

    // Should complete without panicking (may succeed or fail)
    assert!(result.is_ok());
}

#[test]
fn test_cli_very_long_arguments() {
    // Test that CLI handles very long arguments gracefully
    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    let long_string = "a".repeat(1000);
    let result = cmd.arg("--template").arg(long_string).output();

    // Should complete without panicking
    assert!(result.is_ok());
}

#[test]
fn test_cli_empty_arguments() {
    // Test that CLI handles empty string arguments
    let mut cmd = Command::cargo_bin("wasm-slim").unwrap();

    let result = cmd.arg("").output();

    // Should complete without panicking
    assert!(result.is_ok());
}