relux-runtime 0.6.0

Internal: runtime for Relux. No semver guarantees.
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
//! Per-run `index.html` generator. Lists every test in a run with its
//! outcome, duration, progress string, and (when available) a link to the
//! per-test artifact directory. Self-contained — no event-log dependencies.
//!
//! The page chrome (CSS / search-and-filter script) lives in the sibling
//! `run_index.css` and `run_index.js` files. They are embedded via
//! `include_str!` so IDE / lint tooling can apply CSS / JS analyses to
//! them instead of treating them as opaque Rust string literals.

use std::fmt::Write as FmtWrite;
use std::fs;
use std::path::Path;

use crate::report::result::Outcome;
use crate::report::result::TestResult;
use crate::report::result::format_duration;

/// Stylesheet inlined into the `<style>` block of `index.html`.
const CSS: &str = include_str!("run_index.css");

/// Filter / search script inlined into the trailing `<script>` block of
/// `index.html`.
const SCRIPT: &str = include_str!("run_index.js");

const FOOTER: &str = "</body></html>\n";

fn html_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

fn header(title: &str) -> String {
    format!(
        "<!DOCTYPE html>\n<html><head><meta charset=\"utf-8\">\
         <title>{}</title><style>{CSS}</style></head><body>\n",
        html_escape(title)
    )
}

fn total_duration(results: &[TestResult]) -> String {
    let total: std::time::Duration = results.iter().map(|r| r.duration).sum();
    format_duration(total)
}

fn details_for(result: &TestResult) -> String {
    match &result.outcome {
        Outcome::Fail(f) => f.summary(),
        Outcome::Cancelled(c) => c.summary(),
        Outcome::Invalid(reason) | Outcome::Skipped(reason) => reason.clone(),
        Outcome::Pass => String::new(),
    }
}

fn render_group(
    html: &mut String,
    run_dir: &Path,
    label: &str,
    pill_class: &str,
    pill_label: &str,
    rows: &[&TestResult],
) {
    if rows.is_empty() {
        return;
    }
    let _ = writeln!(
        html,
        "<div class=\"group-header\" data-group-header=\"{label}\">\u{2014} {label} \
         <span class=\"count\">({count})</span></div>",
        count = rows.len(),
    );
    let _ = writeln!(html, "<div class=\"group\" data-group=\"{label}\">");
    for r in rows {
        let row_open = match &r.log_dir {
            Some(log_dir) => {
                let rel = log_dir.strip_prefix(run_dir).unwrap_or(log_dir);
                format!(
                    "<a class=\"row\" data-name=\"{name_attr}\" href=\"{href}/event.html\">",
                    name_attr = html_escape(&r.test_name),
                    href = rel.display(),
                )
            }
            None => format!(
                "<div class=\"row\" data-name=\"{name_attr}\">",
                name_attr = html_escape(&r.test_name),
            ),
        };
        let row_close = if r.log_dir.is_some() {
            "</a>"
        } else {
            "</div>"
        };

        let details = details_for(r);
        let details_cell = if matches!(r.outcome, Outcome::Pass) && r.flaky_retries > 0 {
            format!(
                "<span class=\"flaky\">flaky &middot; {}</span>",
                r.flaky_retries
            )
        } else if details.is_empty() {
            String::new()
        } else {
            html_escape(&details)
        };

        let _ = writeln!(
            html,
            "{row_open}\
             <span class=\"test\"><span class=\"name\">{name}</span>\
             <span class=\"path\">{path}</span></span>\
             <span class=\"result\"><span class=\"pill {pill_class}\">{pill_label}</span></span>\
             <span class=\"details\">{details_cell}</span>\
             <span class=\"duration\">{duration}</span>\
             {row_close}",
            name = html_escape(&r.test_name),
            path = html_escape(&r.test_path),
            duration = format_duration(r.duration),
        );
    }
    let _ = writeln!(html, "</div>");
}

pub fn generate(
    run_dir: &Path,
    results: &[TestResult],
    wall_duration: std::time::Duration,
    jobs: usize,
) {
    let html = render(run_dir, results, wall_duration, jobs);
    let path = run_dir.join("index.html");
    let _ = fs::write(path, html);
}

