apic-cli 0.2.2

A lightweight, Git-friendly CLI tool for designing and collaborating on API contracts
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
//! End-to-end tests that drive the actual `apic` binary.
//!
//! Each test runs in its own temporary project directory so they can execute
//! in parallel without interfering. The `EDITOR` is set to `true` (a no-op
//! that exits 0) so `create` never blocks on an interactive editor.

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};

/// A throwaway project directory, removed when the test starts.
fn fresh_dir(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("apic_e2e_{tag}"));
    let _ = fs::remove_dir_all(&dir);
    fs::create_dir_all(&dir).unwrap();
    dir
}

fn apic(dir: &Path) -> Command {
    let mut cmd = Command::cargo_bin("apic").unwrap();
    cmd.current_dir(dir)
        .env("EDITOR", "true")
        .env_remove("VISUAL");
    cmd
}

/// Initializes a project with a `contracts/` working directory.
fn init_project(tag: &str) -> PathBuf {
    let dir = fresh_dir(tag);
    fs::create_dir_all(dir.join("contracts")).unwrap();
    apic(&dir)
        .args(["init", "--set-dir", "contracts"])
        .assert()
        .success();
    dir
}

#[test]
fn init_creates_config_and_refuses_second_init() {
    let dir = init_project("init");
    assert!(dir.join(".apic/config.toml").exists());
    apic(&dir)
        .arg("init")
        .assert()
        .failure()
        .stderr(predicate::str::contains("Already initialized"));
}

#[test]
fn init_seeds_template_file() {
    let dir = init_project("seed_template");
    let template = dir.join(".apic/template.json");
    assert!(template.exists(), "init should seed .apic/template.json");
    let content = fs::read_to_string(&template).unwrap();
    // The built-in default's endpoint name.
    assert!(content.contains("endpoint-name"));
}

#[test]
fn commands_outside_a_project_report_not_initialized() {
    let dir = fresh_dir("noproject");
    apic(&dir)
        .arg("validate")
        .assert()
        .failure()
        .stderr(predicate::str::contains("Not initialized"));
}

#[test]
fn create_scaffolds_then_read_renders_it() {
    let dir = init_project("create_read");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "auth/login.json"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Created"));
    assert!(dir.join("contracts/auth/login.json").exists());

    apic(&dir)
        .args(["read", "-f", "login"])
        .assert()
        .success()
        .stdout(predicate::str::contains("/resource/{id}/action"));
}

#[test]
fn remove_deletes_a_resolved_contract() {
    let dir = init_project("remove");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "auth/login.json"])
        .assert()
        .success();
    assert!(dir.join("contracts/auth/login.json").exists());

    // Non-interactive (no TTY) proceeds without prompting.
    apic(&dir)
        .args(["remove", "-f", "login"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Removed"));
    assert!(!dir.join("contracts/auth/login.json").exists());
}

#[test]
fn remove_reports_when_nothing_matches() {
    let dir = init_project("remove_missing");
    apic(&dir)
        .args(["remove", "-f", "nope"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("No contract found"));
}

#[test]
fn create_refuses_to_overwrite() {
    let dir = init_project("overwrite");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "x.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "x.json"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("already exists"));
}

#[test]
fn create_uses_customized_template() {
    let dir = init_project("custom_template");
    // Overwrite the seeded template with a valid contract that adds a header.
    let custom = r#"{
        "name": "custom",
        "method": "GET",
        "url": { "protocol": "https", "host": "api.example.com", "path": ["x"] },
        "headers": [ { "name": "device-id", "value": "{device_id}" } ],
        "responses": []
    }"#;
    fs::write(dir.join(".apic/template.json"), custom).unwrap();

    apic(&dir)
        .args(["create", "--editor", "true", "-f", "foo.json"])
        .assert()
        .success();

    let created = fs::read_to_string(dir.join("contracts/foo.json")).unwrap();
    assert!(
        created.contains("device-id"),
        "create should use the custom template"
    );
}

#[test]
fn create_fails_when_template_malformed() {
    let dir = init_project("malformed_template");
    let broken = "{ not valid json";
    fs::write(dir.join(".apic/template.json"), broken).unwrap();

    apic(&dir)
        .args(["create", "--editor", "true", "-f", "bar.json"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("is not valid JSON"));

    // No contract is created when the template is invalid...
    assert!(!dir.join("contracts/bar.json").exists());
    // ...and the user's (broken) template is left untouched.
    let template = fs::read_to_string(dir.join(".apic/template.json")).unwrap();
    assert_eq!(template, broken);
}

#[test]
fn create_rejects_path_traversal() {
    let dir = init_project("traversal");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "../../escape.json"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("outside the working directory"));
    assert!(!dir.join("escape.json").exists());
}

#[test]
fn read_unknown_contract_reports_not_found() {
    let dir = init_project("read_missing");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "a.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["read", "-f", "zzz_no_match_zzz"])
        .assert()
        .success()
        .stdout(predicate::str::contains("No contract found"));
}

#[test]
fn validate_passes_for_valid_and_fails_for_broken() {
    let dir = init_project("validate");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "good.json"])
        .assert()
        .success();

    // A valid contract validates and exits 0.
    apic(&dir)
        .arg("validate")
        .assert()
        .success()
        .stdout(predicate::str::contains("ok"))
        .stdout(predicate::str::contains("0 failed"));

    // A malformed contract makes validate exit non-zero.
    fs::write(dir.join("contracts/broken.json"), "{ not json").unwrap();
    apic(&dir)
        .arg("validate")
        .assert()
        .failure()
        .stdout(predicate::str::contains("FAIL"));
}

