Skip to main content

pixelcoords_core/
report.rs

1//! The document every scoring command prints.
2//!
3//! `assert`, `find`, `resolve`, `wait`, and `diff` answer one class of
4//! question — what is true on screen right now — and a caller that reads
5//! two of them should not have to write two parsers. One envelope, one
6//! schema counter, one place the aggregate answer lives.
7//!
8//! The aggregate is `ok`, and it is the only thing that moves up here.
9//! Row-level answers stay on the rows: `assert --stdin` returns one
10//! verdict per input line, and a caller scoring a trajectory needs to
11//! know *which* click missed, not merely that one did.
12//!
13//! `doctor` and `windows` keep their own documents. They report on the
14//! machine rather than on a session, and forcing them into `results[]`
15//! would buy nothing.
16
17use serde::Serialize;
18
19/// The schema version of every document in this module.
20///
21/// Starts at 2 rather than 1: it replaces two independent counters that
22/// were both sitting at 1, and a consumer that pinned either of them
23/// would otherwise see a version it recognizes on a shape it does not.
24pub const CLI_SCHEMA_VERSION: u32 = 2;
25
26/// Which command produced a document, so a consumer reading a stored
27/// report does not have to infer it from which fields are present.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
29#[serde(rename_all = "lowercase")]
30pub enum Command {
31    Assert,
32    Find,
33    Resolve,
34    Wait,
35    Diff,
36}
37
38/// A command's answer: the rows it produced, and whether they pass.
39///
40/// Generic over the row type rather than an enum of every command's
41/// shape, so adding a command adds a row type instead of editing a type
42/// every existing consumer matches on.
43#[derive(Debug, Clone, PartialEq, Serialize)]
44pub struct Report<T> {
45    pub schema: u32,
46    pub command: Command,
47    /// When the frames behind this answer were taken. Absent for the
48    /// commands that do not capture — `assert`, and `resolve` without
49    /// `--relocate`. Supplied by the caller; this crate has no clock.
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub captured_utc: Option<String>,
52    /// How many times the screen was polled to reach this answer, and how
53    /// long that took. Present only for commands that loop — `wait` — and
54    /// provenance in the same sense `captured_utc` is: they describe how
55    /// the answer was obtained, never what it is.
56    ///
57    /// `elapsed_ms` is measured rather than derived. `wait` decides when
58    /// to stop by counting polls, not by watching a clock, so the two do
59    /// not imply each other: capture time is real and is not budgeted.
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub polls: Option<u32>,
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub elapsed_ms: Option<u64>,
64    /// The aggregate the exit code mirrors: 0 when true, 1 when false.
65    ///
66    /// Each command computes this from its own rows, in its own module.
67    /// What makes a command report failure is the most important line it
68    /// has, and it belongs where a reader will look for it rather than in
69    /// a trait implementation five files away.
70    pub ok: bool,
71    pub results: Vec<T>,
72}
73
74impl<T> Report<T> {
75    /// A document from a command that captured.
76    pub fn captured(command: Command, captured_utc: String, ok: bool, results: Vec<T>) -> Self {
77        Self {
78            schema: CLI_SCHEMA_VERSION,
79            command,
80            captured_utc: Some(captured_utc),
81            polls: None,
82            elapsed_ms: None,
83            ok,
84            results,
85        }
86    }
87
88    /// A document from a command that answered from the session alone.
89    pub fn offline(command: Command, ok: bool, results: Vec<T>) -> Self {
90        Self {
91            schema: CLI_SCHEMA_VERSION,
92            command,
93            captured_utc: None,
94            polls: None,
95            elapsed_ms: None,
96            ok,
97            results,
98        }
99    }
100
101    /// Record how much polling produced this answer. Only `wait` loops,
102    /// so only `wait` calls this.
103    #[must_use]
104    pub fn polled(mut self, polls: u32, elapsed_ms: u64) -> Self {
105        self.polls = Some(polls);
106        self.elapsed_ms = Some(elapsed_ms);
107        self
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[derive(Debug, Clone, PartialEq, Serialize)]
116    struct Row {
117        hit: bool,
118    }
119
120    #[test]
121    fn a_captured_document_carries_its_timestamp() {
122        let report = Report::captured(
123            Command::Find,
124            "2026-07-31T00:00:00Z".into(),
125            true,
126            vec![Row { hit: true }],
127        );
128        let json = serde_json::to_value(&report).unwrap();
129        assert_eq!(json["schema"], 2);
130        assert_eq!(json["command"], "find");
131        assert_eq!(json["captured_utc"], "2026-07-31T00:00:00Z");
132        assert_eq!(json["ok"], true);
133        assert_eq!(json["results"][0]["hit"], true);
134    }
135
136    #[test]
137    fn an_offline_document_omits_the_timestamp_rather_than_nulling_it() {
138        let report = Report::offline(Command::Assert, false, vec![Row { hit: false }]);
139        let json = serde_json::to_value(&report).unwrap();
140        assert!(
141            json.get("captured_utc").is_none(),
142            "a command that did not capture has no capture time to report, \
143             and null would invite a consumer to parse one"
144        );
145        assert_eq!(json["command"], "assert");
146        assert_eq!(json["ok"], false);
147    }
148
149    #[test]
150    fn row_answers_survive_the_aggregate() {
151        // The whole reason `hit` does not move up to `ok`: a caller
152        // scoring a trajectory needs to know which click missed.
153        let report = Report::offline(
154            Command::Assert,
155            false,
156            vec![Row { hit: true }, Row { hit: false }],
157        );
158        let json = serde_json::to_value(&report).unwrap();
159        assert_eq!(json["ok"], false);
160        assert_eq!(json["results"][0]["hit"], true);
161        assert_eq!(json["results"][1]["hit"], false);
162    }
163
164    #[test]
165    fn every_command_serializes_lowercase() {
166        for (command, name) in [
167            (Command::Assert, "assert"),
168            (Command::Find, "find"),
169            (Command::Resolve, "resolve"),
170            (Command::Wait, "wait"),
171            (Command::Diff, "diff"),
172        ] {
173            assert_eq!(serde_json::to_value(command).unwrap(), name);
174        }
175    }
176
177    #[test]
178    fn only_a_polling_command_reports_polls() {
179        // The two fields are provenance for the one command that loops.
180        // Every other document must not grow them, or a consumer would
181        // start expecting a poll count from `assert`.
182        let still = Report::offline(Command::Assert, true, vec![Row { hit: true }]);
183        let json = serde_json::to_value(&still).unwrap();
184        assert!(json.get("polls").is_none() && json.get("elapsed_ms").is_none());
185
186        let polled = Report::captured(Command::Wait, "t".into(), true, vec![Row { hit: true }])
187            .polled(61, 30_412);
188        let json = serde_json::to_value(&polled).unwrap();
189        assert_eq!(json["polls"], 61);
190        assert_eq!(
191            json["elapsed_ms"], 30_412,
192            "measured, not derived from the budget — capture time is real"
193        );
194    }
195
196    #[test]
197    fn an_empty_result_set_still_serializes_as_an_array() {
198        let report: Report<Row> = Report::offline(Command::Diff, true, Vec::new());
199        let json = serde_json::to_value(&report).unwrap();
200        assert!(
201            json["results"].is_array(),
202            "a consumer indexing results[] must not have to handle null"
203        );
204    }
205}