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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// NOTEBOOK-002: Cell Execution Engine Enhancements
// Phase 4: Notebook Excellence - Rich Execution Results
//
// This module provides rich execution results that capture:
// - Return value (expression result)
// - Stdout (print statements)
// - Stderr (warnings, errors)
// - Execution time
// - Success/failure status
//
// Quality Requirements:
// - Cyclomatic Complexity: ≤10 per function (Toyota Way)
// - Line Coverage: ≥85%
// - Branch Coverage: ≥90%

use crate::notebook::html::HtmlFormatter;
use std::time::Duration;

/// Result of executing a notebook cell
///
/// Captures all output, timing, and status information from cell execution.
///
/// # Examples
///
/// ```
/// use ruchy::notebook::execution::CellExecutionResult;
/// use std::time::Duration;
///
/// let result = CellExecutionResult::success(
///     "42".to_string(),
///     String::new(),
///     String::new(),
///     Duration::from_millis(5)
/// );
///
/// assert!(result.is_success());
/// assert_eq!(result.output(), "42");
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct CellExecutionResult {
    /// The return value of the cell execution
    output: String,
    /// Captured stdout (print statements)
    stdout: String,
    /// Captured stderr (warnings, errors)
    stderr: String,
    /// Execution time
    duration: Duration,
    /// Whether execution succeeded
    success: bool,
    /// Error message if execution failed
    error: Option<String>,
}

impl CellExecutionResult {
    /// Create a successful execution result
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::notebook::execution::CellExecutionResult;
    /// use std::time::Duration;
    ///
    /// let result = CellExecutionResult::success(
    ///     "hello".to_string(),
    ///     "debug output".to_string(),
    ///     String::new(),
    ///     Duration::from_micros(100)
    /// );
    ///
    /// assert!(result.is_success());
    /// assert_eq!(result.output(), "hello");
    /// assert_eq!(result.stdout(), "debug output");
    /// ```
    pub fn success(output: String, stdout: String, stderr: String, duration: Duration) -> Self {
        Self {
            output,
            stdout,
            stderr,
            duration,
            success: true,
            error: None,
        }
    }

    /// Create a failed execution result
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::notebook::execution::CellExecutionResult;
    /// use std::time::Duration;
    ///
    /// let result = CellExecutionResult::failure(
    ///     "Parse error: unexpected token".to_string(),
    ///     Duration::from_millis(1)
    /// );
    ///
    /// assert!(!result.is_success());
    /// assert_eq!(result.error().unwrap(), "Parse error: unexpected token");
    /// ```
    pub fn failure(error: String, duration: Duration) -> Self {
        Self {
            output: String::new(),
            stdout: String::new(),
            stderr: String::new(),
            duration,
            success: false,
            error: Some(error),
        }
    }

    /// Check if execution succeeded
    pub fn is_success(&self) -> bool {
        self.success
    }

    /// Get the output value
    pub fn output(&self) -> &str {
        &self.output
    }

    /// Get captured stdout
    pub fn stdout(&self) -> &str {
        &self.stdout
    }

    /// Get captured stderr
    pub fn stderr(&self) -> &str {
        &self.stderr
    }

    /// Get execution duration
    pub fn duration(&self) -> Duration {
        self.duration
    }

    /// Get error message if execution failed
    pub fn error(&self) -> Option<&str> {
        self.error.as_deref()
    }

    /// Get duration in milliseconds
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::notebook::execution::CellExecutionResult;
    /// use std::time::Duration;
    ///
    /// let result = CellExecutionResult::success(
    ///     "42".to_string(),
    ///     String::new(),
    ///     String::new(),
    ///     Duration::from_millis(250)
    /// );
    ///
    /// assert_eq!(result.duration_ms(), 250);
    /// ```
    pub fn duration_ms(&self) -> u128 {
        self.duration.as_millis()
    }

