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
#![cfg_attr(test, allow(clippy::expect_used, clippy::unwrap_used))]

use std::path::PathBuf;

use assert_cmd::cargo::cargo_bin_cmd;
use predicates::prelude::*;
use tempfile::TempDir;

const VALID: &str = r#"{:variants {:mobile-dark {:viewport [375 812] :scheme :dark}
            :desktop-light {:viewport [1280 900]}
            :desktop-dark {:viewport [1280 900] :scheme :dark}}
 :baseline {:repo "kejadlen/ketchup"}
 :server (fn [] {:cmd ["bundle" "exec" "rake" "snapshots:serve"]})
 :flow (fn [{: goto : wait-for : click : snap : variant}] nil)}"#;

const RUNNABLE: &str = r#"{:variants {:desktop {:viewport [1280 900]}}
 :server (fn [] {:cmd ["cargo" "run" "--quiet" "--package" "parallax-fixture"] :timeout 120})
 :flow (fn [] nil)}"#;

fn config_file(source: &str) -> (TempDir, PathBuf) {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("config.fnl");
    fs_err::write(&path, source).expect("write config");
    (dir, path)
}

fn list_fails_with(source: &str, fragments: &[&str]) {
    let (_dir, path) = config_file(source);
    let mut stderr = predicate::str::contains("config.fnl").boxed();
    for fragment in fragments {
        stderr = stderr.and(predicate::str::contains(*fragment)).boxed();
    }
    cargo_bin_cmd!("parallax")
        .arg("list")
        .arg("--config")
        .arg(&path)
        .assert()
        .failure()
        .stderr(stderr);
}

#[test]
fn lists_variant_names_in_run_order() {
    let (_dir, path) = config_file(VALID);
    cargo_bin_cmd!("parallax")
        .arg("list")
        .arg("--config")
        .arg(&path)
        .assert()
        .success()
        .stdout("desktop-dark\ndesktop-light\nmobile-dark\n");
}

#[test]
fn list_defaults_to_parallax_fnl_in_the_current_directory() {
    let dir = tempfile::tempdir().expect("tempdir");
    fs_err::write(dir.path().join("parallax.fnl"), VALID).expect("write config");
    cargo_bin_cmd!("parallax")
        .arg("list")
        .current_dir(dir.path())
        .assert()
        .success()
        .stdout("desktop-dark\ndesktop-light\nmobile-dark\n");
}

#[test]
fn missing_config_file_fails() {
    cargo_bin_cmd!("parallax")
        .arg("--config")
        .arg("missing.fnl")
        .arg("list")
        .assert()
        .failure()
        .stderr(predicate::str::contains("missing.fnl"));
}

#[test]
fn syntax_error_points_at_the_config_line() {
    list_fails_with("(local ok 1)\n)\n", &["config.fnl:2:", "Parse error"]);
}

#[test]
fn empty_variants_fail() {
    list_fails_with(
        "{:variants {} :server (fn [] {:cmd [\"true\"]}) :flow (fn [] nil)}\n",
        &["variants must not be empty"],
    );
}

#[test]
fn unknown_fields_fail() {
    list_fails_with(
        concat!(
            "{:variants {:desktop {:viewport [1280 900]}} :extra 1\n",
            " :server (fn [] {:cmd [\"true\"]}) :flow (fn [] nil)}\n",
        ),
        &["unknown field `extra`", "`server`, `flow`"],
    );
}

#[test]
fn nested_errors_name_the_key_path() {
    list_fails_with(
        concat!(
            "{:variants {:desktop {:viewport [1280 900] :shceme :dark}}\n",
            " :server (fn [] {:cmd [\"true\"]}) :flow (fn [] nil)}\n",
        ),
        &["variants.desktop.shceme:"],
    );
}

#[test]
fn non_table_config_fails() {
    list_fails_with("\"nope\"\n", &["must return a table, not string"]);
}

#[test]
fn config_that_returns_nothing_fails() {
    list_fails_with("(local ok 1)\n", &["must return a table, not nil"]);
}

#[test]
fn server_must_be_a_function() {
    list_fails_with(
        "{:variants {:desktop {:viewport [1280 900]}} :server \"nope\" :flow (fn [] nil)}\n",
        &["server must be a function"],
    );
}

#[test]
fn flow_must_be_a_function() {
    list_fails_with(
        "{:variants {:desktop {:viewport [1280 900]}} :server (fn [] nil) :flow \"nope\"}\n",
        &["flow must be a function"],
    );
}

#[test]
fn run_starts_and_stops_the_server_per_variant() {
    let (_dir, path) = config_file(RUNNABLE);
    let output = tempfile::tempdir().expect("tempdir");
    cargo_bin_cmd!("parallax")
        .arg("run")
        .arg("--config")
        .arg(&path)
        .arg("--output")
        .arg(output.path())
        .assert()
        .success();
    let log = output.path().join("desktop").join("server.log");
    assert!(
        fs_err::metadata(&log)
            .map(|meta| meta.is_file())
            .unwrap_or(false)
    );
}

#[test]
fn run_without_an_output_dir_still_works() {
    let (_dir, path) = config_file(RUNNABLE);
    cargo_bin_cmd!("parallax")
        .arg("run")
        .arg("--config")
        .arg(&path)
        .assert()
        .success();
}

#[test]
fn shows_help() {
    cargo_bin_cmd!("parallax").arg("--help").assert().success();
}

#[test]
fn shows_version() {
    cargo_bin_cmd!("parallax")
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("parallax"));
}