kaptein-viewmodel 0.28.0

Kaptein view-model: renderer-agnostic logic (the product)
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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
//! View-definition (lens) schema — the declarative, **data-first** extension tier
//! (ADR-0004 tier 1, ADR-0012).
//!
//! A lens binds a CRD (or built-in resource) to columns, status inference, and actions
//! with **no code** — it is a YAML/JSON document checked into Git and PR-reviewed. This
//! module is the renderer-agnostic *semantics* of a lens: the data model and the
//! validation that decide whether a lens is well-formed. The frontends render it; the
//! core evaluates it. This module is wasm-pure (serde only, no `kube`/`tokio`), so the
//! browser UI and the headless agent share the same validation as the CLI.
//!
//! The **schema** (the JSON Schema document + example lenses) is MIT/Apache-2.0 per
//! ADR-0004's licensing split; this Rust implementation lives in the BUSL core.

use serde::{Deserialize, Serialize};

use crate::render::{Cell, Row, RowId};
use crate::surface::{Column, ColumnKind};

/// The lens schema version this release validates. Bumped on a breaking change to the
/// lens schema (see `docs/versioning.md`); a lens declaring a different `api_version` is
/// refused with a migration error.
pub const LENS_SCHEMA_VERSION: u32 = 1;

/// The resource a lens describes: `group` (empty for core), `version`, `kind`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GroupVersionKind {
    /// API group, e.g. `""` (core), `"apps"`, `"postgresql.cnpg.io"`.
    #[serde(default)]
    pub group: String,
    /// API version, e.g. `"v1"`.
    pub version: String,
    /// Resource kind, e.g. `"Pod"`, `"Cluster"` (CNPG), `"VirtualMachine"` (KubeVirt).
    pub kind: String,
}

impl GroupVersionKind {
    /// A compact `group/version/kind` string for diagnostics (group omitted for core).
    pub fn display(&self) -> String {
        if self.group.is_empty() {
            format!("{}/{}", self.version, self.kind)
        } else {
            format!("{}/{}/{}", self.group, self.version, self.kind)
        }
    }
}

/// A comparison operator in a status-inference rule.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RuleOp {
    /// `field == value` (string/number/bool equality).
    Eq,
    /// `field != value`.
    Ne,
    /// `field > value` (numeric).
    Gt,
    /// `field >= value` (numeric).
    Gte,
    /// `field < value` (numeric).
    Lt,
    /// `field <= value` (numeric).
    Lte,
    /// `field contains value` (substring).
    Contains,
}

/// A status-inference rule: when `field` `op` `value` holds, assign `level`.
///
/// The `field` is a dotted JSON path (e.g. `status.phase`, `spec.replicas`); the core
/// resolves it against the live object. This is declarative, so the schema is
/// structural — no free-form expression language (that lands with the full lens engine
/// in later M2.2 work).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StatusRule {
    /// Dotted JSON path to the field, e.g. `"status.phase"`.
    pub field: String,
    /// The comparison.
    pub op: RuleOp,
    /// The value to compare against (string, number, or bool).
    pub value: serde_json::Value,
    /// The level to assign when the rule matches.
    pub level: crate::render::StatusLevel,
}

/// A status-inference rule over Kubernetes conditions (`status.conditions[]`).
///
/// The scalar [`StatusRule`] cannot express how the majority of modern CRDs signal
/// readiness — via a typed condition (`type` + `status`) rather than a bare phase. This
/// rule matches the first condition whose `type` equals [`Self::condition_type`]; if
/// that condition's `status` equals [`Self::status`], the rule fires at [`Self::level`].
/// This is what lets Strimzi Kafka, KubeVirt VirtualMachine, cert-manager Certificate,
/// Keycloak, Tekton PipelineRun, Karpenter NodePool, and Knative Service all be declared
/// as data (ADR-0012's "prove the schema against the hardest lenses" test).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ConditionRule {
    /// The condition `type` to match, e.g. `"Ready"`, `"ReconciliationSucceeded"`.
    pub condition_type: String,
    /// The condition `status` to match: `"True"`, `"False"`, or `"Unknown"` (the three
    /// canonical Kubernetes condition statuses).
    pub status: String,
    /// The level to assign when the condition matches.
    pub level: crate::render::StatusLevel,
}

