mockforge-ui 0.3.88

Admin UI for MockForge - web-based interface for managing mock servers
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Graph visualization handlers
//!
//! These handlers provide graph data for visualizing mock environments,
//! endpoints, their relationships, and state transitions.

use axum::{
    extract::State,
    http::StatusCode,
    response::{
        sse::{Event, Sse},
        IntoResponse, Json,
    },
};
use futures_util::stream::{self, Stream};
use mockforge_core::graph::GraphBuilder;
use mockforge_core::request_chaining::ChainDefinition;
use serde_json::Value;
use std::convert::Infallible;
use std::time::Duration;

use super::AdminState;
use crate::models::ApiResponse;

/// Get graph data for visualization
///
/// This endpoint aggregates data from multiple sources:
/// - Endpoints from UI Builder API
/// - Request chains
/// - State machines (if available)
/// - Workspaces/services
pub async fn get_graph(State(state): State<AdminState>) -> impl IntoResponse {
    let mut builder = GraphBuilder::new();

    // Fetch chains from the HTTP server
    if let Some(http_addr) = state.http_server_addr {
        match fetch_chains_from_server(http_addr).await {
            Ok(chains) => {
                builder.from_chains(&chains);
            }
            Err(e) => {
                tracing::warn!("Failed to fetch chains for graph: {}", e);
                // Continue without chains - graph will still work
            }
        }
    }

    // Fetch endpoints from UI Builder API if available
    if let Some(http_addr) = state.http_server_addr {
        match fetch_endpoints_from_ui_builder(http_addr).await {
            Ok(endpoints) => {
                // Convert UI Builder endpoints to graph format
                for endpoint in endpoints {
                    let protocol_str = match endpoint.protocol {
                        mockforge_http::ui_builder::Protocol::Http => "http",
                        mockforge_http::ui_builder::Protocol::Grpc => "grpc",
                        mockforge_http::ui_builder::Protocol::Websocket => "websocket",
                        mockforge_http::ui_builder::Protocol::Graphql => "graphql",
                        mockforge_http::ui_builder::Protocol::Mqtt => "mqtt",
                        mockforge_http::ui_builder::Protocol::Smtp => "smtp",
                        mockforge_http::ui_builder::Protocol::Kafka => "kafka",
                        mockforge_http::ui_builder::Protocol::Amqp => "amqp",
                        mockforge_http::ui_builder::Protocol::Ftp => "ftp",
                    };

                    let mut metadata = std::collections::HashMap::new();
                    metadata.insert("enabled".to_string(), Value::Bool(endpoint.enabled));
                    if let Some(desc) = endpoint.description {
                        metadata.insert("description".to_string(), Value::String(desc));
                    }

                    // Extract method and path from HTTP config if available
                    if let mockforge_http::ui_builder::EndpointProtocolConfig::Http(http_config) =
                        &endpoint.config
                    {
                        metadata.insert(
                            "method".to_string(),
                            Value::String(http_config.method.clone()),
                        );
                        metadata
                            .insert("path".to_string(), Value::String(http_config.path.clone()));
                    }

                    let protocol = match protocol_str {
                        "http" => mockforge_core::graph::Protocol::Http,
                        "grpc" => mockforge_core::graph::Protocol::Grpc,
                        "websocket" => mockforge_core::graph::Protocol::Websocket,
                        "graphql" => mockforge_core::graph::Protocol::Graphql,
                        "mqtt" => mockforge_core::graph::Protocol::Mqtt,
                        "smtp" => mockforge_core::graph::Protocol::Smtp,
                        "kafka" => mockforge_core::graph::Protocol::Kafka,
                        "amqp" => mockforge_core::graph::Protocol::Amqp,
                        "ftp" => mockforge_core::graph::Protocol::Ftp,
                        _ => mockforge_core::graph::Protocol::Http,
                    };

                    builder.add_endpoint(endpoint.id, endpoint.name, protocol, metadata);
                }
            }
            Err(e) => {
                tracing::debug!("UI Builder endpoints not available: {}", e);
                // Continue without endpoints - graph will still work with chains
            }
        }
    }

    // Build the graph
    let graph_data = builder.build();

    Json(ApiResponse::success(graph_data))
}

