proef-core 0.6.0

Engine-agnostic core of proef: parsing, binding, lowering, IR, emit, dispatch, World, events, errors
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
//! `render_html` — a self-contained HTML view of a run's event stream.
//!
//! A *derived* view (ADR-0008), never a second record: it replays the same
//! `events.jsonl` the console and `JUnit` reporters consume. Pure and
//! deterministic in `events` (sans-IO core), so it snapshot-locks like the
//! emitter. Events reaching here are already redacted at the sink
//! (`report::sink`), so no secret value can enter the page — the same
//! assumption `explain` makes.

use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::path::Path;

use crate::emit::slugify;
use crate::event::Event;
use crate::step::Status;

/// One step's row in the report.
struct StepRow {
    line: usize,
    text: String,
    status: Status,
    attempts: u32,
    duration_ms: u64,
    detail: Option<String>,
}

/// One scenario's block: identity, aggregate status, and its steps in order.
#[derive(Default)]
struct ScenarioBlock {
    file: String,
    name: String,
    status: Option<Status>,
    steps: Vec<StepRow>,
    /// Run-relative start/end ms and worker index — injected observability
    /// (ADR-0015), present only when the record carries timing. When present
    /// they drive the cross-worker run timeline; absent, the report falls back
    /// to the per-scenario waterfalls alone.
    start_ms: Option<u64>,
    end_ms: Option<u64>,
    worker: Option<u64>,
}

/// Render `events` as a standalone HTML document. `artifacts_href` is the link
/// prefix for each scenario's `.hurl` artifact (e.g. `"artifacts"`, resolved
/// relative to wherever the caller writes the file); the artifact filename is
/// derived with the same slug the emitter uses, so the links match on disk.
pub fn render_html(events: &[Event], artifacts_href: &str) -> String {
    let mut run_id = String::new();
    let mut blocks: Vec<ScenarioBlock> = Vec::new();
    let mut index: BTreeMap<(String, String), usize> = BTreeMap::new();
    let mut total_steps = 0usize;
    let mut total_attempts = 0u64;
    let mut run_finished: Option<(usize, usize, usize)> = None;

    for event in events {
        match event {
            Event::RunStarted { run_id: id, .. } => run_id = id.to_string(),
            Event::StepFinished {
                scenario,
                step,
                status,
                attempts,
                duration_ms,
                detail,
                ..
            } => {
                total_steps += 1;
                total_attempts += u64::from(*attempts);
                let at = block_index(&mut blocks, &mut index, &step.file, scenario);
                blocks[at].steps.push(StepRow {
                    line: step.line,
                    text: step.text.to_string(),
                    status: *status,
                    attempts: *attempts,
                    duration_ms: *duration_ms,
                    detail: detail.clone(),
                });
            }
            Event::ScenarioStarted {
                scenario,
                file,
                timestamp_ms,
                worker,
            } => {
                let at = block_index(&mut blocks, &mut index, file, scenario);
                blocks[at].start_ms = *timestamp_ms;
                blocks[at].worker = *worker;
            }
            Event::ScenarioFinished {
                scenario,
                file,
                status,
                timestamp_ms,
                worker,
            } => {
                let at = block_index(&mut blocks, &mut index, file, scenario);
                blocks[at].status = Some(*status);
                blocks[at].end_ms = *timestamp_ms;
                if blocks[at].worker.is_none() {
                    blocks[at].worker = *worker;
                }
            }
            Event::RunFinished {
                passed,
                failed,
                skipped,
                ..
            } => {
                run_finished = Some((*passed, *failed, *skipped));
            }
            _ => {}
        }
    }

    let (passed, failed, skipped) = suite_totals(run_finished, &blocks);
    // `warned` is informational only — never part of the aligned three
    // numbers above (no other surface breaks it out either) — so it stays a
    // plain count of every rendered block regardless of phase.
    let warned = blocks
        .iter()
        .filter(|block| block.status == Some(Status::Warned))
        .count();

    let mut html = String::with_capacity(2048 + blocks.len() * 256);
    let _ = writeln!(
        html,
        "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n\
         <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\
         <title>proef report — {run}</title>\n<style>{STYLE}</style>\n</head>\n<body>\n\
         <h1>proef run <code>{run}</code></h1>",
        run = esc(&run_id)
    );
    html.push_str("<p class=\"summary\">");
    tally(&mut html, "pass", passed, "passed");
    tally(&mut html, "fail", failed, "failed");
    tally(&mut html, "skip", skipped, "skipped");
    if warned > 0 {
        tally(&mut html, "warn", warned, "warned");
    }
    let _ = writeln!(
        html,
        "<span class=\"steps\">{total_steps} steps · {total_attempts} attempts</span></p>"
    );

    render_timeline(&mut html, &blocks);
    for block in &blocks {
        render_block(&mut html, block, artifacts_href, failed);
    }
    html.push_str("</body>\n</html>\n");
    html
}

