use std::path::{Path, PathBuf};
use pretty_assertions::assert_eq;
use super::{render, Installed, PairWith, ServiceSpec, Unpairable};
use crate::daemon::{ServiceFiles, ServiceManager};
fn service(manager: ServiceManager) -> ServiceFiles {
ServiceFiles::daemon_http(manager, Path::new("/nonexistent"), None)
}
fn spec(program: &str, environment: &[(&str, &str)]) -> ServiceSpec {
ServiceSpec {
program: PathBuf::from(program),
args: [
"daemon",
"serve",
"--http",
"100.101.102.103:8766",
"--allow-host",
"devbox",
]
.map(str::to_owned)
.to_vec(),
environment: environment
.iter()
.map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
.collect(),
pair_with: PairWith {
url: "ws://100.101.102.103:8766/ws"
.parse()
.expect("a bridge URL"),
listen: "100.101.102.103:8766".parse().expect("an address"),
},
}
}
#[test]
fn the_systemd_unit_quotes_what_systemd_would_expand() {
let spec = spec(
"/home/me/50%$ x/rho",
&[
("PATH", "/home/me/.local/bin:/usr/bin"),
("RHO_HOME", "/home/me/50%$ \"x\" \\b"),
],
);
assert_eq!(
render(
&service(ServiceManager::Systemd),
&spec,
Path::new("/home/me")
)
.expect("a unit"),
r#"# Written by `rho daemon bridge setup`, which rewrites it; `rho daemon bridge remove` removes it.
# rho-bridge-pair {"url":"ws://100.101.102.103:8766/ws","listen":"100.101.102.103:8766"}
[Unit]
Description=rho daemon HTTP WebSocket bridge
After=network.target
[Service]
ExecStart="/home/me/50%%$ x/rho" "daemon" "serve" "--http" "100.101.102.103:8766" "--allow-host" "devbox"
Environment="PATH=/home/me/.local/bin:/usr/bin"
Environment="RHO_HOME=/home/me/50%%$ \"x\" \\b"
# Stop only the bridge. The daemon keeps running its turns.
KillMode=process
Restart=always
RestartSec=10
[Install]
WantedBy=default.target
"#
);
assert!(render(
&service(ServiceManager::Systemd),
&self::spec("/home/me/it's/rho", &[]),
Path::new("/home/me")
)
.is_err());
}
#[test]
fn the_launchd_agent_escapes_xml() {
let spec = spec(
"/Applications/R&D <rho>/rho",
&[("PATH", "/opt/homebrew/bin:/usr/bin")],
);
assert_eq!(
render(
&service(ServiceManager::Launchd),
&spec,
Path::new("/Users/me")
)
.expect("a plist"),
r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!-- Written by `rho daemon bridge setup`, which rewrites it; `rho daemon bridge remove` removes it. -->
<!-- rho-bridge-pair {"url":"ws://100.101.102.103:8766/ws","listen":"100.101.102.103:8766"} -->
<plist version="1.0">
<dict>
<key>Label</key>
<string>dev.rho.daemon-http</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/R&D <rho>/rho</string>
<string>daemon</string>
<string>serve</string>
<string>--http</string>
<string>100.101.102.103:8766</string>
<string>--allow-host</string>
<string>devbox</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/opt/homebrew/bin:/usr/bin</string>
</dict>
<!-- Stop only the bridge. The daemon keeps running its turns. -->
<key>AbandonProcessGroup</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardErrorPath</key>
<string>/Users/me/Library/Logs/rho-daemon-http.log</string>
</dict>
</plist>
"#
);
}
#[test]
fn the_service_file_records_where_clients_pair() {
let pair_with = PairWith {
url: "wss://rho-dev--1.example.com/a--b/ws"
.parse()
.expect("a bridge URL"),
listen: "[::1]:8766".parse().expect("an address"),
};
let spec = ServiceSpec {
pair_with: pair_with.clone(),
..spec("/usr/bin/rho", &[])
};
for manager in [ServiceManager::Systemd, ServiceManager::Launchd] {
let text = render(&service(manager), &spec, Path::new("/home/me")).expect("a file");
let comments_hold_no_double_dash = text.split("<!--").skip(1).all(|rest| {
!rest
.split_once("-->")
.map_or(rest, |(comment, _)| comment)
.contains("--")
});
assert_eq!(
(
PairWith::read(&text).expect("a record"),
comments_hold_no_double_dash
),
(Some(pair_with.clone()), true),
"{manager:?}"
);
}
}
#[test]
fn pair_finds_where_clients_reach_the_installed_bridge() {
let spec = spec("/usr/bin/rho", &[]);
let text = render(
&service(ServiceManager::Systemd),
&spec,
Path::new("/home/me"),
)
.expect("a unit");
let unrecorded: String = text
.lines()
.filter(|line| !line.contains("rho-bridge-pair"))
.map(|line| format!("{line}\n"))
.collect();
let pairs = |installed: Installed| {
installed.pair_with().map_err(|error| {
*error
.downcast_ref::<Unpairable>()
.unwrap_or_else(|| panic!("not an Unpairable: {error:#}"))
})
};
assert_eq!(
[
pairs(Installed::BySetup(text)),
pairs(Installed::Absent),
pairs(Installed::ByHand),
pairs(Installed::BySetup(unrecorded)),
],
[
Ok(spec.pair_with.clone()),
Err(Unpairable::NotInstalled),
Err(Unpairable::ByHand),
Err(Unpairable::Unrecorded),
]
);
}