use std::time::{Duration, Instant};
use gitway_lib::{GitwayConfig, GitwaySession, hostkey};
fn integration_enabled() -> bool {
std::env::var("GITWAY_INTEGRATION_TESTS")
.map(|v| !v.is_empty())
.unwrap_or(false)
}
#[tokio::test]
async fn connect_to_github_verifies_host_key() {
if !integration_enabled() {
return;
}
let config = GitwayConfig::github();
let session = GitwaySession::connect(&config)
.await
.expect("connection and host-key verification must succeed");
session
.close()
.await
.expect("graceful disconnect must succeed");
}
#[tokio::test]
async fn host_key_mismatch_is_rejected() {
if !integration_enabled() {
return;
}
let fake_fp = "SHA256:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
let reachability_config = GitwayConfig::builder("github.com")
.skip_host_check(true)
.build();
let _s = GitwaySession::connect(&reachability_config)
.await
.expect("server must be reachable");
let tmp = tempfile::NamedTempFile::new().expect("temp file");
std::fs::write(tmp.path(), format!("github.com {fake_fp}\n"))
.expect("write temp known_hosts");
let config = GitwayConfig::builder("github.com")
.custom_known_hosts(tmp.path())
.build();
let result = GitwaySession::connect(&config).await;
assert!(result.is_err());
let err = result.unwrap_err();
assert!(
err.is_host_key_mismatch(),
"expected host-key-mismatch error, got: {err}"
);
_ = hostkey::DEFAULT_GITHUB_HOST; }
#[tokio::test]
async fn insecure_skip_host_check_bypasses_verification() {
if !integration_enabled() {
return;
}
let config = GitwayConfig::builder("github.com")
.skip_host_check(true)
.build();
let session = GitwaySession::connect(&config)
.await
.expect("connection must succeed with skip_host_check");
session.close().await.expect("disconnect must succeed");
}
#[tokio::test]
async fn cold_start_handshake_is_fast() {
const HARD_LIMIT: Duration = Duration::from_secs(10);
if !integration_enabled() {
return;
}
let config = GitwayConfig::github();
let t0 = Instant::now();
let session = GitwaySession::connect(&config)
.await
.expect("connection must succeed for timing test");
let elapsed = t0.elapsed();
session.close().await.expect("disconnect must succeed");
eprintln!(
"cold-start handshake: {:.0} ms (NFR-1 target: ≤ 2000 ms)",
elapsed.as_secs_f64() * 1000.0
);
assert!(
elapsed <= HARD_LIMIT,
"cold-start took {elapsed:?}, exceeding the {HARD_LIMIT:?} hard limit"
);
}