rusty-fmp 0.5.3

JSON CLI and client library for Financial Modeling Prep
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
use assert_cmd::Command;
use serde_json::Value;

/// Parse schema output from the binary.
fn schema_body() -> Value {
    let output = Command::cargo_bin("fmp-agent")
        .unwrap()
        .env("FMP_API_KEY", "test_only_not_used_by_schema")
        .arg("schema")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    serde_json::from_slice(&output).expect("schema output should be valid JSON")
}

#[test]
fn schema_version_is_3() {
    let body = schema_body();
    assert_eq!(body["schema_version"], 3, "schema_version must be 3");
    assert_eq!(body["binary"], "fmp-agent");

    let version = env!("CARGO_PKG_VERSION");
    assert_eq!(body["version"], version);
}

#[test]
fn schema_has_groups_in_order() {
    let body = schema_body();
    let groups = body["groups"].as_array().expect("groups must be an array");
    let expected = [
        "help",
        "company",
        "market",
        "fundamentals",
        "analyst",
        "insider",
        "calendar",
        "macro",
        "technical",
        "sec",
        "etf",
        "crypto",
        "forex",
        "news",
    ];
    let names: Vec<&str> = groups.iter().filter_map(|g| g.as_str()).collect();
    assert_eq!(names, expected, "groups must list names in declared order");
}

#[test]
fn schema_batch_quote_multi_value_arg() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();

    let bq = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["market", "batch-quote"]))
        .expect("must have market batch-quote leaf");

    assert_eq!(bq["name"], "batch-quote");
    assert_eq!(bq["api_key_required"], true);
    assert_eq!(bq["preferred_path"], "market batch-quote");
    assert!(bq["aliases"].as_array().unwrap().is_empty());
    assert!(bq["about"].is_string(), "about must be a string");
    assert!(
        bq["long_about"]
            .as_str()
            .is_some_and(|s| s.contains("Examples:")),
        "long_about must contain examples"
    );

    let symbols_arg = bq["args"]
        .as_array()
        .unwrap()
        .iter()
        .find(|a| a["name"] == "symbols")
        .expect("batch-quote must have symbols arg");

    assert_eq!(symbols_arg["kind"], "positional");
    assert_eq!(symbols_arg["required"], true);
    assert_eq!(symbols_arg["parser"]["hint"], "string");
    assert_eq!(symbols_arg["multi_value"], true);
}

#[test]
fn schema_price_change_multi_value_arg() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();

    let price_change = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["market", "price-change"]))
        .expect("must have market price-change leaf");

    assert_eq!(price_change["name"], "price-change");
    assert_eq!(price_change["api_key_required"], true);
    assert_eq!(price_change["preferred_path"], "market price-change");
    assert!(price_change["aliases"].as_array().unwrap().is_empty());

    let symbols_arg = price_change["args"]
        .as_array()
        .unwrap()
        .iter()
        .find(|a| a["name"] == "symbols")
        .expect("price-change must have symbols arg");

    assert_eq!(symbols_arg["kind"], "positional");
    assert_eq!(symbols_arg["required"], true);
    assert_eq!(symbols_arg["parser"]["hint"], "string");
    assert_eq!(symbols_arg["multi_value"], true);
}

#[test]
fn schema_market_quote_leaf() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();
    let mq = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["market", "quote"]))
        .expect("must have market quote leaf");

    assert_eq!(mq["name"], "quote");
    assert_eq!(mq["api_key_required"], true);
    assert_eq!(mq["preferred_path"], "market quote");
    assert_eq!(mq["aliases"], serde_json::json!(["quote"]));
    assert!(mq["about"].is_string(), "about must be a string");
}

#[test]
fn schema_leaf_is_not_api_key_required() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();

    // Metadata-only commands that do not need FMP_API_KEY
    for &name in &["schema", "commands", "completions", "doctor"] {
        let cmd = commands
            .iter()
            .find(|c| c["name"] == name && c["path"] == serde_json::json!([name]))
            .unwrap_or_else(|| panic!("must have {name} leaf"));

        assert_eq!(
            cmd["api_key_required"], false,
            "{name} should not require an API key"
        );
        assert_eq!(cmd["preferred_path"], name);
    }
}