fn render(
    run_dir: &Path,
    results: &[TestResult],
    wall_duration: std::time::Duration,
    jobs: usize,
) -> String {
    let mut html = header("relux run summary");

    let run_name = run_dir.file_name().unwrap_or_default().to_string_lossy();
    let total = results.len();
    let flaky: u32 = results.iter().map(|r| r.flaky_retries).sum();
    let overall_ok = !results.iter().any(|r| {
        matches!(
            r.outcome,
            Outcome::Fail(_) | Outcome::Cancelled(_) | Outcome::Invalid(_)
        )
    });
    let (pill_class, pill_label) = if overall_ok {
        ("pass", "OK")
    } else {
        ("fail", "FAILED")
    };

    let flaky_seg = if flaky > 0 {
        format!(" &middot; {flaky} flaky retries")
    } else {
        String::new()
    };

    let timing = if jobs > 1 {
        format!(
            "{} <span class=\"cum\">({} cumulative in {jobs} jobs)</span>",
            format_duration(wall_duration),
            total_duration(results),
        )
    } else {
        format_duration(wall_duration)
    };

    let _ = writeln!(
        html,
        "<header class=\"appbar\">\
         <span class=\"crumbs\">runs<span class=\"sep\">/</span>\
         <span class=\"run-id\">{}</span></span>\
         <span class=\"pill {pill_class}\">{pill_label}</span>\
         <span class=\"timing\">{timing} &middot; {total} tests{flaky_seg}</span>\
         </header>",
        html_escape(&run_name),
    );

    let mut report_links = Vec::new();
    if run_dir.join("results.tap").exists() {
        report_links.push("<a href=\"results.tap\">TAP</a>");
    }
    if run_dir.join("junit.xml").exists() {
        report_links.push("<a href=\"junit.xml\">JUnit XML</a>");
    }
    if !report_links.is_empty() {
        let _ = writeln!(
            html,
            "<div class=\"reports-bar\">{}</div>",
            report_links.join(" <span class=\"sep\">&middot;</span> ")
        );
    }

    let _ = writeln!(
        html,
        "<div class=\"search-row\">\
         <div class=\"search-input\">\
         <span class=\"glyph\">&#x2315;</span>\
         <input type=\"search\" data-search-input placeholder=\"filter by test name\u{2026}\" aria-label=\"filter test rows\">\
         <span class=\"count\"></span>\
         <kbd class=\"kbd\">\u{2318}S</kbd>\
         </div>\
         </div>"
    );

    let failed_rows: Vec<&TestResult> = results
        .iter()
        .filter(|r| matches!(r.outcome, Outcome::Fail(_)))
        .collect();
    let cancelled_rows: Vec<&TestResult> = results
        .iter()
        .filter(|r| matches!(r.outcome, Outcome::Cancelled(_)))
        .collect();
    let invalid_rows: Vec<&TestResult> = results
        .iter()
        .filter(|r| matches!(r.outcome, Outcome::Invalid(_)))
        .collect();
    let skipped_rows: Vec<&TestResult> = results
        .iter()
        .filter(|r| matches!(r.outcome, Outcome::Skipped(_)))
        .collect();
    let passed_rows: Vec<&TestResult> = results
        .iter()
        .filter(|r| matches!(r.outcome, Outcome::Pass))
        .collect();

    let _ = writeln!(html, "<main>");
    render_group(&mut html, run_dir, "failed", "fail", "FAIL", &failed_rows);
    render_group(
        &mut html,
        run_dir,
        "cancelled",
        "cancel",
        "CANCEL",
        &cancelled_rows,
    );
    render_group(
        &mut html,
        run_dir,
        "invalid",
        "invalid",
        "INVALID",
        &invalid_rows,
    );
    render_group(&mut html, run_dir, "skipped", "skip", "SKIP", &skipped_rows);
    render_group(&mut html, run_dir, "passed", "pass", "PASS", &passed_rows);
    let _ = writeln!(html, "</main>");

    let _ = writeln!(html, "<script>{SCRIPT}</script>");
    html.push_str(FOOTER);
    html
}

#[cfg(test)]
mod tests {
    use crate::report::result::Failure;
    use crate::report::result::FailureContext;
    use crate::report::result::Outcome;
    use crate::report::result::TestResult;
    use std::path::PathBuf;
    use std::time::Duration;

    fn pass(name: &str) -> TestResult {
        TestResult {
            test_name: name.to_string(),
            test_path: format!("tests/{name}.relux"),
            outcome: Outcome::Pass,
            duration: Duration::from_millis(120),
            progress: "1/1".to_string(),
            log_dir: Some(PathBuf::from(format!("/tmp/run/{name}"))),
            warnings: Vec::new(),
            flaky_retries: 0,
        }
    }

    fn fail(name: &str) -> TestResult {
        let mut r = pass(name);
        r.outcome = Outcome::Fail(Failure::Runtime {
            message: "boom".into(),
            span: None,
            shell: Some("default".into()),
            context: FailureContext::pre_vm(),
        });
        r
    }

    fn skip(name: &str, reason: &str) -> TestResult {
        let mut r = pass(name);
        r.outcome = Outcome::Skipped(reason.to_string());
        r
    }

    fn invalid(name: &str, reason: &str) -> TestResult {
        let mut r = pass(name);
        r.outcome = Outcome::Invalid(reason.to_string());
        r
    }

    fn render(run_dir: &std::path::Path, results: &[TestResult]) -> String {
        super::render(run_dir, results, Duration::from_millis(50), 1)
    }

