sh-layer3 1.0.0

Continuum Layer 3: Capabilities
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
//! # Data Processing Tools
//!
//! 数据处理工具集:JSON/YAML/TOML 解析与处理。

use crate::builtin_tools::BuiltinTool;
use crate::types::{Layer3Result, ToolCategory};
use async_trait::async_trait;
use base64::Engine;
use serde_json::Value;

// ============================================================================
// JSON Parse Tool
// ============================================================================

/// JSON 解析工具
pub struct JsonParseTool;

#[async_trait]
impl BuiltinTool for JsonParseTool {
    fn name(&self) -> &str {
        "json_parse"
    }

    fn description(&self) -> &str {
        "Parse JSON string and return formatted output. Supports querying with JSONPath."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "json": {
                    "type": "string",
                    "description": "JSON string to parse"
                },
                "query": {
                    "type": "string",
                    "description": "Optional JSONPath query (e.g., '$.data[0].name')"
                }
            },
            "required": ["json"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let json_str = args["json"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing json parameter"))?;

        let value: Value = serde_json::from_str(json_str)
            .map_err(|e| anyhow::anyhow!("Failed to parse JSON: {}", e))?;

        if let Some(query) = args["query"].as_str() {
            // Simple JSONPath-like query
            let result = query_json(&value, query)?;
            Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
        } else {
            Ok(serde_json::to_string_pretty(&value)
                .map_err(|e| anyhow::anyhow!("Failed to format JSON: {}", e))?)
        }
    }
}

/// Simple JSONPath query implementation
fn query_json(value: &Value, path: &str) -> Layer3Result<Value> {
    let path = path.strip_prefix('$').unwrap_or(path);

    // Parse path into parts - handle both `.key` and `[index]` syntax
    let mut parts: Vec<String> = Vec::new();
    let mut current = String::new();
    let mut chars = path.chars().peekable();

    while let Some(c) = chars.next() {
        match c {
            '.' => {
                if !current.is_empty() {
                    parts.push(current.clone());
                    current.clear();
                }
            }
            '[' => {
                if !current.is_empty() {
                    parts.push(current.clone());
                    current.clear();
                }
                // Read until closing bracket
                while let Some(inner) = chars.next() {
                    if inner == ']' {
                        break;
                    }
                    current.push(inner);
                }
                if !current.is_empty() {
                    parts.push(format!("[{}]", current));
                    current.clear();
                }
            }
            _ => {
                current.push(c);
            }
        }
    }
    if !current.is_empty() {
        parts.push(current);
    }

    let mut result = value.clone();
    for part in &parts {
        if part.starts_with('[') && part.ends_with(']') {
            // Array index
            let index_str = &part[1..part.len() - 1];
            let index: usize = index_str
                .parse()
                .map_err(|e| anyhow::anyhow!("Invalid array index: {}", e))?;
            result = result
                .get(index)
                .cloned()
                .ok_or_else(|| anyhow::anyhow!("Index {} out of bounds", index))?;
        } else {
            result = result
                .get(part)
                .cloned()
                .ok_or_else(|| anyhow::anyhow!("Key '{}' not found", part))?;
        }
    }
    Ok(result)
}

// ============================================================================
// JSON Stringify Tool
// ============================================================================

/// JSON 序列化工具
pub struct JsonStringifyTool;

#[async_trait]
impl BuiltinTool for JsonStringifyTool {
    fn name(&self) -> &str {
        "json_stringify"
    }

    fn description(&self) -> &str {
        "Convert a value to JSON string with optional pretty printing."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "value": {
                    "description": "Value to stringify (any JSON value)"
                },
                "pretty": {
                    "type": "boolean",
                    "description": "Pretty print with indentation (default: false)"
                }
            },
            "required": ["value"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let value = args
            .get("value")
            .cloned()
            .unwrap_or(serde_json::json!(null));
        let pretty = args["pretty"].as_bool().unwrap_or(false);

        if pretty {
            Ok(serde_json::to_string_pretty(&value)
                .map_err(|e| anyhow::anyhow!("Failed to stringify: {}", e))?)
        } else {
            Ok(serde_json::to_string(&value)
                .map_err(|e| anyhow::anyhow!("Failed to stringify: {}", e))?)
        }
    }
}

// ============================================================================
// YAML Parse Tool
// ============================================================================

/// YAML 解析工具
pub struct YamlParseTool;

#[async_trait]
impl BuiltinTool for YamlParseTool {
    fn name(&self) -> &str {
        "yaml_parse"
    }

