Skip to main content

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