gitflow-github 1.0.0

gf 的 GitHub 适配器,基于 gh CLI 提供 Issue/PR/Release/Review 等能力。GitHub adapter for gf via gh CLI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
//! GitHub Pull Request 提供者实现。
//!
//! 通过 `gh` CLI 实现 [`PrProvider`] trait,支持 Pull Request 的创建、列表、查看、
//! 关闭、合并、检出、草稿状态切换和分支同步。
//! 命令执行通过 [`CommandRunner`] 抽象,捕获 stdout 并解析 JSON。

use async_trait::async_trait;
use gitflow_core::{
    CoreError, Result,
    pr::{CreatePrArgs, ListPrArgs, PrData, PrProvider},
    types::{CommentData, MergeResult, MergeStrategy, State},
};
use tracing::debug;

use crate::{
    error::parse_gh_error,
    runner::{CommandRunner, RealCommandRunner},
};

/// `gh pr` 请求的 JSON 字段列表。
const PR_FIELDS: &str =
    "number,title,body,state,isDraft,author,baseRefName,headRefName,createdAt,updatedAt,url";

/// GitHub Pull Request 提供者,通过 `gh` CLI 操作。
///
/// 该结构体通过调用 `gh` CLI 实现 [`PrProvider`] trait 的所有方法,
/// 使上层命令能够以统一的方式操作 GitHub Pull Request。
///
/// 命令执行通过 [`CommandRunner`] 抽象,生产环境默认使用
/// [`RealCommandRunner`],测试可注入自定义 runner 以模拟成功或失败场景。
///
/// # Examples
///
/// ```no_run
/// use gitflow_github::GitHubPrProvider;
///
/// let provider = GitHubPrProvider::new("octocat/hello-world");
/// ```
#[derive(Debug, Clone)]
pub struct GitHubPrProvider<R: CommandRunner = RealCommandRunner> {
    /// GitHub `owner/repo`,如 `"byx-darwin/gitflow-cli"`。
    repo: String,
    /// 用于执行 `gh` CLI 命令的 runner。
    runner: R,
}

impl GitHubPrProvider<RealCommandRunner> {
    /// 创建新的 GitHub Pull Request 提供者,使用真实的进程执行器。
    ///
    /// `repo` 格式为 `owner/repo`。
    #[must_use]
    pub fn new(repo: impl Into<String>) -> Self {
        Self {
            repo: repo.into(),
            runner: RealCommandRunner,
        }
    }
}

impl<R: CommandRunner> GitHubPrProvider<R> {
    /// 使用自定义 [`CommandRunner`] 创建提供者。
    ///
    /// 主要用于测试,可注入模拟 runner 以控制 `gh` CLI 的输出。
    /// `repo` 格式为 `owner/repo`。
    #[must_use]
    pub fn with_runner(repo: impl Into<String>, runner: R) -> Self {
        Self {
            repo: repo.into(),
            runner,
        }
    }
}

