upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/test_utils/src/lib.rs
blob: 1a4231aed6e2933762ca73c98ae40d3da3441412 (plain)
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
use std::ffi::OsStr;

use anyhow::{ensure, Context, Result};
use dialoguer::theme::{ColorfulTheme, Theme};
use directories::ProjectDirs;
use nostr::{self, prelude::FromSkStr};
use once_cell::sync::Lazy;
use rexpect::session::{Options, PtySession};
use strip_ansi_escapes::strip_str;

pub static TEST_KEY_1_NSEC: &str =
    "nsec1ppsg5sm2aexq06juxmu9evtutr6jkwkhp98exxxvwamhru9lyx9s3rwseq";
pub static TEST_KEY_1_SK_HEX: &str =
    "08608a436aee4c07ea5c36f85cb17c58f52b3ad7094f9318cc777771f0bf218b";
pub static TEST_KEY_1_NPUB: &str =
    "npub175lyhnt6nn00qjw0v3navw9pxgv43txnku0tpxprl4h6mvpr6a5qlphudg";
pub static TEST_KEY_1_DISPLAY_NAME: &str = "bob";
pub static TEST_KEY_1_ENCRYPTED: &str = "ncryptsec1qyq607h3cykxc3f2a44u89cdk336fptccn3fm5pf3nmf93d3c86qpunc7r6klwcn6lyszjy72wxwqq9aljg4pm6atvjrds9e248yhv76xfnt464265kgnjsvg8rlg06wg4sp9uljzfpu8zuaztcvfn2j8ggdrg8mldh850cy75efsyqqansert9wqmn4e6khpgvfz7h5le9";
pub static TEST_KEY_1_ENCRYPTED_WEAK: &str = "ncryptsec1qy8ke0tjqnn8wt3w6lnc86c27ry3qrptxctjfcgruryxy0at238kwyjwsswd7z88thysruzw3awlrsxjvw5uptcd7vt70ft9rtkx00m8cgy3khm4hxa5d2gfnc6athnfruy2eyl6pkas8k34jg85z7xjqqadzfzh9rp0fzxqtw0tvxksac3n8yc98uksvuf93e0lcvqy8j6";
pub static TEST_KEY_1_KEYS: Lazy<nostr::Keys> =
    Lazy::new(|| nostr::Keys::from_sk_str(TEST_KEY_1_NSEC).unwrap());

pub static TEST_KEY_2_NSEC: &str =
    "nsec1ypglg6nj6ep0g2qmyfqcv2al502gje3jvpwye6mthmkvj93tqkesknv6qm";
pub static TEST_KEY_2_NPUB: &str =
    "npub1h2yz2eh0798nh25hvypenrz995nla9dktfuk565ljf3ghnkhdljsul834e";

pub static TEST_KEY_2_DISPLAY_NAME: &str = "carole";
pub static TEST_KEY_2_ENCRYPTED: &str = "...2";
pub static TEST_KEY_2_KEYS: Lazy<nostr::Keys> =
    Lazy::new(|| nostr::Keys::from_sk_str(TEST_KEY_2_NSEC).unwrap());

pub static TEST_INVALID_NSEC: &str = "nsec1ppsg5sm2aex";
pub static TEST_PASSWORD: &str = "769dfd£pwega8SHGv3!#Bsfd5t";
pub static TEST_INVALID_PASSWORD: &str = "INVALID769dfd£pwega8SHGv3!";
pub static TEST_WEAK_PASSWORD: &str = "fhaiuhfwe";
pub static TEST_RANDOM_TOKEN: &str = "lkjh2398HLKJ43hrweiJ6FaPfdssgtrg";

/// wrapper for a cli testing tool - currently wraps rexpect and dialoguer
///
/// 1. allow more accurate articulation of expected behaviour
/// 2. provide flexibility to swap rexpect for a tool that better maps to
///    expected behaviour
/// 3. provides flexability to swap dialoguer with another cli interaction tool
pub struct CliTester {
    rexpect_session: PtySession,
    formatter: ColorfulTheme,
}

impl CliTester {
    pub fn expect_input(&mut self, prompt: &str) -> Result<CliTesterInputPrompt> {
        let mut i = CliTesterInputPrompt {
            tester: self,
            prompt: prompt.to_string(),
        };
        i.prompt(false).context("initial input prompt")?;
        Ok(i)
    }

    pub fn expect_input_eventually(&mut self, prompt: &str) -> Result<CliTesterInputPrompt> {
        let mut i = CliTesterInputPrompt {
            tester: self,
            prompt: prompt.to_string(),
        };
        i.prompt(true).context("initial input prompt")?;
        Ok(i)
    }

