- features.browserShare (봇용 크롬창 화면공유 선택 표시, off by default) and a driver card on the features screen: the Virtual Display Driver 25.7.23 (MIT, SignPath-signed) ships in display-driver/ (hash-checked by package.sh); install and uninstall run jarvis.exe elevated once (--display-driver), settings in %ProgramData%\JarvisBot\display-driver, another program's copy of the driver is left alone - the monitor is on the desktop only while the bot browser uses it, at the rightmost real monitor's bottom-right corner; the stop closes Chrome first, then takes the monitor off - Chrome starts with no window and the app opens it over CDP in the background, so it never takes the focus; the window is a tool window (no Alt+Tab, no taskbar button), fills the monitor's work area and keeps drawing when covered; without a monitor it runs headless with the reason on the dashboard - Chrome runs with --do-not-de-elevate on Windows: started by an app running as administrator it restarted itself as the user and the app lost hold of it - checked on the win-ci Windows VM from install to uninstall (focus kept while typing, no taskbar button, no Chrome left, nothing left after uninstall) and on Linux with a real Chrome Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
160 lines
8.2 KiB
Rust
160 lines
8.2 KiB
Rust
//! The bot browser with a real Chrome (admin.spec.md, "Bot browser"). Runs only with
|
|
//! `JARVIS_TEST_CHROME=<chrome executable>`; needs the internet for the public page. On Ubuntu also
|
|
//! set `JARVIS_CHROME_NO_SANDBOX=1`.
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
use std::time::Duration;
|
|
|
|
use jarvis_app::browser::BotBrowser;
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
|
|
async fn titles(client: &reqwest::Client, port: u16) -> Vec<(String, String)> {
|
|
let text = client.get(format!("http://127.0.0.1:{port}/json/list")).send().await.unwrap().text().await.unwrap();
|
|
let list: serde_json::Value = serde_json::from_str(&text).unwrap();
|
|
list.as_array().unwrap().iter()
|
|
.map(|t| (t["url"].as_str().unwrap_or("").to_owned(), t["title"].as_str().unwrap_or("").to_owned())).collect()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_bot_browser_reaches_public_pages_never_this_pc_and_stops() {
|
|
let Ok(exe) = std::env::var("JARVIS_TEST_CHROME") else {
|
|
eprintln!("skipped: set JARVIS_TEST_CHROME");
|
|
return;
|
|
};
|
|
// A page on this PC the browser must never reach.
|
|
let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let local = l.local_addr().unwrap().port();
|
|
let hits = Arc::new(Mutex::new(0));
|
|
let h2 = hits.clone();
|
|
tokio::spawn(async move {
|
|
while let Ok((mut c, _)) = l.accept().await {
|
|
*h2.lock().unwrap() += 1;
|
|
let mut b = [0u8; 1024];
|
|
let _ = c.read(&mut b).await;
|
|
let _ = c.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 30\r\n\r\n<title>secret local</title>xx").await;
|
|
}
|
|
});
|
|
|
|
let profile = std::env::temp_dir().join(format!("jarvis-bot-browser-{}", std::process::id()));
|
|
let b = BotBrowser::start(std::path::Path::new(&exe), &profile, None).await.expect("chrome starts");
|
|
assert!(b.running());
|
|
let client = reqwest::Client::builder().no_proxy().build().unwrap();
|
|
for url in [format!("http://127.0.0.1:{local}/"), format!("http://localhost:{local}/"), "https://example.com/".into()] {
|
|
client.put(format!("http://127.0.0.1:{}/json/new?{url}", b.cdp_port)).send().await.unwrap();
|
|
}
|
|
let mut got = vec![];
|
|
for _ in 0..40 {
|
|
got = titles(&client, b.cdp_port).await;
|
|
if got.iter().any(|(_, t)| t == "Example Domain") {
|
|
break;
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(250)).await;
|
|
}
|
|
eprintln!("tabs: {got:?}");
|
|
assert!(got.iter().any(|(_, t)| t == "Example Domain"), "the public page loads through the proxy");
|
|
assert!(!got.iter().any(|(_, t)| t.contains("secret local")));
|
|
assert_eq!(*hits.lock().unwrap(), 0, "nothing reached this PC");
|
|
|
|
let port = b.cdp_port;
|
|
let t0 = std::time::Instant::now();
|
|
b.stop().await;
|
|
eprintln!("stopped in {:?}", t0.elapsed());
|
|
assert!(!b.running());
|
|
assert!(client.get(format!("http://127.0.0.1:{port}/json/version")).send().await.is_err(), "CDP is closed");
|
|
let _ = std::fs::remove_dir_all(&profile);
|
|
}
|
|
|
|
/// The install step downloads the pinned Chrome (about 200 MB) and unpacks a runnable browser.
|
|
/// Runs only with `JARVIS_TEST_CHROME_INSTALL=1`.
|
|
#[tokio::test]
|
|
async fn the_browser_step_installs_a_chrome_that_starts() {
|
|
if std::env::var("JARVIS_TEST_CHROME_INSTALL").as_deref() != Ok("1") {
|
|
eprintln!("skipped: set JARVIS_TEST_CHROME_INSTALL=1");
|
|
return;
|
|
}
|
|
let data = std::env::temp_dir().join(format!("jarvis-chrome-install-{}", std::process::id()));
|
|
let layout = jarvis_app::install::Layout { data: data.clone(), src: std::path::PathBuf::new() };
|
|
let steps = jarvis_app::install::steps().into_iter().filter(|s| s.id() == "browser").collect();
|
|
let installer = jarvis_app::install::Installer::new(layout.clone(), steps);
|
|
assert!(installer.run_all().await, "{}", installer.status());
|
|
assert!(installer.done("browser"));
|
|
assert!(!data.join("cache").join("downloads").read_dir().map(|mut d| d.next().is_some()).unwrap_or(false),
|
|
"the archive is removed after unpacking");
|
|
let b = BotBrowser::start(&layout.chrome_exe(), &jarvis_app::browser::profile_dir(&data), None).await.expect("starts");
|
|
assert!(b.running());
|
|
b.stop().await;
|
|
let _ = std::fs::remove_dir_all(&data);
|
|
}
|
|
|
|
/// With a screen, Chrome starts without a window and the app opens exactly one, at the screen's
|
|
/// place and size; the page tab the bot opens joins it. Needs a display on Linux (`DISPLAY`); on
|
|
/// Windows the screen is the virtual monitor (its driver installed), on the desktop only meanwhile.
|
|
#[tokio::test]
|
|
async fn on_a_screen_the_bot_browser_has_one_window_at_the_monitor() {
|
|
let Ok(exe) = std::env::var("JARVIS_TEST_CHROME") else {
|
|
eprintln!("skipped: set JARVIS_TEST_CHROME");
|
|
return;
|
|
};
|
|
if cfg!(unix) && std::env::var_os("DISPLAY").is_none() {
|
|
eprintln!("skipped: no DISPLAY");
|
|
return;
|
|
}
|
|
let profile = std::env::temp_dir().join(format!("jarvis-bot-browser-screen-{}", std::process::id()));
|
|
let screen = if cfg!(windows) {
|
|
let r = jarvis_app::vdisplay::attach().expect("the virtual monitor goes on");
|
|
eprintln!("virtual monitor at {r:?}");
|
|
r
|
|
} else {
|
|
jarvis_app::vdisplay::Rect { x: 40, y: 30, w: 1280, h: 720 }
|
|
};
|
|
let b = BotBrowser::start(std::path::Path::new(&exe), &profile, Some(screen)).await.expect("chrome starts");
|
|
let client = reqwest::Client::builder().no_proxy().build().unwrap();
|
|
let pages = |l: serde_json::Value| l.as_array().unwrap().iter().filter(|t| t["type"] == "page")
|
|
.map(|t| t["id"].as_str().unwrap().to_owned()).collect::<Vec<_>>();
|
|
let list: serde_json::Value = client.get(format!("http://127.0.0.1:{}/json/list", b.cdp_port)).send().await.unwrap()
|
|
.text().await.map(|t| serde_json::from_str(&t).unwrap()).unwrap();
|
|
let first = pages(list);
|
|
assert_eq!(first.len(), 1, "one page in one window");
|
|
client.put(format!("http://127.0.0.1:{}/json/new?about:blank", b.cdp_port)).send().await.unwrap();
|
|
|
|
// Each page's window over CDP: both tabs share the one window, at the screen's rectangle.
|
|
let ws: serde_json::Value = client.get(format!("http://127.0.0.1:{}/json/version", b.cdp_port)).send().await.unwrap()
|
|
.text().await.map(|t| serde_json::from_str(&t).unwrap()).unwrap();
|
|
let (mut s, _) = tokio_tungstenite::connect_async(ws["webSocketDebuggerUrl"].as_str().unwrap()).await.unwrap();
|
|
let list: serde_json::Value = client.get(format!("http://127.0.0.1:{}/json/list", b.cdp_port)).send().await.unwrap()
|
|
.text().await.map(|t| serde_json::from_str(&t).unwrap()).unwrap();
|
|
let ids = pages(list);
|
|
assert_eq!(ids.len(), 2);
|
|
let mut windows = std::collections::HashSet::new();
|
|
for (i, id) in ids.iter().enumerate() {
|
|
use futures_util::{SinkExt, StreamExt};
|
|
let msg = serde_json::json!({"id": i + 1, "method": "Browser.getWindowForTarget", "params": {"targetId": id}});
|
|
s.send(tokio_tungstenite::tungstenite::Message::text(msg.to_string())).await.unwrap();
|
|
let r = loop {
|
|
let v: serde_json::Value = serde_json::from_str(s.next().await.unwrap().unwrap().to_text().unwrap()).unwrap();
|
|
if v["id"] == i + 1 { break v["result"].clone(); }
|
|
};
|
|
eprintln!("window: {r}");
|
|
windows.insert(r["windowId"].as_i64().unwrap());
|
|
let b = &r["bounds"];
|
|
let (left, top, width, height) = (b["left"].as_i64().unwrap(), b["top"].as_i64().unwrap(),
|
|
b["width"].as_i64().unwrap(), b["height"].as_i64().unwrap());
|
|
let (x, y, w, h) = (screen.x as i64, screen.y as i64, screen.w as i64, screen.h as i64);
|
|
if cfg!(windows) {
|
|
// Inside the monitor, filling it but for the monitor's own taskbar if Windows gave it one.
|
|
assert!(left >= x && top >= y && left + width <= x + w && top + height <= y + h, "{b}");
|
|
assert!(width * height * 10 >= w * h * 8, "{b}");
|
|
} else {
|
|
assert_eq!((left, top, width, height), (x, y, w, h));
|
|
}
|
|
}
|
|
assert_eq!(windows.len(), 1, "the bot's tab opens in the same window");
|
|
if std::env::var_os("JARVIS_TEST_HOLD").is_some() {
|
|
// Time for a look at the screen (the Windows check takes a screenshot meanwhile).
|
|
tokio::time::sleep(std::time::Duration::from_secs(8)).await;
|
|
}
|
|
b.stop().await;
|
|
jarvis_app::vdisplay::detach();
|
|
let _ = std::fs::remove_dir_all(&profile);
|
|
}
|