xerv-core 0.1.0

Workflow orchestration core: memory-mapped arena, write-ahead log, traits, and type system
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
//! HTTP provider for network request abstraction.
//!
//! Allows tests to mock HTTP responses while production code makes real requests.

use parking_lot::RwLock;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;

/// HTTP response from a provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpResponse {
    /// HTTP status code.
    pub status: u16,
    /// Response headers.
    pub headers: HashMap<String, String>,
    /// Response body.
    pub body: Vec<u8>,
}

impl HttpResponse {
    /// Create a new HTTP response.
    pub fn new(status: u16, body: impl Into<Vec<u8>>) -> Self {
        Self {
            status,
            headers: HashMap::new(),
            body: body.into(),
        }
    }

    /// Create a JSON response.
    pub fn json(status: u16, value: &serde_json::Value) -> Self {
        let body = serde_json::to_vec(value).expect("Failed to serialize JSON");
        let mut headers = HashMap::new();
        headers.insert("content-type".to_string(), "application/json".to_string());
        Self {
            status,
            headers,
            body,
        }
    }

    /// Get the body as a string.
    pub fn body_string(&self) -> String {
        String::from_utf8_lossy(&self.body).into_owned()
    }

    /// Get the body as JSON.
    pub fn body_json(&self) -> Result<serde_json::Value, serde_json::Error> {
        serde_json::from_slice(&self.body)
    }

    /// Add a header.
    pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.insert(key.into(), value.into());
        self
    }
}

/// HTTP request for recording.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpRequest {
    /// HTTP method.
    pub method: String,
    /// Request URL.
    pub url: String,
    /// Request headers.
    pub headers: HashMap<String, String>,
    /// Request body.
    pub body: Vec<u8>,
}

/// Error type for HTTP operations.
#[derive(Debug, Clone, thiserror::Error)]
pub enum HttpError {
    /// Connection failed.
    #[error("Connection failed: {0}")]
    ConnectionFailed(String),
    /// Request timed out.
    #[error("Request timed out")]
    Timeout,
    /// No mock rule matched.
    #[error("No mock rule matched for {method} {url}")]
    NoMockMatch { method: String, url: String },
    /// Other error.
    #[error("{0}")]
    Other(String),
}

/// Provider trait for HTTP operations.
pub trait HttpProvider: Send + Sync {
    /// Make an HTTP request.
    fn request(
        &self,
        method: &str,
        url: &str,
        headers: HashMap<String, String>,
        body: Option<Vec<u8>>,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<HttpResponse, HttpError>> + Send + '_>,
    >;

    /// Check if this is a mock provider.
    fn is_mock(&self) -> bool;
}

/// Real HTTP provider that makes actual network requests.
#[derive(Debug, Clone)]
pub struct RealHttp {
    /// Request timeout duration.
    timeout: Duration,
}

impl RealHttp {
    /// Create a new real HTTP provider with default 30 second timeout.
    pub fn new() -> Self {
        Self {
            timeout: Duration::from_secs(30),
        }
    }

    /// Create with a custom timeout.
    pub fn with_timeout(timeout: Duration) -> Self {
        Self { timeout }
    }

