bmux_plugin_sdk 0.0.1-alpha.0

Plugin SDK for bmux — the types and traits plugin authors need
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
//! Prompt data types for building interactive user prompts.
//!
//! These types define the structure of prompt requests, field types, validation
//! rules, and response values.  The actual prompt host registration and
//! async submission machinery lives in `bmux_cli::runtime::prompt` — this
//! module only provides the serializable data model so that plugins can
//! construct prompts without depending on the full CLI crate.

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::sync::atomic::{AtomicU64, Ordering};

// ── Prompt policy & layout ───────────────────────────────────────────────────

/// Controls how a prompt request interacts with already-queued prompts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PromptPolicy {
    /// Always append to the end of the queue.
    Enqueue,
    /// Cancel the active prompt (if any) and jump to the front.
    ReplaceActive,
    /// If another prompt is already active or queued, reject immediately
    /// with [`PromptResponse::RejectedBusy`].
    RejectIfBusy,
}

/// Width constraints for the prompt overlay.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct PromptWidth {
    pub min: u16,
    pub max: u16,
}

impl Default for PromptWidth {
    fn default() -> Self {
        Self { min: 40, max: 92 }
    }
}

// ── Options ──────────────────────────────────────────────────────────────────

/// A selectable option for [`PromptField::SingleSelect`] and
/// [`PromptField::MultiToggle`] prompts.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PromptOption {
    pub value: String,
    pub label: String,
}

impl PromptOption {
    #[must_use]
    pub fn new(value: impl Into<String>, label: impl Into<String>) -> Self {
        Self {
            value: value.into(),
            label: label.into(),
        }
    }
}

// ── Validation ───────────────────────────────────────────────────────────────

/// Validation rules for [`PromptField::TextInput`] fields.
///
/// When a validation rule is set, the prompt UI will check the user's input
/// on submission and display an inline error if the value is invalid.  The
/// prompt stays open until the user corrects the input or cancels.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PromptValidation {
    /// The value must not be empty or whitespace-only.
    NonEmpty,
    /// The value must parse as a positive integer (`u64 > 0`).
    PositiveInteger,
    /// The value must parse as an integer (`i64`).
    Integer,
    /// The value must parse as a floating-point number (`f64`).
    Number,
    /// The value must match a regular expression pattern.
    Regex {
        /// The regex pattern (as a string).
        pattern: String,
        /// Human-readable error message shown when validation fails.
        message: String,
    },
}

impl PromptValidation {
    /// Validate a text input value against this rule.
    ///
    /// Returns `Ok(())` if valid, or `Err(message)` with a human-readable
    /// error description.
    ///
    /// # Errors
    ///
    /// Returns `Err(String)` with a human-readable message when the value
    /// does not satisfy the validation rule.
    pub fn validate(&self, value: &str) -> Result<(), String> {
        match self {
            Self::NonEmpty => {
                if value.trim().is_empty() {
                    Err("value must not be empty".to_string())
                } else {
                    Ok(())
                }
            }
            Self::PositiveInteger => match value.trim().parse::<u64>() {
                Ok(0) => Err("value must be a positive integer (> 0)".to_string()),
                Ok(_) => Ok(()),
                Err(_) => Err("value must be a positive integer".to_string()),
            },
            Self::Integer => {
                if value.trim().parse::<i64>().is_ok() {
                    Ok(())
                } else {
                    Err("value must be an integer".to_string())
                }
            }
            Self::Number => {
                if value.trim().parse::<f64>().is_ok() {
                    Ok(())
                } else {
                    Err("value must be a number".to_string())
                }
            }
            Self::Regex { pattern, message } => {
                // Best-effort regex validation using a simple approach.
                // The full regex crate is intentionally not a dependency of
                // the SDK — callers that need regex validation should ensure
                // the pattern is valid before constructing the prompt.
                //
                // At the SDK level we store the pattern; the host (bmux_cli)
                // performs the actual regex match with the `regex` crate.
                //
                // This method always returns Ok for Regex variants — the host
                // prompt UI is responsible for the actual match.
                let _ = (pattern, message);
                Ok(())
            }
        }
    }
}

// ── Field types ──────────────────────────────────────────────────────────────