/// An action a lens declares. This is the lens-native form (snake_case `state`) — it
/// maps to the render contract's `semantic::Action` at evaluation time. It is a separate
/// type so the lens schema stays its own clean, user-authored contract.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LensAction {
    /// Stable action id, e.g. `"describe"`.
    pub id: String,
    /// Message key resolved by the frontend for i18n.
    pub label_key: String,
    /// Initial RBAC-preflight state (`allowed` — the core will grey it out if preflight
    /// denies it).
    #[serde(rename = "state", default = "default_action_state")]
    pub state: String,
}

fn default_action_state() -> String {
    "allowed".into()
}

/// The view-definition (lens) document.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ViewDefinition {
    /// Unique reverse-DNS id, e.g. `"com.example.cnpg-lens"`.
    pub id: String,
    /// The lens schema version this document was written against. Must equal
    /// [`LENS_SCHEMA_VERSION`] (this release refuses anything else).
    pub api_version: u32,
    /// The resource kind this lens describes.
    pub target: GroupVersionKind,
    /// Columns to render (each is a view-model `Column`; semantics, not geometry).
    #[serde(default)]
    pub columns: Vec<Column>,
    /// Optional status-inference rules, evaluated in order (first match wins).
    #[serde(default)]
    pub status: Vec<StatusRule>,
    /// Optional condition-based status rules (`status.conditions[]`), evaluated after
    /// `status` (first match wins within the whole sequence).
    #[serde(default)]
    pub conditions: Vec<ConditionRule>,
    /// Actions this lens makes available, with their RBAC-preflight state.
    #[serde(default)]
    pub actions: Vec<LensAction>,
}

/// Validate a view definition, returning a list of problems (empty = valid).
///
/// This is the "reviewable in PRs" gate: a lens that fails validation is refused, never
/// silently ignored — exactly like a prod-regex typo in the config.
pub fn validate_viewdef(vd: &ViewDefinition) -> Vec<String> {
    let mut problems = Vec::new();

    if vd.id.trim().is_empty() {
        problems.push("id: must not be empty".into());
    } else if !vd.id.contains('.') {
        problems.push(format!(
            "id {:?}: must be reverse-DNS (e.g. \"com.example.cnpg-lens\")",
            vd.id
        ));
    }

    if vd.api_version != LENS_SCHEMA_VERSION {
        problems.push(format!(
            "api_version: this release supports lens schema v{LENS_SCHEMA_VERSION}, but the \
             lens declares v{} — a migration is required (docs/versioning.md)",
            vd.api_version
        ));
    }

    if vd.target.version.trim().is_empty() {
        problems.push("target.version: must not be empty".into());
    }
    if vd.target.kind.trim().is_empty() {
        problems.push("target.kind: must not be empty".into());
    }

    // Column ids must be unique and non-empty.
    let mut seen = std::collections::HashSet::new();
    for col in &vd.columns {
        if col.id.trim().is_empty() {
            problems.push("columns: a column has an empty id".into());
        } else if !seen.insert(col.id.as_str()) {
            problems.push(format!("columns: duplicate column id {:?}", col.id));
        }
        if !valid_header_key(&col.header_key) {
            problems.push(format!(
                "columns.{:?}: header_key must be a dotted i18n key (e.g. \"col.name\")",
                col.id
            ));
        }
        // A data column's value must come from somewhere: either a `field` path, or a
        // `Status` kind (whose value is *inferred* by the status/condition rules).
        if col.kind != ColumnKind::Status && col.field.as_deref().is_none_or(str::is_empty) {
            problems.push(format!(
                "columns.{:?}: a non-status column needs a `field` (dotted JSON path) so \
                 its value is data-bound, not implicit (ADR-0012)",
                col.id
            ));
        } else if let Some(field) = col.field.as_deref()
            && !field.is_empty()
            && !valid_field_path(field)
        {
            problems.push(format!(
                "columns.{:?}.field {:?}: not a dotted JSON path",
                col.id, field
            ));
        }
    }

    // Status rules: field must be a dotted path; numeric ops need a numeric value.
    for (i, rule) in vd.status.iter().enumerate() {
        if !valid_field_path(&rule.field) {
            problems.push(format!(
                "status[{i}].field {:?}: not a dotted JSON path",
                rule.field
            ));
        }
        match rule.op {
            RuleOp::Gt | RuleOp::Gte | RuleOp::Lt | RuleOp::Lte => {
                if !rule.value.is_number() {
                    problems.push(format!(
                        "status[{i}].value: a numeric operator ({:?}) needs a numeric value",
                        rule.op
                    ));
                }
            }
            RuleOp::Contains => {
                if !rule.value.is_string() {
                    problems.push(format!(
                        "status[{i}].value: `contains` needs a string value"
                    ));
                }
            }
            RuleOp::Eq | RuleOp::Ne => {}
        }
    }

    // Action ids must be unique.
    let mut seen_actions = std::collections::HashSet::new();
    for action in &vd.actions {
        if action.id.trim().is_empty() || !seen_actions.insert(action.id.as_str()) {
            problems.push(format!(
                "actions: duplicate or empty action id {:?}",
                action.id
            ));
        }
    }

    // Condition rules: the type must be non-empty and the status must be one of the
    // canonical Kubernetes condition statuses (True/False/Unknown).
    for (i, rule) in vd.conditions.iter().enumerate() {
        if rule.condition_type.trim().is_empty() {
            problems.push(format!("conditions[{i}].condition_type: must not be empty"));
        }
        if !is_condition_status(&rule.status) {
            problems.push(format!(
                "conditions[{i}].status {:?}: must be one of \"True\", \"False\", \"Unknown\"",
                rule.status
            ));
        }
    }

    problems
}

