Files
RuneStone/src-tauri/src/git_sync_tests.rs
T
Knotty Wyvern 2448c68abf Move blocking host work off the main thread and fix Git SSH settings
Window close no longer waits on the main thread. The handler revokes
browser sessions and plugin leases with short locks. A cleanup thread
stops terminals, closes browser tabs, and erases plugin review folders.
The browser cleanup sends closeTab without a wait for the reply, and
stops the engine outside the engine lock.

Terminal, plugin, background, and browser commands do their file,
process, and plugin-lock work on blocking workers. Terminal events go
to the window that opened the session. Command names, arguments,
results, and events do not change.

The Git service keeps GIT_SSH, GIT_SSH_COMMAND, GIT_SSH_VARIANT, and a
configured core.sshCommand. It adds its SSH defaults only when the user
has none. It still removes other inherited GIT_* variables.

After the first check, Git status starts 2 processes and an unstaged
diff starts 1. The service finds Git once, checks Git once per run,
caches the repository check per root until the Git metadata changes,
waits on its pipes instead of a 15 ms poll, and locks each root
separately.

Lease errors name the plugin. A profile copy rejects junctions.
2026-10-27 23:08:59 -07:00

182 lines
6.7 KiB
Rust

use super::*;
struct Fixture(PathBuf);
impl Fixture {
fn new() -> Self {
let root =
std::env::temp_dir().join(format!("runestone-git-sync-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&root).unwrap();
let fixture = Self(dunce::canonicalize(root).unwrap());
fixture.git(
".",
&["init", "--bare", "--initial-branch=main", "remote.git"],
);
fixture.git(".", &["clone", "remote.git", "local"]);
fixture.git("local", &["config", "user.name", "RuneStone Test"]);
fixture.git("local", &["config", "user.email", "[email protected]"]);
fixture
}
fn path(&self, directory: &str) -> PathBuf {
self.0.join(directory)
}
fn git(&self, directory: &str, args: &[&str]) -> String {
checked(&self.path(directory), args).unwrap()
}
fn commit(&self, directory: &str, name: &str, contents: &str) {
std::fs::write(self.path(directory).join(name), contents).unwrap();
self.git(directory, &["add", "--", name]);
self.git(
directory,
&[
"-c",
"user.name=RuneStone Test",
"-c",
"[email protected]",
"commit",
"-m",
contents,
],
);
}
fn seed(&self) {
self.commit("local", "first.txt", "Initial commit");
synchronize(&self.path("local"), true).unwrap();
self.git(".", &["clone", "remote.git", "peer"]);
}
}
impl Drop for Fixture {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[test]
fn push_and_fast_forward_pull_use_only_the_upstream_branch() {
let fixture = Fixture::new();
fixture.seed();
fixture.git("local", &["branch", "unpublished"]);
fixture.git("local", &["tag", "unpublished-tag"]);
fixture.git("local", &["config", "push.default", "matching"]);
fixture.git("local", &["config", "remote.origin.mirror", "true"]);
fixture.commit("local", "second.txt", "Local change");
synchronize(&fixture.path("local"), true).unwrap();
let refs = fixture.git("remote.git", &["for-each-ref", "--format=%(refname)"]);
assert_eq!(refs.trim(), "refs/heads/main");
fixture.git("peer", &["pull", "--ff-only"]);
fixture.commit("peer", "peer.txt", "Peer change");
fixture.git("peer", &["push"]);
fixture.git("local", &["config", "pull.rebase", "true"]);
synchronize(&fixture.path("local"), false).unwrap();
assert_eq!(
fixture.git("local", &["rev-parse", "HEAD"]),
fixture.git("peer", &["rev-parse", "HEAD"])
);
assert_eq!(
std::fs::read_to_string(fixture.path("local").join("peer.txt")).unwrap(),
"Peer change"
);
}
#[test]
fn divergent_push_and_pull_preserve_local_and_remote_commits() {
let fixture = Fixture::new();
fixture.seed();
fixture.commit("peer", "peer.txt", "Peer change");
fixture.git("peer", &["push"]);
fixture.commit("local", "local.txt", "Local change");
fixture.git(
"local",
&[
"config",
"remote.origin.push",
"+refs/heads/main:refs/heads/main",
],
);
fixture.git("local", &["config", "pull.rebase", "true"]);
fixture.git("local", &["config", "pull.ff", "false"]);
let local = fixture.git("local", &["rev-parse", "HEAD"]);
let remote = fixture.git("remote.git", &["rev-parse", "HEAD"]);
assert!(synchronize(&fixture.path("local"), true).is_err());
assert!(synchronize(&fixture.path("local"), false).is_err());
assert_eq!(fixture.git("local", &["rev-parse", "HEAD"]), local);
assert_eq!(fixture.git("remote.git", &["rev-parse", "HEAD"]), remote);
assert!(!fixture.path("local/.git/MERGE_HEAD").exists());
assert!(!fixture.path("local/.git/rebase-merge").exists());
}
#[test]
fn dirty_missing_upstream_detached_and_remote_errors_are_clear() {
let fixture = Fixture::new();
fixture.seed();
std::fs::write(fixture.path("local/first.txt"), "Unsaved change").unwrap();
fixture.git("local", &["config", "merge.autoStash", "true"]);
assert!(synchronize(&fixture.path("local"), false)
.unwrap_err()
.to_string()
.contains("clean working tree"));
assert_eq!(
std::fs::read_to_string(fixture.path("local/first.txt")).unwrap(),
"Unsaved change"
);
fixture.git("local", &["branch", "--unset-upstream"]);
assert!(synchronize(&fixture.path("local"), true)
.unwrap_err()
.to_string()
.contains("no remote upstream"));
fixture.git("local", &["checkout", "--detach"]);
assert!(synchronize(&fixture.path("local"), true)
.unwrap_err()
.to_string()
.contains("Select a branch"));
fixture.git("local", &["checkout", "main"]);
fixture.git("local", &["branch", "--set-upstream-to=origin/main"]);
fixture.git(
"local",
&["remote", "set-url", "origin", "../missing-remote.git"],
);
assert!(synchronize(&fixture.path("local"), true)
.unwrap_err()
.to_string()
.contains("Push failed"));
}
#[test]
fn graph_shows_branch_forks_merges_tags_and_detached_history() {
let fixture = Fixture::new();
assert!(graph(&fixture.path("local")).unwrap().commits.is_empty());
fixture.seed();
fixture.git("local", &["checkout", "-b", "feature"]);
fixture.commit("local", "feature.txt", "Feature commit");
fixture.git("local", &["checkout", "main"]);
fixture.commit("local", "main.txt", "Main commit");
fixture.git(
"local",
&["merge", "--no-ff", "feature", "-m", "Merge feature"],
);
fixture.git("local", &["tag", "test-tag"]);
let history = graph(&fixture.path("local")).unwrap();
for expected in [
"HEAD -> main",
"feature",
"test-tag",
"Merge feature",
"RuneStone Test",
] {
assert!(format!("{history:?}").contains(expected), "Missing {expected}: {history:?}");
}
let merge = history.commits.iter().find(|commit| commit.summary == "Merge feature").unwrap();
assert_eq!(merge.parents.len(), 2);
let details = commit_details(&fixture.path("local"), &merge.id).unwrap();
assert_eq!(details.files.len(), 1);
assert_eq!(details.files[0].path, "feature.txt");
assert!(commit_diff(&fixture.path("local"), &merge.id, "feature.txt").unwrap().contains("+Feature commit"));
for parent in &merge.parents {
assert!(history.commits.iter().any(|commit| &commit.id == parent));
}
fixture.git("local", &["checkout", "--detach"]);
fixture.commit("local", "detached.txt", "Detached commit");
assert!(graph(&fixture.path("local"))
.unwrap()
.commits.iter().any(|commit| commit.summary == "Detached commit"));
}