goose 0.19.1

A load testing framework inspired by Locust.
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//! PDF report generation functionality
//!
//! This module provides PDF report generation by converting existing HTML reports
//! to PDF format using headless Chrome. It leverages the HTML report generation
//! from the print module and converts it to PDF with configurable options.
//!
//! **Note**: This entire module is only available when compiled with the `pdf-reports` feature flag.

use crate::logger::ScopedLogLevel;
use crate::report::chrome::ResolvedChrome;
use crate::GooseError;
use headless_chrome::{protocol::cdp::Page, Browser, LaunchOptions};
use log::{debug, LevelFilter};
use std::{
    ffi::OsStr,
    time::{Duration, Instant},
};

/// Centralized error message module for consistent PDF error handling
mod pdf_errors {
    use crate::report::chrome::ResolvedChrome;
    use crate::GooseError;
    use std::time::Duration;

    /// Creates a consistent error for Chrome launch failures
    pub fn chrome_launch_error(inner_error: String, chrome: &ResolvedChrome) -> GooseError {
        GooseError::InvalidOption {
            option: "--report-file".to_string(),
            value: format!("Chrome launch failed: {inner_error}"),
            detail: super::launch_diagnosis::diagnose(&inner_error, chrome),
        }
    }

    /// Creates a consistent error for Chrome configuration failures
    pub fn chrome_config_error(inner_error: String) -> GooseError {
        GooseError::InvalidOption {
            option: "--report-file".to_string(),
            value: format!("Chrome configuration failed: {inner_error}"),
            detail: "Goose could not build the options to launch Chrome with. This is a bug in Goose; please report it at https://github.com/tag1consulting/goose/issues with this error.".to_string(),
        }
    }

    /// Creates a consistent error for Chrome operation failures
    pub fn chrome_operation_error(operation: &str, inner_error: String) -> GooseError {
        GooseError::InvalidOption {
            option: "--report-file".to_string(),
            value: format!("{operation} failed: {inner_error}"),
            detail: format!("Chrome {operation} operation failed. This may indicate Chrome instability or insufficient system resources."),
        }
    }

    /// Creates a consistent error for timeout scenarios
    pub fn timeout_error(duration: Duration) -> GooseError {
        GooseError::InvalidOption {
            option: "--pdf-timeout".to_string(),
            value: "timeout".to_string(),
            detail: format!("Chrome operation timed out after {duration:?}. This may indicate Chrome is unresponsive or the system is under heavy load."),
        }
    }

    /// Creates a consistent error for content measurement failures
    pub fn content_measurement_error(inner_error: String) -> GooseError {
        GooseError::InvalidOption {
            option: "--report-file".to_string(),
            value: format!("Failed to measure content height: {inner_error}"),
            detail: "Unable to calculate content dimensions for PDF".to_string(),
        }
    }

    /// Creates a consistent error for PDF generation failures with scale information
    pub fn pdf_generation_error(
        inner_error: String,
        scale: f64,
        width: f64,
        height: f64,
    ) -> GooseError {
        GooseError::InvalidOption {
            option: "--report-file".to_string(),
            value: format!("PDF generation failed: {inner_error}"),
            detail: format!("Chrome's print-to-PDF operation failed. Scale: {scale}, Paper size: {width:.1}\" x {height:.1}\". Try adjusting --pdf-scale or reducing report complexity."),
        }
    }
}

/// Manages Chrome browser process lifecycle with automatic cleanup.
///
/// This struct provides RAII-based resource management for Chrome processes,
/// ensuring proper cleanup even in error scenarios.
struct ChromeSession {
    browser: Browser,
    process_id: Option<u32>,
    start_time: Instant,
}

