Skip to main content

astrs_cli/command/
monitor.rs

1//! `astrs list`, `astrs logs [-f]` and the dataflow half of `astrs status`
2//! (blueprint §17's Monitoring row).
3//!
4//! ```text
5//!   astrs list          ─► List{all}            ─► DataflowList  ─► table
6//!   astrs status <ref>  ─► Info{include_nodes}  ─► DataflowList  ─► table
7//!                          Check{dataflow}      ─► DataflowResult (terminal)
8//!   astrs logs <ref>    ─► Logs{query}          ─► Logs{records}  ─► lines
9//!   astrs logs -f       ─► LogSubscribe         ─► Log frames ────► lines
10//!                          TopicUnsubscribe     ◄── Ctrl-C
11//! ```
12//!
13//! # Read verbs, deliberately
14//!
15//! Blueprint §16 splits the coordinator API into read verbs (list, logs,
16//! topic) and mutating ones (start, stop, param set). Everything in this
17//! module is on the read side, so none of it *requires* a token: a
18//! development coordinator started without one is still inspectable, which
19//! is exactly the setup a first-time user has. A coordinator that does
20//! demand a token refuses the greeting, and that refusal is reported with
21//! the address that was dialled.
22//!
23//! # `logs` and `logs -f` are two different requests
24//!
25//! [`astrs_wire::ControlRequest::Logs`] takes a **required**
26//! [`astrs_wire::DataflowId`] and answers with a bounded batch;
27//! [`astrs_wire::ControlRequest::LogSubscribe`] takes an *optional* one and
28//! pushes frames until the subscription ends. So `astrs logs -f` with no
29//! dataflow named follows the whole cluster, while `astrs logs` with none
30//! resolves the single dataflow that exists and refuses to guess between
31//! two — see [`crate::command::client::Client::sole_dataflow`].
32
33use std::io::Write;
34use std::time::Instant;
35
36use astrs_wire::{
37    ControlReply, ControlRequest, DataflowId, DataflowResult, DataflowSummary, LogLevel, LogQuery,
38    LogRecord, NodeId, NodeInfo,
39};
40
41use crate::command::client::{
42    Client, DataflowRef, Endpoint, new_subscription_id, reply_name, runtime,
43};
44use crate::command::log_stream::{LogFilter, LogStyle, StreamItem, prefix_width, render};
45use crate::command::signals::Signals;
46use crate::error::CliError;
47
48/// `astrs list`'s arguments.
49#[derive(Debug, Clone, Default)]
50pub struct ListArgs {
51    /// Include dataflows that have already finished.
52    pub all: bool,
53    /// Emit JSON rather than a table.
54    pub json: bool,
55}
56
57/// What `astrs list` found.
58#[derive(Debug, Clone)]
59pub struct ListReport {
60    /// One row per dataflow, in the order the coordinator returned them.
61    pub dataflows: Vec<DataflowSummary>,
62}
63
64impl ListReport {
65    /// The human table.
66    ///
67    /// Fixed-width columns rather than a box-drawing table: `astrs list |
68    /// grep` and `astrs list | awk '{print $1}'` are what operators
69    /// actually do with this, and both break the moment a border character
70    /// appears.
71    #[must_use]
72    pub fn table(&self) -> String {
73        if self.dataflows.is_empty() {
74            return "no dataflows (start one with `astrs start <manifest>`)".to_owned();
75        }
76        let mut lines = vec![format!(
77            "{:<36} {:<20} {:<10} {:>5} {:>7}  {}",
78            "ID", "NAME", "STATUS", "NODES", "RUNNING", "DAEMONS"
79        )];
80        for summary in &self.dataflows {
81            lines.push(format!(
82                "{:<36} {:<20} {:<10} {:>5} {:>7}  {}",
83                summary.id.to_string(),
84                summary.name.clone().unwrap_or_else(|| "-".to_owned()),
85                summary.status.as_str(),
86                summary.node_count,
87                summary.running_nodes,
88                if summary.daemons.is_empty() {
89                    "-".to_owned()
90                } else {
91                    summary
92                        .daemons
93                        .iter()
94                        .map(ToString::to_string)
95                        .collect::<Vec<_>>()
96                        .join(",")
97                }
98            ));
99        }
100        lines.join("\n")
101    }
102
103    /// The `--json` form.
104    #[must_use]
105    pub fn to_json(&self) -> serde_json::Value {
106        serde_json::json!({
107            "count": self.dataflows.len(),
108            "dataflows": self
109                .dataflows
110                .iter()
111                .map(summary_json)
112                .collect::<Vec<_>>(),
113        })
114    }
115}
116
117/// One dataflow summary as JSON.
118fn summary_json(summary: &DataflowSummary) -> serde_json::Value {
119    serde_json::json!({
120        "id": summary.id.to_string(),
121        "name": summary.name,
122        "status": summary.status.as_str(),
123        "nodes": summary.node_count,
124        "running": summary.running_nodes,
125        "daemons": summary
126            .daemons
127            .iter()
128            .map(ToString::to_string)
129            .collect::<Vec<_>>(),
130        "started_at": summary.started_at.map(|stamp| stamp.to_string()),
131    })
132}
133
134/// Lists the cluster's dataflows.
135///
136/// # Errors
137///
138/// - [`CliError::NoCluster`] if nothing is listening.
139/// - As [`Client::request`] for a refusal or a protocol failure.
140pub fn list(
141    out: &mut dyn Write,
142    endpoint: &Endpoint,
143    args: &ListArgs,
144) -> Result<ListReport, CliError> {
145    let runtime = runtime()?;
146    let dataflows = runtime.block_on(async {
147        let mut client = Client::connect(endpoint).await?;
148        client.list_dataflows(args.all).await
149    })?;
150    let report = ListReport { dataflows };
151    emit(out, args.json, &report.table(), || report.to_json());
152    Ok(report)
153}
154
155/// `astrs logs`'s arguments.
156#[derive(Debug, Clone, Default)]
157pub struct LogsArgs {
158    /// The dataflow to read; resolved or followed cluster-wide when absent.
159    pub dataflow: Option<DataflowRef>,
160    /// Only this node's records.
161    pub node: Option<String>,
162    /// Hide records below this level.
163    pub level: Option<LogLevel>,
164    /// At most this many records in a dump.
165    pub limit: Option<u32>,
166    /// Keep streaming instead of dumping.
167    pub follow: bool,
168    /// Emit JSON rather than rendered lines.
169    pub json: bool,
170    /// Colorize rendered lines.
171    pub color: bool,
172}
173
174/// What one `astrs logs` did.
175#[derive(Debug, Clone)]
176pub struct LogsReport {
177    /// How many records were printed.
178    pub printed: usize,
179    /// Whether a dump was cut short by the query's limit.
180    pub truncated: bool,
181    /// Whether this was a `-f` stream rather than a dump.
182    pub followed: bool,
183}
184
185impl LogsReport {
186    /// The `--json` form (a `-f` stream prints records as it goes, so this
187    /// is the trailer rather than the payload).
188    #[must_use]
189    pub fn to_json(&self) -> serde_json::Value {
190        serde_json::json!({
191            "printed": self.printed,
192            "truncated": self.truncated,
193            "followed": self.followed,
194        })
195    }
196}
197
198/// Dumps, or follows, a dataflow's logs.
199///
200/// # Errors
201///
202/// - [`CliError::BadArgument`] if `--node` is not a usable node id.
203/// - [`CliError::UnknownDataflow`] if no dataflow was named and the cluster
204///   does not have exactly one.
205/// - [`CliError::NoCluster`] if nothing is listening.
206/// - As [`Client::request`] otherwise.
207pub fn logs(
208    out: &mut dyn Write,
209    endpoint: &Endpoint,
210    args: &LogsArgs,
211) -> Result<LogsReport, CliError> {
212    let node = parse_node(args.node.as_deref())?;
213    let mut query = LogQuery::new();
214    if let Some(level) = args.level {
215        query = query.with_min_level(level);
216    }
217    query.limit = args.limit.or(query.limit);
218
219    let filter = LogFilter::new()
220        .with_min_level(args.level.unwrap_or(LogLevel::Trace))
221        .with_node(node.clone());
222    let runtime = runtime()?;
223
224    if args.follow {
225        return runtime.block_on(follow(out, endpoint, args, node, query, &filter));
226    }
227
228    let (records, truncated) = runtime.block_on(async {
229        let mut client = Client::connect(endpoint).await?;
230        let dataflow = match &args.dataflow {
231            Some(reference) => client.resolve(reference).await?,
232            None => client.sole_dataflow(true).await?,
233        };
234        match client
235            .request(
236                "logs",
237                &ControlRequest::Logs {
238                    dataflow,
239                    node: node.clone(),
240                    query,
241                },
242            )
243            .await?
244        {
245            ControlReply::Logs { records, truncated } => Ok((records, truncated)),
246            other => Err(CliError::UnexpectedReply {
247                request: "logs",
248                reply: reply_name(&other),
249            }),
250        }
251    })?;
252
253    if args.json {
254        let rendered: Vec<serde_json::Value> = records.iter().map(record_json).collect();
255        let _ = writeln!(
256            out,
257            "{}",
258            serde_json::to_string_pretty(&serde_json::json!({
259                "truncated": truncated,
260                "records": rendered,
261            }))
262            .unwrap_or_else(|_| "{}".to_owned())
263        );
264    } else {
265        let style = LogStyle {
266            color: args.color,
267            prefix_width: prefix_width(
268                records
269                    .iter()
270                    .filter_map(|record| record.node.as_ref().map(NodeId::as_str)),
271            ),
272            elapsed: false,
273        };
274        for record in &records {
275            let item = StreamItem::Record(Box::new(record.clone()));
276            if filter.accepts(&item) {
277                let _ = writeln!(out, "{}", render(&item, style, std::time::Duration::ZERO));
278            }
279        }
280        if truncated {
281            let _ = writeln!(
282                out,
283                "(truncated: the coordinator's page limit was reached — narrow the query or pass --limit)"
284            );
285        }
286        let _ = out.flush();
287    }
288    Ok(LogsReport {
289        printed: records.len(),
290        truncated,
291        followed: false,
292    })
293}
294
295/// The `-f` path: subscribe, print pushed frames, unsubscribe on Ctrl-C.
296///
297/// The subscription is closed explicitly rather than left to the socket
298/// closing, so a coordinator that outlives this CLI does not keep fanning
299/// records at a session that stopped reading them.
300async fn follow(
301    out: &mut dyn Write,
302    endpoint: &Endpoint,
303    args: &LogsArgs,
304    node: Option<NodeId>,
305    query: LogQuery,
306    filter: &LogFilter,
307) -> Result<LogsReport, CliError> {
308    let mut client = Client::connect(endpoint).await?;
309    let dataflow: Option<DataflowId> = match &args.dataflow {
310        Some(reference) => Some(client.resolve(reference).await?),
311        None => None,
312    };
313    let subscription = new_subscription_id();
314    client
315        .request_ok(
316            "logs -f",
317            &ControlRequest::LogSubscribe {
318                dataflow,
319                node,
320                query,
321                subscription,
322            },
323        )
324        .await?;
325
326    let style = LogStyle {
327        color: args.color,
328        prefix_width: prefix_width(std::iter::empty()),
329        elapsed: false,
330    };
331    let started = Instant::now();
332    let mut printed = 0usize;
333    let mut signals = Signals::install();
334    let mut interrupted = false;
335
336    loop {
337        // `client` is borrowed by the frame arm for the whole `select!`, so
338        // the interrupt arm records the fact and the unsubscribe happens
339        // after the loop rather than inside it.
340        tokio::select! {
341            frame = client.next_frame() => {
342                match frame? {
343                    Some(frame) => {
344                        printed += usize::from(print_frame(out, &frame, filter, style, started, args.json));
345                    }
346                    // The coordinator closed the connection: the cluster
347                    // went down, which ends a tail rather than failing it.
348                    None => break,
349                }
350            }
351            () = signals.next() => {
352                interrupted = true;
353                break;
354            }
355        }
356    }
357
358    if interrupted {
359        let _ = client
360            .request_ok(
361                "logs -f",
362                &ControlRequest::TopicUnsubscribe { subscription },
363            )
364            .await;
365    }
366    let report = LogsReport {
367        printed,
368        truncated: false,
369        followed: true,
370    };
371    if args.json {
372        let _ = writeln!(
373            out,
374            "{}",
375            serde_json::to_string(&report.to_json()).unwrap_or_else(|_| "{}".to_owned())
376        );
377        let _ = out.flush();
378    }
379    Ok(report)
380}
381
382/// Prints one pushed frame, returning whether anything was written.
383fn print_frame(
384    out: &mut dyn Write,
385    frame: &astrs_wire::Frame,
386    filter: &LogFilter,
387    style: LogStyle,
388    started: Instant,
389    json: bool,
390) -> bool {
391    use astrs_wire::{FrameKind, LogFrame, WireDecode};
392
393    if frame.kind() != FrameKind::Log {
394        return false;
395    }
396    let Ok(log) = LogFrame::decode_exact(frame.payload()) else {
397        // One malformed record must not end a tail that is otherwise fine.
398        return false;
399    };
400    let item = StreamItem::Record(Box::new(log.record));
401    if !filter.accepts(&item) {
402        return false;
403    }
404    let line = if json {
405        match &item {
406            StreamItem::Record(record) => {
407                serde_json::to_string(&record_json(record)).unwrap_or_default()
408            }
409            StreamItem::Notice { message, .. } => message.clone(),
410        }
411    } else {
412        render(&item, style, started.elapsed())
413    };
414    if writeln!(out, "{line}").is_err() {
415        return false;
416    }
417    let _ = out.flush();
418    true
419}
420
421/// One log record as JSON.
422fn record_json(record: &LogRecord) -> serde_json::Value {
423    serde_json::json!({
424        "timestamp": record.timestamp.to_string(),
425        "level": level_word(record.level),
426        "dataflow": record.dataflow.map(|id| id.to_string()),
427        "node": record.node.as_ref().map(NodeId::as_str),
428        "target": record.target,
429        "message": record.message,
430        "fields": record.fields,
431    })
432}
433
434/// A stable lowercase word per level, for JSON output.
435const fn level_word(level: LogLevel) -> &'static str {
436    match level {
437        LogLevel::Trace => "trace",
438        LogLevel::Debug => "debug",
439        LogLevel::Info => "info",
440        LogLevel::Warn => "warn",
441        LogLevel::Error => "error",
442        // `LogLevel` is `#[non_exhaustive]`; an unknown severity keeps its
443        // own word rather than being reported as one it is not.
444        _ => "unknown",
445    }
446}
447
448/// `astrs status <dataflow>`'s arguments.
449#[derive(Debug, Clone, Default)]
450pub struct DataflowStatusArgs {
451    /// The dataflow to describe; the cluster's only one when absent.
452    pub dataflow: Option<DataflowRef>,
453    /// Emit JSON rather than a table.
454    pub json: bool,
455}
456
457/// What `astrs status <dataflow>` found.
458#[derive(Debug, Clone)]
459pub struct DataflowStatusReport {
460    /// The dataflow's row.
461    pub summary: Option<DataflowSummary>,
462    /// One row per node.
463    pub nodes: Vec<NodeInfo>,
464    /// The final verdict, when the dataflow has already reached one.
465    pub result: Option<DataflowResult>,
466}
467
468impl DataflowStatusReport {
469    /// `0` while the dataflow is healthy or still running, `1` once it has
470    /// finished with a failure — so a script can poll it.
471    #[must_use]
472    pub fn exit_code(&self) -> i32 {
473        i32::from(
474            self.result
475                .as_ref()
476                .is_some_and(DataflowResult::has_failures),
477        )
478    }
479
480    /// The human table.
481    #[must_use]
482    pub fn table(&self) -> String {
483        let mut lines = Vec::new();
484        if let Some(summary) = &self.summary {
485            lines.push(format!(
486                "{} ({}) {} — {}/{} node(s) running",
487                summary.id,
488                summary.name.clone().unwrap_or_else(|| "unnamed".to_owned()),
489                summary.status.as_str(),
490                summary.running_nodes,
491                summary.node_count
492            ));
493        }
494        if !self.nodes.is_empty() {
495            lines.push(format!(
496                "{:<20} {:<10} {:>8} {:>9}  {}",
497                "NODE", "STATE", "PID", "RESTARTS", "EXIT"
498            ));
499            for node in &self.nodes {
500                lines.push(format!(
501                    "{:<20} {:<10} {:>8} {:>9}  {}",
502                    node.node.as_str(),
503                    node.state.as_str(),
504                    node.pid
505                        .map_or_else(|| "-".to_owned(), |pid| pid.to_string()),
506                    node.restart_count,
507                    node.exit_cause
508                        .as_ref()
509                        .map_or_else(|| "-".to_owned(), ToString::to_string)
510                ));
511            }
512        }
513        if let Some(result) = &self.result {
514            lines.push(format!(
515                "final: {} ({} node result(s), {} failed)",
516                result.status.as_str(),
517                result.node_results.len(),
518                result
519                    .node_results
520                    .values()
521                    .filter(|cause| cause.is_failure())
522                    .count()
523            ));
524        }
525        if lines.is_empty() {
526            lines.push("the coordinator knows nothing about that dataflow".to_owned());
527        }
528        lines.join("\n")
529    }
530
531    /// The `--json` form.
532    #[must_use]
533    pub fn to_json(&self) -> serde_json::Value {
534        serde_json::json!({
535            "dataflow": self.summary.as_ref().map(summary_json),
536            "nodes": self
537                .nodes
538                .iter()
539                .map(|node| serde_json::json!({
540                    "node": node.node.as_str(),
541                    "state": node.state.as_str(),
542                    "pid": node.pid,
543                    "generation": node.generation,
544                    "restarts": node.restart_count,
545                    "daemon": node.daemon.to_string(),
546                    "exit_cause": node.exit_cause.as_ref().map(ToString::to_string),
547                }))
548                .collect::<Vec<_>>(),
549            "final": self.result.as_ref().map(|result| serde_json::json!({
550                "status": result.status.as_str(),
551                "failed": result.has_failures(),
552                "message": result.message,
553            })),
554            "exit_code": self.exit_code(),
555        })
556    }
557}
558
559/// Describes one dataflow: its row, its nodes, and its verdict if it has
560/// one.
561///
562/// # Errors
563///
564/// - [`CliError::UnknownDataflow`] if the reference resolves to nothing.
565/// - [`CliError::NoCluster`] if nothing is listening.
566/// - As [`Client::request`] otherwise.
567pub fn dataflow_status(
568    out: &mut dyn Write,
569    endpoint: &Endpoint,
570    args: &DataflowStatusArgs,
571) -> Result<DataflowStatusReport, CliError> {
572    let runtime = runtime()?;
573    let report = runtime.block_on(async {
574        let mut client = Client::connect(endpoint).await?;
575        let dataflow = match &args.dataflow {
576            Some(reference) => client.resolve(reference).await?,
577            None => client.sole_dataflow(true).await?,
578        };
579        let (summary, nodes) = match client
580            .request(
581                "status",
582                &ControlRequest::Info {
583                    dataflow,
584                    include_nodes: true,
585                },
586            )
587            .await?
588        {
589            ControlReply::DataflowList { dataflows, nodes } => {
590                (dataflows.into_iter().next(), nodes)
591            }
592            other => {
593                return Err(CliError::UnexpectedReply {
594                    request: "status",
595                    reply: reply_name(&other),
596                });
597            }
598        };
599        let result = match client
600            .request(
601                "status",
602                &ControlRequest::Check {
603                    dataflow: Some(dataflow),
604                },
605            )
606            .await?
607        {
608            ControlReply::DataflowResult { result } => Some(*result),
609            // `Ok` is the coordinator's answer for "known, not finished".
610            _ => None,
611        };
612        Ok::<DataflowStatusReport, CliError>(DataflowStatusReport {
613            summary,
614            nodes,
615            result,
616        })
617    })?;
618    emit(out, args.json, &report.table(), || report.to_json());
619    Ok(report)
620}
621
622/// Validates a `--node` argument.
623///
624/// # Errors
625///
626/// [`CliError::BadArgument`] naming what a node id may contain.
627fn parse_node(node: Option<&str>) -> Result<Option<NodeId>, CliError> {
628    let Some(text) = node else {
629        return Ok(None);
630    };
631    NodeId::new(text)
632        .map(Some)
633        .map_err(|error| CliError::BadArgument {
634            flag: "node",
635            value: text.to_owned(),
636            reason: error.to_string(),
637        })
638}
639
640/// Writes either the human text or the JSON object.
641fn emit(out: &mut dyn Write, json: bool, text: &str, value: impl FnOnce() -> serde_json::Value) {
642    if json {
643        let _ = writeln!(
644            out,
645            "{}",
646            serde_json::to_string_pretty(&value()).unwrap_or_else(|_| "{}".to_owned())
647        );
648    } else {
649        let _ = writeln!(out, "{text}");
650    }
651    let _ = out.flush();
652}
653
654#[cfg(test)]
655mod tests {
656    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
657
658    use super::*;
659    use astrs_time::HlcTimestamp;
660    use astrs_wire::{AuthToken, DataflowStatus, NodeExitCause, NodeRunState};
661
662    fn endpoint() -> Endpoint {
663        // Port 1 on loopback: never bound by this suite, refused instantly.
664        Endpoint::new(
665            std::net::SocketAddr::from(([127, 0, 0, 1], 1)),
666            AuthToken::ZERO,
667        )
668    }
669
670    fn summary(name: Option<&str>, status: DataflowStatus) -> DataflowSummary {
671        DataflowSummary {
672            id: DataflowId::from_u128(7),
673            name: name.map(ToOwned::to_owned),
674            status,
675            daemons: Vec::new(),
676            node_count: 3,
677            running_nodes: 2,
678            started_at: Some(HlcTimestamp::new(1, 0)),
679        }
680    }
681
682    #[test]
683    fn an_empty_list_says_how_to_start_something() {
684        let report = ListReport {
685            dataflows: Vec::new(),
686        };
687        assert!(report.table().contains("astrs start"), "{}", report.table());
688        assert_eq!(report.to_json()["count"], 0);
689    }
690
691    #[test]
692    fn a_list_table_has_one_header_and_one_row_per_dataflow() {
693        let report = ListReport {
694            dataflows: vec![
695                summary(Some("perception"), DataflowStatus::Running),
696                summary(None, DataflowStatus::Finished),
697            ],
698        };
699        let table = report.table();
700        assert_eq!(table.lines().count(), 3, "{table}");
701        assert!(table.contains("perception"), "{table}");
702        assert!(table.contains("running"), "{table}");
703        assert!(table.contains("finished"), "{table}");
704    }
705
706    #[test]
707    fn list_json_carries_every_row() {
708        let report = ListReport {
709            dataflows: vec![summary(Some("perception"), DataflowStatus::Running)],
710        };
711        let json = report.to_json();
712        assert_eq!(json["count"], 1);
713        assert_eq!(json["dataflows"][0]["name"], "perception");
714        assert_eq!(json["dataflows"][0]["status"], "running");
715        assert_eq!(json["dataflows"][0]["nodes"], 3);
716    }
717
718    #[test]
719    fn a_node_filter_must_be_a_usable_node_id() {
720        assert!(parse_node(None).unwrap().is_none());
721        assert_eq!(
722            parse_node(Some("cam"))
723                .unwrap()
724                .map(|id| id.as_str().to_owned()),
725            Some("cam".to_owned())
726        );
727        let error = parse_node(Some("not a node")).unwrap_err();
728        match error {
729            CliError::BadArgument { flag, .. } => assert_eq!(flag, "node"),
730            other => panic!("expected BadArgument, got {other}"),
731        }
732    }
733
734    #[test]
735    fn every_level_has_a_stable_json_word() {
736        assert_eq!(level_word(LogLevel::Trace), "trace");
737        assert_eq!(level_word(LogLevel::Warn), "warn");
738        assert_eq!(level_word(LogLevel::Error), "error");
739    }
740
741    #[test]
742    fn a_record_renders_every_field_a_script_needs() {
743        let record = LogRecord::new(HlcTimestamp::new(3, 1), LogLevel::Warn, "slow frame")
744            .with_node(NodeId::new("cam").unwrap());
745        let json = record_json(&record);
746        assert_eq!(json["level"], "warn");
747        assert_eq!(json["node"], "cam");
748        assert_eq!(json["message"], "slow frame");
749        assert!(json["timestamp"].is_string());
750    }
751
752    #[test]
753    fn a_dataflow_status_table_lists_nodes_and_the_final_verdict() {
754        let mut result = DataflowResult::new(DataflowId::from_u128(7), HlcTimestamp::new(4, 0));
755        result.status = DataflowStatus::Failed;
756        result.record(
757            NodeId::new("cam").unwrap(),
758            NodeExitCause::ExitCode { code: 3 },
759        );
760        let report = DataflowStatusReport {
761            summary: Some(summary(Some("perception"), DataflowStatus::Failed)),
762            nodes: vec![NodeInfo {
763                dataflow: DataflowId::from_u128(7),
764                node: NodeId::new("cam").unwrap(),
765                daemon: astrs_wire::DaemonId::generate(None),
766                state: NodeRunState::Failed,
767                pid: Some(4242),
768                generation: 1,
769                restart_count: 2,
770                inputs: std::collections::BTreeMap::new(),
771                outputs: std::collections::BTreeMap::new(),
772                started_at: None,
773                exit_cause: Some(NodeExitCause::ExitCode { code: 3 }),
774            }],
775            result: Some(result),
776        };
777
778        let table = report.table();
779        assert!(table.contains("perception"), "{table}");
780        assert!(table.contains("cam"), "{table}");
781        assert!(table.contains("4242"), "{table}");
782        assert!(table.contains("final: failed"), "{table}");
783        assert_eq!(report.exit_code(), 1);
784
785        let json = report.to_json();
786        assert_eq!(json["nodes"][0]["restarts"], 2);
787        assert_eq!(json["final"]["failed"], true);
788        assert_eq!(json["exit_code"], 1);
789    }
790
791    #[test]
792    fn a_dataflow_with_no_verdict_yet_exits_zero() {
793        let report = DataflowStatusReport {
794            summary: Some(summary(Some("perception"), DataflowStatus::Running)),
795            nodes: Vec::new(),
796            result: None,
797        };
798        assert_eq!(report.exit_code(), 0);
799        assert!(report.table().contains("running"));
800    }
801
802    #[test]
803    fn an_empty_status_report_says_the_coordinator_knows_nothing() {
804        let report = DataflowStatusReport {
805            summary: None,
806            nodes: Vec::new(),
807            result: None,
808        };
809        assert!(
810            report.table().contains("knows nothing"),
811            "{}",
812            report.table()
813        );
814    }
815
816    #[test]
817    fn a_pushed_log_frame_is_rendered_and_a_filtered_one_is_not() {
818        use astrs_wire::{Frame, FrameFlags, FrameKind, LogFrame, SubscriptionId, WireEncode};
819
820        let record = LogRecord::new(HlcTimestamp::new(2, 0), LogLevel::Info, "frame 1")
821            .with_node(NodeId::new("cam").unwrap());
822        let frame = Frame::new(
823            FrameKind::Log,
824            FrameFlags::EMPTY,
825            LogFrame::new(SubscriptionId::new(3), record)
826                .encode_to_vec()
827                .unwrap(),
828        )
829        .unwrap();
830        let style = LogStyle {
831            color: false,
832            prefix_width: 0,
833            elapsed: false,
834        };
835
836        let mut out = Vec::new();
837        assert!(print_frame(
838            &mut out,
839            &frame,
840            &LogFilter::new(),
841            style,
842            Instant::now(),
843            false
844        ));
845        assert_eq!(String::from_utf8(out).unwrap().trim_end(), "[cam] frame 1");
846
847        // `--json` prints the same record as one object per line.
848        let mut out = Vec::new();
849        assert!(print_frame(
850            &mut out,
851            &frame,
852            &LogFilter::new(),
853            style,
854            Instant::now(),
855            true
856        ));
857        let value: serde_json::Value =
858            serde_json::from_str(String::from_utf8(out).unwrap().trim_end()).unwrap();
859        assert_eq!(value["node"], "cam");
860        assert_eq!(value["message"], "frame 1");
861
862        // A filter that excludes it writes nothing…
863        let mut out = Vec::new();
864        assert!(!print_frame(
865            &mut out,
866            &frame,
867            &LogFilter::new().with_min_level(LogLevel::Error),
868            style,
869            Instant::now(),
870            false
871        ));
872        assert!(out.is_empty());
873    }
874
875    #[test]
876    fn a_frame_that_is_not_a_log_or_does_not_decode_is_skipped_not_fatal() {
877        use astrs_wire::{Frame, FrameFlags, FrameKind};
878
879        let style = LogStyle::default();
880        let other = Frame::new(
881            FrameKind::ControlReply,
882            FrameFlags::EMPTY,
883            b"not a log".to_vec(),
884        )
885        .unwrap();
886        let mut out = Vec::new();
887        assert!(!print_frame(
888            &mut out,
889            &other,
890            &LogFilter::new(),
891            style,
892            Instant::now(),
893            false
894        ));
895
896        let malformed = Frame::new(FrameKind::Log, FrameFlags::EMPTY, vec![0xFF; 4]).unwrap();
897        assert!(!print_frame(
898            &mut out,
899            &malformed,
900            &LogFilter::new(),
901            style,
902            Instant::now(),
903            false
904        ));
905        assert!(out.is_empty(), "a tail survives one bad record");
906    }
907
908    #[test]
909    fn every_read_verb_reports_a_dead_cluster_rather_than_hanging() {
910        let endpoint = endpoint();
911        let list_error = list(&mut Vec::new(), &endpoint, &ListArgs::default()).unwrap_err();
912        assert!(
913            matches!(list_error, CliError::NoCluster { .. }),
914            "{list_error}"
915        );
916
917        let logs_error = logs(&mut Vec::new(), &endpoint, &LogsArgs::default()).unwrap_err();
918        assert!(
919            matches!(logs_error, CliError::NoCluster { .. }),
920            "{logs_error}"
921        );
922
923        let status_error =
924            dataflow_status(&mut Vec::new(), &endpoint, &DataflowStatusArgs::default())
925                .unwrap_err();
926        assert!(
927            matches!(status_error, CliError::NoCluster { .. }),
928            "{status_error}"
929        );
930    }
931
932    #[test]
933    fn a_bad_node_filter_is_refused_before_anything_is_dialled() {
934        let args = LogsArgs {
935            node: Some("not a node".to_owned()),
936            ..LogsArgs::default()
937        };
938        let error = logs(&mut Vec::new(), &endpoint(), &args).unwrap_err();
939        assert!(matches!(error, CliError::BadArgument { .. }), "{error}");
940    }
941}