uptrakit-surfaces 0.0.2

Shared surface contracts for built-in and provider UI integration
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
use serde::{Deserialize, Serialize};

use crate::SurfaceId;

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Targeting {
    Universal,
    Targeted,
}

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Scope {
    Global,
    Tenant,
}

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProviderKind {
    BuiltIn,
    Plugin,
    Service,
}

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum SurfaceNode {
    Section {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        header_action_ids: Vec<crate::InteractionId>,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        children: Vec<SurfaceNode>,
    },
    TextBlock {
        text: String,
    },
    KeyValue {
        data_source_id: crate::DataSourceId,
    },
    Table {
        data_source_id: crate::DataSourceId,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        columns: Vec<SurfaceTableColumn>,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        row_actions: Vec<SurfaceTableRowAction>,
    },
    Form {
        interaction_id: crate::InteractionId,
    },
    ActionBar {
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        action_ids: Vec<crate::InteractionId>,
    },
    Tabs {
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        tabs: Vec<SurfaceTab>,
    },
    Callout {
        level: CalloutLevel,
        text: String,
    },
    EmptyState {
        title: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        description: Option<String>,
    },
    ModalTrigger {
        interaction_id: crate::InteractionId,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        modal_nodes: Vec<SurfaceNode>,
    },
    WorkflowTrigger {
        interaction_id: crate::InteractionId,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        step_nodes: Vec<SurfaceNode>,
    },
}

impl SurfaceNode {
    /// Constructs a [`SurfaceNode::Section`] with an optional title and children,
    /// and no header action buttons.
    ///
    /// Use this constructor instead of the struct literal because [`SurfaceNode`] is
    /// `#[non_exhaustive]` — external crates cannot construct variants directly.
    #[must_use]
    pub fn section(title: Option<impl Into<String>>, children: Vec<Self>) -> Self {
        Self::Section {
            title: title.map(Into::into),
            header_action_ids: vec![],
            children,
        }
    }

    /// Constructs a [`SurfaceNode::Section`] with an optional title, header action
    /// button IDs, and children.
    ///
    /// `header_action_ids` refers to [`crate::InteractionId`]s that the Dashboard
    /// renders as icon buttons in the section header row.
    ///
    /// Use this constructor instead of the struct literal because [`SurfaceNode`] is
    /// `#[non_exhaustive]` — external crates cannot construct variants directly.
    #[must_use]
    pub fn section_with_header_actions(
        title: Option<impl Into<String>>,
        header_action_ids: Vec<crate::InteractionId>,
        children: Vec<Self>,
    ) -> Self {
        Self::Section {
            title: title.map(Into::into),
            header_action_ids,
            children,
        }
    }
}

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SurfaceTableColumn {
    pub key: String,
    pub label: String,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_optional_cell_type"
    )]
    pub cell_type: Option<SurfaceTableCellType>,
}

impl SurfaceTableColumn {
    /// Creates a new column with no cell type (plain text rendering).
    pub fn new(key: impl Into<String>, label: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            label: label.into(),
            cell_type: None,
        }
    }
}

fn deserialize_optional_cell_type<'de, D>(
    deserializer: D,
) -> Result<Option<SurfaceTableCellType>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = Option::<serde_json::Value>::deserialize(deserializer)?;
    Ok(value.and_then(|v| serde_json::from_value(v).ok()))
}

/// Cell type for a surface table column.
///
/// Forward compatibility: unknown `kind` values deserialize to `None` via
/// [`deserialize_optional_cell_type`] rather than `Other(String)`, because
/// a completely unknown cell type has no meaningful rendering — silently
/// treating it as a plain-text column is safer than propagating an opaque value.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum SurfaceTableCellType {
    EntityLink { entity_type: SurfaceEntityType },
}

/// Wire-safe entity type enum.
///
/// Known variants are type-safe; unknown values from newer peers become
/// `Other(String)` for forward compatibility. Uses custom `Serialize`
/// and `Deserialize` so that `Other(String)` emits a bare string on
/// the wire (not `{"other":"..."}`).
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SurfaceEntityType {
    Host,
    Other(String),
}

impl SurfaceEntityType {
    /// Returns the snake_case wire string for this entity type.
    pub fn as_str(&self) -> &str {
        match self {
            Self::Host => "host",
            Self::Other(s) => s.as_str(),
        }
    }
}

impl From<String> for SurfaceEntityType {
    fn from(s: String) -> Self {
        match s.as_str() {
            "host" => Self::Host,
            _ => Self::Other(s),
        }
    }
}

