#[cfg(unix)]
fn fake_ssh(dir: &Path, home: &Path) {
crate::proc::stand_in_script(dir, "ssh", &fake_ssh_body(home));
}
#[cfg(unix)]
fn fake_ssh_script(home: &Path) -> String {
format!("#!/bin/sh\n{}", fake_ssh_body(home))
}
#[cfg(unix)]
fn fake_ssh_body(home: &Path) -> String {
format!(
"while [ $# -gt 0 ]; do case \"$1\" in -S|-o) shift 2;; -*) shift;; *) break;; esac; done\nshift\nHOME='{}' exec sh -c \"$*\"\n",
home.display()
)
}
#[cfg(unix)]
fn busy_fake_ssh(dir: &Path, home: &Path, hold: std::time::Duration) {
use std::io::Write as _;
use std::os::unix::fs::OpenOptionsExt;
let shim = dir.join("ssh");
let mut f = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o755)
.open(&shim)
.unwrap();
f.write_all(fake_ssh_script(home).as_bytes()).unwrap();
f.flush().unwrap();
std::thread::spawn(move || {
std::thread::sleep(hold);
drop(f);
});
crate::proc::stand_in("ssh", shim);
}
#[cfg(target_os = "linux")]
#[test]
fn a_live_repush_ships_a_file_without_touching_the_terminal() {
let bin = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
fake_ssh(bin.path(), home.path());
let rsync = crate::proc::record_runs(bin.path(), "rsync");
let local = bin.path().join("keybindings.json");
std::fs::write(&local, "[]\n").unwrap();
let keybindings = *crate::config_sync::SYNCABLE
.iter()
.find(|s| s.name == "keybindings.json")
.unwrap();
let result = push_over_bulk_lane(
"box",
&bin.path().join("ctl"),
&[(keybindings, local)],
&bin.path().join("config-sync.json"),
);
assert!(result.failed.is_empty(), "{:?}", result.failed);
let runs = rsync.lines();
assert_eq!(runs.len(), 1, "one file, one rsync: {runs:?}");
assert!(
runs[0].ends_with("| stdin /dev/null | stdout /dev/null | stderr /dev/null"),
"{runs:?}"
);
}
#[cfg(target_os = "linux")]
#[test]
fn closing_an_unused_connection_prints_nothing_over_croft() {
let bin = tempfile::tempdir().unwrap();
let ssh = crate::proc::record_runs(bin.path(), "ssh");
let socket_dir = bin.path().join("socket");
std::fs::create_dir(&socket_dir).unwrap();
close_master(AdoptedMaster {
host: String::from("box"),
socket_dir: socket_dir.clone(),
socket_path: socket_dir.join("ctl"),
});
assert_eq!(
ssh.lines(),
[format!(
"-S {}/ctl -O exit box | stdin /dev/null | stdout /dev/null | stderr /dev/null",
socket_dir.display()
)]
);
assert!(!socket_dir.exists(), "the socket's directory is removed");
}
#[cfg(unix)]
#[test]
fn the_config_hash_read_sees_the_remotes_files() {
let bin = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
let dir = home.path().join(".config/croft");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("triggers.json"), "{\"c\": \"#123456\"}\n").unwrap();
fake_ssh(bin.path(), home.path());
let got = read_remote_hashes(ssh_socket_read(
&bin.path().join("ctl"),
"box",
&crate::config_sync::remote_hash_script(),
))
.expect("the listing reads");
assert_eq!(
got.get("triggers.json"),
Some(&crate::config_sync::content_hash(b"{\"c\": \"#123456\"}\n")),
"{got:?}"
);
}
#[cfg(unix)]
#[test]
fn the_config_diff_read_returns_the_remote_copy() {
let bin = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
let dir = home.path().join(".config/croft");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("triggers.json"), "remote copy\n").unwrap();
fake_ssh(bin.path(), home.path());
let got = read_remote_config(&bin.path().join("ctl"), "box", "triggers.json").unwrap();
assert_eq!(String::from_utf8_lossy(&got), "remote copy\n");
}
#[cfg(unix)]
#[test]
fn a_briefly_busy_ssh_still_reads_the_remote_copy() {
let bin = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
let dir = home.path().join(".config/croft");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("triggers.json"), "remote copy\n").unwrap();
busy_fake_ssh(
bin.path(),
home.path(),
std::time::Duration::from_millis(100),
);
let got = read_remote_config(&bin.path().join("ctl"), "box", "triggers.json").unwrap();
assert_eq!(String::from_utf8_lossy(&got), "remote copy\n");
}
#[cfg(unix)]
#[test]
fn a_read_still_keeps_ssh_off_the_terminal() {
let bin = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
fake_ssh(bin.path(), home.path());
let mut cmd = ssh_socket_read(&bin.path().join("ctl"), "box", "cat && echo done >&2");
assert!(cmd.get_args().any(|a| a == "-n"));
let out = crate::proc::spawn_retrying_busy(&mut cmd)
.and_then(std::process::Child::wait_with_output)
.unwrap();
assert!(out.stdout.is_empty());
assert!(out.stderr.is_empty(), "stderr stays off the terminal");
}
#[cfg(unix)]
#[test]
fn an_unreadable_listing_pushes_nothing() {
let bin = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
fake_ssh(bin.path(), home.path());
assert_eq!(
read_remote_hashes(ssh_socket_read(&bin.path().join("ctl"), "box", "exit 255")),
None
);
}
#[cfg(unix)]
#[test]
fn a_session_host_or_dtach_on_the_remote_makes_the_session_persistent() {
use std::os::unix::fs::PermissionsExt;
let exe = |path: &Path, body: &str| {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, format!("#!/bin/sh\n{body}\n")).unwrap();
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
};
let check = |home: &Path, extra_path: &Path| {
std::process::Command::new("/bin/sh")
.arg("-c")
.arg(session_supervisor_check_command())
.env("HOME", home)
.env("PATH", format!("{}:/usr/bin:/bin", extra_path.display()))
.status()
.unwrap()
.success()
};
let home = tempfile::tempdir().unwrap();
let bin = tempfile::tempdir().unwrap();
assert!(!check(home.path(), bin.path()), "neither supervisor");
let croft = home.path().join(".cargo/bin/croft");
exe(&croft, "[ \"$1 $2\" = 'session-host --probe' ]");
assert!(check(home.path(), bin.path()), "croft's session host");
exe(&croft, "exit 2");
assert!(!check(home.path(), bin.path()));
exe(&bin.path().join("dtach"), "exit 0");
assert!(check(home.path(), bin.path()), "dtach");
let launch = remote_croft_command_for_terminal(None, None, None, None, false, &[], false);
assert!(launch.contains("croft session-host --probe >/dev/null 2>&1"));
assert!(launch.contains("command -v dtach >/dev/null 2>&1"));
}
fn report(path: &str, host: Option<&str>) -> CwdReport {
(std::path::PathBuf::from(path), host.map(str::to_string))
}
#[test]
fn a_new_remote_report_is_the_workspace_path() {
let before = report("/Users/me/code", Some("localhost"));
let now = report("/srv/app", Some("db-1.internal"));
assert_eq!(
remote_workspace_path("db-1", Some(&before), Some(&now)).as_deref(),
Some("/srv/app")
);
assert_eq!(
remote_workspace_path("db-1", None, Some(&now)).as_deref(),
Some("/srv/app")
);
}
#[test]
fn an_unchanged_report_counts_only_when_it_names_the_host() {
let same = report("/srv/app", Some("db-1"));
assert_eq!(
remote_workspace_path("db-1", Some(&same), Some(&same)).as_deref(),
Some("/srv/app"),
"the remote prompt reported before the offer sampled"
);
let fqdn = report("/srv/app", Some("DB-1.internal"));
assert_eq!(
remote_workspace_path("db-1", Some(&fqdn), Some(&fqdn)).as_deref(),
Some("/srv/app"),
"a domain-qualified name for the same host, any case"
);
let stale = report("/home/old", Some("web-7"));
assert_eq!(
remote_workspace_path("db-1", Some(&stale), Some(&stale)),
None,
"left over from an earlier session on another host"
);
let prefix = report("/x", Some("db-10"));
assert_eq!(
remote_workspace_path("db-1", Some(&prefix), Some(&prefix)),
None,
"db-10 is not db-1"
);
}
#[test]
fn a_local_or_hostless_report_is_never_remote() {
let local = report("/Users/me", Some("localhost"));
assert_eq!(remote_workspace_path("db-1", None, Some(&local)), None);
let hostless = report("/Users/me", None);
assert_eq!(remote_workspace_path("db-1", None, Some(&hostless)), None);
assert_eq!(remote_workspace_path("db-1", None, None), None);
}
#[test]
fn an_ssh_command_line_yields_its_destination() {
let d = |cmd: &[&str]| ssh_destination(cmd);
assert_eq!(d(&["ssh", "box"]).as_deref(), Some("box"));
assert_eq!(d(&["ssh", "user@box"]).as_deref(), Some("user@box"));
assert_eq!(d(&["ssh", "-p", "2222", "box"]).as_deref(), Some("box"));
assert_eq!(
d(&["ssh", "-i", "~/.ssh/id", "box"]).as_deref(),
Some("box")
);
assert_eq!(
d(&["ssh", "-o", "StrictHostKeyChecking=no", "box"]).as_deref(),
Some("box")
);
assert_eq!(d(&["ssh", "-4qt", "box"]).as_deref(), Some("box"));
assert_eq!(d(&["ssh", "-p2222", "box"]).as_deref(), Some("box"));
for f in [
"B", "b", "c", "D", "E", "e", "F", "I", "i", "J", "L", "l", "m", "O", "o", "P", "p", "Q",
"R", "S", "W", "w",
] {
let flag = format!("-{f}");
assert_eq!(
d(&["ssh", &flag, "decoy", "box"]).as_deref(),
Some("box"),
"-{f} swallowed its argument's place"
);
}
assert_eq!(
d(&["ssh", "box", "tail", "-f", "log"]).as_deref(),
Some("box")
);
assert_eq!(d(&["ssh", "--", "box"]).as_deref(), Some("box"));
assert_eq!(d(&["ssh", "-4", "--", "box"]).as_deref(), Some("box"));
assert_eq!(d(&["/usr/bin/ssh", "box"]).as_deref(), Some("box"));
assert_eq!(d(&["ssh", "--", "-4"]).as_deref(), Some("-4"));
}
#[test]
fn the_offer_appears_only_for_a_known_host_in_an_ssh_pane() {
let targets = parse_ssh_config("Host db-1\n HostName 10.0.0.4\n");
let o = |cmd: &[&str]| {
ssh_reroot_decision(cmd, &targets)
.ok()
.map(|h| h.alias.clone())
};
assert_eq!(o(&["ssh", "db-1"]).as_deref(), Some("db-1"));
assert_eq!(
o(&["ssh", "-p", "2222", "deploy@10.0.0.4"]).as_deref(),
Some("db-1")
);
assert_eq!(o(&["zsh"]), None);
assert_eq!(o(&["ssh", "unknown-box"]), None);
assert_eq!(o(&["ssh"]), None);
assert_eq!(o(&["sshfs", "db-1:/srv", "/mnt"]), None);
}
#[test]
fn a_destination_resolves_against_the_configured_hosts() {
let targets = parse_ssh_config(
"Host db-1\n HostName 10.0.0.4\n User deploy\n\nHost web\n HostName web.example\n",
);
let m = |dest: &str| resolve_offer_host(dest, &targets).map(|t| t.alias.clone());
assert_eq!(m("db-1").as_deref(), Some("db-1"));
assert_eq!(m("deploy@db-1").as_deref(), Some("db-1"));
assert_eq!(m("root@db-1").as_deref(), Some("db-1"));
assert_eq!(m("10.0.0.4").as_deref(), Some("db-1"));
assert_eq!(m("web.example").as_deref(), Some("web"));
assert_eq!(m("DB-1").as_deref(), Some("db-1"));
assert_eq!(m("some-random-box"), None);
assert_eq!(m(""), None);
assert_eq!(m("db-1:22"), None);
let shadowed =
parse_ssh_config("Host jump\n HostName db-1\n\nHost db-1\n HostName 10.0.0.4\n");
assert_eq!(
resolve_offer_host("db-1", &shadowed)
.map(|t| t.alias.clone())
.as_deref(),
Some("db-1"),
"an alias must beat another entry's HostName"
);
assert_eq!(
resolve_offer_host("10.0.0.4", &shadowed)
.map(|t| t.alias.clone())
.as_deref(),
Some("db-1")
);
}
#[test]
fn the_decision_names_the_host_or_says_why_not() {
let targets = parse_ssh_config("Host db-1\n HostName 10.0.0.4\n");
let d = |argv: &[&str]| ssh_reroot_decision(argv, &targets).map(|t| t.alias.clone());
assert_eq!(d(&["ssh", "db-1"]), Ok(String::from("db-1")));
let err = d(&["ssh", "other-box"]).unwrap_err();
assert!(err.contains("other-box"), "unhelpful: {err}");
assert!(err.contains("ssh/config"), "unhelpful: {err}");
let err = d(&["zsh"]).unwrap_err();
assert_eq!(err, "This pane is not an SSH session");
assert!(!err.contains("ssh/config"));
}
#[test]
fn a_long_option_program_is_refused_rather_than_misparsed() {
let d = |cmd: &[&str]| ssh_destination(cmd);
for cmd in [
vec!["mosh", "box"],
vec!["mosh", "--port", "60000", "box"],
vec!["mosh", "--ssh", "ssh -p 2222", "box"],
vec!["et", "box"],
vec!["et", "--port", "2022", "box"],
] {
assert_eq!(
d(&cmd),
None,
"{cmd:?} must be refused while the grammar cannot parse it"
);
}
}
#[test]
fn a_non_ssh_command_line_yields_nothing() {
let d = |cmd: &[&str]| ssh_destination(cmd);
for cmd in [
vec!["zsh"],
vec!["ssh"], vec!["ssh", "-p", "2222"], vec!["ssh-agent"], vec!["ssh-keygen", "-t", "ed25519"],
vec!["sshfs", "box:/srv", "/mnt"], vec!["git", "push", "ssh://box/repo"],
vec![],
] {
assert_eq!(d(&cmd), None, "must not match: {cmd:?}");
}
}
#[test]
fn a_background_shell_puts_the_host_before_the_script() {
let ssh = SshControl {
host: String::from("somebox"),
socket_dir: PathBuf::from("/tmp/x"),
socket_path: PathBuf::from("/tmp/x/ctl"),
};
let cmd = ssh.background_shell("mkdir -p ~/.config/croft");
let args: Vec<String> = cmd
.get_args()
.map(|a| a.to_string_lossy().into_owned())
.collect();
let host_at = args.iter().position(|a| a == "somebox");
let script_at = args.iter().position(|a| a == "mkdir -p ~/.config/croft");
assert!(
host_at.is_some(),
"the destination host must be passed: {args:?}"
);
assert!(script_at.is_some(), "the script must be passed: {args:?}");
assert!(
host_at < script_at,
"ssh takes the destination BEFORE the command, or it reads the \
command as the hostname: {args:?}"
);
assert!(
args.iter().any(|a| a == "-n"),
"a background command must not inherit stdin: {args:?}"
);
}
use super::*;
#[test]
#[cfg(unix)]
fn relay_setup_error_appends_stderr_only_when_present() {
use std::os::unix::process::ExitStatusExt as _;
let status = ExitStatus::from_raw(256); assert_eq!(
relay_setup_error(status, b" ssh: connect refused \n"),
format!("remote relay setup failed with {status}: ssh: connect refused")
);
assert_eq!(
relay_setup_error(status, b" \n"),
format!("remote relay setup failed with {status}")
);
assert!(!relay_setup_error(status, b"").ends_with(": "));
}
#[test]
fn the_source_sync_ships_only_build_inputs() {
let tmp = tempfile::tempdir().unwrap();
let src = tmp.path().join("checkout");
let dst = tmp.path().join("staged");
for dir in [
"src",
"assets",
"vendor/async-lsp/src",
"target.noindex/debug",
"target/debug",
".git",
] {
std::fs::create_dir_all(src.join(dir)).unwrap();
}
std::fs::write(src.join("src/main.rs"), "fn main() {}").unwrap();
std::fs::write(src.join("assets/logo.png"), "png").unwrap();
std::fs::write(src.join("Cargo.toml"), "[package]").unwrap();
std::fs::write(src.join("Cargo.lock"), "lock").unwrap();
std::fs::write(src.join("build.rs"), "fn main() {}").unwrap();
std::fs::write(src.join("rust-toolchain.toml"), "[toolchain]").unwrap();
std::fs::write(src.join("vendor/async-lsp/src/lib.rs"), "//").unwrap();
std::fs::write(src.join("target.noindex/debug/croft"), "HUGE").unwrap();
std::fs::write(src.join("target/debug/croft"), "HUGE").unwrap();
std::fs::write(src.join(".git/config"), "gitdir").unwrap();
std::fs::write(src.join("assets/.DS_Store"), "finder").unwrap();
std::fs::create_dir_all(src.join("src/vendor/target")).unwrap();
std::fs::write(src.join("src/vendor/target/blob"), "HUGE").unwrap();
let mut rsync = Command::new("rsync");
rsync.arg("-a").args(source_sync_filter_args());
rsync
.arg(format!("{}/", src.display()))
.arg(format!("{}/", dst.display()));
let Ok(status) = rsync.status() else {
return; };
assert!(status.success(), "rsync exited with {status}");
for shipped in [
"src/main.rs",
"assets/logo.png",
"Cargo.toml",
"Cargo.lock",
"build.rs",
"rust-toolchain.toml",
"vendor/async-lsp/src/lib.rs",
] {
assert!(dst.join(shipped).exists(), "{shipped} must be shipped");
}
for withheld in [
"target.noindex",
"target",
".git",
"assets/.DS_Store",
"src/vendor/target",
] {
assert!(
!dst.join(withheld).exists(),
"{withheld} must never be shipped: this is the 176 GB bug"
);
}
}
fn staged_remote_source(home: &std::path::Path) -> std::path::PathBuf {
let source = home.join(".cache/croft/source");
std::fs::create_dir_all(source.join("target/remote-fast")).unwrap();
std::fs::create_dir_all(source.join("target.noindex/debug")).unwrap();
std::fs::create_dir_all(source.join(".cargo")).unwrap();
std::fs::write(source.join(".cargo/config.toml"), "[build]\njobs = 1\n").unwrap();
std::fs::write(source.join("build.rs"), "fn main() {}").unwrap();
std::fs::create_dir_all(source.join("src")).unwrap();
std::fs::write(source.join("src/main.rs"), "fn main() {}").unwrap();
std::fs::write(source.join("Cargo.toml"), "[package]\n").unwrap();
source
}
fn assert_prep_outcome(source: &std::path::Path) {
assert!(
source.join("target/remote-fast").is_dir(),
"removing the remote's own target/ would force a from-scratch rebuild every install"
);
assert!(
source.join("src/main.rs").is_file() && source.join("Cargo.toml").is_file(),
"inputs present in the local checkout must survive: deleting them re-ships \
everything and yanks the tree out from under a concurrent cargo install"
);
assert!(
!source.join("target.noindex").exists(),
"the artifact tree a previous croft shipped must be reclaimed"
);
assert!(
!source.join(".cargo").exists(),
"a stale dotfile like .cargo/config.toml would silently reconfigure every later remote build"
);
assert!(
!source.join("build.rs").exists(),
"an input no longer in the local checkout must not survive on the remote (tar never deletes)"
);
}
#[test]
fn the_remote_dir_prep_evicts_stale_entries_but_keeps_cache_and_inputs() {
let checkout = tempfile::tempdir().unwrap();
std::fs::create_dir_all(checkout.path().join("src")).unwrap();
std::fs::write(checkout.path().join("Cargo.toml"), "[package]\n").unwrap();
let prep = remote_source_dir_prep(checkout.path());
let tmp = tempfile::tempdir().unwrap();
let source = staged_remote_source(tmp.path());
let status = std::process::Command::new("sh")
.arg("-c")
.arg(&prep)
.env("HOME", tmp.path())
.status()
.unwrap();
assert!(status.success(), "the prep command must run under plain sh");
assert_prep_outcome(&source);
let empty = tempfile::tempdir().unwrap();
let status = std::process::Command::new("sh")
.arg("-c")
.arg(&prep)
.env("HOME", empty.path())
.status()
.unwrap();
assert!(status.success(), "a first install has no source dir yet");
}
#[test]
fn the_remote_dir_prep_follows_a_symlinked_source_dir() {
let checkout = tempfile::tempdir().unwrap();
std::fs::create_dir_all(checkout.path().join("src")).unwrap();
std::fs::write(checkout.path().join("Cargo.toml"), "[package]\n").unwrap();
let prep = remote_source_dir_prep(checkout.path());
let tmp = tempfile::tempdir().unwrap();
let real = tempfile::tempdir().unwrap();
let source = staged_remote_source(real.path());
std::fs::create_dir_all(tmp.path().join(".cache/croft")).unwrap();
std::os::unix::fs::symlink(
real.path().join(".cache/croft/source"),
tmp.path().join(".cache/croft/source"),
)
.unwrap();
let status = std::process::Command::new("sh")
.arg("-c")
.arg(&prep)
.env("HOME", tmp.path())
.status()
.unwrap();
assert!(status.success(), "the prep command must run under plain sh");
assert_prep_outcome(&source);
}
#[test]
fn the_tar_fallback_packs_only_build_inputs() {
let tmp = tempfile::tempdir().unwrap();
let src = tmp.path().join("checkout");
std::fs::create_dir_all(src.join("src")).unwrap();
std::fs::create_dir_all(src.join("target.noindex")).unwrap();
std::fs::write(src.join("src/main.rs"), "fn main() {}").unwrap();
std::fs::write(src.join("Cargo.toml"), "[package]").unwrap();
std::fs::write(src.join("target.noindex/blob"), "HUGE").unwrap();
let members = source_sync_tar_members(&src);
assert!(members.contains(&"src"), "src is a build input");
assert!(
members.contains(&"Cargo.toml"),
"Cargo.toml is a build input"
);
assert!(
!members.iter().any(|m| m.starts_with("target")),
"the artifact tree must never be packed"
);
assert!(
!members.contains(&"build.rs"),
"a member that does not exist must not be named, or tar fails"
);
}
#[test]
fn the_pinned_toolchain_has_every_cross_target() {
if let Some(reason) = cross_compile_unavailable_reason() {
eprintln!("SKIP the_pinned_toolchain_has_every_cross_target: {reason}");
return;
}
let Ok(out) = cross_tool_command_in_checkout("rustup")
.args(["target", "list", "--installed"])
.output()
else {
eprintln!("SKIP the_pinned_toolchain_has_every_cross_target: rustup would not run");
return;
};
if !out.status.success() {
eprintln!("SKIP the_pinned_toolchain_has_every_cross_target: rustup errored");
return;
}
let text = String::from_utf8_lossy(&out.stdout);
let installed: Vec<&str> = text.lines().map(str::trim).collect();
let missing: Vec<&str> = crate::cli::CROSS_TARGETS
.iter()
.copied()
.filter(|t| !installed.contains(t))
.collect();
assert!(
missing.is_empty(),
"the pinned toolchain is missing {missing:?}, so every remote \
update will compile croft on the remote box instead of shipping \
a prebuilt binary. Fix from inside this checkout, so the \
rust-toolchain.toml override applies:\n rustup target add {}",
missing.join(" ")
);
}
#[test]
fn only_toolchain_sensitive_calls_run_in_the_checkout() {
let source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
assert_eq!(
cross_tool_command_in_checkout("rustup").get_current_dir(),
Some(source.as_path()),
"a target query must resolve the pinned toolchain"
);
assert_eq!(
cross_tool_command("cargo").get_current_dir(),
None,
"a presence probe must not trigger a toolchain auto-install"
);
}
#[test]
fn a_release_binary_is_stamped_by_its_version_not_a_missing_tree() {
let tmp = tempfile::tempdir().unwrap();
let gone = tmp.path().join("home/runner/work/croft/croft");
assert_eq!(build_stamp_for(&gone, "0.1.9").unwrap(), "release-v0.1.9");
assert_ne!(
build_stamp_for(&gone, "0.1.9").unwrap(),
build_stamp_for(&gone, "0.1.10").unwrap(),
"a new release is a new stamp, so the remote updates"
);
std::fs::write(tmp.path().join("Cargo.toml"), "[package]").unwrap();
let root = tmp.path().to_path_buf();
assert_eq!(
build_stamp_for(&root, "0.1.9").unwrap(),
source_stamp_for(&root).unwrap(),
"a source tree is still hashed"
);
}
#[test]
fn source_stamp_tracks_build_inputs_only() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path().to_path_buf();
std::fs::create_dir_all(root.join("src")).unwrap();
std::fs::create_dir_all(root.join("target.noindex")).unwrap();
std::fs::create_dir_all(root.join("docs")).unwrap();
std::fs::write(root.join("Cargo.toml"), "[package]").unwrap();
std::fs::write(root.join("src/main.rs"), "fn main() {}").unwrap();
std::fs::write(root.join("target.noindex/dep.rlib"), "artifact-v1").unwrap();
std::fs::write(root.join("docs/NOTES.md"), "notes-v1").unwrap();
let base = source_stamp_for(&root).unwrap();
std::fs::write(root.join("target.noindex/dep.rlib"), "artifact-v2").unwrap();
std::fs::write(root.join("docs/NOTES.md"), "notes-v2").unwrap();
assert_eq!(
source_stamp_for(&root).unwrap(),
base,
"build artifacts and docs must not change the source stamp"
);
std::fs::write(root.join("src/main.rs"), "fn main() { changed(); }").unwrap();
assert_ne!(
source_stamp_for(&root).unwrap(),
base,
"a source edit must change the stamp"
);
}
#[test]
fn drop_to_local_exit_code_maps_to_return_to_local() {
assert_eq!(
classify_remote_status(Some(DROP_TO_LOCAL_EXIT_CODE)),
RemoteStatusClass::ReturnToLocal
);
assert_eq!(classify_remote_status(Some(0)), RemoteStatusClass::Exited);
assert_eq!(
classify_remote_status(Some(127)),
RemoteStatusClass::NotInstalled
);
assert_eq!(classify_remote_status(Some(1)), RemoteStatusClass::Failed);
assert_eq!(classify_remote_status(None), RemoteStatusClass::Failed);
}
#[test]
fn parse_ssh_config_extracts_explicit_hosts() {
let targets = parse_ssh_config(
r#"
Host *
User nobody
Host genesis-cloud github.com
HostName 10.0.0.2
User ubuntu
Host !blocked *.internal
User root
"#,
);
assert_eq!(
targets,
vec![
RemoteTarget {
alias: String::from("genesis-cloud"),
host_name: Some(String::from("10.0.0.2")),
user: Some(String::from("ubuntu")),
},
RemoteTarget {
alias: String::from("github.com"),
host_name: Some(String::from("10.0.0.2")),
user: Some(String::from("ubuntu")),
},
]
);
}
#[test]
fn remote_croft_command_quotes_paths() {
let command = remote_croft_command_for_terminal(
Some("/tmp/it's here"),
None,
None,
None,
false,
&[],
false,
);
assert!(command.contains("croft '/tmp/it'\"'\"'s here'"));
assert!(command.starts_with("export CROFT_REMOTE_AUTOUPDATE=1;"));
}
#[test]
fn remote_croft_command_forwards_supported_terminal_program() {
let command =
remote_croft_command_for_terminal(None, Some("iTerm.app"), None, None, false, &[], false);
assert!(command.contains("export CROFT_FORCE_INLINE_IMAGES=1 TERM_PROGRAM='iTerm.app';"));
}
#[test]
fn remote_croft_command_honors_croft_pane_image_gate() {
let blocked =
remote_croft_command_for_terminal(None, Some("croft"), None, Some("0"), false, &[], false);
assert!(!blocked.contains("CROFT_FORCE_INLINE_IMAGES"));
let allowed =
remote_croft_command_for_terminal(None, Some("croft"), None, Some("1"), false, &[], false);
assert!(allowed.contains("export CROFT_FORCE_INLINE_IMAGES=1 TERM_PROGRAM='croft';"));
}
#[test]
fn remote_croft_command_exports_kitty_hint_for_ghostty() {
let command =
remote_croft_command_for_terminal(None, Some("ghostty"), None, None, false, &[], false);
assert!(command.contains("export TERM_PROGRAM=ghostty;"));
assert!(!command.contains("CROFT_FORCE_INLINE_IMAGES"));
}
#[test]
fn remote_croft_command_exports_kitty_hint_from_term_only() {
let command =
remote_croft_command_for_terminal(None, None, Some("xterm-kitty"), None, false, &[], false);
assert!(command.contains("export TERM_PROGRAM=ghostty;"));
}
#[test]
fn remote_launch_carries_only_a_deterministic_relay_key() {
let id = relay_session_id("");
let env = vec![(String::from("CROFT_RELAY_KEY"), id.clone())];
let command = remote_croft_command_for_terminal(None, None, None, None, false, &env, false);
assert!(
command.contains(&format!("export CROFT_RELAY_KEY='{id}'")),
"launch must export the deterministic relay key, got: {command}",
);
assert!(
!command.contains("CROFT_DROP_RELAY"),
"launch must not use the old per-connection relay env, got: {command}",
);
}
#[test]
fn relay_session_id_matches_dtach_socket_and_is_deterministic() {
assert_eq!(relay_session_id(""), relay_session_id(""));
assert_ne!(relay_session_id("/srv/app"), relay_session_id("/srv/other"));
assert!(dtach_socket_path(None).contains(&relay_session_id("")));
assert!(dtach_socket_path(Some("/srv/app")).contains(&relay_session_id("/srv/app")));
}
#[test]
fn cross_tool_search_dirs_cover_gui_launch_tool_locations() {
let launchd_path = OsString::from("/usr/bin:/bin:/usr/sbin:/sbin");
let home = PathBuf::from("/Users/example");
let dirs = cross_tool_search_dirs_from(Some(launchd_path.as_os_str()), Some(home.as_os_str()));
assert_eq!(dirs.first(), Some(&PathBuf::from("/usr/bin")));
assert!(
dirs.contains(&home.join(".cargo").join("bin")),
"cargo, rustup, and cargo-zigbuild installed by rustup live here, but launchd omits it"
);
assert!(
dirs.contains(&PathBuf::from("/opt/homebrew/bin")),
"Apple Silicon Homebrew's zig lives here, but launchd omits it"
);
assert!(dirs.contains(&PathBuf::from("/usr/local/bin")));
}
#[test]
fn cross_tool_lookup_finds_home_cargo_bin_outside_inherited_path() {
use std::os::unix::fs::PermissionsExt;
let tmp = tempfile::tempdir().unwrap();
let bin_dir = tmp.path().join(".cargo").join("bin");
std::fs::create_dir_all(&bin_dir).unwrap();
let tool = bin_dir.join("cargo-zigbuild");
std::fs::write(&tool, "#!/bin/sh\nexit 0\n").unwrap();
std::fs::set_permissions(&tool, std::fs::Permissions::from_mode(0o755)).unwrap();
let launchd_path = OsString::from("/usr/bin:/bin:/usr/sbin:/sbin");
let dirs =
cross_tool_search_dirs_from(Some(launchd_path.as_os_str()), Some(tmp.path().as_os_str()));
assert_eq!(
crate::proc::find::first_in("cargo-zigbuild", &dirs),
Some(tool),
"the remote fast path must work when Croft.app starts with launchd's stripped PATH"
);
}
#[test]
fn remote_croft_command_forwards_osk_flag_when_local_osk_is_armed() {
let command = remote_croft_command_for_terminal(None, None, None, None, true, &[], false);
assert!(command.contains("export CROFT_FORCE_OSK=1;"));
let command = remote_croft_command_for_terminal(None, None, None, None, false, &[], false);
assert!(!command.contains("CROFT_FORCE_OSK"));
}
#[test]
fn remote_command_prefers_session_host_and_falls_back_to_dtach() {
let command =
remote_croft_command_for_terminal(Some("/srv/app"), None, None, None, false, &[], false);
assert!(command.contains("if croft session-host --probe"));
assert!(command.contains("exec croft session-host --socket"));
assert!(command.contains(".mux.sock"));
assert!(command.contains("--workspace '/srv/app' -- croft '/srv/app'"));
assert!(command.contains("elif command -v dtach"));
assert!(command.contains("dtach -A"));
assert!(command.contains("-E"));
assert!(command.contains("-z"));
assert!(command.contains("-r winch"));
assert!(command.contains("export CROFT_SESSION_PERSISTENT=1;"));
assert!(command.contains("else exec croft"));
}
#[test]
fn remote_command_omits_workspace_flag_without_a_path() {
let command = remote_croft_command_for_terminal(None, None, None, None, false, &[], false);
assert!(command.contains("exec croft session-host --socket"));
assert!(!command.contains("--workspace"));
assert!(command.contains(" -- croft;"));
}
#[test]
fn dtach_socket_is_stable_per_workspace_and_differs_across_paths() {
let a1 = dtach_socket_path(Some("/srv/app"));
let a2 = dtach_socket_path(Some("/srv/app"));
let b = dtach_socket_path(Some("/srv/other"));
assert_eq!(a1, a2, "same workspace must map to the same dtach session");
assert_ne!(a1, b, "different workspaces must not share a session");
assert!(a1.contains("/.cache/croft/sessions/"));
let m = mux_socket_path(Some("/srv/app"));
assert_ne!(m, a1);
assert!(m.ends_with(".mux.sock"));
assert!(m.contains(&relay_session_id("/srv/app")));
let c = collab_socket_path(Some("/srv/app"));
assert_ne!(c, a1);
assert_ne!(c, m);
assert!(c.ends_with(".collab.sock"));
assert!(c.contains(&relay_session_id("/srv/app")));
}
#[test]
fn remote_command_solo_skips_the_mux_and_wires_the_collab_relay() {
let command = remote_croft_command_for_terminal(
Some("/srv/app"),
Some("iTerm.app"),
Some("xterm-256color"),
None,
false,
&[],
true,
);
assert!(!command.contains("session-host --socket"));
assert!(!command.contains("dtach -A"));
assert!(command.contains("if croft collab-relay --probe"));
assert!(command.contains("collab-relay --ensure --socket"));
assert!(command.contains(&collab_socket_path(Some("/srv/app"))));
assert!(command.contains("export CROFT_COLLAB_SOCKET="));
assert!(command.contains("export CROFT_COLLAB_ROLE=guest;"));
assert!(command.contains("exec croft '/srv/app'"));
let shared = remote_croft_command_for_terminal(
Some("/srv/app"),
Some("iTerm.app"),
Some("xterm-256color"),
None,
false,
&[],
false,
);
assert!(shared.contains("if croft session-host --probe"));
assert!(!shared.contains("CROFT_COLLAB_ROLE"));
}
#[test]
fn transport_failure_is_exit_255_only() {
assert!(is_transport_failure(Some(255)));
assert!(!is_transport_failure(Some(0)));
assert!(!is_transport_failure(Some(1)));
assert!(!is_transport_failure(Some(101)));
assert!(!is_transport_failure(None));
}
#[test]
fn a_lost_session_turns_off_every_mode_croft_turned_on() {
let release = lost_session_release_seq();
let takeover = String::from_utf8(crate::app::takeover_mode_seq()).unwrap();
let mut modes = 0;
for set in takeover.split("\x1b[?").skip(1) {
let Some(mode) = set.split('h').next().filter(|m| set.len() > m.len()) else {
continue;
};
for mode in mode.split(';') {
modes += 1;
assert!(
release.contains(&format!("\x1b[?{mode}l")),
"mode {mode} is left on: {release:?}"
);
}
}
assert!(
modes >= 4,
"the takeover's modes were all read: {takeover:?}"
);
assert!(
release.contains("\x1b[0 q"),
"the terminal's own cursor shape"
);
assert!(release.contains("\x1b[?25h"), "a visible cursor");
let rest = release.replace("\x1b[?25h", "");
assert!(
!rest.contains("h\x1b") && !rest.ends_with('h'),
"nothing else is turned on: {release:?}"
);
}
#[test]
fn a_lost_session_leaves_the_alt_screen_last_and_its_colours_come_back_on_resume() {
let release = lost_session_release_seq();
let kitty = release.find("\x1b[=0;1u").expect("the kitty flags cleared");
assert!(release.find("\x1b[<u").is_some_and(|pop| pop < kitty));
assert!(release.ends_with("\x1b7\x1b[?1049l"), "{release:?}");
assert!(kitty < release.len() - "\x1b7\x1b[?1049l".len());
assert!(
release.contains(crate::app::reset_host_colors_seq()),
"{release:?}"
);
let theme = crate::theme::Theme::BLACK;
let reattach = String::from_utf8(crate::app::reattach_reassert_seq(theme)).unwrap();
let takeover = String::from_utf8(crate::app::takeover_mode_seq()).unwrap();
assert!(reattach.starts_with(&takeover), "{reattach:?}");
let (r, g, b) = theme.editor_bg_rgb();
assert!(
reattach.contains(&format!("\x1b]11;rgb:{r:02x}/{g:02x}/{b:02x}\x07")),
"the theme's background: {reattach:?}"
);
assert!(
reattach.contains("\x1b]10;rgb:"),
"and foreground: {reattach:?}"
);
}
#[test]
fn the_ssh_control_dir_is_fresh_and_owner_only() {
use std::os::unix::fs::PermissionsExt;
let dir = super::ssh_control_dir().unwrap();
let mode = std::fs::metadata(&dir).unwrap().permissions().mode();
assert_eq!(mode & 0o777, 0o700);
assert!(std::fs::DirBuilder::new().create(&dir).is_err());
std::fs::remove_dir(&dir).unwrap();
}
#[test]
fn a_pulled_folder_leaves_its_hidden_entries_behind() {
let dir = tempfile::Builder::new()
.prefix("croft-pull")
.tempdir()
.unwrap();
let home = dir.path().join("home");
std::fs::create_dir_all(home.join(".ssh")).unwrap();
std::fs::create_dir_all(home.join("docs/.git")).unwrap();
std::fs::write(home.join(".ssh/id"), "key").unwrap();
std::fs::write(home.join("docs/.git/cfg"), "x").unwrap();
std::fs::write(home.join("docs/a.txt"), "a").unwrap();
std::fs::write(home.join("-dash"), "d").unwrap();
let list = |parent: &Path, name: &str| {
let packed = Command::new("tar")
.args(super::pull_tar_args(parent, std::ffi::OsStr::new(name)))
.output()
.unwrap();
assert!(
packed.status.success(),
"{}",
String::from_utf8_lossy(&packed.stderr)
);
let mut listing = Command::new("tar")
.args(["-t", "-f", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
use std::io::Write;
listing
.stdin
.take()
.unwrap()
.write_all(&packed.stdout)
.unwrap();
String::from_utf8(listing.wait_with_output().unwrap().stdout).unwrap()
};
let names = list(dir.path(), "home");
assert!(
names.contains("home/docs/a.txt") && names.contains("home/-dash"),
"{names}"
);
assert!(
!names.contains(".ssh") && !names.contains(".git"),
"{names}"
);
assert_eq!(list(&home, "-dash").trim(), "-dash");
}
#[test]
fn a_pull_refuses_hidden_paths_and_links_into_them() {
let dir = tempfile::Builder::new()
.prefix("croft-pull")
.tempdir()
.unwrap();
let root = std::fs::canonicalize(dir.path()).unwrap();
std::fs::create_dir(root.join(".ssh")).unwrap();
std::fs::write(root.join(".ssh/id_ed25519"), "key").unwrap();
std::fs::write(root.join("-notes.txt"), "hi").unwrap();
std::os::unix::fs::symlink(root.join(".ssh/id_ed25519"), root.join("key")).unwrap();
assert!(super::pull_source_allowed(&root.join("-notes.txt")));
assert!(!super::pull_source_allowed(&root.join(".ssh/id_ed25519")));
assert!(
!super::pull_source_allowed(&root.join("key")),
"a link into .ssh"
);
assert!(!super::pull_source_allowed(
&root.join("x/../.ssh/id_ed25519")
));
assert!(!super::pull_source_allowed(Path::new("relative.txt")));
}
#[test]
fn parse_relay_request_pulls_have_id_and_path() {
match super::parse_relay_request("pull\tabc-123\t/Users/v/foo.txt") {
Some(super::RelayRequest::Pull { id, src }) => {
assert_eq!(id, "abc-123");
assert_eq!(src, "/Users/v/foo.txt");
}
other => panic!("expected Pull, got {:?}", other.is_some()),
}
}
#[test]
fn activate_command_writes_stamp_then_clears_updating_marker() {
let command = super::activate_command("deadbeef");
assert!(command.contains("mv \"$HOME/.cargo/bin/croft.new\" \"$HOME/.cargo/bin/croft\""));
let stamp_at = command
.find("printf %s 'deadbeef' > \"$HOME/.cache/croft/install-stamp\"")
.expect("stamp write present");
let clear_at = command
.find("rm -f \"$HOME/.cache/croft/updating\"")
.expect("marker clear present");
assert!(
stamp_at < clear_at,
"stamp must be written before marker clear"
);
}
#[test]
fn parse_relay_request_open_extracts_id_and_url() {
match super::parse_relay_request("open\topen-1\thttps://example.com/x") {
Some(super::RelayRequest::Open { id, url }) => {
assert_eq!(id, "open-1");
assert_eq!(url, "https://example.com/x");
}
_ => panic!("expected Open variant"),
}
}
#[test]
fn url_is_safe_to_open_accepts_http_https_mailto() {
assert!(super::url_is_safe_to_open("https://example.com/path"));
assert!(super::url_is_safe_to_open("http://example.com"));
assert!(super::url_is_safe_to_open("HTTPS://Example.COM"));
assert!(super::url_is_safe_to_open("mailto:foo@example.com"));
}
#[test]
fn url_is_safe_to_open_rejects_other_schemes_and_control_chars() {
assert!(!super::url_is_safe_to_open("file:///etc/passwd"));
assert!(!super::url_is_safe_to_open("javascript:alert(1)"));
assert!(!super::url_is_safe_to_open("ssh://attacker"));
assert!(!super::url_is_safe_to_open("https://example.com\nrm -rf /"));
assert!(!super::url_is_safe_to_open("https://example.com\r\nx"));
assert!(!super::url_is_safe_to_open("plaintext"));
}
#[test]
fn parse_relay_request_clipboard_requires_id_only() {
match super::parse_relay_request("clipboard\tclip-7") {
Some(super::RelayRequest::Clipboard { id }) => assert_eq!(id, "clip-7"),
_ => panic!("expected Clipboard variant"),
}
}
#[cfg(unix)]
#[test]
fn a_relay_copy_lands_on_this_clipboard_and_leaves_no_file() {
let bin = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
fake_ssh(bin.path(), home.path());
let inbox = home.path().join("inbox");
std::fs::create_dir(&inbox).unwrap();
let file = inbox.join("copy-1-1.txt");
std::fs::write(&file, "copied text").unwrap();
super::handle_copy_request(
"box",
&bin.path().join("ctl"),
inbox.to_str().unwrap(),
"copy-1-1",
);
assert_eq!(
crate::clipboard::read_string().as_deref(),
Some("copied text")
);
assert!(!file.exists(), "the payload's file is removed");
}
#[cfg(unix)]
#[test]
fn a_relay_copy_past_the_cap_is_dropped() {
let bin = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
fake_ssh(bin.path(), home.path());
let inbox = home.path().join("inbox");
std::fs::create_dir(&inbox).unwrap();
let file = inbox.join("copy-1-2.txt");
std::fs::write(&file, "x".repeat(super::MAX_COPY_PAYLOAD_BYTES + 1)).unwrap();
super::handle_copy_request(
"box",
&bin.path().join("ctl"),
inbox.to_str().unwrap(),
"copy-1-2",
);
assert_eq!(crate::clipboard::read_string(), None);
assert!(!file.exists(), "the payload's file is removed");
}
#[test]
fn parse_relay_request_copy_carries_only_an_id() {
match super::parse_relay_request("copy\tcopy-7") {
Some(super::RelayRequest::Copy { id }) => assert_eq!(id, "copy-7"),
_ => panic!("expected Copy variant"),
}
assert!(super::parse_relay_request("copy\t").is_none());
}
#[test]
fn a_copy_id_that_leaves_the_inbox_is_refused() {
for id in [
"../../../../etc/passwd",
"../outside",
"copy/7",
"copy 7",
"copy;rm -rf /",
"copy$(id)",
"",
] {
assert!(
!super::copy_request_id_is_safe(id),
"{id:?} must not be accepted as a copy id"
);
}
assert!(super::copy_request_id_is_safe("copy-12345-0"));
}
#[test]
fn parse_relay_request_rejects_malformed_lines() {
assert!(super::parse_relay_request("ping\tabc\t/x").is_none());
assert!(super::parse_relay_request("pull\t\t/x").is_none());
assert!(super::parse_relay_request("pull\tabc\t").is_none());
assert!(super::parse_relay_request("clipboard\t").is_none());
}
#[test]
fn parse_relay_request_forward_extracts_port_and_open_flag() {
match super::parse_relay_request("forward\tfwd-1\t3000\t1") {
Some(super::RelayRequest::Forward {
id,
port,
open,
remote_ipv6,
}) => {
assert_eq!(id, "fwd-1");
assert_eq!(port, "3000");
assert!(open);
assert!(!remote_ipv6);
}
_ => panic!("expected Forward variant"),
}
match super::parse_relay_request("forward\tfwd-v6\t8080\t0\t[::1]") {
Some(super::RelayRequest::Forward {
id,
port,
open,
remote_ipv6,
}) => {
assert_eq!(id, "fwd-v6");
assert_eq!(port, "8080");
assert!(!open);
assert!(remote_ipv6);
}
_ => panic!("expected IPv6 Forward variant"),
}
match super::parse_relay_request("unforward\tunfwd-v6\t51234\t8080\t[::1]") {
Some(super::RelayRequest::Unforward {
local,
remote,
remote_ipv6,
}) => {
assert_eq!(local, "51234");
assert_eq!(remote, "8080");
assert!(remote_ipv6);
}
_ => panic!("expected IPv6 Unforward variant"),
}
assert!(super::parse_relay_request("forward\tfwd-bad\t8080\t0\texample.com").is_none());
assert!(super::parse_relay_request("unforward\tu-bad\t5000\t8080\texample.com").is_none());
}
#[test]
fn parse_relay_request_forward_defaults_open_to_false() {
match super::parse_relay_request("forward\tfwd-2\t8080") {
Some(super::RelayRequest::Forward { open, .. }) => assert!(!open),
_ => panic!("expected Forward variant"),
}
assert!(super::parse_relay_request("forward\tfwd-3\t").is_none());
}
#[test]
fn pick_local_port_mirrors_a_free_port() {
let p = super::pick_local_port(54321);
assert!(p == 54321 || p >= 1024, "got {p}");
}
#[test]
fn remote_install_command_installs_from_staged_source() {
let command = remote_install_command("abc123");
assert!(command.contains("cargo install --path \"$HOME/.cache/croft/source\""));
assert!(command.contains("rustup.rs"));
assert!(command.contains("printf %s 'abc123' > \"$HOME/.cache/croft/install-stamp\""));
}
#[test]
fn remote_install_leaves_rc_files_alone_and_never_prompts_for_sudo() {
let command = remote_install_command("abc123");
let rustup = command
.lines()
.find(|l| l.contains("sh.rustup.rs"))
.expect("rustup bootstraps a box without cargo");
assert!(rustup.contains("--no-modify-path"), "{rustup}");
assert!(command.contains("sudo -n \"$@\""));
assert!(
!command.contains("CROFT_SUDO"),
"a variable holding `sudo -n` is one word to zsh"
);
let said = REMOTE_COMPILE_SIDE_EFFECTS.join(" ");
for named in [
"rustup",
"~/.rustup",
"~/.cargo",
"sudo",
"dtach",
"curl",
"ca-certificates",
] {
assert!(said.contains(named), "the prompt names {named}: {said}");
}
}
#[test]
fn a_broken_config_read_keeps_the_last_good_sync_exclusions() {
let mut ex = SyncExclusions { known: None };
let broken = crate::config_layers::MergedConfig {
prefs: crate::prefs::Prefs::default(),
provenance: Default::default(),
chain: Vec::new(),
warnings: vec![String::from("config.json: expected value at line 1")],
user_config_error: Some(String::from("config.json: expected value at line 1")),
};
ex.update(broken.clone());
assert_eq!(ex.known, None);
let prefs = crate::prefs::Prefs {
config_sync_excluded_hosts: vec![String::from("shared-box")],
config_sync_excluded_files: vec![String::from("macros.json")],
..Default::default()
};
let clean = crate::config_layers::MergedConfig {
prefs,
provenance: Default::default(),
chain: Vec::new(),
warnings: Vec::new(),
user_config_error: None,
};
ex.update(clean);
let good = Some((
vec![String::from("shared-box")],
vec![String::from("macros.json")],
));
assert_eq!(ex.known, good);
ex.update(broken);
assert_eq!(ex.known, good);
}
#[test]
fn a_live_repush_ships_each_edit_once_and_retries_a_failed_one() {
use crate::config_sync::ConfigWatch;
let dir = tempfile::tempdir().unwrap();
let keys = dir.path().join("keybindings.json");
let snips = dir.path().join("snippets.json");
let macros = dir.path().join("macros.json");
std::fs::write(&keys, "[]").unwrap();
let mut state = RepushState::new(ConfigWatch::new(vec![
keys.clone(),
snips.clone(),
macros.clone(),
]));
let t0 = std::time::Instant::now();
let mut sent: Vec<Vec<&'static str>> = Vec::new();
let mut lines: Vec<String> = Vec::new();
let tick = |state: &mut RepushState,
now: std::time::Instant,
excluded: Option<Vec<String>>,
fail: &'static [&'static str],
sent: &mut Vec<Vec<&'static str>>,
lines: &mut Vec<String>| {
repush_tick(
state,
dir.path(),
now,
&mut || match excluded.clone() {
Some(files) => RepushGate::Push(files),
None => RepushGate::HostExcluded,
},
&mut |files| {
sent.push(files.iter().map(|(s, _)| s.name).collect());
PushResult {
failed: fail.to_vec(),
held: Vec::new(),
}
},
&mut |l| lines.push(l),
);
};
let step = ConfigWatch::INTERVAL;
tick(
&mut state,
t0 + step,
Some(vec![]),
&[],
&mut sent,
&mut lines,
);
assert!(sent.is_empty(), "nothing edited yet");
std::fs::write(&keys, r#"[{"key": "ctrl+k"}]"#).unwrap();
std::fs::write(&snips, "{}").unwrap();
std::fs::write(¯os, "{}").unwrap();
let failed_at = t0 + step * 2;
tick(
&mut state,
failed_at,
Some(vec![String::from("macros.json")]),
&["snippets.json"],
&mut sent,
&mut lines,
);
assert_eq!(sent, vec![vec!["keybindings.json", "snippets.json"]]);
assert_eq!(
lines,
vec![
"Config sync: re-pushed keybindings.json".to_string(),
"Config sync: could not re-push snippets.json (the remote keeps its copy)".to_string(),
]
);
tick(
&mut state,
t0 + step * 3,
Some(vec![]),
&[],
&mut sent,
&mut lines,
);
assert_eq!(sent.len(), 1, "no retry before REPUSH_RETRY");
tick(
&mut state,
failed_at + REPUSH_RETRY,
Some(vec![]),
&[],
&mut sent,
&mut lines,
);
assert_eq!(sent.last().unwrap(), &vec!["snippets.json"]);
assert!(state.pending.is_empty());
assert_eq!(
lines.last().unwrap(),
"Config sync: re-pushed snippets.json"
);
std::fs::write(&keys, "[ ]").unwrap();
let later = failed_at + REPUSH_RETRY + step;
tick(&mut state, later, None, &[], &mut sent, &mut lines);
assert_eq!(sent.len(), 2, "an excluded host is never pushed to");
assert!(state.pending.is_empty());
}
#[test]
fn a_live_repush_holds_changes_until_the_exclusions_read_cleanly() {
use crate::config_sync::ConfigWatch;
let dir = tempfile::tempdir().unwrap();
let keys = dir.path().join("keybindings.json");
std::fs::write(&keys, "[]").unwrap();
let mut state = RepushState::new(ConfigWatch::new(vec![keys.clone()]));
let t0 = std::time::Instant::now();
std::fs::write(&keys, r#"[{"key": "ctrl+k"}]"#).unwrap();
let mut sent = 0;
let mut lines = Vec::new();
let first = t0 + ConfigWatch::INTERVAL * 2;
repush_tick(
&mut state,
dir.path(),
first,
&mut || RepushGate::Unknown,
&mut |_| {
sent += 1;
PushResult::default()
},
&mut |l| lines.push(l),
);
assert_eq!(sent, 0, "nothing leaves while the exclusions are unknown");
assert_eq!(state.pending.len(), 1, "the edit is held");
assert!(lines[0].contains("holding changes"), "{lines:?}");
repush_tick(
&mut state,
dir.path(),
first + REPUSH_RETRY,
&mut || RepushGate::Push(Vec::new()),
&mut |_| {
sent += 1;
PushResult::default()
},
&mut |l| lines.push(l),
);
assert_eq!(sent, 1, "it goes once they read cleanly");
assert!(state.pending.is_empty());
}
#[test]
fn a_live_repush_holds_a_file_edited_on_the_remote() {
use crate::config_sync::ConfigWatch;
let dir = tempfile::tempdir().unwrap();
let keys = dir.path().join("keybindings.json");
std::fs::write(&keys, "[]").unwrap();
let mut state = RepushState::new(ConfigWatch::new(vec![keys.clone()]));
let t0 = std::time::Instant::now();
std::fs::write(&keys, r#"[{"key": "ctrl+k"}]"#).unwrap();
let mut lines = Vec::new();
repush_tick(
&mut state,
dir.path(),
t0 + ConfigWatch::INTERVAL * 2,
&mut || RepushGate::Push(Vec::new()),
&mut |_| PushResult {
failed: Vec::new(),
held: vec!["keybindings.json"],
},
&mut |l| lines.push(l),
);
assert!(state.pending.is_empty(), "a held file is not retried");
assert!(state.retry_at.is_none());
assert!(!lines.iter().any(|l| l.contains("re-pushed")), "{lines:?}");
assert!(
lines
.iter()
.any(|l| l.contains("left keybindings.json alone") && l.contains("--diff")),
"{lines:?}"
);
}
#[test]
fn remote_install_marks_the_build_for_the_oom_killer_first() {
let command = remote_install_command("abc123");
let adj = command
.find("echo 1000 > /proc/self/oom_score_adj")
.unwrap();
let install = command.find("cargo install --path").unwrap();
assert!(adj < install, "the score must be set before the compile");
let out = Command::new("sh")
.args(["-n", "-c", &command])
.output()
.unwrap();
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
}
#[test]
fn remote_install_compile_yields_to_a_live_croft_session() {
let command = remote_install_command("abc123");
assert!(
command.contains("pgrep -x croft"),
"must detect a live croft session on the box"
);
assert!(
command.contains("CROFT_JOBS=1"),
"a live session drops the compile to a single job"
);
assert!(
command.contains("ionice") && command.contains("-c3"),
"codegen writes must run in the idle IO class so the session's PTY never waits on them"
);
let gate = command.find("pgrep -x croft").unwrap();
let install = command.find("cargo install --path").unwrap();
assert!(gate < install, "the session check must precede the compile");
}
#[test]
fn remote_install_compile_is_memory_bounded() {
let command = remote_install_command("abc123");
let install = command.find("cargo install --path").unwrap();
let before = |needle: &str| {
let at = command
.find(needle)
.unwrap_or_else(|| panic!("missing {needle:?}"));
assert!(at < install, "{needle:?} must come before the compile");
};
before("/proc/meminfo");
before("-lt 16777216");
before("systemd-run --user --scope");
before("MemoryMax=");
before("MemorySwapMax=0");
before("CROFT_MARK=\"$HOME/.cache/croft/building.$$\"");
before("trap '");
assert!(
command.contains(r#"eval "exec $CROFT_MEMCAP $CROFT_NICE $CROFT_IONICE""#),
"the memory cap must wrap the compile itself"
);
let avail = command.find("-lt 2097152").unwrap();
let avail_branch = &command[avail..command[avail..].find("fi\n").unwrap() + avail];
assert!(
avail_branch.contains("CROFT_JOBS=1"),
"low available memory must drop to one job: {avail_branch}"
);
let cleared = command
.rfind("rm -f \"$CROFT_MARK\" \"$CROFT_MARK.boot\"")
.unwrap();
assert!(cleared > install);
}
#[cfg(unix)]
fn install_command(
dir: &std::path::Path,
cargo_exit: u8,
systemd_run: &str,
seen: &std::path::Path,
hold: Option<&std::path::Path>,
) -> std::process::Command {
install_command_under("sh", dir, cargo_exit, systemd_run, seen, hold)
}
#[cfg(unix)]
fn install_command_under(
shell: &str,
dir: &std::path::Path,
cargo_exit: u8,
systemd_run: &str,
seen: &std::path::Path,
hold: Option<&std::path::Path>,
) -> std::process::Command {
use std::os::unix::fs::PermissionsExt;
let bin = dir.join("bin");
let home = dir.join("home");
std::fs::create_dir_all(&bin).unwrap();
std::fs::create_dir_all(home.join(".cache/croft")).unwrap();
let stub = |name: &str, body: &str| {
let path = bin.join(name);
std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap();
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
};
for ok in ["cc", "pkg-config", "dtach"] {
stub(ok, "exit 0");
}
stub("pgrep", "exit 1");
stub("systemd-run", systemd_run);
let tries =
crate::test_budget::spawn_budget(std::time::Duration::from_secs(5)).as_millis() / 50;
stub(
"cargo",
&format!(
r#"i=0
while [ "$i" -lt {tries} ]; do
for f in "$HOME"/.cache/croft/building.*; do
case "$f" in
*.boot) ;;
*) if [ "$(cat "$f" 2>/dev/null)" = "$$" ]; then m=$$; b=$(cat "$f.boot" 2>/dev/null); break 2; fi ;;
esac
done
sleep 0.05; i=$((i+1))
done
printf '%s %s %s' "$$" "$m" "$b" > "$CROFT_TEST_SEEN"
if [ -n "$CROFT_TEST_HOLD" ]; then
: > "$CROFT_TEST_HOLD.ready"
j=0
while [ ! -e "$CROFT_TEST_HOLD" ] && [ "$j" -lt {tries} ]; do sleep 0.05; j=$((j+1)); done
fi
exit {cargo_exit}"#
),
);
let path = format!("{}:/usr/bin:/bin", bin.display());
let mut cmd = std::process::Command::new(shell);
cmd.arg("-c")
.arg(remote_install_command("abc123"))
.env_clear()
.env("HOME", &home)
.env("PATH", path)
.env("CROFT_TEST_SEEN", seen)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null());
if let Some(hold) = hold {
cmd.env("CROFT_TEST_HOLD", hold);
}
cmd
}
#[cfg(unix)]
fn run_install_script(dir: &std::path::Path, cargo_exit: u8) -> (bool, String) {
let seen = dir.join("seen");
let status = install_command(dir, cargo_exit, "exit 1", &seen, None)
.status()
.unwrap();
let seen = std::fs::read_to_string(seen).unwrap_or_default();
(status.success(), seen)
}
#[cfg(target_os = "linux")]
fn running(pid: u32) -> bool {
std::fs::read_to_string(format!("/proc/{pid}/stat"))
.ok()
.and_then(|s| s.rsplit_once(") ").map(|(_, rest)| !rest.starts_with('Z')))
.unwrap_or(false)
}
#[cfg(target_os = "linux")]
fn build_seen(dir: &std::path::Path) -> bool {
crate::update_watch::source_build_running(
&dir.join("home/.cache/croft"),
crate::update_watch::boot_id,
crate::update_watch::boot_time,
running,
)
}
#[cfg(target_os = "linux")]
#[test]
fn a_killed_install_shell_leaves_the_marker_naming_the_live_compile() {
let tmp = tempfile::tempdir().unwrap();
let hold = tmp.path().join("hold");
let seen = tmp.path().join("seen");
let mut shell = install_command(tmp.path(), 0, "exit 1", &seen, Some(&hold))
.spawn()
.unwrap();
let ready = tmp.path().join("hold.ready");
crate::test_budget::await_spawned(
std::time::Duration::from_secs(10),
"the stub compile to start",
|| ready.exists(),
);
let seen = std::fs::read_to_string(&seen).unwrap();
let cargo: u32 = seen.split(' ').next().unwrap().parse().unwrap();
assert!(seen.starts_with(&format!("{cargo} {cargo}")), "{seen}");
unsafe {
libc::kill(shell.id() as libc::pid_t, libc::SIGTERM);
}
let _ = shell.wait();
assert!(running(cargo), "the compile must outlive its shell");
assert_eq!(
leftover_markers(tmp.path())
.iter()
.filter(|n| !n.ends_with(".boot"))
.map(|n| std::fs::read_to_string(tmp.path().join("home/.cache/croft").join(n)).unwrap())
.collect::<Vec<_>>(),
vec![cargo.to_string()],
"the marker must keep naming the compile"
);
assert!(build_seen(tmp.path()), "croft must still see the build");
std::fs::write(&hold, "").unwrap();
crate::test_budget::await_spawned(
std::time::Duration::from_secs(10),
"the build to read as over once the compile exits",
|| !build_seen(tmp.path()),
);
}
#[cfg(target_os = "linux")]
#[test]
fn a_second_install_finishing_first_leaves_the_first_ones_marker() {
let tmp = tempfile::tempdir().unwrap();
let hold = tmp.path().join("hold");
let mut long = install_command(
tmp.path(),
0,
"exit 1",
&tmp.path().join("seen-long"),
Some(&hold),
)
.spawn()
.unwrap();
let ready = tmp.path().join("hold.ready");
crate::test_budget::await_spawned(
std::time::Duration::from_secs(10),
"the long build's compile to start",
|| ready.exists(),
);
let short = install_command(
tmp.path(),
0,
"exit 1",
&tmp.path().join("seen-short"),
None,
)
.status()
.unwrap();
assert!(short.success());
assert!(
build_seen(tmp.path()),
"the short build's end hid the long one: {:?}",
leftover_markers(tmp.path())
);
std::fs::write(&hold, "").unwrap();
assert!(long.wait().unwrap().success());
assert!(leftover_markers(tmp.path()).is_empty());
}
#[cfg(target_os = "linux")]
#[test]
fn the_install_script_caps_the_compile_only_when_the_limit_reads_back() {
for (probe, capped) in [("67108864", true), ("max", false)] {
let tmp = tempfile::tempdir().unwrap();
let log = tmp.path().join("capped");
let systemd_run = format!(
r#"while [ $# -gt 0 ]; do case "$1" in -p) shift 2 ;; --*) shift ;; *) break ;; esac; done
if [ "$1" = sh ]; then echo {probe}; exit 0; fi
: > "{}"
exec "$@""#,
log.display()
);
let seen = tmp.path().join("seen");
let status = install_command(tmp.path(), 0, &systemd_run, &seen, None)
.status()
.unwrap();
assert!(status.success(), "probe {probe}");
assert_eq!(log.exists(), capped, "probe {probe}");
let seen = std::fs::read_to_string(&seen).unwrap();
let mut f = seen.split(' ');
assert_eq!(f.next(), f.next(), "probe {probe}: {seen}");
}
}
#[cfg(unix)]
fn leftover_markers(dir: &std::path::Path) -> Vec<String> {
std::fs::read_dir(dir.join("home/.cache/croft"))
.unwrap()
.flatten()
.map(|e| e.file_name().to_string_lossy().into_owned())
.filter(|n| n.starts_with("building."))
.collect()
}
#[cfg(unix)]
#[test]
fn the_install_script_marks_the_compile_and_clears_the_mark() {
let tmp = tempfile::tempdir().unwrap();
let (ok, seen) = run_install_script(tmp.path(), 0);
assert!(ok, "the stubbed install must succeed");
let mut fields = seen.splitn(3, ' ');
let (own, marked, boot) = (
fields.next().unwrap(),
fields.next().expect("cargo ran and reported"),
fields.next().unwrap_or_default(),
);
let host_boot = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")
.map(|b| b.trim().to_string())
.unwrap_or_default();
assert_eq!(boot.trim(), host_boot, "the marker's boot sidecar");
assert_eq!(
own, marked,
"while cargo runs the marker must name cargo itself, so a killed \
script shell cannot make the build look finished"
);
assert!(leftover_markers(tmp.path()).is_empty());
assert_eq!(
std::fs::read_to_string(tmp.path().join("home/.cache/croft/install-stamp")).unwrap(),
"abc123"
);
let tmp = tempfile::tempdir().unwrap();
let (ok, _) = run_install_script(tmp.path(), 3);
assert!(!ok, "a failing compile must fail the install");
assert!(
leftover_markers(tmp.path()).is_empty(),
"a failed build left its marker behind"
);
}
#[cfg(unix)]
#[test]
fn the_install_script_reaches_cargo_under_zsh() {
if !crate::lsp::manager::is_on_path("zsh") {
eprintln!("SKIPPED: zsh not on PATH");
return;
}
let under_zsh = |dir: &std::path::Path, cargo_exit| {
let seen = dir.join("seen");
let status = install_command_under("zsh", dir, cargo_exit, "exit 1", &seen, None)
.status()
.unwrap();
(
status.success(),
std::fs::read_to_string(seen).unwrap_or_default(),
)
};
let tmp = tempfile::tempdir().unwrap();
let (ok, seen) = under_zsh(tmp.path(), 0);
assert!(ok, "an empty cache must not stop the install under zsh");
let mut fields = seen.splitn(3, ' ');
let own = fields.next().unwrap();
let marked = fields.next().expect("cargo ran under zsh");
assert_eq!(own, marked);
assert!(leftover_markers(tmp.path()).is_empty());
assert_eq!(
std::fs::read_to_string(tmp.path().join("home/.cache/croft/install-stamp")).unwrap(),
"abc123"
);
let tmp = tempfile::tempdir().unwrap();
let (ok, _) = under_zsh(tmp.path(), 3);
assert!(!ok, "a failing compile must fail the install under zsh");
assert!(
!tmp.path().join("home/.cache/croft/install-stamp").exists(),
"a failed compile must not write the install stamp"
);
}
#[cfg(unix)]
#[test]
fn the_install_script_sweeps_only_dead_markers() {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().join("home/.cache/croft");
std::fs::create_dir_all(&cache).unwrap();
std::fs::write(cache.join("building.1"), std::process::id().to_string()).unwrap();
std::fs::write(cache.join("building.2"), "2147483646").unwrap();
let this_boot = std::fs::read_to_string("/proc/sys/kernel/random/boot_id").ok();
if let Some(boot) = &this_boot {
std::fs::write(cache.join("building.1.boot"), boot.trim()).unwrap();
std::fs::write(cache.join("building.3"), std::process::id().to_string()).unwrap();
std::fs::write(cache.join("building.3.boot"), "another-boot").unwrap();
}
std::fs::write(cache.join("building.4.boot"), "orphan").unwrap();
let (ok, _) = run_install_script(tmp.path(), 0);
assert!(ok);
let mut left = leftover_markers(tmp.path());
left.sort();
let expect: Vec<String> = if this_boot.is_some() {
vec!["building.1".into(), "building.1.boot".into()]
} else {
vec!["building.1".into()]
};
assert_eq!(
left, expect,
"only the live build of this boot, and its sidecar, may survive another build"
);
}
#[test]
fn install_log_tee_writes_lines_to_file_and_forwards_them_unchanged() {
let dir = std::env::temp_dir().join(format!("croft-log-tee-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("install.log");
let (down_tx, down_rx) = std::sync::mpsc::channel::<String>();
let tee = spawn_log_tee(path.clone(), down_tx);
tee.send(String::from("Cross-compiling croft locally"))
.unwrap();
tee.send(String::from("Local cross-build skipped (zig missing)"))
.unwrap();
let timeout = std::time::Duration::from_secs(2);
assert_eq!(
down_rx.recv_timeout(timeout).unwrap(),
"Cross-compiling croft locally",
"downstream consumers must still receive every line"
);
assert_eq!(
down_rx.recv_timeout(timeout).unwrap(),
"Local cross-build skipped (zig missing)"
);
drop(tee);
let deadline = std::time::Instant::now() + timeout;
let mut contents = String::new();
while std::time::Instant::now() < deadline {
contents = std::fs::read_to_string(&path).unwrap_or_default();
if contents.contains("zig missing") {
break;
}
std::thread::sleep(std::time::Duration::from_millis(50));
}
assert!(contents.contains("Cross-compiling croft locally"));
assert!(
contents.contains("Local cross-build skipped (zig missing)"),
"the skip REASON is the whole point of the persistent log"
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn remote_install_command_ensures_c_toolchain_unconditionally() {
let command = remote_install_command("abc123");
assert!(command.contains("croft_ensure_build_toolchain"));
assert!(command.contains("build-essential"));
let ensure_call = command
.rfind("\ncroft_ensure_build_toolchain")
.expect("toolchain ensure must be invoked");
let cargo_install = command
.find("cargo install --path")
.expect("cargo install must be present");
assert!(
ensure_call < cargo_install,
"the C toolchain must be ensured before `cargo install` runs"
);
}
#[test]
fn a_relay_request_is_claimed_by_exactly_one_pump() {
let tmp = tempfile::tempdir().unwrap();
let inbox = tmp.path().join("inbox").display().to_string();
let run = |cmd: String| {
std::process::Command::new("sh")
.arg("-c")
.arg(cmd)
.status()
.unwrap()
.success()
};
assert!(run(claim_command(&inbox, "open-1-2")));
assert!(
!run(claim_command(&inbox, "open-1-2")),
"a second claim loses"
);
assert!(run(claim_command(&inbox, "open-1-3")));
assert_eq!(
super::parse_relay_request("open\topen-1\thttps://x/")
.and_then(|r| r.id().map(str::to_string)),
Some(String::from("open-1"))
);
}
#[test]
fn arch_to_musl_triple_maps_every_arch_uname_reports_for_supported_targets() {
assert_eq!(
arch_to_musl_triple("x86_64"),
Some("x86_64-unknown-linux-musl")
);
assert_eq!(
arch_to_musl_triple("amd64"),
Some("x86_64-unknown-linux-musl"),
"BSD-style `uname -m` reports amd64 for the same machine class linux reports as x86_64; the fast-install path must accept both"
);
assert_eq!(
arch_to_musl_triple("aarch64"),
Some("aarch64-unknown-linux-musl")
);
assert_eq!(
arch_to_musl_triple("arm64"),
Some("aarch64-unknown-linux-musl"),
"Apple Silicon Linux VMs report arm64; same triple as aarch64"
);
}
#[test]
fn arch_to_musl_triple_returns_none_for_unsupported_archs_so_caller_falls_back() {
for arch in ["i686", "armv7l", "ppc64le", "riscv64", "mips", ""] {
assert_eq!(
arch_to_musl_triple(arch),
None,
"{arch} has no statically-known musl target in croft's bundled toolchain; caller must fall back to remote cargo install"
);
}
}
#[test]
fn remote_install_check_uses_cargo_path_before_probe() {
let command = remote_install_check_command();
let path_pos = command
.find("export PATH=\"$HOME/.cargo/bin:$PATH\"")
.unwrap();
let probe_pos = command.find("command -v croft").unwrap();
assert!(command.contains(". \"$HOME/.cargo/env\""));
assert!(path_pos < probe_pos);
assert!(command.contains("cat \"$HOME/.cache/croft/install-stamp\""));
}
#[test]
fn shell_quote_for_e_arg_passes_alnum_paths_through() {
assert_eq!(
shell_quote_for_e_arg(Path::new("/tmp/croft-ssh-1234/ctl")),
"/tmp/croft-ssh-1234/ctl",
);
}
#[test]
fn shell_quote_for_e_arg_quotes_paths_with_spaces() {
let q = shell_quote_for_e_arg(Path::new("/Users/v a/croft/ctl"));
assert_eq!(q, "'/Users/v a/croft/ctl'");
}
#[test]
fn shell_quote_for_e_arg_escapes_single_quotes_inside_path() {
let q = shell_quote_for_e_arg(Path::new("/tmp/it's/ctl"));
assert_eq!(q, "'/tmp/it'\\''s/ctl'");
}
#[test]
fn ssh_control_socket_path_is_short_and_inside_temp_dir() {
let dir = std::env::temp_dir().join("croft-ssh-test");
let socket = ssh_control_socket_path_for_test(&dir);
assert!(socket.starts_with(&dir));
assert_eq!(socket.file_name().and_then(|s| s.to_str()), Some("ctl"));
}
fn args_of(cmd: &Command) -> Vec<String> {
cmd.get_args()
.map(|a| a.to_string_lossy().into_owned())
.collect()
}
#[test]
fn background_ssh_never_reads_the_attached_sessions_stdin() {
let socket = PathBuf::from("/tmp/croft-ctl/ctl");
let background = args_of(&ssh_socket_command(&socket, true));
assert!(
background.iter().any(|a| a == "-n"),
"a background command sharing the session's tty steals its keystrokes"
);
let interactive = args_of(&ssh_socket_command(&socket, false));
assert!(
!interactive.iter().any(|a| a == "-n"),
"the interactive session is the one process that must keep the tty"
);
}
#[test]
fn binary_ship_never_rides_the_interactive_master_when_lane_is_dedicated() {
let lane = crate::remote_bulk::BulkLane::new(
crate::remote_bulk::LaneMode::Dedicated {
socket_path: PathBuf::from("/tmp/bulk/ctl"),
},
512,
);
let cmd = ship_file_rsync_command(
&lane,
Path::new("/tmp/interactive/ctl"),
Path::new("/tmp/target/croft"),
"host:.cargo/bin/croft.new",
);
let args = args_of(&cmd);
let e = args
.iter()
.position(|a| a == "-e")
.map(|i| args[i + 1].clone())
.expect("rsync must use an explicit -e remote shell");
assert!(e.contains("/tmp/bulk/ctl"));
assert!(!e.contains("/tmp/interactive/ctl"));
assert!(
args.iter().any(|a| a == "--bwlimit=512"),
"unpaced bulk saturates the uplink queue and lags the session even on its own connection"
);
}
#[test]
fn a_config_push_rides_the_bulk_lane_like_the_binary_does() {
let lane = crate::remote_bulk::BulkLane::new(
crate::remote_bulk::LaneMode::Dedicated {
socket_path: PathBuf::from("/tmp/bulk/ctl"),
},
512,
);
let cmd = ship_file_rsync_command(
&lane,
Path::new("/tmp/interactive/ctl"),
Path::new("/home/u/.config/croft/keybindings.json"),
&crate::config_sync::remote_dest("host", "keybindings.json"),
);
let args = args_of(&cmd);
let e = args
.iter()
.position(|a| a == "-e")
.map(|i| args[i + 1].clone())
.expect("rsync must use an explicit -e remote shell");
assert!(e.contains("/tmp/bulk/ctl"));
assert!(!e.contains("/tmp/interactive/ctl"));
assert!(args.iter().any(|a| a == "--bwlimit=512"));
assert!(
args.iter().any(|a| a == "--checksum"),
"content comparison is what makes the push a no-op when nothing changed"
);
assert!(
args.iter()
.any(|a| a == "host:.config/croft/keybindings.json"),
"the file must land in the remote's config dir, got {args:?}"
);
}
#[test]
fn a_config_push_never_carries_the_trust_bearing_config_json() {
let lane = crate::remote_bulk::BulkLane::new(crate::remote_bulk::LaneMode::SharedMux, 300);
for s in crate::config_sync::SYNCABLE {
let cmd = ship_file_rsync_command(
&lane,
Path::new("/tmp/ctl"),
Path::new("/home/u/.config/croft").join(s.name).as_path(),
&crate::config_sync::remote_dest("host", s.name),
);
let args = args_of(&cmd);
assert!(
!args.iter().any(|a| a.ends_with("config.json")),
"config.json carries MCP consent and must never be in a push argv, got {args:?}"
);
}
}
#[test]
fn binary_ship_throttles_on_the_shared_mux_when_no_lane_is_available() {
let lane = crate::remote_bulk::BulkLane::new(crate::remote_bulk::LaneMode::SharedMux, 300);
let cmd = ship_file_rsync_command(
&lane,
Path::new("/tmp/interactive/ctl"),
Path::new("/tmp/target/croft"),
"host:.cargo/bin/croft.new",
);
let args = args_of(&cmd);
let e = args
.iter()
.position(|a| a == "-e")
.map(|i| args[i + 1].clone())
.expect("rsync must use an explicit -e remote shell");
assert!(e.contains("/tmp/interactive/ctl"));
assert!(args.iter().any(|a| a == "--bwlimit=300"));
assert!(args.iter().any(|a| a == "--checksum"));
}
#[test]
fn source_sync_honors_the_bulk_lane_and_allow_lists_the_build_inputs() {
let lane = crate::remote_bulk::BulkLane::new(
crate::remote_bulk::LaneMode::Dedicated {
socket_path: PathBuf::from("/tmp/bulk/ctl"),
},
700,
);
let cmd = source_sync_rsync_command(
&lane,
Path::new("/tmp/interactive/ctl"),
std::ffi::OsStr::new("/Users/v/croft/"),
"host:.cache/croft/source/",
);
let args = args_of(&cmd);
let e = args
.iter()
.position(|a| a == "-e")
.map(|i| args[i + 1].clone())
.expect("rsync must use an explicit -e remote shell");
assert!(e.contains("/tmp/bulk/ctl"));
assert!(!e.contains("/tmp/interactive/ctl"));
assert!(args.iter().any(|a| a == "--bwlimit=700"));
for name in SOURCE_STAMP_INPUTS {
assert!(
args.iter().any(|a| a == &format!("--include=/{name}")),
"{name} is a build input and must be shipped"
);
}
assert!(
args.iter().any(|a| a == "--exclude=*"),
"the allow-list is only an allow-list if everything else is denied"
);
for name in SOURCE_SKIP_NAMES {
assert!(
args.iter().any(|a| a == &format!("--exclude={name}")),
"{name} is skipped by the stamp walk, so it must not be shipped"
);
}
assert!(args.iter().any(|a| a == "--delete"));
}
#[test]
fn raise_fd_limit_lifts_the_soft_limit_beyond_the_linkers_needs() {
let soft = raise_fd_limit();
assert!(
soft >= 4096,
"croft's cross-link opens ~250 rlibs plus zig's own files; a {soft}-fd soft limit reproduces ProcessFdQuotaExceeded"
);
}
#[test]
fn cargo_lock_croft_version_matches_cargo_toml_so_locked_cross_build_never_falls_back() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let toml = std::fs::read_to_string(root.join("Cargo.toml")).unwrap();
let toml_version = toml
.lines()
.find_map(|l| {
l.strip_prefix("version = \"")
.and_then(|r| r.strip_suffix('"'))
})
.expect("Cargo.toml has a package version");
let lock = std::fs::read_to_string(root.join("Cargo.lock")).unwrap();
let lock_version = lock
.split("name = \"croft-software\"")
.nth(1)
.and_then(|after| {
after.lines().find_map(|l| {
l.trim()
.strip_prefix("version = \"")
.and_then(|r| r.strip_suffix('"'))
})
})
.expect("Cargo.lock has a croft entry");
assert_eq!(
lock_version, toml_version,
"Cargo.lock croft version {lock_version} drifted from Cargo.toml {toml_version}; run `cargo update -p croft-software`"
);
}
#[test]
fn snapshot_dirs_are_detected_and_real_repos_are_not() {
assert!(source_dir_is_snapshot(
"/Users/u/.cargo/registry/src/index.crates.io-6f17d22bba15001f/croft-software-0.1.757"
));
assert!(source_dir_is_snapshot(
"/home/u/.cargo/git/checkouts/croft-9a1f/de1a7ab"
));
assert!(source_dir_is_snapshot(
"/custom/cargo-home/.cargo/git/checkouts/croft-9a1f/de1a7ab"
));
assert!(source_dir_is_snapshot(
"/custom/cargo-home/registry/src/index.crates.io-6f17d22bba15001f/croft-software-0.1.757"
));
assert!(source_dir_is_snapshot(
"/custom/cargo-home/git/checkouts/croft-9a1f/de1a7ab"
));
assert!(!source_dir_is_snapshot("/Users/u/Documents/croft"));
assert!(!source_dir_is_snapshot("/home/u/work/croft"));
assert!(
source_snapshot_warning().is_none(),
"the test build itself must come from a working tree, not a snapshot"
);
}
#[test]
fn remote_launch_carries_whether_underline_colours_are_read() {
for (supported, value) in [(true, "1"), (false, "0")] {
let env = with_underline_colors(&[], supported);
let command = remote_croft_command_for_terminal(None, None, None, None, false, &env, false);
assert!(
command.contains(&format!("export CROFT_UNDERLINE_COLORS='{value}';")),
"{command}"
);
}
let mine = vec![(String::from("CROFT_UNDERLINE_COLORS"), String::from("1"))];
let env = with_underline_colors(&mine, false);
assert_eq!(env.last(), mine.last(), "the user's entry comes last");
let launch = remote_croft_command_for_terminal(None, None, None, None, false, &[], false);
assert!(
launch.contains("export CROFT_SESSION_PERSISTENT=1 CROFT_UNDERLINE_COLORS=0; exec dtach"),
"{launch}"
);
}
#[cfg(unix)]
#[test]
fn a_pull_finishes_when_tar_prints_more_than_a_pipe_holds() {
let dir = tempfile::Builder::new()
.prefix("croft-pull")
.tempdir()
.unwrap();
let src = dir.path().join("notes.txt");
std::fs::write(&src, "hello").unwrap();
let bin = tempfile::Builder::new()
.prefix("croft-bin")
.tempdir()
.unwrap();
let bin_path = bin.path().to_path_buf();
let log = bin.path().join("ssh-args");
let src = src.display().to_string();
let (tx, rx) = std::sync::mpsc::channel();
let log_for_ssh = log.clone();
std::thread::spawn(move || {
crate::proc::stand_in_script(
&bin_path,
"tar",
"head -c 1048576 /dev/zero >&2\necho archive\n",
);
crate::proc::stand_in_script(
&bin_path,
"ssh",
&format!(
"echo \"$*\" >> '{}'\ncat >/dev/null\n",
log_for_ssh.display()
),
);
super::handle_pull_request(
"host",
std::path::Path::new("/nonexistent/ctl"),
"/tmp/inbox",
"req1",
&src,
);
let _ = tx.send(());
});
assert!(
rx.recv_timeout(std::time::Duration::from_secs(20)).is_ok(),
"the pull hung on tar's warnings"
);
let calls = std::fs::read_to_string(&log).unwrap_or_default();
assert!(calls.contains("tar -x"), "the archive was sent: {calls}");
}
#[cfg(target_os = "linux")]
#[test]
fn an_install_inside_croft_places_the_prebuilt_off_the_terminal() {
let bin = tempfile::tempdir().unwrap();
let ssh_runs = crate::proc::record_runs(bin.path(), "ssh");
let rsync_runs = crate::proc::record_runs(bin.path(), "rsync");
let binary = bin.path().join("croft");
std::fs::write(&binary, "binary").unwrap();
let ssh = SshControl {
host: String::from("box"),
socket_dir: bin.path().to_path_buf(),
socket_path: bin.path().join("ctl"),
};
let lane = crate::remote_bulk::BulkLane::new(crate::remote_bulk::LaneMode::SharedMux, 0);
for name in ["ssh", "rsync"] {
let recorder = std::fs::read_to_string(bin.path().join(name)).unwrap();
let body: String = recorder.lines().skip(2).map(|l| format!("{l}\n")).collect();
crate::proc::stand_in_script(bin.path(), name, &format!("{body}echo {name} step ran\n"));
}
let (tx, rx) = std::sync::mpsc::channel();
place_prebuilt(
&ssh,
&binary,
"stamp",
&mut |b| ship_prebuilt_logged(&lane, &ssh, b, &tx),
&mut |step| run_command_streaming(step, &tx),
)
.unwrap();
let ssh_lines = ssh_runs.lines();
let rsync_lines = rsync_runs.lines();
assert_eq!(ssh_lines.len(), 2, "mkdir and activate: {ssh_lines:?}");
assert_eq!(rsync_lines.len(), 1, "one copy: {rsync_lines:?}");
for run in ssh_lines.iter().chain(&rsync_lines) {
assert!(
run.ends_with("| stdin /dev/null | stdout pipe | stderr pipe"),
"{run}"
);
}
drop(tx);
let logged: Vec<String> = rx.iter().collect();
assert_eq!(
logged,
["ssh step ran", "rsync step ran", "ssh step ran"],
"each step's output went into the install log"
);
}
#[cfg(unix)]
#[test]
fn a_noisy_tar_failure_reaches_the_remote() {
let dir = tempfile::Builder::new()
.prefix("croft-pull")
.tempdir()
.unwrap();
let src = dir.path().join("notes.txt");
std::fs::write(&src, "hello").unwrap();
let bin = tempfile::Builder::new()
.prefix("croft-bin")
.tempdir()
.unwrap();
let bin_path = bin.path().to_path_buf();
let calls = bin.path().join("ssh-args");
let fed = bin.path().join("ssh-stdin");
let src = src.display().to_string();
let (tx, rx) = std::sync::mpsc::channel();
let (calls_for_ssh, fed_for_ssh) = (calls.clone(), fed.clone());
std::thread::spawn(move || {
crate::proc::stand_in_script(
&bin_path,
"tar",
"yes 'tar: notes.txt: Cannot open: Permission denied' | head -c 524288 >&2\n\
echo 'tar: Exiting with failure status due to previous errors' >&2\n\
exit 2\n",
);
crate::proc::stand_in_script(
&bin_path,
"ssh",
&format!(
"echo \"$*\" >> '{}'\ncat >> '{}'\n",
calls_for_ssh.display(),
fed_for_ssh.display()
),
);
super::handle_pull_request(
"host",
std::path::Path::new("/nonexistent/ctl"),
"/tmp/inbox",
"req1",
&src,
);
let _ = tx.send(());
});
assert!(
rx.recv_timeout(std::time::Duration::from_secs(20)).is_ok(),
"the pull hung on tar's warnings"
);
let calls = std::fs::read_to_string(&calls).unwrap_or_default();
assert!(
calls.contains("/tmp/inbox/req1/.err"),
"the failure was written back: {}",
calls
.lines()
.map(|l| &l[..l.len().min(200)])
.collect::<Vec<_>>()
.join("\n")
);
let fed = std::fs::read_to_string(&fed).unwrap_or_default();
assert!(
fed.contains("tar: Exiting with failure status due to previous errors"),
"the failure says why: {} bytes fed",
fed.len()
);
}