mod common;
use common::{invoke, invoke_collecting_views, jj_init_with_file, start_test_server};
use rap_client::callback_server::start_callback_channel;
use std::path::Path;
use std::process::Command;
fn jj_cmd(repo: &Path) -> Command {
let mut cmd = Command::new("jj");
cmd.arg("-R").arg(repo).current_dir(repo);
cmd
}
#[tokio::test]
async fn jj_execute_command_preserves_description() {
let _ = tracing_subscriber::fmt::try_init();
let tmp = jj_init_with_file("README.md", "hello\n");
let repo = tmp.path();
let server_url = start_test_server(&repo.join(".test-metadata")).await;
let (callback_url, mut rx) = start_callback_channel()
.await
.expect("start callback channel");
let group_id = "test-thread";
let repo_str = repo.to_str().expect("repo path to str");
let text = invoke(
&server_url,
&callback_url,
group_id,
"clone_repo",
serde_json::json!({ "repo": repo_str }),
&mut rx,
None,
)
.await;
assert!(text.contains("Repository initialized"), "got: {text}");
let text = invoke(
&server_url,
&callback_url,
group_id,
"describe_overall_changes",
serde_json::json!({ "message": "feat: my cool feature" }),
&mut rx,
None,
)
.await;
assert_eq!(text, "Edits described.");
let (text, views) = invoke_collecting_views(
&server_url,
&callback_url,
group_id,
"execute_command",
serde_json::json!({ "command": "echo new-content > new_file.txt" }),
&mut rx,
1,
)
.await;
assert!(text.contains("exit code"), "got: {text}");
assert_eq!(views.len(), 1, "expected exactly one ViewUpdate");
let files = views[0].content["files"]
.as_array()
.expect("files should be an array");
insta::assert_json_snapshot!(files);
let desc = String::from_utf8_lossy(
&jj_cmd(repo)
.args([
"log",
"--no-graph",
"-r",
&format!("sandbox-{group_id}"),
"-T",
"description",
])
.output()
.expect("run jj log")
.stdout,
)
.to_string();
assert!(
desc.contains("feat: my cool feature"),
"expected original description preserved, got: {desc}"
);
assert!(
!desc.contains("echo new-content"),
"description should not contain the command, got: {desc}"
);
}
#[tokio::test]
async fn sandboxed_tmpdir_is_not_inside_sandbox_repo() {
let _ = tracing_subscriber::fmt::try_init();
let tmp = jj_init_with_file("README.md", "hello\n");
let repo = tmp.path();
let server_url = common::start_test_server_sandboxed(&repo.join(".test-metadata"), true).await;
let (callback_url, mut rx) = start_callback_channel()
.await
.expect("start callback channel");
let group_id = "tmpdir-test";
let repo_str = repo.to_str().expect("repo path to str");
let text = invoke(
&server_url,
&callback_url,
group_id,
"clone_repo",
serde_json::json!({ "repo": repo_str }),
&mut rx,
None,
)
.await;
assert!(text.contains("Repository initialized"), "got: {text}");
let text = invoke(
&server_url,
&callback_url,
group_id,
"execute_command",
serde_json::json!({ "command": "echo TMPDIR=$TMPDIR; echo PWD=$PWD" }),
&mut rx,
None,
)
.await;
let tmpdir_val = text
.lines()
.find_map(|l| l.strip_prefix("TMPDIR="))
.expect("TMPDIR line not found in output");
let pwd_val = text
.lines()
.find_map(|l| l.strip_prefix("PWD="))
.expect("PWD line not found in output");
let tmpdir = Path::new(tmpdir_val);
let pwd = Path::new(pwd_val);
let repo_canon = repo.canonicalize().expect("canonicalize repo");
assert!(
!tmpdir.starts_with(&repo_canon) && !tmpdir.starts_with(pwd),
"TMPDIR ({tmpdir_val}) must not be inside the repo ({repo_canon:?}) or sandbox ({pwd_val})"
);
assert!(
!pwd.starts_with(tmpdir),
"Sandbox dir ({pwd_val}) must not be inside TMPDIR ({tmpdir_val})"
);
}
#[tokio::test]
async fn sandboxed_command_can_write_sccache_cache_dir() {
let _ = tracing_subscriber::fmt::try_init();
let sccache_tmp = tempfile::tempdir().expect("create sccache temp dir");
unsafe {
std::env::set_var("SCCACHE_DIR", sccache_tmp.path());
}
let tmp = jj_init_with_file("README.md", "hello\n");
let repo = tmp.path();
let server_url = common::start_test_server_sandboxed(&repo.join(".test-metadata"), true).await;
let (callback_url, mut rx) = start_callback_channel()
.await
.expect("start callback channel");
let group_id = "sccache-test";
let repo_str = repo.to_str().expect("repo path to str");
let text = invoke(
&server_url,
&callback_url,
group_id,
"clone_repo",
serde_json::json!({ "repo": repo_str }),
&mut rx,
None,
)
.await;
assert!(text.contains("Repository initialized"), "got: {text}");
let sccache_dir = sccache_tmp.path().to_str().expect("sccache dir as str");
let cmd = format!("touch {sccache_dir}/sandbox-write-test");
let text = invoke(
&server_url,
&callback_url,
group_id,
"execute_command",
serde_json::json!({ "command": cmd }),
&mut rx,
None,
)
.await;
insta::assert_snapshot!(text, @"Command completed with exit code 0");
unsafe {
std::env::remove_var("SCCACHE_DIR");
}
}