ultimo 0.4.0

Modern Rust web framework with automatic TypeScript client generation
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
//! RPC (Remote Procedure Call) system for Ultimo
//!
//! Provides type-safe RPC functionality with automatic TypeScript client generation.

use crate::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

/// RPC mode determines how procedures are exposed as HTTP endpoints
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RpcMode {
    /// Single JSON-RPC endpoint: POST /rpc with {"method": "...", "params": {}}
    ///
    /// Best for:
    /// - Internal APIs and microservices
    /// - When you need request batching
    /// - Simple routing requirements
    /// - When all RPCs need same middleware
    #[default]
    JsonRpc,

    /// Individual REST-like endpoints: GET /api/getUser, POST /api/createUser
    ///
    /// Best for:
    /// - Public APIs
    /// - When debugging is important (clear URLs in Network tab)
    /// - When you need HTTP caching (GET requests)
    /// - RESTful conventions
    Rest,
}

/// RPC procedure handler
pub type RpcHandler = Arc<dyn Fn(serde_json::Value) -> RpcHandlerFuture + Send + Sync>;

/// Future returned by RPC handlers
pub type RpcHandlerFuture =
    std::pin::Pin<Box<dyn std::future::Future<Output = Result<serde_json::Value>> + Send>>;

/// RPC registry for managing procedures
#[derive(Clone)]
pub struct RpcRegistry {
    mode: RpcMode,
    procedures: Arc<std::sync::Mutex<HashMap<String, RpcHandler>>>,
    type_definitions: Arc<std::sync::Mutex<Vec<TypeDefinition>>>,
    metadata: Arc<std::sync::Mutex<HashMap<String, ProcedureMetadata>>>,
}

/// Type definition for TypeScript generation
#[derive(Debug, Clone, Serialize)]
pub struct TypeDefinition {
    pub name: String,
    pub input_type: String,
    pub output_type: String,
    pub ts_input: String,
    pub ts_output: String,
}

/// Metadata about an RPC procedure
#[derive(Debug, Clone)]
pub struct ProcedureMetadata {
    pub name: String,
    pub is_query: bool, // true = GET (idempotent), false = POST (mutation)
}

impl RpcRegistry {
    /// Create a new RPC registry with default JsonRpc mode
    pub fn new() -> Self {
        Self::new_with_mode(RpcMode::JsonRpc)
    }

    /// Create a new RPC registry with specified mode
    pub fn new_with_mode(mode: RpcMode) -> Self {
        Self {
            mode,
            procedures: Arc::new(std::sync::Mutex::new(HashMap::new())),
            type_definitions: Arc::new(std::sync::Mutex::new(Vec::new())),
            metadata: Arc::new(std::sync::Mutex::new(HashMap::new())),
        }
    }

    /// Get the current RPC mode
    pub fn mode(&self) -> RpcMode {
        self.mode
    }

    /// Register an RPC procedure with optional TypeScript type information
    pub fn register<F, Fut, I, O>(&self, name: impl Into<String>, handler: F)
    where
        F: Fn(I) -> Fut + Send + Sync + Clone + 'static,
        Fut: std::future::Future<Output = Result<O>> + Send + 'static,
        I: for<'de> Deserialize<'de> + 'static,
        O: Serialize + 'static,
    {
        self.register_with_types(name, handler, "any".to_string(), "any".to_string())
    }

    /// Register an RPC procedure with explicit TypeScript types
    pub fn register_with_types<F, Fut, I, O>(
        &self,
        name: impl Into<String>,
        handler: F,
        ts_input: String,
        ts_output: String,
    ) where
        F: Fn(I) -> Fut + Send + Sync + Clone + 'static,
        Fut: std::future::Future<Output = Result<O>> + Send + 'static,
        I: for<'de> Deserialize<'de> + 'static,
        O: Serialize + 'static,
    {
        self.procedure(name, handler, ts_input, ts_output, false)
    }

    /// Register a query procedure (idempotent, uses GET in REST mode)
    pub fn query<F, Fut, I, O>(
        &self,
        name: impl Into<String>,
        handler: F,
        ts_input: String,
        ts_output: String,
    ) where
        F: Fn(I) -> Fut + Send + Sync + Clone + 'static,
        Fut: std::future::Future<Output = Result<O>> + Send + 'static,
        I: for<'de> Deserialize<'de> + 'static,
        O: Serialize + 'static,
    {
        self.procedure(name, handler, ts_input, ts_output, true)
    }