/// A canonical Kubernetes condition status: `True`, `False`, or `Unknown`.
fn is_condition_status(status: &str) -> bool {
    matches!(status, "True" | "False" | "Unknown")
}

/// A dotted JSON field path: leading identifier, then `.identifier` segments (or
/// `[0]`-style numeric indexes).
fn valid_field_path(field: &str) -> bool {
    let mut parts = field.split('.');
    let Some(first) = parts.next() else {
        return false;
    };
    if !is_identifier(first) {
        return false;
    }
    parts.all(is_segment)
}

/// Resolve a dotted field path (with `[i]` subscripts) against a JSON object. Returns
/// `None` when the path is absent or a segment does not exist. Used by status-rule
/// evaluation so a lens can read `status.phase` or `spec.containers[0].name`.
fn resolve_field<'a>(root: &'a serde_json::Value, field: &str) -> Option<&'a serde_json::Value> {
    let mut cur = root;
    for segment in field.split('.') {
        // Split a `name[0][1]` segment into the identifier and its subscripts.
        let (ident, subscripts) = split_subscripts(segment);
        cur = cur.get(ident)?;
        for sub in subscripts {
            cur = cur.get(sub)?;
        }
    }
    Some(cur)
}

/// Split `"containers[0][1]"` into `("containers", [0, 1])`. A segment with no
/// subscripts yields an empty index list.
fn split_subscripts(segment: &str) -> (&str, Vec<usize>) {
    let mut idx = segment.len();
    let mut subs = Vec::new();
    while idx > 0 && segment[..idx].ends_with(']') {
        if let Some(open) = segment[..idx].rfind('[') {
            let inside = &segment[open + 1..idx - 1];
            if let Ok(n) = inside.parse::<usize>() {
                subs.push(n);
            }
            idx = open;
        } else {
            break;
        }
    }
    subs.reverse();
    (&segment[..idx], subs)
}

/// Evaluate a lens's status rules against a live resource's JSON representation,
/// returning the first matching level, or `None` when no rule matches. This is the
/// "status inference" half of the lens engine (ADR-0012): the frontend colors the status
/// chip from the level; the lens declares the meaning.
///
/// Scalar `status` rules are evaluated first, then `conditions` rules (first match wins
/// across the whole sequence).
pub fn evaluate_status(
    vd: &ViewDefinition,
    resource: &serde_json::Value,
) -> Option<crate::render::StatusLevel> {
    for rule in &vd.status {
        if rule_matches(rule, resource) {
            return Some(rule.level);
        }
    }
    for rule in &vd.conditions {
        if condition_matches(rule, resource) {
            return Some(rule.level);
        }
    }
    None
}

