grr-cli 0.13.4

Google tools from the terminal, at maximum performance: 401 methods across 14 Google APIs in one generated command tree, zero-config, over HTTP/3
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
#![cfg(feature = "cli")]
//! CLI integration tests: exercise the generated command surface.
//!
//! All tests use --help / clap failures only, so they need no config
//! file, network, or credential — except the dry-run parity pair, which
//! injects a dummy OAuth client via environment variables (the config
//! loader merges `GRR_OAUTH__*` over any file) and never reaches the
//! network because --dry-run returns before a token is requested.

use assert_cmd::Command;
use predicates::prelude::*;

fn grr() -> Command {
    let mut cmd = Command::cargo_bin("grr").unwrap();
    // The command path lazily auto-migrates skills when
    // ~/.agents/skills/.grr-skills.json records a different version. Point
    // HOME (and Windows' USERPROFILE) at a path that holds no manifest, so a
    // test can never read or write the developer's real skills.
    let home = std::env::temp_dir().join("grr-cli-test-home");
    cmd.env("HOME", &home).env("USERPROFILE", &home);
    cmd
}

/// A command with a dummy OAuth client injected so config loading and
/// auth-handle construction succeed without touching the network.
///
/// `GRR_CONFIG_PATH` points at a file that does not exist: the developer
/// machine running these tests has a real `~/.grr/config.toml`, and
/// figment rejects an env override landing on the same OAuth field the
/// file already fills (two aliases of one serde field). The missing file
/// is figment's "empty profile", so the test stays hermetic everywhere.
fn configured_grr() -> Command {
    let mut cmd = grr();
    cmd.env("GRR_CONFIG_PATH", "no-such-config-for-tests.toml")
        .env("GRR_OAUTH__CLIENT_ID", "test-id.apps.googleusercontent.com")
        .env("GRR_OAUTH__CLIENT_SECRET", "test-secret");
    cmd
}

#[test]
fn version_output_is_a_banner_with_the_semver_on_the_first_line() {
    // `--version` prints a banner: `grr <semver>`, the mascot, then the
    // tagline. The first line must stay exactly `grr <semver>` —
    // scripts/benchmark.ts parses it as the measured version and its gate
    // compares it against Cargo.toml, and generate-demo.ts matches
    // /^grr \d+\.\d+\.\d+$/m — while the Homebrew formula only needs the
    // version as a substring anywhere in the output.
    //
    // assert_cmd captures stdout through a pipe, so this also pins the
    // no-TTY contract: the plain ASCII crab, never escape sequences.
    grr()
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::starts_with(concat!(
            "grr ",
            env!("CARGO_PKG_VERSION"),
            "\n"
        )))
        // the mascot rides along: wide-set glossy eyes, small smile
        .stdout(predicate::str::contains("(o) (o)"))
        .stdout(predicate::str::contains("\\_/"))
        // the tagline is the crate's about line — factual, no marketing
        .stdout(predicate::str::contains(
            "Google tools from the terminal, at maximum performance",
        ))
        .stdout(predicate::str::contains("\u{1b}").not());
}

#[test]
fn version_draws_the_pixel_mascot_when_the_terminal_advertises_colour() {
    // `CLICOLOR_FORCE` is the opt-in that asks for the full logo through a
    // pipe. The banner must still lead with the bare semver — the colour
    // logo goes below it, never in front of it.
    let output = grr()
        .env_remove("NO_COLOR")
        .env("CLICOLOR_FORCE", "1")
        .arg("--version")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let banner = String::from_utf8(output).expect("banner is UTF-8");
    let first_line = banner.lines().next().expect("banner has a first line");
    assert_eq!(first_line, format!("grr {}", env!("CARGO_PKG_VERSION")));
    // U+2580 UPPER HALF BLOCK, carrying two pixel rows per character row.
    assert!(banner.contains('\u{2580}'));
    assert!(banner.contains("\u{1b}[38;2;"), "no truecolor foreground");
    assert!(banner.contains("\u{1b}[48;2;"), "no truecolor background");
    assert!(
        banner.contains("Google tools from the terminal, at maximum performance"),
        "the colour path drops the tagline"
    );
    // Every logo row is reset, so the logo cannot bleed into the tagline.
    assert!(banner.contains("\u{2580}\u{1b}[0m\n"));
}