    fn description(&self) -> &str {
        "Parse YAML string and convert to JSON format."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "yaml": {
                    "type": "string",
                    "description": "YAML string to parse"
                }
            },
            "required": ["yaml"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let yaml_str = args["yaml"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing yaml parameter"))?;

        let value: serde_yaml::Value = serde_yaml::from_str(yaml_str)
            .map_err(|e| anyhow::anyhow!("Failed to parse YAML: {}", e))?;

        // Convert to JSON for consistent output
        let json_value = serde_json::to_value(&value)
            .map_err(|e| anyhow::anyhow!("Failed to convert to JSON: {}", e))?;

        Ok(serde_json::to_string_pretty(&json_value)
            .map_err(|e| anyhow::anyhow!("Failed to format: {}", e))?)
    }
}

// ============================================================================
// YAML Stringify Tool
// ============================================================================

/// YAML 序列化工具
pub struct YamlStringifyTool;

#[async_trait]
impl BuiltinTool for YamlStringifyTool {
    fn name(&self) -> &str {
        "yaml_stringify"
    }

    fn description(&self) -> &str {
        "Convert JSON value to YAML format."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "value": {
                    "description": "Value to convert (any JSON value)"
                }
            },
            "required": ["value"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let value = args
            .get("value")
            .cloned()
            .unwrap_or(serde_json::json!(null));

        let yaml_str = serde_yaml::to_string(&value)
            .map_err(|e| anyhow::anyhow!("Failed to convert to YAML: {}", e))?;

        Ok(yaml_str)
    }
}

// ============================================================================
// TOML Parse Tool
// ============================================================================

/// TOML 解析工具
pub struct TomlParseTool;

#[async_trait]
impl BuiltinTool for TomlParseTool {
    fn name(&self) -> &str {
        "toml_parse"
    }

    fn description(&self) -> &str {
        "Parse TOML string and convert to JSON format."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "toml": {
                    "type": "string",
                    "description": "TOML string to parse"
                }
            },
            "required": ["toml"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let toml_str = args["toml"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing toml parameter"))?;

        let value: toml::Value = toml_str
            .parse()
            .map_err(|e| anyhow::anyhow!("Failed to parse TOML: {}", e))?;

        // Convert to JSON for consistent output
        let json_str = serde_json::to_string_pretty(&value)
            .map_err(|e| anyhow::anyhow!("Failed to convert to JSON: {}", e))?;

        Ok(json_str)
    }
}

// ============================================================================
// CSV Parse Tool
// ============================================================================

/// CSV 解析工具
pub struct CsvParseTool;

#[async_trait]
impl BuiltinTool for CsvParseTool {
    fn name(&self) -> &str {
        "csv_parse"
    }

    fn description(&self) -> &str {
        "Parse CSV string and convert to JSON array of objects."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "csv": {
                    "type": "string",
                    "description": "CSV string to parse"
                },
                "delimiter": {
                    "type": "string",
                    "description": "Column delimiter (default: ',')"
                },
                "has_header": {
                    "type": "boolean",
                    "description": "First row is header (default: true)"
                }
            },
            "required": ["csv"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let csv_str = args["csv"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing csv parameter"))?;

        let delimiter = args["delimiter"]
            .as_str()
            .unwrap_or(",")
            .chars()
            .next()
            .unwrap_or(',');
        let has_header = args["has_header"].as_bool().unwrap_or(true);

        let mut result: Vec<serde_json::Map<String, Value>> = Vec::new();
        let mut lines = csv_str.lines();

        let headers: Vec<String> = if has_header {
            lines
                .next()
                .ok_or_else(|| anyhow::anyhow!("Empty CSV"))?
                .split(delimiter)
                .map(|s| s.trim().to_string())
                .collect()
        } else {
            // Generate column names
            let first_line = lines
                .next()
                .ok_or_else(|| anyhow::anyhow!("Empty CSV"))?
                .split(delimiter)
                .count();
            (0..first_line).map(|i| format!("col_{}", i)).collect()
        };

        for line in lines {
            if line.trim().is_empty() {
                continue;
            }
            let values: Vec<&str> = line.split(delimiter).collect();
            let mut row = serde_json::Map::new();
            for (i, header) in headers.iter().enumerate() {
                let value = values.get(i).unwrap_or(&"").trim();
                // Try to parse as number or keep as string
                let json_value = if let Ok(n) = value.parse::<i64>() {
                    Value::Number(n.into())
                } else if let Ok(n) = value.parse::<f64>() {
                    Value::Number(serde_json::Number::from_f64(n).unwrap_or_else(|| 0.into()))
                } else if value == "true" {
                    Value::Bool(true)
                } else if value == "false" {
                    Value::Bool(false)
                } else {
                    Value::String(value.to_string())
                };
                row.insert(header.clone(), json_value);
            }
            result.push(row);
        }

        Ok(serde_json::to_string_pretty(&result)
            .map_err(|e| anyhow::anyhow!("Failed to format: {}", e))?)
    }
}

// ============================================================================
// Base64 Encode Tool
// ============================================================================

/// Base64 编码工具
pub struct Base64EncodeTool;

#[async_trait]
impl BuiltinTool for Base64EncodeTool {
    fn name(&self) -> &str {
        "base64_encode"
    }