    /// Register a mutation procedure (state-changing, uses POST in REST mode)
    pub fn mutation<F, Fut, I, O>(
        &self,
        name: impl Into<String>,
        handler: F,
        ts_input: String,
        ts_output: String,
    ) where
        F: Fn(I) -> Fut + Send + Sync + Clone + 'static,
        Fut: std::future::Future<Output = Result<O>> + Send + 'static,
        I: for<'de> Deserialize<'de> + 'static,
        O: Serialize + 'static,
    {
        self.procedure(name, handler, ts_input, ts_output, false)
    }

    /// Internal method to register with options
    fn procedure<F, Fut, I, O>(
        &self,
        name: impl Into<String>,
        handler: F,
        ts_input: String,
        ts_output: String,
        is_query: bool,
    ) where
        F: Fn(I) -> Fut + Send + Sync + Clone + 'static,
        Fut: std::future::Future<Output = Result<O>> + Send + 'static,
        I: for<'de> Deserialize<'de> + 'static,
        O: Serialize + 'static,
    {
        let name = name.into();
        let name_clone = name.clone();

        let wrapped_handler: RpcHandler = Arc::new(move |input| {
            let handler = handler.clone();
            Box::pin(async move {
                let input: I = serde_json::from_value(input)
                    .map_err(|e| crate::UltimoError::BadRequest(format!("Invalid input: {}", e)))?;
                let output = handler(input).await?;
                serde_json::to_value(output).map_err(|e| {
                    crate::UltimoError::Internal(format!("Serialization error: {}", e))
                })
            })
        });

        self.procedures
            .lock()
            .unwrap()
            .insert(name.clone(), wrapped_handler);

        // Store type definition metadata
        let type_def = TypeDefinition {
            name: name_clone.clone(),
            input_type: std::any::type_name::<I>().to_string(),
            output_type: std::any::type_name::<O>().to_string(),
            ts_input,
            ts_output,
        };

        self.type_definitions.lock().unwrap().push(type_def);

        // Store procedure metadata
        let metadata = ProcedureMetadata {
            name: name_clone.clone(),
            is_query,
        };

        self.metadata.lock().unwrap().insert(name, metadata);
    }

    /// Call an RPC procedure
    pub async fn call(&self, name: &str, input: serde_json::Value) -> Result<serde_json::Value> {
        let handler = {
            let procedures = self.procedures.lock().unwrap();
            procedures
                .get(name)
                .ok_or_else(|| {
                    crate::UltimoError::NotFound(format!("Procedure '{}' not found", name))
                })?
                .clone()
        };

        handler(input).await
    }

    /// Get all registered procedure names
    pub fn list_procedures(&self) -> Vec<String> {
        self.procedures.lock().unwrap().keys().cloned().collect()
    }

    /// Generate TypeScript client code
    pub fn generate_typescript_client(&self) -> String {
        match self.mode {
            RpcMode::JsonRpc => self.generate_json_rpc_client(),
            RpcMode::Rest => self.generate_rest_client(),
        }
    }

    /// Generate JSON-RPC style client (single endpoint)
    fn generate_json_rpc_client(&self) -> String {
        let type_defs = self.type_definitions.lock().unwrap();

        let mut client = String::from(
            r#"// Auto-generated TypeScript client for Ultimo RPC (JSON-RPC Mode)
// DO NOT EDIT - This file is automatically generated

export class UltimoRpcClient {
  constructor(private baseUrl: string = '/api/rpc') {}

  private async call<T>(method: string, params: any): Promise<T> {
    const response = await fetch(this.baseUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ method, params }),
    });

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || 'RPC call failed');
    }

    const data = await response.json();
    return data.result;
  }

