mockforge-http 0.3.111

HTTP/REST protocol support for MockForge
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
//! Snapshot Diff API Handlers
//!
//! Provides endpoints for comparing mock server snapshots across different
//! environments, personas, scenarios, or "realities" (reality continuum levels).
//!
//! This enables side-by-side visualization for demos and debugging.

use axum::{
    extract::{Path, Query, State},
    http::StatusCode,
    response::Json,
    routing::{get, post},
    Router,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

use crate::management::{ManagementState, MockConfig};

/// Snapshot of mock server state at a point in time
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MockSnapshot {
    /// Snapshot ID
    pub id: String,
    /// Timestamp when snapshot was taken
    pub timestamp: i64,
    /// Environment ID (if applicable)
    pub environment_id: Option<String>,
    /// Persona ID (if applicable)
    pub persona_id: Option<String>,
    /// Scenario ID (if applicable)
    pub scenario_id: Option<String>,
    /// Reality level (0.0-1.0, if applicable)
    pub reality_level: Option<f64>,
    /// All mocks in this snapshot
    pub mocks: Vec<MockSnapshotItem>,
    /// Metadata about the snapshot
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Individual mock item in a snapshot
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MockSnapshotItem {
    /// Mock ID
    pub id: String,
    /// HTTP method
    pub method: String,
    /// Path pattern
    pub path: String,
    /// Response status code
    pub status_code: u16,
    /// Response body
    pub response_body: serde_json::Value,
    /// Response headers
    pub response_headers: Option<HashMap<String, String>>,
    /// Mock configuration
    pub config: serde_json::Value,
}

/// Comparison between two snapshots
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SnapshotDiff {
    /// Left snapshot (baseline)
    pub left: MockSnapshot,
    /// Right snapshot (comparison)
    pub right: MockSnapshot,
    /// Differences found
    pub differences: Vec<Difference>,
    /// Summary statistics
    pub summary: DiffSummary,
}

/// Individual difference between snapshots
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Difference {
    /// Type of difference
    pub diff_type: DifferenceType,
    /// Mock ID (if applicable)
    pub mock_id: Option<String>,
    /// Path of the mock
    pub path: String,
    /// Method of the mock
    pub method: String,
    /// Description of the difference
    pub description: String,
    /// Left value (if applicable)
    pub left_value: Option<serde_json::Value>,
    /// Right value (if applicable)
    pub right_value: Option<serde_json::Value>,
    /// Field path where difference occurs (for nested differences)
    pub field_path: Option<String>,
}

/// Type of difference
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DifferenceType {
    /// Mock exists in left but not right
    MissingInRight,
    /// Mock exists in right but not left
    MissingInLeft,
    /// Status code differs
    StatusCodeMismatch,
    /// Response body differs
    BodyMismatch,
    /// Response headers differ
    HeadersMismatch,
    /// Configuration differs
    ConfigMismatch,
}

/// Summary statistics for a diff
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffSummary {
    /// Total mocks in left snapshot
    pub left_total: usize,
    /// Total mocks in right snapshot
    pub right_total: usize,
    /// Number of differences found
    pub differences_count: usize,
    /// Number of mocks only in left
    pub only_in_left: usize,
    /// Number of mocks only in right
    pub only_in_right: usize,
    /// Number of mocks with differences
    pub mocks_with_differences: usize,
}

/// Request to create a snapshot
#[derive(Debug, Deserialize)]
pub struct CreateSnapshotRequest {
    /// Environment ID (optional)
    pub environment_id: Option<String>,
    /// Persona ID (optional)
    pub persona_id: Option<String>,
    /// Scenario ID (optional)
    pub scenario_id: Option<String>,
    /// Reality level (optional, 0.0-1.0)
    pub reality_level: Option<f64>,
    /// Metadata to attach
    #[serde(default)]
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Request to compare snapshots
#[derive(Debug, Deserialize)]
pub struct CompareSnapshotsRequest {
    /// Left snapshot ID (or use environment/persona/scenario)
    pub left_snapshot_id: Option<String>,
    /// Right snapshot ID (or use environment/persona/scenario)
    pub right_snapshot_id: Option<String>,
    /// Left environment ID (alternative to snapshot_id)
    pub left_environment_id: Option<String>,
    /// Right environment ID (alternative to snapshot_id)
    pub right_environment_id: Option<String>,
    /// Left persona ID (alternative to snapshot_id)
    pub left_persona_id: Option<String>,
    /// Right persona ID (alternative to snapshot_id)
    pub right_persona_id: Option<String>,
    /// Left scenario ID (alternative to snapshot_id)
    pub left_scenario_id: Option<String>,
    /// Right scenario ID (alternative to snapshot_id)
    pub right_scenario_id: Option<String>,
    /// Left reality level (alternative to snapshot_id)
    pub left_reality_level: Option<f64>,
    /// Right reality level (alternative to snapshot_id)
    pub right_reality_level: Option<f64>,
}

/// In-memory storage for snapshots
type SnapshotStorage = Arc<tokio::sync::RwLock<HashMap<String, MockSnapshot>>>;

/// Global snapshot storage
fn snapshot_store() -> &'static SnapshotStorage {
    static STORE: std::sync::OnceLock<SnapshotStorage> = std::sync::OnceLock::new();
    STORE.get_or_init(|| Arc::new(tokio::sync::RwLock::new(HashMap::new())))
}

/// Create a snapshot of current mock server state
async fn create_snapshot(
    State(state): State<ManagementState>,
    Json(request): Json<CreateSnapshotRequest>,
) -> Result<Json<MockSnapshot>, StatusCode> {
    // Get current mocks from the state
    let mocks = state.mocks.read().await.clone();

    // Convert mocks to snapshot items
    let snapshot_items: Vec<MockSnapshotItem> = mocks
        .iter()
        .map(|mock| MockSnapshotItem {
            id: mock.id.clone(),
            method: mock.method.clone(),
            path: mock.path.clone(),
            status_code: mock.status_code.unwrap_or(200),
            response_body: mock.response.body.clone(),
            response_headers: mock.response.headers.clone(),
            config: serde_json::to_value(mock).unwrap_or_default(),
        })
        .collect();

    // Create snapshot
    let snapshot = MockSnapshot {
        id: uuid::Uuid::new_v4().to_string(),
        timestamp: chrono::Utc::now().timestamp(),
        environment_id: request.environment_id,
        persona_id: request.persona_id,
        scenario_id: request.scenario_id,
        reality_level: request.reality_level,
        mocks: snapshot_items,
        metadata: request.metadata,
    };

    // Store snapshot in memory
    let mut store = snapshot_store().write().await;
    store.insert(snapshot.id.clone(), snapshot.clone());

    Ok(Json(snapshot))
}

/// Get a snapshot by ID
async fn get_snapshot(
    Path(snapshot_id): Path<String>,
    State(_state): State<ManagementState>,
) -> Result<Json<MockSnapshot>, StatusCode> {
    let store = snapshot_store().read().await;
    store.get(&snapshot_id).cloned().map(Json).ok_or(StatusCode::NOT_FOUND)
}

/// List all snapshots
async fn list_snapshots(
    Query(params): Query<HashMap<String, String>>,
    State(_state): State<ManagementState>,
) -> Result<Json<Vec<MockSnapshot>>, StatusCode> {
    let store = snapshot_store().read().await;
    let mut snapshots: Vec<MockSnapshot> = store
        .values()
        .filter(|s| {
            // Filter by environment_id if provided
            if let Some(env_id) = params.get("environment_id") {
                if s.environment_id.as_deref() != Some(env_id.as_str()) {
                    return false;
                }
            }
            // Filter by persona_id if provided
            if let Some(persona_id) = params.get("persona_id") {
                if s.persona_id.as_deref() != Some(persona_id.as_str()) {
                    return false;
                }
            }
            // Filter by scenario_id if provided
            if let Some(scenario_id) = params.get("scenario_id") {
                if s.scenario_id.as_deref() != Some(scenario_id.as_str()) {
                    return false;
                }
            }
            true
        })
        .cloned()
        .collect();

    // Sort by timestamp descending (most recent first)
    snapshots.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));

    Ok(Json(snapshots))
}

