aperture-cli 0.1.9

Dynamic CLI generator for OpenAPI specifications
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
//! Tests for command search functionality
#![allow(clippy::needless_collect)]

use aperture_cli::cache::models::{
    CachedCommand, CachedParameter, CachedSpec, PaginationInfo, CACHE_FORMAT_VERSION,
};
use aperture_cli::search::{format_search_results, CommandSearcher};
use std::collections::{BTreeMap, HashMap};

#[allow(clippy::too_many_lines)]
fn create_test_spec(name: &str) -> CachedSpec {
    CachedSpec {
        cache_format_version: CACHE_FORMAT_VERSION,
        name: name.to_string(),
        version: "1.0.0".to_string(),
        base_url: Some("https://api.example.com".to_string()),
        commands: vec![
            CachedCommand {
                operation_id: "getUser".to_string(),
                method: "GET".to_string(),
                path: "/users/{id}".to_string(),
                summary: Some("Get a user by ID".to_string()),
                description: Some("Retrieves detailed user information".to_string()),
                tags: vec!["users".to_string()],
                name: "users".to_string(),
                parameters: vec![CachedParameter {
                    name: "id".to_string(),
                    location: "path".to_string(),
                    required: true,
                    description: Some("User ID".to_string()),
                    schema: None,
                    schema_type: Some("string".to_string()),
                    format: None,
                    default_value: None,
                    enum_values: vec![],
                    example: None,
                }],
                request_body: None,
                responses: vec![],
                security_requirements: vec![],
                deprecated: false,
                external_docs_url: None,
                examples: vec![],
                display_group: None,
                display_name: None,
                aliases: vec![],
                hidden: false,
                pagination: PaginationInfo::default(),
            },
            CachedCommand {
                operation_id: "listUsers".to_string(),
                method: "GET".to_string(),
                path: "/users".to_string(),
                summary: Some("List all users".to_string()),
                description: Some("Returns a paginated list of users".to_string()),
                tags: vec!["users".to_string()],
                name: "users".to_string(),
                parameters: vec![],
                request_body: None,
                responses: vec![],
                security_requirements: vec![],
                deprecated: false,
                external_docs_url: None,
                examples: vec![],
                display_group: None,
                display_name: None,
                aliases: vec![],
                hidden: false,
                pagination: PaginationInfo::default(),
            },
            CachedCommand {
                operation_id: "createUser".to_string(),
                method: "POST".to_string(),
                path: "/users".to_string(),
                summary: Some("Create a new user".to_string()),
                description: Some("Creates a new user account".to_string()),
                tags: vec!["users".to_string()],
                name: "users".to_string(),
                parameters: vec![],
                request_body: None,
                responses: vec![],
                security_requirements: vec![],
                deprecated: false,
                external_docs_url: None,
                examples: vec![],
                display_group: None,
                display_name: None,
                aliases: vec![],
                hidden: false,
                pagination: PaginationInfo::default(),
            },
            CachedCommand {
                operation_id: "getIssue".to_string(),
                method: "GET".to_string(),
                path: "/issues/{id}".to_string(),
                summary: Some("Get an issue by ID".to_string()),
                description: Some("Retrieves issue details".to_string()),
                tags: vec!["issues".to_string()],
                name: "issues".to_string(),
                parameters: vec![],
                request_body: None,
                responses: vec![],
                security_requirements: vec![],
                deprecated: false,
                external_docs_url: None,
                examples: vec![],
                display_group: None,
                display_name: None,
                aliases: vec![],
                hidden: false,
                pagination: PaginationInfo::default(),
            },
        ],
        servers: vec![],
        security_schemes: HashMap::new(),
        skipped_endpoints: vec![],
        server_variables: HashMap::new(),
    }
}

#[test]
fn test_search_by_operation_id() {
    let searcher = CommandSearcher::new();
    let mut specs = BTreeMap::new();
    let spec = create_test_spec("test-api");

    // Debug: Print command operation IDs to verify the test data
    for cmd in &spec.commands {
        eprintln!(
            "Command: operation_id={}, method={}, path={}",
            cmd.operation_id, cmd.method, cmd.path
        );
    }

    specs.insert("test-api".to_string(), spec);

    let results = searcher.search(&specs, "getUser", None).unwrap();

    eprintln!("Search for 'getUser' found {} results", results.len());
    for result in &results {
        eprintln!(
            "  - {} (score: {})",
            result.command.operation_id, result.score
        );
    }

    assert_eq!(results.len(), 1);
    assert_eq!(results[0].command.operation_id, "getUser");
    assert_eq!(results[0].api_context, "test-api");
}