    /// Get the configured timeout.
    pub fn timeout(&self) -> Duration {
        self.timeout
    }
}

impl Default for RealHttp {
    fn default() -> Self {
        Self::new()
    }
}

impl HttpProvider for RealHttp {
    fn request(
        &self,
        _method: &str,
        _url: &str,
        _headers: HashMap<String, String>,
        _body: Option<Vec<u8>>,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<HttpResponse, HttpError>> + Send + '_>,
    > {
        let timeout = self.timeout;
        // Real HTTP implementation would use hyper here with the configured timeout
        // For now, return an error indicating it's not implemented
        Box::pin(async move {
            // The timeout would be used here in a real implementation:
            // tokio::time::timeout(timeout, actual_request).await
            let _ = timeout; // Acknowledge timeout is available for future implementation
            Err(HttpError::Other(
                "Real HTTP not implemented - use MockHttp for testing".to_string(),
            ))
        })
    }

    fn is_mock(&self) -> bool {
        false
    }
}

/// A rule for matching and responding to HTTP requests.
#[derive(Clone)]
pub struct MockHttpRule {
    /// HTTP method to match (None = any method).
    pub method: Option<String>,
    /// URL pattern (regex).
    pub url_pattern: Regex,
    /// Response to return.
    pub response: HttpResponse,
    /// Simulated latency.
    pub latency: Option<Duration>,
    /// Number of times this rule should match (None = unlimited).
    pub times: Option<usize>,
    /// Number of times this rule has matched.
    matched_count: usize,
}

impl MockHttpRule {
    /// Create a new rule matching any method.
    pub fn new(url_pattern: &str, response: HttpResponse) -> Self {
        Self {
            method: None,
            url_pattern: Regex::new(url_pattern).expect("Invalid URL regex pattern"),
            response,
            latency: None,
            times: None,
            matched_count: 0,
        }
    }

    /// Set the HTTP method to match.
    pub fn with_method(mut self, method: &str) -> Self {
        self.method = Some(method.to_uppercase());
        self
    }

    /// Set simulated latency.
    pub fn with_latency(mut self, latency: Duration) -> Self {
        self.latency = Some(latency);
        self
    }

    /// Set the number of times this rule should match.
    pub fn times(mut self, n: usize) -> Self {
        self.times = Some(n);
        self
    }

    /// Check if this rule matches the request.
    fn matches(&self, method: &str, url: &str) -> bool {
        // Check method
        if let Some(ref expected_method) = self.method {
            if expected_method != method.to_uppercase().as_str() {
                return false;
            }
        }

        // Check if we've exceeded the match limit
        if let Some(limit) = self.times {
            if self.matched_count >= limit {
                return false;
            }
        }

        // Check URL pattern
        self.url_pattern.is_match(url)
    }
}

impl std::fmt::Debug for MockHttpRule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MockHttpRule")
            .field("method", &self.method)
            .field("url_pattern", &self.url_pattern.as_str())
            .field("response_status", &self.response.status)
            .field("latency", &self.latency)
            .field("times", &self.times)
            .finish()
    }
}

/// Mock HTTP provider for testing.
///
/// Allows defining rules that match requests and return mock responses.
///
/// # Example
///
/// ```
/// use xerv_core::testing::{MockHttp, MockHttpRule, HttpResponse};
/// use serde_json::json;
///
/// let mock = MockHttp::new()
///     .rule(
///         MockHttpRule::new(r"^https://api\.example\.com/users/\d+$", HttpResponse::json(200, &json!({"name": "Alice"})))
///             .with_method("GET")
///     )
///     .rule(
///         MockHttpRule::new(r"^https://api\.example\.com/users$", HttpResponse::json(201, &json!({"id": 1})))
///             .with_method("POST")
///     );
/// ```
pub struct MockHttp {
    rules: RwLock<Vec<MockHttpRule>>,
    requests: RwLock<Vec<HttpRequest>>,
    fail_on_unmatched: bool,
}

impl MockHttp {
    /// Create a new mock HTTP provider.
    pub fn new() -> Self {
        Self {
            rules: RwLock::new(Vec::new()),
            requests: RwLock::new(Vec::new()),
            fail_on_unmatched: true,
        }
    }

    /// Add a rule.
    pub fn rule(self, rule: MockHttpRule) -> Self {
        self.rules.write().push(rule);
        self
    }

    /// Set whether to fail on unmatched requests.
    pub fn fail_on_unmatched(mut self, fail: bool) -> Self {
        self.fail_on_unmatched = fail;
        self
    }

