runbeam-sdk 0.11.0

Rust SDK for integrating with the Runbeam Cloud API, providing JWT validation, API client, and generic secure token storage
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
use serde::{Deserialize, Serialize};
use std::fmt;

/// Runbeam API error type
///
/// Represents all possible errors that can occur when interacting with
/// the Runbeam Cloud API or performing related operations.
#[derive(Debug)]
pub enum RunbeamError {
    /// JWT validation failed
    JwtValidation(String),
    /// API request failed (network, HTTP, or response parsing error)
    Api(ApiError),
    /// Token storage operation failed
    Storage(crate::storage::StorageError),
    /// Configuration error
    Config(String),
    /// TOML validation failed
    Validation(crate::validation::ValidationError),
}

impl fmt::Display for RunbeamError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RunbeamError::JwtValidation(msg) => write!(f, "JWT validation failed: {}", msg),
            RunbeamError::Api(err) => write!(f, "API error: {}", err),
            RunbeamError::Storage(err) => write!(f, "Storage error: {}", err),
            RunbeamError::Config(msg) => write!(f, "Configuration error: {}", msg),
            RunbeamError::Validation(err) => write!(f, "Validation error: {}", err),
        }
    }
}

impl std::error::Error for RunbeamError {}

impl From<ApiError> for RunbeamError {
    fn from(err: ApiError) -> Self {
        RunbeamError::Api(err)
    }
}

impl From<crate::storage::StorageError> for RunbeamError {
    fn from(err: crate::storage::StorageError) -> Self {
        RunbeamError::Storage(err)
    }
}

impl From<jsonwebtoken::errors::Error> for RunbeamError {
    fn from(err: jsonwebtoken::errors::Error) -> Self {
        RunbeamError::JwtValidation(err.to_string())
    }
}

impl From<crate::validation::ValidationError> for RunbeamError {
    fn from(err: crate::validation::ValidationError) -> Self {
        RunbeamError::Validation(err)
    }
}

/// API-specific errors
#[derive(Debug)]
pub enum ApiError {
    /// Network error (connection, timeout, etc.)
    Network(String),
    /// HTTP error with status code
    Http { status: u16, message: String },
    /// Failed to parse response
    Parse(String),
    /// Request building failed
    Request(String),
}

impl fmt::Display for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ApiError::Network(msg) => write!(f, "Network error: {}", msg),
            ApiError::Http { status, message } => {
                write!(f, "HTTP {} error: {}", status, message)
            }
            ApiError::Parse(msg) => write!(f, "Parse error: {}", msg),
            ApiError::Request(msg) => write!(f, "Request error: {}", msg),
        }
    }
}

impl std::error::Error for ApiError {}

impl From<reqwest::Error> for ApiError {
    fn from(err: reqwest::Error) -> Self {
        if err.is_timeout() {
            ApiError::Network("Request timeout".to_string())
        } else if err.is_connect() {
            ApiError::Network(format!("Connection failed: {}", err))
        } else if let Some(status) = err.status() {
            ApiError::Http {
                status: status.as_u16(),
                message: err.to_string(),
            }
        } else {
            ApiError::Network(err.to_string())
        }
    }
}

/// User information from JWT claims
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserInfo {
    pub id: String,
    pub email: String,
    pub name: String,
}

/// User authentication token (JWT)
///
/// This token is used for authenticating user actions with the Runbeam Cloud API.
/// It has a shorter lifespan than machine tokens and is typically issued after
/// a user successfully logs in via the browser-based OAuth flow.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserToken {
    /// JWT token for API authentication
    pub token: String,
    /// Token expiration timestamp (seconds since Unix epoch)
    #[serde(default)]
    pub expires_at: Option<i64>,
    /// User information from JWT claims
    #[serde(default)]
    pub user: Option<UserInfo>,
}

impl UserToken {
    /// Create a new user token
    pub fn new(token: String, expires_at: Option<i64>, user: Option<UserInfo>) -> Self {
        Self {
            token,
            expires_at,
            user,
        }
    }
}

/// Team information from JWT claims
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TeamInfo {
    pub id: String,
    pub name: String,
}

