openrouter_api 0.6.0

A Rust client library for the OpenRouter API
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
//! Targeted tests for the Key Info endpoint and types.
//!
//! Current coverage: 4 serde tests in types/key_info.rs + 1 constructor test in api/key_info.rs.
//! Gaps addressed here:
//! - KeyInfoApi URL construction (verifiable without network)
//! - KeyInfoResponse convenience method correctness under all combinations
//! - RateLimitInfo edge cases (None fields, zero requests, unusual intervals)
//! - Unlimited credit key (limit: null)
//! - KeyInfoData partial field presence (real API often omits optional fields)
//! - Wiremock-backed integration: correct HTTP method (GET), correct path, auth header present

#[cfg(test)]
mod tests {
    use crate::client::ClientConfig;
    use crate::tests::test_helpers::test_client_config;
    use crate::types::key_info::{KeyInfoData, KeyInfoResponse, RateLimitInfo};
    use url::Url;

    // =========================================================================
    // KeyInfoResponse — convenience methods
    // =========================================================================

    #[test]
    fn test_limit_remaining_is_none_when_field_absent() {
        let r = KeyInfoResponse {
            data: KeyInfoData {
                label: None,
                limit: None,
                limit_remaining: None,
                usage: None,
                is_free_tier: None,
                rate_limit: None,
            },
        };
        assert!(r.limit_remaining().is_none());
    }

    #[test]
    fn test_limit_remaining_is_some_when_set() {
        let r = KeyInfoResponse {
            data: KeyInfoData {
                label: None,
                limit: Some(100.0),
                limit_remaining: Some(42.5),
                usage: None,
                is_free_tier: None,
                rate_limit: None,
            },
        };
        assert_eq!(r.limit_remaining(), Some(42.5));
    }

    #[test]
    fn test_limit_is_none_for_unlimited_key() {
        // OpenRouter represents unlimited credit as null/absent limit.
        let json = r#"{"data": {"usage": 5.0}}"#;
        let r: KeyInfoResponse = serde_json::from_str(json).unwrap();
        assert!(r.limit().is_none(), "null limit must map to None");
    }

    #[test]
    fn test_limit_remaining_zero_is_some_not_none() {
        // A depleted key returns 0.0 — this must be Some(0.0), not None.
        let json = r#"{"data": {"limit": 10.0, "limit_remaining": 0.0}}"#;
        let r: KeyInfoResponse = serde_json::from_str(json).unwrap();
        assert_eq!(
            r.limit_remaining(),
            Some(0.0),
            "limit_remaining of 0.0 must be Some(0.0)"
        );
    }

    #[test]
    fn test_usage_zero_is_some_not_none() {
        let json = r#"{"data": {"usage": 0.0}}"#;
        let r: KeyInfoResponse = serde_json::from_str(json).unwrap();
        assert_eq!(r.usage(), Some(0.0));
    }

    #[test]
    fn test_is_free_tier_false_when_field_absent() {
        // is_free_tier: None must default to false via unwrap_or(false).
        let r = KeyInfoResponse {
            data: KeyInfoData {
                label: None,
                limit: None,
                limit_remaining: None,
                usage: None,
                is_free_tier: None,
                rate_limit: None,
            },
        };
        assert!(!r.is_free_tier());
    }

    #[test]
    fn test_is_free_tier_true_when_set() {
        let json = r#"{"data": {"is_free_tier": true}}"#;
        let r: KeyInfoResponse = serde_json::from_str(json).unwrap();
        assert!(r.is_free_tier());
    }

    #[test]
    fn test_data_accessor_returns_same_as_direct_field() {
        let r = KeyInfoResponse {
            data: KeyInfoData {
                label: Some("test".to_string()),
                limit: Some(50.0),
                limit_remaining: Some(25.0),
                usage: Some(25.0),
                is_free_tier: Some(false),
                rate_limit: None,
            },
        };
        // Direct field access works correctly.
        assert_eq!(r.data.label, Some("test".to_string()));
        assert_eq!(r.data.limit, Some(50.0));
    }

    // =========================================================================
    // RateLimitInfo edge cases
    // =========================================================================

    #[test]
    fn test_rate_limit_all_fields_none() {
        let json = r#"{"data": {"rate_limit": {}}}"#;
        let r: KeyInfoResponse = serde_json::from_str(json).unwrap();
        let rl = r.data.rate_limit.unwrap();
        assert!(rl.requests.is_none());
        assert!(rl.interval.is_none());
    }