/// Render a lens + a live resource into the render contract's `Row` (ADR-0005).
///
/// This is the "status-rule *rendering*" half of M2.2: it maps a `ViewDefinition`'s
/// columns onto a resource's JSON, so a frontend (TUI/GUI/browser/headless) consumes the
/// *same* `Row`/`Cell` for the same input. Column semantics:
///
/// - A column whose `field` is set resolves that dotted path against the resource and
///   emits a typed cell (numbers → `Number`, strings/bools/null → `Text`).
/// - A `Status`-kind column's value is **inferred** via [`evaluate_status`]: the lens's
///   status/condition rules decide the `StatusLevel` and the chip label.
/// - A `field` that is absent/`None` on a `Text` column renders an empty cell; a
///   missing field on a `Status` column renders an `Info` chip (no rule matched).
///
/// The stable `RowId` is the resource `metadata.uid` when present, else
/// `namespace/name` (the same identity contract as `kaptein-integration`).
pub fn render_row(vd: &ViewDefinition, resource: &serde_json::Value) -> Row {
    let id = resource
        .get("metadata")
        .and_then(|m| m.get("uid"))
        .and_then(|u| u.as_str())
        .map(|uid| RowId(uid.to_string()))
        .unwrap_or_else(|| {
            let name = resource
                .get("metadata")
                .and_then(|m| m.get("name"))
                .and_then(|n| n.as_str())
                .unwrap_or_default();
            let ns = resource
                .get("metadata")
                .and_then(|m| m.get("namespace"))
                .and_then(|n| n.as_str())
                .unwrap_or_default();
            RowId(if ns.is_empty() {
                name.to_string()
            } else {
                format!("{ns}/{name}")
            })
        });

    let cells = vd
        .columns
        .iter()
        .map(|col| cell_for_column(col, resource, vd))
        .collect();

    Row { id, cells }
}

/// Build the `Cell` for a single lens column against a resource.
fn cell_for_column(col: &Column, resource: &serde_json::Value, vd: &ViewDefinition) -> Cell {
    if col.kind == ColumnKind::Status {
        // The status chip is *inferred* (not read from a single field): the lens's
        // rules decide the level and label.
        let (level, label) = match evaluate_status(vd, resource) {
            Some(level) => (level, level_label(level)),
            None => (crate::render::StatusLevel::Info, "unknown".to_string()),
        };
        return Cell::Status {
            level,
            label_key: label,
        };
    }

    // Data columns read a dotted field path; a missing/unset field is an empty cell.
    let Some(field) = col.field.as_deref() else {
        return empty_cell_for_kind(col.kind);
    };
    match resolve_field(resource, field) {
        Some(serde_json::Value::Number(n)) if n.is_i64() => Cell::Number {
            value: n.as_i64().unwrap_or(0),
        },
        Some(serde_json::Value::Number(n)) => Cell::Text {
            value: n.to_string(),
        },
        Some(serde_json::Value::String(s)) => Cell::Text { value: s.clone() },
        Some(serde_json::Value::Bool(b)) => Cell::Text {
            value: b.to_string(),
        },
        Some(serde_json::Value::Null) | None => empty_cell_for_kind(col.kind),
        Some(other) => Cell::Text {
            value: other.to_string(),
        },
    }
}

/// An empty cell matching a column's kind (empty text, or `0` for numbers).
fn empty_cell_for_kind(kind: ColumnKind) -> Cell {
    match kind {
        ColumnKind::Number => Cell::Number { value: 0 },
        _ => Cell::Text {
            value: String::new(),
        },
    }
}

/// A stable, i18n-facing label for a status level (the frontend resolves the key).
fn level_label(level: crate::render::StatusLevel) -> String {
    match level {
        crate::render::StatusLevel::Ok => "status.ok".into(),
        crate::render::StatusLevel::Info => "status.info".into(),
        crate::render::StatusLevel::Warning => "status.warning".into(),
        crate::render::StatusLevel::Error => "status.error".into(),
        crate::render::StatusLevel::Pending => "status.pending".into(),
    }
}

