gitbucket-cli 0.8.1

A command-line tool for GitBucket, inspired by GitHub CLI
mod support;

use serde_json::json;
use support::gb_cmd::{assert_failure, stderr, GbTestEnv};
use support::mock_http::{spawn_scripted_server, ScriptedResponse};

#[test]
fn numbered_pr_operations_validate_the_target_before_using_issue_subresources() {
    let actions = [
        vec![
            "pr",
            "edit",
            "5",
            "--title",
            "wrong",
            "--add-assignee",
            "alice",
            "--web",
        ],
        vec!["pr", "close", "5"],
        vec!["pr", "reopen", "5"],
        vec!["pr", "merge", "5"],
        vec![
            "pr",
            "comment",
            "list",
            "5",
            "--kind",
            "discussion",
            "--json",
        ],
        // Missing noninteractive input is rejected before lookup; input_display
        // covers that preflight. Valid writes still verify the PR target here.
        vec!["pr", "comment", "5", "--body", "must not post"],
        vec![
            "pr",
            "comment",
            "5",
            "--edit-last",
            "--body",
            "must not edit",
        ],
        vec!["pr", "view", "5", "--comments", "--kind", "discussion"],
        vec!["pr", "view", "5", "--web"],
        vec!["pr", "checkout", "5"],
        vec!["pr", "diff", "5"],
    ];
    for mode in ["404", "null", "mismatch", "wrong_base", "forbidden"] {
        for args in &actions {
            let env = GbTestEnv::new();
            let (status, body) = match mode {
                "404" => ("404 Not Found", json!({"message":"not a PR"})),
                "null" => ("200 OK", json!(null)),
                "mismatch" => (
                    "200 OK",
                    json!({"number":6,"title":"another PR","state":"open"}),
                ),
                "wrong_base" => (
                    "200 OK",
                    json!({
                        "number": 5, "title": "PR in another repository", "state": "open",
                        "head": {"ref": "topic", "sha": "1".repeat(40)},
                        "base": {"ref": "main", "sha": "2".repeat(40),
                            "repo": {"name": "other", "full_name": "alice/other"}}
                    }),
                ),
                _ => ("403 Forbidden", json!({"message":"denied"})),
            };
            let snapshot_lookup = mode == "null" && matches!(args[1], "checkout" | "diff");
            let mut responses = vec![ScriptedResponse::json(
                "GET /bucket/api/v3/repos/alice/project/pulls/5 HTTP/1.1",
                status,
                &body.to_string(),
            )];
            if snapshot_lookup {
                responses.push(ScriptedResponse::json(
                    "GET /bucket/api/v3/repos/alice/project/pulls?state=all HTTP/1.1",
                    "200 OK",
                    "[]",
                ));
            }
            let (port, server) = spawn_scripted_server(responses);
            let output = env
                .repo_api_command(format!("127.0.0.1:{port}/bucket"), "alice/project")
                .env("GB_USER", "alice")
                .env("GB_PASSWORD", "must-not-be-used")
                .stdin(std::process::Stdio::null())
                .args(args)
                .output()
                .unwrap();
            assert_failure(&output);
            assert!(
                output.stdout.is_empty(),
                "{args:?}: {}",
                String::from_utf8_lossy(&output.stdout)
            );
            let message = stderr(&output);
            assert!(
                message.contains(match mode {
                    "404" => "API error (404)",
                    "null" if snapshot_lookup => "Pull request #5 not found",
                    "null" if args[1] == "merge" => "Canonical PR details are unavailable",
                    "null" => "Canonical details for pull request #5 are unavailable",
                    "mismatch" => "PR target mismatch",
                    "wrong_base" if matches!(args[1], "checkout" | "diff") => {
                        "PR base repository does not match"
                    }
                    "wrong_base" => "PR target mismatch",
                    _ => "API error (403)",
                }),
                "{args:?}, {mode}: {message}"
            );
            let requests = server.join().unwrap();
            assert_eq!(requests.len(), if snapshot_lookup { 2 } else { 1 });
            assert!(requests
                .iter()
                .all(|request| request.method == "GET" && request.target.contains("/pulls")));
        }
    }
}