/// Fetch endpoints from UI Builder API
async fn fetch_endpoints_from_ui_builder(
    http_addr: std::net::SocketAddr,
) -> Result<Vec<mockforge_http::ui_builder::EndpointConfig>, String> {
    let url = format!("http://{}/__mockforge/ui-builder/endpoints", http_addr);
    let client = reqwest::Client::new();

    let response = client
        .get(&url)
        .send()
        .await
        .map_err(|e| format!("Failed to fetch endpoints: {}", e))?;

    if !response.status().is_success() {
        return Err(format!("HTTP error: {}", response.status()));
    }

    let json: Value =
        response.json().await.map_err(|e| format!("Failed to parse response: {}", e))?;

    // Extract endpoints from response
    // Assuming it returns: { "endpoints": [...] } or { "data": { "endpoints": [...] } }
    let endpoints_array = json
        .get("endpoints")
        .or_else(|| json.get("data").and_then(|d| d.get("endpoints")))
        .and_then(|v| v.as_array())
        .ok_or_else(|| "Invalid response format: endpoints array not found".to_string())?;

    let mut endpoints = Vec::new();
    for endpoint_value in endpoints_array {
        if let Ok(endpoint) = serde_json::from_value::<mockforge_http::ui_builder::EndpointConfig>(
            endpoint_value.clone(),
        ) {
            endpoints.push(endpoint);
        }
    }

    Ok(endpoints)
}

/// SSE endpoint for real-time graph updates
pub async fn graph_sse(
    State(state): State<AdminState>,
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
    tracing::info!("SSE endpoint /graph/sse accessed - starting real-time graph updates");

    // Clone state for use in the stream
    let http_addr = state.http_server_addr;

    let stream = stream::unfold((), move |_| {
        let http_addr = http_addr;
        async move {
            tokio::time::sleep(Duration::from_secs(5)).await; // Update every 5 seconds

            // Build graph data (same logic as get_graph)
            let mut builder = GraphBuilder::new();

            // Fetch chains
            if let Some(addr) = http_addr {
                if let Ok(chains) = fetch_chains_from_server(addr).await {
                    builder.from_chains(&chains);
                }

                // Fetch endpoints from UI Builder
                if let Ok(endpoints) = fetch_endpoints_from_ui_builder(addr).await {
                    for endpoint in endpoints {
                        let protocol_str = match endpoint.protocol {
                            mockforge_http::ui_builder::Protocol::Http => "http",
                            mockforge_http::ui_builder::Protocol::Grpc => "grpc",
                            mockforge_http::ui_builder::Protocol::Websocket => "websocket",
                            mockforge_http::ui_builder::Protocol::Graphql => "graphql",
                            mockforge_http::ui_builder::Protocol::Mqtt => "mqtt",
                            mockforge_http::ui_builder::Protocol::Smtp => "smtp",
                            mockforge_http::ui_builder::Protocol::Kafka => "kafka",
                            mockforge_http::ui_builder::Protocol::Amqp => "amqp",
                            mockforge_http::ui_builder::Protocol::Ftp => "ftp",
                        };

                        let mut metadata = std::collections::HashMap::new();
                        metadata.insert("enabled".to_string(), Value::Bool(endpoint.enabled));
                        if let Some(desc) = endpoint.description {
                            metadata.insert("description".to_string(), Value::String(desc));
                        }

                        if let mockforge_http::ui_builder::EndpointProtocolConfig::Http(
                            http_config,
                        ) = &endpoint.config
                        {
                            metadata.insert(
                                "method".to_string(),
                                Value::String(http_config.method.clone()),
                            );
                            metadata.insert(
                                "path".to_string(),
                                Value::String(http_config.path.clone()),
                            );
                        }

                        let protocol = match protocol_str {
                            "http" => mockforge_core::graph::Protocol::Http,
                            "grpc" => mockforge_core::graph::Protocol::Grpc,
                            "websocket" => mockforge_core::graph::Protocol::Websocket,
                            "graphql" => mockforge_core::graph::Protocol::Graphql,
                            "mqtt" => mockforge_core::graph::Protocol::Mqtt,
                            "smtp" => mockforge_core::graph::Protocol::Smtp,
                            "kafka" => mockforge_core::graph::Protocol::Kafka,
                            "amqp" => mockforge_core::graph::Protocol::Amqp,
                            "ftp" => mockforge_core::graph::Protocol::Ftp,
                            _ => mockforge_core::graph::Protocol::Http,
                        };

                        builder.add_endpoint(endpoint.id, endpoint.name, protocol, metadata);
                    }
                }
            }

            let graph_data = builder.build();
            let json_data = serde_json::to_string(&graph_data).unwrap_or_default();

            Some((Ok(Event::default().data(json_data)), ()))
        }
    });

    Sse::new(stream).keep_alive(
        axum::response::sse::KeepAlive::new()
            .interval(Duration::from_secs(15))
            .text("keep-alive-text"),
    )
}

