venus 0.1.1

Reactive notebook environment for Rust
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! End-to-end tests for Venus CLI commands.
//!
//! These tests verify that the CLI produces expected output
//! when run against real notebook files.

#![allow(deprecated)] // Allow deprecated Command::cargo_bin for tests

use std::fs;
use std::path::PathBuf;

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

// =============================================================================
// Test Helpers
// =============================================================================

/// Create a temporary directory with a test notebook.
struct TestNotebook {
    _temp_dir: TempDir,
    notebook_path: PathBuf,
}

impl TestNotebook {
    fn new(filename: &str, source: &str) -> Self {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let notebook_path = temp_dir.path().join(filename);
        fs::write(&notebook_path, source).expect("Failed to write notebook");

        Self {
            _temp_dir: temp_dir,
            notebook_path,
        }
    }

    fn path(&self) -> &PathBuf {
        &self.notebook_path
    }

    fn ipynb_path(&self) -> PathBuf {
        self.notebook_path.with_extension("ipynb")
    }
}

/// Create a simple notebook with primitive types.
fn simple_notebook() -> String {
    r#"//! Simple Test Notebook
//!
//! ```cargo
//! [dependencies]
//! ```

/// Returns a base number.
#[venus::cell]
pub fn base() -> i32 {
    42
}

/// Doubles the base value.
#[venus::cell]
pub fn doubled(base: &i32) -> i32 {
    base * 2
}

/// Adds ten to doubled.
#[venus::cell]
pub fn plus_ten(doubled: &i32) -> i32 {
    doubled + 10
}
"#
    .to_string()
}

/// Create a notebook with String types.
fn string_notebook() -> String {
    r#"//! String Test Notebook
//!
//! ```cargo
//! [dependencies]
//! ```

/// Returns a greeting.
#[venus::cell]
pub fn greeting() -> String {
    "Hello, Venus!".to_string()
}

/// Transforms the greeting.
#[venus::cell]
pub fn shouted(greeting: &String) -> String {
    greeting.to_uppercase()
}
"#
    .to_string()
}

// =============================================================================
// venus run Tests
// =============================================================================

