qudag-cli 0.5.0

Command-line interface for QuDAG - Manage nodes, peers, dark addresses, rUv token exchange, and business plan payouts
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
//! Tests for CLI commands using mock implementations
//!
//! NOTE: This test file is disabled during RED phase of TDD
//! These tests require mock implementations that will be created in GREEN phase

#[cfg(disabled_until_green_phase)]
mod cli_command_tests {
    use std::process::Command;
    use std::str;
    use std::sync::Arc;

    /// Test helper to run CLI commands
    async fn run_cli_command(args: &[&str]) -> Result<String, Box<dyn std::error::Error>> {
        let output = Command::new("cargo")
            .args(&["run", "--bin", "qudag", "--"])
            .args(args)
            .output()?;

        if output.status.success() {
            Ok(String::from_utf8(output.stdout)?)
        } else {
            Err(format!(
                "Command failed: {}",
                String::from_utf8_lossy(&output.stderr)
            )
            .into())
        }
    }

    #[tokio::test]
    async fn test_cli_start_command() {
        // This test would typically use the mock RPC server
        // For now, we test the command parsing and output format

        let scenario = TestScenarioBuilder::new()
            .add_node("test-cli-node".to_string())
            .build()
            .await;

        let node = scenario.nodes.get("test-cli-node").unwrap();

        // Test node start via mock
        node.start().await.expect("Failed to start node");
        let status = node.get_status().await;
        assert_eq!(status.state, "Running");

        // In a real scenario, we would verify the CLI output format
        println!("Node started successfully:");
        println!("  ID: {}", status.node_id);
        println!("  State: {}", status.state);
        println!("  Uptime: {} seconds", status.uptime);
    }

    #[tokio::test]
    async fn test_cli_peer_management() {
        let scenario = TestScenarioBuilder::new()
            .add_node("peer-test-node".to_string())
            .build()
            .await;

        let node = scenario.nodes.get("peer-test-node").unwrap();
        node.start().await.expect("Failed to start node");

        // Test peer addition
        node.add_peer("192.168.1.50:8080".to_string())
            .await
            .expect("Failed to add peer");

        let status = node.get_status().await;
        assert_eq!(status.peers.len(), 1);
        assert_eq!(status.peers[0].address, "192.168.1.50:8080");

        // Test peer removal
        let peer_id = status.peers[0].id.clone();
        node.remove_peer(&peer_id)
            .await
            .expect("Failed to remove peer");

        let status = node.get_status().await;
        assert_eq!(status.peers.len(), 0);

        println!("Peer management test completed successfully");
    }

    #[tokio::test]
    async fn test_cli_network_stats() {
        let scenario = TestScenarioBuilder::new()
            .add_node("stats-node".to_string())
            .build()
            .await;

        let node = scenario.nodes.get("stats-node").unwrap();
        node.start().await.expect("Failed to start node");

        // Add some peers and simulate activity
        node.add_peer("10.0.0.1:8080".to_string())
            .await
            .expect("Failed to add peer");
        node.add_peer("10.0.0.2:8080".to_string())
            .await
            .expect("Failed to add peer");

        node.simulate_activity().await;

        let status = node.get_status().await;
        let stats = &status.network_stats;

        // Verify network statistics format
        println!("Network Statistics:");
        println!("==================");
        println!("  Total Connections:    {}", stats.total_connections);
        println!("  Active Connections:   {}", stats.active_connections);
        println!("  Messages Sent:        {}", stats.messages_sent);
        println!("  Messages Received:    {}", stats.messages_received);
        println!("  Bytes Sent:           {}", stats.bytes_sent);
        println!("  Bytes Received:       {}", stats.bytes_received);
        println!("  Average Latency:      {:.2} ms", stats.average_latency);

        assert!(stats.active_connections > 0);
        assert!(stats.messages_sent > 0);
        assert!(stats.average_latency > 0.0);
    }