    /// Fluent builder: start defining a GET rule.
    pub fn on_get(self, url_pattern: &str) -> MockHttpBuilder {
        MockHttpBuilder {
            mock: self,
            method: Some("GET".to_string()),
            url_pattern: url_pattern.to_string(),
            latency: None,
            times: None,
        }
    }

    /// Fluent builder: start defining a POST rule.
    pub fn on_post(self, url_pattern: &str) -> MockHttpBuilder {
        MockHttpBuilder {
            mock: self,
            method: Some("POST".to_string()),
            url_pattern: url_pattern.to_string(),
            latency: None,
            times: None,
        }
    }

    /// Fluent builder: start defining a PUT rule.
    pub fn on_put(self, url_pattern: &str) -> MockHttpBuilder {
        MockHttpBuilder {
            mock: self,
            method: Some("PUT".to_string()),
            url_pattern: url_pattern.to_string(),
            latency: None,
            times: None,
        }
    }

    /// Fluent builder: start defining a DELETE rule.
    pub fn on_delete(self, url_pattern: &str) -> MockHttpBuilder {
        MockHttpBuilder {
            mock: self,
            method: Some("DELETE".to_string()),
            url_pattern: url_pattern.to_string(),
            latency: None,
            times: None,
        }
    }

    /// Fluent builder: start defining a rule matching any method.
    pub fn on_any(self, url_pattern: &str) -> MockHttpBuilder {
        MockHttpBuilder {
            mock: self,
            method: None,
            url_pattern: url_pattern.to_string(),
            latency: None,
            times: None,
        }
    }

    /// Get all recorded requests.
    pub fn requests(&self) -> Vec<HttpRequest> {
        self.requests.read().clone()
    }

    /// Clear recorded requests.
    pub fn clear_requests(&self) {
        self.requests.write().clear();
    }

    /// Assert that a specific request was made.
    pub fn assert_request_made(&self, method: &str, url_pattern: &str) -> bool {
        let re = Regex::new(url_pattern).expect("Invalid URL pattern");
        let requests = self.requests.read();
        requests
            .iter()
            .any(|r| r.method.eq_ignore_ascii_case(method) && re.is_match(&r.url))
    }

    /// Get the number of requests made.
    pub fn request_count(&self) -> usize {
        self.requests.read().len()
    }
}

impl Default for MockHttp {
    fn default() -> Self {
        Self::new()
    }
}

impl HttpProvider for MockHttp {
    fn request(
        &self,
        method: &str,
        url: &str,
        headers: HashMap<String, String>,
        body: Option<Vec<u8>>,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<HttpResponse, HttpError>> + Send + '_>,
    > {
        // Record the request
        self.requests.write().push(HttpRequest {
            method: method.to_string(),
            url: url.to_string(),
            headers: headers.clone(),
            body: body.clone().unwrap_or_default(),
        });

        // Find a matching rule
        let mut rules = self.rules.write();
        let matched = rules.iter_mut().find(|rule| rule.matches(method, url));

        match matched {
            Some(rule) => {
                rule.matched_count += 1;
                let response = rule.response.clone();
                let latency = rule.latency;

                Box::pin(async move {
                    if let Some(delay) = latency {
                        tokio::time::sleep(delay).await;
                    }
                    Ok(response)
                })
            }
            None => {
                if self.fail_on_unmatched {
                    let method = method.to_string();
                    let url = url.to_string();
                    Box::pin(async move { Err(HttpError::NoMockMatch { method, url }) })
                } else {
                    // Return a 404 for unmatched requests
                    Box::pin(async move { Ok(HttpResponse::new(404, b"Not Found".to_vec())) })
                }
            }
        }
    }

    fn is_mock(&self) -> bool {
        true
    }
}

/// Builder for fluent mock HTTP rule creation.
pub struct MockHttpBuilder {
    mock: MockHttp,
    method: Option<String>,
    url_pattern: String,
    latency: Option<Duration>,
    times: Option<usize>,
}

impl MockHttpBuilder {
    /// Set simulated latency.
    pub fn with_latency(mut self, latency: Duration) -> Self {
        self.latency = latency.into();
        self
    }