    #[test]
    fn test_rate_limit_various_intervals() {
        for interval in &["10s", "1m", "1h", "1d"] {
            let json = format!(
                r#"{{"data": {{"rate_limit": {{"requests": 100, "interval": "{interval}"}} }} }}"#
            );
            let r: KeyInfoResponse = serde_json::from_str(&json).unwrap();
            let rl = r.data.rate_limit.unwrap();
            assert_eq!(rl.interval.as_deref(), Some(*interval));
        }
    }

    #[test]
    fn test_rate_limit_zero_requests() {
        let json = r#"{"data": {"rate_limit": {"requests": 0, "interval": "10s"}}}"#;
        let r: KeyInfoResponse = serde_json::from_str(json).unwrap();
        let rl = r.data.rate_limit.unwrap();
        assert_eq!(rl.requests, Some(0));
    }

    #[test]
    fn test_rate_limit_high_request_count() {
        let json = r#"{"data": {"rate_limit": {"requests": 1000000, "interval": "1d"}}}"#;
        let r: KeyInfoResponse = serde_json::from_str(json).unwrap();
        let rl = r.data.rate_limit.unwrap();
        assert_eq!(rl.requests, Some(1_000_000));
    }

    #[test]
    fn test_rate_limit_info_equality() {
        let a = RateLimitInfo {
            requests: Some(200),
            interval: Some("10s".to_string()),
        };
        let b = RateLimitInfo {
            requests: Some(200),
            interval: Some("10s".to_string()),
        };
        assert_eq!(a, b);
    }

    // =========================================================================
    // Deserialization — realistic partial responses (OpenRouter often omits fields)
    // =========================================================================

    #[test]
    fn test_only_usage_field_present() {
        let json = r#"{"data": {"usage": 12.75}}"#;
        let r: KeyInfoResponse = serde_json::from_str(json).unwrap();
        assert_eq!(r.usage(), Some(12.75));
        assert!(r.limit().is_none());
        assert!(r.limit_remaining().is_none());
        assert!(!r.is_free_tier());
    }

    #[test]
    fn test_label_only_response() {
        let json = r#"{"data": {"label": "production-key"}}"#;
        let r: KeyInfoResponse = serde_json::from_str(json).unwrap();
        assert_eq!(r.data.label.as_deref(), Some("production-key"));
        assert!(r.limit().is_none());
    }

    #[test]
    fn test_key_info_response_equality_derived() {
        let a = KeyInfoResponse {
            data: KeyInfoData {
                label: Some("key".to_string()),
                limit: Some(100.0),
                limit_remaining: Some(50.0),
                usage: Some(50.0),
                is_free_tier: Some(false),
                rate_limit: Some(RateLimitInfo {
                    requests: Some(100),
                    interval: Some("10s".to_string()),
                }),
            },
        };
        let b = a.clone();
        assert_eq!(a, b);
    }

    // =========================================================================
    // KeyInfoApi — constructor creates headers correctly
    // =========================================================================

    #[test]
    fn test_key_info_api_has_authorization_header() {
        use crate::api::key_info::KeyInfoApi;

        let config = test_client_config();
        let client = reqwest::Client::new();
        let api = KeyInfoApi::new(client, &config).unwrap();
        assert!(
            api.config.headers.contains_key("authorization"),
            "KeyInfoApi must inject Authorization header"
        );
    }

    #[test]
    fn test_key_info_api_base_url_resolves_correct_path() {
        use crate::api::key_info::KeyInfoApi;

        let config = test_client_config();
        let client = reqwest::Client::new();
        let api = KeyInfoApi::new(client, &config).unwrap();

        // Verify that the base_url joined with "auth/key" gives the expected path.
        let url = api.config.base_url.join("auth/key").unwrap();
        assert!(
            url.path().ends_with("/auth/key"),
            "Expected path ending with /auth/key, got: {}",
            url.path()
        );
    }

    // =========================================================================
    // Wiremock integration: GET /auth/key with auth header, returns 200 JSON
    // =========================================================================