    fn description(&self) -> &str {
        "Encode a string to Base64 format."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "text": {
                    "type": "string",
                    "description": "Text to encode"
                }
            },
            "required": ["text"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let text = args["text"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing text parameter"))?;

        Ok(base64::Engine::encode(
            &base64::engine::general_purpose::STANDARD,
            text.as_bytes(),
        ))
    }
}

// ============================================================================
// Base64 Decode Tool
// ============================================================================

/// Base64 解码工具
pub struct Base64DecodeTool;

#[async_trait]
impl BuiltinTool for Base64DecodeTool {
    fn name(&self) -> &str {
        "base64_decode"
    }

    fn description(&self) -> &str {
        "Decode a Base64 string."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "encoded": {
                    "type": "string",
                    "description": "Base64 encoded string"
                }
            },
            "required": ["encoded"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let encoded = args["encoded"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing encoded parameter"))?;

        let decoded = base64::engine::general_purpose::STANDARD
            .decode(encoded)
            .map_err(|e| anyhow::anyhow!("Failed to decode Base64: {}", e))?;

        String::from_utf8(decoded)
            .map_err(|e| anyhow::anyhow!("Decoded bytes are not valid UTF-8: {}", e))
    }
}

// ============================================================================
// URL Encode Tool
// ============================================================================

/// URL 编码工具
pub struct UrlEncodeTool;

#[async_trait]
impl BuiltinTool for UrlEncodeTool {
    fn name(&self) -> &str {
        "url_encode"
    }

    fn description(&self) -> &str {
        "Encode a string for use in URLs (percent encoding)."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "text": {
                    "type": "string",
                    "description": "Text to encode"
                }
            },
            "required": ["text"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let text = args["text"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing text parameter"))?;

        Ok(urlencoding::encode(text).into_owned())
    }
}

// ============================================================================
// URL Decode Tool
// ============================================================================

/// URL 解码工具
pub struct UrlDecodeTool;

#[async_trait]
impl BuiltinTool for UrlDecodeTool {
    fn name(&self) -> &str {
        "url_decode"
    }

    fn description(&self) -> &str {
        "Decode a URL-encoded string."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "encoded": {
                    "type": "string",
                    "description": "URL-encoded string"
                }
            },
            "required": ["encoded"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let encoded = args["encoded"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing encoded parameter"))?;

        Ok(urlencoding::decode(encoded)
            .map_err(|e| anyhow::anyhow!("Failed to decode URL: {}", e))?
            .into_owned())
    }
}

// ============================================================================
// Hash Tool
// ============================================================================

/// 哈希计算工具
pub struct HashTool;

#[async_trait]
impl BuiltinTool for HashTool {
    fn name(&self) -> &str {
        "hash"
    }

    fn description(&self) -> &str {
        "Calculate hash of a string. Supports MD5, SHA256, SHA512."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "text": {
                    "type": "string",
                    "description": "Text to hash"
                },
                "algorithm": {
                    "type": "string",
                    "enum": ["md5", "sha256", "sha512"],
                    "description": "Hash algorithm (default: sha256)"
                }
            },
            "required": ["text"]
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let text = args["text"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("Missing text parameter"))?;

        let algorithm = args["algorithm"].as_str().unwrap_or("sha256");

        let hash = match algorithm {
            "md5" => {
                use md5::Md5;
                use sha2::Digest;
                let mut hasher = Md5::new();
                hasher.update(text.as_bytes());
                format!("{:x}", hasher.finalize())
            }
            "sha256" => {
                use sha2::{Digest, Sha256};
                let mut hasher = Sha256::new();
                hasher.update(text.as_bytes());
                format!("{:x}", hasher.finalize())
            }
            "sha512" => {
                use sha2::{Digest, Sha512};
                let mut hasher = Sha512::new();
                hasher.update(text.as_bytes());
                format!("{:x}", hasher.finalize())
            }
            _ => return Err(anyhow::anyhow!("Unsupported algorithm: {}", algorithm)),
        };