#[test]
fn schema_alias_quote_has_preferred_path() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();
    let alias = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["quote"]))
        .expect("must have top-level quote alias");

    assert_eq!(alias["preferred_path"], "market quote");
    assert_eq!(alias["aliases"], serde_json::json!([]));
    assert_eq!(alias["api_key_required"], true);
}

#[test]
fn schema_canonical_has_alias_backref() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();

    // company profile has alias "profile"
    let cp = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["company", "profile"]))
        .expect("must have company profile leaf");
    assert_eq!(cp["aliases"], serde_json::json!(["profile"]));

    // calendar earnings has alias "earnings"
    let ce = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["calendar", "earnings"]))
        .expect("must have calendar earnings leaf");
    assert_eq!(ce["aliases"], serde_json::json!(["earnings"]));

    // market historical has alias "historical"
    let mh = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["market", "historical"]))
        .expect("must have market historical leaf");
    assert_eq!(mh["aliases"], serde_json::json!(["historical"]));
}

#[test]
fn schema_positional_arg_has_metadata() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();

    // market quote has a positional "symbol" arg
    let mq = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["market", "quote"]))
        .expect("must have market quote leaf");

    let symbol_arg = mq["args"]
        .as_array()
        .unwrap()
        .iter()
        .find(|a| a["name"] == "symbol")
        .expect("market quote must have symbol arg");

    // Existing fields
    assert_eq!(symbol_arg["kind"], "positional");
    assert_eq!(symbol_arg["value_name"], "SYMBOL");
    assert_eq!(symbol_arg["required"], true);

    // New v3 fields
    assert!(
        symbol_arg["long"].is_null(),
        "positional arg should not have long flag"
    );
    assert!(
        symbol_arg["short"].is_null(),
        "positional arg should not have short flag"
    );
    assert_eq!(symbol_arg["parser"]["hint"], "string");
    assert!(
        symbol_arg["possible_values"].is_null(),
        "positional arg should not have possible values"
    );
    assert_eq!(symbol_arg["multi_value"], false);
}

#[test]
fn schema_commands_have_args() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();

    // At least one command has a positional arg
    let has_positional = commands.iter().any(|c| {
        c["args"]
            .as_array()
            .into_iter()
            .flatten()
            .any(|a| a["kind"] == "positional")
    });
    assert!(
        has_positional,
        "at least one command should have a positional arg"
    );

    // At least one command has an option arg
    let has_option = commands.iter().any(|c| {
        c["args"]
            .as_array()
            .into_iter()
            .flatten()
            .any(|a| a["kind"] == "option")
    });
    assert!(has_option, "at least one command should have an option arg");

    // At least one long_about contains Examples:
    let has_examples = commands.iter().any(|c| {
        c["long_about"]
            .as_str()
            .is_some_and(|s| s.contains("Examples:"))
    });
    assert!(
        has_examples,
        "at least one long_about must contain Examples:"
    );
}

#[test]
fn schema_works_without_api_key() {
    let output = Command::cargo_bin("fmp-agent")
        .unwrap()
        .env_remove("FMP_API_KEY")
        .arg("schema")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let body: Value =
        serde_json::from_slice(&output).expect("schema output without key should be valid JSON");
    assert_eq!(body["schema_version"], 3);
}

#[test]
fn schema_option_with_default_has_long_and_integer_hint() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();

    // technical sma has --period-length with default 10
    let sma = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["technical", "sma"]))
        .expect("must have technical sma leaf");

    let period_arg = sma["args"]
        .as_array()
        .unwrap()
        .iter()
        .find(|a| a["name"] == "period_length")
        .expect("technical sma must have period_length arg");

    assert_eq!(period_arg["kind"], "option");
    assert_eq!(period_arg["long"], "period-length");
    assert!(period_arg["short"].is_null());
    assert_eq!(period_arg["default"], 10);
    assert_eq!(period_arg["parser"]["hint"], "integer");
    assert!(period_arg["possible_values"].is_null());
    assert_eq!(period_arg["multi_value"], false);
}