"#,
        );

        // Generate method for each procedure
        for def in type_defs.iter() {
            client.push_str(&format!(
                r#"  async {}(params: {}): Promise<{}> {{
    return this.call('{}', params);
  }}

"#,
                def.name, def.ts_input, def.ts_output, def.name
            ));
        }

        client.push_str("}\n");
        self.append_type_definitions(&mut client);
        client
    }

    /// Generate REST style client (individual endpoints)
    fn generate_rest_client(&self) -> String {
        let type_defs = self.type_definitions.lock().unwrap();
        let metadata = self.metadata.lock().unwrap();

        let mut client = String::from(
            r#"// Auto-generated TypeScript client for Ultimo RPC (REST Mode)
// DO NOT EDIT - This file is automatically generated

export class UltimoRpcClient {
  constructor(private baseUrl: string = '/api') {}

  private async get<T>(path: string, params?: Record<string, any>): Promise<T> {
    const url = new URL(this.baseUrl + path, window.location.origin);
    if (params) {
      Object.entries(params).forEach(([key, value]) => {
        url.searchParams.append(key, String(value));
      });
    }

    const response = await fetch(url.toString(), {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    });

    if (!response.ok) {
      const error = await response.json().catch(() => ({ message: response.statusText }));
      throw new Error(error.message || 'Request failed');
    }

    return response.json();
  }

  private async post<T>(path: string, body: any): Promise<T> {
    const response = await fetch(this.baseUrl + path, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(body),
    });

    if (!response.ok) {
      const error = await response.json().catch(() => ({ message: response.statusText }));
      throw new Error(error.message || 'Request failed');
    }

    return response.json();
  }

"#,
        );

        // Generate method for each procedure
        for def in type_defs.iter() {
            let meta = metadata.get(&def.name);
            let is_query = meta.map(|m| m.is_query).unwrap_or(false);

            if is_query {
                // Query: Use GET
                client.push_str(&format!(
                    r#"  async {}(params: {}): Promise<{}> {{
    return this.get('/{}', params);
  }}

"#,
                    def.name, def.ts_input, def.ts_output, def.name,
                ));
            } else {
                // Mutation: Use POST
                client.push_str(&format!(
                    r#"  async {}(params: {}): Promise<{}> {{
    return this.post('/{}'{}params);
  }}

"#,
                    def.name,
                    def.ts_input,
                    def.ts_output,
                    def.name,
                    if def.ts_input == "{}" { "" } else { ", " }
                ));
            }
        }

        client.push_str("}\n");
        self.append_type_definitions(&mut client);
        client
    }

    /// Append type definitions to generated client
    fn append_type_definitions(&self, client: &mut String) {
        client.push_str("\n// Type Definitions\n");
        client.push_str("export interface User {\n");
        client.push_str("  id: number;\n");
        client.push_str("  name: string;\n");
        client.push_str("  email: string;\n");
        client.push_str("}\n");
    }

    /// Generate TypeScript client and save to file
    pub fn generate_client_file(&self, output_path: &str) -> std::io::Result<()> {
        let client_code = self.generate_typescript_client();
        std::fs::write(output_path, client_code)?;
        Ok(())
    }

    /// Generate OpenAPI specification from RPC registry
    ///
    /// # Arguments
    /// * `title` - API title
    /// * `version` - API version
    /// * `base_path` - Base path for REST mode endpoints (e.g., "/api")
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use ultimo::rpc::{RpcRegistry, RpcMode};
    ///
    /// let rpc = RpcRegistry::new_with_mode(RpcMode::Rest);
    /// // ... register procedures ...
    ///
    /// let spec = rpc.generate_openapi("My API", "1.0.0", "/api");
    /// spec.write_to_file("openapi.json").unwrap();
    /// ```
    pub fn generate_openapi(
        &self,
        title: &str,
        version: &str,
        base_path: &str,
    ) -> crate::openapi::OpenApiSpec {
        use crate::openapi::{
            MediaType, OpenApiBuilder, Operation, Parameter, ParameterLocation, PathItem,
            RequestBody, Response,
        };
        use std::collections::HashMap;

        let mut spec = OpenApiBuilder::new()
            .title(title)
            .version(version)
            .server(
                "http://localhost:3000".to_string(),
                Some("Development server".to_string()),
            )
            .build();

        let type_defs = self.type_definitions.lock().unwrap();
        let metadata = self.metadata.lock().unwrap();

        match self.mode {
            RpcMode::Rest => {
                // Generate individual REST endpoints
                for type_def in type_defs.iter() {
                    let proc_metadata = metadata.get(&type_def.name);
                    let is_query = proc_metadata.map(|m| m.is_query).unwrap_or(false);

                    let path = format!("{}/{}", base_path, type_def.name);
                    let method = if is_query { "GET" } else { "POST" };

                    // Create operation
                    let mut operation = Operation {
                        summary: Some(format!("{} {}", method, type_def.name)),
                        description: None,
                        operation_id: Some(type_def.name.clone()),
                        tags: Some(vec!["RPC".to_string()]),
                        parameters: None,
                        request_body: None,
                        responses: HashMap::new(),
                    };

                    // Add response
                    let response_schema =
                        crate::openapi::OpenApiSpec::ts_to_schema(&type_def.ts_output);
                    let mut content = HashMap::new();
                    content.insert(
                        "application/json".to_string(),
                        MediaType {
                            schema: response_schema,
                            example: None,
                        },
                    );

                    operation.responses.insert(
                        "200".to_string(),
                        Response {
                            description: "Successful response".to_string(),
                            content: Some(content),
                        },
                    );

                    // Add request body for POST or query params for GET
                    if is_query {
                        // GET: Add query parameters if input type is object
                        if type_def.ts_input != "{}" {
                            let _param_schema =
                                crate::openapi::OpenApiSpec::ts_to_schema(&type_def.ts_input);
                            let mut parameters = vec![];

                            // Simple parameter extraction (could be enhanced)
                            if type_def.ts_input.contains("id") {
                                parameters.push(Parameter {
                                    name: "id".to_string(),
                                    location: ParameterLocation::Query,
                                    description: Some("Request parameter".to_string()),
                                    required: Some(true),
                                    schema: crate::openapi::Schema {
                                        schema_type: Some("string".to_string()),
                                        format: None,
                                        properties: None,
                                        required: None,
                                        items: None,
                                        reference: None,
                                    },
                                });
                            }

                            if !parameters.is_empty() {
                                operation.parameters = Some(parameters);
                            }
                        }
                    } else {
                        // POST: Add request body
                        let request_schema =
                            crate::openapi::OpenApiSpec::ts_to_schema(&type_def.ts_input);
                        let mut content = HashMap::new();
                        content.insert(
                            "application/json".to_string(),
                            MediaType {
                                schema: request_schema,
                                example: None,
                            },
                        );

                        operation.request_body = Some(RequestBody {
                            description: Some("Request body".to_string()),
                            content,
                            required: Some(true),
                        });
                    }

                    // Add to spec
                    let mut path_item = PathItem {
                        get: None,
                        post: None,
                        put: None,
                        delete: None,
                        patch: None,
                    };

                    if is_query {
                        path_item.get = Some(operation);
                    } else {
                        path_item.post = Some(operation);
                    }

                    spec.add_path(path, path_item);
                }
            }
            RpcMode::JsonRpc => {
                // Generate single JSON-RPC endpoint
                let path = "/rpc".to_string();

                let mut responses = HashMap::new();
                responses.insert(
                    "200".to_string(),
                    Response {
                        description: "Successful RPC response".to_string(),
                        content: Some({
                            let mut content = HashMap::new();
                            content.insert(
                                "application/json".to_string(),
                                MediaType {
                                    schema: crate::openapi::Schema {
                                        schema_type: Some("object".to_string()),
                                        format: None,
                                        properties: Some({
                                            let mut props = HashMap::new();
                                            props.insert(
                                                "result".to_string(),
                                                Box::new(crate::openapi::Schema {
                                                    schema_type: Some("object".to_string()),
                                                    format: None,
                                                    properties: None,
                                                    required: None,
                                                    items: None,
                                                    reference: None,
                                                }),
                                            );
                                            props
                                        }),
                                        required: Some(vec!["result".to_string()]),
                                        items: None,
                                        reference: None,
                                    },
                                    example: None,
                                },
                            );
                            content
                        }),
                    },
                );

                let operation = Operation {
                    summary: Some("JSON-RPC endpoint".to_string()),
                    description: Some(format!(
                        "Available methods: {}",
                        type_defs
                            .iter()
                            .map(|t| &t.name)
                            .cloned()
                            .collect::<Vec<_>>()
                            .join(", ")
                    )),
                    operation_id: Some("rpc".to_string()),
                    tags: Some(vec!["JSON-RPC".to_string()]),
                    parameters: None,
                    request_body: Some(RequestBody {
                        description: Some("JSON-RPC request".to_string()),
                        content: {
                            let mut content = HashMap::new();
                            content.insert(
                                "application/json".to_string(),
                                MediaType {
                                    schema: crate::openapi::Schema {
                                        schema_type: Some("object".to_string()),
                                        format: None,
                                        properties: Some({
                                            let mut props = HashMap::new();
                                            props.insert(
                                                "method".to_string(),
                                                Box::new(crate::openapi::Schema {
                                                    schema_type: Some("string".to_string()),
                                                    format: None,
                                                    properties: None,
                                                    required: None,
                                                    items: None,
                                                    reference: None,
                                                }),
                                            );
                                            props.insert(
                                                "params".to_string(),
                                                Box::new(crate::openapi::Schema {
                                                    schema_type: Some("object".to_string()),
                                                    format: None,
                                                    properties: None,
                                                    required: None,
                                                    items: None,
                                                    reference: None,
                                                }),
                                            );
                                            props
                                        }),
                                        required: Some(vec!["method".to_string(), "params".to_string()]),
                                        items: None,
                                        reference: None,
                                    },
                                    example: Some(serde_json::json!({
                                        "method": type_defs.first().map(|t| &t.name).unwrap_or(&"exampleMethod".to_string()),
                                        "params": {}
                                    })),
                                },
                            );
                            content
                        },
                        required: Some(true),
                    }),
                    responses,
                };

                let path_item = PathItem {
                    get: None,
                    post: Some(operation),
                    put: None,
                    delete: None,
                    patch: None,
                };

                spec.add_path(path, path_item);
            }
        }

        spec
    }
}