#[test]
fn test_run_nonexistent_notebook() {
    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["run", "/nonexistent/notebook.rs"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found").or(predicate::str::contains("Notebook")));
}

#[test]
fn test_run_simple_notebook() {
    let notebook = TestNotebook::new("simple.rs", &simple_notebook());

    let output = Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["run", notebook.path().to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

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

    // Print output for debugging if test fails
    if !output.status.success() {
        eprintln!("STDOUT:\n{}", stdout);
        eprintln!("STDERR:\n{}", stderr);
    }

    // Should complete successfully
    assert!(
        output.status.success(),
        "venus run should succeed. stderr: {}",
        stderr
    );

    // Should parse 3 cells
    assert!(
        stdout.contains("3 cells"),
        "Should report 3 cells. stdout: {}",
        stdout
    );

    // Should show completion message
    assert!(
        stdout.contains("Completed"),
        "Should show completion. stdout: {}",
        stdout
    );
}

#[test]
fn test_run_specific_cell() {
    let notebook = TestNotebook::new("specific.rs", &simple_notebook());

    let output = Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args([
            "run",
            notebook.path().to_str().unwrap(),
            "--cell",
            "doubled",
        ])
        .output()
        .expect("Failed to execute command");

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

    if !output.status.success() {
        eprintln!("STDOUT:\n{}", stdout);
        eprintln!("STDERR:\n{}", stderr);
    }

    assert!(
        output.status.success(),
        "venus run --cell should succeed. stderr: {}",
        stderr
    );

    // Should complete with 2 cells (base and doubled)
    assert!(
        stdout.contains("Completed") && stdout.contains("2 cells"),
        "Should run 2 cells (base and doubled). stdout: {}",
        stdout
    );
}

#[test]
fn test_run_nonexistent_cell() {
    let notebook = TestNotebook::new("nonexistent_cell.rs", &simple_notebook());

    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args([
            "run",
            notebook.path().to_str().unwrap(),
            "--cell",
            "nonexistent",
        ])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found"));
}

#[test]
fn test_run_empty_notebook() {
    let empty_source = r#"//! Empty notebook
//!
//! ```cargo
//! [dependencies]
//! ```

// No cells defined
fn helper() -> i32 { 42 }
"#;

    let notebook = TestNotebook::new("empty.rs", empty_source);

    let output = Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["run", notebook.path().to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

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

    // Should succeed but report no cells
    assert!(output.status.success(), "Should succeed even with no cells");
    assert!(
        stdout.contains("No cells found"),
        "Should report no cells. stdout: {}",
        stdout
    );
}

// =============================================================================
// venus sync Tests
// =============================================================================

#[test]
fn test_sync_nonexistent_notebook() {
    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["sync", "/nonexistent/notebook.rs"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found").or(predicate::str::contains("Notebook")));
}

#[test]
fn test_sync_creates_ipynb() {
    let notebook = TestNotebook::new("sync_test.rs", &simple_notebook());

    let output = Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["sync", notebook.path().to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

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

    if !output.status.success() {
        eprintln!("STDOUT:\n{}", stdout);
        eprintln!("STDERR:\n{}", stderr);
    }

    assert!(
        output.status.success(),
        "venus sync should succeed. stderr: {}",
        stderr
    );

    // Check that ipynb file was created
    let ipynb_path = notebook.ipynb_path();
    assert!(
        ipynb_path.exists(),
        "Should create .ipynb file at {:?}",
        ipynb_path
    );
}

#[test]
fn test_sync_generates_valid_json() {
    let notebook = TestNotebook::new("valid_json.rs", &simple_notebook());

    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["sync", notebook.path().to_str().unwrap()])
        .assert()
        .success();

    // Read and parse the ipynb file
    let ipynb_path = notebook.ipynb_path();
    let content = fs::read_to_string(&ipynb_path).expect("Failed to read ipynb file");

    let notebook_json: serde_json::Value =
        serde_json::from_str(&content).expect("ipynb should be valid JSON");

    // Verify basic Jupyter notebook structure
    assert!(
        notebook_json.get("cells").is_some(),
        "Should have 'cells' field"
    );
    assert!(
        notebook_json.get("metadata").is_some(),
        "Should have 'metadata' field"
    );
    assert!(
        notebook_json.get("nbformat").is_some(),
        "Should have 'nbformat' field"
    );

    // Verify cells array
    let cells = notebook_json["cells"]
        .as_array()
        .expect("cells should be array");
    assert!(!cells.is_empty(), "Should have at least one cell");
}

#[test]
fn test_sync_includes_cells() {
    let notebook = TestNotebook::new("with_cells.rs", &simple_notebook());

    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["sync", notebook.path().to_str().unwrap()])
        .assert()
        .success();

    let ipynb_path = notebook.ipynb_path();
    let content = fs::read_to_string(&ipynb_path).expect("Failed to read ipynb file");
    let notebook_json: serde_json::Value =
        serde_json::from_str(&content).expect("ipynb should be valid JSON");

    let cells = notebook_json["cells"]
        .as_array()
        .expect("cells should be array");

    // Find code cells (should have our function definitions)
    let code_cells: Vec<_> = cells.iter().filter(|c| c["cell_type"] == "code").collect();

    // Should have at least 3 code cells (base, doubled, plus_ten)
    assert!(
        code_cells.len() >= 3,
        "Should have at least 3 code cells, found {}",
        code_cells.len()
    );
}

#[test]
fn test_sync_string_notebook() {
    let notebook = TestNotebook::new("strings.rs", &string_notebook());

    let output = Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["sync", notebook.path().to_str().unwrap()])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success(), "venus sync should succeed");

    let ipynb_path = notebook.ipynb_path();
    assert!(ipynb_path.exists(), "Should create .ipynb file");

    // Verify valid JSON
    let content = fs::read_to_string(&ipynb_path).expect("Failed to read ipynb file");
    let _: serde_json::Value = serde_json::from_str(&content).expect("Should be valid JSON");
}

// =============================================================================
// venus new Tests
// =============================================================================

#[test]
fn test_new_creates_notebook() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");

    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .current_dir(temp_dir.path())
        .args(["new", "my_notebook"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Created"));

    let notebook_path = temp_dir.path().join("my_notebook.rs");
    assert!(notebook_path.exists(), "Should create notebook file");

    let content = fs::read_to_string(&notebook_path).expect("Failed to read notebook");
    assert!(
        content.contains("#[venus::cell]"),
        "Should contain cell macro"
    );
    assert!(
        content.contains("venus::prelude"),
        "Should import venus prelude"
    );
}

#[test]
fn test_new_refuses_existing() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let existing = temp_dir.path().join("existing.rs");
    fs::write(&existing, "// existing file").expect("Failed to create file");

    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .current_dir(temp_dir.path())
        .args(["new", "existing"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("already exists"));
}

// =============================================================================
// General CLI Tests
// =============================================================================

#[test]
fn test_help() {
    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Reactive notebook environment"));
}

#[test]
fn test_version() {
    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("venus"));
}

#[test]
fn test_run_help() {
    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["run", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("notebook"));
}

#[test]
fn test_sync_help() {
    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["sync", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("ipynb").or(predicate::str::contains("notebook")));
}

// ============================================================================
// Export Command Tests
// ============================================================================

#[test]
fn test_export_creates_html() {
    let notebook = TestNotebook::new("export_test.rs", &simple_notebook());

    let output_dir = tempfile::TempDir::new().expect("Failed to create temp dir");
    let html_path = output_dir.path().join("output.html");

    let output = Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args([
            "export",
            notebook.path().to_str().unwrap(),
            "-o",
            html_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

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

    if !output.status.success() {
        eprintln!("STDOUT:\n{}", stdout);
        eprintln!("STDERR:\n{}", stderr);
    }

    assert!(
        output.status.success(),
        "venus export should succeed. stderr: {}",
        stderr
    );

    // Check HTML file was created
    assert!(html_path.exists(), "HTML file should be created");

    // Check HTML content
    let html_content = std::fs::read_to_string(&html_path).expect("Failed to read HTML file");
    assert!(
        html_content.contains("<!DOCTYPE html>"),
        "Should be valid HTML"
    );
    assert!(
        html_content.contains("Venus Notebook"),
        "Should have Venus title"
    );
    assert!(html_content.contains("base"), "Should include base cell");
    assert!(
        html_content.contains("doubled"),
        "Should include doubled cell"
    );
}

#[test]
fn test_export_includes_outputs() {
    let notebook = TestNotebook::new("export_outputs.rs", &simple_notebook());

    let output_dir = tempfile::TempDir::new().expect("Failed to create temp dir");
    let html_path = output_dir.path().join("output.html");

    let output = Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args([
            "export",
            notebook.path().to_str().unwrap(),
            "-o",
            html_path.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success(), "venus export should succeed");

    let html_content = std::fs::read_to_string(&html_path).expect("Failed to read HTML file");

    // Should have output sections
    assert!(
        html_content.contains("cell-output"),
        "Should have output sections"
    );

    // Should have success styling
    assert!(
        html_content.contains("class=\"cell success\""),
        "Should mark successful cells"
    );
}

#[test]
fn test_export_help() {
    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["export", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("HTML"));
}

// ============================================================================
// Watch Command Tests
// ============================================================================

#[test]
fn test_watch_help() {
    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["watch", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("auto-run"));
}

#[test]
fn test_watch_nonexistent_notebook() {
    Command::cargo_bin("venus")
        .expect("Failed to find venus binary")
        .args(["watch", "nonexistent.rs"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found"));
}