/// Gateway information returned from authorize endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GatewayInfo {
    pub id: String,
    pub code: String,
    pub name: String,
    #[serde(default)]
    pub authorized_by: Option<AuthorizedBy>,
}

/// User who authorized the gateway
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorizedBy {
    pub id: String,
    pub name: String,
    pub email: String,
}

/// Response from Runbeam Cloud authorize endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorizeResponse {
    pub machine_token: String,
    pub expires_in: f64,
    pub expires_at: String,
    pub gateway: GatewayInfo,
    #[serde(default)]
    pub abilities: Vec<String>,
}

/// Request payload for storing/updating Harmony configuration
///
/// This is used by the `harmony.update` endpoint to send TOML configuration
/// from Harmony instances back to Runbeam Cloud for storage as database models.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StoreConfigRequest {
    /// Type of configuration being stored ("gateway", "pipeline", or "transform")
    #[serde(rename = "type")]
    pub config_type: String,
    /// Optional ID for updating existing resources (omitted for creates)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// TOML configuration content
    pub config: String,
}

/// Response from storing/updating Harmony configuration
///
/// The API returns UpdateSuccessResource format.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StoreConfigResponse {
    /// Success flag
    pub success: bool,
    /// Success message
    pub message: String,
    /// Response data with model and change info
    pub data: StoreConfigModel,
}

/// Model information from store config response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StoreConfigModel {
    /// Model ID (ULID)
    pub id: String,
    /// Model type ("gateway", "pipeline", "transform")
    #[serde(rename = "type")]
    pub model_type: String,
    /// Action taken ("created", "updated")
    pub action: String,
}

/// Mesh information returned from Runbeam Cloud API
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MeshInfo {
    /// Unique mesh identifier (ULID)
    pub id: String,
    /// Human-readable mesh name
    pub name: String,
    /// Protocol type for mesh communication (http, http3)
    #[serde(rename = "type")]
    pub mesh_type: String,
    /// Mesh provider (local, runbeam)
    pub provider: String,
    /// Authentication type for mesh members (currently only "jwt")
    #[serde(default = "default_auth_type")]
    pub auth_type: String,
    /// JWT secret for HS256 symmetric key authentication (local provider)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jwt_secret: Option<String>,
    /// Path to RSA private key (PEM) for RS256 JWT signing (local provider)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jwt_private_key_path: Option<String>,
    /// Path to RSA public key (PEM) for RS256 JWT verification (local provider)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jwt_public_key_path: Option<String>,
    /// List of ingress definition names
    #[serde(default)]
    pub ingress: Vec<String>,
    /// List of egress definition names
    #[serde(default)]
    pub egress: Vec<String>,
    /// Whether the mesh is enabled
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Optional description
    #[serde(default)]
    pub description: Option<String>,
}

/// Mesh ingress information - allows other mesh members to send requests
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MeshIngressInfo {
    /// Unique ingress identifier (ULID)
    pub id: String,
    /// Human-readable ingress name
    pub name: String,
    /// Protocol type for incoming mesh requests (http, http3)
    #[serde(rename = "type")]
    pub ingress_type: String,
    /// Pipeline name that owns this ingress (required)
    pub pipeline: String,
    /// Mode: 'default' allows all requests, 'mesh' requires valid mesh authentication
    #[serde(default = "default_mode")]
    pub mode: String,
    /// Optional endpoint override. If omitted, the first endpoint in the pipeline is used.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub endpoint: Option<String>,
    /// List of URLs that map to this ingress
    #[serde(default)]
    pub urls: Vec<String>,
    /// Whether the ingress is enabled
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Optional description
    #[serde(default)]
    pub description: Option<String>,
}

/// Mesh egress information - allows sending requests to other mesh members
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MeshEgressInfo {
    /// Unique egress identifier (ULID)
    pub id: String,
    /// Human-readable egress name
    pub name: String,
    /// Protocol type for outgoing mesh requests (http, http3)
    #[serde(rename = "type")]
    pub egress_type: String,
    /// Pipeline name that owns this egress (required)
    pub pipeline: String,
    /// Mode: 'default' allows all destinations, 'mesh' requires destination to match a mesh ingress
    #[serde(default = "default_mode")]
    pub mode: String,
    /// Optional backend override. If omitted, the first backend in the pipeline is used.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub backend: Option<String>,
    /// Whether the egress is enabled
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Optional description
    #[serde(default)]
    pub description: Option<String>,
}

