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
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::{
    io::{BufRead, BufReader, Write},
    num::ParseIntError,
    path::Path,
    time::Duration,
};

use anyhow::{bail, Context as _, Result};
use async_graphql::http::GraphiQLSource;
use axum::response::{self, IntoResponse};
use http::Uri;
#[cfg(test)]
use linera_base::command::parse_version_message;
use linera_base::data_types::TimeDelta;
pub use linera_client::util::*;
use tracing::debug;

/// Extension trait for [`tokio::process::Child`].
pub trait ChildExt: std::fmt::Debug {
    fn ensure_is_running(&mut self) -> Result<()>;
}

impl ChildExt for tokio::process::Child {
    fn ensure_is_running(&mut self) -> Result<()> {
        if let Some(status) = self.try_wait().context("try_wait child process")? {
            bail!(
                "Child process {:?} already exited with status: {}",
                self,
                status
            );
        }
        debug!("Child process {:?} is running as expected.", self);
        Ok(())
    }
}

pub fn read_json<T: serde::de::DeserializeOwned>(path: impl Into<std::path::PathBuf>) -> Result<T> {
    Ok(serde_json::from_reader(fs_err::File::open(path)?)?)
}

#[cfg(with_testing)]
#[macro_export]
macro_rules! test_name {
    () => {
        stdext::function_name!()
            .strip_suffix("::{{closure}}")
            .expect("should be called from the body of a test")
    };
}

pub struct Markdown<B> {
    buffer: B,
}

impl Markdown<BufReader<fs_err::File>> {
    pub fn new(path: impl AsRef<Path>) -> std::io::Result<Self> {
        let buffer = BufReader::new(fs_err::File::open(path.as_ref())?);
        Ok(Self { buffer })
    }
}

impl<B> Markdown<B>
where
    B: BufRead,
{
    #[expect(clippy::while_let_on_iterator)]
    pub fn extract_bash_script_to(
        self,
        mut output: impl Write,
        pause_after_linera_service: Option<Duration>,
        pause_after_gql_mutations: Option<Duration>,
    ) -> std::io::Result<()> {
        let mut lines = self.buffer.lines();

        while let Some(line) = lines.next() {
            let line = line?;

            if line.starts_with("```bash") {
                if line.ends_with("ignore") {
                    continue;
                } else {
                    let mut quote = String::new();
                    while let Some(line) = lines.next() {
                        let line = line?;
                        if line.starts_with("```") {
                            break;
                        }
                        quote += &line;
                        quote += "\n";

                        if let Some(pause) = pause_after_linera_service {
                            if line.contains("linera service") {
                                quote += &format!("sleep {}\n", pause.as_secs());
                            }
                        }
                    }
                    writeln!(output, "{}", quote)?;
                }
            } else if let Some(uri) = line.strip_prefix("```gql,uri=") {
                let mut quote = String::new();
                while let Some(line) = lines.next() {
                    let line = line?;
                    if line.starts_with("```") {
                        break;
                    }
                    quote += &line;
                    quote += "\n";
                }

                writeln!(output, "QUERY=\"{}\"", quote.replace('"', "\\\""))?;
                writeln!(
                    output,
                    "JSON_QUERY=$( jq -n --arg q \"$QUERY\" '{{\"query\": $q}}' )"
                )?;
                writeln!(
                    output,
                    "QUERY_RESULT=$( \
                     curl -w '\\n' -g -X POST \
                       -H \"Content-Type: application/json\" \
                       -d \"$JSON_QUERY\" {uri} \
                     | tee /dev/stderr \
                     | jq -e .data \
                     )"
                )?;

                if let Some(pause) = pause_after_gql_mutations {
                    // Hack: let's add a pause after mutations.
                    if quote.starts_with("mutation") {
                        writeln!(output, "sleep {}\n", pause.as_secs())?;
                    }
                }
            }
        }

        output.flush()?;
        Ok(())
    }
}

/// Returns an HTML response constructing the GraphiQL web page for the given URI.
pub(crate) async fn graphiql(uri: Uri) -> impl IntoResponse {
    let source = GraphiQLSource::build()
        .endpoint(uri.path())
        .subscription_endpoint("/ws")
        .finish();
    response::Html(source)
}

pub fn parse_millis(s: &str) -> Result<Duration, ParseIntError> {
    Ok(Duration::from_millis(s.parse()?))
}

pub fn parse_millis_delta(s: &str) -> Result<TimeDelta, ParseIntError> {
    Ok(TimeDelta::from_millis(s.parse()?))
}

#[test]
fn test_parse_version_message() {
    let s = "something\n . . . version12\nother things";
    assert_eq!(parse_version_message(s), "version12");

    let s = "something\n . . . version12other things";
    assert_eq!(parse_version_message(s), "things");

    let s = "something . . . version12 other things";
    assert_eq!(parse_version_message(s), "");

    let s = "";
    assert_eq!(parse_version_message(s), "");
}

#[test]
fn test_ignore() {
    let readme = r#"
first line
```bash
some bash
```
second line
```bash
some other bash
```
third line
```bash,ignore
this will be ignored
```
    "#;
    let buffer = std::io::Cursor::new(readme);
    let markdown = Markdown { buffer };
    let mut script = Vec::new();
    markdown
        .extract_bash_script_to(&mut script, None, None)
        .unwrap();
    let expected = "some bash\n\nsome other bash\n\n";
    assert_eq!(String::from_utf8_lossy(&script), expected);
}