impl ChromeSession {
    /// Creates a new Chrome session with proper resource tracking.
    ///
    /// The launch is bounded by `timeout`. `headless_chrome` waits 30 seconds
    /// for Chrome to report its DevTools address, and on a port it picked
    /// itself retries ten more times, so a Chrome that exits at once (a
    /// missing shared library) would otherwise take five minutes to fail.
    /// Goose picks the port, so a failure is not retried, except when the
    /// port turned out to be taken.
    ///
    /// # Returns
    /// * `Result<ChromeSession, GooseError>` - The Chrome session or an error
    fn new(
        chrome: &ResolvedChrome,
        args: Vec<&'static OsStr>,
        timeout: Duration,
    ) -> Result<Self, GooseError> {
        let start_time = Instant::now();

        debug!(
            "Launching {} ({}) for PDF generation",
            chrome.path.display(),
            chrome.source.describe()
        );
        let mut attempts = 0;
        let browser = loop {
            attempts += 1;
            let launch_options = LaunchOptions::default_builder()
                .path(Some(chrome.path.clone()))
                .headless(true)
                .args(args.clone())
                .port(free_port())
                .build()
                .map_err(|e| pdf_errors::chrome_config_error(e.to_string()))?;
            let (tx, rx) = std::sync::mpsc::channel();
            std::thread::spawn(move || {
                let _ = tx.send(Browser::new(launch_options));
            });
            let remaining = timeout.saturating_sub(start_time.elapsed());
            match rx.recv_timeout(remaining) {
                Ok(Ok(browser)) => break browser,
                Ok(Err(e)) if attempts < 3 && e.to_string().contains("already in use") => continue,
                Ok(Err(e)) => return Err(pdf_errors::chrome_launch_error(e.to_string(), chrome)),
                // The launch thread ends on its own, and kills Chrome if it started.
                Err(_) => {
                    return Err(pdf_errors::chrome_launch_error(
                        format!(
                            "Chrome did not report its DevTools address within {} seconds",
                            timeout.as_secs()
                        ),
                        chrome,
                    ))
                }
            }
        };

        // Attempt to get the process ID for tracking
        let process_id = browser.get_process_id();

        if let Some(pid) = process_id {
            debug!("Chrome browser launched with process ID: {pid}");
        } else {
            debug!("Chrome browser launched (process ID not available)");
        }

        Ok(ChromeSession {
            browser,
            process_id,
            start_time,
        })
    }

    /// Gets a reference to the browser instance.
    fn browser(&self) -> &Browser {
        &self.browser
    }

    /// Checks if the Chrome operation has exceeded the timeout.
    fn check_timeout(&self, timeout: Duration) -> Result<(), GooseError> {
        if self.start_time.elapsed() > timeout {
            return Err(pdf_errors::timeout_error(timeout));
        }
        Ok(())
    }
}

impl Drop for ChromeSession {
    fn drop(&mut self) {
        let elapsed = self.start_time.elapsed();

        if let Some(pid) = self.process_id {
            debug!("Cleaning up Chrome process {pid} (ran for {elapsed:?})");
        } else {
            debug!("Cleaning up Chrome browser session (ran for {elapsed:?})");
        }

        // The Browser's Drop trait handles the actual process termination
        // when self.browser is dropped
    }
}

/// A free local port for Chrome's DevTools, or `None` to let
/// `headless_chrome` pick one.
fn free_port() -> Option<u16> {
    std::net::TcpListener::bind(("127.0.0.1", 0))
        .and_then(|listener| listener.local_addr())
        .map(|address| address.port())
        .ok()
}

/// Get Chrome launch arguments based on verbosity level
fn get_chrome_launch_args(verbose: bool) -> Vec<&'static OsStr> {
    let mut args = vec![
        OsStr::new("--no-sandbox"),
        OsStr::new("--disable-dev-shm-usage"),
        OsStr::new("--headless"),
    ];

    if !verbose {
        // Normal operation: Maximum logging suppression
        args.extend([
            // Core logging suppression
            OsStr::new("--disable-logging"),
            OsStr::new("--log-level=3"), // Only fatal errors
            OsStr::new("--silent"),
            // Component-specific suppression
            OsStr::new("--disable-dev-tools-console"),
            OsStr::new("--disable-extensions"),
            OsStr::new("--disable-plugins"),
            OsStr::new("--disable-default-apps"),
            OsStr::new("--disable-sync"),
            // Background process suppression
            OsStr::new("--disable-background-timer-throttling"),
            OsStr::new("--disable-renderer-backgrounding"),
            OsStr::new("--disable-backgrounding-occluded-windows"),
        ]);
    } else {
        // Verbose mode: Enable Chrome logging for debugging
        args.extend([
            OsStr::new("--enable-logging"),
            OsStr::new("--log-level=0"), // All logs
            OsStr::new("--v=1"),         // Chrome verbose logging
        ]);
    }

    args
}