/// Fetch chains from the HTTP server
async fn fetch_chains_from_server(
    http_addr: std::net::SocketAddr,
) -> Result<Vec<ChainDefinition>, String> {
    let url = format!("http://{}/__mockforge/chains", http_addr);
    let client = reqwest::Client::new();

    let response = client
        .get(&url)
        .send()
        .await
        .map_err(|e| format!("Failed to fetch chains: {}", e))?;

    if !response.status().is_success() {
        return Err(format!("HTTP error: {}", response.status()));
    }

    let json: Value =
        response.json().await.map_err(|e| format!("Failed to parse response: {}", e))?;

    // Extract chains from the response
    // The response format depends on the chain API implementation
    // Assuming it returns: { "chains": [...] } or { "data": { "chains": [...] } }
    let chains_array = json
        .get("chains")
        .or_else(|| json.get("data").and_then(|d| d.get("chains")))
        .and_then(|v| v.as_array())
        .ok_or_else(|| "Invalid response format: chains array not found".to_string())?;

    let mut chains = Vec::new();
    for chain_value in chains_array {
        // Try to get the full chain definition
        // First try to get by ID and fetch full details
        if let Some(chain_id) = chain_value.get("id").and_then(|v| v.as_str()) {
            match fetch_chain_details(http_addr, chain_id).await {
                Ok(Some(chain)) => chains.push(chain),
                Ok(None) => {
                    // Chain not found, skip
                    tracing::warn!("Chain {} not found, skipping", chain_id);
                }
                Err(e) => {
                    tracing::warn!("Failed to fetch chain {}: {}", chain_id, e);
                    // Try to parse from summary if available
                    if let Ok(chain) =
                        serde_json::from_value::<ChainDefinition>(chain_value.clone())
                    {
                        chains.push(chain);
                    }
                }
            }
        }
    }

    Ok(chains)
}

/// Fetch full chain details by ID
async fn fetch_chain_details(
    http_addr: std::net::SocketAddr,
    chain_id: &str,
) -> Result<Option<ChainDefinition>, String> {
    let url = format!("http://{}/__mockforge/chains/{}", http_addr, chain_id);
    let client = reqwest::Client::new();

    let response = client
        .get(&url)
        .send()
        .await
        .map_err(|e| format!("Failed to fetch chain details: {}", e))?;

    if response.status() == StatusCode::NOT_FOUND {
        return Ok(None);
    }

    if !response.status().is_success() {
        return Err(format!("HTTP error: {}", response.status()));
    }

    let json: Value =
        response.json().await.map_err(|e| format!("Failed to parse response: {}", e))?;

    // Extract chain from response
    // Assuming it returns: { "chain": {...} } or { "data": {...} } or just the chain object
    let chain_value = json.get("chain").or_else(|| json.get("data")).unwrap_or(&json);

    serde_json::from_value::<ChainDefinition>(chain_value.clone())
        .map(Some)
        .map_err(|e| format!("Failed to deserialize chain: {}", e))
}