/// The headline `(passed, failed, skipped)` — mirrors every other surface
/// that reads `RunFinished` (console `summary:`, `explain`, `--output json`,
/// `JUnit`, TAP, the SLA gate, the exit code): main-suite scenarios only,
/// `[run] setup`/`teardown` excluded (ADR-0014). A truncated record has no
/// `RunFinished` to read, so fall back to counting every rendered block — the
/// only totals a dead run can offer.
fn suite_totals(
    run_finished: Option<(usize, usize, usize)>,
    blocks: &[ScenarioBlock],
) -> (usize, usize, usize) {
    run_finished.unwrap_or_else(|| {
        let (mut passed, mut failed, mut skipped) = (0usize, 0usize, 0usize);
        for block in blocks {
            match block.status {
                Some(Status::Passed) => passed += 1,
                Some(Status::Failed) => failed += 1,
                Some(Status::Skipped) => skipped += 1,
                Some(Status::Warned) | None => {}
            }
        }
        (passed, failed, skipped)
    })
}

/// Find or create the block for `(file, scenario)`, preserving first-seen order.
fn block_index(
    blocks: &mut Vec<ScenarioBlock>,
    index: &mut BTreeMap<(String, String), usize>,
    file: &str,
    scenario: &str,
) -> usize {
    *index
        .entry((file.to_string(), scenario.to_string()))
        .or_insert_with(|| {
            blocks.push(ScenarioBlock {
                file: file.to_string(),
                name: scenario.to_string(),
                ..ScenarioBlock::default()
            });
            blocks.len() - 1
        })
}

/// One `<details>` per scenario — failures open by default so the report leads
/// with what broke. `headline_failed` is the aligned failed count in the
/// summary bar above (`RunFinished`'s, suite-only per ADR-0014, or the
/// counted-block fallback on a truncated record): a block whose own status is
/// `Failed` while that count is `0` cannot be one of the failures the
/// headline counts — it is necessarily a `[run] setup`/`teardown` fault
/// excluded from it, so it is flagged here rather than left to read as the
/// page contradicting its own summary.
fn render_block(
    html: &mut String,
    block: &ScenarioBlock,
    artifacts_href: &str,
    headline_failed: usize,
) {
    let status = block.status.unwrap_or(Status::Skipped);
    let open = if status == Status::Failed {
        " open"
    } else {
        ""
    };
    let _ = write!(
        html,
        "<details class=\"scenario {cls}\"{open}>\n<summary>\
         <span class=\"pill {cls}\">{word}</span> \
         <span class=\"loc\">{file}</span> {name}",
        cls = status_class(status),
        word = status_word(status),
        file = esc(&block.file),
        name = esc(&block.name),
    );
    if status == Status::Failed && headline_failed == 0 {
        html.push_str(
            " <span class=\"phase-note\">setup/teardown — excluded from totals above</span>",
        );
    }
    // Link the artifact only when the scenario actually ran hurl steps (else
    // no `.hurl` was emitted for it).
    if !block.steps.is_empty() {
        let stem = Path::new(&block.file).file_stem().map_or_else(
            || "feature".to_owned(),
            |stem| stem.to_string_lossy().into_owned(),
        );
        let slug = format!("{}--{}", slugify(&stem), slugify(&block.name));
        let _ = write!(
            html,
            " <a class=\"artifact\" href=\"{href}/{slug}.hurl\">artifact</a>",
            href = esc(artifacts_href),
        );
    }
    html.push_str("</summary>\n<ol class=\"steps\">\n");
    // Per-scenario timing waterfall: each step's bar is offset by the steps
    // before it and as wide as its own duration, both as a fraction of the
    // scenario total. Purely derived from `duration_ms` (no timestamps), so it
    // shows the *sequential* cascade within one scenario — not cross-worker
    // occupancy, which would need an injected clock the sans-IO core never reads.
    let total_ms: u64 = block.steps.iter().map(|step| step.duration_ms).sum();
    let mut elapsed_ms: u64 = 0;
    for step in &block.steps {
        let _ = write!(
            html,
            "<li class=\"{cls}\"><span class=\"glyph\">{glyph}</span> {text}\
             <span class=\"meta\">:{line} · {attempts}× · {ms}ms</span>",
            cls = status_class(step.status),
            glyph = status_glyph(step.status),
            text = esc(&step.text),
            line = step.line,
            attempts = step.attempts,
            ms = step.duration_ms,
        );
        if total_ms > 0 {
            let _ = write!(
                html,
                "<span class=\"track\"><span class=\"bar {cls}\" \
                 style=\"margin-left:{offset}%;width:{width}%\"></span></span>",
                cls = status_class(step.status),
                offset = pct(elapsed_ms, total_ms),
                width = pct(step.duration_ms, total_ms),
            );
        }
        elapsed_ms += step.duration_ms;
        if let Some(detail) = &step.detail {
            let _ = write!(html, "<pre class=\"detail\">{}</pre>", esc(detail));
        }
        html.push_str("</li>\n");
    }
    html.push_str("</ol>\n</details>\n");
}