#[test]
fn no_color_beats_the_force_opt_in() {
    // NO_COLOR's whole contract is "never colour my output"; an
    // environment setting both has said so twice, and NO_COLOR wins.
    grr()
        .env("NO_COLOR", "1")
        .env("CLICOLOR_FORCE", "1")
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("(o) (o)"))
        .stdout(predicate::str::contains("\u{2580}").not());
}

#[test]
fn top_level_help_lists_static_commands_and_every_service() {
    grr()
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("auth"))
        .stdout(predicate::str::contains("api"))
        .stdout(predicate::str::contains("schema"))
        .stdout(predicate::str::contains("completions"))
        .stdout(predicate::str::contains("transport"))
        .stdout(predicate::str::contains("skills"))
        .stdout(predicate::str::contains("gmail"))
        .stdout(predicate::str::contains("calendar"))
        .stdout(predicate::str::contains("drive"))
        .stdout(predicate::str::contains("people"))
        .stdout(predicate::str::contains("chat"))
        .stdout(predicate::str::contains("forms"))
        .stdout(predicate::str::contains("tasks"))
        .stdout(predicate::str::contains("docs"))
        .stdout(predicate::str::contains("sheets"))
        .stdout(predicate::str::contains("slides"));
}

#[test]
fn gmail_help_shows_the_resource_tree() {
    grr()
        .arg("gmail")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("users"))
        .stdout(predicate::str::contains("Gmail API operations"));
}

#[test]
fn gmail_users_messages_lists_leaf_commands() {
    grr()
        .arg("gmail")
        .arg("users")
        .arg("messages")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("gmail.users.messages.list"))
        .stdout(predicate::str::contains("gmail.users.messages.get"))
        .stdout(predicate::str::contains("gmail.users.messages.send"));
}

#[test]
fn message_list_help_documents_typed_flags() {
    grr()
        .arg("gmail")
        .arg("users")
        .arg("messages")
        .arg("list")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("--user-id <USER_ID>"))
        .stdout(predicate::str::contains("--max-results <MAX_RESULTS>"))
        .stdout(predicate::str::contains("--label-ids <LABEL_IDS>"))
        .stdout(predicate::str::contains("--q <Q>"))
        // required flags are promoted into the usage line by clap
        .stdout(predicate::str::contains(
            "gmail.users.messages.list [OPTIONS] --user-id <USER_ID>",
        ))
        // the escape hatches ride on every leaf
        .stdout(predicate::str::contains("--params <JSON>"))
        .stdout(predicate::str::contains("--body-file <PATH|->"))
        .stdout(predicate::str::contains("--query <KEY=VALUE>"))
        .stdout(predicate::str::contains("--dry-run"))
        .stdout(predicate::str::contains("-f, --format <FORMAT>"));
}

#[test]
fn a_leaf_rejects_a_missing_required_flag() {
    grr()
        .arg("gmail")
        .arg("users")
        .arg("messages")
        .arg("list")
        .assert()
        .failure()
        .stderr(predicate::str::contains("--user-id"));
}

#[test]
fn a_leaf_rejects_unknown_flags() {
    grr()
        .arg("gmail")
        .arg("users")
        .arg("messages")
        .arg("list")
        .arg("--user-id")
        .arg("me")
        .arg("--bogus")
        .assert()
        .failure()
        .stderr(predicate::str::contains("unexpected argument"));
}

#[test]
fn format_rejection_is_a_clap_error() {
    grr()
        .arg("gmail")
        .arg("users")
        .arg("messages")
        .arg("list")
        .arg("--user-id")
        .arg("me")
        .arg("--format")
        .arg("invalid")
        .assert()
        .failure()
        .stderr(predicate::str::contains("invalid value"));
}

#[test]
fn enum_typed_flags_reject_values_outside_the_documented_set() {
    grr()
        .arg("gmail")
        .arg("users")
        .arg("messages")
        .arg("get")
        .arg("--user-id")
        .arg("me")
        .arg("--id")
        .arg("m1")
        .arg("--param-format")
        .arg("not-a-format")
        .assert()
        .failure()
        .stderr(predicate::str::contains("invalid value"));
}

#[test]
fn bare_gmail_without_subcommand_is_a_usage_error() {
    grr().arg("gmail").assert().failure();
}

#[test]
fn auth_help_lists_login_and_status() {
    grr()
        .arg("auth")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("login"))
        .stdout(predicate::str::contains("status"))
        .stdout(predicate::str::contains("setup"))
        .stdout(predicate::str::contains("--device"));
}