impl Serialize for SurfaceEntityType {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for SurfaceEntityType {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        String::deserialize(deserializer).map(SurfaceEntityType::from)
    }
}

/// Cell value for entity-link columns.
///
/// Plugins construct via [`SurfaceEntityRef::unresolved`] (`entity_id` only).
/// The framework enriches `label` and `found` before sending the wire response.
/// `found: None` is a transient pre-enrichment state — must not appear in the
/// final wire response for cells whose resolver ran.
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SurfaceEntityRef {
    pub entity_id: uuid::Uuid,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub found: Option<bool>,
}

impl SurfaceEntityRef {
    /// Constructs an unresolved ref for use by plugin handlers.
    /// The framework enriches `label` and `found` in the enrichment step.
    pub fn unresolved(entity_id: uuid::Uuid) -> Self {
        Self {
            entity_id,
            label: None,
            found: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SurfaceTableRowAction {
    pub interaction_id: crate::InteractionId,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub visible_when: Option<SurfaceRowVisibleWhen>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SurfaceRowVisibleWhen {
    pub field: String,
    pub condition: SurfaceRowCondition,
}

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SurfaceRowCondition {
    Present,
    Absent,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SurfaceTab {
    pub id: crate::SurfaceTabId,
    pub label: String,
    pub root: SurfaceNode,
}

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CalloutLevel {
    Info,
    Warning,
    Danger,
}

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SurfaceDescriptor {
    pub surface_id: SurfaceId,
    pub label: String,
    pub priority: i32,
    pub slot: String,
    pub scope: Scope,
    pub targeting: Targeting,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub required_permission: Option<String>,
    pub provider_kind: ProviderKind,
    pub required_capabilities: CapabilitySet,
    pub root_node: SurfaceNode,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub context_selector: Option<SurfaceContextSelectorDescriptor>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub nav_icon: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tab_group: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tab_group_label: Option<String>,
}

impl SurfaceDescriptor {
    /// Returns a zero-arg [`SurfaceDescriptorBuilder`] for constructing a [`SurfaceDescriptor`].
    ///
    /// # Example
    ///
    /// ```rust
    /// use uptrakit_surfaces::{
    ///     CapabilitySet, ProviderKind, Scope, SurfaceDescriptor, SurfaceId, SurfaceNode, Targeting,
    /// };
    ///
    /// let descriptor = SurfaceDescriptor::builder()
    ///     .surface_id(SurfaceId::new("provider.sample.surface").unwrap())
    ///     .label("Sample")
    ///     .priority(200)
    ///     .slot("surface.page")
    ///     .scope(Scope::Tenant)
    ///     .targeting(Targeting::Universal)
    ///     .provider_kind(ProviderKind::Plugin)
    ///     .required_capabilities(CapabilitySet::default())
    ///     .root_node(SurfaceNode::section(None::<String>, vec![]))
    ///     .build();
    /// ```
    #[must_use]
    pub fn builder() -> SurfaceDescriptorBuilder {
        SurfaceDescriptorBuilder::default()
    }
}

/// Builder for [`SurfaceDescriptor`].
///
/// Obtain an instance via [`SurfaceDescriptor::builder`] and call [`build`](Self::build) to
/// finalise the descriptor. Optional fields ([`required_permission`](Self::required_permission)
/// and [`context_selector`](Self::context_selector)) default to `None`.
///
/// [`build`](Self::build) panics if any required field has not been set.
#[derive(Debug, Clone, Default)]
pub struct SurfaceDescriptorBuilder {
    surface_id: Option<SurfaceId>,
    label: Option<String>,
    priority: Option<i32>,
    slot: Option<String>,
    scope: Option<Scope>,
    targeting: Option<Targeting>,
    required_permission: Option<String>,
    provider_kind: Option<ProviderKind>,
    required_capabilities: Option<CapabilitySet>,
    root_node: Option<SurfaceNode>,
    context_selector: Option<SurfaceContextSelectorDescriptor>,
    nav_icon: Option<String>,
    tab_group: Option<String>,
    tab_group_label: Option<String>,
}

impl SurfaceDescriptorBuilder {
    /// Sets the surface identifier.
    #[must_use]
    pub fn surface_id(mut self, surface_id: SurfaceId) -> Self {
        self.surface_id = Some(surface_id);
        self
    }

    /// Sets the human-readable label.
    #[must_use]
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Sets the display priority within the slot.
    #[must_use]
    pub fn priority(mut self, priority: i32) -> Self {
        self.priority = Some(priority);
        self
    }

    /// Sets the slot identifier (e.g. `"surface.page"`).
    #[must_use]
    pub fn slot(mut self, slot: impl Into<String>) -> Self {
        self.slot = Some(slot.into());
        self
    }

    /// Sets the scope (global or tenant).
    #[must_use]
    pub fn scope(mut self, scope: Scope) -> Self {
        self.scope = Some(scope);
        self
    }

    /// Sets the targeting mode.
    #[must_use]
    pub fn targeting(mut self, targeting: Targeting) -> Self {
        self.targeting = Some(targeting);
        self
    }

    /// Sets the permission string required to view this surface (optional).
    #[must_use]
    pub fn required_permission(mut self, permission: impl Into<String>) -> Self {
        self.required_permission = Some(permission.into());
        self
    }

    /// Sets the provider kind.
    #[must_use]
    pub fn provider_kind(mut self, provider_kind: ProviderKind) -> Self {
        self.provider_kind = Some(provider_kind);
        self
    }

    /// Sets the set of capabilities this surface requires from the framework.
    #[must_use]
    pub fn required_capabilities(mut self, required_capabilities: CapabilitySet) -> Self {
        self.required_capabilities = Some(required_capabilities);
        self
    }

    /// Sets the root [`SurfaceNode`] of the surface layout.
    #[must_use]
    pub fn root_node(mut self, root_node: SurfaceNode) -> Self {
        self.root_node = Some(root_node);
        self
    }

    /// Attaches a context-selector dropdown descriptor to the surface (optional).
    #[must_use]
    pub fn context_selector(mut self, context_selector: SurfaceContextSelectorDescriptor) -> Self {
        self.context_selector = Some(context_selector);
        self
    }

    /// Sets the nav icon name (optional; must match a key in the frontend `SURFACE_NAV_ICONS` allowlist).
    #[must_use]
    pub fn nav_icon(mut self, nav_icon: impl Into<String>) -> Self {
        self.nav_icon = Some(nav_icon.into());
        self
    }

    /// Groups this surface with others sharing `id` under one settings tab.
    ///
    /// If `id` matches an existing built-in tab (e.g. `"general"`), content is appended
    /// to that tab and `label` is ignored. Otherwise a new tab labelled `label` is created.
    #[must_use]
    pub fn tab_group(mut self, id: impl Into<String>, label: impl Into<String>) -> Self {
        self.tab_group = Some(id.into());
        self.tab_group_label = Some(label.into());
        self
    }

    /// Consumes the builder and returns the [`SurfaceDescriptor`].
    ///
    /// # Panics
    ///
    /// Panics if any required field (`surface_id`, `label`, `priority`, `slot`, `scope`,
    /// `targeting`, `provider_kind`, `required_capabilities`, `root_node`) has not been set.
    #[must_use]
    #[expect(
        clippy::expect_used,
        reason = "builder pattern — panicking on missing required fields is intentional and documented"
    )]
    pub fn build(self) -> SurfaceDescriptor {
        SurfaceDescriptor {
            surface_id: self
                .surface_id
                .expect("SurfaceDescriptorBuilder: surface_id not set"),
            label: self.label.expect("SurfaceDescriptorBuilder: label not set"),
            priority: self
                .priority
                .expect("SurfaceDescriptorBuilder: priority not set"),
            slot: self.slot.expect("SurfaceDescriptorBuilder: slot not set"),
            scope: self.scope.expect("SurfaceDescriptorBuilder: scope not set"),
            targeting: self
                .targeting
                .expect("SurfaceDescriptorBuilder: targeting not set"),
            required_permission: self.required_permission,
            provider_kind: self
                .provider_kind
                .expect("SurfaceDescriptorBuilder: provider_kind not set"),
            required_capabilities: self
                .required_capabilities
                .expect("SurfaceDescriptorBuilder: required_capabilities not set"),
            root_node: self
                .root_node
                .expect("SurfaceDescriptorBuilder: root_node not set"),
            context_selector: self.context_selector,
            nav_icon: self.nav_icon,
            tab_group: self.tab_group,
            tab_group_label: self.tab_group_label,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct FrameworkGeneration {
    pub major: u16,
    pub minor: u16,
}

impl FrameworkGeneration {
    pub const fn new(major: u16, minor: u16) -> Self {
        Self { major, minor }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct FrameworkGenerationRange {
    pub min: FrameworkGeneration,
    pub max: FrameworkGeneration,
}

impl FrameworkGenerationRange {
    #[must_use]
    pub const fn includes(&self, value: FrameworkGeneration) -> bool {
        is_generation_le(self.min, value) && is_generation_le(value, self.max)
    }
}

const fn is_generation_le(left: FrameworkGeneration, right: FrameworkGeneration) -> bool {
    left.major < right.major || (left.major == right.major && left.minor <= right.minor)
}

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Capability {
    SectionNode,
    TextBlockNode,
    KeyValueNode,
    TableNode,
    FormNode,
    ActionBarNode,
    TabsNode,
    CalloutNode,
    EmptyStateNode,
    ModalTriggerNode,
    WorkflowTriggerNode,
    MutationAction,
    FormSubmit,
    Workflow,
    Navigate,
    DataLoad,
    ConfirmableAction,
    StaticDataSource,
    ControllerQueryDataSource,
    ProviderQueryDataSource,
    UniversalTargeting,
    TargetedTargeting,
    SensitiveFields,
    ProviderInitiatedActions,
    ContextSelector,
    EntityLinkColumn,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CapabilitySet(pub std::collections::BTreeSet<Capability>);

impl CapabilitySet {
    #[must_use]
    pub fn from_capabilities(caps: impl IntoIterator<Item = Capability>) -> Self {
        Self(caps.into_iter().collect())
    }

    #[must_use]
    pub fn contains_all(&self, other: &Self) -> bool {
        other.0.iter().all(|cap| self.0.contains(cap))
    }
}

/// Describes a context-selector dropdown rendered above a surface's content.
///
/// When present on a `SurfaceDescriptor`, `SurfaceReadPanel` fetches the
/// options from `rest_api_path` and renders a `ProviderSelector` above the
/// surface content. The selected value is merged into `baseParams` under
/// `param_key`, driving both the table data load and optional interaction gates.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SurfaceContextSelectorDescriptor {
    /// Param key injected into `baseParams` when a specific option is selected.
    pub param_key: String,
    /// Label shown above the selector dropdown.
    pub label: String,
    /// Label for the "show all" option (no param injected).
    pub all_option_label: String,
    /// REST API path returning a JSON array or paginated `items` list.
    pub rest_api_path: String,
    /// Field in each item used as the option value.
    pub value_field: String,
    /// Field in each item used as the option label.
    pub label_field: String,
    /// Interaction IDs disabled (with tooltip) when no specific option is selected.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required_for_interactions: Vec<crate::InteractionId>,
}

impl SurfaceContextSelectorDescriptor {
    /// Constructs a new [`SurfaceContextSelectorDescriptor`].
    ///
    /// Required because the struct is `#[non_exhaustive]` — external crates cannot use
    /// struct literal syntax and must call this constructor instead.
    #[must_use]
    pub fn new(
        param_key: impl Into<String>,
        label: impl Into<String>,
        all_option_label: impl Into<String>,
        rest_api_path: impl Into<String>,
        value_field: impl Into<String>,
        label_field: impl Into<String>,
        required_for_interactions: Vec<crate::InteractionId>,
    ) -> Self {
        Self {
            param_key: param_key.into(),
            label: label.into(),
            all_option_label: all_option_label.into(),
            rest_api_path: rest_api_path.into(),
            value_field: value_field.into(),
            label_field: label_field.into(),
            required_for_interactions,
        }
    }
}

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

    #[test]
    fn context_selector_capability_serializes_to_snake_case() {
        let cap = Capability::ContextSelector;
        let serialized = serde_json::to_string(&cap).expect("serialize");
        assert_eq!(serialized, r#""context_selector""#);
    }

    #[test]
    fn surface_descriptor_context_selector_round_trips() {
        let descriptor = SurfaceDescriptor {
            surface_id: SurfaceId::new("test.surface").unwrap(),
            label: "Test".to_string(),
            priority: 100,
            slot: "surface.page".to_string(),
            scope: Scope::Global,
            targeting: Targeting::Universal,
            required_permission: None,
            provider_kind: ProviderKind::Plugin,
            required_capabilities: CapabilitySet::from_capabilities([Capability::ContextSelector]),
            root_node: SurfaceNode::section(None::<String>, vec![]),
            context_selector: Some(SurfaceContextSelectorDescriptor {
                param_key: "plugin_config_id".to_string(),
                label: "Configuration".to_string(),
                all_option_label: "All Configurations".to_string(),
                rest_api_path: "/api/v1/plugin-configs".to_string(),
                value_field: "id".to_string(),
                label_field: "name".to_string(),
                required_for_interactions: vec![crate::InteractionId::new("discover").unwrap()],
            }),
            nav_icon: None,
            tab_group: None,
            tab_group_label: None,
        };

        let json = serde_json::to_string(&descriptor).expect("serialize");
        let deserialized: SurfaceDescriptor = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(descriptor, deserialized);

        let context_selector = deserialized.context_selector.unwrap();
        assert_eq!(context_selector.param_key, "plugin_config_id");
        assert_eq!(
            context_selector.required_for_interactions,
            vec![crate::InteractionId::new("discover").unwrap()]
        );
    }

    #[test]
    fn surface_descriptor_without_context_selector_omits_field_in_json() {
        let descriptor = SurfaceDescriptor {
            surface_id: SurfaceId::new("test.surface").unwrap(),
            label: "Test".to_string(),
            priority: 100,
            slot: "surface.page".to_string(),
            scope: Scope::Global,
            targeting: Targeting::Universal,
            required_permission: None,
            provider_kind: ProviderKind::Plugin,
            required_capabilities: CapabilitySet::default(),
            root_node: SurfaceNode::section(None::<String>, vec![]),
            context_selector: None,
            nav_icon: None,
            tab_group: None,
            tab_group_label: None,
        };

        let json = serde_json::to_string(&descriptor).expect("serialize");
        assert!(
            !json.contains("context_selector"),
            "absent context_selector must be omitted from JSON"
        );
    }

    #[test]
    fn surface_table_cell_type_entity_link_serializes_correctly() {
        let mut col = SurfaceTableColumn::new("host", "Host");
        col.cell_type = Some(SurfaceTableCellType::EntityLink {
            entity_type: SurfaceEntityType::Host,
        });
        let json = serde_json::to_string(&col).expect("serialize");
        let parsed: serde_json::Value = serde_json::from_str(&json).expect("parse");
        assert_eq!(parsed["cell_type"]["kind"], "entity_link");
        assert_eq!(parsed["cell_type"]["entity_type"], "host");
    }

    #[test]
    fn surface_table_column_without_cell_type_omits_field() {
        let col = SurfaceTableColumn::new("name", "Name");
        let json = serde_json::to_string(&col).expect("serialize");
        assert!(!json.contains("cell_type"));
    }

    #[test]
    fn unknown_cell_type_deserializes_to_none() {
        let json =
            r#"{"key":"host","label":"Host","cell_type":{"kind":"future_type","extra":"data"}}"#;
        let col: SurfaceTableColumn = serde_json::from_str(json).expect("deserialize");
        assert!(col.cell_type.is_none());
    }

    #[test]
    fn surface_entity_type_host_serializes_to_bare_string() {
        let t = SurfaceEntityType::Host;
        let s = serde_json::to_string(&t).expect("serialize");
        assert_eq!(s, r#""host""#);
    }

    #[test]
    fn surface_entity_type_other_serializes_to_bare_string() {
        let t = SurfaceEntityType::Other("my_future_type".to_string());
        let s = serde_json::to_string(&t).expect("serialize");
        assert_eq!(s, r#""my_future_type""#);
    }

    #[test]
    fn surface_entity_type_unknown_string_deserializes_to_other() {
        let t: SurfaceEntityType = serde_json::from_str(r#""unknown_type""#).expect("deserialize");
        assert_eq!(t, SurfaceEntityType::Other("unknown_type".to_string()));
    }

    #[test]
    fn surface_entity_ref_unresolved_serializes_without_label_or_found() {
        let entity_id = uuid::Uuid::parse_str("00000000-0000-0000-0000-000000000001").unwrap();
        let r = SurfaceEntityRef::unresolved(entity_id);
        let json = serde_json::to_string(&r).expect("serialize");
        let val: serde_json::Value = serde_json::from_str(&json).expect("parse");
        assert_eq!(val["entity_id"], entity_id.to_string());
        assert!(val.get("label").is_none());
        assert!(val.get("found").is_none());
    }

    #[test]
    fn entity_link_column_capability_serializes_to_snake_case() {
        let cap = Capability::EntityLinkColumn;
        let s = serde_json::to_string(&cap).expect("serialize");
        assert_eq!(s, r#""entity_link_column""#);
    }
}