bnto-core 0.18.2

Core WASM engine library for Bnto — shared types, traits, and orchestration
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
// Run summary — one serializable verdict for a whole run, so a consumer's
// human summary line and its machine-readable line come from the same values.
// Per-node file counts come from the run's pipeline events (`NodeTally`);
// each node's output metadata comes from `PipelineResult::node_metadata`.

use std::fmt::Display;

use serde::Serialize;

use crate::events::{NodeInfo, PipelineEvent};
use crate::pipeline::{NodeMetadataById, PipelineResult};

/// Whether a run succeeded.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum RunStatus {
    Pass,
    Fail,
}

/// One top-level processing node's part in a run.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct NodeSummary {
    /// The node's id from the recipe.
    pub id: String,
    /// The node's type key (e.g. "file-copy").
    pub node_type: String,
    /// Files the node processed, summed over every time it ran.
    pub files_processed: usize,
    /// The node's recorded output metadata (what `{{node.<id>.*}}` reads);
    /// `{}` when it recorded none or never ran.
    pub metadata: serde_json::Map<String, serde_json::Value>,
    /// Why the node's `when` guard skipped it; left out of the JSON when
    /// the node wasn't skipped.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skip_reason: Option<String>,
}

impl NodeSummary {
    fn not_yet_run(info: &NodeInfo) -> Self {
        Self {
            id: info.id.clone(),
            node_type: info.node_type.clone(),
            files_processed: 0,
            metadata: serde_json::Map::new(),
            skip_reason: None,
        }
    }
}

/// The outcome of a whole run.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RunSummary {
    pub status: RunStatus,
    /// Files the run wrote; the consumer sets it from what its output step did.
    pub files_written: usize,
    pub duration_ms: u64,
    /// Top-level processing nodes in pipeline order.
    pub nodes: Vec<NodeSummary>,
    /// Why the run failed; `None` on a pass.
    pub error: Option<String>,
    /// The text a message-mode run reported (keep-awake's "No active
    /// session."). Left out of the JSON when the run reported none.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

impl RunSummary {
    /// A passing run: one file written per result file until the consumer says otherwise.
    pub fn from_result(result: &PipelineResult, duration_ms: u64) -> Self {
        Self {
            status: RunStatus::Pass,
            files_written: result.files.len(),
            duration_ms,
            nodes: Vec::new(),
            error: None,
            message: None,
        }
    }

    /// A failed run, carrying `error`'s message.
    pub fn from_error(error: impl Display, duration_ms: u64) -> Self {
        Self {
            status: RunStatus::Fail,
            files_written: 0,
            duration_ms,
            nodes: Vec::new(),
            error: Some(error.to_string()),
            message: None,
        }
    }

    /// The same summary with `nodes` in place of its node list.
    pub fn with_nodes(self, nodes: Vec<NodeSummary>) -> Self {
        Self { nodes, ..self }
    }

    /// The same summary reporting `message` as the run's text.
    pub fn with_message(self, message: Option<String>) -> Self {
        Self { message, ..self }
    }

    /// The same summary with each node's metadata looked up by id in `metadata`.
    pub fn with_node_metadata(mut self, metadata: &NodeMetadataById) -> Self {
        for node in &mut self.nodes {
            if let Some(recorded) = metadata.get(&node.id) {
                node.metadata = recorded.clone();
            }
        }
        self
    }
}

/// Builds the per-node summaries from a run's pipeline events.
#[derive(Debug, Default)]
pub struct NodeTally {
    nodes: Vec<NodeSummary>,
}

impl NodeTally {
    /// Fold one event into the tally.
    pub fn record(&mut self, event: &PipelineEvent) {
        match event {
            PipelineEvent::PipelineStarted { nodes, .. } => {
                self.nodes = nodes.iter().map(NodeSummary::not_yet_run).collect();
            }
            // Nodes inside a container count toward the container, not the run.
            PipelineEvent::NodeCompleted {
                node_id,
                files_processed,
                parent_node_id: None,
                ..
            } => self.add_files(node_id, *files_processed),
            PipelineEvent::NodeSkipped {
                node_id,
                reason,
                parent_node_id: None,
                ..
            } => self.mark_skipped(node_id, reason),
            _ => {}
        }
    }

    /// Auto-iteration completes a per-file node once per file, so counts add up.
    fn add_files(&mut self, node_id: &str, files: usize) {
        if let Some(node) = self.nodes.iter_mut().find(|n| n.id == node_id) {
            node.files_processed += files;
        }
    }