/// Generate a PDF report from HTML content using headless Chrome
///
/// This function uses Chrome's native argument system to control logging behavior
/// based on the verbose parameter. In normal operation, Chrome logging is suppressed
/// for clean output. In verbose mode, Chrome debugging information is displayed.
///
/// The function implements RAII-based resource management to ensure Chrome processes
/// are properly cleaned up even in error scenarios, with timeout protection to prevent
/// hanging operations.
pub(crate) fn generate_pdf_from_html(
    html_content: &str,
    scale: f64,
    verbose: bool,
    timeout_seconds: u64,
    chrome: &ResolvedChrome,
) -> Result<Vec<u8>, GooseError> {
    render_pdf(html_content, scale, verbose, timeout_seconds, chrome).map(|rendered| rendered.pdf)
}

/// Confirms `chrome` can render a PDF, the way [`generate_pdf_from_html`]
/// does, and returns Chrome's version when it reports one.
pub(crate) fn preflight_pdf(
    scale: f64,
    verbose: bool,
    timeout_seconds: u64,
    chrome: &ResolvedChrome,
) -> Result<Option<String>, GooseError> {
    render_pdf(
        "<!DOCTYPE html><html><body><p>Goose PDF report check</p></body></html>",
        scale,
        verbose,
        timeout_seconds,
        chrome,
    )
    .map(|rendered| rendered.version)
}

/// A PDF rendered in memory.
struct Rendered {
    pdf: Vec<u8>,
    /// Chrome's version, such as `141.0.7390.122`.
    version: Option<String>,
}

/// Launches Chrome and prints `html_content` to a PDF in memory.
fn render_pdf(
    html_content: &str,
    scale: f64,
    verbose: bool,
    timeout_seconds: u64,
    chrome: &ResolvedChrome,
) -> Result<Rendered, GooseError> {
    // Get appropriate Chrome arguments based on verbosity
    let chrome_args = get_chrome_launch_args(verbose);

    // Create a thread-safe scoped logging guard that temporarily adjusts log level for headless Chrome operations
    // This suppresses Rust logging output during PDF generation while maintaining thread safety
    let _log_guard = if !verbose {
        Some(ScopedLogLevel::new(LevelFilter::Error)?)
    } else {
        None
    };

    // Set timeout for Chrome operations (configurable)
    let timeout = Duration::from_secs(timeout_seconds);

    // Launch Chrome with appropriate arguments and automatic resource management
    let chrome_session = ChromeSession::new(chrome, chrome_args, timeout)?;

    // The product is reported as `HeadlessChrome/141.0.7390.122`.
    let version = chrome_session.browser().get_version().ok().map(|version| {
        match version.product.split_once('/') {
            Some((_, number)) => number.to_string(),
            None => version.product,
        }
    });

    // Create a new tab
    chrome_session.check_timeout(timeout)?;
    let tab = chrome_session
        .browser()
        .new_tab()
        .map_err(|e| pdf_errors::chrome_operation_error("tab creation", e.to_string()))?;

    // Give Chrome the HTML as a blank page's content. A data URL, which
    // Chrome refuses past about 2 MB, would fail for a report with many
    // request names, and a file would give the page access to local files.
    chrome_session.check_timeout(timeout)?;
    tab.navigate_to("about:blank")
        .and_then(|tab| tab.wait_until_navigated())
        .and_then(|tab| tab.call_method(Page::GetFrameTree(None)))
        .and_then(|tree| {
            tab.call_method(Page::SetDocumentContent {
                frame_id: tree.frame_tree.frame.id,
                html: html_content.to_string(),
            })
        })
        .map_err(|e| pdf_errors::chrome_operation_error("HTML loading", e.to_string()))?;

    // Wait for the page, and the chart library it loads, to finish loading.
    loop {
        chrome_session.check_timeout(timeout)?;
        let state = tab
            .evaluate("document.readyState", false)
            .map_err(|e| pdf_errors::chrome_operation_error("page loading", e.to_string()))?;
        if state.value.as_ref().and_then(|value| value.as_str()) == Some("complete") {
            break;
        }
        std::thread::sleep(Duration::from_millis(50));
    }

    // Optimized content height calculation using document.documentElement.scrollHeight
    // This is significantly more performant than iterating through all DOM elements
    // while still accurately capturing the full content height including overflow content
    let content_height_script = r#"
        (function() {
            // Primary method: Use scrollHeight which efficiently captures full content height
            // This includes content that overflows the viewport and handles most edge cases
            const scrollHeight = document.documentElement.scrollHeight / 96; // Convert to inches
            
            // Fallback: Check document body height in case scrollHeight is unreliable
            // Some edge cases with CSS positioning might make scrollHeight insufficient
            const bodyHeight = Math.max(
                document.body.scrollHeight,
                document.body.offsetHeight
            ) / 96; // Convert to inches
            
            // Use the maximum of both methods with a small buffer for safety
            const calculatedHeight = Math.max(scrollHeight, bodyHeight);
            const bufferedHeight = calculatedHeight + 0.05; // ~5px buffer in inches
            
            return Math.max(bufferedHeight, 1.0); // Minimum 1 inch for degenerate cases
        })();
    "#;

    chrome_session.check_timeout(timeout)?;
    let content_height_inches = tab
        .evaluate(content_height_script, true)
        .map_err(|e| pdf_errors::content_measurement_error(e.to_string()))?
        .value
        .unwrap_or_default()
        .as_f64()
        .unwrap_or(11.0);

    // Calculate adjusted page dimensions based on scale factor
    // When scaling up content, we need to increase page size proportionally
    // to ensure the scaled content fits within the page boundaries
    let base_width = 8.5;
    let adjusted_width = base_width * scale;
    let adjusted_height = content_height_inches * scale;

    chrome_session.check_timeout(timeout)?;
    let pdf_data = tab
        .print_to_pdf(Some(headless_chrome::types::PrintToPdfOptions {
            landscape: Some(false),
            display_header_footer: Some(false),
            print_background: Some(true),
            scale: Some(scale), // Use the passed scale parameter
            paper_width: Some(adjusted_width),
            paper_height: Some(adjusted_height),
            margin_top: Some(0.1),    // Hardcoded sensible default
            margin_bottom: Some(0.1), // Hardcoded sensible default
            margin_left: Some(0.1),   // Hardcoded sensible default
            margin_right: Some(0.1),  // Hardcoded sensible default
            page_ranges: None,
            ignore_invalid_page_ranges: Some(false),
            header_template: None,
            footer_template: None,
            prefer_css_page_size: Some(false),
            transfer_mode: None,
            generate_document_outline: Some(false),
            generate_tagged_pdf: Some(false),
        }))
        .map_err(|e| {
            pdf_errors::pdf_generation_error(e.to_string(), scale, adjusted_width, adjusted_height)
        })?;

    chrome_session.check_timeout(timeout)?;

    // The ScopedLogLevel guard (_log_guard) automatically restores the original
    // log level when it goes out of scope here, ensuring no permanent global state changes.
    // Thread safety is guaranteed by the mutex synchronization in ScopedLogLevel.
    // The ChromeSession automatically cleans up Chrome processes via Drop trait.
    Ok(Rendered {
        pdf: pdf_data,
        version,
    })
}