    #[tokio::test]
    async fn test_cli_network_test_command() {
        let scenario = TestScenarioBuilder::new()
            .add_node("network-test-node".to_string())
            .build()
            .await;

        let node = scenario.nodes.get("network-test-node").unwrap();
        let rpc = MockRpcClient::new(node.clone());

        // Simulate network test via RPC
        let request = qudag_cli::rpc::RpcRequest {
            id: uuid::Uuid::new_v4(),
            method: "test_network".to_string(),
            params: serde_json::Value::Null,
        };

        let response = rpc.process_request(request).await;
        assert!(response.result.is_some());

        if let Some(result) = response.result {
            let test_results: Vec<qudag_cli::rpc::NetworkTestResult> =
                serde_json::from_value(result).unwrap();

            println!("Network Test Results:");
            println!("====================");

            for result in &test_results {
                println!("  Peer ID: {}", result.peer_id);
                println!("  Address: {}", result.address);

                if result.reachable {
                    println!("  Status: ✓ Reachable");
                    if let Some(latency) = result.latency {
                        println!("  Latency: {:.1} ms", latency);
                    }
                } else {
                    println!("  Status: ✗ Unreachable");
                    if let Some(error) = &result.error {
                        println!("  Error: {}", error);
                    }
                }
                println!();
            }

            assert_eq!(test_results.len(), 3);
            assert!(test_results.iter().any(|r| r.reachable));
            assert!(test_results.iter().any(|r| !r.reachable));
        }
    }

    #[tokio::test]
    async fn test_cli_status_command() {
        let scenario = TestScenarioBuilder::new()
            .add_node("status-node".to_string())
            .build()
            .await;

        let node = scenario.nodes.get("status-node").unwrap();
        node.start().await.expect("Failed to start node");

        // Add some test data
        node.add_peer("peer1.example.com:8080".to_string())
            .await
            .expect("Failed to add peer");
        node.simulate_activity().await;

        let status = node.get_status().await;

        // Test status output format
        println!("Node Status:");
        println!("============");
        println!("  Node ID: {}", status.node_id);
        println!("  State: {}", status.state);
        println!("  Uptime: {} seconds", status.uptime);
        println!("  Connected Peers: {}", status.peers.len());
        println!();

        if !status.peers.is_empty() {
            println!("Peer Information:");
            for peer in &status.peers {
                println!("  - ID: {}", peer.id);
                println!("    Address: {}", peer.address);
                println!("    Connected: {} seconds", peer.connected_duration);
                println!("    Messages Sent: {}", peer.messages_sent);
                println!("    Messages Received: {}", peer.messages_received);
            }
            println!();
        }

        println!("DAG Statistics:");
        println!("  Vertices: {}", status.dag_stats.vertex_count);
        println!("  Edges: {}", status.dag_stats.edge_count);
        println!("  Tips: {}", status.dag_stats.tip_count);
        println!("  Finalized Height: {}", status.dag_stats.finalized_height);
        println!(
            "  Pending Transactions: {}",
            status.dag_stats.pending_transactions
        );
        println!();

        println!("Memory Usage:");
        println!("  Current: {} bytes", status.memory_usage.current_usage);
        println!("  Peak: {} bytes", status.memory_usage.peak_usage);
        println!(
            "  Total Allocated: {} bytes",
            status.memory_usage.total_allocated
        );

        assert_eq!(status.state, "Running");
        assert_eq!(status.peers.len(), 1);
    }

    #[tokio::test]
    async fn test_cli_error_scenarios() {
        let scenario = TestScenarioBuilder::new()
            .add_node("error-test-node".to_string())
            .with_global_behavior(
                "problematic_operation".to_string(),
                MockBehavior {
                    should_succeed: false,
                    latency_ms: 0,
                    error_message: "Service temporarily unavailable".to_string(),
                    custom_response: None,
                },
            )
            .build()
            .await;

        let node = scenario.nodes.get("error-test-node").unwrap();
        let rpc = MockRpcClient::new(node.clone());

        // Test error handling
        let request = qudag_cli::rpc::RpcRequest {
            id: uuid::Uuid::new_v4(),
            method: "problematic_operation".to_string(),
            params: serde_json::Value::Null,
        };

        let response = rpc.process_request(request).await;
        assert!(response.result.is_none());
        assert!(response.error.is_some());

        if let Some(error) = response.error {
            println!("Error response received:");
            println!("  Code: {}", error.code);
            println!("  Message: {}", error.message);
            assert_eq!(error.message, "Service temporarily unavailable");
        }

        // Test double start error
        node.start().await.expect("Failed to start node");
        let result = node.start().await;
        assert!(result.is_err());

        println!("Double start error: {}", result.unwrap_err());
    }