    #[tokio::test]
    async fn test_get_key_info_wiremock_happy_path() {
        use crate::api::key_info::KeyInfoApi;
        use wiremock::{matchers, Mock, MockServer, ResponseTemplate};

        let mock_server = MockServer::start().await;

        let body = serde_json::json!({
            "data": {
                "label": "test-key",
                "limit": 100.0,
                "limit_remaining": 75.0,
                "usage": 25.0,
                "is_free_tier": false,
                "rate_limit": {
                    "requests": 200,
                    "interval": "10s"
                }
            }
        });

        Mock::given(matchers::method("GET"))
            .and(matchers::path("/api/v1/auth/key"))
            .and(matchers::header_exists("authorization"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&body))
            .expect(1)
            .mount(&mock_server)
            .await;

        let config = ClientConfig {
            base_url: Url::parse(&format!("{}/api/v1/", mock_server.uri())).unwrap(),
            timeout: std::time::Duration::from_secs(10),
            ..test_client_config()
        };

        let client = reqwest::Client::new();
        let api = KeyInfoApi::new(client, &config).unwrap();
        let response = api.get_key_info().await.unwrap();

        assert_eq!(response.data.label.as_deref(), Some("test-key"));
        assert_eq!(response.limit(), Some(100.0));
        assert_eq!(response.limit_remaining(), Some(75.0));
        assert_eq!(response.usage(), Some(25.0));
        assert!(!response.is_free_tier());
    }

    #[tokio::test]
    async fn test_get_key_info_wiremock_401_unauthorized() {
        use crate::api::key_info::KeyInfoApi;
        use wiremock::{matchers, Mock, MockServer, ResponseTemplate};

        let mock_server = MockServer::start().await;

        let error_body = serde_json::json!({
            "error": {
                "message": "Invalid API key",
                "code": 401
            }
        });

        Mock::given(matchers::method("GET"))
            .and(matchers::path("/api/v1/auth/key"))
            .respond_with(ResponseTemplate::new(401).set_body_json(&error_body))
            .expect(1)
            .mount(&mock_server)
            .await;

        let config = ClientConfig {
            base_url: Url::parse(&format!("{}/api/v1/", mock_server.uri())).unwrap(),
            timeout: std::time::Duration::from_secs(10),
            ..test_client_config()
        };

        let client = reqwest::Client::new();
        let api = KeyInfoApi::new(client, &config).unwrap();
        let result = api.get_key_info().await;

        assert!(result.is_err(), "401 must produce an error");
        match result.unwrap_err() {
            crate::error::Error::ApiError { code, .. } => {
                assert_eq!(code, 401);
            }
            other => panic!("Expected ApiError, got: {:?}", other),
        }
    }

    #[tokio::test]
    async fn test_get_key_info_wiremock_minimal_response() {
        use crate::api::key_info::KeyInfoApi;
        use wiremock::{matchers, Mock, MockServer, ResponseTemplate};

        let mock_server = MockServer::start().await;

        // Minimal response — only the required data wrapper.
        Mock::given(matchers::method("GET"))
            .and(matchers::path("/api/v1/auth/key"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"data": {}})))
            .expect(1)
            .mount(&mock_server)
            .await;

        let config = ClientConfig {
            base_url: Url::parse(&format!("{}/api/v1/", mock_server.uri())).unwrap(),
            timeout: std::time::Duration::from_secs(10),
            ..test_client_config()
        };

        let client = reqwest::Client::new();
        let api = KeyInfoApi::new(client, &config).unwrap();
        let response = api.get_key_info().await.unwrap();

        assert!(response.limit().is_none());
        assert!(!response.is_free_tier());
    }

    #[tokio::test]
    async fn test_get_key_info_wiremock_free_tier_key() {
        use crate::api::key_info::KeyInfoApi;
        use wiremock::{matchers, Mock, MockServer, ResponseTemplate};

        let mock_server = MockServer::start().await;

        Mock::given(matchers::method("GET"))
            .and(matchers::path("/api/v1/auth/key"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {
                    "is_free_tier": true,
                    "usage": 0.0,
                    "rate_limit": {"requests": 20, "interval": "1m"}
                }
            })))
            .expect(1)
            .mount(&mock_server)
            .await;

        let config = ClientConfig {
            base_url: Url::parse(&format!("{}/api/v1/", mock_server.uri())).unwrap(),
            timeout: std::time::Duration::from_secs(10),
            ..test_client_config()
        };

        let client = reqwest::Client::new();
        let api = KeyInfoApi::new(client, &config).unwrap();
        let response = api.get_key_info().await.unwrap();

        assert!(response.is_free_tier());
        assert_eq!(response.usage(), Some(0.0));
        assert_eq!(
            response
                .data
                .rate_limit
                .as_ref()
                .unwrap()
                .interval
                .as_deref(),
            Some("1m")
        );
    }
}