#[async_trait]
impl<R: CommandRunner + 'static> PrProvider for GitHubPrProvider<R> {
    async fn create(&self, args: CreatePrArgs) -> Result<PrData> {
        let repo = args.repo.as_deref().unwrap_or(&self.repo);

        let mut cmd_args: Vec<&str> = vec![
            "pr",
            "create",
            "--repo",
            repo,
            "--title",
            &args.title,
            "--head",
            &args.head,
            "--base",
            &args.base,
        ];

        if let Some(body) = &args.body {
            cmd_args.push("--body");
            cmd_args.push(body);
        }

        if args.draft {
            cmd_args.push("--draft");
        }

        debug!(
            repo = %self.repo,
            title = %args.title,
            head = %args.head,
            base = %args.base,
            "spawning `gh pr create`"
        );

        let output = self
            .runner
            .run("gh", &cmd_args)
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        // Parse the PR URL from stdout (format: https://github.com/owner/repo/pull/123)
        let stdout = String::from_utf8_lossy(&output.stdout);
        let pr_number = parse_pr_number_from_url(&stdout).ok_or_else(|| {
            CoreError::Platform(format!("Failed to parse PR URL from output: {stdout}"))
        })?;

        // Fetch full PR details via view
        self.view(pr_number).await
    }

    async fn list(&self, args: ListPrArgs) -> Result<Vec<PrData>> {
        let mut cmd_args: Vec<&str> = vec!["pr", "list", "--repo", &self.repo, "--json", PR_FIELDS];

        if let Some(state) = &args.state {
            cmd_args.push("--state");
            cmd_args.push(match state {
                State::Open => "open",
                State::Closed => "closed",
            });
        }

        let limit_str = args.limit.map(|limit| limit.to_string());
        if let Some(ref limit) = limit_str {
            cmd_args.push("--limit");
            cmd_args.push(limit);
        }

        debug!(repo = %self.repo, "spawning `gh pr list`");

        let output = self
            .runner
            .run("gh", &cmd_args)
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        let prs: Vec<PrData> =
            serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;

        Ok(prs)
    }

    async fn view(&self, number: u64) -> Result<PrData> {
        debug!(repo = %self.repo, number, "spawning `gh pr view`");

        let number_str = number.to_string();
        let output = self
            .runner
            .run(
                "gh",
                &[
                    "pr",
                    "view",
                    &number_str,
                    "--repo",
                    &self.repo,
                    "--json",
                    PR_FIELDS,
                ],
            )
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        let pr: PrData =
            serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;

        Ok(pr)
    }

    /// 关闭指定编号的 PR。
    ///
    /// 调用 `gh pr close <number> --repo <repo>` 关闭 PR,
    /// 并返回更新后的完整 PR 数据。
    ///
    /// # Errors
    ///
    /// 当 PR 不存在、已关闭或 `gh` CLI 调用失败时返回错误。
    async fn close(&self, number: u64) -> Result<PrData> {
        debug!(repo = %self.repo, number, "spawning `gh pr close`");

        let number_str = number.to_string();
        let output = self
            .runner
            .run("gh", &["pr", "close", &number_str, "--repo", &self.repo])
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        // Fetch updated PR details
        self.view(number).await
    }

    /// 重新打开指定编号的 PR。
    ///
    /// 调用 `gh pr reopen <number> --repo <repo>` 重新打开已关闭的 PR,
    /// 并返回更新后的完整 PR 数据。
    ///
    /// # Errors
    ///
    /// 当 PR 不存在、未关闭或 `gh` CLI 调用失败时返回错误。
    async fn reopen(&self, number: u64) -> Result<PrData> {
        debug!(repo = %self.repo, number, "spawning `gh pr reopen`");

        let number_str = number.to_string();
        let output = self
            .runner
            .run("gh", &["pr", "reopen", &number_str, "--repo", &self.repo])
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        // Fetch updated PR details
        self.view(number).await
    }

    /// 在指定 PR 上添加评论。
    ///
    /// 调用 `gh api repos/{repo}/issues/{number}/comments -X POST` 创建评论,
    /// 直接从 POST 响应中解析新创建的评论数据。
    /// (PR 评论使用与 Issue 评论相同的 API 端点。)
    ///
    /// # Errors
    ///
    /// 当 PR 不存在、`body` 为空或 `gh` CLI 调用失败时返回错误。
    async fn comment(&self, number: u64, body: &str) -> Result<CommentData> {
        debug!(repo = %self.repo, number, "spawning `gh api` POST to create PR comment");

        let api_path = format!(
            "repos/{repo}/issues/{number}/comments",
            repo = self.repo,
            number = number
        );

        let body_field = format!("body={body}");
        let output = self
            .runner
            .run("gh", &["api", &api_path, "-X", "POST", "-f", &body_field])
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh api: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        let comment: crate::issue::GitHubCommentApiResponse =
            serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;

        Ok(comment.into())
    }

    /// 合并指定编号的 PR。
    ///
    /// 调用 `gh pr merge <number> --repo <repo>` 并根据 `strategy` 参数
    /// 添加 `--squash`、`--rebase` 或 `--merge` 标志。
    /// 未指定策略时使用 `--merge`(标准合并)。
    ///
    /// # Errors
    ///
    /// 当 PR 不存在、存在冲突无法合并或 `gh` CLI 调用失败时返回错误。
    async fn merge(&self, number: u64, strategy: Option<MergeStrategy>) -> Result<MergeResult> {
        debug!(repo = %self.repo, number, ?strategy, "spawning `gh pr merge`");

        let number_str = number.to_string();
        let mut cmd_args: Vec<&str> = vec!["pr", "merge", &number_str, "--repo", &self.repo];

        match strategy {
            Some(MergeStrategy::Squash) => cmd_args.push("--squash"),
            Some(MergeStrategy::Rebase) => cmd_args.push("--rebase"),
            Some(MergeStrategy::Merge) | None => cmd_args.push("--merge"),
        }

        let output = self
            .runner
            .run("gh", &cmd_args)
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        // `gh pr merge` outputs a human-readable message, not JSON.
        let message = String::from_utf8_lossy(&output.stdout).trim().to_string();
        Ok(MergeResult {
            merged: true,
            sha: None,
            message: Some(message),
        })
    }

    /// 在本地检出指定 PR 的分支。
    ///
    /// 调用 `gh pr checkout <number> --repo <repo>` 在本地工作区创建并切换到
    /// PR 的来源分支。如果本地已存在该分支,则尝试更新它。
    ///
    /// # Errors
    ///
    /// 当 PR 不存在、本地 git 操作失败或 `gh` CLI 调用失败时返回错误。
    async fn checkout(&self, number: u64) -> Result<()> {
        debug!(repo = %self.repo, number, "spawning `gh pr checkout`");

        let number_str = number.to_string();
        let output = self
            .runner
            .run("gh", &["pr", "checkout", &number_str, "--repo", &self.repo])
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        Ok(())
    }

    /// 将草稿 PR 标记为可审查状态(ready for review)。
    ///
    /// 调用 `gh pr ready <number> --repo <repo>` 将草稿 PR 转为可审查状态,
    /// 并通过 `gh pr view` 重新获取更新后的 PR 数据。
    ///
    /// # Errors
    ///
    /// 当 PR 不存在、不是草稿状态或 `gh` CLI 调用失败时返回错误。
    async fn mark_ready(&self, number: u64) -> Result<PrData> {
        debug!(repo = %self.repo, number, "spawning `gh pr ready`");

        let number_str = number.to_string();
        let output = self
            .runner
            .run("gh", &["pr", "ready", &number_str, "--repo", &self.repo])
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        // `gh pr ready` does not return JSON; re-fetch the PR to get updated data.
        self.view(number).await
    }

    /// 将 PR 标记为草稿状态(work in progress)。
    ///
    /// 调用 `gh pr convert-to-draft <number> --repo <repo>` 将可审查的 PR 转为草稿,
    /// 并通过 `gh pr view` 重新获取更新后的 PR 数据。
    ///
    /// # Errors
    ///
    /// 当 PR 不存在、已是草稿状态或 `gh` CLI 调用失败时返回错误。
    async fn mark_wip(&self, number: u64) -> Result<PrData> {
        debug!(repo = %self.repo, number, "spawning `gh pr convert-to-draft`");

        let number_str = number.to_string();
        let output = self
            .runner
            .run(
                "gh",
                &["pr", "convert-to-draft", &number_str, "--repo", &self.repo],
            )
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        // `gh pr convert-to-draft` does not return JSON; re-fetch the PR.
        self.view(number).await
    }

    /// 同步 PR 的分支(将 base 分支的最新变更合入 head 分支)。
    ///
    /// 调用 `gh pr update-branch <number> --repo <repo>` 将 PR 的来源分支
    /// 更新到与目标分支的最新状态同步,解决分支过时问题。
    ///
    /// # Errors
    ///
    /// 当 PR 不存在、同步存在冲突或 `gh` CLI 调用失败时返回错误。
    async fn sync_branch(&self, number: u64) -> Result<()> {
        debug!(repo = %self.repo, number, "spawning `gh pr update-branch`");

        let number_str = number.to_string();
        let output = self
            .runner
            .run(
                "gh",
                &["pr", "update-branch", &number_str, "--repo", &self.repo],
            )
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        Ok(())
    }
}