#[test]
fn schema_enum_arg_has_possible_values() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();

    // completions has a shell arg with value_enum
    let completions = commands
        .iter()
        .find(|c| c["name"] == "completions")
        .expect("must have completions leaf");

    let shell_arg = completions["args"]
        .as_array()
        .unwrap()
        .iter()
        .find(|a| a["name"] == "shell")
        .expect("completions must have shell arg");

    assert_eq!(shell_arg["kind"], "positional");
    assert_eq!(shell_arg["required"], true);
    assert_eq!(shell_arg["parser"]["hint"], "enum");

    let possible = shell_arg["possible_values"]
        .as_array()
        .expect("shell arg must have possible_values array");
    assert!(!possible.is_empty(), "shell should have possible values");

    // Each possible value has name and help
    for pv in possible {
        assert!(
            pv["name"].is_string(),
            "each possible value must have a name"
        );
    }

    for &topic in &["environment", "exit-codes", "schema", "examples"] {
        let cmd = commands
            .iter()
            .find(|c| c["path"] == serde_json::json!(["help", topic]))
            .unwrap_or_else(|| panic!("must have help {topic} leaf"));

        assert_eq!(
            cmd["api_key_required"], false,
            "help {topic} should not require an API key"
        );
    }
}

#[test]
fn schema_option_has_short_flag_when_present() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();

    // commands --grouped uses a long flag (bool)
    let cmd = commands
        .iter()
        .find(|c| c["name"] == "commands")
        .expect("must have commands leaf");

    let grouped_arg = cmd["args"]
        .as_array()
        .unwrap()
        .iter()
        .find(|a| a["name"] == "grouped")
        .expect("commands must have grouped arg");

    assert_eq!(grouped_arg["kind"], "option");
    assert_eq!(grouped_arg["long"], "grouped");
    assert!(grouped_arg["short"].is_null());
    assert_eq!(grouped_arg["parser"]["hint"], "bool");
}

#[test]
fn schema_args_long_flag_matches_clap_spelling() {
    let body = schema_body();
    let commands = body["commands"].as_array().unwrap();

    // fundamentals income-statement has --period with enum values and --limit with default 5
    let income = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["fundamentals", "income-statement"]))
        .expect("must have income statement leaf");

    let limit_arg = income["args"]
        .as_array()
        .unwrap()
        .iter()
        .find(|a| a["name"] == "limit")
        .expect("income statement must have limit arg");

    assert_eq!(limit_arg["long"], "limit");
    assert_eq!(limit_arg["default"], 5);

    let period_arg = income["args"]
        .as_array()
        .unwrap()
        .iter()
        .find(|a| a["name"] == "period")
        .expect("income statement must have period arg");

    assert_eq!(period_arg["long"], "period");
    assert_eq!(period_arg["default"], "annual");
    assert_eq!(period_arg["parser"]["hint"], "enum");
    assert_eq!(
        period_arg["possible_values"],
        serde_json::json!([
            { "name": "annual", "help": "Annual fiscal period rows" },
            { "name": "quarter", "help": "Quarterly fiscal period rows" }
        ])
    );

    let ratios = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["fundamentals", "ratios"]))
        .expect("must have ratios leaf");

    assert!(
        ratios["args"]
            .as_array()
            .unwrap()
            .iter()
            .all(|a| a["name"] != "period"),
        "unconfirmed annual fundamentals must not expose --period"
    );

    // sec filings has --from and --to
    let filings = commands
        .iter()
        .find(|c| c["path"] == serde_json::json!(["sec", "filings"]))
        .expect("must have sec filings leaf");

    let from_arg = filings["args"]
        .as_array()
        .unwrap()
        .iter()
        .find(|a| a["name"] == "from")
        .expect("sec filings must have from arg");

    assert_eq!(from_arg["long"], "from");
    assert!(from_arg["short"].is_null());
    assert_eq!(from_arg["required"], false);
}

#[test]
fn other_commands_still_require_api_key() {
    // dotenvy may load a valid key from .env, so set it to empty to
    // confirm non-discovery commands still require a populated key.
    Command::cargo_bin("fmp-agent")
        .unwrap()
        .env("FMP_API_KEY", "")
        .args(["market", "quote", "AAPL"])
        .assert()
        .failure()
        .code(3);
}