mod support;
use std::fs;
use tempfile::TempDir;
use thoughts_tool::git::sync::GitSync;
#[ignore = "integration test - run with: just test-integration"]
#[tokio::test]
async fn sync_from_worktree_fetches_and_pushes() {
let remote = TempDir::new().unwrap();
support::git_ok(remote.path(), &["init", "--bare", "."]);
let main = TempDir::new().unwrap();
support::git_ok(main.path(), &["init"]);
support::git_ok(
main.path(),
&["remote", "add", "origin", remote.path().to_str().unwrap()],
);
fs::write(main.path().join("x.txt"), "x").unwrap();
support::git_ok(main.path(), &["add", "."]);
support::git_ok(
main.path(),
&[
"-c",
"user.name=Test",
"-c",
"user.email=test@example.com",
"commit",
"-m",
"base",
],
);
support::git_ok(main.path(), &["branch", "-M", "main"]);
support::git_ok(main.path(), &["push", "-u", "origin", "main"]);
let wt_path = main.path().join("wt");
support::git_ok(
main.path(),
&[
"worktree",
"add",
wt_path.to_str().unwrap(),
"-b",
"wt-branch",
"main",
],
);
fs::write(wt_path.join("y.txt"), "y").unwrap();
let sync = GitSync::new(&wt_path, None).unwrap();
sync.sync("test-mount").await.unwrap();
let stdout = support::git_stdout(remote.path(), &["ls-remote", ".", "refs/heads/wt-branch"]);
assert!(stdout.contains("refs/heads/wt-branch"));
}
#[ignore = "integration test - run with: just test-integration"]
#[tokio::test]
async fn sync_stages_and_commits_changes() {
let remote = TempDir::new().unwrap();
support::git_ok(remote.path(), &["init", "--bare", "."]);
let repo = TempDir::new().unwrap();
support::git_ok(repo.path(), &["init"]);
support::git_ok(
repo.path(),
&["remote", "add", "origin", remote.path().to_str().unwrap()],
);
fs::write(repo.path().join("initial.txt"), "initial").unwrap();
support::git_ok(repo.path(), &["add", "."]);
support::git_ok(
repo.path(),
&[
"-c",
"user.name=Test",
"-c",
"user.email=test@example.com",
"commit",
"-m",
"initial",
],
);
support::git_ok(repo.path(), &["branch", "-M", "main"]);
support::git_ok(repo.path(), &["push", "-u", "origin", "main"]);
let initial_count: i32 = support::git_stdout(repo.path(), &["rev-list", "--count", "HEAD"])
.parse()
.unwrap();
fs::write(repo.path().join("new.txt"), "new content").unwrap();
let sync = GitSync::new(repo.path(), None).unwrap();
sync.sync("test-mount").await.unwrap();
let final_count: i32 = support::git_stdout(repo.path(), &["rev-list", "--count", "HEAD"])
.parse()
.unwrap();
assert_eq!(final_count, initial_count + 1);
let files = support::git_stdout(repo.path(), &["show", "--name-only", "--format=", "HEAD"]);
assert!(files.contains("new.txt"));
}
#[ignore = "integration test - run with: just test-integration"]
#[tokio::test]
async fn sync_no_changes_no_commit() {
let remote = TempDir::new().unwrap();
support::git_ok(remote.path(), &["init", "--bare", "."]);
let repo = TempDir::new().unwrap();
support::git_ok(repo.path(), &["init"]);
support::git_ok(
repo.path(),
&["remote", "add", "origin", remote.path().to_str().unwrap()],
);
fs::write(repo.path().join("initial.txt"), "initial").unwrap();
support::git_ok(repo.path(), &["add", "."]);
support::git_ok(
repo.path(),
&[
"-c",
"user.name=Test",
"-c",
"user.email=test@example.com",
"commit",
"-m",
"initial",
],
);
support::git_ok(repo.path(), &["branch", "-M", "main"]);
support::git_ok(repo.path(), &["push", "-u", "origin", "main"]);
let initial_head = support::git_stdout(repo.path(), &["rev-parse", "HEAD"]);
let sync = GitSync::new(repo.path(), None).unwrap();
sync.sync("test-mount").await.unwrap();
let final_head = support::git_stdout(repo.path(), &["rev-parse", "HEAD"]);
assert_eq!(initial_head, final_head);
}