#[test]
fn validate_template_reports_ok_fail_and_rejects_filename() {
    let dir = init_project("validate_template");

    // The seeded default template is valid: exits 0 with `ok`.
    apic(&dir)
        .args(["validate", "--template"])
        .assert()
        .success()
        .stdout(predicate::str::contains("ok"));

    // A malformed template makes `validate --template` exit non-zero with `FAIL`.
    fs::write(dir.join(".apic/template.json"), "{ not json").unwrap();
    apic(&dir)
        .args(["validate", "--template"])
        .assert()
        .failure()
        .stdout(predicate::str::contains("FAIL"));

    // `--template` and `--find` are mutually exclusive.
    apic(&dir)
        .args(["validate", "--template", "--find", "foo"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("cannot be used with"));
}

#[test]
fn validate_folder_query_checks_every_contract_recursively() {
    let dir = init_project("validate_folder");
    // Two contracts under auth/ (one nested deeper) and one outside it.
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "auth/login.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "auth/sub/refresh.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "user/user.json"])
        .assert()
        .success();

    // A trailing-slash query validates only the contracts under that folder,
    // at any depth — the user/ contract is not counted.
    apic(&dir)
        .args(["validate", "-f", "auth/"])
        .assert()
        .success()
        .stdout(predicate::str::contains("auth/sub/refresh.json"))
        .stdout(predicate::str::contains("2 passed, 0 failed"));

    // A non-existent folder query fails clearly.
    apic(&dir)
        .args(["validate", "-f", "nope/"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("No such folder"));
}

#[test]
fn read_resolves_path_extensionless_and_fuzzy_forms() {
    let dir = init_project("resolve");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "user/user.json"])
        .assert()
        .success();

    // Every form resolves to the same contract.
    for form in ["user/user.json", "user/user", "user.json", "user"] {
        apic(&dir)
            .args(["read", "-f", form])
            .assert()
            .success()
            .stdout(predicate::str::contains("/resource/{id}/action"));
    }
}

#[test]
fn open_resolves_and_succeeds() {
    let dir = init_project("open");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "user/user.json"])
        .assert()
        .success();

    for form in ["user/user.json", "user/user", "user"] {
        apic(&dir)
            .args(["open", "--editor", "true", "-f", form])
            .assert()
            .success();
    }
}

#[test]
fn open_missing_contract_fails() {
    let dir = init_project("open_missing");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "a.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["open", "--editor", "true", "-f", "zzz_no_match_zzz"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("No contract found"));
}

#[test]
fn list_defaults_to_relative_paths() {
    let dir = init_project("list_rel");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "auth/login.json"])
        .assert()
        .success();
    // Default output is relative: the contract path, not the absolute prefix.
    apic(&dir)
        .arg("list")
        .assert()
        .success()
        .stdout(predicate::str::contains("auth/login.json"))
        .stdout(predicate::str::contains(dir.to_string_lossy().to_string()).not());
}