#[cfg(test)]
mod tests {
    use super::*;
    use mockforge_core::graph::Protocol;

    // ==================== GraphBuilder Tests ====================

    #[test]
    fn test_graph_builder_creation() {
        let builder = GraphBuilder::new();
        let graph = builder.build();
        assert_eq!(graph.nodes.len(), 0);
        assert_eq!(graph.edges.len(), 0);
        assert_eq!(graph.clusters.len(), 0);
    }

    #[test]
    fn test_graph_builder_add_endpoint() {
        let mut builder = GraphBuilder::new();
        let mut metadata = std::collections::HashMap::new();
        metadata.insert("enabled".to_string(), Value::Bool(true));

        builder.add_endpoint(
            "endpoint-1".to_string(),
            "Test Endpoint".to_string(),
            Protocol::Http,
            metadata,
        );

        let graph = builder.build();
        assert_eq!(graph.nodes.len(), 1);
    }

    #[test]
    fn test_graph_builder_multiple_endpoints() {
        let mut builder = GraphBuilder::new();

        for i in 0..5 {
            let metadata = std::collections::HashMap::new();
            builder.add_endpoint(
                format!("endpoint-{}", i),
                format!("Endpoint {}", i),
                Protocol::Http,
                metadata,
            );
        }

        let graph = builder.build();
        assert_eq!(graph.nodes.len(), 5);
    }

    #[test]
    fn test_graph_builder_different_protocols() {
        let mut builder = GraphBuilder::new();
        let metadata = std::collections::HashMap::new();

        let protocols = vec![
            Protocol::Http,
            Protocol::Grpc,
            Protocol::Websocket,
            Protocol::Graphql,
            Protocol::Mqtt,
        ];

        for (i, protocol) in protocols.into_iter().enumerate() {
            builder.add_endpoint(
                format!("endpoint-{}", i),
                format!("Protocol {}", i),
                protocol,
                metadata.clone(),
            );
        }

        let graph = builder.build();
        assert_eq!(graph.nodes.len(), 5);
    }

    #[test]
    fn test_graph_builder_with_metadata() {
        let mut builder = GraphBuilder::new();
        let mut metadata = std::collections::HashMap::new();
        metadata.insert("method".to_string(), Value::String("GET".to_string()));
        metadata.insert("path".to_string(), Value::String("/api/users".to_string()));
        metadata.insert("enabled".to_string(), Value::Bool(true));

        builder.add_endpoint(
            "http-endpoint".to_string(),
            "HTTP API".to_string(),
            Protocol::Http,
            metadata,
        );

        let graph = builder.build();
        assert_eq!(graph.nodes.len(), 1);
    }

    // ==================== Protocol Conversion Tests ====================

    #[test]
    fn test_protocol_http() {
        let protocol = Protocol::Http;
        assert!(matches!(protocol, Protocol::Http));
    }

    #[test]
    fn test_protocol_grpc() {
        let protocol = Protocol::Grpc;
        assert!(matches!(protocol, Protocol::Grpc));
    }

    #[test]
    fn test_protocol_websocket() {
        let protocol = Protocol::Websocket;
        assert!(matches!(protocol, Protocol::Websocket));
    }

    #[test]
    fn test_protocol_graphql() {
        let protocol = Protocol::Graphql;
        assert!(matches!(protocol, Protocol::Graphql));
    }

    #[test]
    fn test_protocol_mqtt() {
        let protocol = Protocol::Mqtt;
        assert!(matches!(protocol, Protocol::Mqtt));
    }