/// Match a condition rule: find `status.conditions[]` and look for a condition whose
/// `type` equals the rule's type and whose `status` equals the rule's status.
fn condition_matches(rule: &ConditionRule, resource: &serde_json::Value) -> bool {
    let Some(conditions) = resource.get("status").and_then(|s| s.get("conditions")) else {
        return false;
    };
    let Some(list) = conditions.as_array() else {
        return false;
    };
    list.iter().any(|cond| {
        cond.get("type").and_then(|t| t.as_str()) == Some(rule.condition_type.as_str())
            && cond.get("status").and_then(|s| s.as_str()) == Some(rule.status.as_str())
    })
}

fn rule_matches(rule: &StatusRule, resource: &serde_json::Value) -> bool {
    let Some(actual) = resolve_field(resource, &rule.field) else {
        return false;
    };
    match rule.op {
        RuleOp::Eq => actual == &rule.value,
        RuleOp::Ne => actual != &rule.value,
        RuleOp::Gt | RuleOp::Gte | RuleOp::Lt | RuleOp::Lte => {
            // Numeric comparison; non-numeric operands never match.
            let (Some(a), Some(b)) = (actual.as_i64(), rule.value.as_i64()) else {
                return false;
            };
            match rule.op {
                RuleOp::Gt => a > b,
                RuleOp::Gte => a >= b,
                RuleOp::Lt => a < b,
                RuleOp::Lte => a <= b,
                _ => unreachable!(),
            }
        }
        RuleOp::Contains => match (actual.as_str(), rule.value.as_str()) {
            (Some(a), Some(b)) => a.contains(b),
            _ => false,
        },
    }
}

fn is_segment(seg: &str) -> bool {
    // A segment is an identifier, optionally followed by one or more `[index]`
    // subscripts: `containers`, `containers[0]`, `containers[0][1]`.
    let mut idx = seg.len();
    while idx > 0 && seg[..idx].ends_with(']') {
        let Some(open) = seg[..idx].rfind('[') else {
            return false;
        };
        let inside = &seg[open + 1..idx - 1];
        if inside.is_empty() || !inside.chars().all(|c| c.is_ascii_digit()) {
            return false;
        }
        idx = open;
    }
    is_identifier(&seg[..idx])
}

fn is_identifier(s: &str) -> bool {
    !s.is_empty()
        && s.chars()
            .enumerate()
            .all(|(i, c)| c.is_alphanumeric() || c == '_' || (i > 0 && c == '-'))
}

/// A dotted i18n header key (e.g. `"col.name"`): leading identifier, then `.identifier`
/// segments. Reuses the identifier rules of the view-model's message keys.
fn valid_header_key(key: &str) -> bool {
    key.split('.').all(is_identifier) && key.contains('.')
}

/// A concrete example: a built-in `Column` set for a CNPG `Cluster` (the hardest-lens
/// acceptance test from ADR-0012). This is data, not code — it is what a lens file
/// declares, and it validates cleanly.
pub fn example_cnpg_columns() -> Vec<Column> {
    vec![
        Column {
            id: "name".into(),
            header_key: "col.name".into(),
            kind: ColumnKind::Text,
            sortable: true,
            field: Some("metadata.name".into()),
        },
        Column {
            id: "instances".into(),
            header_key: "col.instances".into(),
            kind: ColumnKind::Number,
            sortable: true,
            field: Some("spec.instances".into()),
        },
        Column {
            id: "status".into(),
            header_key: "col.status".into(),
            kind: ColumnKind::Status,
            sortable: true,
            field: None,
        },
    ]
}

