use crate::source::Stop;
use crate::teleport::{Mode, PathArg, classify_arg, parse, platform, quote, run_with_bin};
use std::fs;
use std::os::unix::fs::PermissionsExt;
fn strs(items: &[&str]) -> Vec<String> {
items.iter().map(|s| (*s).to_string()).collect()
}
#[test]
fn maps_uname() {
let arm = platform("Linux aarch64\n");
assert_eq!(arm, Some(("aarch64-linux", "tesserax-linux-arm64")));
assert_eq!(platform("Linux riscv64"), None);
assert_eq!(quote("a'b"), r"'a'\''b'");
}
#[test]
fn parses_modes() {
let copy = parse(&strs(&["--from", "dist", "a@h", "local"])).unwrap();
assert_eq!(copy.mode, Mode::Copy(strs(&["a@h", "local"])));
let run = parse(&strs(&[
"--host", "a@h", "--recipe", "flux2", "serve", "--host", "0.0.0.0",
]))
.unwrap();
assert_eq!(
run.mode,
Mode::Run {
host: "a@h".into(),
command: strs(&["--recipe", "flux2", "serve", "--host", "0.0.0.0"]),
}
);
let sep = parse(&strs(&[
"--host", "a@h", "--from", "d", "--", "--from", "x",
]))
.unwrap();
assert_eq!(sep.from.as_deref(), Some(std::path::Path::new("d")));
assert_eq!(
sep.mode,
Mode::Run {
host: "a@h".into(),
command: strs(&["--from", "x"]),
}
);
assert!(parse(&strs(&["--host", "a@h"])).is_err());
assert!(parse(&strs(&["a@h", "--host", "b@h", "models"])).is_err());
}
#[test]
fn classifies_paths() {
let dir = tessera_testkit::scratch("teleport-classify");
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
let file = dir.join("w.safetensors");
fs::write(&file, b"ok").unwrap();
let fstr = file.to_string_lossy();
assert_eq!(
classify_arg("remote:/data/m").unwrap(),
PathArg::Keep("/data/m".into())
);
assert_eq!(
classify_arg("--model=remote:/data/m").unwrap(),
PathArg::Keep("--model=/data/m".into())
);
assert_eq!(
classify_arg("hf:org/repo").unwrap(),
PathArg::Keep("hf:org/repo".into())
);
assert_eq!(
classify_arg(&format!("local:{fstr}:0.8")).unwrap(),
PathArg::Local {
prefix: String::new(),
path: file.clone(),
suffix: ":0.8".into(),
}
);
assert_eq!(
classify_arg(&format!("--lora=local:{fstr}:0.5")).unwrap(),
PathArg::Local {
prefix: "--lora=".into(),
path: file,
suffix: ":0.5".into(),
}
);
let out = dir.join("new.png");
assert_eq!(
classify_arg(&format!("local:{}", out.display())).unwrap(),
PathArg::Local {
prefix: String::new(),
path: out,
suffix: String::new(),
}
);
assert!(classify_arg("local:").is_err());
assert!(classify_arg("remote:").is_err());
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn exec_syncs_paths() {
let dir = tessera_testkit::scratch("teleport-exec");
let _ = fs::remove_dir_all(&dir);
let src_dir = dir.join("bundle");
fs::create_dir_all(src_dir.join("sub")).unwrap();
let input = dir.join("in.bin");
fs::write(&input, b"hello").unwrap();
fs::write(src_dir.join("sub/a.txt"), b"nested").unwrap();
let out = dir.join("out/result.txt");
let root = dir.join("root");
let bin = dir.join("mock.sh");
fs::write(
&bin,
"#!/bin/sh\nset -e\nif [ \"$1\" = fail ]; then exit 7; fi\n\
f=\"${1%:0.8}\"; suf=\"${1#$f}\"\n\
printf '%s|%s|%s|%s|%s' \"$(cat \"$f\")\" \"$suf\" \"$(cat \"$2/sub/a.txt\")\" \"$4\" \"$5\" > \"$3\"\n",
)
.unwrap();
fs::set_permissions(&bin, fs::Permissions::from_mode(0o755)).unwrap();
let cmd = vec![
format!("local:{}:0.8", input.display()),
format!("local:{}", src_dir.display()),
format!("local:{}", out.display()),
"remote:/r/path".into(),
"hf:org/model".into(),
];
let rstr = root.to_string_lossy();
let bstr = bin.to_string_lossy();
run_with_bin("local", &rstr, &bstr, &cmd).unwrap();
assert_eq!(
fs::read_to_string(&out).unwrap(),
"hello|:0.8|nested|/r/path|hf:org/model"
);
fs::write(&input, b"again").unwrap();
run_with_bin("local", &rstr, &bstr, &cmd).unwrap();
assert_eq!(
fs::read_to_string(&out).unwrap(),
"again|:0.8|nested|/r/path|hf:org/model"
);
let fail = run_with_bin("local", &rstr, &bstr, &strs(&["fail"]));
assert!(matches!(fail, Err(Stop::Code(7))));
let _ = fs::remove_dir_all(&dir);
}