    #[test]
    fn test_protocol_smtp() {
        let protocol = Protocol::Smtp;
        assert!(matches!(protocol, Protocol::Smtp));
    }

    #[test]
    fn test_protocol_kafka() {
        let protocol = Protocol::Kafka;
        assert!(matches!(protocol, Protocol::Kafka));
    }

    #[test]
    fn test_protocol_amqp() {
        let protocol = Protocol::Amqp;
        assert!(matches!(protocol, Protocol::Amqp));
    }

    #[test]
    fn test_protocol_ftp() {
        let protocol = Protocol::Ftp;
        assert!(matches!(protocol, Protocol::Ftp));
    }

    // ==================== Graph Structure Tests ====================

    #[test]
    fn test_graph_empty_clusters() {
        let builder = GraphBuilder::new();
        let graph = builder.build();
        assert!(graph.clusters.is_empty());
    }

    #[test]
    fn test_graph_empty_edges() {
        let builder = GraphBuilder::new();
        let graph = builder.build();
        assert!(graph.edges.is_empty());
    }

    // ==================== Edge Cases ====================

    #[test]
    fn test_graph_builder_empty_metadata() {
        let mut builder = GraphBuilder::new();
        let metadata = std::collections::HashMap::new();

        builder.add_endpoint(
            "minimal".to_string(),
            "Minimal Endpoint".to_string(),
            Protocol::Http,
            metadata,
        );

        let graph = builder.build();
        assert_eq!(graph.nodes.len(), 1);
    }

    #[test]
    fn test_graph_builder_unicode_names() {
        let mut builder = GraphBuilder::new();
        let metadata = std::collections::HashMap::new();

        builder.add_endpoint(
            "unicode-日本語".to_string(),
            "ユニコード".to_string(),
            Protocol::Http,
            metadata,
        );

        let graph = builder.build();
        assert_eq!(graph.nodes.len(), 1);
    }

    #[test]
    fn test_graph_builder_special_characters() {
        let mut builder = GraphBuilder::new();
        let metadata = std::collections::HashMap::new();

        builder.add_endpoint(
            "special-!@#$%".to_string(),
            "Special <>&'\"".to_string(),
            Protocol::Http,
            metadata,
        );

        let graph = builder.build();
        assert_eq!(graph.nodes.len(), 1);
    }

    #[test]
    fn test_graph_builder_long_names() {
        let mut builder = GraphBuilder::new();
        let metadata = std::collections::HashMap::new();
        let long_id = "a".repeat(1000);
        let long_name = "b".repeat(1000);

        builder.add_endpoint(long_id, long_name, Protocol::Http, metadata);

        let graph = builder.build();
        assert_eq!(graph.nodes.len(), 1);
    }

    #[test]
    fn test_graph_builder_complex_metadata() {
        let mut builder = GraphBuilder::new();
        let mut metadata = std::collections::HashMap::new();
        metadata.insert("nested".to_string(), serde_json::json!({"key": {"inner": "value"}}));
        metadata.insert("array".to_string(), serde_json::json!([1, 2, 3]));
        metadata.insert("null".to_string(), Value::Null);

        builder.add_endpoint(
            "complex".to_string(),
            "Complex Metadata".to_string(),
            Protocol::Http,
            metadata,
        );

        let graph = builder.build();
        assert_eq!(graph.nodes.len(), 1);
    }

    #[test]
    fn test_graph_builder_duplicate_ids() {
        let mut builder = GraphBuilder::new();
        let metadata = std::collections::HashMap::new();

        // Add same ID twice - behavior depends on implementation
        builder.add_endpoint(
            "same-id".to_string(),
            "First".to_string(),
            Protocol::Http,
            metadata.clone(),
        );
        builder.add_endpoint("same-id".to_string(), "Second".to_string(), Protocol::Grpc, metadata);

        let graph = builder.build();
        // Either 1 (replaced) or 2 (both added) depending on implementation
        assert!(!graph.nodes.is_empty());
    }
}