#[test]
fn test_search_by_method() {
    let searcher = CommandSearcher::new();
    let mut specs = BTreeMap::new();
    specs.insert("test-api".to_string(), create_test_spec("test-api"));

    let results = searcher.search(&specs, "POST", None).unwrap();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0].command.method, "POST");
}

#[test]
fn test_search_by_keyword_in_summary() {
    let searcher = CommandSearcher::new();
    let mut specs = BTreeMap::new();
    specs.insert("test-api".to_string(), create_test_spec("test-api"));

    let results = searcher.search(&specs, "paginated", None).unwrap();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0].command.operation_id, "listUsers");
}

#[test]
fn test_search_with_api_filter() {
    let searcher = CommandSearcher::new();
    let mut specs = BTreeMap::new();
    specs.insert("api1".to_string(), create_test_spec("api1"));
    specs.insert("api2".to_string(), create_test_spec("api2"));

    // Search without filter should return results from both APIs
    let results = searcher.search(&specs, "user", None).unwrap();
    assert!(results.len() > 3); // Should find user operations in both APIs

    // Search with filter should only return from specified API
    let results = searcher.search(&specs, "user", Some("api1")).unwrap();
    assert!(results.iter().all(|r| r.api_context == "api1"));
}

#[test]
fn test_fuzzy_search() {
    let searcher = CommandSearcher::new();
    let mut specs = BTreeMap::new();
    specs.insert("test-api".to_string(), create_test_spec("test-api"));

    // Test partial match - searching for "user" should find user-related operations
    let results = searcher.search(&specs, "user", None).unwrap();
    assert!(
        !results.is_empty(),
        "Fuzzy search should find results for partial matches"
    );

    // Should find multiple user operations
    let user_ops: Vec<_> = results
        .iter()
        .filter(|r| r.command.operation_id.to_lowercase().contains("user"))
        .collect();
    assert!(
        user_ops.len() >= 2,
        "Should find at least 2 user operations"
    );

    // Test that exact matches score higher than partial matches
    let exact_results = searcher.search(&specs, "getUser", None).unwrap();
    assert!(!exact_results.is_empty());
    assert_eq!(exact_results[0].command.operation_id, "getUser");

    // Verify fuzzy matching works with similar terms
    let list_results = searcher.search(&specs, "list", None).unwrap();
    assert!(!list_results.is_empty(), "Should find list operations");
}

#[test]
fn test_regex_search() {
    let searcher = CommandSearcher::new();
    let mut specs = BTreeMap::new();
    specs.insert("test-api".to_string(), create_test_spec("test-api"));

    // Regex pattern to find all "get" operations
    let results = searcher.search(&specs, r"get\w+", None).unwrap();

    assert_eq!(results.len(), 2); // getUser and getIssue
    assert!(results
        .iter()
        .all(|r| r.command.operation_id.starts_with("get")));
}

#[test]
fn test_find_similar_commands() {
    let searcher = CommandSearcher::new();
    let spec = create_test_spec("test");

    // Find similar to a typo
    let suggestions = searcher.find_similar_commands(&spec, "usr get-usr", 3);

    assert!(!suggestions.is_empty());
    // Should suggest "users get-user" as the closest match
    assert!(suggestions[0].0.contains("get-user"));
}

#[test]
fn test_format_search_results() {
    let searcher = CommandSearcher::new();
    let mut specs = BTreeMap::new();
    specs.insert("test-api".to_string(), create_test_spec("test-api"));

    let results = searcher.search(&specs, "getUser", None).unwrap();

    // Test non-verbose output
    let output = format_search_results(&results, false);
    assert!(output
        .iter()
        .any(|line| line.contains("aperture api test-api")));
    assert!(output.iter().any(|line| line.contains("GET /users/{id}")));

    // Test verbose output
    let output = format_search_results(&results, true);
    assert!(output.iter().any(|line| line.contains("Parameters:")));
}

#[test]
fn test_empty_search_results() {
    let results = vec![];
    let output = format_search_results(&results, false);

    assert_eq!(output.len(), 1);
    assert_eq!(output[0], "No matching operations found.");
}

#[test]
fn test_search_scoring() {
    let searcher = CommandSearcher::new();
    let mut specs = BTreeMap::new();
    specs.insert("test-api".to_string(), create_test_spec("test-api"));

    // Search for "user" should rank operations with "user" in different places
    let results = searcher.search(&specs, "user", None).unwrap();

    assert!(!results.is_empty());
    // Results should be sorted by score (highest first)
    for i in 1..results.len() {
        assert!(results[i - 1].score >= results[i].score);
    }
}