/// Explains why Chrome did not start. Each step that can't run is skipped,
/// and no step can hang: every command it runs has a short timeout.
mod launch_diagnosis {
    use crate::report::chrome::ResolvedChrome;
    use std::io::Read;
    use std::path::Path;
    use std::process::{Command, Stdio};
    use std::time::{Duration, Instant};

    const HOW_TO: &str = "Install Chrome or Chromium, or set the CHROME environment variable to the full path of a working Chrome or Chromium binary.";

    /// How the launch failed, from the error text.
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    pub(super) enum Failure {
        /// The binary is for another architecture (ENOEXEC).
        ExecFormat,
        /// ENOENT: the binary, or on Linux its ELF interpreter, is missing.
        NotFound,
        /// EACCES: the binary is not executable.
        PermissionDenied,
        Other,
    }

    pub(super) fn classify(error: &str) -> Failure {
        // `Command::spawn` errors end with the OS error number.
        if error.contains("Exec format error") || error.contains("os error 8)") {
            Failure::ExecFormat
        } else if error.contains("No such file or directory") || error.contains("os error 2)") {
            Failure::NotFound
        } else if error.contains("Permission denied") || error.contains("os error 13)") {
            Failure::PermissionDenied
        } else {
            Failure::Other
        }
    }

    /// The libraries `ldd` reports missing. glibc's `ldd` prints
    /// `libnss3.so => not found`; musl's prints
    /// `Error loading shared library libnss3.so: No such file or directory`.
    pub(super) fn missing_libraries(ldd_output: &str) -> Vec<String> {
        let mut missing: Vec<String> = Vec::new();
        for line in ldd_output.lines().map(str::trim) {
            let library = if let Some((library, rest)) = line.split_once("=>") {
                (rest.trim() == "not found").then(|| library.trim())
            } else if let Some(rest) = line.strip_prefix("Error loading shared library ") {
                rest.split(':').next().map(str::trim)
            } else {
                None
            };
            if let Some(library) = library.filter(|library| !library.is_empty()) {
                if !missing.iter().any(|seen| seen == library) {
                    missing.push(library.to_string());
                }
            }
        }
        missing
    }