/// The cross-worker run timeline (ADR-0015): a lane per worker, each scenario a
/// bar from its start to its finish, positioned on a shared run-relative axis so
/// concurrency is visible at a glance. Rendered only when the record carries
/// injected timing (`start`/`end` timestamps); absent, the report shows just the
/// per-scenario waterfalls, so old records degrade cleanly.
fn render_timeline(html: &mut String, blocks: &[ScenarioBlock]) {
    let timed: Vec<&ScenarioBlock> = blocks
        .iter()
        .filter(|block| block.start_ms.is_some() && block.end_ms.is_some())
        .collect();
    let Some(max_end) = timed.iter().filter_map(|block| block.end_ms).max() else {
        return; // no timed scenarios — old record, waterfalls only
    };
    if max_end == 0 {
        return; // a zero-length run has nothing to place on the axis
    }
    let mut workers: Vec<u64> = timed
        .iter()
        .map(|block| block.worker.unwrap_or(0))
        .collect();
    workers.sort_unstable();
    workers.dedup();

    let _ = writeln!(
        html,
        "<h2 class=\"timeline-h\">Timeline <span class=\"count\">{max_end}ms</span></h2>\n\
         <div class=\"timeline\">"
    );
    for worker in &workers {
        let _ = write!(
            html,
            "<div class=\"lane\"><span class=\"lane-label\">worker {worker}</span>\
             <div class=\"lane-track\">"
        );
        for block in timed
            .iter()
            .filter(|block| block.worker.unwrap_or(0) == *worker)
        {
            let start = block.start_ms.unwrap_or(0);
            let end = block.end_ms.unwrap_or(start).max(start);
            let _ = write!(
                html,
                "<span class=\"tbar {cls}\" style=\"left:{left}%;width:{width}%\" \
                 title=\"{title} ({dur}ms)\"></span>",
                cls = status_class(block.status.unwrap_or(Status::Skipped)),
                left = pct(start, max_end),
                width = pct(end - start, max_end),
                title = esc(&block.name),
                dur = end - start,
            );
        }
        html.push_str("</div></div>\n");
    }
    html.push_str("</div>\n");
}

/// `n / total` as a percentage string with one decimal place, using integer
/// math only (no lossy float cast). `total` must be non-zero (callers guard).
fn pct(n: u64, total: u64) -> String {
    let permille = u128::from(n) * 1000 / u128::from(total); // 0..=1000
    format!("{}.{}", permille / 10, permille % 10)
}

/// Write one `<span>` count into the summary bar, omitting nothing (callers gate
/// on zero where a bucket should hide).
fn tally(html: &mut String, class: &str, count: usize, word: &str) {
    let _ = write!(html, "<span class=\"count {class}\">{count} {word}</span> ");
}

fn status_class(status: Status) -> &'static str {
    match status {
        Status::Passed => "pass",
        Status::Failed => "fail",
        Status::Skipped => "skip",
        Status::Warned => "warn",
    }
}

fn status_word(status: Status) -> &'static str {
    match status {
        Status::Passed => "passed",
        Status::Failed => "failed",
        Status::Skipped => "skipped",
        Status::Warned => "warned",
    }
}

fn status_glyph(status: Status) -> &'static str {
    match status {
        Status::Passed => "",
        Status::Failed => "",
        Status::Skipped => "·",
        Status::Warned => "",
    }
}