fn default_true() -> bool {
    true
}

fn default_mode() -> String {
    "default".to_string()
}

fn default_auth_type() -> String {
    "jwt".to_string()
}

fn default_poll_interval() -> u32 {
    30
}

// ========================================================================================
// RESOURCE RESOLUTION TYPES
// ========================================================================================

/// Response from resolving a resource reference
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolveResourceResponse {
    /// The resolved resource data
    pub data: ResolvedResource,
    /// Resolution metadata
    pub meta: ResolutionMeta,
}

/// Metadata about the resolution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolutionMeta {
    /// Provider that resolved this resource
    pub provider: String,
    /// When the resolution occurred
    pub resolved_at: String,
}

/// A resolved resource (type varies based on resource type)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolvedResource {
    /// Resource type (ingress, egress, pipeline, etc.)
    #[serde(rename = "type")]
    pub resource_type: String,
    /// Resource ID (ULID)
    pub id: String,
    /// Resource name
    pub name: String,
    /// Team ID
    #[serde(default)]
    pub team_id: Option<String>,
    /// Whether the resource is enabled
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Gateway ID (for gateway-scoped resources)
    #[serde(default)]
    pub gateway_id: Option<String>,
    /// Pipeline ID (for pipeline-scoped resources)
    #[serde(default)]
    pub pipeline_id: Option<String>,
    /// Mesh ID (for mesh ingress/egress)
    #[serde(default)]
    pub mesh_id: Option<String>,
    /// URLs (for ingress resources)
    #[serde(default)]
    pub urls: Vec<String>,
    /// Protocol (http, http3, etc.)
    #[serde(default)]
    pub protocol: Option<String>,
    /// Mode (default, mesh)
    #[serde(default)]
    pub mode: Option<String>,
    /// Backend ID (for egress resources)
    #[serde(default)]
    pub backend_id: Option<String>,
    /// Service ID (for endpoints/backends)
    #[serde(default)]
    pub service_id: Option<String>,
    /// Endpoint ID (for ingress resources)
    #[serde(default)]
    pub endpoint_id: Option<String>,
    /// Description
    #[serde(default)]
    pub description: Option<String>,
    /// Provider (for mesh resources)
    #[serde(default)]
    pub provider: Option<String>,
    /// Auth type (for mesh resources)
    #[serde(default)]
    pub auth_type: Option<String>,
}

// ========================================================================================
// PROVIDER TYPES
// ========================================================================================

/// Provider configuration for resource resolution
///
/// Providers define how resources are resolved - either locally from config files
/// or remotely from a provider API (e.g., Runbeam Cloud).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderConfig {
    /// Base URL for provider API. Required for remote providers, omitted for 'local'.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub api: Option<String>,
    /// Whether this provider is active
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Polling interval in seconds for change detection
    #[serde(default = "default_poll_interval")]
    pub poll_interval_secs: u32,
}

impl Default for ProviderConfig {
    fn default() -> Self {
        Self {
            api: None,
            enabled: true,
            poll_interval_secs: 30,
        }
    }
}

/// Type of resource being referenced
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ResourceType {
    Ingress,
    Egress,
    Pipeline,
    Endpoint,
    Backend,
    Mesh,
}

impl fmt::Display for ResourceType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ResourceType::Ingress => write!(f, "ingress"),
            ResourceType::Egress => write!(f, "egress"),
            ResourceType::Pipeline => write!(f, "pipeline"),
            ResourceType::Endpoint => write!(f, "endpoint"),
            ResourceType::Backend => write!(f, "backend"),
            ResourceType::Mesh => write!(f, "mesh"),
        }
    }
}

impl std::str::FromStr for ResourceType {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "ingress" => Ok(ResourceType::Ingress),
            "egress" => Ok(ResourceType::Egress),
            "pipeline" => Ok(ResourceType::Pipeline),
            "endpoint" => Ok(ResourceType::Endpoint),
            "backend" => Ok(ResourceType::Backend),
            "mesh" => Ok(ResourceType::Mesh),
            _ => Err(format!("Unknown resource type: {}", s)),
        }
    }
}