/// A concrete example status rule (CNPG): "phase == ClusterIsReady" → Ok.
pub fn example_status_rule() -> StatusRule {
    StatusRule {
        field: "status.phase".into(),
        op: RuleOp::Eq,
        value: serde_json::json!("ClusterIsReady"),
        level: crate::render::StatusLevel::Ok,
    }
}

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

    fn col(id: &str) -> Column {
        Column {
            id: id.into(),
            header_key: format!("col.{id}"),
            kind: ColumnKind::Text,
            sortable: true,
            field: Some(format!("metadata.{id}")),
        }
    }

    fn status_col(id: &str) -> Column {
        Column {
            id: id.into(),
            header_key: format!("col.{id}"),
            kind: ColumnKind::Status,
            sortable: true,
            field: None,
        }
    }

    fn action(id: &str) -> LensAction {
        LensAction {
            id: id.into(),
            label_key: format!("action.{id}"),
            state: "allowed".into(),
        }
    }

    fn valid() -> ViewDefinition {
        ViewDefinition {
            id: "com.example.cnpg-lens".into(),
            api_version: LENS_SCHEMA_VERSION,
            target: GroupVersionKind {
                group: "postgresql.cnpg.io".into(),
                version: "v1".into(),
                kind: "Cluster".into(),
            },
            columns: vec![col("name"), status_col("status")],
            status: vec![example_status_rule()],
            conditions: vec![],
            actions: vec![action("describe")],
        }
    }

    #[test]
    fn valid_lens_has_no_problems() {
        assert!(validate_viewdef(&valid()).is_empty());
    }

    #[test]
    fn missing_reverse_dns_id_is_flagged() {
        let mut vd = valid();
        vd.id = "no-dot-here".into();
        let problems = validate_viewdef(&vd);
        assert!(problems.iter().any(|p| p.contains("reverse-DNS")));
    }

    #[test]
    fn wrong_api_version_is_flagged() {
        let mut vd = valid();
        vd.api_version = 999;
        let problems = validate_viewdef(&vd);
        assert!(problems.iter().any(|p| p.contains("api_version")));
    }

    #[test]
    fn duplicate_column_id_is_flagged() {
        let mut vd = valid();
        vd.columns = vec![col("name"), col("name")];
        let problems = validate_viewdef(&vd);
        assert!(problems.iter().any(|p| p.contains("duplicate column")));
    }

    #[test]
    fn numeric_op_with_string_value_is_flagged() {
        let mut vd = valid();
        vd.status = vec![StatusRule {
            field: "spec.replicas".into(),
            op: RuleOp::Gt,
            value: serde_json::json!("many"),
            level: crate::render::StatusLevel::Warning,
        }];
        let problems = validate_viewdef(&vd);
        assert!(problems.iter().any(|p| p.contains("numeric")));
    }

    #[test]
    fn contains_op_with_numeric_value_is_flagged() {
        let mut vd = valid();
        vd.status = vec![StatusRule {
            field: "status.phase".into(),
            op: RuleOp::Contains,
            value: serde_json::json!(3),
            level: crate::render::StatusLevel::Warning,
        }];
        let problems = validate_viewdef(&vd);
        assert!(problems.iter().any(|p| p.contains("contains")));
    }

    #[test]
    fn malformed_field_path_is_flagged() {
        let mut vd = valid();
        vd.status = vec![StatusRule {
            field: ".bad.path".into(),
            op: RuleOp::Eq,
            value: serde_json::json!("x"),
            level: crate::render::StatusLevel::Ok,
        }];
        let problems = validate_viewdef(&vd);
        assert!(problems.iter().any(|p| p.contains("field")));
    }

    #[test]
    fn duplicate_action_id_is_flagged() {
        let mut vd = valid();
        vd.actions = vec![action("x"), action("x")];
        let problems = validate_viewdef(&vd);
        assert!(problems.iter().any(|p| p.contains("action")));
    }

    #[test]
    fn field_path_validator_accepts_indexes() {
        assert!(valid_field_path("status.phase"));
        assert!(valid_field_path("spec.containers[0].name"));
        assert!(valid_field_path("metadata.labels.app"));
        assert!(!valid_field_path(""));
        assert!(!valid_field_path(".phase"));
        assert!(!valid_field_path("status..phase"));
    }

    #[test]
    fn resolve_field_reads_nested_and_indexed_paths() {
        let v = serde_json::json!({
            "status": {"phase": "Running"},
            "spec": {"containers": [{"name": "app"}]}
        });
        assert_eq!(
            resolve_field(&v, "status.phase"),
            Some(&serde_json::json!("Running"))
        );
        assert_eq!(
            resolve_field(&v, "spec.containers[0].name"),
            Some(&serde_json::json!("app"))
        );
        assert_eq!(resolve_field(&v, "status.nope"), None);
    }

    #[test]
    fn evaluate_status_first_match_wins() {
        let mut vd = valid();
        vd.status = vec![
            StatusRule {
                field: "status.phase".into(),
                op: RuleOp::Eq,
                value: serde_json::json!("Running"),
                level: crate::render::StatusLevel::Ok,
            },
            StatusRule {
                field: "status.phase".into(),
                op: RuleOp::Ne,
                value: serde_json::json!("Running"),
                level: crate::render::StatusLevel::Warning,
            },
        ];
        let running = serde_json::json!({"status": {"phase": "Running"}});
        assert_eq!(
            evaluate_status(&vd, &running),
            Some(crate::render::StatusLevel::Ok)
        );
        let pending = serde_json::json!({"status": {"phase": "Pending"}});
        assert_eq!(
            evaluate_status(&vd, &pending),
            Some(crate::render::StatusLevel::Warning)
        );
        let empty = serde_json::json!({});
        assert_eq!(evaluate_status(&vd, &empty), None);
    }

    #[test]
    fn numeric_rule_compares_numerically() {
        let mut vd = valid();
        vd.status = vec![StatusRule {
            field: "spec.replicas".into(),
            op: RuleOp::Gt,
            value: serde_json::json!(1),
            level: crate::render::StatusLevel::Warning,
        }];
        let three = serde_json::json!({"spec": {"replicas": 3}});
        assert_eq!(
            evaluate_status(&vd, &three),
            Some(crate::render::StatusLevel::Warning)
        );
        let one = serde_json::json!({"spec": {"replicas": 1}});
        assert_eq!(evaluate_status(&vd, &one), None);
    }

    #[test]
    fn contains_rule_matches_substring() {
        let mut vd = valid();
        vd.status = vec![StatusRule {
            field: "status.message".into(),
            op: RuleOp::Contains,
            value: serde_json::json!("back-off"),
            level: crate::render::StatusLevel::Error,
        }];
        let msg = serde_json::json!({"status": {"message": "back-off pulling image"}});
        assert_eq!(
            evaluate_status(&vd, &msg),
            Some(crate::render::StatusLevel::Error)
        );
    }

    #[test]
    fn condition_rule_matches_ready_true() {
        let mut vd = valid();
        vd.status = vec![];
        vd.conditions = vec![
            ConditionRule {
                condition_type: "Ready".into(),
                status: "True".into(),
                level: crate::render::StatusLevel::Ok,
            },
            ConditionRule {
                condition_type: "Ready".into(),
                status: "False".into(),
                level: crate::render::StatusLevel::Error,
            },
        ];
        let ready = serde_json::json!({
            "status": {"conditions": [{"type": "Ready", "status": "True"}]}
        });
        assert_eq!(
            evaluate_status(&vd, &ready),
            Some(crate::render::StatusLevel::Ok)
        );
        let not_ready = serde_json::json!({
            "status": {"conditions": [{"type": "Ready", "status": "False"}]}
        });
        assert_eq!(
            evaluate_status(&vd, &not_ready),
            Some(crate::render::StatusLevel::Error)
        );
        // A condition of a different type must not match.
        let other = serde_json::json!({
            "status": {"conditions": [{"type": "Progressing", "status": "True"}]}
        });
        assert_eq!(evaluate_status(&vd, &other), None);
        // Missing conditions must not match.
        assert_eq!(
            evaluate_status(&vd, &serde_json::json!({"status": {}})),
            None
        );
    }

    #[test]
    fn invalid_condition_status_is_flagged() {
        let mut vd = valid();
        vd.conditions = vec![ConditionRule {
            condition_type: "Ready".into(),
            status: "Yes".into(),
            level: crate::render::StatusLevel::Ok,
        }];
        let problems = validate_viewdef(&vd);
        assert!(problems.iter().any(|p| p.contains("conditions[0].status")));
    }

    #[test]
    fn empty_condition_type_is_flagged() {
        let mut vd = valid();
        vd.conditions = vec![ConditionRule {
            condition_type: "".into(),
            status: "True".into(),
            level: crate::render::StatusLevel::Ok,
        }];
        let problems = validate_viewdef(&vd);
        assert!(
            problems
                .iter()
                .any(|p| p.contains("conditions[0].condition_type"))
        );
    }

    #[test]
    fn non_status_column_without_field_is_flagged() {
        let mut vd = valid();
        // A text column with no `field` cannot be data-bound.
        vd.columns = vec![Column {
            id: "name".into(),
            header_key: "col.name".into(),
            kind: ColumnKind::Text,
            sortable: true,
            field: None,
        }];
        let problems = validate_viewdef(&vd);
        assert!(problems.iter().any(|p| p.contains("field")));
    }

    #[test]
    fn malformed_column_field_is_flagged() {
        let mut vd = valid();
        vd.columns = vec![Column {
            id: "name".into(),
            header_key: "col.name".into(),
            kind: ColumnKind::Text,
            sortable: true,
            field: Some(".bad.path".into()),
        }];
        let problems = validate_viewdef(&vd);
        assert!(
            problems
                .iter()
                .any(|p| p.contains("not a dotted JSON path"))
        );
    }

    #[test]
    fn render_row_maps_fields_and_infers_status() {
        // A lens with a name (data), instances (number), and status (inferred) column.
        let vd = ViewDefinition {
            id: "com.example.cnpg-lens".into(),
            api_version: LENS_SCHEMA_VERSION,
            target: GroupVersionKind {
                group: "postgresql.cnpg.io".into(),
                version: "v1".into(),
                kind: "Cluster".into(),
            },
            columns: vec![
                Column {
                    id: "name".into(),
                    header_key: "col.name".into(),
                    kind: ColumnKind::Text,
                    sortable: true,
                    field: Some("metadata.name".into()),
                },
                Column {
                    id: "instances".into(),
                    header_key: "col.instances".into(),
                    kind: ColumnKind::Number,
                    sortable: true,
                    field: Some("spec.instances".into()),
                },
                Column {
                    id: "status".into(),
                    header_key: "col.status".into(),
                    kind: ColumnKind::Status,
                    sortable: true,
                    field: None,
                },
            ],
            status: vec![StatusRule {
                field: "status.phase".into(),
                op: RuleOp::Eq,
                value: serde_json::json!("ClusterIsReady"),
                level: crate::render::StatusLevel::Ok,
            }],
            conditions: vec![],
            actions: vec![],
        };

        let resource = serde_json::json!({
            "metadata": {"uid": "abc-123", "name": "pg", "namespace": "db"},
            "spec": {"instances": 3},
            "status": {"phase": "ClusterIsReady"}
        });

        let row = render_row(&vd, &resource);
        // Stable identity is metadata.uid.
        assert_eq!(row.id, RowId("abc-123".into()));
        assert_eq!(row.cells.len(), 3);
        // name → Text("pg")
        assert_eq!(row.cells[0], Cell::Text { value: "pg".into() });
        // instances → Number(3)
        assert_eq!(row.cells[1], Cell::Number { value: 3 });
        // status → inferred Ok chip
        assert_eq!(
            row.cells[2],
            Cell::Status {
                level: crate::render::StatusLevel::Ok,
                label_key: "status.ok".into(),
            }
        );
    }

    #[test]
    fn render_row_falls_back_to_ns_name_identity_and_info_status() {
        let vd = ViewDefinition {
            id: "com.example.t".into(),
            api_version: LENS_SCHEMA_VERSION,
            target: GroupVersionKind {
                group: "example.io".into(),
                version: "v1".into(),
                kind: "Thing".into(),
            },
            columns: vec![Column {
                id: "status".into(),
                header_key: "col.status".into(),
                kind: ColumnKind::Status,
                sortable: true,
                field: None,
            }],
            status: vec![],
            conditions: vec![],
            actions: vec![],
        };
        // No uid → ns/name identity; no matching rule → Info "unknown" chip.
        let resource = serde_json::json!({
            "metadata": {"name": "x", "namespace": "n"}
        });
        let row = render_row(&vd, &resource);
        assert_eq!(row.id, RowId("n/x".into()));
        assert_eq!(
            row.cells[0],
            Cell::Status {
                level: crate::render::StatusLevel::Info,
                label_key: "unknown".into(),
            }
        );
    }
}