    fn mark_skipped(&mut self, node_id: &str, reason: &str) {
        if let Some(node) = self.nodes.iter_mut().find(|n| n.id == node_id) {
            node.skip_reason = Some(reason.to_string());
        }
    }

    /// The tallied nodes, in pipeline order.
    pub fn nodes(&self) -> Vec<NodeSummary> {
        self.nodes.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::errors::BntoError;
    use crate::pipeline::PipelineFileResult;
    use crate::processor::FileData;
    use serde_json::Map;

    fn result_with(names: &[&str], duration_ms: u64) -> PipelineResult {
        let files = names
            .iter()
            .map(|n| PipelineFileResult {
                name: n.to_string(),
                data: FileData::Bytes(vec![]),
                mime_type: "text/plain".into(),
                metadata: serde_json::Map::new(),
            })
            .collect();
        PipelineResult {
            files,
            duration_ms,
            warnings: vec![],
            node_metadata: Default::default(),
        }
    }

    fn started(nodes: &[(&str, &str)]) -> PipelineEvent {
        PipelineEvent::PipelineStarted {
            total_nodes: nodes.len(),
            total_files: 1,
            nodes: nodes
                .iter()
                .map(|(id, t)| NodeInfo {
                    id: id.to_string(),
                    node_type: t.to_string(),
                })
                .collect(),
        }
    }

    fn completed(id: &str, files: usize, parent: Option<&str>) -> PipelineEvent {
        PipelineEvent::NodeCompleted {
            node_id: id.into(),
            duration_ms: 1,
            files_processed: files,
            parent_node_id: parent.map(String::from),
        }
    }

    fn node(id: &str, node_type: &str, files_processed: usize) -> NodeSummary {
        NodeSummary {
            id: id.into(),
            node_type: node_type.into(),
            files_processed,
            metadata: Map::new(),
            skip_reason: None,
        }
    }

    fn skipped(id: &str, parent: Option<&str>) -> PipelineEvent {
        PipelineEvent::NodeSkipped {
            node_id: id.into(),
            node_index: 0,
            total_nodes: 1,
            node_type: "shell-run".into(),
            parent_node_id: parent.map(String::from),
            reason: "not on this OS".into(),
        }
    }

    fn object(value: serde_json::Value) -> Map<String, serde_json::Value> {
        value.as_object().unwrap().clone()
    }

    // --- RunSummary ---

    #[test]
    fn test_from_result_passes_with_the_result_files_and_duration() {
        let summary = RunSummary::from_result(&result_with(&["a.txt", "b.txt"], 9), 42);
        assert_eq!(summary.status, RunStatus::Pass);
        assert_eq!(summary.files_written, 2);
        assert_eq!(summary.duration_ms, 42);
        assert_eq!(summary.error, None);
    }

    #[test]
    fn test_from_result_with_two_tallied_nodes_carries_each_node() {
        let mut tally = NodeTally::default();
        tally.record(&started(&[
            ("collect", "file-collect"),
            ("copy", "file-copy"),
        ]));
        tally.record(&completed("collect", 1, None));
        tally.record(&completed("copy", 3, None));
        let summary =
            RunSummary::from_result(&result_with(&["a", "b", "c"], 5), 5).with_nodes(tally.nodes());
        assert_eq!(
            summary.nodes,
            vec![
                node("collect", "file-collect", 1),
                node("copy", "file-copy", 3)
            ]
        );
    }

    #[test]
    fn test_from_error_fails_with_the_message() {
        let err = BntoError::ProcessingFailed("disk full".into());
        let summary = RunSummary::from_error(&err, 7);
        assert_eq!(summary.status, RunStatus::Fail);
        assert_eq!(summary.error.as_deref(), Some(err.to_string().as_str()));
        assert_eq!(summary.files_written, 0);
        assert_eq!(summary.duration_ms, 7);
        assert!(summary.nodes.is_empty());
    }

    #[test]
    fn test_from_error_takes_a_plain_message() {
        let summary = RunSummary::from_error("unresolved {{vars.Out}}", 0);
        assert_eq!(summary.error.as_deref(), Some("unresolved {{vars.Out}}"));
    }

    #[test]
    fn test_summary_serializes_to_the_documented_shape() {
        let summary = RunSummary::from_result(&result_with(&["a"], 3), 3).with_nodes(vec![node(
            "copy",
            "file-copy",
            1,
        )]);
        let json = serde_json::to_value(&summary).unwrap();
        assert_eq!(
            json,
            serde_json::json!({
                "status": "pass",
                "files_written": 1,
                "duration_ms": 3,
                "nodes": [{
                    "id": "copy",
                    "node_type": "file-copy",
                    "files_processed": 1,
                    "metadata": {}
                }],
                "error": null
            })
        );
    }

    #[test]
    fn test_with_node_metadata_fills_each_node_by_id() {
        let mut result = result_with(&["a"], 1);
        let copied = object(serde_json::json!({ "copied": 3, "up_to_date": 1 }));
        result.node_metadata.insert("copy".into(), copied.clone());
        result.node_metadata.insert("inner".into(), Map::new());
        let nodes = vec![
            node("collect", "file-collect", 1),
            node("copy", "file-copy", 4),
        ];
        let summary = RunSummary::from_result(&result, 1)
            .with_nodes(nodes)
            .with_node_metadata(&result.node_metadata);
        assert_eq!(summary.nodes[0].metadata, Map::new());
        assert_eq!(summary.nodes[1].metadata, copied);
    }

    #[test]
    fn test_node_metadata_serializes_as_an_object() {
        let mut copy = node("copy", "file-copy", 1);
        copy.metadata = object(serde_json::json!({ "copied": 1 }));
        let json = serde_json::to_value(&copy).unwrap();
        assert_eq!(json["metadata"], serde_json::json!({ "copied": 1 }));
    }

    #[test]
    fn test_summary_with_a_message_serializes_it() {
        let summary = RunSummary::from_result(&result_with(&["sh-output"], 1), 1)
            .with_message(Some("No active session.".into()));
        let json = serde_json::to_value(summary).unwrap();
        assert_eq!(json["message"], "No active session.");
    }

    #[test]
    fn test_summary_without_a_message_leaves_the_field_out() {
        let json = serde_json::to_value(RunSummary::from_result(&result_with(&["a"], 1), 1));
        assert!(json.unwrap().get("message").is_none());
        let json = serde_json::to_value(RunSummary::from_error("boom", 1)).unwrap();
        assert!(json.get("message").is_none());
    }

    #[test]
    fn test_failed_summary_serializes_status_fail() {
        let json = serde_json::to_value(RunSummary::from_error("boom", 1)).unwrap();
        assert_eq!(json["status"], "fail");
        assert_eq!(json["error"], "boom");
    }

    // --- NodeTally ---

    #[test]
    fn test_tally_sums_a_node_that_runs_once_per_file() {
        let mut tally = NodeTally::default();
        tally.record(&started(&[("rename", "file-rename")]));
        tally.record(&completed("rename", 1, None));
        tally.record(&completed("rename", 1, None));
        assert_eq!(tally.nodes(), vec![node("rename", "file-rename", 2)]);
    }

    #[test]
    fn test_tally_ignores_nodes_inside_a_container() {
        let mut tally = NodeTally::default();
        tally.record(&started(&[("loop", "loop")]));
        tally.record(&completed("inner", 4, Some("loop")));
        tally.record(&completed("loop", 2, None));
        assert_eq!(tally.nodes(), vec![node("loop", "loop", 2)]);
    }

    #[test]
    fn test_tally_marks_a_skipped_node_with_its_reason() {
        let mut tally = NodeTally::default();
        tally.record(&started(&[("mac", "group"), ("linux", "group")]));
        tally.record(&completed("mac", 1, None));
        tally.record(&skipped("linux", None));
        let nodes = tally.nodes();
        assert_eq!(nodes[0].skip_reason, None);
        assert_eq!(nodes[1].skip_reason.as_deref(), Some("not on this OS"));
    }

    #[test]
    fn test_tally_ignores_a_skip_inside_a_container() {
        let mut tally = NodeTally::default();
        tally.record(&started(&[("g", "group")]));
        tally.record(&skipped("inner", Some("g")));
        tally.record(&completed("g", 1, None));
        assert_eq!(tally.nodes(), vec![node("g", "group", 1)]);
    }

    #[test]
    fn test_skip_reason_is_in_the_json_only_for_a_skipped_node() {
        let mut ran = node("a", "file-copy", 1);
        let json = serde_json::to_value(&ran).unwrap();
        assert!(json.get("skip_reason").is_none(), "{json}");
        ran.skip_reason = Some("when a equals b".into());
        let json = serde_json::to_value(&ran).unwrap();
        assert_eq!(json["skip_reason"], "when a equals b");
    }

    #[test]
    fn test_tally_keeps_a_node_that_never_completed_at_zero() {
        let mut tally = NodeTally::default();
        tally.record(&started(&[("a", "file-collect"), ("b", "file-copy")]));
        tally.record(&completed("a", 1, None));
        assert_eq!(
            tally.nodes(),
            vec![node("a", "file-collect", 1), node("b", "file-copy", 0)]
        );
    }
}