lean_ctx_cli/dashboard/
base_path.rs1pub fn normalize(input: &str) -> String {
22 let trimmed = input.trim().trim_matches('/');
23 if trimmed.is_empty() {
24 String::new()
25 } else {
26 format!("/{trimmed}")
27 }
28}
29
30pub fn strip<'a>(path: &'a str, base: &str) -> &'a str {
35 if base.is_empty() {
36 return path;
37 }
38 if path == base {
39 return "/";
40 }
41 match path.strip_prefix(base) {
42 Some(rest) if rest.starts_with('/') => rest,
43 _ => path,
44 }
45}
46
47pub fn rewrite_asset_urls(body: &str, base: &str) -> String {
52 if base.is_empty() {
53 return body.to_string();
54 }
55 const PREFIXES: &[&str] = &["/static/", "/api/", "/favicon"];
58 const DELIMS: &[char] = &['"', '\'', '`', '('];
61
62 let mut out = body.to_string();
63 for prefix in PREFIXES {
64 for &d in DELIMS {
65 let from = format!("{d}{prefix}");
66 if out.contains(&from) {
67 let to = format!("{d}{base}{prefix}");
68 out = out.replace(&from, &to);
69 }
70 }
71 }
72 out
73}
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn normalize_canonicalizes() {
81 assert_eq!(normalize(""), "");
82 assert_eq!(normalize("/"), "");
83 assert_eq!(normalize(" "), "");
84 assert_eq!(normalize("dashboard"), "/dashboard");
85 assert_eq!(normalize("/dashboard"), "/dashboard");
86 assert_eq!(normalize("/dashboard/"), "/dashboard");
87 assert_eq!(normalize("//dashboard//"), "/dashboard");
88 assert_eq!(normalize("/lean/ctx"), "/lean/ctx");
89 }
90
91 #[test]
92 fn strip_empty_base_is_noop() {
93 assert_eq!(strip("/api/stats", ""), "/api/stats");
94 assert_eq!(strip("/", ""), "/");
95 }
96
97 #[test]
98 fn strip_removes_prefix() {
99 assert_eq!(strip("/dashboard", "/dashboard"), "/");
100 assert_eq!(strip("/dashboard/", "/dashboard"), "/");
101 assert_eq!(strip("/dashboard/api/stats", "/dashboard"), "/api/stats");
102 assert_eq!(
103 strip("/dashboard/static/style.css", "/dashboard"),
104 "/static/style.css"
105 );
106 }
107
108 #[test]
109 fn strip_accepts_already_rootrelative() {
110 assert_eq!(strip("/api/stats", "/dashboard"), "/api/stats");
112 assert_eq!(strip("/", "/dashboard"), "/");
113 }
114
115 #[test]
116 fn strip_does_not_match_partial_segment() {
117 assert_eq!(strip("/dashboardx/api", "/dashboard"), "/dashboardx/api");
119 }
120
121 #[test]
122 fn rewrite_empty_base_is_noop() {
123 let html = r#"<script src="/static/lib/api.js"></script>"#;
124 assert_eq!(rewrite_asset_urls(html, ""), html);
125 }
126
127 #[test]
128 fn rewrite_html_attributes() {
129 let html = r#"<link href="/static/style.css"><script src="/static/lib/api.js"></script><link rel="icon" href="/favicon.svg">"#;
130 let out = rewrite_asset_urls(html, "/dashboard");
131 assert!(out.contains(r#"href="/dashboard/static/style.css""#));
132 assert!(out.contains(r#"src="/dashboard/static/lib/api.js""#));
133 assert!(out.contains(r#"href="/dashboard/favicon.svg""#));
134 assert!(!out.contains(r#"href="/static/"#));
135 }
136
137 #[test]
138 fn rewrite_js_string_and_template_literals() {
139 let js = "fetch('/api/stats'); const u = `/api/search?q=${q}`; api('/api/pulse');";
140 let out = rewrite_asset_urls(js, "/dashboard");
141 assert!(out.contains("fetch('/dashboard/api/stats')"));
142 assert!(out.contains("`/dashboard/api/search?q=${q}`"));
143 assert!(out.contains("api('/dashboard/api/pulse')"));
144 }
145
146 #[test]
147 fn rewrite_css_url() {
148 let css = "src: url('/static/fonts/inter-variable.woff2');";
149 let out = rewrite_asset_urls(css, "/dashboard");
150 assert!(out.contains("url('/dashboard/static/fonts/inter-variable.woff2')"));
151 }
152
153 #[test]
154 fn rewrite_fetch_interceptor_guard() {
155 let js = "if (url.startsWith('/api/')) attachToken();";
158 let out = rewrite_asset_urls(js, "/dashboard");
159 assert!(out.contains("url.startsWith('/dashboard/api/')"));
160 }
161
162 #[test]
163 fn rewrite_does_not_double_prefix() {
164 let html = r#"<script src="/static/x.js"></script>"#;
165 let once = rewrite_asset_urls(html, "/dashboard");
166 assert_eq!(once.matches("/dashboard/static/x.js").count(), 1);
169 assert!(!once.contains("/dashboard/dashboard/"));
170 }
171}