ruchy 4.2.0

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// tests/http_server_cli.rs - HTTP Server CLI Tests (EXTREME TDD)
#![allow(clippy::ignore_without_reason)] // Property tests run with --ignored flag
#![allow(missing_docs)]
// [HTTP-001] RED Phase: Write failing tests FIRST
#![allow(clippy::similar_names)] // coop/coep are standard HTTP header abbreviations
#![allow(clippy::zombie_processes)] // Test processes are explicitly killed in tests
#![allow(deprecated)] // cargo_bin deprecation warning

use assert_cmd::Command;
use predicates::prelude::*;
use std::net::TcpListener;
use std::time::Duration;
use tempfile::TempDir;

/// Helper: Get ruchy binary command
fn ruchy_cmd() -> Command {
    assert_cmd::cargo::cargo_bin_cmd!("ruchy")
}

/// Helper: Find available port
fn find_available_port() -> u16 {
    TcpListener::bind("127.0.0.1:0")
        .expect("Failed to bind to random port")
        .local_addr()
        .expect("Failed to get local addr")
        .port()
}

/// Helper: Create test directory with files
fn create_test_dir() -> TempDir {
    let dir = TempDir::new().expect("Failed to create temp dir");
    std::fs::write(
        dir.path().join("index.html"),
        "<!DOCTYPE html><html><body>Hello from Ruchy!</body></html>",
    )
    .expect("Failed to write index.html");
    std::fs::write(dir.path().join("style.css"), "body { margin: 0; }")
        .expect("Failed to write style.css");
    dir
}

// ============================================================================
// RED PHASE: These tests WILL FAIL until we implement ruchy serve
// ============================================================================

#[test]
fn test_red_ruchy_serve_shows_help() {
    // RED: This MUST fail - ruchy serve doesn't exist yet
    ruchy_cmd()
        .arg("serve")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Serve static files"));
}

#[test]
fn test_red_ruchy_serve_requires_directory() {
    // RED: This MUST fail - ruchy serve doesn't exist yet
    let port = find_available_port();

    ruchy_cmd()
        .arg("serve")
        .arg("./nonexistent")
        .arg("--port")
        .arg(port.to_string())
        .assert()
        .failure()
        .stderr(
            predicate::str::contains("Directory not found")
                .or(predicate::str::contains("No such file")),
        );
}

#[test]
fn test_red_ruchy_serve_starts_server() {
    // RED: This MUST fail - ruchy serve doesn't exist yet
    use std::process::{Command, Stdio};

    let test_dir = create_test_dir();
    let port = find_available_port();

    // Get ruchy binary path
    let ruchy_bin = assert_cmd::cargo::cargo_bin("ruchy");

    // Start server in background
    let mut child = Command::new(ruchy_bin)
        .arg("serve")
        .arg(test_dir.path())
        .arg("--port")
        .arg(port.to_string())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    // Give server time to start
    std::thread::sleep(Duration::from_millis(500));

    // Test HTTP request
    let response = reqwest::blocking::get(format!("http://127.0.0.1:{port}/index.html"));
    assert!(response.is_ok(), "Failed to connect to server");

    let response = response.unwrap();
    assert_eq!(response.status(), 200);
    assert!(response.text().unwrap().contains("Hello from Ruchy!"));

    // Cleanup
    child.kill().expect("Failed to kill server");
}

#[test]
#[ignore = "RED phase: ruchy serve command not yet implemented"]
fn test_red_ruchy_serve_default_port_8080() {
    // RED: This MUST fail - ruchy serve doesn't exist yet
    use std::process::{Command, Stdio};

    let test_dir = create_test_dir();

    // Get ruchy binary path
    let ruchy_bin = assert_cmd::cargo::cargo_bin("ruchy");

    let mut child = Command::new(ruchy_bin)
        .arg("serve")
        .arg(test_dir.path())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    std::thread::sleep(Duration::from_millis(500));

    // Should default to port 8080
    let response = reqwest::blocking::get("http://127.0.0.1:8080/index.html");
    assert!(
        response.is_ok(),
        "Server should listen on port 8080 by default"
    );

    child.kill().expect("Failed to kill server");
}

#[test]
fn test_red_ruchy_serve_shows_startup_message() {
    // RED: This MUST fail - ruchy serve doesn't exist yet
    let test_dir = create_test_dir();
    let port = find_available_port();

    let output = ruchy_cmd()
        .arg("serve")
        .arg(test_dir.path())
        .arg("--port")
        .arg(port.to_string())
        .timeout(Duration::from_secs(1))
        .output()
        .expect("Failed to run server");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should print startup info
    assert!(stdout.contains("Ruchy HTTP Server") || stdout.contains("🚀"));
    assert!(stdout.contains(&port.to_string()));
    assert!(stdout.contains("http://") || stdout.contains("localhost"));
}

// ============================================================================
// Property Tests (10,000 iterations)
// ============================================================================