/// The concrete field type for a prompt.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PromptField {
    /// Yes / No confirmation.
    Confirm {
        default: bool,
        yes_label: String,
        no_label: String,
    },
    /// Free-text input.
    TextInput {
        initial_value: String,
        placeholder: Option<String>,
        required: bool,
        /// Optional validation rule applied on submission.
        validation: Option<PromptValidation>,
    },
    /// Pick one option from a list.
    SingleSelect {
        options: Vec<PromptOption>,
        default_index: usize,
        /// Emit selection-change events while the user moves through the list.
        /// Hosts can use this for live previews without waiting for submit.
        live_preview: bool,
    },
    /// Fuzzy-search a list and pick one option.
    SearchSelect {
        options: Vec<PromptOption>,
        default_index: usize,
        placeholder: Option<String>,
        /// Emit selection-change events while the user moves through the filtered list.
        /// Hosts can use this for live previews without waiting for submit.
        live_preview: bool,
    },
    /// Toggle multiple options on/off.
    MultiToggle {
        options: Vec<PromptOption>,
        default_indices: Vec<usize>,
        min_selected: usize,
    },
    /// Multi-field settings form. This is intentionally generic so plugins can
    /// use it for advanced configuration without the prompt host knowing the
    /// plugin domain.
    Form {
        sections: Vec<PromptFormSection>,
        live_preview: bool,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PromptFormSection {
    pub id: String,
    pub title: String,
    pub description: Option<String>,
    pub fields: Vec<PromptFormField>,
}

impl PromptFormSection {
    #[must_use]
    pub fn new(
        id: impl Into<String>,
        title: impl Into<String>,
        fields: Vec<PromptFormField>,
    ) -> Self {
        Self {
            id: id.into(),
            title: title.into(),
            description: None,
            fields,
        }
    }

    #[must_use]
    pub fn description(mut self, value: impl Into<String>) -> Self {
        self.description = Some(value.into());
        self
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PromptFormField {
    pub id: String,
    pub label: String,
    pub description: Option<String>,
    pub disabled: bool,
    pub disabled_reason: Option<String>,
    pub required: bool,
    pub kind: PromptFormFieldKind,
}

impl PromptFormField {
    #[must_use]
    pub fn new(id: impl Into<String>, label: impl Into<String>, kind: PromptFormFieldKind) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            description: None,
            disabled: false,
            disabled_reason: None,
            required: false,
            kind,
        }
    }

    #[must_use]
    pub fn description(mut self, value: impl Into<String>) -> Self {
        self.description = Some(value.into());
        self
    }

    #[must_use]
    pub fn disabled(mut self, reason: impl Into<String>) -> Self {
        self.disabled = true;
        self.disabled_reason = Some(reason.into());
        self
    }

    #[must_use]
    pub const fn required(mut self, required: bool) -> Self {
        self.required = required;
        self
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PromptFormFieldKind {
    Bool {
        default: bool,
    },
    Text {
        initial_value: String,
        placeholder: Option<String>,
        validation: Option<PromptValidation>,
    },
    Integer {
        initial_value: i64,
        min: Option<i64>,
        max: Option<i64>,
    },
    Number {
        initial_value: String,
        min: Option<String>,
        max: Option<String>,
    },
    SingleSelect {
        options: Vec<PromptOption>,
        default_index: usize,
    },
    MultiToggle {
        options: Vec<PromptOption>,
        default_indices: Vec<usize>,
        min_selected: usize,
    },
}

// ── Prompt request ───────────────────────────────────────────────────────────

static PROMPT_REQUEST_SEQUENCE: AtomicU64 = AtomicU64::new(1);

fn next_prompt_id() -> u64 {
    PROMPT_REQUEST_SEQUENCE.fetch_add(1, Ordering::Relaxed)
}

/// A complete prompt request.
///
/// Constructed via the builder methods [`PromptRequest::confirm`],
/// [`PromptRequest::text_input`], [`PromptRequest::single_select`], and
/// [`PromptRequest::multi_toggle`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PromptRequest {
    pub id: u64,
    pub owner_plugin_id: Option<String>,
    pub modal_id: Option<String>,
    pub title: String,
    pub message: Option<String>,
    pub submit_label: String,
    pub cancel_label: String,
    pub esc_cancels: bool,
    pub policy: PromptPolicy,
    pub width: PromptWidth,
    pub field: PromptField,
}

impl PromptRequest {
    // ── Constructors ─────────────────────────────────────────────────

    #[must_use]
    pub fn confirm(title: impl Into<String>) -> Self {
        Self {
            id: next_prompt_id(),
            owner_plugin_id: None,
            modal_id: None,
            title: title.into(),
            message: None,
            submit_label: "Submit".to_string(),
            cancel_label: "Cancel".to_string(),
            esc_cancels: true,
            policy: PromptPolicy::Enqueue,
            width: PromptWidth::default(),
            field: PromptField::Confirm {
                default: false,
                yes_label: "Yes".to_string(),
                no_label: "No".to_string(),
            },
        }
    }

    #[must_use]
    pub fn text_input(title: impl Into<String>) -> Self {
        Self {
            id: next_prompt_id(),
            owner_plugin_id: None,
            modal_id: None,
            title: title.into(),
            message: None,
            submit_label: "Submit".to_string(),
            cancel_label: "Cancel".to_string(),
            esc_cancels: true,
            policy: PromptPolicy::Enqueue,
            width: PromptWidth::default(),
            field: PromptField::TextInput {
                initial_value: String::new(),
                placeholder: None,
                required: false,
                validation: None,
            },
        }
    }

    #[must_use]
    pub fn single_select(title: impl Into<String>, options: Vec<PromptOption>) -> Self {
        Self {
            id: next_prompt_id(),
            owner_plugin_id: None,
            modal_id: None,
            title: title.into(),
            message: None,
            submit_label: "Select".to_string(),
            cancel_label: "Cancel".to_string(),
            esc_cancels: true,
            policy: PromptPolicy::Enqueue,
            width: PromptWidth::default(),
            field: PromptField::SingleSelect {
                options,
                default_index: 0,
                live_preview: false,
            },
        }
    }

    #[must_use]
    pub fn search_select(title: impl Into<String>, options: Vec<PromptOption>) -> Self {
        Self {
            id: next_prompt_id(),
            owner_plugin_id: None,
            modal_id: None,
            title: title.into(),
            message: None,
            submit_label: "Select".to_string(),
            cancel_label: "Cancel".to_string(),
            esc_cancels: true,
            policy: PromptPolicy::Enqueue,
            width: PromptWidth::default(),
            field: PromptField::SearchSelect {
                options,
                default_index: 0,
                placeholder: Some("Type to search".to_string()),
                live_preview: false,
            },
        }
    }

    #[must_use]
    pub fn multi_toggle(title: impl Into<String>, options: Vec<PromptOption>) -> Self {
        Self {
            id: next_prompt_id(),
            owner_plugin_id: None,
            modal_id: None,
            title: title.into(),
            message: None,
            submit_label: "Apply".to_string(),
            cancel_label: "Cancel".to_string(),
            esc_cancels: true,
            policy: PromptPolicy::Enqueue,
            width: PromptWidth::default(),
            field: PromptField::MultiToggle {
                options,
                default_indices: Vec::new(),
                min_selected: 0,
            },
        }
    }

    #[must_use]
    pub fn form(title: impl Into<String>, sections: Vec<PromptFormSection>) -> Self {
        Self {
            id: next_prompt_id(),
            owner_plugin_id: None,
            modal_id: None,
            title: title.into(),
            message: None,
            submit_label: "Apply".to_string(),
            cancel_label: "Cancel".to_string(),
            esc_cancels: true,
            policy: PromptPolicy::Enqueue,
            width: PromptWidth::default(),
            field: PromptField::Form {
                sections,
                live_preview: false,
            },
        }
    }

    // ── Builder methods ──────────────────────────────────────────────

    #[must_use]
    pub fn message(mut self, message: impl Into<String>) -> Self {
        self.message = Some(message.into());
        self
    }

    #[must_use]
    pub fn submit_label(mut self, label: impl Into<String>) -> Self {
        self.submit_label = label.into();
        self
    }

    #[must_use]
    pub fn cancel_label(mut self, label: impl Into<String>) -> Self {
        self.cancel_label = label.into();
        self
    }

    #[must_use]
    pub const fn esc_cancels(mut self, enabled: bool) -> Self {
        self.esc_cancels = enabled;
        self
    }

    #[must_use]
    pub const fn policy(mut self, policy: PromptPolicy) -> Self {
        self.policy = policy;
        self
    }

    #[must_use]
    pub const fn width_range(mut self, min: u16, max: u16) -> Self {
        let normalized = if min <= max {
            PromptWidth { min, max }
        } else {
            PromptWidth { min: max, max: min }
        };
        self.width = normalized;
        self
    }

    #[must_use]
    pub fn owner_plugin_id(mut self, value: impl Into<String>) -> Self {
        self.owner_plugin_id = Some(value.into());
        self
    }

    #[must_use]
    pub fn modal_id(mut self, value: impl Into<String>) -> Self {
        self.modal_id = Some(value.into());
        self
    }

    #[must_use]
    pub const fn confirm_default(mut self, default: bool) -> Self {
        if let PromptField::Confirm {
            default: slot_default,
            ..
        } = &mut self.field
        {
            *slot_default = default;
        }
        self
    }

    #[must_use]
    pub fn confirm_labels(mut self, yes: impl Into<String>, no: impl Into<String>) -> Self {
        if let PromptField::Confirm {
            yes_label,
            no_label,
            ..
        } = &mut self.field
        {
            *yes_label = yes.into();
            *no_label = no.into();
        }
        self
    }

    #[must_use]
    pub fn input_initial(mut self, value: impl Into<String>) -> Self {
        if let PromptField::TextInput { initial_value, .. } = &mut self.field {
            *initial_value = value.into();
        }
        self
    }

    #[must_use]
    pub fn input_placeholder(mut self, value: impl Into<String>) -> Self {
        if let PromptField::TextInput { placeholder, .. } = &mut self.field {
            *placeholder = Some(value.into());
        }
        self
    }

    #[must_use]
    pub const fn input_required(mut self, required: bool) -> Self {
        if let PromptField::TextInput {
            required: slot_required,
            ..
        } = &mut self.field
        {
            *slot_required = required;
        }
        self
    }

    /// Set a validation rule for a [`PromptField::TextInput`] field.
    ///
    /// The validation is checked when the user presses Enter.  If it fails,
    /// the prompt stays open and an error message is displayed inline.
    #[must_use]
    pub fn input_validation(mut self, validation: PromptValidation) -> Self {
        if let PromptField::TextInput {
            validation: slot, ..
        } = &mut self.field
        {
            *slot = Some(validation);
        }
        self
    }

    #[must_use]
    pub const fn single_default_index(mut self, index: usize) -> Self {
        if let PromptField::SingleSelect { default_index, .. } = &mut self.field {
            *default_index = index;
        }
        self
    }

    #[must_use]
    pub const fn single_live_preview(mut self, enabled: bool) -> Self {
        match &mut self.field {
            PromptField::SingleSelect { live_preview, .. }
            | PromptField::SearchSelect { live_preview, .. } => {
                *live_preview = enabled;
            }
            _ => {}
        }
        self
    }

    #[must_use]
    pub fn search_placeholder(mut self, value: impl Into<String>) -> Self {
        if let PromptField::SearchSelect { placeholder, .. } = &mut self.field {
            *placeholder = Some(value.into());
        }
        self
    }

    #[must_use]
    pub fn multi_defaults(mut self, indices: Vec<usize>) -> Self {
        if let PromptField::MultiToggle {
            default_indices, ..
        } = &mut self.field
        {
            *default_indices = indices;
        }
        self
    }

    #[must_use]
    pub const fn multi_min_selected(mut self, min_selected: usize) -> Self {
        if let PromptField::MultiToggle {
            min_selected: slot_min,
            ..
        } = &mut self.field
        {
            *slot_min = min_selected;
        }
        self
    }

    #[must_use]
    pub const fn form_live_preview(mut self, enabled: bool) -> Self {
        if let PromptField::Form { live_preview, .. } = &mut self.field {
            *live_preview = enabled;
        }
        self
    }
}

// ── Response types ───────────────────────────────────────────────────────────

/// The typed value extracted from a completed prompt.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PromptValue {
    Confirm(bool),
    Text(String),
    Single(String),
    Multi(Vec<String>),
    Form(BTreeMap<String, PromptFormValue>),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PromptFormValue {
    Bool(bool),
    Text(String),
    Integer(i64),
    Number(String),
    Single(String),
    Multi(Vec<String>),
}

/// The outcome of a prompt interaction.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PromptResponse {
    /// The user submitted a value.
    Submitted(PromptValue),
    /// The user cancelled (e.g. pressed Esc).
    Cancelled,
    /// The prompt was rejected because the host was busy and the policy
    /// was [`PromptPolicy::RejectIfBusy`].
    RejectedBusy,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PromptEvent {
    SelectionChanged {
        index: usize,
        value: String,
    },
    FormChanged {
        field_id: String,
        value: PromptFormValue,
        values: BTreeMap<String, PromptFormValue>,
    },
}

impl PromptResponse {
    #[must_use]
    pub const fn submitted_value(&self) -> Option<&PromptValue> {
        if let Self::Submitted(value) = self {
            Some(value)
        } else {
            None
        }
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

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

    #[test]
    fn validation_non_empty_rejects_blank() {
        let rule = PromptValidation::NonEmpty;
        assert!(rule.validate("hello").is_ok());
        assert!(rule.validate("").is_err());
        assert!(rule.validate("   ").is_err());
    }

    #[test]
    fn validation_positive_integer_accepts_valid() {
        let rule = PromptValidation::PositiveInteger;
        assert!(rule.validate("1").is_ok());
        assert!(rule.validate("42").is_ok());
        assert!(rule.validate("0").is_err());
        assert!(rule.validate("-1").is_err());
        assert!(rule.validate("abc").is_err());
        assert!(rule.validate("").is_err());
    }

    #[test]
    fn validation_integer_accepts_negative() {
        let rule = PromptValidation::Integer;
        assert!(rule.validate("0").is_ok());
        assert!(rule.validate("-42").is_ok());
        assert!(rule.validate("100").is_ok());
        assert!(rule.validate("3.14").is_err());
        assert!(rule.validate("abc").is_err());
    }

    #[test]
    fn validation_number_accepts_float() {
        let rule = PromptValidation::Number;
        assert!(rule.validate("3.14").is_ok());
        assert!(rule.validate("-0.5").is_ok());
        assert!(rule.validate("42").is_ok());
        assert!(rule.validate("abc").is_err());
    }

    #[test]
    fn validation_regex_defers_to_host() {
        let rule = PromptValidation::Regex {
            pattern: r"^\d+$".to_string(),
            message: "digits only".to_string(),
        };
        // SDK-level validate always returns Ok for Regex — host handles it.
        assert!(rule.validate("anything").is_ok());
    }

    #[test]
    fn text_input_builder_sets_validation() {
        let request = PromptRequest::text_input("Duration")
            .input_validation(PromptValidation::PositiveInteger);
        let PromptField::TextInput { validation, .. } = &request.field else {
            panic!("expected TextInput");
        };
        assert_eq!(validation, &Some(PromptValidation::PositiveInteger));
    }

    #[test]
    fn prompt_response_submitted_value() {
        let response = PromptResponse::Submitted(PromptValue::Text("hello".into()));
        assert_eq!(
            response.submitted_value(),
            Some(&PromptValue::Text("hello".into()))
        );

        let cancelled = PromptResponse::Cancelled;
        assert_eq!(cancelled.submitted_value(), None);
    }

    #[test]
    fn prompt_form_request_round_trips_through_service_codec() {
        let request = PromptRequest::form(
            "Performance settings",
            vec![PromptFormSection::new(
                "general",
                "General",
                vec![
                    PromptFormField::new(
                        "enabled",
                        "Enabled",
                        PromptFormFieldKind::Bool { default: true },
                    ),
                    PromptFormField::new(
                        "sample_interval_ms",
                        "Sample interval",
                        PromptFormFieldKind::Integer {
                            initial_value: 1_000,
                            min: Some(250),
                            max: Some(60_000),
                        },
                    ),
                    PromptFormField::new(
                        "label",
                        "Label",
                        PromptFormFieldKind::Text {
                            initial_value: "CPU".to_string(),
                            placeholder: Some("Metric label".to_string()),
                            validation: Some(PromptValidation::NonEmpty),
                        },
                    ),
                ],
            )],
        );

        let payload = crate::encode_service_message(&request).expect("encode prompt request");
        let decoded: PromptRequest =
            crate::decode_service_message(&payload).expect("decode prompt request");

        assert_eq!(decoded, request);
    }

    #[test]
    fn prompt_form_values_round_trip_through_service_codec() {
        let values = BTreeMap::from([
            ("enabled".to_string(), PromptFormValue::Bool(true)),
            (
                "sample_interval_ms".to_string(),
                PromptFormValue::Integer(1_000),
            ),
            (
                "metrics".to_string(),
                PromptFormValue::Multi(vec!["cpu".to_string(), "memory".to_string()]),
            ),
        ]);

        let payload = crate::encode_service_message(&values).expect("encode form values");
        let decoded: BTreeMap<String, PromptFormValue> =
            crate::decode_service_message(&payload).expect("decode form values");

        assert_eq!(decoded, values);
    }
}