    /// Check if execution was slow (>100ms)
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::notebook::execution::CellExecutionResult;
    /// use std::time::Duration;
    ///
    /// let fast = CellExecutionResult::success(
    ///     "42".to_string(), String::new(), String::new(),
    ///     Duration::from_millis(50)
    /// );
    /// assert!(!fast.is_slow());
    ///
    /// let slow = CellExecutionResult::success(
    ///     "42".to_string(), String::new(), String::new(),
    ///     Duration::from_millis(150)
    /// );
    /// assert!(slow.is_slow());
    /// ```
    pub fn is_slow(&self) -> bool {
        self.duration > Duration::from_millis(100)
    }

    /// Check if there's any stdout output
    pub fn has_stdout(&self) -> bool {
        !self.stdout.is_empty()
    }

    /// Check if there's any stderr output
    pub fn has_stderr(&self) -> bool {
        !self.stderr.is_empty()
    }

    /// Format the execution result as HTML
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::notebook::execution::CellExecutionResult;
    /// use std::time::Duration;
    ///
    /// let result = CellExecutionResult::success(
    ///     "42".to_string(),
    ///     String::new(),
    ///     String::new(),
    ///     Duration::from_millis(5)
    /// );
    ///
    /// let html = result.as_html();
    /// assert!(html.contains("42"));
    /// assert!(html.contains("<div"));
    /// ```
    pub fn as_html(&self) -> String {
        let formatter = HtmlFormatter::new();

        if self.success {
            let mut html = String::new();

            // Add output
            if !self.output.is_empty() {
                html.push_str(&formatter.format_value(&self.output));
            }

            // Add stdout if present
            if self.has_stdout() {
                html.push_str(r#"<div class="notebook-stdout">"#);
                html.push_str(&formatter.format_value(&self.stdout));
                html.push_str("</div>");
            }

            // Add stderr if present
            if self.has_stderr() {
                html.push_str(r#"<div class="notebook-stderr">"#);
                html.push_str(&formatter.format_value(&self.stderr));
                html.push_str("</div>");
            }

            // Add timing info if slow
            if self.is_slow() {
                html.push_str(&format!(
                    r#"<div class="notebook-timing">⏱️ {}ms</div>"#,
                    self.duration_ms()
                ));
            }

            html
        } else {
            // Error formatting
            formatter.format_error(self.error.as_deref().unwrap_or("Unknown error"))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // RED PHASE: Write tests that define expected behavior

    #[test]
    fn test_notebook_002_success_result_creation() {
        let result = CellExecutionResult::success(
            "42".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(5),
        );

        assert!(result.is_success());
        assert_eq!(result.output(), "42");
        assert_eq!(result.stdout(), "");
        assert_eq!(result.stderr(), "");
        assert_eq!(result.duration_ms(), 5);
        assert!(result.error().is_none());
    }

    #[test]
    fn test_notebook_002_failure_result_creation() {
        let result =
            CellExecutionResult::failure("Parse error".to_string(), Duration::from_millis(1));

        assert!(!result.is_success());
        assert_eq!(
            result.error().expect("operation should succeed in test"),
            "Parse error"
        );
        assert_eq!(result.output(), "");
        assert_eq!(result.duration_ms(), 1);
    }

    #[test]
    fn test_notebook_002_result_with_stdout() {
        let result = CellExecutionResult::success(
            "result".to_string(),
            "Hello from print".to_string(),
            String::new(),
            Duration::from_micros(100),
        );

        assert!(result.has_stdout());
        assert!(!result.has_stderr());
        assert_eq!(result.stdout(), "Hello from print");
    }

    #[test]
    fn test_notebook_002_result_with_stderr() {
        let result = CellExecutionResult::success(
            "42".to_string(),
            String::new(),
            "Warning: deprecated".to_string(),
            Duration::from_millis(10),
        );

        assert!(!result.has_stdout());
        assert!(result.has_stderr());
        assert_eq!(result.stderr(), "Warning: deprecated");
    }

    #[test]
    fn test_notebook_002_result_with_both_streams() {
        let result = CellExecutionResult::success(
            "output".to_string(),
            "stdout message".to_string(),
            "stderr message".to_string(),
            Duration::from_millis(20),
        );

        assert!(result.has_stdout());
        assert!(result.has_stderr());
        assert_eq!(result.stdout(), "stdout message");
        assert_eq!(result.stderr(), "stderr message");
    }

    #[test]
    fn test_notebook_002_slow_execution_detection() {
        let fast = CellExecutionResult::success(
            "42".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(50),
        );
        assert!(!fast.is_slow());

        let slow = CellExecutionResult::success(
            "42".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(150),
        );
        assert!(slow.is_slow());

        let at_threshold = CellExecutionResult::success(
            "42".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(100),
        );
        assert!(!at_threshold.is_slow());
    }

    #[test]
    fn test_notebook_002_duration_conversion() {
        let result = CellExecutionResult::success(
            "test".to_string(),
            String::new(),
            String::new(),
            Duration::from_secs(2),
        );

        assert_eq!(result.duration_ms(), 2000);
        assert!(result.is_slow());
    }

    #[test]
    fn test_notebook_002_clone_result() {
        let original = CellExecutionResult::success(
            "data".to_string(),
            "stdout".to_string(),
            "stderr".to_string(),
            Duration::from_millis(42),
        );

        let cloned = original.clone();

        assert_eq!(original, cloned);
        assert_eq!(cloned.output(), "data");
        assert_eq!(cloned.stdout(), "stdout");
    }

    #[test]
    fn test_notebook_002_debug_format() {
        let result = CellExecutionResult::success(
            "42".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(5),
        );

        let debug_str = format!("{result:?}");
        assert!(debug_str.contains("CellExecutionResult"));
        assert!(debug_str.contains("42"));
    }

    #[test]
    fn test_notebook_002_empty_output() {
        let result = CellExecutionResult::success(
            String::new(),
            String::new(),
            String::new(),
            Duration::from_micros(10),
        );

        assert!(result.is_success());
        assert_eq!(result.output(), "");
        assert!(!result.has_stdout());
        assert!(!result.has_stderr());
    }

    #[test]
    fn test_notebook_002_error_with_details() {
        let result = CellExecutionResult::failure(
            "Runtime error: Division by zero at line 42".to_string(),
            Duration::from_millis(3),
        );

        assert!(!result.is_success());
        assert!(result
            .error()
            .expect("operation should succeed in test")
            .contains("Runtime error: Division by zero"));
        assert!(result
            .error()
            .expect("operation should succeed in test")
            .contains("line 42"));
    }

    #[test]
    fn test_notebook_002_multiline_output() {
        let result = CellExecutionResult::success(
            "line1\nline2\nline3".to_string(),
            "debug1\ndebug2".to_string(),
            "warn1\nwarn2".to_string(),
            Duration::from_millis(15),
        );

        assert!(result.output().contains('\n'));
        assert!(result.stdout().contains('\n'));
        assert!(result.stderr().contains('\n'));
    }

    #[test]
    fn test_notebook_002_unicode_output() {
        let result = CellExecutionResult::success(
            "Hello 世界 🌍".to_string(),
            "stdout 日本語".to_string(),
            "stderr Ελληνικά".to_string(),
            Duration::from_millis(8),
        );

        assert_eq!(result.output(), "Hello 世界 🌍");
        assert_eq!(result.stdout(), "stdout 日本語");
        assert_eq!(result.stderr(), "stderr Ελληνικά");
    }

    #[test]
    fn test_notebook_002_large_output() {
        let large_output = "x".repeat(10000);
        let result = CellExecutionResult::success(
            large_output,
            String::new(),
            String::new(),
            Duration::from_millis(25),
        );

        assert_eq!(result.output().len(), 10000);
        assert!(result.is_success());
    }

    #[test]
    fn test_notebook_002_zero_duration() {
        let result = CellExecutionResult::success(
            "instant".to_string(),
            String::new(),
            String::new(),
            Duration::ZERO,
        );

        assert_eq!(result.duration_ms(), 0);
        assert!(!result.is_slow());
    }

    #[test]
    fn test_notebook_002_getters_immutability() {
        let result = CellExecutionResult::success(
            "data".to_string(),
            "log".to_string(),
            "warn".to_string(),
            Duration::from_millis(10),
        );

        // Getters return references, not ownership
        let _out1 = result.output();
        let _out2 = result.output(); // Can call multiple times

        assert_eq!(result.output(), "data");
    }

    #[test]
    fn test_notebook_002_partial_eq() {
        let result1 = CellExecutionResult::success(
            "42".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(5),
        );

        let result2 = CellExecutionResult::success(
            "42".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(5),
        );

        let result3 = CellExecutionResult::success(
            "43".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(5),
        );

        assert_eq!(result1, result2);
        assert_ne!(result1, result3);
    }

    #[test]
    fn test_notebook_002_special_characters_in_error() {
        let result = CellExecutionResult::failure(
            "Error: \"quoted\" 'text' with\nnewlines\tand\ttabs".to_string(),
            Duration::from_millis(2),
        );

        assert!(result
            .error()
            .expect("operation should succeed in test")
            .contains("\"quoted\""));
        assert!(result
            .error()
            .expect("operation should succeed in test")
            .contains('\n'));
        assert!(result
            .error()
            .expect("operation should succeed in test")
            .contains('\t'));
    }

    #[test]
    fn test_notebook_002_very_fast_execution() {
        let result = CellExecutionResult::success(
            "42".to_string(),
            String::new(),
            String::new(),
            Duration::from_nanos(500),
        );

        assert!(result.duration_ms() < 1);
        assert!(!result.is_slow());
    }

    // NOTEBOOK-004: Tests for HTML formatting integration

    #[test]
    fn test_notebook_004_as_html_success() {
        let result = CellExecutionResult::success(
            "42".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(5),
        );

        let html = result.as_html();
        assert!(html.contains("42"));
        assert!(html.contains("<div"));
    }

    #[test]
    fn test_notebook_004_as_html_error() {
        let result =
            CellExecutionResult::failure("Parse error".to_string(), Duration::from_millis(1));

        let html = result.as_html();
        assert!(html.contains("Parse error"));
        assert!(html.contains("error"));
        assert!(html.contains(""));
    }

    #[test]
    fn test_notebook_004_as_html_with_stdout() {
        let result = CellExecutionResult::success(
            "result".to_string(),
            "debug output".to_string(),
            String::new(),
            Duration::from_millis(10),
        );

        let html = result.as_html();
        assert!(html.contains("result"));
        assert!(html.contains("debug output"));
        assert!(html.contains("notebook-stdout"));
    }

    #[test]
    fn test_notebook_004_as_html_with_stderr() {
        let result = CellExecutionResult::success(
            "value".to_string(),
            String::new(),
            "warning message".to_string(),
            Duration::from_millis(10),
        );

        let html = result.as_html();
        assert!(html.contains("value"));
        assert!(html.contains("warning message"));
        assert!(html.contains("notebook-stderr"));
    }

    #[test]
    fn test_notebook_004_as_html_slow_execution() {
        let result = CellExecutionResult::success(
            "slow result".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(150),
        );

        let html = result.as_html();
        assert!(html.contains("slow result"));
        assert!(html.contains("150ms"));
        assert!(html.contains("notebook-timing"));
    }

    #[test]
    fn test_notebook_004_as_html_escapes_dangerous_content() {
        let result = CellExecutionResult::success(
            "<script>alert('xss')</script>".to_string(),
            String::new(),
            String::new(),
            Duration::from_millis(5),
        );

        let html = result.as_html();
        assert!(!html.contains("<script>"));
        assert!(html.contains("&lt;script&gt;"));
    }
}