Skip to main content

omni_dev/browser/
snippet.rs

1//! Renders the browser-side JS snippet that the operator pastes into a DevTools
2//! console. The template is embedded at compile time; the WebSocket port and
3//! session token are interpolated at print time.
4
5/// The snippet template, with `__OMNI_BRIDGE_PORT__` / `__OMNI_BRIDGE_TOKEN__`
6/// placeholders.
7const TEMPLATE: &str = include_str!("../templates/browser-bridge.js");
8
9/// Renders the snippet for the given WebSocket port and session token.
10///
11/// The port is interpolated as a bare numeric literal and the token inside the
12/// single-quoted string literal already present in the template.
13#[must_use]
14pub fn render(ws_port: u16, token: &str) -> String {
15    TEMPLATE
16        .replace("__OMNI_BRIDGE_PORT__", &ws_port.to_string())
17        .replace("__OMNI_BRIDGE_TOKEN__", token)
18}
19
20#[cfg(test)]
21#[allow(clippy::unwrap_used, clippy::expect_used)]
22mod tests {
23    use super::*;
24
25    #[test]
26    fn render_interpolates_and_leaves_no_placeholders() {
27        let out = render(9999, "deadbeef-token");
28        assert!(out.contains("const PORT = 9999"));
29        assert!(out.contains("'deadbeef-token'"));
30        assert!(!out.contains("__OMNI_BRIDGE_PORT__"));
31        assert!(!out.contains("__OMNI_BRIDGE_TOKEN__"));
32    }
33
34    #[test]
35    fn render_uses_the_supplied_port() {
36        let out = render(40000, "t");
37        assert!(out.contains("const PORT = 40000"));
38    }
39
40    #[test]
41    fn render_includes_binary_body_handling() {
42        let out = render(9999, "t");
43        assert!(out.contains("arrayBuffer"));
44        assert!(out.contains("base64"));
45        assert!(out.contains("btoa"));
46    }
47
48    #[test]
49    fn render_includes_streaming() {
50        let out = render(9999, "t");
51        assert!(out.contains("getReader"));
52        assert!(out.contains("activeStreams"));
53        assert!(out.contains("cancel"));
54    }
55}