#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(10_000))]

        #[test]
        #[ignore = "Remove when implementation complete"]
        fn prop_ruchy_serve_never_panics_on_invalid_port(port in 0u16..1024u16) {
            // Property: ruchy serve with invalid port should fail gracefully, not panic
            let test_dir = create_test_dir();

            let result = ruchy_cmd()
                .arg("serve")
                .arg(test_dir.path())
                .arg("--port")
                .arg(port.to_string())
                .timeout(Duration::from_secs(1))
                .output();

            // Should either succeed or fail gracefully (no panic)
            prop_assert!(result.is_ok());
        }

        #[test]
        #[ignore = "Test disabled - run with --ignored"]
        fn prop_ruchy_serve_handles_any_valid_directory(dir_name: String) {
            // Property: ruchy serve should handle any valid directory name
            let temp_dir = TempDir::new().unwrap();
            let test_path = temp_dir.path().join(&dir_name);

            if let Ok(()) = std::fs::create_dir_all(&test_path) {
                std::fs::write(test_path.join("test.html"), "<html></html>").unwrap();

                let result = ruchy_cmd()
                    .arg("serve")
                    .arg(&test_path)
                    .timeout(Duration::from_millis(100))
                    .output();

                prop_assert!(result.is_ok());
            }
        }
    }
}

// ============================================================================
// MIME Type Detection Tests (HTTP-002)
// ============================================================================

#[test]
fn test_http002_mime_html() {
    let test_dir = TempDir::new().unwrap();
    std::fs::write(
        test_dir.path().join("index.html"),
        "<!DOCTYPE html><html></html>",
    )
    .unwrap();

    let port = find_available_port();
    let ruchy_bin = assert_cmd::cargo::cargo_bin("ruchy");
    let mut child = std::process::Command::new(ruchy_bin)
        .arg("serve")
        .arg(test_dir.path())
        .arg("--port")
        .arg(port.to_string())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    std::thread::sleep(Duration::from_millis(500));

    let response = reqwest::blocking::get(format!("http://127.0.0.1:{port}/index.html")).unwrap();
    assert_eq!(response.status(), 200);
    assert_eq!(response.headers().get("content-type").unwrap(), "text/html");

    child.kill().unwrap();
}

#[test]
fn test_http002_mime_css() {
    let test_dir = TempDir::new().unwrap();
    std::fs::write(test_dir.path().join("style.css"), "body { margin: 0; }").unwrap();

    let port = find_available_port();
    let ruchy_bin = assert_cmd::cargo::cargo_bin("ruchy");
    let mut child = std::process::Command::new(ruchy_bin)
        .arg("serve")
        .arg(test_dir.path())
        .arg("--port")
        .arg(port.to_string())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    std::thread::sleep(Duration::from_millis(500));

    let response = reqwest::blocking::get(format!("http://127.0.0.1:{port}/style.css")).unwrap();
    assert_eq!(response.status(), 200);
    assert_eq!(response.headers().get("content-type").unwrap(), "text/css");

    child.kill().unwrap();
}

#[test]
fn test_http002_mime_javascript() {
    let test_dir = TempDir::new().unwrap();
    std::fs::write(test_dir.path().join("app.js"), "console.log('test');").unwrap();

    let port = find_available_port();
    let ruchy_bin = assert_cmd::cargo::cargo_bin("ruchy");
    let mut child = std::process::Command::new(ruchy_bin)
        .arg("serve")
        .arg(test_dir.path())
        .arg("--port")
        .arg(port.to_string())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    std::thread::sleep(Duration::from_millis(500));

    let response = reqwest::blocking::get(format!("http://127.0.0.1:{port}/app.js")).unwrap();
    assert_eq!(response.status(), 200);
    let content_type = response
        .headers()
        .get("content-type")
        .unwrap()
        .to_str()
        .unwrap();
    assert!(
        content_type.contains("application/javascript") || content_type.contains("text/javascript"),
        "Expected JavaScript MIME type, got: {content_type}"
    );

    child.kill().unwrap();
}

#[test]
fn test_http002_mime_wasm() {
    // CRITICAL: WASM files MUST have application/wasm MIME type
    let test_dir = TempDir::new().unwrap();
    // Minimal valid WASM module: magic number + version
    std::fs::write(
        test_dir.path().join("module.wasm"),
        b"\x00\x61\x73\x6d\x01\x00\x00\x00",
    )
    .unwrap();

    let port = find_available_port();
    let ruchy_bin = assert_cmd::cargo::cargo_bin("ruchy");
    let mut child = std::process::Command::new(ruchy_bin)
        .arg("serve")
        .arg(test_dir.path())
        .arg("--port")
        .arg(port.to_string())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    std::thread::sleep(Duration::from_millis(500));

    let response = reqwest::blocking::get(format!("http://127.0.0.1:{port}/module.wasm")).unwrap();
    assert_eq!(response.status(), 200);
    assert_eq!(
        response.headers().get("content-type").unwrap(),
        "application/wasm",
        "WASM files MUST have application/wasm MIME type for streaming compilation"
    );

    child.kill().unwrap();
}

