azure-devops 0.1.0

Idiomatic Rust client for Azure DevOps REST APIs
Documentation
//! Shared test utilities: seed data, mock server helpers.
//! Only compiled during `cargo test`.

use crate::auth::Credentials;
use crate::AzureDevOps;
use httpmock::MockServer;
use url::Url;

/// Create an `AzureDevOps` client pointed at a mock server.
pub fn mock_client(server: &MockServer) -> AzureDevOps {
    let base_url = Url::parse(&format!("{}/", server.base_url())).unwrap();
    let credentials = Credentials::from_pat("test-pat");
    let http = reqwest::Client::builder().build().unwrap();
    AzureDevOps::new(http, base_url, credentials, "7.1".into())
}

// ── Seed JSON payloads ──

pub mod seed {
    use serde_json::json;

    pub fn project_list() -> serde_json::Value {
        json!({
            "count": 2,
            "value": [
                {
                    "id": "e8f1a3b2-4c5d-6e7f-8a9b-0c1d2e3f4a5b",
                    "name": "Veles",
                    "description": "Main project",
                    "url": "https://dev.azure.com/ruslan-rystsov/_apis/projects/e8f1a3b2",
                    "state": "wellFormed",
                    "revision": 42,
                    "visibility": "private",
                    "lastUpdateTime": "2024-01-15T10:30:00Z"
                },
                {
                    "id": "f9a2b3c4-5d6e-7f8a-9b0c-1d2e3f4a5b6c",
                    "name": "Secondary",
                    "description": null,
                    "url": "https://dev.azure.com/ruslan-rystsov/_apis/projects/f9a2b3c4",
                    "state": "wellFormed",
                    "revision": 7,
                    "visibility": "private",
                    "lastUpdateTime": "2023-11-01T08:00:00Z"
                }
            ]
        })
    }

    pub fn single_project() -> serde_json::Value {
        json!({
            "id": "e8f1a3b2-4c5d-6e7f-8a9b-0c1d2e3f4a5b",
            "name": "Veles",
            "description": "Main project",
            "url": "https://dev.azure.com/ruslan-rystsov/_apis/projects/e8f1a3b2",
            "state": "wellFormed",
            "revision": 42,
            "visibility": "private",
            "lastUpdateTime": "2024-01-15T10:30:00Z"
        })
    }

    pub fn work_item() -> serde_json::Value {
        json!({
            "id": 123,
            "rev": 3,
            "url": "https://dev.azure.com/ruslan-rystsov/_apis/wit/workItems/123",
            "fields": {
                "System.Id": 123,
                "System.Title": "Test Bug",
                "System.State": "Active",
                "System.WorkItemType": "Bug",
                "System.AssignedTo": {
                    "displayName": "Ruslan Rystsov",
                    "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
                }
            },
            "relations": [
                {
                    "rel": "System.LinkTypes.Hierarchy-Reverse",
                    "url": "https://dev.azure.com/ruslan-rystsov/_apis/wit/workItems/100",
                    "attributes": { "isLocked": false }
                }
            ]
        })
    }

    pub fn work_items_list() -> serde_json::Value {
        json!([{
            "id": 123,
            "rev": 3,
            "url": "https://dev.azure.com/ruslan-rystsov/_apis/wit/workItems/123",
            "fields": { "System.Title": "Test Bug", "System.State": "Active" }
        }, {
            "id": 124,
            "rev": 1,
            "url": "https://dev.azure.com/ruslan-rystsov/_apis/wit/workItems/124",
            "fields": { "System.Title": "Feature Request", "System.State": "New" }
        }])
    }

    pub fn wiql_result() -> serde_json::Value {
        json!({
            "asOf": "2024-01-15T10:30:00Z",
            "queryResultType": "workItem",
            "queryType": "flat",
            "columns": [
                { "referenceName": "System.Id", "name": "ID", "url": null },
                { "referenceName": "System.Title", "name": "Title", "url": null }
            ],
            "workItems": [
                { "id": 123, "url": "https://dev.azure.com/ruslan-rystsov/_apis/wit/workItems/123" },
                { "id": 124, "url": "https://dev.azure.com/ruslan-rystsov/_apis/wit/workItems/124" }
            ]
        })
    }

    pub fn repositories_list() -> serde_json::Value {
        json!([{
            "id": "repo-guid-1",
            "name": "Veles",
            "url": "https://dev.azure.com/ruslan-rystsov/_apis/git/repositories/repo-guid-1",
            "webUrl": "https://dev.azure.com/ruslan-rystsov/Veles/_git/Veles",
            "remoteUrl": "https://ruslan-rystsov@dev.azure.com/ruslan-rystsov/Veles/_git/Veles",
            "sshUrl": "git@ssh.dev.azure.com:v3/ruslan-rystsov/Veles/Veles",
            "defaultBranch": "refs/heads/main",
            "size": 12345678,
            "isDisabled": false,
            "isFork": false,
            "project": {
                "id": "e8f1a3b2-4c5d-6e7f-8a9b-0c1d2e3f4a5b",
                "name": "Veles",
                "url": "https://dev.azure.com/ruslan-rystsov/_apis/projects/e8f1a3b2",
                "state": "wellFormed",
                "revision": 42,
                "visibility": "private"
            }
        }])
    }

