Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/gigit. git client
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221//! Exercises the actor layer without Tauri in the picture.//!//! The commands are thin enough that testing them adds nothing; what is worth//! testing is that the actors hand off correctly, stay alive across requests,//! and clean up when a repository closes.
use gigit_git::testing::Fixture;use gigit_git::{Head, RefKind};use gigit_lib::{RepositoryId, WorkspaceHandle};
/// Start a workspace actor on the test runtime and hand back its handle.fn workspace() -> WorkspaceHandle { let (handle, actor) = WorkspaceHandle::new(); tokio::spawn(actor);
handle}
#[tokio::test]async fn opens_a_repository_and_reports_its_head() { let fixture = Fixture::with_one_commit(); let workspace = workspace();
let opened = workspace .open(fixture.path().to_path_buf()) .await .expect("should open");
assert!(matches!(opened.summary.head, Head::Branch { .. })); assert!(!opened.summary.is_bare);}
#[tokio::test]async fn opening_from_a_subdirectory_finds_the_repository() { let fixture = Fixture::with_one_commit(); let nested = fixture.path().join("deeply").join("nested"); std::fs::create_dir_all(&nested).expect("could not create nested directories"); let workspace = workspace();
let from_root = workspace .open(fixture.path().to_path_buf()) .await .expect("should open from the root"); let from_nested = workspace .open(nested) .await .expect("should open from below");
assert_eq!( from_root.id, from_nested.id, "the same repository reached by two paths is one repository" );}
#[tokio::test]async fn opening_twice_reuses_the_same_actor() { let fixture = Fixture::with_one_commit(); let workspace = workspace(); let path = fixture.path().to_path_buf();
let first = workspace.open(path.clone()).await.expect("should open"); let second = workspace.open(path).await.expect("should open again");
assert_eq!(first.id, second.id);
// Closing once is enough: the second open resolved to the same entry // rather than adding another. assert!(workspace .close(first.id.clone()) .await .expect("should close")); assert!( !workspace.close(second.id).await.expect("should not error"), "the repository should already be gone" );}
#[tokio::test]async fn serves_many_requests_from_one_open_repository() { let fixture = Fixture::with_one_commit(); fixture.branch("feature/graph"); fixture.tag("v1.0.0"); let workspace = workspace();
let opened = workspace .open(fixture.path().to_path_buf()) .await .expect("should open");
// The actor has to stay alive between requests, not open once and exit. for _ in 0..3 { let references = workspace .references(opened.id.clone()) .await .expect("should list references");
assert!(references.iter().any(|entry| entry.kind == RefKind::Tag)); assert!(references .iter() .any(|entry| entry.shorthand == "feature/graph"));
workspace .summary(opened.id.clone()) .await .expect("should summarise"); }}
#[tokio::test]async fn concurrent_requests_are_all_answered() { let fixture = Fixture::with_one_commit(); let workspace = workspace(); let opened = workspace .open(fixture.path().to_path_buf()) .await .expect("should open");
// One repository thread serving requests that arrive together: they queue // rather than contend, and none of them are dropped. let requests: Vec<_> = (0..16) .map(|_| { let workspace = workspace.clone(); let id = opened.id.clone(); tokio::spawn(async move { workspace.references(id).await }) }) .collect();
for request in requests { request .await .expect("the task should not panic") .expect("every request should be answered"); }}
#[tokio::test]async fn requests_for_an_unknown_repository_fail_cleanly() { let workspace = workspace(); let id: RepositoryId = serde_json::from_str("\"/nowhere/.git\"").expect("should deserialise");
let error = workspace .references(id) .await .expect_err("there is no such repository");
assert!( error.to_string().contains("no repository is open"), "unexpected error: {error}" );}
#[tokio::test]async fn opening_something_that_is_not_a_repository_fails() { let directory = tempfile::tempdir().expect("could not create a temporary directory"); let workspace = workspace();
let error = workspace .open(directory.path().to_path_buf()) .await .expect_err("there is no repository here");
assert!( error.to_string().contains("no git repository found"), "unexpected error: {error}" );}
#[tokio::test]async fn a_failed_open_leaves_the_workspace_usable() { let fixture = Fixture::with_one_commit(); let directory = tempfile::tempdir().expect("could not create a temporary directory"); let workspace = workspace();
workspace .open(directory.path().to_path_buf()) .await .expect_err("should fail to open");
// A repository thread that fails to open must not take the workspace // actor down with it. workspace .open(fixture.path().to_path_buf()) .await .expect("the workspace should still work");}
#[tokio::test]async fn closing_frees_the_repository() { let fixture = Fixture::with_one_commit(); let workspace = workspace(); let opened = workspace .open(fixture.path().to_path_buf()) .await .expect("should open");
assert!(workspace .close(opened.id.clone()) .await .expect("should close"));
workspace .references(opened.id) .await .expect_err("a closed repository should not answer");}
#[tokio::test]async fn errors_serialise_with_a_kind_the_ui_can_branch_on() { let workspace = workspace(); let id: RepositoryId = serde_json::from_str("\"/nowhere/.git\"").expect("should deserialise");
let error = workspace.summary(id).await.expect_err("should fail"); let json = serde_json::to_value(&error).expect("should serialise");
assert_eq!(json["kind"], "unknownRepository"); assert!( json["message"].as_str().is_some_and(|m| !m.is_empty()), "the message should be human readable" );}