Skip to main content

glass_dev/development/
debug.rs

1use super::{DevelopmentGraph, RuntimeLink};
2use serde::{Deserialize, Serialize};
3use std::collections::BTreeMap;
4
5#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
6#[serde(rename_all = "camelCase")]
7pub struct SemanticEntityState {
8    pub role: Option<String>,
9    pub name: Option<String>,
10    pub enabled: Option<bool>,
11    pub actionable: bool,
12}
13
14pub type SemanticSnapshot = BTreeMap<String, SemanticEntityState>;
15
16#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
17#[serde(tag = "kind", rename_all = "camelCase")]
18pub enum SemanticBreakpoint {
19    EntityDisappears { entity_id: String },
20    AccessibleNameMissing { entity_id: Option<String> },
21    RoleChanges { entity_id: String },
22    ActionabilityLost { entity_id: String },
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
26#[serde(rename_all = "camelCase")]
27pub struct SemanticBreakpointHit {
28    pub breakpoint: SemanticBreakpoint,
29    pub entity_id: String,
30    pub before: Option<SemanticEntityState>,
31    pub after: Option<SemanticEntityState>,
32    pub likely_source: Option<RuntimeLink>,
33}
34
35pub fn evaluate_breakpoints(
36    breakpoints: &[SemanticBreakpoint],
37    before: &SemanticSnapshot,
38    after: &SemanticSnapshot,
39    graph: &DevelopmentGraph,
40) -> Vec<SemanticBreakpointHit> {
41    let mut hits = Vec::new();
42    for breakpoint in breakpoints.iter().take(128) {
43        let candidates: Vec<String> = match breakpoint {
44            SemanticBreakpoint::AccessibleNameMissing { entity_id: None } => {
45                before.keys().cloned().collect()
46            }
47            SemanticBreakpoint::EntityDisappears { entity_id }
48            | SemanticBreakpoint::RoleChanges { entity_id }
49            | SemanticBreakpoint::ActionabilityLost { entity_id }
50            | SemanticBreakpoint::AccessibleNameMissing {
51                entity_id: Some(entity_id),
52            } => vec![entity_id.clone()],
53        };
54        for entity_id in candidates {
55            let previous = before.get(&entity_id);
56            let current = after.get(&entity_id);
57            let triggered = match breakpoint {
58                SemanticBreakpoint::EntityDisappears { .. } => {
59                    previous.is_some() && current.is_none()
60                }
61                SemanticBreakpoint::AccessibleNameMissing { .. } => {
62                    previous
63                        .and_then(|state| state.name.as_deref())
64                        .is_some_and(|name| !name.trim().is_empty())
65                        && current.is_some_and(|state| {
66                            state
67                                .name
68                                .as_deref()
69                                .is_none_or(|name| name.trim().is_empty())
70                        })
71                }
72                SemanticBreakpoint::RoleChanges { .. } => {
73                    previous.is_some()
74                        && current.is_some()
75                        && previous.and_then(|state| state.role.as_ref())
76                            != current.and_then(|state| state.role.as_ref())
77                }
78                SemanticBreakpoint::ActionabilityLost { .. } => {
79                    previous.is_some_and(|state| state.actionable)
80                        && current.is_some_and(|state| !state.actionable)
81                }
82            };
83            if triggered {
84                hits.push(SemanticBreakpointHit {
85                    breakpoint: breakpoint.clone(),
86                    entity_id: entity_id.clone(),
87                    before: previous.cloned(),
88                    after: current.cloned(),
89                    likely_source: graph.best_link(&entity_id).cloned(),
90                });
91            }
92        }
93    }
94    hits
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn semantic_breakpoint_reports_regression_and_source_evidence() {
103        let mut before = SemanticSnapshot::new();
104        before.insert(
105            "action.checkout.submit".into(),
106            SemanticEntityState {
107                role: Some("button".into()),
108                name: Some("Pay now".into()),
109                enabled: Some(true),
110                actionable: true,
111            },
112        );
113        let mut after = before.clone();
114        after.get_mut("action.checkout.submit").unwrap().actionable = false;
115        let hits = evaluate_breakpoints(
116            &[SemanticBreakpoint::ActionabilityLost {
117                entity_id: "action.checkout.submit".into(),
118            }],
119            &before,
120            &after,
121            &DevelopmentGraph::default(),
122        );
123        assert_eq!(hits.len(), 1);
124        assert!(hits[0].before.as_ref().unwrap().actionable);
125        assert!(!hits[0].after.as_ref().unwrap().actionable);
126    }
127}