    pub fn expect_password(&mut self, prompt: &str) -> Result<CliTesterPasswordPrompt> {
        let mut i = CliTesterPasswordPrompt {
            tester: self,
            prompt: prompt.to_string(),
            confirmation_prompt: "".to_string(),
        };
        i.prompt().context("initial password prompt")?;
        Ok(i)
    }
}

pub struct CliTesterInputPrompt<'a> {
    tester: &'a mut CliTester,
    prompt: String,
}

impl CliTesterInputPrompt<'_> {
    fn prompt(&mut self, eventually: bool) -> Result<&mut Self> {
        let mut s = String::new();
        self.tester
            .formatter
            .format_prompt(&mut s, self.prompt.as_str())
            .expect("diagluer theme formatter should succeed");
        s.push(' ');

        ensure!(
            s.contains(self.prompt.as_str()),
            "dialoguer must be broken as formatted prompt success doesnt contain prompt"
        );

        if eventually {
            self.tester
                .expect_eventually(sanatize(s).as_str())
                .context("expect input prompt eventually")?;
        } else {
            self.tester
                .expect(sanatize(s).as_str())
                .context("expect input prompt")?;
        }

        Ok(self)
    }

    pub fn succeeds_with(&mut self, input: &str) -> Result<&mut Self> {
        self.tester.send_line(input)?;
        self.tester
            .expect(input)
            .context("expect input to be printed")?;
        self.tester
            .expect("\r")
            .context("expect new line after input to be printed")?;

        let mut s = String::new();
        self.tester
            .formatter
            .format_input_prompt_selection(&mut s, self.prompt.as_str(), input)
            .expect("diagluer theme formatter should succeed");
        if !s.contains(self.prompt.as_str()) {
            panic!("dialoguer must be broken as formatted prompt success doesnt contain prompt");
        }
        let formatted_success = format!("{}\r\n", sanatize(s));

        self.tester
            .expect(formatted_success.as_str())
            .context("expect immediate prompt success")?;
        Ok(self)
    }
}

pub struct CliTesterPasswordPrompt<'a> {
    tester: &'a mut CliTester,
    prompt: String,
    confirmation_prompt: String,
}

impl CliTesterPasswordPrompt<'_> {
    fn prompt(&mut self) -> Result<&mut Self> {
        let p = match self.confirmation_prompt.is_empty() {
            true => self.prompt.as_str(),
            false => self.confirmation_prompt.as_str(),
        };

        let mut s = String::new();
        self.tester
            .formatter
            .format_password_prompt(&mut s, p)
            .expect("diagluer theme formatter should succeed");

        ensure!(s.contains(p), "dialoguer must be broken");

        self.tester
            .expect(format!("\r{}", sanatize(s)).as_str())
            .context("expect password input prompt")?;
        Ok(self)
    }

    pub fn with_confirmation(&mut self, prompt: &str) -> Result<&mut Self> {
        self.confirmation_prompt = prompt.to_string();
        Ok(self)
    }

    pub fn succeeds_with(&mut self, password: &str) -> Result<&mut Self> {
        self.tester.send_line(password)?;

        self.tester
            .expect("\r\n")
            .context("expect new lines after password input")?;

        if !self.confirmation_prompt.is_empty() {
            self.prompt()
                .context("expect password confirmation prompt")?;
            self.tester.send_line(password)?;
            self.tester
                .expect("\r\n\r")
                .context("expect new lines after password confirmation input")?;
        }

        let mut s = String::new();
        self.tester
            .formatter
            .format_password_prompt_selection(&mut s, self.prompt.as_str())
            .expect("diagluer theme formatter should succeed");

        ensure!(s.contains(self.prompt.as_str()), "dialoguer must be broken");

        self.tester
            .expect(format!("\r{}\r\n", sanatize(s)).as_str())
            .context("expect password prompt success")?;

        Ok(self)
    }
}

impl CliTester {
    pub fn new<I, S>(args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        Self {
            rexpect_session: rexpect_with(args, 2000).expect("rexpect to spawn new process"),
            formatter: ColorfulTheme::default(),
        }
    }
    pub fn new_with_timeout<I, S>(timeout_ms: u64, args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        Self {
            rexpect_session: rexpect_with(args, timeout_ms).expect("rexpect to spawn new process"),
            formatter: ColorfulTheme::default(),
        }
    }

