fob_cli/dev/
error_overlay.rs1pub fn generate_error_overlay(error: &str) -> Result<String, String> {
28 use fob_gen::{Allocator, HtmlBuilder};
29
30 let allocator = Allocator::default();
31 let html_builder = HtmlBuilder::new(allocator);
32
33 html_builder
34 .error_overlay(error)
35 .map_err(|e| format!("Failed to generate error overlay: {}", e))
36}
37
38#[cfg(test)]
48mod tests {
49 use super::*;
50
51 fn html_escape(s: &str) -> String {
65 s.chars()
66 .map(|c| match c {
67 '&' => "&".to_string(),
68 '<' => "<".to_string(),
69 '>' => ">".to_string(),
70 '"' => """.to_string(),
71 '\'' => "'".to_string(),
72 _ => c.to_string(),
73 })
74 .collect()
75 }
76
77 #[test]
78 fn test_html_escape_ampersand() {
79 assert_eq!(html_escape("a & b"), "a & b");
80 }
81
82 #[test]
83 fn test_html_escape_angle_brackets() {
84 assert_eq!(html_escape("<script>"), "<script>");
85 }
86
87 #[test]
88 fn test_html_escape_quotes() {
89 assert_eq!(
90 html_escape(r#"He said "hello""#),
91 "He said "hello""
92 );
93 assert_eq!(html_escape("It's working"), "It's working");
94 }
95
96 #[test]
97 fn test_html_escape_combined() {
98 let input = r#"Error in <Component attr="value" & 'test'>"#;
99 let expected =
100 r#"Error in <Component attr="value" & 'test'>"#;
101 assert_eq!(html_escape(input), expected);
102 }
103
104 #[test]
105 fn test_html_escape_no_special_chars() {
106 let input = "Normal error message";
107 assert_eq!(html_escape(input), input);
108 }
109
110 #[test]
111 fn test_generate_error_overlay_contains_escaped_error() {
112 let error = "<script>alert('xss')</script>";
113 let html = generate_error_overlay(error).expect("HTML generation should succeed");
114
115 assert!(html.contains("<script>"));
117 assert!(!html.contains("<script>alert"));
118 }
119
120 #[test]
121 fn test_generate_error_overlay_structure() {
122 let html = generate_error_overlay("Test error").expect("HTML generation should succeed");
123
124 assert!(html.contains("<!DOCTYPE html>"));
126 assert!(html.contains("Build Error"));
127 assert!(html.contains("Test error"));
128 assert!(html.contains("Retry Build"));
129 assert!(html.contains("/__fob_sse__"));
130 }
131}