    #[test]
    fn render_contains_basic_markers() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let results = vec![pass("alpha"), fail("beta")];
        let html = render(run_dir, &results);
        assert!(html.contains("alpha"));
        assert!(html.contains("beta"));
        assert!(html.contains("<header class=\"appbar\""));
        assert!(html.contains("class=\"pill fail\""));
        assert!(html.contains("FAILED"));
        assert!(html.contains("run-001"));
    }

    #[test]
    fn render_header_ok_when_all_pass() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let results = vec![pass("alpha"), pass("beta")];
        let html = render(run_dir, &results);
        assert!(html.contains("class=\"pill pass\""));
        assert!(html.contains(">OK<"));
    }

    #[test]
    fn render_timing_shows_wall_only_when_jobs_is_one() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let html = super::render(run_dir, &[pass("alpha")], Duration::from_millis(50), 1);
        assert!(html.contains("50.0 ms"));
        assert!(!html.contains("cumulative"));
    }

    #[test]
    fn render_timing_shows_cumulative_when_jobs_gt_one() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let html = super::render(
            run_dir,
            &[pass("alpha"), pass("beta")],
            Duration::from_millis(50),
            8,
        );
        assert!(html.contains("50.0 ms"));
        assert!(html.contains("240.0 ms cumulative in 8 jobs"));
    }

    #[test]
    fn render_flaky_segment_only_when_nonzero() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let mut r = pass("alpha");
        r.flaky_retries = 2;
        let html_with = render(run_dir, &[r]);
        assert!(html_with.contains("2 flaky retries"));

        let html_without = render(run_dir, &[pass("beta")]);
        assert!(!html_without.contains("flaky retries"));
    }

    #[test]
    fn render_groups_omit_empty() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let results = vec![pass("alpha")];
        let html = render(run_dir, &results);
        assert!(html.contains("data-group=\"passed\""));
        assert!(!html.contains("data-group=\"failed\""));
        assert!(!html.contains("data-group=\"invalid\""));
        assert!(!html.contains("data-group=\"skipped\""));
    }

    #[test]
    fn render_failed_details_carry_failure_summary() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let results = vec![fail("beta")];
        let html = render(run_dir, &results);
        assert!(html.contains("runtime error in shell 'default': boom"));
    }

    #[test]
    fn render_skipped_details_carry_reason() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let results = vec![skip("gamma", "tagged @skip")];
        let html = render(run_dir, &results);
        assert!(html.contains("tagged @skip"));
        assert!(html.contains("data-group=\"skipped\""));
    }

    #[test]
    fn render_invalid_details_carry_reason() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let results = vec![invalid("delta", "could not resolve import")];
        let html = render(run_dir, &results);
        assert!(html.contains("could not resolve import"));
        assert!(html.contains("data-group=\"invalid\""));
    }

    #[test]
    fn render_passed_row_with_flaky_retries_shows_badge() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let mut r = pass("alpha");
        r.flaky_retries = 3;
        let html = render(run_dir, &[r]);
        assert!(html.contains("class=\"flaky\""));
        assert!(html.contains("flaky &middot; 3"));
    }

    #[test]
    fn render_row_is_anchor_when_log_dir_present() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let results = vec![pass("alpha")];
        let html = render(run_dir, &results);
        assert!(html.contains("<a class=\"row\""));
        assert!(html.contains("alpha/event.html"));
    }

    #[test]
    fn render_row_is_div_when_log_dir_absent() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let mut r = pass("alpha");
        r.log_dir = None;
        let html = render(run_dir, &[r]);
        assert!(html.contains("<div class=\"row\""));
        assert!(!html.contains("alpha/event.html"));
    }

    #[test]
    fn render_includes_search_input_with_data_attr() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let html = render(run_dir, &[pass("alpha")]);
        assert!(html.contains("data-search-input"));
        assert!(html.contains("class=\"kbd\""));
    }

    #[test]
    fn render_includes_inline_script() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let html = render(run_dir, &[pass("alpha")]);
        assert!(html.contains("<script>"));
        assert!(html.contains("addEventListener"));
        assert!(html.contains("data-search-input"));
    }

    #[test]
    fn render_group_order_failed_invalid_skipped_passed() {
        let run_dir = std::path::Path::new("/tmp/run-001");
        let results = vec![
            pass("p1"),
            skip("s1", "skip-reason"),
            fail("f1"),
            invalid("i1", "invalid-reason"),
        ];
        let html = render(run_dir, &results);
        let pos_failed = html.find("data-group=\"failed\"").unwrap();
        let pos_invalid = html.find("data-group=\"invalid\"").unwrap();
        let pos_skipped = html.find("data-group=\"skipped\"").unwrap();
        let pos_passed = html.find("data-group=\"passed\"").unwrap();
        assert!(pos_failed < pos_invalid);
        assert!(pos_invalid < pos_skipped);
        assert!(pos_skipped < pos_passed);
    }
}