jb_space 0.2.0

Jetbrains Space API wrapper
Documentation
use mockito::{Matcher, Server};
use space::{Client, CodeDiscussionAnchor, CreateCodeDiscussionQuery};
use utils::load_fixture;

mod utils;

#[tokio::test]
async fn test_create_code_discussion_success() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock(
            "POST",
            "/api/http/projects/id:proj123/code-reviews/code-discussions",
        )
        .match_header("Authorization", "Bearer test_token")
        .match_header("Accept", "application/json")
        .match_header("Content-Type", "application/json")
        .match_body(Matcher::JsonString(
            serde_json::json!({
                "text": "Please fix this",
                "repository": "test-repo",
                "anchor": {
                    "revision": "main",
                    "filename": ""
                },
                "pending": false,
                "reviewId": "number:42"
            })
            .to_string(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(load_fixture("code_discussion_positive_response.json").to_string())
        .create_async()
        .await;

    let client = Client::new(&server.url(), "test_token", None);
    let query =
        CreateCodeDiscussionQuery::new("proj123", "test-repo", 42, "Please fix this", "main");

    let result = client.create_code_discussion(query).await;

    mock.assert();
    assert!(result.is_ok());

    let discussion = result.unwrap();

    assert_eq!(discussion.id, "0198ad99-11aa-7bc3-9f77-f3b58ce4c111");
    assert_eq!(discussion.project_id, "proj123");
    assert_eq!(discussion.anchor.revision, "main");
    assert_eq!(discussion.anchor.filename, Some("src/main.rs".to_string()));
    assert_eq!(discussion.anchor.line, Some(10));
    assert_eq!(discussion.anchor.old_line, None);
    assert!(discussion.end_anchor.is_none());
    assert!(discussion.resolvable);
    assert!(!discussion.resolved);
    assert!(!discussion.pending);
    assert!(!discussion.archived);
    assert_eq!(
        discussion.created_at,
        chrono::DateTime::parse_from_rfc3339("2026-08-31T11:07:49.040Z")
            .unwrap()
            .with_timezone(&chrono::Utc)
    );
}

#[tokio::test]
async fn test_create_code_discussion_with_all_fields() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock(
            "POST",
            "/api/http/projects/id:proj123/code-reviews/code-discussions",
        )
        .match_header("Authorization", "Bearer test_token")
        .match_header("Accept", "application/json")
        .match_header("Content-Type", "application/json")
        .match_body(Matcher::JsonString(
            serde_json::json!({
                "text": "Multi line comment",
                "repository": "test-repo",
                "anchor": {
                    "revision": "feature",
                    "filename": "src/lib.rs",
                    "line": 10,
                    "oldLine": 8
                },
                "endAnchor": {
                    "revision": "feature",
                    "filename": "src/lib.rs",
                    "line": 12
                },
                "pending": true,
                "reviewId": "number:7"
            })
            .to_string(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(load_fixture("code_discussion_positive_response.json").to_string())
        .create_async()
        .await;

    let client = Client::new(&server.url(), "test_token", None);
    let mut query =
        CreateCodeDiscussionQuery::new("proj123", "test-repo", 7, "Multi line comment", "feature");
    query.pending = true;
    query.anchor.filename = Some("src/lib.rs".to_string());
    query.anchor.line = Some(10);
    query.anchor.old_line = Some(8);
    query.end_anchor = Some(CodeDiscussionAnchor {
        revision: "feature".to_string(),
        filename: Some("src/lib.rs".to_string()),
        line: Some(12),
        old_line: None,
    });

    let result = client.create_code_discussion(query).await;

    mock.assert();
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_create_code_discussion_not_found() {
    let mut server = Server::new_async().await;

    let mock = server
        .mock(
            "POST",
            "/api/http/projects/id:proj123/code-reviews/code-discussions",
        )
        .match_header("Authorization", "Bearer test_token")
        .match_header("Accept", "application/json")
        .match_body(Matcher::Any)
        .with_status(404)
        .with_header("content-type", "application/json")
        .with_body(r#"{"error": "Review not found"}"#)
        .create_async()
        .await;

    let client = Client::new(&server.url(), "test_token", None);
    let query = CreateCodeDiscussionQuery::new("proj123", "test-repo", 999, "Text", "main");

    let result = client.create_code_discussion(query).await;

    mock.assert();

    match result {
        Err(space::Error::HttpCodeIsNotSuccessful { status, body }) => {
            assert_eq!(status.as_u16(), 404);
            assert_eq!(body, r#"{"error": "Review not found"}"#);
        }
        other => panic!("Expected HttpCodeIsNotSuccessful, got: {other:?}"),
    }
}

#[tokio::test]
async fn test_create_code_discussion_network_error() {
    let client = Client::new("http://127.0.0.1:12345", "test_token", None);
    let query = CreateCodeDiscussionQuery::new("proj123", "test-repo", 42, "Text", "main");

    let result = client.create_code_discussion(query).await;

    assert!(result.is_err());
}