/// Parse PR number from GitHub URL.
///
/// Extracts the numeric PR number from URLs like:
/// - `https://github.com/owner/repo/pull/123`
/// - `https://github.enterprise.com/org/project/pull/456`
fn parse_pr_number_from_url(url: &str) -> Option<u64> {
    url.lines().find_map(|line| {
        let line = line.trim();
        if line.contains("/pull/") {
            line.rsplit('/').next().and_then(|s| s.parse().ok())
        } else {
            None
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runner::MockCommandRunner;

    #[test]
    fn test_should_construct_github_pr_provider() {
        let provider = GitHubPrProvider::new("octocat/hello-world");
        assert_eq!(provider.repo, "octocat/hello-world");
    }

    #[test]
    fn test_should_construct_github_pr_provider_from_string() {
        let repo = String::from("octocat/hello-world");
        let provider = GitHubPrProvider::new(repo);
        assert_eq!(provider.repo, "octocat/hello-world");
    }

    #[test]
    fn test_should_deserialize_pr_data_from_gh_output() {
        // 模拟 `gh pr view --json ...` 的实际输出
        let gh_json = br#"{
            "number": 123,
            "title": "Add new feature",
            "body": "This PR adds a new feature",
            "state": "open",
            "draft": false,
            "author": {"login": "alice", "id": "2"},
            "baseBranch": "main",
            "headBranch": "feature/new-thing",
            "createdAt": "2026-02-20T14:00:00Z",
            "updatedAt": "2026-02-21T10:30:00Z",
            "url": "https://github.com/octocat/hello-world/pull/123"
        }"#;

        let pr: PrData = serde_json::from_slice(gh_json).expect("valid PrData JSON");
        assert_eq!(pr.number, 123);
        assert_eq!(pr.title, "Add new feature");
        assert_eq!(pr.state, State::Open);
        assert!(!pr.draft);
        assert_eq!(pr.author.login, "alice");
        assert_eq!(pr.base_branch, "main");
        assert_eq!(pr.head_branch, "feature/new-thing");
        assert_eq!(pr.url, "https://github.com/octocat/hello-world/pull/123");
    }

    #[test]
    fn test_should_deserialize_empty_pr_list_from_gh_output() {
        let gh_json = b"[]";
        let prs: Vec<PrData> = serde_json::from_slice(gh_json).expect("valid PrData list");
        assert!(prs.is_empty());
    }

    #[test]
    fn test_should_deserialize_draft_pr_from_gh_output() {
        let gh_json = br#"{
            "number": 456,
            "title": "WIP: experiment",
            "body": null,
            "state": "open",
            "draft": true,
            "author": {"login": "bob", "id": "3"},
            "baseBranch": "main",
            "headBranch": "wip/experiment",
            "createdAt": "2026-03-10T09:00:00Z",
            "updatedAt": "2026-03-10T09:00:00Z",
            "url": "https://github.com/octocat/hello-world/pull/456"
        }"#;

        let pr: PrData = serde_json::from_slice(gh_json).expect("valid PrData JSON");
        assert!(pr.draft);
        assert!(pr.body.is_none());
        assert_eq!(pr.title, "WIP: experiment");
    }

    #[test]
    fn test_should_debug_format_provider() {
        let provider = GitHubPrProvider::new("octocat/hello-world");
        let debug = format!("{provider:?}");
        assert!(debug.contains("GitHubPrProvider"));
        assert!(debug.contains("octocat/hello-world"));
    }

    // --- close/reopen: deserialized PrData tests ---

    #[test]
    fn test_should_deserialize_closed_pr_from_gh_close_output() {
        // 模拟 `gh pr close --json ...` 的返回数据
        let gh_json = br#"{
            "number": 50,
            "title": "Obsolete change",
            "body": "Superseded by #55",
            "state": "closed",
            "draft": false,
            "author": {"login": "dev", "id": "10"},
            "baseBranch": "main",
            "headBranch": "feature/obsolete",
            "createdAt": "2026-05-01T08:00:00Z",
            "updatedAt": "2026-05-02T12:00:00Z",
            "url": "https://github.com/octocat/hello-world/pull/50"
        }"#;

        let pr: PrData = serde_json::from_slice(gh_json).expect("valid closed PrData");
        assert_eq!(pr.number, 50);
        assert_eq!(pr.state, State::Closed);
    }

    #[test]
    fn test_should_deserialize_reopened_pr_from_gh_reopen_output() {
        let gh_json = br#"{
            "number": 50,
            "title": "Obsolete change",
            "body": "Actually still needed",
            "state": "open",
            "draft": false,
            "author": {"login": "dev", "id": "10"},
            "baseBranch": "main",
            "headBranch": "feature/obsolete",
            "createdAt": "2026-05-01T08:00:00Z",
            "updatedAt": "2026-05-03T09:00:00Z",
            "url": "https://github.com/octocat/hello-world/pull/50"
        }"#;

        let pr: PrData = serde_json::from_slice(gh_json).expect("valid reopened PrData");
        assert_eq!(pr.number, 50);
        assert_eq!(pr.state, State::Open);
    }

    // --- comment: CommentData deserialization tests ---

    #[test]
    fn test_should_deserialize_comment_data_from_gh_pr_comment_output() {
        // 模拟 `gh pr comment --json id,body,author,createdAt` 的输出
        let gh_json = br#"{
            "id": 2002,
            "body": "Approved, merging now.",
            "author": {"login": "reviewer", "id": "88"},
            "createdAt": "2026-06-20T16:00:00Z"
        }"#;

        let comment: CommentData = serde_json::from_slice(gh_json).expect("valid CommentData");
        assert_eq!(comment.id, 2002);
        assert_eq!(comment.body, "Approved, merging now.");
        assert_eq!(comment.author.login, "reviewer");
        assert_eq!(comment.author.id, "88");
    }

    #[tokio::test]
    async fn test_should_return_newly_created_pr_comment_not_stale() {
        use crate::runner::MockCommandRunner;

        // Mock `gh api POST` response — the comment just created
        let post_response_json = r#"{
            "id": 8888888888,
            "body": "NEW PR comment",
            "user": {"login": "reviewer", "id": 99},
            "created_at": "2026-08-04T13:00:00Z"
        }"#;

        let runner = MockCommandRunner::success(post_response_json);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.comment(7, "NEW PR comment").await.unwrap();

        // The returned id must be the NEW comment's id, not a stale one
        assert_eq!(result.id, 8_888_888_888);
        assert_eq!(result.body, "NEW PR comment");
        assert_eq!(result.author.login, "reviewer");
    }

    #[tokio::test]
    async fn test_should_return_error_when_pr_comment_api_fails() {
        use crate::runner::MockCommandRunner;

        let runner = MockCommandRunner::failure(
            r#"{"message": "Not Found", "documentation_url": "https://docs.github.com"}"#,
            1,
        );
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.comment(999, "test").await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        // `parse_gh_error` returns `PlatformCliError`, which converts to
        // `CoreError::Cli` (matching the existing comment error-path test).
        assert!(
            matches!(err, CoreError::Cli(_)),
            "expected Cli error, got: {err:?}"
        );
    }

    // --- merge: MergeResult deserialization tests ---

    #[test]
    fn test_should_deserialize_merge_result_from_gh_merge_output() {
        // `gh pr merge` 返回人类可读文本,不是 JSON。
        // MergeResult 由代码构造,但需确保序列化/反序列化正确。
        let gh_text = b"Pull request #123 was successfully merged.\n";
        let message = String::from_utf8_lossy(gh_text).trim().to_string();
        let result = MergeResult {
            merged: true,
            sha: None,
            message: Some(message),
        };

        assert!(result.merged);
        assert!(result.message.as_deref().is_some());
        assert_eq!(
            result.message.as_deref(),
            Some("Pull request #123 was successfully merged.")
        );
    }

    #[test]
    fn test_should_roundtrip_merge_result_via_serde() {
        let result = MergeResult {
            merged: true,
            sha: Some("deadbeef1234".into()),
            message: Some("Squash merged".into()),
        };
        let json = serde_json::to_string(&result).expect("serialize");
        let round_tripped: MergeResult = serde_json::from_str(&json).expect("deserialize");
        assert!(round_tripped.merged);
        assert_eq!(round_tripped.sha, result.sha);
        assert_eq!(round_tripped.message, result.message);
    }

    #[test]
    fn test_should_serialize_merge_result_skips_null_fields() {
        let result = MergeResult {
            merged: false,
            sha: None,
            message: None,
        };
        let json = serde_json::to_string(&result).expect("serialize");
        assert!(!json.contains("null"));
        assert_eq!(json, r#"{"merged":false}"#);
    }

    // --- provider construction / clone tests ---

    #[test]
    fn test_should_create_provider_with_different_repos() {
        let r1 = GitHubPrProvider::new("org/repo-a");
        let r2 = GitHubPrProvider::new("org/repo-b");
        assert_eq!(r1.repo, "org/repo-a");
        assert_eq!(r2.repo, "org/repo-b");
    }

    #[test]
    fn test_should_clone_github_pr_provider() {
        let original = GitHubPrProvider::new("owner/repo");
        let cloned = original.clone();
        assert_eq!(original.repo, cloned.repo);
    }

    // --- Failure-path tests using an injected MockCommandRunner ---

    fn sample_create_args() -> CreatePrArgs {
        CreatePrArgs {
            title: "Add feature".to_string(),
            body: Some("Detailed description".to_string()),
            head: "feature/new".to_string(),
            base: "main".to_string(),
            draft: false,
            repo: None,
        }
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_create() {
        let runner = MockCommandRunner::failure(r#"{"message": "Validation failed"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.create(sample_create_args()).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_on_url_parse_failure_for_create() {
        // `create` succeeds but stdout has no parseable PR URL, so URL parsing fails.
        let runner = MockCommandRunner::success("no pull url here");
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.create(sample_create_args()).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Platform(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_list() {
        let runner = MockCommandRunner::failure(r#"{"message": "Forbidden"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.list(ListPrArgs::default()).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_serialization_error_on_invalid_json_for_list() {
        let runner = MockCommandRunner::success("invalid json");
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.list(ListPrArgs::default()).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Serialization(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_view() {
        let runner = MockCommandRunner::failure(r#"{"message": "PR not found"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.view(999).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_serialization_error_on_invalid_json_for_view() {
        let runner = MockCommandRunner::success("invalid json");
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.view(1).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Serialization(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_close() {
        let runner = MockCommandRunner::failure(r#"{"message": "Not found"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.close(42).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_reopen() {
        let runner = MockCommandRunner::failure(r#"{"message": "Not found"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.reopen(42).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_merge() {
        let runner = MockCommandRunner::failure(r#"{"message": "Merge conflict"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.merge(42, None).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_on_invalid_json_for_merge() {
        // `gh pr merge` returns human-readable text, so any successful output is a
        // valid message. A failing exit status is the merge failure path instead.
        let runner = MockCommandRunner::failure("invalid json", 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.merge(42, Some(MergeStrategy::Squash)).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_checkout() {
        let runner = MockCommandRunner::failure(r#"{"message": "Not found"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.checkout(42).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_sync_branch() {
        let runner = MockCommandRunner::failure(r#"{"message": "Conflict"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.sync_branch(42).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_mark_ready() {
        let runner = MockCommandRunner::failure(r#"{"message": "Not a draft"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.mark_ready(42).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_mark_wip() {
        let runner = MockCommandRunner::failure(r#"{"message": "Already a draft"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.mark_wip(42).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gh_fails_for_comment() {
        let runner = MockCommandRunner::failure(r#"{"message": "Not found"}"#, 256);
        let provider = GitHubPrProvider::with_runner("owner/repo", runner);

        let result = provider.comment(42, "a comment").await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            gitflow_core::CoreError::Cli(_)
        ));
    }
}