    /// Set the number of times this rule should match.
    pub fn times(mut self, n: usize) -> Self {
        self.times = Some(n);
        self
    }

    /// Set the response to return.
    pub fn respond(self, response: HttpResponse) -> MockHttp {
        let mut rule = MockHttpRule::new(&self.url_pattern, response);
        rule.method = self.method;
        rule.latency = self.latency;
        rule.times = self.times;
        self.mock.rule(rule)
    }

    /// Set a JSON response.
    pub fn respond_json(self, status: u16, value: serde_json::Value) -> MockHttp {
        self.respond(HttpResponse::json(status, &value))
    }

    /// Set a plain text response.
    pub fn respond_text(self, status: u16, text: &str) -> MockHttp {
        let mut response = HttpResponse::new(status, text.as_bytes().to_vec());
        response
            .headers
            .insert("content-type".to_string(), "text/plain".to_string());
        self.respond(response)
    }

    /// Set an error response.
    pub fn respond_error(self, status: u16, message: &str) -> MockHttp {
        self.respond_json(status, serde_json::json!({"error": message}))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[tokio::test]
    async fn mock_http_matches_get() {
        let mock = MockHttp::new()
            .on_get(r"^https://api\.example\.com/users/\d+$")
            .respond_json(200, json!({"name": "Alice"}));

        let response = mock
            .request(
                "GET",
                "https://api.example.com/users/123",
                HashMap::new(),
                None,
            )
            .await
            .unwrap();

        assert_eq!(response.status, 200);
        let body: serde_json::Value = response.body_json().unwrap();
        assert_eq!(body["name"], "Alice");
    }

    #[tokio::test]
    async fn mock_http_matches_post() {
        let mock = MockHttp::new()
            .on_post(r"^https://api\.example\.com/users$")
            .respond_json(201, json!({"id": 42}));

        let response = mock
            .request(
                "POST",
                "https://api.example.com/users",
                HashMap::new(),
                Some(b"{}".to_vec()),
            )
            .await
            .unwrap();

        assert_eq!(response.status, 201);
    }

    #[tokio::test]
    async fn mock_http_fails_on_unmatched() {
        let mock = MockHttp::new()
            .on_get(r"^https://api\.example\.com/users$")
            .respond_json(200, json!([]));

        let result = mock
            .request("GET", "https://api.example.com/other", HashMap::new(), None)
            .await;

        assert!(matches!(result, Err(HttpError::NoMockMatch { .. })));
    }

    #[tokio::test]
    async fn mock_http_records_requests() {
        let mock = MockHttp::new().on_get(r".*").respond_json(200, json!({}));

        mock.request("GET", "https://example.com/a", HashMap::new(), None)
            .await
            .unwrap();
        mock.request("GET", "https://example.com/b", HashMap::new(), None)
            .await
            .unwrap();

        assert_eq!(mock.request_count(), 2);
        assert!(mock.assert_request_made("GET", r"example\.com/a"));
        assert!(mock.assert_request_made("GET", r"example\.com/b"));
    }

    #[tokio::test]
    async fn mock_http_times_limit() {
        let mock = MockHttp::new()
            .on_get(r"^https://api\.example\.com/users$")
            .times(2)
            .respond_json(200, json!([]));

        // First two requests succeed
        mock.request("GET", "https://api.example.com/users", HashMap::new(), None)
            .await
            .unwrap();
        mock.request("GET", "https://api.example.com/users", HashMap::new(), None)
            .await
            .unwrap();

        // Third request fails (no matching rule)
        let result = mock
            .request("GET", "https://api.example.com/users", HashMap::new(), None)
            .await;
        assert!(matches!(result, Err(HttpError::NoMockMatch { .. })));
    }
}