    pub fn restart_with<I, S>(&mut self, args: I) -> &mut Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        self.rexpect_session
            .process
            .exit()
            .expect("process to exit");
        self.rexpect_session = rexpect_with(args, 2000).expect("rexpect to spawn new process");
        self
    }

    pub fn exit(&mut self) -> Result<()> {
        match self
            .rexpect_session
            .process
            .exit()
            .context("expect proccess to exit")
        {
            Ok(_) => Ok(()),
            Err(e) => Err(e),
        }
    }

    /// returns what came before expected message
    pub fn expect_eventually(&mut self, message: &str) -> Result<String> {
        let before = self
            .rexpect_session
            .exp_string(message)
            .context("exp_string failed")?;
        Ok(before)
    }

    pub fn expect(&mut self, message: &str) -> Result<&mut Self> {
        let before = self.expect_eventually(message)?;
        ensure!(
            before.is_empty(),
            format!(
                "expected message \"{}\". but got \"{}\" first.",
                message.replace('\n', "\\n").replace('\r', "\\r"),
                before.replace('\n', "\\n").replace('\r', "\\r"),
            ),
        );
        Ok(self)
    }

    pub fn expect_end(&mut self) -> Result<()> {
        let before = self
            .rexpect_session
            .exp_eof()
            .context("expected immediate end but got timed out")?;
        ensure!(
            before.is_empty(),
            format!(
                "expected immediate end but got '{}' first.",
                before.replace('\n', "\\n").replace('\r', "\\r"),
            ),
        );
        Ok(())
    }

    pub fn expect_end_with(&mut self, message: &str) -> Result<()> {
        let before = self
            .rexpect_session
            .exp_eof()
            .context("expected immediate end but got timed out")?;
        assert_eq!(before, message);
        Ok(())
    }
    pub fn expect_end_eventually(&mut self) -> Result<String> {
        self.rexpect_session
            .exp_eof()
            .context("expected end eventually but got timed out")
    }

    pub fn expect_end_eventually_with(&mut self, message: &str) -> Result<()> {
        self.expect_eventually(message)?;
        self.expect_end()
    }

    fn send_line(&mut self, line: &str) -> Result<()> {
        self.rexpect_session
            .send_line(line)
            .context("send_line failed")?;
        Ok(())
    }
}

/// sanatize unicode string for rexpect
fn sanatize(s: String) -> String {
    // remove ansi codes as they don't work with rexpect
    strip_str(s)
        // sanatize unicode rexpect issue 105 is resolved https://github.com/rust-cli/rexpect/issues/105
        .as_bytes()
        .iter()
        .map(|c| *c as char)
        .collect::<String>()
}

pub fn rexpect_with<I, S>(args: I, timeout_ms: u64) -> Result<PtySession, rexpect::error::Error>
where
    I: IntoIterator<Item = S>,
    S: AsRef<std::ffi::OsStr>,
{
    let mut cmd = std::process::Command::new(assert_cmd::cargo::cargo_bin("ngit"));
    cmd.args(args);
    // using branch for PR https://github.com/rust-cli/rexpect/pull/103 to strip ansi escape codes
    rexpect::session::spawn_with_options(
        cmd,
        Options {
            timeout_ms: Some(timeout_ms),
            strip_ansi_escape_codes: true,
        },
    )
}

/// backup and remove application config and data
pub fn before() -> Result<()> {
    backup_existing_config()
}

/// restore backuped application config and data
pub fn after() -> Result<()> {
    restore_config_backup()
}

/// run func between before and after scripts which backup, reset and restore
/// application config
///
/// TODO: fix issue: if func panics, after() is not run.
pub fn with_fresh_config<F>(func: F) -> Result<()>
where
    F: Fn() -> Result<()>,
{
    before()?;
    func()?;
    after()
}

fn backup_existing_config() -> Result<()> {
    let config_path = get_dirs().config_dir().join("config.json");
    let backup_config_path = get_dirs().config_dir().join("config-backup.json");
    if config_path.exists() {
        std::fs::rename(config_path, backup_config_path)?;
    }
    Ok(())
}

fn restore_config_backup() -> Result<()> {
    let config_path = get_dirs().config_dir().join("config.json");
    let backup_config_path = get_dirs().config_dir().join("config-backup.json");
    if config_path.exists() {
        std::fs::remove_file(&config_path)?;
    }
    if backup_config_path.exists() {
        std::fs::rename(backup_config_path, config_path)?;
    }
    Ok(())
}

fn get_dirs() -> ProjectDirs {
    ProjectDirs::from("", "CodeCollaboration", "ngit")
        .expect("rust directories crate should return ProjectDirs")
}