#[test]
fn read_renders_accept_column_for_multipart_file_fields() {
    let dir = init_project("multipart");
    let contract = r#"{
        "name": "upload-avatar",
        "method": "POST",
        "url": { "protocol": "https", "host": "api.example.com", "path": ["user", "avatar"] },
        "headers": [
            { "name": "Content-Type", "value": "multipart/form-data" }
        ],
        "request": {
            "schema": [
                { "name": "avatar", "type": "file", "default": null,
                  "description": "Avatar image", "required": true,
                  "accept": "image/png, image/jpeg" },
                { "name": "caption", "type": "string", "default": null,
                  "description": "Optional caption", "required": false }
            ]
        },
        "responses": []
    }"#;
    fs::write(dir.join("contracts/upload.json"), contract).unwrap();

    apic(&dir)
        .args(["read", "-f", "upload"])
        .assert()
        .success()
        .stdout(predicate::str::contains("ACCEPT"))
        .stdout(predicate::str::contains("image/png, image/jpeg"));

    // Contracts without accept fields keep the four-column table.
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "plain.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["read", "-f", "plain"])
        .assert()
        .success()
        .stdout(predicate::str::contains("ACCEPT").not());
}

#[test]
fn read_example_shows_raw_json_payloads() {
    let dir = init_project("example_view");
    let contract = r#"{
        "name": "login",
        "method": "POST",
        "url": { "protocol": "https", "host": "api.example.com", "path": ["auth", "login"] },
        "headers": [],
        "request": {
            "schema": [
                { "name": "username", "type": "string", "default": null,
                  "description": "Username", "required": true }
            ],
            "example": { "username": "rizukirr", "password": "123qweA@" }
        },
        "responses": [
            { "code": 200, "description": "ok",
              "example": { "status": 200, "message": "welcome" } },
            { "code": 401, "description": "denied",
              "schema": [
                  { "name": "status", "type": "int", "default": null,
                    "description": "Status", "required": true, "properties": null }
              ] }
        ]
    }"#;
    fs::write(dir.join("contracts/login.json"), contract).unwrap();

    // Default view renders the schema table with the example shown beneath
    // it, labeled, so structure and payload stay adjacent.
    apic(&dir)
        .args(["read", "-f", "login"])
        .assert()
        .success()
        .stdout(predicate::str::contains("NAME")) // schema table header
        .stdout(predicate::str::contains("Example:"))
        .stdout(predicate::str::contains("\"password\": \"123qweA@\""))
        // Sections without an example get no note in the default view.
        .stdout(predicate::str::contains("(no example provided)").not());

    // --example shows raw JSON payloads only; a response without one says so.
    apic(&dir)
        .args(["read", "-f", "login", "--example"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\"username\": \"rizukirr\""))
        .stdout(predicate::str::contains("\"message\": \"welcome\""))
        .stdout(predicate::str::contains("(no example provided)"));
}

#[test]
fn read_example_only_contract_renders_example_by_default() {
    let dir = init_project("example_only");
    let contract = r#"{
        "name": "ping",
        "method": "GET",
        "url": { "protocol": "https", "host": "api.example.com", "path": ["ping"] },
        "headers": [],
        "request": { "example": { "probe": true } },
        "responses": [
            { "code": 200, "description": "pong", "example": { "pong": true } }
        ]
    }"#;
    fs::write(dir.join("contracts/ping.json"), contract).unwrap();

    // With no schema at all, the default view falls back to the examples.
    apic(&dir)
        .args(["read", "-f", "ping"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\"probe\": true"))
        .stdout(predicate::str::contains("\"pong\": true"));
}

#[test]
fn list_filter_fuzzy_matches_contracts() {
    let dir = init_project("list_filter");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "user/user.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "auth/login.json"])
        .assert()
        .success();

    // Matching contracts are shown, non-matching ones are not.
    apic(&dir)
        .args(["list", "--filter", "user"])
        .assert()
        .success()
        .stdout(predicate::str::contains("user/user.json"))
        .stdout(predicate::str::contains("login").not());

    // A filter matching nothing prints nothing and still exits 0.
    apic(&dir)
        .args(["list", "--filter", "zzz_no_match"])
        .assert()
        .success()
        .stdout(predicate::str::is_empty());
}