/// Compare two snapshots
async fn compare_snapshots(
    State(state): State<ManagementState>,
    Json(request): Json<CompareSnapshotsRequest>,
) -> Result<Json<SnapshotDiff>, StatusCode> {
    let store = snapshot_store().read().await;

    // Load left snapshot: by ID if provided, otherwise create from current mocks
    let left_snapshot = if let Some(ref id) = request.left_snapshot_id {
        store.get(id).cloned().ok_or(StatusCode::NOT_FOUND)?
    } else {
        let current_mocks = state.mocks.read().await.clone();
        create_snapshot_from_mocks(
            &current_mocks,
            request.left_environment_id.clone(),
            request.left_persona_id.clone(),
            request.left_scenario_id.clone(),
            request.left_reality_level,
        )
    };

    // Load right snapshot: by ID if provided, otherwise create from current mocks
    let right_snapshot = if let Some(ref id) = request.right_snapshot_id {
        store.get(id).cloned().ok_or(StatusCode::NOT_FOUND)?
    } else {
        let current_mocks = state.mocks.read().await.clone();
        create_snapshot_from_mocks(
            &current_mocks,
            request.right_environment_id.clone(),
            request.right_persona_id.clone(),
            request.right_scenario_id.clone(),
            request.right_reality_level,
        )
    };

    // Compare snapshots
    let diff = compare_snapshot_objects(&left_snapshot, &right_snapshot);

    Ok(Json(diff))
}

