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
/// WebSocket interface for live mock updates
///
/// Provides real-time notifications when mocks are created, updated, or deleted.
/// Used by developer tools like VS Code extension for live synchronization.
use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade};
use axum::extract::State;
use axum::response::IntoResponse;
use axum::routing::get;
use axum::Router;
use futures::stream::StreamExt;
use futures::SinkExt;
use serde::{Deserialize, Serialize};
use tokio::sync::broadcast;
use tracing::*;
/// Default broadcast channel capacity for WebSocket mock events
const DEFAULT_WS_BROADCAST_CAPACITY: usize = 100;
/// Get the WebSocket broadcast channel capacity from environment or use default
fn get_ws_broadcast_capacity() -> usize {
std::env::var("MOCKFORGE_WS_BROADCAST_CAPACITY")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(DEFAULT_WS_BROADCAST_CAPACITY)
}
/// Events that can be broadcasted to WebSocket clients
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum MockEvent {
/// Mock was created
MockCreated {
/// The created mock configuration
mock: super::management::MockConfig,
/// ISO 8601 timestamp of the event
timestamp: String,
},
/// Mock was updated
MockUpdated {
/// The updated mock configuration
mock: super::management::MockConfig,
/// ISO 8601 timestamp of the event
timestamp: String,
},
/// Mock was deleted
MockDeleted {
/// ID of the deleted mock
id: String,
/// ISO 8601 timestamp of the event
timestamp: String,
},
/// Server statistics changed
StatsUpdated {
/// Updated server statistics
stats: super::management::ServerStats,
/// ISO 8601 timestamp of the event
timestamp: String,
},
/// Connection established confirmation
Connected {
/// Connection confirmation message
message: String,
/// ISO 8601 timestamp of the event
timestamp: String,
},
/// State machine was created or updated
StateMachineUpdated {
/// Resource type of the state machine
resource_type: String,
/// The state machine definition
state_machine: mockforge_core::intelligent_behavior::rules::StateMachine,
/// ISO 8601 timestamp of the event
timestamp: String,
},
/// State machine was deleted
StateMachineDeleted {
/// Resource type of the deleted state machine
resource_type: String,
/// ISO 8601 timestamp of the event
timestamp: String,
},
/// State instance was created
StateInstanceCreated {
/// Resource ID
resource_id: String,
/// Resource type
resource_type: String,
/// Initial state
initial_state: String,
/// ISO 8601 timestamp of the event
timestamp: String,
},
/// State transition occurred
StateTransitioned {
/// Resource ID
resource_id: String,
/// Resource type
resource_type: String,
/// Previous state
from_state: String,
/// New state
to_state: String,
/// Current state data
state_data: std::collections::HashMap<String, serde_json::Value>,
/// ISO 8601 timestamp of the event
timestamp: String,
},
/// State instance was deleted
StateInstanceDeleted {
/// Resource ID
resource_id: String,
/// Resource type
resource_type: String,
/// ISO 8601 timestamp of the event
timestamp: String,
},
}
impl MockEvent {
/// Create a mock created event
pub fn mock_created(mock: super::management::MockConfig) -> Self {
Self::MockCreated {
mock,
timestamp: chrono::Utc::now().to_rfc3339(),
}
}
/// Create a mock updated event
pub fn mock_updated(mock: super::management::MockConfig) -> Self {
Self::MockUpdated {
mock,
timestamp: chrono::Utc::now().to_rfc3339(),
}
}
/// Create a mock deleted event
pub fn mock_deleted(id: String) -> Self {
Self::MockDeleted {
id,
timestamp: chrono::Utc::now().to_rfc3339(),
}
}
/// Create a stats updated event
pub fn stats_updated(stats: super::management::ServerStats) -> Self {
Self::StatsUpdated {
stats,
timestamp: chrono::Utc::now().to_rfc3339(),
}
}
/// Create a connection established event
pub fn connected(message: String) -> Self {
Self::Connected {
message,
timestamp: chrono::Utc::now().to_rfc3339(),
}
}
/// Create a state machine updated event
pub fn state_machine_updated(
resource_type: String,
state_machine: mockforge_core::intelligent_behavior::rules::StateMachine,
) -> Self {
Self::StateMachineUpdated {
resource_type,
state_machine,
timestamp: chrono::Utc::now().to_rfc3339(),
}
}
/// Create a state machine deleted event
pub fn state_machine_deleted(resource_type: String) -> Self {
Self::StateMachineDeleted {
resource_type,
timestamp: chrono::Utc::now().to_rfc3339(),
}
}
/// Create a state instance created event
pub fn state_instance_created(
resource_id: String,
resource_type: String,
initial_state: String,
) -> Self {
Self::StateInstanceCreated {
resource_id,
resource_type,
initial_state,
timestamp: chrono::Utc::now().to_rfc3339(),
}
}
/// Create a state transitioned event
pub fn state_transitioned(
resource_id: String,
resource_type: String,
from_state: String,
to_state: String,
state_data: std::collections::HashMap<String, serde_json::Value>,
) -> Self {
Self::StateTransitioned {
resource_id,
resource_type,
from_state,
to_state,
state_data,
timestamp: chrono::Utc::now().to_rfc3339(),
}
}
/// Create a state instance deleted event
pub fn state_instance_deleted(resource_id: String, resource_type: String) -> Self {
Self::StateInstanceDeleted {
resource_id,
resource_type,
timestamp: chrono::Utc::now().to_rfc3339(),
}
}
}
/// Shared state for WebSocket management
#[derive(Clone)]
pub struct WsManagementState {
/// Broadcast channel for sending events to all connected clients
pub tx: broadcast::Sender<MockEvent>,
}
impl WsManagementState {
/// Create a new WebSocket management state with broadcast channel
///
/// The broadcast channel capacity can be configured via the
/// `MOCKFORGE_WS_BROADCAST_CAPACITY` environment variable.
pub fn new() -> Self {
let capacity = get_ws_broadcast_capacity();
let (tx, _) = broadcast::channel(capacity);
Self { tx }
}
/// Broadcast an event to all connected clients
pub fn broadcast(
&self,
event: MockEvent,
) -> Result<usize, Box<broadcast::error::SendError<MockEvent>>> {
self.tx.send(event).map_err(Box::new)
}
}
impl Default for WsManagementState {
fn default() -> Self {
Self::new()
}
}
/// WebSocket upgrade handler
async fn ws_handler(
ws: WebSocketUpgrade,
State(state): State<WsManagementState>,
) -> impl IntoResponse {
ws.on_upgrade(move |socket| handle_socket(socket, state))
}
/// Handle a WebSocket connection
async fn handle_socket(socket: WebSocket, state: WsManagementState) {
let (mut sender, mut receiver) = socket.split();
// Subscribe to broadcast channel
let mut rx = state.tx.subscribe();
// Send initial connection confirmation
let connected_event = MockEvent::connected("Connected to MockForge management API".to_string());
if let Ok(json) = serde_json::to_string(&connected_event) {
if sender.send(Message::Text(json.into())).await.is_err() {
return;
}
}
// Spawn a task to forward broadcast messages to this client
let mut send_task = tokio::spawn(async move {
while let Ok(event) = rx.recv().await {
if let Ok(json) = serde_json::to_string(&event) {
if sender.send(Message::Text(json.into())).await.is_err() {
break;
}
}
}
});
// Handle incoming messages from client (for now, just keep connection alive)
let mut recv_task = tokio::spawn(async move {
while let Some(Ok(msg)) = receiver.next().await {
match msg {
Message::Text(text) => {
debug!("Received WebSocket message: {}", text);
// Could handle client commands here in the future
}
Message::Close(_) => {
info!("WebSocket client disconnected");
break;
}
_ => {}
}
}
});
// Wait for either task to finish
tokio::select! {
_ = &mut send_task => {
debug!("Send task completed");
recv_task.abort();
}
_ = &mut recv_task => {
debug!("Receive task completed");
send_task.abort();
}
}
}
/// Build the WebSocket management router
pub fn ws_management_router(state: WsManagementState) -> Router {
Router::new().route("/", get(ws_handler)).with_state(state)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ws_management_state_creation() {
let _state = WsManagementState::new();
// Should be able to create state without errors
}
#[test]
fn test_mock_event_creation() {
use super::super::management::{MockConfig, MockResponse};
let mock = MockConfig {
id: "test-1".to_string(),
name: "Test Mock".to_string(),
method: "GET".to_string(),
path: "/test".to_string(),
response: MockResponse {
body: serde_json::json!({"message": "test"}),
headers: None,
},
enabled: true,
latency_ms: None,
status_code: Some(200),
request_match: None,
priority: None,
scenario: None,
required_scenario_state: None,
new_scenario_state: None,
};
let event = MockEvent::mock_created(mock);
// Should serialize successfully
let json = serde_json::to_string(&event).unwrap();
assert!(json.contains("mock_created"));
}
#[test]
fn test_broadcast_event() {
let state = WsManagementState::new();
let event = MockEvent::connected("Test connection".to_string());
// Should be able to send even with no subscribers
let result = state.broadcast(event);
// With no subscribers, broadcast returns Err (send count = 0)
assert!(result.is_err(), "expected broadcast to fail with no subscribers");
}
#[tokio::test]
async fn test_ws_management_router_creation() {
let state = WsManagementState::new();
let _router = ws_management_router(state);
// Router should be created successfully
}
}