    pub fn pull_requests_list() -> serde_json::Value {
        json!([{
            "pullRequestId": 1,
            "codeReviewId": 1,
            "title": "Add feature X",
            "description": "This PR adds feature X",
            "status": "active",
            "mergeStatus": "succeeded",
            "sourceRefName": "refs/heads/feature/x",
            "targetRefName": "refs/heads/main",
            "creationDate": "2024-01-15T10:30:00Z",
            "createdBy": { "displayName": "Ruslan Rystsov", "id": "a1b2c3d4", "uniqueName": "ruslan@example.com" },
            "isDraft": false,
            "reviewers": [
                { "displayName": "Reviewer One", "id": "r1", "vote": 10, "hasDeclined": false, "isRequired": true }
            ],
            "repository": {
                "id": "repo-guid-1",
                "name": "Veles",
                "url": "https://dev.azure.com/ruslan-rystsov/_apis/git/repositories/repo-guid-1"
            }
        }])
    }

    pub fn builds_list() -> serde_json::Value {
        json!([{
            "id": 456,
            "buildNumber": "20240115.1",
            "status": "completed",
            "result": "succeeded",
            "reason": "manual",
            "queueTime": "2024-01-15T10:00:00Z",
            "startTime": "2024-01-15T10:01:00Z",
            "finishTime": "2024-01-15T10:05:00Z",
            "definition": { "id": 10, "name": "CI Pipeline", "path": "\\" },
            "project": { "id": "e8f1a3b2", "name": "Veles" },
            "sourceBranch": "refs/heads/main",
            "requestedBy": { "displayName": "Ruslan Rystsov", "id": "a1b2c3d4" },
            "tags": ["release"]
        }])
    }

    pub fn build_timeline() -> serde_json::Value {
        json!({
            "id": "timeline-1",
            "changeId": 1,
            "records": [
                { "id": "r1", "name": "Build", "type": "Stage", "state": "completed", "result": "succeeded", "order": 1 },
                { "id": "r2", "name": "Compile", "type": "Phase", "state": "completed", "result": "succeeded", "order": 2, "parentId": "r1" },
                { "id": "r3", "name": "Test", "type": "Phase", "state": "completed", "result": "succeeded", "order": 3, "parentId": "r1" }
            ]
        })
    }

    pub fn pipelines_list() -> serde_json::Value {
        json!([{
            "id": 1,
            "name": "Main CI",
            "url": "https://dev.azure.com/ruslan-rystsov/_apis/pipelines/1",
            "folder": "\\",
            "revision": 5,
            "configuration": {
                "type": "yaml",
                "path": "azure-pipelines.yml",
                "repository": { "id": "repo-guid-1", "name": "Veles", "type": "azureReposGit" }
            }
        }])
    }

    pub fn pipeline_runs_list() -> serde_json::Value {
        json!([{
            "id": 100,
            "name": "20240115.1",
            "url": "https://dev.azure.com/ruslan-rystsov/_apis/pipelines/1/runs/100",
            "createdDate": "2024-01-15T10:00:00Z",
            "finishedDate": "2024-01-15T10:05:00Z",
            "state": "completed",
            "result": "succeeded",
            "pipeline": { "id": 1, "name": "Main CI" }
        }])
    }
}

// ── Mock response helpers ──

use httpmock::prelude::*;
use httpmock::Mock;

/// Mock a GET endpoint returning JSON.
pub fn mock_get<'a>(
    server: &'a MockServer,
    path: &str,
    status: u16,
    body: serde_json::Value
) -> Mock<'a> {
    server.mock(|when, then| {
        when.method(GET).path(path);
        then.status(status)
            .header(crate::constants::hdr::CONTENT_TYPE, crate::constants::ct::JSON)
            .json_body(body);
    })
}

/// Mock a POST endpoint returning JSON.
pub fn mock_post<'a>(
    server: &'a MockServer,
    path: &str,
    status: u16,
    body: serde_json::Value
) -> Mock<'a> {
    server.mock(|when, then| {
        when.method(POST).path(path);
        then.status(status)
            .header(crate::constants::hdr::CONTENT_TYPE, crate::constants::ct::JSON)
            .json_body(body);
    })
}

/// Mock an error response.
pub fn mock_error<'a>(
    server: &'a MockServer,
    path: &str,
    method: httpmock::Method,
    status: u16,
    message: &str
) -> Mock<'a> {
    server.mock(|when, then| {
        when.method(method).path(path);
        then.status(status)
            .header(crate::constants::hdr::CONTENT_TYPE, crate::constants::ct::JSON)
            .json_body(serde_json::json!({ crate::constants::JSON_MESSAGE_KEY: message }));
    })
}