mod support;
use std::path::PathBuf;
use std::process::{Command, Output};
fn command(args: &[&str]) -> Command {
if args.first() == Some(&"capture")
&& let Some(index) = args.iter().position(|arg| *arg == "--repo")
{
support::install_empty_ready_change_store(std::path::Path::new(args[index + 1]));
}
let mut command = Command::new(env!("CARGO_BIN_EXE_pointbreak"));
command.args(args);
for selector in pointbreak::environment::RUNTIME_VARIABLES {
command.env_remove(selector);
}
command.env_remove("SHORE_HOME");
command.env_remove("XDG_DATA_HOME");
command.env_remove("HOME");
command.env_remove("APPDATA");
command
}
fn output(command: &mut Command) -> Output {
command.output().expect("run pointbreak")
}
fn key_path(output: &Output) -> PathBuf {
assert!(
output.status.success(),
"stderr:\n{}",
String::from_utf8_lossy(&output.stderr)
);
let document: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
PathBuf::from(document["path"].as_str().unwrap())
}
#[test]
fn explicit_pointbreak_home_owns_key_placement_and_old_name_is_ignored() {
let canonical = tempfile::tempdir().unwrap();
let old = tempfile::tempdir().unwrap();
let output = output(
command(&["key", "init", "--name", "canonical"])
.env("POINTBREAK_HOME", canonical.path())
.env("SHORE_HOME", old.path()),
);
assert!(key_path(&output).starts_with(canonical.path().join("keys")));
assert!(!old.path().join("keys").exists());
}
#[test]
fn old_home_name_cannot_redirect_default_key_placement() {
let old = tempfile::tempdir().unwrap();
let xdg = tempfile::tempdir().unwrap();
let fallback = tempfile::tempdir().unwrap();
let output = output(
command(&["key", "init", "--name", "default-path"])
.env("SHORE_HOME", old.path())
.env("XDG_DATA_HOME", xdg.path())
.env("HOME", fallback.path()),
);
assert!(
key_path(&output).starts_with(xdg.path().join("pointbreak").join("keys")),
"key path: {}",
key_path(&output).display()
);
assert!(!old.path().join("keys").exists());
}
#[test]
fn empty_and_relative_explicit_homes_fail_without_creating_directories() {
for (name, value) in [("empty", ""), ("relative", "relative-home")] {
let fallback = tempfile::tempdir().unwrap();
let output = output(
command(&["key", "init", "--name", name])
.current_dir(fallback.path())
.env("POINTBREAK_HOME", value)
.env("HOME", fallback.path()),
);
assert!(!output.status.success(), "{name} explicit home must fail");
assert!(
String::from_utf8_lossy(&output.stderr).contains("POINTBREAK_HOME"),
"stderr:\n{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(!fallback.path().join(".pointbreak").exists());
assert!(!fallback.path().join("relative-home").exists());
}
}
#[test]
fn one_explicit_home_owns_the_reported_user_store_and_key_paths() {
let repo = support::dump_repo();
let repo_path = repo.path().to_str().unwrap();
let home = tempfile::tempdir().unwrap();
let fallback = tempfile::tempdir().unwrap();
let capture = output(
command(&["capture", "--repo", repo_path])
.env("POINTBREAK_HOME", home.path())
.env("HOME", fallback.path()),
);
assert!(
capture.status.success(),
"capture stderr:\n{}",
String::from_utf8_lossy(&capture.stderr)
);
let paths = output(
command(&["store", "paths", "--repo", repo_path])
.env("POINTBREAK_HOME", home.path())
.env("HOME", fallback.path()),
);
assert!(
paths.status.success(),
"paths stderr:\n{}",
String::from_utf8_lossy(&paths.stderr)
);
let document: serde_json::Value = serde_json::from_slice(&paths.stdout).unwrap();
assert_eq!(document["home"], home.path().to_str().unwrap());
assert_eq!(document["keys"], home.path().join("keys").to_str().unwrap());
assert!(!fallback.path().join(".pointbreak").exists());
}