    #[tokio::test]
    async fn test_cli_concurrent_operations() {
        let scenario = TestScenarioBuilder::new()
            .add_node("concurrent-node".to_string())
            .build()
            .await;

        let node = scenario.nodes.get("concurrent-node").unwrap();
        let rpc = Arc::new(MockRpcClient::new(node.clone()));

        node.start().await.expect("Failed to start node");

        // Test concurrent RPC requests
        let mut handles = Vec::new();

        for i in 0..10 {
            let rpc_clone = rpc.clone();
            let handle = tokio::spawn(async move {
                let request = qudag_cli::rpc::RpcRequest {
                    id: uuid::Uuid::new_v4(),
                    method: "get_status".to_string(),
                    params: serde_json::Value::Null,
                };

                let response = rpc_clone.process_request(request).await;
                (i, response.result.is_some())
            });

            handles.push(handle);
        }

        // Wait for all requests to complete
        let results = futures::future::join_all(handles).await;

        // Verify all requests succeeded
        for result in results {
            let (i, success) = result.expect("Task panicked");
            assert!(success, "Request {} failed", i);
        }

        // Check request history
        let history = rpc.get_request_history();
        assert_eq!(history.len(), 10);

        println!(
            "Successfully processed {} concurrent requests",
            history.len()
        );
    }

    #[tokio::test]
    async fn test_cli_complex_scenario() {
        // Create a complex scenario with multiple nodes and various behaviors
        let scenario = TestScenarioBuilder::new()
            .add_node("gateway".to_string())
            .add_node("worker-1".to_string())
            .add_node("worker-2".to_string())
            .add_node("monitor".to_string())
            .with_topology(NetworkTopology::Star {
                center: "gateway".to_string(),
            })
            .with_global_behavior(
                "heartbeat".to_string(),
                MockBehavior {
                    should_succeed: true,
                    latency_ms: 5,
                    error_message: String::new(),
                    custom_response: Some(serde_json::json!({
                        "timestamp": chrono::Utc::now().timestamp(),
                        "status": "healthy"
                    })),
                },
            )
            .build()
            .await;

        // Start all nodes
        scenario
            .start_all_nodes()
            .await
            .expect("Failed to start nodes");

        // Simulate network activity for a short period
        let activity_duration = std::time::Duration::from_millis(500);
        scenario.simulate_activity(activity_duration).await;

        // Get aggregate statistics
        let stats = scenario.get_aggregate_stats().await;

        println!("Complex Scenario Results:");
        println!("========================");
        println!("  Nodes: {}", scenario.nodes.len());
        println!("  Total Connections: {}", stats.total_connections);
        println!("  Active Connections: {}", stats.active_connections);
        println!(
            "  Messages Exchanged: {}",
            stats.messages_sent + stats.messages_received
        );
        println!(
            "  Data Transferred: {} KB",
            (stats.bytes_sent + stats.bytes_received) / 1024
        );
        println!("  Average Latency: {:.2} ms", stats.average_latency);

        // Verify star topology
        let gateway = scenario.nodes.get("gateway").unwrap();
        let gateway_peers = gateway.peers.read().await.len();
        assert_eq!(gateway_peers, 3, "Gateway should have 3 peer connections");

        for worker in ["worker-1", "worker-2", "monitor"] {
            let node = scenario.nodes.get(worker).unwrap();
            let peer_count = node.peers.read().await.len();
            assert_eq!(
                peer_count, 1,
                "{} should have 1 peer connection to gateway",
                worker
            );
        }

        // Test RPC operations on each node
        for (node_id, node) in &scenario.nodes {
            let rpc = MockRpcClient::new(node.clone());

            let status_request = qudag_cli::rpc::RpcRequest {
                id: uuid::Uuid::new_v4(),
                method: "get_status".to_string(),
                params: serde_json::Value::Null,
            };

            let response = rpc.process_request(status_request).await;
            assert!(
                response.result.is_some(),
                "Status request failed for {}",
                node_id
            );

            let heartbeat_request = qudag_cli::rpc::RpcRequest {
                id: uuid::Uuid::new_v4(),
                method: "heartbeat".to_string(),
                params: serde_json::Value::Null,
            };

            let response = rpc.process_request(heartbeat_request).await;
            assert!(
                response.result.is_some(),
                "Heartbeat failed for {}",
                node_id
            );

            if let Some(result) = response.result {
                assert_eq!(result["status"], "healthy");
            }
        }

        scenario
            .stop_all_nodes()
            .await
            .expect("Failed to stop nodes");

        println!("Complex scenario completed successfully");
    }
} // End of disabled cli_command_tests module}