/// Helper to create a snapshot from mocks
fn create_snapshot_from_mocks(
    mocks: &[MockConfig],
    environment_id: Option<String>,
    persona_id: Option<String>,
    scenario_id: Option<String>,
    reality_level: Option<f64>,
) -> MockSnapshot {
    let snapshot_items: Vec<MockSnapshotItem> = mocks
        .iter()
        .map(|mock| MockSnapshotItem {
            id: mock.id.clone(),
            method: mock.method.clone(),
            path: mock.path.clone(),
            status_code: mock.status_code.unwrap_or(200),
            response_body: mock.response.body.clone(),
            response_headers: mock.response.headers.clone(),
            config: serde_json::to_value(mock).unwrap_or_default(),
        })
        .collect();

    MockSnapshot {
        id: uuid::Uuid::new_v4().to_string(),
        timestamp: chrono::Utc::now().timestamp(),
        environment_id,
        persona_id,
        scenario_id,
        reality_level,
        mocks: snapshot_items,
        metadata: HashMap::new(),
    }
}

/// Compare two snapshot objects
fn compare_snapshot_objects(left: &MockSnapshot, right: &MockSnapshot) -> SnapshotDiff {
    let mut differences = Vec::new();

    // Create maps for quick lookup
    let left_map: HashMap<String, &MockSnapshotItem> =
        left.mocks.iter().map(|m| (format!("{}:{}", m.method, m.path), m)).collect();

    let right_map: HashMap<String, &MockSnapshotItem> =
        right.mocks.iter().map(|m| (format!("{}:{}", m.method, m.path), m)).collect();

    // Find mocks only in left
    for (key, left_mock) in &left_map {
        if !right_map.contains_key(key) {
            differences.push(Difference {
                diff_type: DifferenceType::MissingInRight,
                mock_id: Some(left_mock.id.clone()),
                path: left_mock.path.clone(),
                method: left_mock.method.clone(),
                description: format!(
                    "Mock {} {} exists in left but not in right",
                    left_mock.method, left_mock.path
                ),
                left_value: Some(serde_json::to_value(left_mock).unwrap_or_default()),
                right_value: None,
                field_path: None,
            });
        }
    }

    // Find mocks only in right
    for (key, right_mock) in &right_map {
        if !left_map.contains_key(key) {
            differences.push(Difference {
                diff_type: DifferenceType::MissingInLeft,
                mock_id: Some(right_mock.id.clone()),
                path: right_mock.path.clone(),
                method: right_mock.method.clone(),
                description: format!(
                    "Mock {} {} exists in right but not in left",
                    right_mock.method, right_mock.path
                ),
                left_value: None,
                right_value: Some(serde_json::to_value(right_mock).unwrap_or_default()),
                field_path: None,
            });
        }
    }

    // Compare common mocks
    for (key, left_mock) in &left_map {
        if let Some(right_mock) = right_map.get(key) {
            // Compare status codes
            if left_mock.status_code != right_mock.status_code {
                differences.push(Difference {
                    diff_type: DifferenceType::StatusCodeMismatch,
                    mock_id: Some(left_mock.id.clone()),
                    path: left_mock.path.clone(),
                    method: left_mock.method.clone(),
                    description: format!(
                        "Status code differs: {} vs {}",
                        left_mock.status_code, right_mock.status_code
                    ),
                    left_value: Some(serde_json::json!(left_mock.status_code)),
                    right_value: Some(serde_json::json!(right_mock.status_code)),
                    field_path: Some("status_code".to_string()),
                });
            }

            // Compare response bodies (deep comparison)
            if left_mock.response_body != right_mock.response_body {
                differences.push(Difference {
                    diff_type: DifferenceType::BodyMismatch,
                    mock_id: Some(left_mock.id.clone()),
                    path: left_mock.path.clone(),
                    method: left_mock.method.clone(),
                    description: format!(
                        "Response body differs for {} {}",
                        left_mock.method, left_mock.path
                    ),
                    left_value: Some(left_mock.response_body.clone()),
                    right_value: Some(right_mock.response_body.clone()),
                    field_path: Some("response_body".to_string()),
                });
            }

            // Compare headers
            if left_mock.response_headers != right_mock.response_headers {
                differences.push(Difference {
                    diff_type: DifferenceType::HeadersMismatch,
                    mock_id: Some(left_mock.id.clone()),
                    path: left_mock.path.clone(),
                    method: left_mock.method.clone(),
                    description: format!(
                        "Response headers differ for {} {}",
                        left_mock.method, left_mock.path
                    ),
                    left_value: left_mock
                        .response_headers
                        .as_ref()
                        .map(|h| serde_json::to_value(h).unwrap_or_default()),
                    right_value: right_mock
                        .response_headers
                        .as_ref()
                        .map(|h| serde_json::to_value(h).unwrap_or_default()),
                    field_path: Some("response_headers".to_string()),
                });
            }
        }
    }

    // Calculate summary
    let only_in_left = differences
        .iter()
        .filter(|d| matches!(d.diff_type, DifferenceType::MissingInRight))
        .count();
    let only_in_right = differences
        .iter()
        .filter(|d| matches!(d.diff_type, DifferenceType::MissingInLeft))
        .count();
    let mocks_with_differences: std::collections::HashSet<String> =
        differences.iter().filter_map(|d| d.mock_id.clone()).collect();

    let summary = DiffSummary {
        left_total: left.mocks.len(),
        right_total: right.mocks.len(),
        differences_count: differences.len(),
        only_in_left,
        only_in_right,
        mocks_with_differences: mocks_with_differences.len(),
    };

    SnapshotDiff {
        left: left.clone(),
        right: right.clone(),
        differences,
        summary,
    }
}

/// Build the snapshot diff router
pub fn snapshot_diff_router(state: ManagementState) -> Router<ManagementState> {
    Router::new()
        .route("/snapshots", post(create_snapshot))
        .route("/snapshots", get(list_snapshots))
        .route("/snapshots/{id}", get(get_snapshot))
        .route("/snapshots/compare", post(compare_snapshots))
        .with_state(state)
}