Skip to main content

facett_syschart/
lib.rs

1//! **facett-syschart** — a **system-map chart**: a handful of *free-positioned*
2//! peer nodes (not a layered DAG) joined by edges, each carrying a **badge**
3//! (a count / status number) and, when selected, an **inline expandable detail
4//! panel**. Click a node to select it; its detail renders in a panel under the
5//! canvas. The host owns *what* a node's detail is (a string here; richer hosts
6//! draw their own widgets after `ui()` keyed off [`SystemChart::selected`]).
7//!
8//! This is the piece [`facett_core::draw`]/[`facett_graph::GraphView`] don't
9//! cover — those paint a *non-interactive* `Scene`; `DepGraphView` is a
10//! *layered* DAG with a fixed deps/dependents drill-down. A "monitoring map" of
11//! a few peer systems (a PKI/OIDC/Nexus triangle, a service mesh) wants
12//! deterministic positions, a per-node badge, click-select, and a free-form
13//! detail panel — that's [`SystemChart`].
14//!
15//! ## FC-2 / FC-9 contract
16//! Built on [`facett_core::Elm`]: the complete observable state lives in one
17//! serializable [`SysChartModel`] ([`state`](facett_core::Elm::state)); every
18//! input is a [`Msg`] applied through the single mutation path
19//! [`update`](SystemChart::update) (FC-2); and [`view`](SystemChart::view) is a
20//! **pure** function of `&self` that paints and *returns* the [`Msg`]s the frame
21//! produced (FC-9). [`impl_facet_via_elm!`](facett_core::impl_facet_via_elm)
22//! writes the `impl Facet` bridge (`for m in view(ui) { update(m) }`). A headless
23//! driver ([`facett_core::harness`]) feeds a `Vec<Msg>` and snapshots
24//! [`state`](facett_core::Elm::state) with no egui / no GPU.
25
26use egui::{Align2, Color32, FontId, Pos2, Rect, Sense, Stroke, Ui, vec2};
27use facett_core::clip::{ClipKind, ClipPayload, CopySource};
28use facett_core::{FacetCaps, Semantics, a11y_node, theme};
29use serde::{Deserialize, Serialize};
30
31/// One system in the map. Positions are **normalized** (`0.0..=1.0` of the
32/// canvas), so the layout is resolution-independent and deterministic — ideal
33/// for headless rendering and screenshots.
34#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
35pub struct SysNode {
36    /// Stable id (used for selection + edges). Unique within a chart.
37    pub id: String,
38    /// Human label drawn next to the node.
39    pub label: String,
40    /// Node colour (the host picks the policy — by status, by hash, …).
41    pub color: Color32,
42    /// A small count/status badge drawn on the node (e.g. an event count).
43    pub badge: u64,
44    /// Normalized position in the canvas (`0,0` = top-left, `1,1` = bottom-right).
45    pub pos: (f32, f32),
46    /// Free-form detail shown in the inline panel when this node is selected.
47    /// Hosts that draw richer detail can leave this empty and render their own
48    /// widgets after `ui()` keyed off [`SystemChart::selected`].
49    pub detail: String,
50}
51
52impl SysNode {
53    pub fn new(id: impl Into<String>, label: impl Into<String>, color: Color32, pos: (f32, f32)) -> Self {
54        Self { id: id.into(), label: label.into(), color, badge: 0, pos, detail: String::new() }
55    }
56    pub fn badge(mut self, n: u64) -> Self {
57        self.badge = n;
58        self
59    }
60    pub fn detail(mut self, d: impl Into<String>) -> Self {
61        self.detail = d.into();
62        self
63    }
64}
65
66/// An undirected link between two node ids.
67#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
68pub struct SysEdge {
69    pub a: String,
70    pub b: String,
71}
72
73impl SysEdge {
74    pub fn new(a: impl Into<String>, b: impl Into<String>) -> Self {
75        Self { a: a.into(), b: b.into() }
76    }
77}
78
79/// A robot-/CLI-addressable control message — the named boundary a headless
80/// driver (or a host toolbar) drives the chart through, the same effect the
81/// canvas gestures produce. Applied by [`SystemChart::update`]; the pure
82/// [`view`](SystemChart::view) *returns* these (it never mutates `self`).
83#[derive(Clone, Debug, PartialEq)]
84pub enum Msg {
85    /// **Toggle**-select the node with this stable id: selecting the already-
86    /// selected node clears it, an unknown id is a no-op (parity with the click).
87    Select(String),
88    /// Clear any selection.
89    ClearSelection,
90    /// Set a node's badge count by id (e.g. a live event count). No-op if unknown.
91    SetBadge(String, u64),
92    /// Set a node's detail text by id. No-op if unknown.
93    SetDetail(String, String),
94}
95
96/// Side work as data (FC-8). The syschart does no I/O — every [`Msg`] mutates
97/// only the in-memory [`SysChartModel`] — so this is uninhabited on purpose: the
98/// type-checked statement that [`SystemChart::update`] never asks the host to do
99/// anything.
100#[derive(Clone, Debug, PartialEq)]
101pub enum Effect {}
102
103/// **The complete observable state (FC-1 / FC-3)** of a [`SystemChart`], in one
104/// serializable, round-trippable struct: every node + edge, the selection (keyed
105/// on the stable [`SysNode::id`], FC-5, so reorder/insert/delete preserves it),
106/// and the canvas height. [`SystemChart::state`](facett_core::Elm::state) hands
107/// back a `&SysChartModel`; a headless driver ([`facett_core::harness`]) snapshots
108/// it after feeding a `Vec<Msg>`.
109#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
110pub struct SysChartModel {
111    /// Free-positioned peer nodes.
112    pub nodes: Vec<SysNode>,
113    /// Undirected links between node ids.
114    pub edges: Vec<SysEdge>,
115    /// Stable id of the selected node (drives the detail panel), keyed on the
116    /// domain id ([`SysNode::id`]) not an enumeration index (FC-5).
117    pub selected: Option<String>,
118    /// Height (px) the node canvas takes before the detail panel.
119    pub canvas_h: f32,
120}
121
122/// The system-map chart [`Facet`](facett_core::Facet): a tab title plus the
123/// complete observable [`SysChartModel`]. The FC-9 pure [`view`](Self::view) reads
124/// it and the FC-2 [`update`](Self::update) is the sole path that mutates it.
125pub struct SystemChart {
126    /// Tab/panel title.
127    pub title: String,
128    /// All observable state (FC-3).
129    pub state: SysChartModel,
130}
131
132const NODE_R: f32 = 16.0;
133
134impl SystemChart {
135    pub fn new(title: impl Into<String>, nodes: Vec<SysNode>, edges: Vec<SysEdge>) -> Self {
136        Self {
137            title: title.into(),
138            state: SysChartModel { nodes, edges, selected: None, canvas_h: 280.0 },
139        }
140    }
141    pub fn with_canvas_height(mut self, h: f32) -> Self {
142        self.state.canvas_h = h;
143        self
144    }
145
146    /// Build a **layered** system map — the architecture/topology "grouped by
147    /// layer" shape. `layers` are top-to-bottom bands (e.g. ingress → services →
148    /// storage); each band's nodes are spread evenly across the width and pinned to
149    /// that band's vertical position, so the map reads as a constellation diagram
150    /// organised by layer. Node `pos` fields are overwritten by the layout; `edges`
151    /// (the dependencies) are added as given. This is [`SystemChart::new`] with the
152    /// free positions computed from the layering instead of supplied.
153    pub fn from_layers(title: impl Into<String>, layers: Vec<Vec<SysNode>>, edges: Vec<SysEdge>) -> Self {
154        let n_layers = layers.len().max(1) as f32;
155        let mut nodes = Vec::new();
156        for (li, layer) in layers.into_iter().enumerate() {
157            // Band y: single layer sits mid-canvas; otherwise 0.12..=0.88 evenly.
158            let y = if n_layers <= 1.0 { 0.5 } else { 0.12 + (li as f32) * (0.76 / (n_layers - 1.0)) };
159            let count = layer.len().max(1) as f32;
160            for (ni, mut node) in layer.into_iter().enumerate() {
161                let x = if count <= 1.0 { 0.5 } else { 0.1 + (ni as f32) * (0.8 / (count - 1.0)) };
162                node.pos = (x, y);
163                nodes.push(node);
164            }
165        }
166        Self::new(title, nodes, edges)
167    }
168
169    /// A self-contained **demo constellation map** — a small nordisk architecture in
170    /// three layers (ingress `gateway` → the `korp`/`nornir`/`holger` services →
171    /// the `skade`/`iceberg` storage), with dependency edges and per-service badges.
172    /// The fixture the `syschart_panel` demo (and the Pages site) mounts.
173    #[must_use]
174    pub fn demo() -> Self {
175        let c = Color32::from_rgb;
176        let layers = vec![
177            vec![SysNode::new("gateway", "Gateway", c(120, 200, 255), (0.0, 0.0)).badge(2).detail("ingress · TLS terminate")],
178            vec![
179                SysNode::new("korp", "korp", c(200, 160, 255), (0.0, 0.0)).badge(5).detail("case + analysis warehouse"),
180                SysNode::new("nornir", "nornir", c(160, 255, 180), (0.0, 0.0)).badge(3).detail("orchestrator + test matrix"),
181                SysNode::new("holger", "holger", c(255, 200, 140), (0.0, 0.0)).badge(1).detail("registry browser"),
182            ],
183            vec![
184                SysNode::new("skade", "skade", c(140, 190, 255), (0.0, 0.0)).badge(8).detail("lakehouse warehouse"),
185                SysNode::new("iceberg", "Iceberg", c(180, 220, 160), (0.0, 0.0)).badge(0).detail("table format"),
186            ],
187        ];
188        let edges = vec![
189            SysEdge::new("gateway", "korp"),
190            SysEdge::new("gateway", "nornir"),
191            SysEdge::new("korp", "holger"),
192            SysEdge::new("korp", "skade"),
193            SysEdge::new("nornir", "skade"),
194            SysEdge::new("skade", "iceberg"),
195        ];
196        Self::from_layers("System Map", layers, edges)
197    }
198
199    /// The nodes (read-only view of the model).
200    pub fn nodes(&self) -> &[SysNode] {
201        &self.state.nodes
202    }
203    /// The edges (read-only view of the model).
204    pub fn edges(&self) -> &[SysEdge] {
205        &self.state.edges
206    }
207
208    fn index_of(&self, id: &str) -> Option<usize> {
209        self.state.nodes.iter().position(|n| n.id == id)
210    }
211
212    /// Index of the currently-selected node, resolved from its stable id.
213    fn selected_idx(&self) -> Option<usize> {
214        self.state.selected.as_deref().and_then(|id| self.index_of(id))
215    }
216
217    /// **FC-2 / FC-8** — the single mutation path. Apply one [`Msg`]; returns the
218    /// (always empty) [`Effect`]s the host should run. The canvas click routes its
219    /// [`Msg`] here too, so live == headless.
220    pub fn update(&mut self, msg: Msg) -> Vec<Effect> {
221        match msg {
222            Msg::Select(id) => {
223                if self.index_of(&id).is_some() {
224                    self.state.selected = if self.state.selected.as_deref() == Some(id.as_str()) {
225                        None
226                    } else {
227                        Some(id)
228                    };
229                }
230            }
231            Msg::ClearSelection => self.state.selected = None,
232            Msg::SetBadge(id, badge) => {
233                if let Some(i) = self.index_of(&id) {
234                    self.state.nodes[i].badge = badge;
235                }
236            }
237            Msg::SetDetail(id, detail) => {
238                if let Some(i) = self.index_of(&id) {
239                    self.state.nodes[i].detail = detail;
240                }
241            }
242        }
243        Vec::new()
244    }
245
246    /// Select a node by id (headless-test + host entry point). Selecting the
247    /// already-selected node deselects it. Unknown id is a no-op. Thin wrapper
248    /// over [`update`](Self::update)`(`[`Msg::Select`]`)` — the single mutation path.
249    pub fn select(&mut self, id: &str) {
250        let _ = self.update(Msg::Select(id.to_string()));
251    }
252    /// Clear the selection ([`Msg::ClearSelection`]).
253    pub fn clear_selection(&mut self) {
254        let _ = self.update(Msg::ClearSelection);
255    }
256    /// The selected node's id, if any.
257    pub fn selected(&self) -> Option<&str> {
258        self.state.selected.as_deref()
259    }
260
261    /// Update a node's badge by id (e.g. a live event count). No-op if unknown.
262    /// Thin wrapper over [`update`](Self::update)`(`[`Msg::SetBadge`]`)`.
263    pub fn set_badge(&mut self, id: &str, badge: u64) {
264        let _ = self.update(Msg::SetBadge(id.to_string(), badge));
265    }
266    /// Update a node's detail text by id. No-op if unknown. Thin wrapper over
267    /// [`update`](Self::update)`(`[`Msg::SetDetail`]`)`.
268    pub fn set_detail(&mut self, id: &str, detail: impl Into<String>) {
269        let _ = self.update(Msg::SetDetail(id.to_string(), detail.into()));
270    }
271
272    /// **Test/host hook (additive).** The public, return-asserted view of the
273    /// private [`center`](Self::center) layout node — the absolute pixel centre
274    /// node `i`'s circle is painted at inside `rect`. Exposed so the syschart
275    /// call-chain matrix can assert the *layout/place* stage (finite, confined to
276    /// the padded inner rect, monotone in the normalized pos) without a painter.
277    /// Calls the **same** private fn `view` paints with — additive, no behaviour
278    /// change. The node radius the layout pads for is [`SystemChart::NODE_R`].
279    pub fn node_center(&self, i: usize, rect: Rect) -> Pos2 {
280        self.center(i, rect)
281    }
282    /// The node radius (px) the free-position layout pads the canvas by, exposed so
283    /// the matrix can assert nodes land inside `rect` by exactly this inset.
284    pub const NODE_R: f32 = NODE_R;
285
286    /// Absolute pixel centre of node `i` inside `rect`.
287    fn center(&self, i: usize, rect: Rect) -> Pos2 {
288        let (nx, ny) = self.state.nodes[i].pos;
289        let pad = NODE_R + 6.0;
290        let inner = Rect::from_min_max(
291            rect.min + vec2(pad, pad),
292            rect.max - vec2(pad, pad),
293        );
294        Pos2::new(
295            inner.min.x + nx.clamp(0.0, 1.0) * inner.width().max(1.0),
296            inner.min.y + ny.clamp(0.0, 1.0) * inner.height().max(1.0),
297        )
298    }
299}
300
301impl SystemChart {
302    /// The copyable text: the selected node's `label · detail` (badge in a suffix)
303    /// when a node is selected, else the whole node list as a TSV rectangle
304    /// (`id \t label \t badge`).
305    pub fn copy_text(&self) -> Option<String> {
306        if self.state.nodes.is_empty() {
307            return None;
308        }
309        if let Some(n) = self.selected().and_then(|id| self.state.nodes.iter().find(|n| n.id == id)) {
310            let mut s = format!("{} (badge {})", n.label, n.badge);
311            if !n.detail.is_empty() {
312                s.push('\n');
313                s.push_str(&n.detail);
314            }
315            return Some(s);
316        }
317        let mut out = String::from("id\tlabel\tbadge");
318        for n in &self.state.nodes {
319            out.push('\n');
320            out.push_str(&format!("{}\t{}\t{}", n.id, n.label, n.badge));
321        }
322        Some(out)
323    }
324
325    /// **FC-9 pure view** — a function of `&self`: it paints the chart and *returns*
326    /// the [`Msg`]s the frame produced (a node click → [`Msg::Select`]). It does
327    /// **not** mutate the model; the [`impl_facet_via_elm!`](facett_core::impl_facet_via_elm)
328    /// bridge applies the returned messages through [`update`](Self::update), so a
329    /// headless driver feeding the same [`Msg`]s reproduces the live behaviour.
330    pub fn view(&self, ui: &mut Ui) -> Vec<Msg> {
331        let mut msgs: Vec<Msg> = Vec::new();
332        let th = theme(ui);
333        // ── node canvas ──────────────────────────────────────────────────────
334        let canvas = vec2(
335            ui.available_width(),
336            self.state.canvas_h.min(ui.available_height().max(self.state.canvas_h)),
337        );
338        let (rect, _resp) = ui.allocate_exact_size(canvas, Sense::hover());
339        let base = ui.id().with("syschart-node");
340        let painter = ui.painter_at(rect);
341
342        let centers: Vec<Pos2> = (0..self.state.nodes.len()).map(|i| self.center(i, rect)).collect();
343
344        // edges first (under the nodes)
345        for e in &self.state.edges {
346            if let (Some(ai), Some(bi)) = (self.index_of(&e.a), self.index_of(&e.b)) {
347                painter.line_segment([centers[ai], centers[bi]], Stroke::new(1.5, th.edge));
348            }
349        }
350
351        // nodes — paint, plus an AccessKit node per element (FC-4) keyed on the
352        // stable domain id (FC-5). A click is collected as a `Msg::Select` and
353        // RETURNED; the `update` bridge applies the (toggle) selection — the view
354        // itself stays pure (no `self` mutation).
355        let sel_id = self.state.selected.clone();
356        for (i, node) in self.state.nodes.iter().enumerate() {
357            let c = centers[i];
358            let selected = sel_id.as_deref() == Some(node.id.as_str());
359            let r = if selected { NODE_R + 3.0 } else { NODE_R };
360            // Elevation shadow under the raised node chip (Windows-pronounced /
361            // macOS-restrained, per the active NativeFeel) — painted BEFORE the fill
362            // so it sits behind the node. No-op under Device (effects-off).
363            facett_core::look::elevation_shadow(ui, Rect::from_center_size(c, vec2(2.0 * r, 2.0 * r)), r);
364            painter.circle_filled(c, r, node.color);
365            let ring = if selected { th.accent } else { th.node_stroke };
366            painter.circle_stroke(c, r, Stroke::new(if selected { 2.5 } else { 1.0 }, ring));
367            // badge inside the node
368            painter.text(c, Align2::CENTER_CENTER, node.badge.to_string(), FontId::proportional(11.0), th.text);
369            // label under the node
370            painter.text(
371                c + vec2(0.0, r + 2.0),
372                Align2::CENTER_TOP,
373                &node.label,
374                FontId::proportional(11.0),
375                th.text,
376            );
377
378            // AccessKit hit-test node over the node's circle (FC-4): role=Button,
379            // label = "{label} · {badge} events", numeric_value = badge, selected.
380            let node_rect = Rect::from_center_size(c, vec2(2.0 * r, 2.0 * r));
381            // ── native-feel cues (mac/windows parity): the Windows reveal glow on
382            // hover + the platform focus ring/rect on the selected node. Both read
383            // the active Theme's NativeFeel through the shared `look::feel` helpers,
384            // so this node follows the platform preset (no-op on Neutral/Device).
385            facett_core::look::reveal_on_hover(ui, node_rect, r);
386            facett_core::look::apply_focus_ring(ui, node_rect, selected, r);
387            let label = format!("{} · {} events", node.label, node.badge);
388            let hit = a11y_node(
389                ui,
390                base,
391                &node.id,
392                Sense::click(),
393                node_rect,
394                Semantics::button(label).value(node.badge as f64).selected(selected),
395            );
396            if hit.clicked() {
397                msgs.push(Msg::Select(node.id.clone()));
398            }
399        }
400
401        // ── inline detail panel ──────────────────────────────────────────────
402        ui.separator();
403        match self.selected_idx() {
404            None => {
405                ui.weak("Click a node to expand its detail.");
406            }
407            Some(i) => {
408                let node = &self.state.nodes[i];
409                ui.horizontal(|ui| {
410                    ui.strong(&node.label);
411                    ui.weak(format!("· {} events", node.badge));
412                });
413                if node.detail.is_empty() {
414                    ui.weak("(no detail)");
415                } else {
416                    for line in node.detail.lines() {
417                        ui.monospace(line);
418                    }
419                }
420            }
421        }
422
423        // ── render-lane emit: this view path RAN ──────────────────────────────
424        #[cfg(feature = "testmatrix")]
425        facett_core::testmatrix::emit(
426            "facett-syschart::SystemChart::view",
427            "ui_render",
428            !self.state.nodes.is_empty(),
429            &format!("nodes={} edges={}", self.state.nodes.len(), self.state.edges.len()),
430        );
431
432        msgs
433    }
434}
435
436// ── typed copy (§16) — read-only: selected node text or node-list rows ─────────
437impl CopySource for SystemChart {
438    fn copy_kinds(&self) -> &[ClipKind] {
439        &[ClipKind::Text]
440    }
441
442    fn copy_payload(&self) -> Option<ClipPayload> {
443        self.copy_text().map(ClipPayload::Text)
444    }
445}
446
447// ── the Elm contract (FC-2 / FC-9) ─────────────────────────────────────────────
448impl facett_core::Elm for SystemChart {
449    type Model = SysChartModel;
450    type Msg = Msg;
451    type Effect = Effect;
452
453    fn title(&self) -> &str {
454        &self.title
455    }
456    fn state(&self) -> &SysChartModel {
457        &self.state
458    }
459    fn update(&mut self, msg: Msg) -> Vec<Effect> {
460        SystemChart::update(self, msg)
461    }
462    fn view(&self, ui: &mut Ui) -> Vec<Msg> {
463        SystemChart::view(self, ui)
464    }
465}
466
467// The bridge macro writes `impl Facet for SystemChart` from the `Elm` impl:
468// `title`, the FC-9 `ui` loop (`for m in view(ui) { update(m) }`), plus the extra
469// overrides below. **Form 3** (`custom_state_json`) because the chart publishes a
470// RICHER `state_json` than plain `serde(state())` — it flattens each node to
471// `id/label/badge/pos/has_detail` (colour omitted, detail as a bool) and each edge
472// to an `[a,b]` pair, exactly the observable shape the demo's mega-matrix asserts.
473facett_core::impl_facet_via_elm!(SystemChart, custom_state_json, {
474    fn copy(&mut self) -> Option<String> {
475        self.copy_payload().map(|p| p.as_text())
476    }
477
478    fn state_json(&self) -> serde_json::Value {
479        let s = &self.state;
480        serde_json::json!({
481            "nodes": s.nodes.iter().map(|n| serde_json::json!({
482                "id": n.id,
483                "label": n.label,
484                "badge": n.badge,
485                "pos": [n.pos.0, n.pos.1],
486                "has_detail": !n.detail.is_empty(),
487            })).collect::<Vec<_>>(),
488            "edges": s.edges.iter().map(|e| serde_json::json!([e.a, e.b])).collect::<Vec<_>>(),
489            "selected": self.selected(),
490        })
491    }
492
493    fn selection_json(&self) -> serde_json::Value {
494        match self.selected() {
495            Some(id) => serde_json::json!(id),
496            None => serde_json::Value::Null,
497        }
498    }
499
500    /// Painted with the active `Theme` (nodes/edges/text/accent ring), and its
501    /// canvas takes the host's available width — themeable + resizable.
502    fn caps(&self) -> FacetCaps {
503        FacetCaps::NONE.themeable().resizable().selectable().copyable()
504    }
505
506    /// Opt into typed downcast so a host (e.g. the demo's robot-UI node-select
507    /// toolbar) can forward a selection to [`SystemChart::select`] when this chart
508    /// lives boxed inside a `FacetDeck`.
509    fn as_any_mut(&mut self) -> Option<&mut dyn std::any::Any> {
510        Some(self)
511    }
512});
513
514#[cfg(test)]
515mod tests {
516    use super::*;
517    // `Elm` for `state()`; `Facet` for `state_json`/`selection_json`.
518    use facett_core::{Elm, Facet, harness};
519
520    #[test]
521    fn typed_copy_is_selected_node_or_node_list_rows() {
522        use facett_core::clip::{ClipKind, CopySource};
523        let mut c = SystemChart::new(
524            "sys",
525            vec![
526                SysNode::new("pki", "PKI", Color32::WHITE, (0.1, 0.1)).badge(3),
527                SysNode::new("oidc", "OIDC", Color32::WHITE, (0.9, 0.1)).badge(7),
528            ],
529            vec![SysEdge::new("pki", "oidc")],
530        );
531        // No selection -> the node list as a TSV rectangle.
532        let p = c.copy_payload().unwrap();
533        assert_eq!(p.kind(), ClipKind::Text);
534        assert!(p.as_text().starts_with("id\tlabel\tbadge"), "{}", p.as_text());
535        assert!(p.as_text().contains("\npki\tPKI\t3"));
536        // Selecting narrows to that node's label + badge.
537        c.select("oidc");
538        assert_eq!(c.copy_payload().unwrap().as_text(), "OIDC (badge 7)");
539        assert!(SystemChart::new("empty", vec![], vec![]).copy_payload().is_none());
540    }
541
542    fn sample() -> SystemChart {
543        let nodes = vec![
544            SysNode::new("pki", "PKI", Color32::from_rgb(120, 200, 255), (0.1, 0.1)).badge(3).detail("issued: a\nissued: b"),
545            SysNode::new("oidc", "OIDC", Color32::from_rgb(200, 160, 255), (0.9, 0.1)).badge(7),
546            SysNode::new("nexus", "Nexus", Color32::from_rgb(160, 255, 180), (0.5, 0.9)).badge(0),
547        ];
548        let edges = vec![
549            SysEdge::new("pki", "oidc"),
550            SysEdge::new("pki", "nexus"),
551            SysEdge::new("oidc", "nexus"),
552        ];
553        SystemChart::new("System Map", nodes, edges)
554    }
555
556    #[test]
557    fn select_toggles_and_reports() {
558        let mut c = sample();
559        assert_eq!(c.selected(), None);
560        c.select("oidc");
561        assert_eq!(c.selected(), Some("oidc"));
562        c.select("oidc"); // toggle off
563        assert_eq!(c.selected(), None);
564        c.select("nope"); // unknown → no-op
565        assert_eq!(c.selected(), None);
566    }
567
568    #[test]
569    fn set_badge_and_detail_mutate_named_node() {
570        let mut c = sample();
571        c.set_badge("nexus", 42);
572        c.set_detail("nexus", "repo: maven-releases");
573        let nexus = c.nodes().iter().find(|n| n.id == "nexus").unwrap();
574        assert_eq!(nexus.badge, 42);
575        assert!(nexus.detail.contains("maven-releases"));
576    }
577
578    #[test]
579    fn state_json_carries_every_node_edge_and_selection() {
580        let mut c = sample();
581        c.select("pki");
582        let j = c.state_json();
583        assert_eq!(j["nodes"].as_array().unwrap().len(), 3);
584        assert_eq!(j["edges"].as_array().unwrap().len(), 3);
585        assert_eq!(j["selected"], "pki");
586        // badge + detail flags surfaced for robot assertions
587        let pki = j["nodes"].as_array().unwrap().iter().find(|n| n["id"] == "pki").unwrap();
588        assert_eq!(pki["badge"], 3);
589        assert_eq!(pki["has_detail"], true);
590    }
591
592    #[test]
593    fn headless_render_draws_and_selection_shows_detail() {
594        // Inject a real chart + a real selection, render offscreen, assert it
595        // both DREW pixels and reported the selected node in its state — the
596        // inject-input/assert-output law, no display.
597        let mut c = sample();
598        c.select("pki");
599        let r = harness::headless_render(&mut c);
600        assert_eq!(r.title, "System Map");
601        assert!(r.drew(), "a 3-node chart should tessellate to vertices");
602        assert_eq!(r.state["selected"], "pki");
603        assert_eq!(r.state["nodes"].as_array().unwrap().len(), 3);
604    }
605
606    #[test]
607    fn caps_advertise_selectable_themeable_resizable() {
608        let caps = sample().caps();
609        assert!(caps.selectable);
610        assert!(caps.themeable);
611        assert!(caps.resizable);
612        assert!(!caps.scalable, "syschart has no zoom yet");
613    }
614
615    // ── FC-2 / FC-9 harness properties: drive `Msg`s, snapshot `state()`, no GPU ──
616
617    #[test]
618    fn drive_select_toggles_via_msg_no_effects() {
619        let mut c = sample();
620        // Select → the id lands; select again → toggles off; unknown id → no-op.
621        let fx = harness::drive(&mut c, [Msg::Select("oidc".into())]);
622        assert!(fx.is_empty(), "syschart issues no Effects (uninhabited)");
623        assert_eq!(c.state().selected.as_deref(), Some("oidc"));
624        harness::drive(&mut c, [Msg::Select("oidc".into())]);
625        assert_eq!(c.state().selected, None, "re-select toggles off");
626        harness::drive(&mut c, [Msg::Select("ghost".into())]);
627        assert_eq!(c.state().selected, None, "unknown id is a no-op");
628    }
629
630    #[test]
631    fn drive_clear_selection_via_msg() {
632        let mut c = sample();
633        let snap = harness::snapshot(&mut c, [Msg::Select("pki".into()), Msg::ClearSelection]);
634        assert_eq!(snap.selected, None);
635    }
636
637    #[test]
638    fn drive_set_badge_and_detail_via_msg() {
639        let mut c = sample();
640        let snap = harness::snapshot(
641            &mut c,
642            [
643                Msg::SetBadge("nexus".into(), 42),
644                Msg::SetDetail("nexus".into(), "repo: maven-releases".into()),
645                // unknown ids are inert
646                Msg::SetBadge("ghost".into(), 99),
647            ],
648        );
649        let nexus = snap.nodes.iter().find(|n| n.id == "nexus").unwrap();
650        assert_eq!(nexus.badge, 42);
651        assert!(nexus.detail.contains("maven-releases"));
652        assert!(snap.nodes.iter().all(|n| n.id != "ghost"));
653    }
654
655    #[test]
656    fn drive_produces_no_effects_for_any_msg() {
657        let mut c = sample();
658        let fx = harness::drive(
659            &mut c,
660            [
661                Msg::Select("pki".into()),
662                Msg::ClearSelection,
663                Msg::SetBadge("oidc".into(), 5),
664                Msg::SetDetail("oidc".into(), "x".into()),
665            ],
666        );
667        assert!(fx.is_empty(), "the syschart never asks the host to do side work");
668    }
669
670    #[test]
671    fn from_layers_groups_nodes_into_vertical_bands() {
672        let c = SystemChart::from_layers(
673            "layered",
674            vec![
675                vec![SysNode::new("a", "A", Color32::WHITE, (0.0, 0.0))],
676                vec![
677                    SysNode::new("b", "B", Color32::WHITE, (0.0, 0.0)),
678                    SysNode::new("c", "C", Color32::WHITE, (0.0, 0.0)),
679                ],
680            ],
681            vec![SysEdge::new("a", "b")],
682        );
683        // Every node survived the flatten, layout overwrote positions.
684        assert_eq!(c.nodes().len(), 3);
685        let y = |id: &str| c.nodes().iter().find(|n| n.id == id).unwrap().pos.1;
686        // Band 0 (A) sits above band 1 (B, C), which share a y.
687        assert!(y("a") < y("b"), "the first layer is above the second");
688        assert!((y("b") - y("c")).abs() < f32::EPSILON, "same-layer nodes share a band");
689        // A single node in a band is centred; two are spread apart horizontally.
690        let x = |id: &str| c.nodes().iter().find(|n| n.id == id).unwrap().pos.0;
691        assert!((x("a") - 0.5).abs() < f32::EPSILON, "a lone node centres");
692        assert!(x("b") < x("c"), "peers spread across the band");
693    }
694
695    #[test]
696    fn demo_is_a_layered_constellation_that_renders() {
697        let mut c = SystemChart::demo();
698        assert_eq!(c.nodes().len(), 6, "gateway + 3 services + 2 storage");
699        assert_eq!(c.edges().len(), 6);
700        // Three distinct vertical bands (gateway / services / storage).
701        use std::collections::BTreeSet;
702        let bands: BTreeSet<i32> = c.nodes().iter().map(|n| (n.pos.1 * 1000.0) as i32).collect();
703        assert_eq!(bands.len(), 3, "three layers ⇒ three vertical bands");
704        // The demo fixture paints headlessly (the same panel the host mounts).
705        let r = harness::headless_render(&mut c);
706        assert_eq!(r.title, "System Map");
707        assert!(r.drew(), "a 6-node constellation tessellates to vertices");
708        assert_eq!(r.state["nodes"].as_array().unwrap().len(), 6);
709    }
710
711    #[test]
712    fn model_serde_round_trips() {
713        let mut c = sample();
714        c.select("oidc");
715        let json = serde_json::to_value(c.state()).unwrap();
716        let back: SysChartModel = serde_json::from_value(json).unwrap();
717        assert_eq!(&back, c.state(), "the Model round-trips through serde (FC-3)");
718    }
719}