    /// What Goose learned about the failed launch.
    pub(super) struct Findings<'a> {
        pub(super) failure: Failure,
        pub(super) exists: bool,
        pub(super) linux: bool,
        pub(super) missing: &'a [String],
        pub(super) version_stderr: Option<&'a str>,
    }

    /// Writes the explanation from what was learned.
    pub(super) fn explain(path: &Path, source: &str, findings: &Findings) -> String {
        let mut text = format!(
            "Goose could not start the Chrome binary {} ({source}).",
            path.display()
        );
        match findings.failure {
            Failure::ExecFormat => text.push_str(&format!(
                " It does not match this machine's architecture ({}).",
                std::env::consts::ARCH
            )),
            Failure::NotFound if !findings.exists => text.push_str(" The file does not exist."),
            Failure::NotFound if findings.linux => text.push_str(
                " The file exists, so the ELF interpreter it was linked against is missing: usually a glibc binary on a musl system such as Alpine, or the reverse.",
            ),
            Failure::PermissionDenied => {
                text.push_str(" The file is not executable by this user.")
            }
            _ => {}
        }
        if !findings.missing.is_empty() {
            text.push_str(&format!(
                " It needs shared libraries that are not installed: {}. Installing your distribution's Chromium package pulls them in.",
                findings.missing.join(", ")
            ));
        }
        if let Some(stderr) = findings.version_stderr.filter(|s| !s.trim().is_empty()) {
            text.push_str(&format!(
                " Running it with --version printed: {}",
                stderr.trim()
            ));
        }
        text.push(' ');
        text.push_str(HOW_TO);
        text
    }

    /// Diagnoses a failed launch of `chrome`.
    pub(super) fn diagnose(error: &str, chrome: &ResolvedChrome) -> String {
        let failure = classify(error);
        let exists = chrome.path.is_file();
        let linux = cfg!(target_os = "linux");
        // Nothing more to learn from a binary that is missing or for another architecture.
        let probe = exists && failure != Failure::ExecFormat;

        let mut missing = Vec::new();
        if probe && linux {
            if let Some((stdout, stderr)) = run(
                Command::new("ldd").arg(&chrome.path),
                Duration::from_secs(5),
            ) {
                missing = missing_libraries(&format!("{stdout}\n{stderr}"));
            }
        }
        let interpreter_missing = linux && exists && failure == Failure::NotFound;
        let version_stderr = if probe && missing.is_empty() && !interpreter_missing {
            run(
                Command::new(&chrome.path).arg("--version"),
                Duration::from_secs(5),
            )
            .map(|(_, stderr)| stderr.lines().take(5).collect::<Vec<_>>().join(" / "))
        } else {
            None
        };

        explain(
            &chrome.path,
            chrome.source.describe(),
            &Findings {
                failure,
                exists,
                linux,
                missing: &missing,
                version_stderr: version_stderr.as_deref(),
            },
        )
    }

    /// Runs a command and returns its output, or `None` if it can't start or
    /// doesn't finish within `timeout`.
    fn run(command: &mut Command, timeout: Duration) -> Option<(String, String)> {
        let mut child = command
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .ok()?;
        let started = Instant::now();
        loop {
            match child.try_wait() {
                Ok(Some(_)) => break,
                Ok(None) if started.elapsed() < timeout => {
                    std::thread::sleep(Duration::from_millis(20))
                }
                _ => {
                    let _ = child.kill();
                    let _ = child.wait();
                    return None;
                }
            }
        }
        let mut stdout = String::new();
        let mut stderr = String::new();
        if let Some(mut out) = child.stdout.take() {
            let _ = out.read_to_string(&mut stdout);
        }
        if let Some(mut err) = child.stderr.take() {
            let _ = err.read_to_string(&mut stderr);
        }
        Some((stdout, stderr))
    }
}

#[cfg(test)]
mod tests {
    use super::launch_diagnosis::{classify, explain, missing_libraries, Failure, Findings};
    use std::path::Path;