#[test]
fn test_search_finds_by_display_name() {
    let spec = CachedSpec {
        cache_format_version: CACHE_FORMAT_VERSION,
        name: "mapped-api".to_string(),
        version: "1.0.0".to_string(),
        commands: vec![CachedCommand {
            operation_id: "getUserById".to_string(),
            name: "users".to_string(),
            description: Some("Fetch a user".to_string()),
            summary: None,
            method: "GET".to_string(),
            path: "/users/{id}".to_string(),
            parameters: vec![],
            request_body: None,
            responses: vec![],
            security_requirements: vec![],
            tags: vec!["users".to_string()],
            deprecated: false,
            external_docs_url: None,
            examples: vec![],
            display_group: None,
            display_name: Some("fetch".to_string()),
            aliases: vec![],
            hidden: false,
            pagination: PaginationInfo::default(),
        }],
        base_url: Some("https://api.example.com".to_string()),
        servers: vec![],
        security_schemes: HashMap::new(),
        skipped_endpoints: vec![],
        server_variables: HashMap::new(),
    };

    let searcher = CommandSearcher::new();
    let mut specs = BTreeMap::new();
    specs.insert("mapped-api".to_string(), spec);

    // Searching by display name "fetch" should find the command
    let results = searcher.search(&specs, "fetch", None).unwrap();
    assert!(!results.is_empty(), "Should find command by display name");
    assert_eq!(results[0].command.operation_id, "getUserById");

    // The command path should use the display name
    assert!(
        results[0].command_path.contains("fetch"),
        "Command path should use display name, got: {}",
        results[0].command_path
    );
}

#[test]
fn test_search_finds_by_alias() {
    let spec = CachedSpec {
        cache_format_version: CACHE_FORMAT_VERSION,
        name: "mapped-api".to_string(),
        version: "1.0.0".to_string(),
        commands: vec![CachedCommand {
            operation_id: "getUserById".to_string(),
            name: "users".to_string(),
            description: Some("Fetch a user".to_string()),
            summary: None,
            method: "GET".to_string(),
            path: "/users/{id}".to_string(),
            parameters: vec![],
            request_body: None,
            responses: vec![],
            security_requirements: vec![],
            tags: vec!["users".to_string()],
            deprecated: false,
            external_docs_url: None,
            examples: vec![],
            display_group: None,
            display_name: None,
            aliases: vec!["lookup".to_string()],
            hidden: false,
            pagination: PaginationInfo::default(),
        }],
        base_url: Some("https://api.example.com".to_string()),
        servers: vec![],
        security_schemes: HashMap::new(),
        skipped_endpoints: vec![],
        server_variables: HashMap::new(),
    };

    let searcher = CommandSearcher::new();
    let mut specs = BTreeMap::new();
    specs.insert("mapped-api".to_string(), spec);

    // Searching by alias "lookup" should find the command
    let results = searcher.search(&specs, "lookup", None).unwrap();
    assert!(!results.is_empty(), "Should find command by alias");
    assert_eq!(results[0].command.operation_id, "getUserById");
}

#[test]
fn test_search_suggestions_include_display_names() {
    let spec = CachedSpec {
        cache_format_version: CACHE_FORMAT_VERSION,
        name: "mapped-api".to_string(),
        version: "1.0.0".to_string(),
        commands: vec![CachedCommand {
            operation_id: "getUserById".to_string(),
            name: "users".to_string(),
            description: Some("Fetch a user".to_string()),
            summary: None,
            method: "GET".to_string(),
            path: "/users/{id}".to_string(),
            parameters: vec![],
            request_body: None,
            responses: vec![],
            security_requirements: vec![],
            tags: vec!["users".to_string()],
            deprecated: false,
            external_docs_url: None,
            examples: vec![],
            display_group: Some("accounts".to_string()),
            display_name: Some("fetch".to_string()),
            aliases: vec!["show".to_string()],
            hidden: false,
            pagination: PaginationInfo::default(),
        }],
        base_url: Some("https://api.example.com".to_string()),
        servers: vec![],
        security_schemes: HashMap::new(),
        skipped_endpoints: vec![],
        server_variables: HashMap::new(),
    };

    let searcher = CommandSearcher::new();

    // Suggestions should use the effective command path with display names
    let suggestions = searcher.find_similar_commands(&spec, "fetc", 5);
    assert!(
        !suggestions.is_empty(),
        "Should suggest command by display name prefix"
    );
    let (suggestion, _score) = &suggestions[0];
    assert!(
        suggestion.contains("accounts") && suggestion.contains("fetch"),
        "Suggestion should use display group and name, got: {suggestions:?}"
    );
}