impl Default for RpcRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// RPC request format
#[derive(Debug, Deserialize)]
pub struct RpcRequest {
    pub method: String,
    pub params: serde_json::Value,
}

/// RPC response format
#[derive(Debug, Serialize)]
pub struct RpcResponse {
    pub result: serde_json::Value,
}

/// RPC error response format
#[derive(Debug, Serialize)]
pub struct RpcErrorResponse {
    pub error: String,
    pub code: i32,
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};

    #[derive(Deserialize)]
    struct TestInput {
        value: i32,
    }

    #[derive(Serialize)]
    struct TestOutput {
        result: i32,
    }

    #[tokio::test]
    async fn test_rpc_registry_creation() {
        let registry = RpcRegistry::new();
        assert_eq!(registry.list_procedures().len(), 0);
    }

    #[tokio::test]
    async fn test_rpc_registry_rest_mode() {
        let registry = RpcRegistry::new_with_mode(RpcMode::Rest);
        assert_eq!(registry.list_procedures().len(), 0);
    }

    #[tokio::test]
    async fn test_rpc_registry_jsonrpc_mode() {
        let registry = RpcRegistry::new_with_mode(RpcMode::JsonRpc);
        assert_eq!(registry.list_procedures().len(), 0);
    }

    #[tokio::test]
    async fn test_rpc_query_registration() {
        let registry = RpcRegistry::new();
        registry.query(
            "test_query",
            |input: TestInput| async move {
                Ok(TestOutput {
                    result: input.value * 2,
                })
            },
            "{ value: number }".to_string(),
            "{ result: number }".to_string(),
        );

        let procedures = registry.list_procedures();
        assert_eq!(procedures.len(), 1);
        assert_eq!(procedures[0], "test_query");
    }

    #[tokio::test]
    async fn test_rpc_mutation_registration() {
        let registry = RpcRegistry::new();
        registry.mutation(
            "test_mutation",
            |input: TestInput| async move {
                Ok(TestOutput {
                    result: input.value + 10,
                })
            },
            "{ value: number }".to_string(),
            "{ result: number }".to_string(),
        );

        let procedures = registry.list_procedures();
        assert_eq!(procedures.len(), 1);
        assert_eq!(procedures[0], "test_mutation");
    }

    #[tokio::test]
    async fn test_rpc_multiple_procedures() {
        let registry = RpcRegistry::new();

        registry.query(
            "query1",
            |_: TestInput| async move { Ok(TestOutput { result: 1 }) },
            "{}".to_string(),
            "{}".to_string(),
        );

        registry.query(
            "query2",
            |_: TestInput| async move { Ok(TestOutput { result: 2 }) },
            "{}".to_string(),
            "{}".to_string(),
        );

        registry.mutation(
            "mutation1",
            |_: TestInput| async move { Ok(TestOutput { result: 3 }) },
            "{}".to_string(),
            "{}".to_string(),
        );

        let procedures = registry.list_procedures();
        assert_eq!(procedures.len(), 3);
        assert!(procedures.contains(&"query1".to_string()));
        assert!(procedures.contains(&"query2".to_string()));
        assert!(procedures.contains(&"mutation1".to_string()));
    }

    #[tokio::test]
    async fn test_typescript_client_generation() {
        let registry = RpcRegistry::new();
        let client = registry.generate_typescript_client();
        assert!(client.contains("UltimoRpcClient"));
        assert!(client.contains("async call"));
    }

    #[tokio::test]
    async fn test_typescript_client_with_procedures() {
        let registry = RpcRegistry::new();

        registry.query(
            "getUser",
            |_: TestInput| async move { Ok(TestOutput { result: 42 }) },
            "{ id: number }".to_string(),
            "{ id: number; name: string }".to_string(),
        );

        let client = registry.generate_typescript_client();
        assert!(client.contains("UltimoRpcClient"));
        assert!(client.contains("getUser"));
        assert!(client.contains("{ id: number }"));
        assert!(client.contains("{ id: number; name: string }"));
    }

    #[tokio::test]
    async fn test_openapi_generation_rest_mode() {
        let registry = RpcRegistry::new_with_mode(RpcMode::Rest);

        registry.query(
            "testQuery",
            |_: TestInput| async move { Ok(TestOutput { result: 123 }) },
            "{ value: number }".to_string(),
            "{ result: number }".to_string(),
        );

        let openapi = registry.generate_openapi("Test API", "1.0.0", "/api");

        assert_eq!(openapi.info.title, "Test API");
        assert_eq!(openapi.info.version, "1.0.0");
        assert!(openapi.paths.contains_key("/api/testQuery"));
    }

    #[tokio::test]
    async fn test_openapi_generation_jsonrpc_mode() {
        let registry = RpcRegistry::new_with_mode(RpcMode::JsonRpc);

        registry.query(
            "testQuery",
            |_: TestInput| async move { Ok(TestOutput { result: 456 }) },
            "{ value: number }".to_string(),
            "{ result: number }".to_string(),
        );

        let openapi = registry.generate_openapi("JSON-RPC API", "2.0.0", "/rpc");

        assert_eq!(openapi.info.title, "JSON-RPC API");
        assert_eq!(openapi.info.version, "2.0.0");
        assert!(openapi.paths.contains_key("/rpc"));
    }

    #[test]
    fn test_rpc_mode_equality() {
        assert_eq!(RpcMode::Rest, RpcMode::Rest);
        assert_eq!(RpcMode::JsonRpc, RpcMode::JsonRpc);
        assert_ne!(RpcMode::Rest, RpcMode::JsonRpc);
    }

    #[tokio::test]
    async fn test_rpc_error_response() {
        let error = RpcErrorResponse {
            error: "Not Found".to_string(),
            code: 404,
        };

        assert_eq!(error.error, "Not Found");
        assert_eq!(error.code, 404);
    }

    #[tokio::test]
    async fn test_openapi_includes_all_procedures() {
        let registry = RpcRegistry::new_with_mode(RpcMode::Rest);

        registry.query(
            "getUser",
            |_: TestInput| async move { Ok(TestOutput { result: 1 }) },
            "{}".to_string(),
            "{}".to_string(),
        );

        registry.query(
            "listUsers",
            |_: TestInput| async move { Ok(TestOutput { result: 2 }) },
            "{}".to_string(),
            "{}".to_string(),
        );

        registry.mutation(
            "createUser",
            |_: TestInput| async move { Ok(TestOutput { result: 3 }) },
            "{}".to_string(),
            "{}".to_string(),
        );

        let openapi = registry.generate_openapi("API", "1.0.0", "/api");

        // In REST mode, each procedure gets its own path
        assert!(openapi.paths.contains_key("/api/getUser"));
        assert!(openapi.paths.contains_key("/api/listUsers"));
        assert!(openapi.paths.contains_key("/api/createUser"));
    }

    #[tokio::test]
    async fn test_list_procedures_returns_names() {
        let registry = RpcRegistry::new();

        registry.query(
            "proc1",
            |_: TestInput| async move { Ok(TestOutput { result: 1 }) },
            "{}".to_string(),
            "{}".to_string(),
        );

        registry.query(
            "proc2",
            |_: TestInput| async move { Ok(TestOutput { result: 2 }) },
            "{}".to_string(),
            "{}".to_string(),
        );

        let names = registry.list_procedures();
        assert_eq!(names.len(), 2);
        assert!(names.contains(&"proc1".to_string()));
        assert!(names.contains(&"proc2".to_string()));
    }
}