/// How to look up the resource
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LookupBy {
    /// Lookup by ULID
    Id(String),
    /// Lookup by name
    Name(String),
}

/// Parsed resource reference
///
/// Supports multiple formats:
/// - `name` -> local.name.{name}
/// - `local.name.{name}` -> explicit local lookup
/// - `{provider}.id.{id}` -> provider-wide ID lookup
/// - `{provider}.{team}.id.{id}` -> team-scoped ID lookup  
/// - `{provider}.{team}.{type}.name.{name}` -> full path lookup
/// - `{provider}.{team}.{type}.id.{id}` -> full path lookup by ID
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResourceReference {
    /// Provider name (e.g., "local", "runbeam")
    pub provider: String,
    /// Optional team identifier
    pub team: Option<String>,
    /// Optional resource type
    pub resource_type: Option<ResourceType>,
    /// How to look up the resource
    pub lookup: LookupBy,
}

impl ResourceReference {
    /// Parse a resource reference string
    ///
    /// # Examples
    /// ```
    /// use runbeam_sdk::runbeam_api::types::ResourceReference;
    ///
    /// // Bare name -> local.name.{name}
    /// let r = ResourceReference::parse("my_ingress").unwrap();
    /// assert_eq!(r.provider, "local");
    ///
    /// // Full path
    /// let r = ResourceReference::parse("runbeam.acme.ingress.name.patient_api").unwrap();
    /// assert_eq!(r.provider, "runbeam");
    /// ```
    pub fn parse(input: &str) -> Result<Self, String> {
        let parts: Vec<&str> = input.split('.').collect();

        match parts.len() {
            // Bare name: "my_ingress" -> local.name.my_ingress
            1 => Ok(ResourceReference {
                provider: "local".to_string(),
                team: None,
                resource_type: None,
                lookup: LookupBy::Name(parts[0].to_string()),
            }),

            // "local.name.{name}" or "{provider}.id.{id}"
            3 => {
                let provider = parts[0];
                match parts[1] {
                    "name" => Ok(ResourceReference {
                        provider: provider.to_string(),
                        team: None,
                        resource_type: None,
                        lookup: LookupBy::Name(parts[2].to_string()),
                    }),
                    "id" => Ok(ResourceReference {
                        provider: provider.to_string(),
                        team: None,
                        resource_type: None,
                        lookup: LookupBy::Id(parts[2].to_string()),
                    }),
                    _ => Err(format!(
                        "Invalid reference format: expected 'name' or 'id', got '{}'",
                        parts[1]
                    )),
                }
            }

            // "{provider}.{team}.id.{id}"
            4 => {
                let provider = parts[0];
                let team = parts[1];
                match parts[2] {
                    "id" => Ok(ResourceReference {
                        provider: provider.to_string(),
                        team: Some(team.to_string()),
                        resource_type: None,
                        lookup: LookupBy::Id(parts[3].to_string()),
                    }),
                    _ => Err(format!(
                        "Invalid reference format: expected 'id' at position 2, got '{}'",
                        parts[2]
                    )),
                }
            }

            // "{provider}.{team}.{type}.name.{name}" or "{provider}.{team}.{type}.id.{id}"
            5 => {
                let provider = parts[0];
                let team = parts[1];
                let resource_type: ResourceType = parts[2].parse()?;
                match parts[3] {
                    "name" => Ok(ResourceReference {
                        provider: provider.to_string(),
                        team: Some(team.to_string()),
                        resource_type: Some(resource_type),
                        lookup: LookupBy::Name(parts[4].to_string()),
                    }),
                    "id" => Ok(ResourceReference {
                        provider: provider.to_string(),
                        team: Some(team.to_string()),
                        resource_type: Some(resource_type),
                        lookup: LookupBy::Id(parts[4].to_string()),
                    }),
                    _ => Err(format!(
                        "Invalid reference format: expected 'name' or 'id', got '{}'",
                        parts[3]
                    )),
                }
            }

            _ => Err(format!(
                "Invalid reference format: unexpected number of parts ({})",
                parts.len()
            )),
        }
    }

