tapes-client 0.1.0

One client for the whole tapes read surface: the sealed contract and a deployment's discovered cassettes, driven through a single pluggable transport.
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
//! Synthesizing clap commands from a cassette surface, and resolving them
//! back into calls.
//!
//! Every generated command is built here rather than derived, because the set
//! is not known until a server has been asked. The shape deliberately matches
//! a consumer's hand-written surface — `<noun> <method>`, whatever shared
//! flags the consumer decorates on, and the server's JSON printed verbatim —
//! so a cassette command is not visibly a second-class citizen next to a
//! hand-written one.
//!
//! # The consumer executes
//!
//! This module stops at [`resolve_invocation`], which turns a parsed match
//! back into the [`Method`] it names and the [`Call`] to make. Executing the
//! call and printing the response stay with the consumer: tapesctl reads its
//! own `--tapes-url` flag (added through the [`augment`] decorator), builds
//! its client, and prints the way its hand-written commands do.

use clap::{Arg, ArgMatches, Command};
use snafu::{OptionExt, ResultExt};

use crate::cassettes::spec::{Cassette, Location, Method, Surface};
use crate::error::{Result, error};
use crate::transport::Call;

/// The flag a request body is supplied through.
const BODY: &str = "body";

/// Add a subcommand for every cassette on the surface.
///
/// Cassette nouns are appended to the static ones rather than replacing them,
/// and a cassette whose name collides with a built-in command is skipped: a
/// server must not be able to redefine what a consumer's own command means on
/// someone's machine.
///
/// `decorate` is applied to every generated method command; it is where a
/// consumer adds the flags its dispatch reads back (tapesctl adds its
/// `--tapes-url`, with the `TAPES_URL` env fallback).
#[must_use]
pub fn augment<F>(mut base: Command, surface: &Surface, decorate: F) -> Command
where
    F: Fn(Command) -> Command,
{
    let built_in: Vec<String> = base
        .get_subcommands()
        .map(|sub| sub.get_name().to_owned())
        .collect();

    for cassette in &surface.cassettes {
        if built_in.iter().any(|name| name == &cassette.name) {
            tracing::debug!(
                cassette = %cassette.name,
                "a cassette shares its name with a built-in command and was not generated",
            );
            continue;
        }
        base = base.subcommand(cassette_command(cassette, &decorate));
    }
    base
}

/// The subcommand for one cassette.
#[must_use]
pub fn cassette_command<F>(cassette: &Cassette, decorate: &F) -> Command
where
    F: Fn(Command) -> Command,
{
    let about = cassette
        .description
        .clone()
        .unwrap_or_else(|| format!("Methods served by the {} cassette", cassette.name));

    let mut command = Command::new(cassette.name.clone())
        .about(about)
        // Without a method there is nothing to call, and the help that lists
        // them is the more useful answer than an error.
        .arg_required_else_help(true)
        .subcommand_required(true);

    for method in &cassette.methods {
        command = command.subcommand(method_command(method, decorate));
    }
    command
}

/// The subcommand for one method.
#[must_use]
pub fn method_command<F>(method: &Method, decorate: &F) -> Command
where
    F: Fn(Command) -> Command,
{
    let mut command = Command::new(method.name.clone());
    if let Some(summary) = &method.summary {
        command = command.about(summary.clone());
    }
    // The route is the one piece of context a user cannot recover from the
    // command name, and it is what makes a generated surface auditable.
    command = command.after_help(format!("Calls {} {}", method.http_method, method.path));

    for param in &method.params {
        let mut arg = Arg::new(param.flag.clone());
        if let Some(description) = &param.description {
            arg = arg.help(description.clone());
        }
        arg = match param.location {
            Location::Path => arg.required(true).value_name(param.flag.to_uppercase()),
            Location::Query | Location::Header => arg
                .long(param.flag.clone())
                .required(param.required)
                .value_name("VALUE"),
        };
        command = command.arg(arg);
    }

    if let Some(required) = method.body {
        command = command.arg(
            Arg::new(BODY)
                .long(BODY)
                .required(required)
                .value_name("JSON")
                .help("Request body as JSON, or @<path> to read it from a file"),
        );
    }

    decorate(command)
}