#[test]
fn config_set_dir_rejects_missing_directory() {
    let dir = init_project("setdir");
    apic(&dir)
        .args(["config", "--set-dir", "does-not-exist"])
        .assert()
        .stderr(predicate::str::contains("does not exist"));
}

#[test]
fn version_matches_package_version() {
    let dir = fresh_dir("version");
    apic(&dir)
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains(env!("CARGO_PKG_VERSION")));
}

#[test]
fn read_ambiguous_basename_errors_when_not_a_tty() {
    let dir = init_project("ambiguous_read");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "user/user.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "auth/user.json"])
        .assert()
        .success();

    // Test stdin/stdout are pipes, not TTYs, so the picker must not run:
    // the command exits non-zero and lists every candidate.
    apic(&dir)
        .args(["read", "-f", "user"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("is ambiguous"))
        .stderr(predicate::str::contains("user/user.json"))
        .stderr(predicate::str::contains("auth/user.json"))
        .stderr(predicate::str::contains("Specify the path"));

    // A precise path still resolves without any prompt.
    apic(&dir)
        .args(["read", "-f", "auth/user.json"])
        .assert()
        .success()
        .stdout(predicate::str::contains("/resource/{id}/action"));
}

#[test]
fn open_ambiguous_basename_errors_when_not_a_tty() {
    let dir = init_project("ambiguous_open");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "user/user.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "auth/user.json"])
        .assert()
        .success();

    apic(&dir)
        .args(["open", "--editor", "true", "-f", "user"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("is ambiguous"));
}

#[test]
fn validate_ambiguous_basename_errors_when_not_a_tty() {
    let dir = init_project("ambiguous_validate");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "user/user.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "auth/user.json"])
        .assert()
        .success();

    apic(&dir)
        .args(["validate", "-f", "user"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("is ambiguous"));
}

#[test]
fn read_fuzzy_score_tie_errors_when_not_a_tty() {
    let dir = init_project("fuzzy_tie");
    // Different basenames, identical structure: "usr" is not a basename
    // match for either, and both fuzzy-score identically -> ambiguous.
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "a/user-a.json"])
        .assert()
        .success();
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "b/user-b.json"])
        .assert()
        .success();

    apic(&dir)
        .args(["read", "-f", "usr"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("is ambiguous"))
        .stderr(predicate::str::contains("user-a.json"))
        .stderr(predicate::str::contains("user-b.json"));
}

#[test]
fn list_piped_output_stays_flat_without_tree_chars() {
    let dir = init_project("list_piped_flat");
    apic(&dir)
        .args(["create", "--editor", "true", "-f", "user/profile/user.json"])
        .assert()
        .success();

    // assert_cmd captures stdout through a pipe (not a TTY), so this pins the
    // scriptable contract: one path per line, no box-drawing characters.
    apic(&dir)
        .arg("list")
        .assert()
        .success()
        .stdout(predicate::str::contains("user/profile/user.json"))
        .stdout(predicate::str::contains("├──").not())
        .stdout(predicate::str::contains("└──").not());

    // Same for --absolute: flat absolute paths, no tree. The tool emits
    // canonicalized, forward-slashed paths; on Windows that long-name/verbatim
    // form differs from `dir`'s raw string, so assert on the stable project-dir
    // leaf and the contract tail rather than the full temp-dir prefix.
    apic(&dir)
        .args(["list", "--absolute", "true"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            dir.file_name().unwrap().to_string_lossy().to_string(),
        ))
        .stdout(predicate::str::contains("user/profile/user.json"))
        .stdout(predicate::str::contains("├──").not());
}

#[test]
fn list_filter_does_not_match_across_path_components() {
    let dir = init_project("list_filter_component");
    for f in ["user/user.json", "user/upload.json", "auth/user.json"] {
        apic(&dir)
            .args(["create", "--editor", "true", "-f", f])
            .assert()
            .success();
    }

    // "user.json" must not match user/upload.json by borrowing "user" from
    // the directory and ".json" from the extension.
    apic(&dir)
        .args(["list", "--filter", "user.json"])
        .assert()
        .success()
        .stdout(predicate::str::contains("user/user.json"))
        .stdout(predicate::str::contains("auth/user.json"))
        .stdout(predicate::str::contains("upload.json").not());
}