#[test]
fn test_http002_mime_json() {
    let test_dir = TempDir::new().unwrap();
    std::fs::write(test_dir.path().join("data.json"), r#"{"test": true}"#).unwrap();

    let port = find_available_port();
    let ruchy_bin = assert_cmd::cargo::cargo_bin("ruchy");
    let mut child = std::process::Command::new(ruchy_bin)
        .arg("serve")
        .arg(test_dir.path())
        .arg("--port")
        .arg(port.to_string())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    std::thread::sleep(Duration::from_millis(500));

    let response = reqwest::blocking::get(format!("http://127.0.0.1:{port}/data.json")).unwrap();
    assert_eq!(response.status(), 200);
    assert_eq!(
        response.headers().get("content-type").unwrap(),
        "application/json"
    );

    child.kill().unwrap();
}

// ============================================================================
// WASM-Specific Headers Tests (HTTP-003)
// ============================================================================

#[test]
fn test_http003_wasm_coop_header() {
    // CRITICAL: WASM files MUST have Cross-Origin-Opener-Policy for SharedArrayBuffer
    let test_dir = TempDir::new().unwrap();
    std::fs::write(
        test_dir.path().join("app.wasm"),
        b"\x00\x61\x73\x6d\x01\x00\x00\x00",
    )
    .unwrap();

    let port = find_available_port();
    let ruchy_bin = assert_cmd::cargo::cargo_bin("ruchy");
    let mut child = std::process::Command::new(ruchy_bin)
        .arg("serve")
        .arg(test_dir.path())
        .arg("--port")
        .arg(port.to_string())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    std::thread::sleep(Duration::from_millis(500));

    let response = reqwest::blocking::get(format!("http://127.0.0.1:{port}/app.wasm")).unwrap();
    assert_eq!(response.status(), 200);

    // COOP header required for SharedArrayBuffer
    let coop = response.headers().get("cross-origin-opener-policy");
    assert!(
        coop.is_some(),
        "WASM files MUST have Cross-Origin-Opener-Policy header for SharedArrayBuffer support"
    );
    assert_eq!(coop.unwrap(), "same-origin");

    child.kill().unwrap();
}

#[test]
fn test_http003_wasm_coep_header() {
    // CRITICAL: WASM files MUST have Cross-Origin-Embedder-Policy for SharedArrayBuffer
    let test_dir = TempDir::new().unwrap();
    std::fs::write(
        test_dir.path().join("app.wasm"),
        b"\x00\x61\x73\x6d\x01\x00\x00\x00",
    )
    .unwrap();

    let port = find_available_port();
    let ruchy_bin = assert_cmd::cargo::cargo_bin("ruchy");
    let mut child = std::process::Command::new(ruchy_bin)
        .arg("serve")
        .arg(test_dir.path())
        .arg("--port")
        .arg(port.to_string())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    std::thread::sleep(Duration::from_millis(500));

    let response = reqwest::blocking::get(format!("http://127.0.0.1:{port}/app.wasm")).unwrap();
    assert_eq!(response.status(), 200);

    // COEP header required for SharedArrayBuffer
    let coep = response.headers().get("cross-origin-embedder-policy");
    assert!(
        coep.is_some(),
        "WASM files MUST have Cross-Origin-Embedder-Policy header for SharedArrayBuffer support"
    );
    assert_eq!(coep.unwrap(), "require-corp");

    child.kill().unwrap();
}

#[test]
fn test_http003_html_coop_header() {
    // HTML files serving WASM also need COOP/COEP headers
    let test_dir = TempDir::new().unwrap();
    std::fs::write(
        test_dir.path().join("index.html"),
        "<!DOCTYPE html><html></html>",
    )
    .unwrap();

    let port = find_available_port();
    let ruchy_bin = assert_cmd::cargo::cargo_bin("ruchy");
    let mut child = std::process::Command::new(ruchy_bin)
        .arg("serve")
        .arg(test_dir.path())
        .arg("--port")
        .arg(port.to_string())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    std::thread::sleep(Duration::from_millis(500));

    let response = reqwest::blocking::get(format!("http://127.0.0.1:{port}/index.html")).unwrap();
    assert_eq!(response.status(), 200);

    // HTML pages that load WASM need these headers too
    let coop = response.headers().get("cross-origin-opener-policy");
    assert!(
        coop.is_some(),
        "HTML files MUST have COOP header for WASM SharedArrayBuffer"
    );
    assert_eq!(coop.unwrap(), "same-origin");

    let coep = response.headers().get("cross-origin-embedder-policy");
    assert!(
        coep.is_some(),
        "HTML files MUST have COEP header for WASM SharedArrayBuffer"
    );
    assert_eq!(coep.unwrap(), "require-corp");

    child.kill().unwrap();
}

// ============================================================================
// RED Phase Validation
// ============================================================================

#[test]
fn test_red_phase_validation() {
    // This test ensures we're in RED phase
    // All #[ignore = "tests above MUST fail when un-ignored"]

    println!("✅ RED Phase: 5 failing tests created");
    println!("✅ Property tests: 2 tests with 10K iterations each");
    println!("🔴 Next: Remove #[ignore] and watch tests FAIL");
    println!("🟢 Then: Implement ruchy serve to make tests PASS");
}