/// HTML-escape text destined for element content or a double-quoted attribute.
fn esc(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\'' => out.push_str("&#39;"),
            _ => out.push(ch),
        }
    }
    out
}

/// Inlined stylesheet — the report is a single self-contained file (no external
/// requests), light/dark aware for a local artifact.
const STYLE: &str = "\
:root{--bg:#fff;--fg:#1a1a1a;--muted:#666;--line:#e2e2e2;--pass:#1a7f37;--fail:#cf222e;--skip:#8a8a8a;--warn:#9a6700;--card:#f6f8fa}\
@media(prefers-color-scheme:dark){:root{--bg:#0d1117;--fg:#e6edf3;--muted:#9aa4af;--line:#30363d;--pass:#3fb950;--fail:#f85149;--skip:#8a8a8a;--warn:#d29922;--card:#161b22}}\
*{box-sizing:border-box}body{margin:0;padding:2rem;max-width:60rem;margin:0 auto;background:var(--bg);color:var(--fg);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif}\
h1{font-size:1.4rem;font-weight:600}code{font-family:ui-monospace,SFMono-Regular,Menlo,monospace}\
.incomplete-banner{color:var(--warn);font-weight:600;margin:0 0 1rem}\
.summary{display:flex;flex-wrap:wrap;gap:.5rem;align-items:center;margin:0 0 1.5rem}\
.count{font-weight:600;padding:.15rem .6rem;border-radius:999px;background:var(--card)}\
.count.pass{color:var(--pass)}.count.fail{color:var(--fail)}.count.skip{color:var(--skip)}.count.warn{color:var(--warn)}\
.summary .steps{color:var(--muted);margin-left:auto}\
.scenario{border:1px solid var(--line);border-radius:8px;margin:.5rem 0;background:var(--card)}\
.scenario summary{cursor:pointer;padding:.6rem .8rem;list-style:none;display:flex;align-items:center;gap:.5rem;flex-wrap:wrap}\
.scenario summary::-webkit-details-marker{display:none}\
.pill{font-size:.72rem;font-weight:700;text-transform:uppercase;letter-spacing:.03em;padding:.1rem .5rem;border-radius:4px;color:#fff}\
.pill.pass{background:var(--pass)}.pill.fail{background:var(--fail)}.pill.skip{background:var(--skip)}.pill.warn{background:var(--warn)}\
.loc{color:var(--muted);font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:.85rem}\
.artifact{margin-left:auto;font-size:.85rem;color:var(--muted)}\
.phase-note{color:var(--muted);font-size:.78rem;font-style:italic}\
.steps{margin:0;padding:.2rem .8rem .8rem 2rem;border-top:1px solid var(--line)}\
.steps li{margin:.3rem 0}.steps .glyph{font-weight:700}\
li.pass .glyph{color:var(--pass)}li.fail .glyph{color:var(--fail)}li.skip .glyph{color:var(--skip)}li.warn .glyph{color:var(--warn)}\
.meta{color:var(--muted);font-size:.8rem;margin-left:.4rem;font-family:ui-monospace,SFMono-Regular,Menlo,monospace}\
.track{display:block;height:4px;margin:.25rem 0 0;background:var(--line);border-radius:2px;overflow:hidden}\
.bar{display:block;height:100%;min-width:1px;border-radius:2px}\
.bar.pass{background:var(--pass)}.bar.fail{background:var(--fail)}.bar.skip{background:var(--skip)}.bar.warn{background:var(--warn)}\
.timeline-h{font-size:1.05rem;font-weight:600;margin:1.5rem 0 .5rem}\
.timeline{margin:0 0 1.5rem}\
.lane{display:flex;align-items:center;gap:.5rem;margin:.25rem 0}\
.lane-label{color:var(--muted);font-size:.75rem;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;min-width:5rem;text-align:right}\
.lane-track{position:relative;flex:1;height:1rem;background:var(--card);border:1px solid var(--line);border-radius:3px}\
.tbar{position:absolute;top:1px;height:calc(100% - 2px);min-width:2px;border-radius:2px;opacity:.9}\
.tbar.pass{background:var(--pass)}.tbar.fail{background:var(--fail)}.tbar.skip{background:var(--skip)}.tbar.warn{background:var(--warn)}\
.detail{background:var(--bg);border:1px solid var(--line);border-radius:6px;padding:.5rem .7rem;margin:.4rem 0 0;white-space:pre-wrap;font-size:.82rem;overflow-x:auto}\
";