use gouqi::core::{ClientCore, JiraDeploymentType, SearchApiVersion};
use gouqi::{Credentials, Jira};
#[test]
fn test_cloud_deployment_detection() {
let core = ClientCore::new("https://gouji.atlassian.net", Credentials::Anonymous)
.expect("Failed to create ClientCore");
let deployment_type = core.detect_deployment_type();
assert_eq!(
deployment_type,
JiraDeploymentType::Cloud,
"Should detect gouji.atlassian.net as Cloud deployment"
);
}
#[test]
fn test_cloud_auto_selects_v3_api() {
let core = ClientCore::new("https://gouji.atlassian.net", Credentials::Anonymous)
.expect("Failed to create ClientCore");
let selected_version = core.get_search_api_version();
assert_eq!(
selected_version,
SearchApiVersion::V3,
"Should auto-select V3 API for Cloud deployment"
);
}
#[test]
fn test_versioned_url_construction_v3() {
let core = ClientCore::new("https://gouji.atlassian.net", Credentials::Anonymous)
.expect("Failed to create ClientCore");
let url = core
.build_versioned_url("api", Some("3"), "/search/jql?jql=project=TEST")
.expect("Failed to build V3 URL");
let expected = "https://gouji.atlassian.net/rest/api/3/search/jql?jql=project=TEST";
assert_eq!(
url.as_str(),
expected,
"Should construct correct V3 search URL"
);
}
#[test]
fn test_versioned_url_construction_v2() {
let core = ClientCore::new("https://gouji.atlassian.net", Credentials::Anonymous)
.expect("Failed to create ClientCore");
let url = core
.build_versioned_url("api", Some("latest"), "/search?jql=project=TEST")
.expect("Failed to build V2 URL");
let expected = "https://gouji.atlassian.net/rest/api/latest/search?jql=project=TEST";
assert_eq!(
url.as_str(),
expected,
"Should construct correct V2 search URL"
);
}
#[test]
fn test_search_endpoint_selection() {
let jira = Jira::new("https://gouji.atlassian.net", Credentials::Anonymous)
.expect("Failed to create Jira client");
let search = jira.search();
let (api_name, endpoint, version) = search.get_search_endpoint();
assert_eq!(api_name, "api");
assert_eq!(endpoint, "/search/jql");
assert_eq!(version, Some("3"));
}
#[test]
fn test_manual_v3_selection() {
let jira = Jira::with_search_api_version(
"https://gouji.atlassian.net",
Credentials::Anonymous,
SearchApiVersion::V3,
)
.expect("Failed to create Jira client with V3");
assert_eq!(jira.get_search_api_version(), SearchApiVersion::V3);
}
#[test]
fn test_manual_v2_override_for_cloud() {
let jira = Jira::with_search_api_version(
"https://gouji.atlassian.net",
Credentials::Anonymous,
SearchApiVersion::V2,
)
.expect("Failed to create Jira client with V2 override");
assert_eq!(jira.get_search_api_version(), SearchApiVersion::V2);
let search = jira.search();
let (api_name, endpoint, version) = search.get_search_endpoint();
assert_eq!(api_name, "api");
assert_eq!(endpoint, "/search");
assert_eq!(version, Some("latest"));
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_async_cloud_api_selection() {
use gouqi::r#async::Jira as AsyncJira;
let jira = AsyncJira::new("https://gouji.atlassian.net", Credentials::Anonymous)
.expect("Failed to create async Jira client");
assert_eq!(jira.get_search_api_version(), SearchApiVersion::V3);
}
#[test]
fn test_real_jira_cloud_v3_search() {
let token = match std::env::var("INTEGRATION_JIRA_TOKEN") {
Ok(token) => token,
Err(_) => {
eprintln!("โ ๏ธ INTEGRATION_JIRA_TOKEN not set, skipping real API test");
return;
}
};
if token.trim().is_empty() {
eprintln!("โ ๏ธ INTEGRATION_JIRA_TOKEN is empty, skipping real API test");
return;
}
println!("๐งช Testing real Jira Cloud V3 search API...");
let credentials = Credentials::Bearer(token);
let jira = Jira::new("https://gouji.atlassian.net", credentials.clone())
.expect("Failed to create Jira client");
assert_eq!(
jira.get_search_api_version(),
SearchApiVersion::V3,
"Should auto-select V3 for Cloud"
);
let search = jira.search();
let (api_name, endpoint, version) = search.get_search_endpoint();
assert_eq!(api_name, "api");
assert_eq!(endpoint, "/search/jql"); assert_eq!(version, Some("3"));
println!("โ
V3 API auto-detection working correctly");
let search_options = gouqi::SearchOptions::builder().max_results(1).build();
println!("๐ Testing V3 search with simple query...");
match search.list("", &search_options) {
Ok(results) => {
println!(
"โ
V3 search successful! Found {} total issues",
results.total
);
println!(" ๐ Search used endpoint: /rest/api/3/search/jql");
if !results.issues.is_empty() {
println!(" ๐ฏ Sample issue key: {}", results.issues[0].key);
} else {
println!(" ๐ No issues found (empty instance or no permissions)");
}
}
Err(e) => {
println!("โ ๏ธ V3 search with empty query failed: {:?}", e);
println!("๐ Trying alternative query for empty instances...");
match search.list("order by key", &search_options) {
Ok(results) => {
println!(
"โ
V3 search with 'order by key' successful! Found {} total issues",
results.total
);
}
Err(e2) => {
println!("โ ๏ธ Alternative query also failed: {:?}", e2);
println!(
" ๐ This indicates the V3 endpoint is accessible but might return different format for empty instances"
);
}
}
}
}
}
#[test]
fn test_explicit_api_version_selection() {
let token = match std::env::var("INTEGRATION_JIRA_TOKEN") {
Ok(token) => token,
Err(_) => {
eprintln!("โ ๏ธ INTEGRATION_JIRA_TOKEN not set, skipping API version test");
return;
}
};
if token.trim().is_empty() {
eprintln!("โ ๏ธ INTEGRATION_JIRA_TOKEN is empty, skipping API version test");
return;
}
let credentials = Credentials::Bearer(token);
println!("๐งช Testing explicit V3 selection...");
let jira_v3 = Jira::with_search_api_version(
"https://gouji.atlassian.net",
credentials.clone(),
SearchApiVersion::V3,
)
.expect("Failed to create V3 Jira client");
assert_eq!(jira_v3.get_search_api_version(), SearchApiVersion::V3);
let search_v3 = jira_v3.search();
let (_api_name, endpoint, version) = search_v3.get_search_endpoint();
assert_eq!(
endpoint, "/search/jql",
"V3 should use /search/jql endpoint"
);
assert_eq!(version, Some("3"), "V3 should use version '3'");
println!("โ
Explicit V3 selection working correctly");
println!("๐งช Testing V2 fallback override...");
let jira_v2 = Jira::with_search_api_version(
"https://gouji.atlassian.net",
credentials,
SearchApiVersion::V2,
)
.expect("Failed to create V2 Jira client");
assert_eq!(jira_v2.get_search_api_version(), SearchApiVersion::V2);
let search_v2 = jira_v2.search();
let (_api_name, endpoint, version) = search_v2.get_search_endpoint();
assert_eq!(endpoint, "/search", "V2 should use /search endpoint");
assert_eq!(version, Some("latest"), "V2 should use 'latest' version");
println!("โ
V2 override selection working correctly");
println!("๐ Note: V2 might return deprecation warnings from Jira Cloud");
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_real_async_jira_cloud_v3_search() {
use gouqi::r#async::Jira as AsyncJira;
let token = match std::env::var("INTEGRATION_JIRA_TOKEN") {
Ok(token) => token,
Err(_) => {
eprintln!("โ ๏ธ INTEGRATION_JIRA_TOKEN not set, skipping async API test");
return;
}
};
if token.trim().is_empty() {
eprintln!("โ ๏ธ INTEGRATION_JIRA_TOKEN is empty, skipping async API test");
return;
}
println!("๐งช Testing real async Jira Cloud V3 search API...");
let credentials = Credentials::Bearer(token);
let jira = AsyncJira::new("https://gouji.atlassian.net", credentials)
.expect("Failed to create async Jira client");
assert_eq!(jira.get_search_api_version(), SearchApiVersion::V3);
let search = jira.search();
let search_options = gouqi::SearchOptions::builder().max_results(1).build();
match search.list("ORDER BY created DESC", &search_options).await {
Ok(results) => {
println!(
"โ
Async V3 search successful! Found {} total issues",
results.total
);
}
Err(e) => {
println!(
"โ ๏ธ Async V3 search returned error (might be permissions): {:?}",
e
);
}
}
}
#[test]
fn test_non_cloud_deployment_detection() {
let core = ClientCore::new("https://jira.company.com", Credentials::Anonymous)
.expect("Failed to create ClientCore for on-premise");
let deployment_type = core.detect_deployment_type();
assert_eq!(
deployment_type,
JiraDeploymentType::Unknown,
"Should detect on-premise as Unknown deployment"
);
let selected_version = core.get_search_api_version();
assert_eq!(
selected_version,
SearchApiVersion::V2,
"Should auto-select V2 API for on-premise deployment"
);
}