    fn findings(failure: Failure) -> Findings<'static> {
        Findings {
            failure,
            exists: true,
            linux: false,
            missing: &[],
            version_stderr: None,
        }
    }

    #[test]
    fn classifies_launch_errors() {
        assert_eq!(
            classify("Exec format error (os error 8)"),
            Failure::ExecFormat
        );
        assert_eq!(
            classify("No such file or directory (os error 2)"),
            Failure::NotFound
        );
        assert_eq!(
            classify("Permission denied (os error 13)"),
            Failure::PermissionDenied
        );
        assert_eq!(
            classify("Could not auto detect a chrome executable"),
            Failure::Other
        );
        // os error 28 is not os error 2.
        assert_eq!(
            classify("No space left on device (os error 28)"),
            Failure::Other
        );
    }

    #[test]
    fn launch_error_names_chrome_variable_and_binary() {
        let detail = explain(
            Path::new("/usr/bin/chromium"),
            "installed",
            &findings(Failure::Other),
        );
        assert!(detail.contains("CHROME"), "{}", detail);
        assert!(
            detail.contains("/usr/bin/chromium (installed)"),
            "{}",
            detail
        );
        assert!(!detail.to_lowercase().contains("download"), "{}", detail);
        assert!(!detail.contains("architecture"), "{}", detail);
    }

    #[test]
    fn launch_error_reports_architecture_mismatch() {
        let detail = explain(
            Path::new("/usr/bin/chromium"),
            "installed",
            &findings(Failure::ExecFormat),
        );
        assert!(detail.contains("architecture"), "{}", detail);
        assert!(detail.contains(std::env::consts::ARCH), "{}", detail);
        assert!(detail.contains("CHROME"), "{}", detail);
        assert!(!detail.to_lowercase().contains("download"), "{}", detail);
    }

    #[test]
    fn launch_error_reports_missing_interpreter_on_linux() {
        let mut found = findings(Failure::NotFound);
        found.linux = true;
        let detail = explain(Path::new("/opt/chrome"), "set by CHROME", &found);
        assert!(detail.contains("ELF interpreter"), "{}", detail);
        assert!(detail.contains("musl"), "{}", detail);

        // A missing file is just missing.
        found.exists = false;
        let detail = explain(Path::new("/opt/chrome"), "set by CHROME", &found);
        assert!(detail.contains("does not exist"), "{}", detail);
        assert!(!detail.contains("ELF"), "{}", detail);

        // Off Linux there is no ELF interpreter to blame.
        found.exists = true;
        found.linux = false;
        let detail = explain(Path::new("/opt/chrome"), "set by CHROME", &found);
        assert!(!detail.contains("ELF"), "{}", detail);
    }

    #[test]
    fn launch_error_lists_missing_libraries_and_stderr() {
        let missing = vec!["libnss3.so".to_string(), "libatk-1.0.so.0".to_string()];
        let mut found = findings(Failure::Other);
        found.linux = true;
        found.missing = &missing;
        let detail = explain(Path::new("/opt/chrome"), "installed", &found);
        assert!(detail.contains("libnss3.so, libatk-1.0.so.0"), "{}", detail);
        assert!(detail.contains("Chromium package"), "{}", detail);

        let mut found = findings(Failure::Other);
        found.version_stderr = Some("  cannot open display  ");
        let detail = explain(Path::new("/opt/chrome"), "installed", &found);
        assert!(
            detail.contains("--version printed: cannot open display"),
            "{}",
            detail
        );

        // Empty stderr adds nothing.
        found.version_stderr = Some("  ");
        let detail = explain(Path::new("/opt/chrome"), "installed", &found);
        assert!(!detail.contains("--version"), "{}", detail);
    }

    #[test]
    fn parses_missing_libraries_from_ldd() {
        let glibc = "\
\tlinux-vdso.so.1 (0x0000ffff9a5e0000)
\tlibdl.so.2 => /lib/aarch64-linux-gnu/libdl.so.2 (0x0000ffff9a590000)
\tlibnss3.so => not found
\tlibnssutil3.so => not found
\tlibnss3.so => not found
\tlibc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000ffff9a3e0000)
";
        assert_eq!(
            missing_libraries(glibc),
            vec!["libnss3.so", "libnssutil3.so"]
        );

        let musl = "\
\t/lib/ld-musl-x86_64.so.1 (0x7f0e1c4a6000)
Error loading shared library libglib-2.0.so.0: No such file or directory (needed by /opt/chrome)
Error loading shared library libnss3.so: No such file or directory (needed by /opt/chrome)
";
        assert_eq!(
            missing_libraries(musl),
            vec!["libglib-2.0.so.0", "libnss3.so"]
        );

        assert!(missing_libraries("\tnot a dynamic executable\n").is_empty());
        assert!(missing_libraries("").is_empty());
    }
}