/// Resolve a matched cassette invocation back into the method it names and
/// the call to make.
///
/// `matches` is the cassette-level match; its own subcommand names the method.
/// Executing the returned [`Call`] — and everything about where to send it —
/// is the consumer's.
pub fn resolve_invocation<'s>(
    surface: &'s Surface,
    name: &str,
    matches: &ArgMatches,
) -> Result<(&'s Method, Call<'s>)> {
    let cassette = surface
        .cassette(name)
        .context(error::UnknownCassetteSnafu { name })?;
    let (method_name, method_matches) =
        matches.subcommand().context(error::UnknownMethodSnafu {
            cassette: name,
            method: "",
        })?;
    let method = cassette
        .methods
        .iter()
        .find(|candidate| candidate.name == method_name)
        .context(error::UnknownMethodSnafu {
            cassette: name,
            method: method_name,
        })?;

    let call = call_for(method, method_matches)?;
    Ok((method, call))
}

/// Assemble the request for a matched method.
pub fn call_for<'a>(method: &'a Method, matches: &ArgMatches) -> Result<Call<'a>> {
    let mut call = Call {
        method: &method.http_method,
        path: &method.path,
        ..Default::default()
    };

    for param in &method.params {
        let Some(value) = matches.get_one::<String>(&param.flag) else {
            continue;
        };
        let pair = (param.wire.clone(), value.clone());
        match param.location {
            Location::Path => call.path_params.push(pair),
            Location::Query => call.query.push(pair),
            Location::Header => call.headers.push(pair),
        }
    }

    // Only ask for `--body` when the operation declared one. clap panics on a
    // lookup of an argument id the command does not define, so an unconditional
    // read would crash every method that takes no body.
    if method.body.is_some() {
        if let Some(raw) = matches.get_one::<String>(BODY) {
            call.body = Some(read_body(raw)?);
        }
    }

    Ok(call)
}