#[test]
fn schema_dumps_json_without_config() {
    // Must succeed with no ~/.grr config: pure contract introspection.
    grr()
        .arg("schema")
        .assert()
        .success()
        .stdout(predicate::str::contains("\"name\":\"grr\""))
        .stdout(predicate::str::contains("\"subcommands\""))
        .stdout(predicate::str::contains("gmail.users.messages.list"))
        .stdout(predicate::str::contains("gmail"));
}

#[test]
fn schema_dumps_every_service() {
    grr()
        .arg("schema")
        .assert()
        .success()
        .stdout(predicate::str::contains("gmail"))
        .stdout(predicate::str::contains("calendar"))
        .stdout(predicate::str::contains("drive"))
        .stdout(predicate::str::contains("people"))
        .stdout(predicate::str::contains("chat"))
        .stdout(predicate::str::contains("forms"))
        .stdout(predicate::str::contains("tasks"))
        .stdout(predicate::str::contains("docs"))
        .stdout(predicate::str::contains("sheets"))
        .stdout(predicate::str::contains("slides"));
}

#[test]
fn schema_table_output_renders_field_value_table() {
    // Schema output is a single JSON object, so -f table takes the
    // Field/Value (non-array) table path. Array flattening is covered by
    // the unit tests in output.rs (arrays need live API results).
    grr()
        .arg("schema")
        .arg("-f")
        .arg("table")
        .assert()
        .success()
        .stdout(predicate::str::contains("Field"))
        .stdout(predicate::str::contains("Value"))
        .stdout(predicate::str::contains("name"))
        .stdout(predicate::str::contains("subcommands"));
}

#[test]
fn completions_cover_the_generated_tree_without_config() {
    // Each script must name the binary and reach into the generated
    // service tree, proving it is built from the composed root command
    // rather than the static derive commands alone.
    for (shell, marker) in [
        ("bash", "complete -F _grr"),
        ("zsh", "#compdef grr"),
        ("powershell", "Register-ArgumentCompleter"),
        ("elvish", "edit:completion:arg-completer[grr]"),
    ] {
        grr()
            .arg("completions")
            .arg(shell)
            .assert()
            .success()
            .stdout(predicate::str::contains(marker))
            .stdout(predicate::str::contains("gmail"))
            .stdout(predicate::str::contains("max-results"))
            .stderr(predicate::str::is_empty());
    }
}

#[test]
fn completions_refuse_fish() {
    // clap_complete's fish generator cannot reach grr's 3-5 level method
    // depth, so fish is refused rather than given a misleading script.
    grr()
        .arg("completions")
        .arg("fish")
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "possible values: bash, elvish, powershell, zsh",
        ));
}

#[test]
fn a_service_with_no_curated_history_is_reachable() {
    // Tasks never had hand-written commands; the generated tree is its
    // entire surface.
    grr()
        .arg("tasks")
        .arg("tasklists")
        .arg("list")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("tasks.tasklists.list"));
}

#[test]
fn the_generated_tree_and_api_call_print_identical_dry_runs() {
    // End-to-end parity: the same method and parameters through the
    // generated tree (typed flags) and through `grr api call` (--param)
    // must produce byte-identical dry-run output. Both stop before any
    // token use, so the dummy client is enough.
    let tree = configured_grr()
        .arg("gmail")
        .arg("users")
        .arg("messages")
        .arg("list")
        .arg("--user-id")
        .arg("me")
        .arg("--dry-run")
        .assert()
        .success();
    let tree_output = String::from_utf8_lossy(&tree.get_output().stdout).into_owned();

    let api = configured_grr()
        .arg("api")
        .arg("call")
        .arg("gmail.users.messages.list")
        .arg("--param")
        .arg("userId=me")
        .arg("--dry-run")
        .assert()
        .success();
    let api_output = String::from_utf8_lossy(&api.get_output().stdout).into_owned();

    assert_eq!(
        tree_output, api_output,
        "dry-run diverged between the generated tree and `grr api call`"
    );
    assert!(tree_output.contains("\"dryRun\":true"), "in: {tree_output}");
    assert!(
        tree_output.contains("https://gmail.googleapis.com/gmail/v1/users/me/messages"),
        "in: {tree_output}"
    );
}