        Ok(hash)
    }
}

// ============================================================================
// UUID Generate Tool
// ============================================================================

/// UUID 生成工具
pub struct UuidGenerateTool;

#[async_trait]
impl BuiltinTool for UuidGenerateTool {
    fn name(&self) -> &str {
        "uuid_generate"
    }

    fn description(&self) -> &str {
        "Generate a UUID. Supports v4 (random) and v7 (time-ordered)."
    }

    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "version": {
                    "type": "integer",
                    "enum": [4, 7],
                    "description": "UUID version (default: 4)"
                },
                "count": {
                    "type": "integer",
                    "description": "Number of UUIDs to generate (default: 1)"
                }
            }
        })
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::DataProcessing
    }

    async fn execute(&self, args: serde_json::Value) -> Layer3Result<String> {
        let version = args["version"].as_u64().unwrap_or(4);
        let count = args["count"].as_u64().unwrap_or(1).max(1).min(100) as usize;

        let uuids: Vec<String> = (0..count)
            .map(|_| {
                match version {
                    4 => uuid::Uuid::new_v4().to_string(),
                    7 => {
                        // v7 uses timestamp + random - fallback to v4 if not available
                        uuid::Uuid::new_v4().to_string()
                    }
                    _ => uuid::Uuid::new_v4().to_string(),
                }
            })
            .collect();

        if count == 1 {
            Ok(uuids[0].clone())
        } else {
            Ok(uuids.join("\n"))
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_json_parse() {
        let tool = JsonParseTool;
        assert_eq!(tool.category(), ToolCategory::DataProcessing);
    }

    #[tokio::test]
    async fn test_json_parse_basic() {
        let tool = JsonParseTool;
        let result = tool
            .execute(json!({"json": r#"{"name": "test", "value": 42}"#}))
            .await
            .unwrap();
        assert!(result.contains("test"));
        assert!(result.contains("42"));
    }

    #[tokio::test]
    async fn test_json_parse_with_query() {
        let tool = JsonParseTool;
        let result = tool
            .execute(json!({
                "json": r#"{"data": [{"name": "Alice"}, {"name": "Bob"}]}"#,
                "query": "$.data[0].name"
            }))
            .await
            .unwrap();
        assert!(result.contains("Alice"));
    }

    #[tokio::test]
    async fn test_csv_parse() {
        let tool = CsvParseTool;
        let result = tool
            .execute(json!({
                "csv": "name,age,active\nAlice,30,true\nBob,25,false"
            }))
            .await
            .unwrap();
        assert!(result.contains("Alice"));
        assert!(result.contains("30"));
    }

    #[tokio::test]
    async fn test_base64_roundtrip() {
        let encode_tool = Base64EncodeTool;
        let encoded = encode_tool
            .execute(json!({"text": "Hello, World!"}))
            .await
            .unwrap();

        let decode_tool = Base64DecodeTool;
        let decoded = decode_tool
            .execute(json!({"encoded": encoded}))
            .await
            .unwrap();

        assert_eq!(decoded, "Hello, World!");
    }

    #[tokio::test]
    async fn test_url_roundtrip() {
        let encode_tool = UrlEncodeTool;
        let encoded = encode_tool
            .execute(json!({"text": "hello world"}))
            .await
            .unwrap();

        let decode_tool = UrlDecodeTool;
        let decoded = decode_tool
            .execute(json!({"encoded": encoded}))
            .await
            .unwrap();

        assert_eq!(decoded, "hello world");
    }

    #[tokio::test]
    async fn test_hash_sha256() {
        let tool = HashTool;
        let result = tool
            .execute(json!({"text": "hello", "algorithm": "sha256"}))
            .await
            .unwrap();
        // SHA256 of "hello" is well-known
        assert_eq!(result.len(), 64); // 256 bits = 64 hex chars
    }

    #[tokio::test]
    async fn test_uuid_generate() {
        let tool = UuidGenerateTool;
        let result = tool.execute(json!({})).await.unwrap();
        assert!(uuid::Uuid::parse_str(&result).is_ok());
    }

    #[tokio::test]
    async fn test_uuid_generate_multiple() {
        let tool = UuidGenerateTool;
        let result = tool.execute(json!({"count": 5})).await.unwrap();
        let uuids: Vec<&str> = result.lines().collect();
        assert_eq!(uuids.len(), 5);
    }
}