test(bare): add comprehensive tests for GitBare functionality

- Add test_from_header_valid to verify valid repository header parsing
- Add test_from_header_empty_path to handle empty path scenarios
- Add test_from_header_relative_storage_path to validate absolute paths
- Add test_from_header_relative_path_without_storage for missing storage
- Add test_from_header_nonexistent_repo to check repo existence
- Add test_from_header_path_traversal to prevent directory traversal
- Add test_from_header_not_a_directory for file instead of directory
- Add test_from_header_dir_without_head to verify bare repository format
- Add test_object_format to validate object format detection
- Add test_oid_to_pb to verify OID conversion functionality
- Add test_oid_to_pb_invalid_hex to handle invalid hex input gracefully

test(error): add comprehensive error handling tests

- Add test_error_display_variants to verify error message formatting
- Add test_error_is_debug to
This commit is contained in:
zhenyi
2026-06-04 15:33:33 +08:00
parent cc202d6d1f
commit 7631e57f69
5 changed files with 515 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
mod common;
fn hdr() -> gitks::pb::RepositoryHeader {
gitks::pb::RepositoryHeader {
relative_path: "test-repo".into(),
..Default::default()
}
}
#[tokio::test]
async fn test_list_refs_via_service() {
let (dir, _gb) = common::setup_bare_repo();
let svc = common::setup_service(dir.path());
// list_refs is called internally by advertise_refs
// We test it indirectly through the service
use gitks::pb::pack_service_server::PackService;
let result = svc
.advertise_refs(tonic::Request::new(gitks::pb::AdvertiseRefsRequest {
repository: Some(hdr()),
protocol: None,
service: String::new(),
}))
.await
.unwrap()
.into_inner();
assert!(!result.references.is_empty(), "should have refs");
let names: Vec<&str> = result.references.iter().map(|r| r.name.as_str()).collect();
assert!(
names.iter().any(|n| n.contains("refs/heads/main")),
"should have main branch, got: {:?}",
names
);
assert!(
names.iter().any(|n| n.contains("refs/heads/feature")),
"should have feature branch, got: {:?}",
names
);
assert!(
names.iter().any(|n| n.contains("refs/tags/v0.1.0")),
"should have tag, got: {:?}",
names
);
}
#[test]
fn test_list_refs_direct() {
let (_dir, gb) = common::setup_bare_repo();
let refs = gb.list_refs().expect("list_refs");
assert!(!refs.is_empty());
let names: Vec<&str> = refs.iter().map(|r| r.name.as_str()).collect();
assert!(names.iter().any(|n| n.contains("refs/heads/main")));
assert!(names.iter().any(|n| n.contains("refs/heads/feature")));
assert!(names.iter().any(|n| n.contains("refs/tags/v0.1.0")));
// Each ref should have a valid target OID
for r in &refs {
let oid = r.target_oid.as_ref().expect("ref should have target_oid");
assert!(!oid.hex.is_empty(), "OID hex should not be empty");
assert_eq!(oid.hex.len(), 40, "SHA-1 hex should be 40 chars");
}
}