    /// Check if this reference is for local resolution only
    pub fn is_local(&self) -> bool {
        self.provider == "local"
    }

    /// Convert back to string representation
    pub fn to_reference_string(&self) -> String {
        let lookup_str = match &self.lookup {
            LookupBy::Id(id) => format!("id.{}", id),
            LookupBy::Name(name) => format!("name.{}", name),
        };

        match (&self.team, &self.resource_type) {
            (Some(team), Some(rt)) => format!("{}.{}.{}.{}", self.provider, team, rt, lookup_str),
            (Some(team), None) => format!("{}.{}.{}", self.provider, team, lookup_str),
            (None, _) => {
                // For local with name lookup, can use shorthand
                if self.provider == "local" {
                    if let LookupBy::Name(name) = &self.lookup {
                        return name.clone();
                    }
                }
                format!("{}.{}", self.provider, lookup_str)
            }
        }
    }
}

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

    #[test]
    fn test_store_config_request_with_id() {
        let request = StoreConfigRequest {
            config_type: "gateway".to_string(),
            id: Some("01k8ek6h9aahhnrv3benret1nn".to_string()),
            config: "[proxy]\nid = \"test\"\n".to_string(),
        };

        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("\"type\":\"gateway\""));
        assert!(json.contains("\"id\":\"01k8ek6h9aahhnrv3benret1nn\""));
        assert!(json.contains("\"config\":"));
        assert!(json.contains("[proxy]"));

        // Test deserialization
        let deserialized: StoreConfigRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.config_type, "gateway");
        assert_eq!(
            deserialized.id,
            Some("01k8ek6h9aahhnrv3benret1nn".to_string())
        );
    }

    #[test]
    fn test_store_config_request_without_id() {
        let request = StoreConfigRequest {
            config_type: "pipeline".to_string(),
            id: None,
            config: "[pipeline]\nname = \"test\"\n".to_string(),
        };

        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("\"type\":\"pipeline\""));
        assert!(json.contains("\"config\":"));
        // Should not contain the id field when None
        assert!(!json.contains("\"id\""));

        // Test deserialization
        let deserialized: StoreConfigRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.config_type, "pipeline");
        assert_eq!(deserialized.id, None);
    }

    #[test]
    fn test_store_config_request_type_field_rename() {
        // Test that the "type" field is correctly serialized despite the field being named config_type
        let json = r#"{"type":"transform","config":"[transform]\nname = \"test\"\n"}"#;
        let request: StoreConfigRequest = serde_json::from_str(json).unwrap();
        assert_eq!(request.config_type, "transform");
        assert_eq!(request.id, None);
    }

    #[test]
    fn test_store_config_response() {
        let json = r#"{
            "success": true,
            "message": "Gateway configuration updated successfully",
            "data": {
                "id": "01k9npa4tatmwddk66xxpcr2r0",
                "type": "gateway",
                "action": "updated"
            }
        }"#;

        let response: StoreConfigResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.success, true);
        assert!(response.message.contains("updated successfully"));
        assert_eq!(response.data.id, "01k9npa4tatmwddk66xxpcr2r0");
        assert_eq!(response.data.model_type, "gateway");
        assert_eq!(response.data.action, "updated");
    }

    // ========================================================================================
    // RESOURCE REFERENCE TESTS
    // ========================================================================================

    #[test]
    fn test_resource_reference_bare_name() {
        let r = ResourceReference::parse("my_ingress").unwrap();
        assert_eq!(r.provider, "local");
        assert_eq!(r.team, None);
        assert_eq!(r.resource_type, None);
        assert_eq!(r.lookup, LookupBy::Name("my_ingress".to_string()));
        assert!(r.is_local());
    }

    #[test]
    fn test_resource_reference_local_name() {
        let r = ResourceReference::parse("local.name.fhir_api").unwrap();
        assert_eq!(r.provider, "local");
        assert_eq!(r.team, None);
        assert_eq!(r.resource_type, None);
        assert_eq!(r.lookup, LookupBy::Name("fhir_api".to_string()));
        assert!(r.is_local());
    }

    #[test]
    fn test_resource_reference_provider_id() {
        let r = ResourceReference::parse("runbeam.id.01JGXYZ123ABC").unwrap();
        assert_eq!(r.provider, "runbeam");
        assert_eq!(r.team, None);
        assert_eq!(r.resource_type, None);
        assert_eq!(r.lookup, LookupBy::Id("01JGXYZ123ABC".to_string()));
        assert!(!r.is_local());
    }

    #[test]
    fn test_resource_reference_team_id() {
        let r = ResourceReference::parse("runbeam.acme.id.01JGXYZ123ABC").unwrap();
        assert_eq!(r.provider, "runbeam");
        assert_eq!(r.team, Some("acme".to_string()));
        assert_eq!(r.resource_type, None);
        assert_eq!(r.lookup, LookupBy::Id("01JGXYZ123ABC".to_string()));
    }

    #[test]
    fn test_resource_reference_full_path_name() {
        let r = ResourceReference::parse("runbeam.acme_health.ingress.name.patient_api").unwrap();
        assert_eq!(r.provider, "runbeam");
        assert_eq!(r.team, Some("acme_health".to_string()));
        assert_eq!(r.resource_type, Some(ResourceType::Ingress));
        assert_eq!(r.lookup, LookupBy::Name("patient_api".to_string()));
    }

    #[test]
    fn test_resource_reference_full_path_id() {
        let r = ResourceReference::parse("runbeam.partner_lab.egress.id.01JGXYZ").unwrap();
        assert_eq!(r.provider, "runbeam");
        assert_eq!(r.team, Some("partner_lab".to_string()));
        assert_eq!(r.resource_type, Some(ResourceType::Egress));
        assert_eq!(r.lookup, LookupBy::Id("01JGXYZ".to_string()));
    }

    #[test]
    fn test_resource_reference_all_types() {
        assert!(ResourceReference::parse("runbeam.t.ingress.name.x").unwrap().resource_type == Some(ResourceType::Ingress));
        assert!(ResourceReference::parse("runbeam.t.egress.name.x").unwrap().resource_type == Some(ResourceType::Egress));
        assert!(ResourceReference::parse("runbeam.t.pipeline.name.x").unwrap().resource_type == Some(ResourceType::Pipeline));
        assert!(ResourceReference::parse("runbeam.t.endpoint.name.x").unwrap().resource_type == Some(ResourceType::Endpoint));
        assert!(ResourceReference::parse("runbeam.t.backend.name.x").unwrap().resource_type == Some(ResourceType::Backend));
        assert!(ResourceReference::parse("runbeam.t.mesh.name.x").unwrap().resource_type == Some(ResourceType::Mesh));
    }

    #[test]
    fn test_resource_reference_invalid_format() {
        assert!(ResourceReference::parse("runbeam.team.invalid.name.x").is_err());
        assert!(ResourceReference::parse("a.b").is_err());
        assert!(ResourceReference::parse("a.b.c.d.e.f").is_err());
    }

    #[test]
    fn test_resource_reference_to_string() {
        // Bare name shorthand
        let r = ResourceReference::parse("my_ingress").unwrap();
        assert_eq!(r.to_reference_string(), "my_ingress");

        // Full path
        let r = ResourceReference::parse("runbeam.acme.ingress.name.patient_api").unwrap();
        assert_eq!(r.to_reference_string(), "runbeam.acme.ingress.name.patient_api");

        // Provider ID
        let r = ResourceReference::parse("runbeam.id.01JGXYZ").unwrap();
        assert_eq!(r.to_reference_string(), "runbeam.id.01JGXYZ");
    }

    #[test]
    fn test_provider_config_default() {
        let config = ProviderConfig::default();
        assert_eq!(config.api, None);
        assert_eq!(config.enabled, true);
        assert_eq!(config.poll_interval_secs, 30);
    }

    #[test]
    fn test_provider_config_serde() {
        let json = r#"{"api":"https://app.runbeam.io","enabled":true,"poll_interval_secs":60}"#;
        let config: ProviderConfig = serde_json::from_str(json).unwrap();
        assert_eq!(config.api, Some("https://app.runbeam.io".to_string()));
        assert_eq!(config.enabled, true);
        assert_eq!(config.poll_interval_secs, 60);
    }
}