Skip to main content

tokencompact_builtins/
test_log.rs

1use crate::{
2    Diagnostic, DiagnosticClass, EvidenceQuality, JavaScriptTestDiagnosticParser,
3    JavaTestDiagnosticParser, Provenance, PythonDiagnosticParser, Severity, TestFailure,
4    TestFailureAccumulator, parse_go_diagnostic,
5};
6
7const MAX_FAILURES: usize = 20;
8const MAX_MESSAGE_BYTES: usize = 1_000;
9
10/// Provider-neutral output for one or more test-log segments.
11#[derive(Clone, Debug, Default, Eq, PartialEq)]
12pub struct TestLogReduction {
13    pub failures: Vec<TestFailure>,
14    pub diagnostics: Vec<Diagnostic>,
15}
16
17/// Streaming, bounded orchestration of language and framework test-log parsers.
18#[derive(Default)]
19pub struct TestLogReducer {
20    javascript: JavaScriptTestDiagnosticParser,
21    java: JavaTestDiagnosticParser,
22    python: PythonDiagnosticParser,
23    failures: TestFailureAccumulator,
24    extracted_failures: Vec<TestFailure>,
25    fallback: Option<(u8, Diagnostic)>,
26    segment_provenance: Option<Provenance>,
27}
28
29impl TestLogReducer {
30    pub fn observe_line(&mut self, line: &str, provenance: &Provenance) {
31        self.segment_provenance
32            .get_or_insert_with(|| provenance.clone());
33        self.failures.observe_line(line);
34        let javascript_diagnostic = self.javascript.observe_line(line);
35        let java_diagnostic = self.java.observe_line(line);
36        let candidate = if let Some(diagnostic) = parse_go_diagnostic(line) {
37            Some((0, as_test_diagnostic(diagnostic, provenance)))
38        } else if let Some(diagnostic) = javascript_diagnostic {
39            Some((0, as_test_diagnostic(diagnostic, provenance)))
40        } else if let Some(diagnostic) = java_diagnostic {
41            Some((0, as_test_diagnostic(diagnostic, provenance)))
42        } else if let Some(diagnostic) = self.python.observe_line(line) {
43            Some((0, as_test_diagnostic(diagnostic, provenance)))
44        } else {
45            failure_evidence_priority(line).map(|priority| {
46                (
47                    priority,
48                    Diagnostic {
49                        severity: Severity::Error,
50                        class: DiagnosticClass::Test,
51                        code: Some("test.fallback".to_owned()),
52                        message: bounded_text(line, MAX_MESSAGE_BYTES),
53                        location: None,
54                        provenance: Some(provenance.clone()),
55                        quality: EvidenceQuality::Fallback,
56                        repetition_count: 1,
57                    },
58                )
59            })
60        };
61        if let Some((priority, diagnostic)) = candidate
62            && self
63                .fallback
64                .as_ref()
65                .is_none_or(|(current, current_diagnostic)| {
66                    priority < *current
67                        || (priority == *current
68                            && diagnostic.location.is_some()
69                            && current_diagnostic.location.is_none())
70                })
71        {
72            self.fallback = Some((priority, diagnostic));
73        }
74    }
75
76    /// Completes a log segment. Incomplete segments do not emit structured
77    /// blocks because their terminal confirmation may be missing.
78    pub fn finish_log(&mut self, complete: bool) {
79        if complete {
80            for diagnostic in [self.javascript.finish(), self.java.finish()]
81                .into_iter()
82                .flatten()
83            {
84                let provenance = self
85                    .segment_provenance
86                    .clone()
87                    .unwrap_or_else(|| Provenance::new("test-log"));
88                let diagnostic = as_test_diagnostic(diagnostic, &provenance);
89                if self.fallback.as_ref().is_none_or(|(priority, current)| {
90                    *priority > 0
91                        || (*priority == 0
92                            && diagnostic.location.is_some()
93                            && current.location.is_none())
94                }) {
95                    self.fallback = Some((0, diagnostic));
96                }
97            }
98            let provenance = self
99                .segment_provenance
100                .clone()
101                .unwrap_or_else(|| Provenance::new("test-log"));
102            for failure in std::mem::take(&mut self.failures).finish() {
103                if self.extracted_failures.len() >= MAX_FAILURES {
104                    break;
105                }
106                let framework = framework_from_message(&failure.message).map(str::to_owned);
107                let finding = TestFailure {
108                    name: failure.name,
109                    message: failure.message,
110                    framework,
111                    location: failure.location,
112                    provenance: Some(provenance.clone()),
113                };
114                if !self.extracted_failures.iter().any(|current| {
115                    current.name == finding.name && current.message == finding.message
116                }) {
117                    self.extracted_failures.push(finding);
118                }
119            }
120        } else {
121            self.failures = TestFailureAccumulator::default();
122        }
123        self.javascript = JavaScriptTestDiagnosticParser::default();
124        self.java = JavaTestDiagnosticParser::default();
125        self.python = PythonDiagnosticParser::default();
126        self.segment_provenance = None;
127    }
128
129    #[must_use]
130    pub fn finish(self) -> TestLogReduction {
131        if self.extracted_failures.is_empty() {
132            return TestLogReduction {
133                diagnostics: self
134                    .fallback
135                    .map(|(_, diagnostic)| diagnostic)
136                    .into_iter()
137                    .collect(),
138                ..TestLogReduction::default()
139            };
140        }
141        let diagnostics = self
142            .extracted_failures
143            .iter()
144            .map(|failure| Diagnostic {
145                severity: Severity::Error,
146                class: DiagnosticClass::Test,
147                code: failure
148                    .framework
149                    .as_ref()
150                    .map(|framework| format!("{framework}.failure")),
151                message: failure.message.clone(),
152                location: failure.location.clone(),
153                provenance: failure.provenance.clone(),
154                quality: if failure.location.is_some() {
155                    EvidenceQuality::Located
156                } else {
157                    EvidenceQuality::Structured
158                },
159                repetition_count: 1,
160            })
161            .collect();
162        TestLogReduction {
163            failures: self.extracted_failures,
164            diagnostics,
165        }
166    }
167}
168
169fn as_test_diagnostic(mut diagnostic: Diagnostic, provenance: &Provenance) -> Diagnostic {
170    diagnostic.class = DiagnosticClass::Test;
171    diagnostic.message = bounded_text(&diagnostic.message, MAX_MESSAGE_BYTES);
172    diagnostic.provenance = Some(provenance.clone());
173    diagnostic.quality = if diagnostic.location.is_some() {
174        EvidenceQuality::Located
175    } else {
176        EvidenceQuality::Structured
177    };
178    diagnostic
179}
180
181fn framework_from_message(message: &str) -> Option<&'static str> {
182    if message.starts_with("Rust test ") {
183        Some("rust-libtest")
184    } else if message.starts_with("C++ test ") {
185        Some("gtest")
186    } else if message.starts_with("Go test ") {
187        Some("go-test")
188    } else {
189        None
190    }
191}
192
193fn failure_evidence_priority(line: &str) -> Option<u8> {
194    let line = line.trim();
195    let lower = line.to_ascii_lowercase();
196    let base = lower
197        .split_once(" [repeated ")
198        .map_or(lower.as_str(), |(base, _)| base);
199    if matches!(base, "failure:" | "failures:")
200        || (line.starts_with("test ") && base.ends_with(" ... ok"))
201    {
202        return None;
203    }
204    if lower.contains("root_cause")
205        || lower.contains("panicked at")
206        || (lower.contains("assertion") && lower.contains(" failed"))
207    {
208        Some(0)
209    } else if lower.contains("error:")
210        || lower.starts_with("error ")
211        || lower.contains("fatal:")
212        || lower.contains("undefined reference")
213    {
214        Some(1)
215    } else if lower.contains("failed:")
216        || lower.contains("failure")
217        || lower.starts_with("test result: failed")
218        || (line.starts_with("test ") && line.ends_with(" ... FAILED"))
219    {
220        Some(2)
221    } else {
222        None
223    }
224}
225
226fn bounded_text(value: &str, maximum_bytes: usize) -> String {
227    if value.len() <= maximum_bytes {
228        return value.to_owned();
229    }
230    let mut boundary = maximum_bytes;
231    while !value.is_char_boundary(boundary) {
232        boundary -= 1;
233    }
234    format!("{}…", &value[..boundary])
235}
236
237#[cfg(test)]
238mod tests {
239    use super::*;
240
241    #[test]
242    fn extracts_framework_neutral_test_failure() {
243        let provenance = Provenance::new("stderr");
244        let mut reducer = TestLogReducer::default();
245        for line in [
246            "test invoice::fails ... FAILED",
247            "---- invoice::fails stdout ----",
248            "thread 'invoice::fails' panicked at src/lib.rs:7:3:",
249            "assertion `left == right` failed",
250        ] {
251            reducer.observe_line(line, &provenance);
252        }
253        reducer.finish_log(true);
254        let reduction = reducer.finish();
255        assert_eq!(reduction.failures.len(), 1);
256        assert_eq!(
257            reduction.failures[0].framework.as_deref(),
258            Some("rust-libtest")
259        );
260    }
261
262    #[test]
263    fn incomplete_log_discards_structured_state() {
264        let provenance = Provenance::new("stderr");
265        let mut reducer = TestLogReducer::default();
266        reducer.observe_line("=== RUN   TestInvoice", &provenance);
267        reducer.observe_line("invoice_test.go:7: got 2; want 3", &provenance);
268        reducer.finish_log(false);
269        assert!(reducer.finish().failures.is_empty());
270    }
271
272    #[test]
273    fn extracts_node_exception_with_application_frame() {
274        let provenance = Provenance::new("stderr");
275        let mut reducer = TestLogReducer::default();
276        for line in [
277            "/tmp/work/cases/runtime_failure.js:2",
278            "TypeError: Cannot read properties of undefined",
279            "    at invoiceTotal (/tmp/work/cases/runtime_failure.js:2:18)",
280            "    at Module._compile (node:internal/modules/cjs/loader:1:1)",
281        ] {
282            reducer.observe_line(line, &provenance);
283        }
284        reducer.finish_log(true);
285        let reduction = reducer.finish();
286        assert!(reduction.failures.is_empty());
287        assert_eq!(reduction.diagnostics.len(), 1);
288        assert_eq!(
289            reduction.diagnostics[0].location.as_ref().unwrap().path,
290            "/tmp/work/cases/runtime_failure.js"
291        );
292    }
293}