/// Resolve a `--body` value, which is either JSON or `@<path>`.
///
/// The body is parsed before it is sent, not passed through: a typo in a JSON
/// literal is otherwise reported by the cassette as a 400 whose message is about
/// the cassette's schema rather than about the quoting mistake that caused it.
pub fn read_body(raw: &str) -> Result<String> {
    let text = match raw.strip_prefix('@') {
        Some(path) => std::fs::read_to_string(path).context(error::BodyFileSnafu { path })?,
        None => raw.to_owned(),
    };
    let parsed: serde_json::Value = serde_json::from_str(&text).context(error::InvalidBodySnafu)?;
    serde_json::to_string(&parsed).context(error::RenderBodySnafu)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use crate::cassettes::spec::{self, ReducerConfig};
    use clap::ArgAction;
    use serde_json::json;

    /// The list tapesctl reserves, which these moved tests were written
    /// against.
    const RESERVED: ReducerConfig<'static> = ReducerConfig {
        reserved_flags: &["tapes-url", "body", "help", "verbose"],
    };

    /// The decorator tapesctl passes: its server flag, with the env fallback.
    fn with_tapes_url(command: Command) -> Command {
        command.arg(
            Arg::new("tapes-url")
                .long("tapes-url")
                .env("TAPES_URL")
                .action(ArgAction::Set)
                .value_name("URL")
                .help("Base URL of the tapes server"),
        )
    }

    /// Shadow of [`super::augment`] pinning that decorator, so the moved test
    /// bodies read exactly as they did before the extraction.
    fn augment(base: Command, surface: &Surface) -> Command {
        super::augment(base, surface, with_tapes_url)
    }

    fn surface_from(name: &str, document: &serde_json::Value) -> Surface {
        Surface {
            cassettes: vec![spec::reduce(name, None, document, &RESERVED)],
        }
    }

    fn hello_surface() -> Surface {
        surface_from(
            "hello-world",
            &json!({"paths": {"/v1/cassettes/hello-world/hello": {
                "get": {"operationId": "getHello", "summary": "Greet"},
                "post": {"operationId": "createHello", "requestBody": {"required": true}}
            }}}),
        )
    }

    fn root() -> Command {
        Command::new("tapesctl").subcommand(Command::new("sessions"))
    }

    #[test]
    fn a_generated_surface_is_a_well_formed_clap_definition() {
        // clap panics at runtime on a malformed definition, and this crate
        // denies panics — so a spec that produced one would be a crash the user
        // triggers just by pointing a consumer at their own server.
        augment(root(), &hello_surface()).debug_assert();
    }

    #[test]
    fn a_consumer_reserving_both_spellings_still_gets_a_well_formed_command() {
        // The adversarial case behind the reserved list's re-rewrite: the
        // consumer's decorator defines --param-body as well as --body, and a
        // cassette parameter named `body` must be pushed past BOTH spellings
        // — one rewrite pass would hand clap a duplicate id and panic at
        // command construction.
        let reserved = ReducerConfig {
            reserved_flags: &["tapes-url", "body", "param-body", "help", "verbose"],
        };
        let document = json!({"paths": {"/v1/cassettes/c/thing": {
            "post": {"operationId": "createThing", "requestBody": {"required": true},
                "parameters": [
                    {"name": "body", "in": "query"},
                    {"name": "param_body", "in": "query"}
                ]}
        }}});
        let surface = Surface {
            cassettes: vec![spec::reduce("c", None, &document, &reserved)],
        };
        let decorate = |command: Command| {
            with_tapes_url(command).arg(
                Arg::new("param-body")
                    .long("param-body")
                    .value_name("VALUE"),
            )
        };

        let command = super::augment(root(), &surface, decorate);
        command.clone().debug_assert();

        // And the rewritten flags are usable, not just panic-free.
        let matches = command
            .try_get_matches_from([
                "tapesctl",
                "c",
                "create-thing",
                "--body",
                "{}",
                "--param-param-body",
                "wire-body",
                "--param-param-body-2",
                "wire-param-body",
                "--tapes-url",
                "http://x",
            ])
            .unwrap();
        let (_, cassette_matches) = matches.subcommand().unwrap();
        let (_, method_matches) = cassette_matches.subcommand().unwrap();
        assert_eq!(
            method_matches
                .get_one::<String>("param-param-body")
                .unwrap(),
            "wire-body",
        );
        assert_eq!(
            method_matches
                .get_one::<String>("param-param-body-2")
                .unwrap(),
            "wire-param-body",
        );
    }

    #[test]
    fn a_cassette_becomes_a_noun_and_its_operations_become_methods() {
        let command = augment(root(), &hello_surface());
        let cassette = command
            .get_subcommands()
            .find(|sub| sub.get_name() == "hello-world")
            .expect("the cassette noun should be generated");
        let methods: Vec<&str> = cassette
            .get_subcommands()
            .map(clap::Command::get_name)
            .collect();
        assert!(methods.contains(&"get-hello"), "got: {methods:?}");
        assert!(methods.contains(&"create-hello"), "got: {methods:?}");
    }

    #[test]
    fn a_cassette_cannot_redefine_a_built_in_command() {
        // A server that shipped a cassette named `sessions` would otherwise
        // change what an existing command does on the user's machine.
        let surface = surface_from(
            "sessions",
            &json!({"paths": {"/v1/cassettes/sessions/x": {"get": {"operationId": "getX"}}}}),
        );
        let command = augment(root(), &surface);
        let sessions: Vec<&clap::Command> = command
            .get_subcommands()
            .filter(|sub| sub.get_name() == "sessions")
            .collect();
        assert_eq!(sessions.len(), 1);
        assert_eq!(sessions[0].get_subcommands().count(), 0);
    }

    #[test]
    fn the_generated_help_names_the_route_it_calls() {
        // The one thing a user cannot infer from the command name.
        let mut command = augment(root(), &hello_surface());
        let help = command
            .find_subcommand_mut("hello-world")
            .and_then(|c| c.find_subcommand_mut("get-hello"))
            .unwrap()
            .render_long_help()
            .to_string();
        assert!(
            help.contains("GET /v1/cassettes/hello-world/hello"),
            "got: {help}"
        );
    }

    #[test]
    fn the_decorator_reaches_every_generated_method() {
        // The decorated flag is what a consumer's dispatch reads back; a
        // method it missed would parse and then have nowhere to send the call.
        let mut command = augment(root(), &hello_surface());
        for name in ["get-hello", "create-hello"] {
            let help = command
                .find_subcommand_mut("hello-world")
                .and_then(|c| c.find_subcommand_mut(name))
                .unwrap()
                .render_long_help()
                .to_string();
            assert!(help.contains("--tapes-url"), "{name} lost the flag: {help}");
        }
    }

    #[test]
    fn a_path_parameter_parses_as_a_positional_and_a_query_parameter_as_a_flag() {
        let surface = surface_from(
            "summary",
            &json!({"paths": {"/v1/cassettes/summary/reports/{id}": {
                "get": {"operationId": "getReport", "parameters": [
                    {"name": "id", "in": "path", "required": true},
                    {"name": "since", "in": "query"}
                ]}
            }}}),
        );
        let matches = augment(root(), &surface)
            .try_get_matches_from([
                "tapesctl",
                "summary",
                "get-report",
                "r-1",
                "--since",
                "yesterday",
                "--tapes-url",
                "http://x",
            ])
            .unwrap();

        let (name, cassette) = matches.subcommand().unwrap();
        assert_eq!(name, "summary");
        let (_, method) = cassette.subcommand().unwrap();
        assert_eq!(method.get_one::<String>("id").unwrap(), "r-1");
        assert_eq!(method.get_one::<String>("since").unwrap(), "yesterday");
    }

    #[test]
    fn a_missing_required_path_parameter_is_rejected_before_any_request() {
        let surface = surface_from(
            "summary",
            &json!({"paths": {"/v1/cassettes/summary/reports/{id}": {
                "get": {"operationId": "getReport"}
            }}}),
        );
        assert!(
            augment(root(), &surface)
                .try_get_matches_from([
                    "tapesctl",
                    "summary",
                    "get-report",
                    "--tapes-url",
                    "http://x"
                ])
                .is_err(),
        );
    }

    #[test]
    fn a_required_body_is_required_and_an_absent_one_is_not_offered() {
        let command = augment(root(), &hello_surface());
        assert!(
            command
                .clone()
                .try_get_matches_from([
                    "tapesctl",
                    "hello-world",
                    "create-hello",
                    "--tapes-url",
                    "http://x"
                ])
                .is_err(),
            "a required body must be demanded up front",
        );
        // `get-hello` declares no request body, so `--body` is not a flag it has.
        assert!(
            command
                .try_get_matches_from([
                    "tapesctl",
                    "hello-world",
                    "get-hello",
                    "--body",
                    "{}",
                    "--tapes-url",
                    "http://x",
                ])
                .is_err(),
        );
    }

    #[test]
    fn a_method_that_takes_no_body_still_builds_a_call() {
        // clap panics on a lookup of an argument id the command does not
        // define, so reading `--body` unconditionally crashed every method that
        // declares none — which is most of them.
        let surface = surface_from(
            "summary",
            &json!({"paths": {"/v1/cassettes/summary/reports": {
                "get": {"operationId": "listReports"}
            }}}),
        );
        let matches = augment(root(), &surface)
            .try_get_matches_from([
                "tapesctl",
                "summary",
                "list-reports",
                "--tapes-url",
                "http://x",
            ])
            .unwrap();
        let (_, cassette) = matches.subcommand().unwrap();
        let (_, method_matches) = cassette.subcommand().unwrap();

        let cassette_spec = surface.cassette("summary").unwrap();
        let call = call_for(&cassette_spec.methods[0], method_matches).unwrap();
        assert!(call.body.is_none());
    }

    #[test]
    fn a_body_is_validated_as_json_before_it_is_sent() {
        // The cassette's 400 would be about its schema, not about the quoting.
        assert!(read_body("{\"a\":1}").is_ok());
        assert!(read_body("not json").is_err());
    }

    #[test]
    fn a_body_can_be_read_from_a_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("body.json");
        std::fs::write(&path, "{\"hello\": \"world\"}").unwrap();

        let body = read_body(&format!("@{}", path.display())).unwrap();
        assert_eq!(body, r#"{"hello":"world"}"#);
        assert!(read_body("@/nonexistent/body.json").is_err());
    }

    #[test]
    fn parameters_are_sent_under_their_wire_names_not_their_flag_names() {
        // `--auth-subject` on the command line, `auth_subject` on the wire —
        // the same split a hand-written surface makes.
        let surface = surface_from(
            "summary",
            &json!({"paths": {"/v1/cassettes/summary/reports": {
                "get": {"operationId": "listReports", "parameters": [
                    {"name": "auth_subject", "in": "query"},
                    {"name": "X-Report-Kind", "in": "header"}
                ]}
            }}}),
        );
        let matches = augment(root(), &surface)
            .try_get_matches_from([
                "tapesctl",
                "summary",
                "list-reports",
                "--auth-subject",
                "local:me",
                "--x-report-kind",
                "daily",
                "--tapes-url",
                "http://x",
            ])
            .unwrap();
        let (_, cassette) = matches.subcommand().unwrap();
        let (_, method_matches) = cassette.subcommand().unwrap();

        let cassette_spec = surface.cassette("summary").unwrap();
        let call = call_for(&cassette_spec.methods[0], method_matches).unwrap();

        assert_eq!(
            call.query,
            vec![("auth_subject".to_owned(), "local:me".to_owned())]
        );
        assert_eq!(
            call.headers,
            vec![("X-Report-Kind".to_owned(), "daily".to_owned())]
        );
    }

    #[tokio::test]
    async fn a_resolved_invocation_calls_the_route_the_spec_named() {
        use crate::http::DirectHttp;
        use url::Url;
        use wiremock::matchers::{method, path, query_param};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/cassettes/summary/reports/r-1"))
            .and(query_param("since", "yesterday"))
            .respond_with(ResponseTemplate::new(200).set_body_string(r#"{"report":"r-1"}"#))
            .mount(&server)
            .await;

        let surface = surface_from(
            "summary",
            &json!({"paths": {"/v1/cassettes/summary/reports/{id}": {
                "get": {"operationId": "getReport", "parameters": [
                    {"name": "id", "in": "path", "required": true},
                    {"name": "since", "in": "query"}
                ]}
            }}}),
        );
        let matches = augment(root(), &surface)
            .try_get_matches_from([
                "tapesctl",
                "summary",
                "get-report",
                "r-1",
                "--since",
                "yesterday",
                "--tapes-url",
                &server.uri(),
            ])
            .unwrap();
        let (name, cassette_matches) = matches.subcommand().unwrap();

        let (_method, call) = resolve_invocation(&surface, name, cassette_matches).unwrap();
        let transport = DirectHttp::new(Url::parse(&server.uri()).unwrap());
        let result = transport.execute(&call).await;
        assert!(result.is_ok(), "got: {result:?}");
    }

    #[tokio::test]
    async fn a_cassette_error_body_is_surfaced_rather_than_the_bare_status() {
        use crate::http::DirectHttp;
        use url::Url;
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/v1/cassettes/summary/reports"))
            .respond_with(ResponseTemplate::new(502).set_body_string(
                r#"{"error":"cassette_unavailable","message":"summary is not answering"}"#,
            ))
            .mount(&server)
            .await;

        let surface = surface_from(
            "summary",
            &json!({"paths": {"/v1/cassettes/summary/reports": {
                "get": {"operationId": "listReports"}
            }}}),
        );
        let matches = augment(root(), &surface)
            .try_get_matches_from([
                "tapesctl",
                "summary",
                "list-reports",
                "--tapes-url",
                &server.uri(),
            ])
            .unwrap();
        let (name, cassette_matches) = matches.subcommand().unwrap();

        let (_method, call) = resolve_invocation(&surface, name, cassette_matches).unwrap();
        let transport = DirectHttp::new(Url::parse(&server.uri()).unwrap());
        let err = transport.execute(&call).await.unwrap_err();
        let rendered = format!("{err}");
        assert!(rendered.contains("502"), "got: {rendered}");
        assert!(rendered.contains("cassette_unavailable"), "got: {rendered}");
    }

    #[test]
    fn an_unknown_method_resolves_to_an_error_not_a_guessed_call() {
        let surface = hello_surface();
        let matches = augment(root(), &surface)
            .try_get_matches_from([
                "tapesctl",
                "hello-world",
                "get-hello",
                "--tapes-url",
                "http://x",
            ])
            .unwrap();
        let (_, cassette_matches) = matches.subcommand().unwrap();

        let err = resolve_invocation(&surface, "absent", cassette_matches).unwrap_err();
        assert!(err.to_string().contains("absent"), "got: {err}");
    }
}