gitflow-gitcode 2.0.0

gf 的 GitCode 适配器,基于 gitcode CLI 提供 Issue/MR/Release/Review 等能力。GitCode adapter for gf via gitcode 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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
//! GitCode Issue 提供者实现。
//!
//! 通过 `gitcode` CLI 实现 [`IssueProvider`] trait。GitCode CLI
//! 使用 `-R` 指定仓库、`--json` 为布尔标志、`version` 子命令检测版本。
//! JSON 响应字段名与 GitHub/GitLab CLI 不同(`user` 而非 `author` 等),
//! 通过 [`IssueApiResponse`] 做字段映射后转换为 core 类型。

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use gitflow_core::{
    CoreError, DEFAULT_LIST_LIMIT, FetchStrategy, Paged, Result, Session, fetch_capped,
    issue::{CreateIssueArgs, EditIssueArgs, IssueData, IssueProvider, ListIssueArgs},
    types::{CommentData, Label, State, UserSummary},
};
use serde::Deserialize;
use tracing::debug;

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

/// gitcode CLI `issue list --json` 的响应类型。
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
struct IssueApiResponse {
    number: String,
    title: String,
    body: Option<String>,
    state: String,
    #[serde(default)]
    labels: Option<Vec<LabelApi>>,
    user: Option<UserApi>,
    #[serde(default)]
    assignees: Option<Vec<UserApi>>,
    created_at: Option<String>,
    updated_at: Option<String>,
    html_url: String,
    #[serde(default)]
    milestone: Option<MilestoneRefApi>,
}

/// gitcode CLI 嵌入在 issue/PR 响应中的 `milestone` 对象的最小字段集。
///
/// 真实形状(2026-09-21 对 `byx-darwin/NexaTrade` issue #1 的实测捕获,
/// `gitcode issue view --json`):
/// ```json
/// {"id": null, "number": 866493, "title": "gf-357-test-milestone",
///  "description": "", "state": "active", "due_on": "2026-10-21"}
/// ```
/// 只映射 `number`/`title`:与 `gitflow_core::types::MilestoneRef` 的契约一致,
/// `id`/`description`/`state`/`due_on` 由 `gf milestone view` 单独提供。
#[derive(Debug, Clone, Deserialize)]
struct MilestoneRefApi {
    number: u64,
    title: String,
}

impl From<MilestoneRefApi> for gitflow_core::types::MilestoneRef {
    fn from(api: MilestoneRefApi) -> Self {
        Self {
            number: api.number,
            title: api.title,
        }
    }
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
struct LabelApi {
    name: String,
    #[serde(default)]
    color: Option<String>,
    #[serde(default)]
    description: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
struct UserApi {
    login: String,
    #[serde(default)]
    id: Option<String>,
}

impl From<IssueApiResponse> for IssueData {
    fn from(api: IssueApiResponse) -> Self {
        Self {
            number: api.number.parse().unwrap_or(0),
            title: api.title,
            body: api.body,
            state: match api.state.as_str() {
                "closed" => State::Closed,
                _ => State::Open,
            },
            labels: api
                .labels
                .unwrap_or_default()
                .into_iter()
                .map(Label::from)
                .collect(),
            author: api.user.map_or(
                UserSummary {
                    login: "unknown".into(),
                    id: String::new(),
                },
                UserSummary::from,
            ),
            assignees: api
                .assignees
                .unwrap_or_default()
                .into_iter()
                .map(UserSummary::from)
                .collect(),
            created_at: api.created_at.and_then(|s| {
                DateTime::parse_from_rfc3339(&s)
                    .ok()
                    .map(|d| d.with_timezone(&Utc))
            }),
            updated_at: api.updated_at.and_then(|s| {
                DateTime::parse_from_rfc3339(&s)
                    .ok()
                    .map(|d| d.with_timezone(&Utc))
            }),
            url: api.html_url,
            milestone: api.milestone.map(Into::into),
        }
    }
}

impl From<LabelApi> for Label {
    fn from(api: LabelApi) -> Self {
        Self {
            name: api.name,
            color: api.color,
            description: api.description,
        }
    }
}

impl From<UserApi> for UserSummary {
    fn from(api: UserApi) -> Self {
        Self {
            login: api.login,
            id: api.id.unwrap_or_default(),
        }
    }
}

/// gitcode CLI `issue comment --json` 的响应类型,兼容两种已观测形态:
/// - v0.6.x:`user` 为对象(含 `login`/`id`)、`id` 为数值、`created_at` 为带偏移 RFC3339
/// - 旧版本:`author` 为纯字符串(用户名)、`id` 为字符串、`created_at` 为 `YYYY-MM-DD HH:MM:SS`
#[derive(Debug, Clone, Deserialize)]
struct CommentApiResponse {
    #[serde(deserialize_with = "gitflow_core::types::deserialize_u64_or_string")]
    id: u64,
    #[serde(default)]
    body: String,
    #[serde(default)]
    author: Option<String>,
    #[serde(default)]
    user: Option<UserApi>,
    #[serde(default)]
    created_at: Option<String>,
}

impl From<CommentApiResponse> for CommentData {
    fn from(api: CommentApiResponse) -> Self {
        let author = api.user.map_or_else(
            || UserSummary {
                login: api.author.unwrap_or_else(|| "unknown".into()),
                id: String::new(),
            },
            UserSummary::from,
        );
        let created_at = api.created_at.as_deref().and_then(|s| {
            DateTime::parse_from_rfc3339(s)
                .map(|dt| dt.with_timezone(&Utc))
                .or_else(|_| {
                    chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S")
                        .map(|ndt| ndt.and_utc())
                })
                .ok()
        });
        Self {
            id: api.id,
            body: api.body,
            author,
            created_at,
        }
    }
}

/// gitcode CLI `issue close/reopen --json` 的响应类型。
///
/// 只是一个精简确认对象——实测响应(2026-09-21,对 byx-darwin/NexaTrade 的
/// 真实 issue #3)为 `{"number":3,"state":"closed","owner":"...","repo":"...","url":"..."}`,
/// 没有 `title`/`body`/`created_at`/`milestone` 等字段。只需要 `number` 去调用
/// `view()` 拿完整数据,其余字段不建模,避免死代码。
#[derive(Debug, Clone, Deserialize)]
struct CloseApiResponse {
    number: u64,
}

/// GitCode Issue 提供者,通过 `gitcode` CLI 操作。
///
/// 命令执行通过 [`CommandRunner`] 抽象,生产环境默认使用
/// [`RealCommandRunner`],测试可注入自定义 runner 以模拟成功或失败场景。
#[derive(Debug, Clone)]
pub struct GitCodeIssueProvider<R: CommandRunner = RealCommandRunner> {
    /// GitCode `owner/repo`。
    repo: String,
    /// 用于执行 `gitcode` CLI 命令的 runner。
    runner: R,
}

impl GitCodeIssueProvider<RealCommandRunner> {
    /// 创建一个新的 `GitCodeIssueProvider`,使用真实的进程执行器。
    #[must_use]
    pub fn new(repo: impl Into<String>) -> Self {
        Self {
            repo: repo.into(),
            runner: RealCommandRunner,
        }
    }

    /// Create a new provider from a shared [`Session`].
    ///
    /// This enables state reuse across multiple operations in workflow chains.
    #[must_use]
    pub fn with_session(session: &Session) -> Self {
        Self {
            repo: session.repo.clone(),
            runner: RealCommandRunner,
        }
    }
}

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

    /// 创建缺失的标签(使用 `--force` 保持幂等)。
    ///
    /// # Errors
    ///
    /// 当 `gitcode label create` 调用失败时返回错误。
    async fn ensure_label_exists(&self, name: &str) -> Result<()> {
        let binary = crate::gitcode_binary();
        debug!(repo = %self.repo, name, "auto-creating missing label via `gc label create`");

        let output = self
            .runner
            .run(
                &binary,
                &[
                    "label", "create", name, "--color", "ededed", "-R", &self.repo,
                ],
            )
            .await
            .map_err(|e| {
                CoreError::Platform(format!("Failed to spawn gitcode label create: {e}"))
            })?;

        if !output.status.success() {
            let gitcode_err = parse_gitcode_error(&output.stderr);
            tracing::debug!(error = %gitcode_err, "Failed to auto-create label");
            return Err(CoreError::Platform(format!(
                "Failed to auto-create label '{name}': {}",
                gitcode_err.user_message
            )));
        }

        Ok(())
    }
}

impl<R: CommandRunner + Clone + 'static> GitCodeIssueProvider<R> {
    /// [`IssueProvider::list`] 的实际实现。
    ///
    /// 抽成普通(非 `async_trait` 装箱)的关联函数,避开泛型异步闭包在
    /// `async_trait` 装箱 Future 内部触发的 HRTB `Send` 检查缺陷。
    async fn list_impl(&self, args: ListIssueArgs) -> Result<Paged<IssueData>> {
        let binary = crate::gitcode_binary();
        let binary = &binary;
        let cap = args.limit.unwrap_or(DEFAULT_LIST_LIMIT);
        let repo = &self.repo;
        let runner = &self.runner;
        let state = args.state;
        let search = &args.search;
        let labels = &args.labels;
        let milestone = &args.milestone;
        // 实测(gitcode-cli 0.12.0):`--per-page` 优先于 `--limit`,且被 API
        // 静默封顶在 100;`--page` 真实翻页,页间编号不重叠。页大小不必超过
        // cap+1:N+1 探测只需多要一条即可判断截断。
        let per_page = cap.saturating_add(1).min(crate::GITCODE_API_MAX_PER_PAGE);

        debug!(repo = %self.repo, cap, per_page, "spawning gitcode issue list");

        fetch_capped(
            FetchStrategy::Paged { per_page },
            cap,
            |page, per_page| async move {
                let page_str = page.to_string();
                let per_page_str = per_page.to_string();
                let mut cmd_args: Vec<&str> = vec!["issue", "list", "-R", repo, "--json"];

                if let Some(state) = &state {
                    cmd_args.push("--state");
                    cmd_args.push(match state {
                        State::Open => "open",
                        State::Closed => "closed",
                        State::All => "all",
                    });
                }
                if let Some(search) = search {
                    cmd_args.push("--search");
                    cmd_args.push(search);
                }
                for label in labels {
                    cmd_args.push("--label");
                    cmd_args.push(label);
                }
                let resolved_milestone_number_str;
                if let Some(identifier) = milestone {
                    let milestone_provider =
                        crate::GitCodeMilestoneProvider::with_runner(repo.as_str(), runner.clone());
                    let resolved = gitflow_core::label::resolve_milestone_identifier(
                        &milestone_provider,
                        identifier,
                    )
                    .await?;
                    resolved_milestone_number_str = resolved.number.to_string();
                    cmd_args.push("--milestone");
                    cmd_args.push(&resolved_milestone_number_str);
                }
                cmd_args.push("--per-page");
                cmd_args.push(&per_page_str);
                cmd_args.push("--page");
                cmd_args.push(&page_str);

                let output = runner
                    .run(binary, &cmd_args)
                    .await
                    .map_err(|e| CoreError::Platform(format!("{e}")))?;
                if !output.status.success() {
                    return Err(parse_gitcode_error(&output.stderr).into());
                }
                let issues: Vec<IssueApiResponse> =
                    serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;
                Ok(issues.into_iter().map(IssueData::from).collect())
            },
        )
        .await
    }
}

#[async_trait]
impl<R: CommandRunner + Clone + 'static> IssueProvider for GitCodeIssueProvider<R> {
    async fn create(&self, args: CreateIssueArgs) -> Result<IssueData> {
        let binary = crate::gitcode_binary();
        let mut cmd_args: Vec<&str> = vec![
            "issue",
            "create",
            "-R",
            &self.repo,
            "--title",
            &args.title,
            "--json",
        ];

        if let Some(body) = &args.body {
            cmd_args.push("--body");
            cmd_args.push(body);
        }
        for label in &args.labels {
            cmd_args.push("--label");
            cmd_args.push(label);
        }
        for assignee in &args.assignees {
            cmd_args.push("--assignee");
            cmd_args.push(assignee);
        }

        let resolved_milestone_number_str;
        if let Some(identifier) = &args.milestone {
            let milestone_provider = crate::GitCodeMilestoneProvider::with_runner(
                self.repo.as_str(),
                self.runner.clone(),
            );
            let resolved =
                gitflow_core::label::resolve_milestone_identifier(&milestone_provider, identifier)
                    .await?;
            resolved_milestone_number_str = resolved.number.to_string();
            cmd_args.push("--milestone");
            cmd_args.push(&resolved_milestone_number_str);
        }

        debug!(repo = %self.repo, title = %args.title, "spawning gitcode issue create");
        let output = self
            .runner
            .run(&binary, &cmd_args)
            .await
            .map_err(|e| CoreError::Platform(format!("{e}")))?;
        if !output.status.success() {
            // gc issue create fails when a requested label doesn't exist.
            // Auto-create missing labels and retry once.
            let missing = extract_missing_labels_from_error(&output.stderr);
            if !missing.is_empty() {
                debug!(
                    repo = %self.repo,
                    missing_count = missing.len(),
                    "auto-creating missing label(s) before retrying issue create"
                );
                for label in &missing {
                    self.ensure_label_exists(label).await?;
                }

                let retry_output = self.runner.run(&binary, &cmd_args).await.map_err(|e| {
                    CoreError::Platform(format!("Failed to spawn gitcode on retry: {e}"))
                })?;
                if !retry_output.status.success() {
                    return Err(parse_gitcode_error(&retry_output.stderr).into());
                }
                return serde_json::from_slice::<IssueApiResponse>(&retry_output.stdout)
                    .map(IssueData::from)
                    .map_err(CoreError::Serialization);
            }

            return Err(parse_gitcode_error(&output.stderr).into());
        }
        serde_json::from_slice::<IssueApiResponse>(&output.stdout)
            .map(IssueData::from)
            .map_err(CoreError::Serialization)
    }

    /// 编辑 Issue 的标题和/或正文。
    ///
    /// 调用 `<gitcode_binary> issue edit <number> -R <repo> [--title T] [--body B]`,
    /// 成功后通过 [`view`](Self::view) 重新拉取最新数据并返回(不解析 `edit` 自身的
    /// stdout,避免依赖未经验证的响应结构)。
    ///
    /// # Errors
    ///
    /// 当 Issue 不存在或 `gitcode` CLI 调用失败时返回错误。
    async fn edit(&self, number: u64, args: EditIssueArgs) -> Result<IssueData> {
        let binary = crate::gitcode_binary();
        let number_str = number.to_string();
        debug!(repo = %self.repo, number, "spawning gitcode issue edit");

        let mut cmd_args: Vec<&str> = vec!["issue", "edit", &number_str, "-R", &self.repo];
        if let Some(title) = &args.title {
            cmd_args.push("--title");
            cmd_args.push(title);
        }
        if let Some(body) = &args.body {
            cmd_args.push("--body");
            cmd_args.push(body);
        }

        let resolved_milestone_number_str;
        match &args.milestone {
            None => {}
            Some(None) => {
                // 实测(2026-09-21 对 byx-darwin/NexaTrade issue #1):`gitcode
                // issue edit --milestone 0` 被 CLI 解析层当作"未设置"直接拒绝
                // ("at least one edit option is required");`--milestone -1`
                // 会被后端静默丢弃而不是清除里程碑——`edit` 响应本身回显
                // milestone: null,但紧随其后的 `issue view` 仍显示原里程碑未变。
                // GitCode CLI 没有任何取消关联里程碑的手段,诚实报错而非假装成功。
                return Err(gitflow_core::CoreError::Platform(
                    "GitCode CLI does not support unassigning a milestone from an issue".into(),
                ));
            }
            Some(Some(identifier)) => {
                let milestone_provider = crate::GitCodeMilestoneProvider::with_runner(
                    self.repo.as_str(),
                    self.runner.clone(),
                );
                let resolved = gitflow_core::label::resolve_milestone_identifier(
                    &milestone_provider,
                    identifier,
                )
                .await?;
                resolved_milestone_number_str = resolved.number.to_string();
                cmd_args.push("--milestone");
                cmd_args.push(&resolved_milestone_number_str);
            }
        }

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

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

        self.view(number).await
    }

    async fn list(&self, args: ListIssueArgs) -> Result<Paged<IssueData>> {
        self.list_impl(args).await
    }

    async fn view(&self, number: u64) -> Result<IssueData> {
        let binary = crate::gitcode_binary();
        let number_str = number.to_string();
        debug!(repo = %self.repo, number, "spawning gitcode issue view");
        let output = self
            .runner
            .run(
                &binary,
                &["issue", "view", &number_str, "-R", &self.repo, "--json"],
            )
            .await
            .map_err(|e| CoreError::Platform(format!("{e}")))?;

        if !output.status.success() {
            return Err(parse_gitcode_error(&output.stderr).into());
        }
        serde_json::from_slice::<IssueApiResponse>(&output.stdout)
            .map(IssueData::from)
            .map_err(CoreError::Serialization)
    }

    async fn close(&self, number: u64) -> Result<IssueData> {
        let binary = crate::gitcode_binary();
        let number_str = number.to_string();
        debug!(repo = %self.repo, number, "spawning gitcode issue close");
        let output = self
            .runner
            .run(
                &binary,
                &[
                    "issue",
                    "close",
                    &number_str,
                    "-R",
                    &self.repo,
                    "--yes",
                    "--json",
                ],
            )
            .await
            .map_err(|e| CoreError::Platform(format!("{e}")))?;

        if !output.status.success() {
            return Err(parse_gitcode_error(&output.stderr).into());
        }
        let close_result: CloseApiResponse =
            serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;
        self.view(close_result.number).await
    }

    async fn reopen(&self, number: u64) -> Result<IssueData> {
        let binary = crate::gitcode_binary();
        let number_str = number.to_string();
        debug!(repo = %self.repo, number, "spawning gitcode issue reopen");
        let output = self
            .runner
            .run(
                &binary,
                &[
                    "issue",
                    "reopen",
                    &number_str,
                    "-R",
                    &self.repo,
                    "--yes",
                    "--json",
                ],
            )
            .await
            .map_err(|e| CoreError::Platform(format!("{e}")))?;

        if !output.status.success() {
            return Err(parse_gitcode_error(&output.stderr).into());
        }
        let reopen_result: CloseApiResponse =
            serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;
        self.view(reopen_result.number).await
    }

    /// 在指定 Issue 上添加评论。
    ///
    /// 调用 `gitcode issue comment <number> -R <repo> --body "<body>" --json`
    /// 发布评论,并返回新建评论的数据。
    ///
    /// # Errors
    ///
    /// 当 Issue 不存在、`body` 为空或 `gitcode` CLI 调用失败时返回错误。
    async fn comment(&self, number: u64, body: &str) -> Result<CommentData> {
        let binary = crate::gitcode_binary();
        let number_str = number.to_string();
        debug!(repo = %self.repo, number, "spawning `gc issue comment`");

        let output = self
            .runner
            .run(
                &binary,
                &[
                    "issue",
                    "comment",
                    &number_str,
                    "-R",
                    &self.repo,
                    "--body",
                    body,
                    "--json",
                ],
            )
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gitcode: {e}")))?;

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

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

        Ok(CommentData::from(api))
    }

    /// 列出指定 Issue 的评论。
    ///
    /// 调用 `gitcode api /repos/{owner}/{repo}/issues/{number}/comments` 获取评论列表,
    /// 通过 `per_page`/`page` 查询参数逐页取到 `limit`。
    ///
    /// GitCode CLI 的 `api` 子命令是否支持这两个查询参数**未经实测**(本环境
    /// 无法获取 GitCode CLI 二进制)。若平台忽略它们,首页会短于 `per_page`,
    /// 翻页循环在第一次调用后即因短页而终止——退化为今天「只取首页」的行为,
    /// 不会死循环也不会丢数据。
    ///
    /// # Errors
    ///
    /// 当 Issue 不存在或 `gitcode` CLI 调用失败时返回错误。
    async fn list_comments(&self, number: u64, limit: Option<u32>) -> Result<Paged<CommentData>> {
        let cap = limit.unwrap_or(DEFAULT_LIST_LIMIT);
        let binary = crate::gitcode_binary();
        let binary = &binary;
        let repo = &self.repo;
        let runner = &self.runner;
        let per_page = cap.saturating_add(1).min(crate::GITCODE_API_MAX_PER_PAGE);

        debug!(repo = %self.repo, number, cap, "spawning `gitcode api` GET issue comments");

        fetch_capped(
            FetchStrategy::Paged { per_page },
            cap,
            |page, per_page| async move {
                let api_path = format!(
                    "/repos/{repo}/issues/{number}/comments?per_page={per_page}&page={page}"
                );

                let output = runner.run(binary, &["api", &api_path]).await.map_err(|e| {
                    CoreError::Platform(format!("Failed to spawn gitcode api: {e}"))
                })?;

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

                let comments: Vec<CommentApiResponse> =
                    serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;
                Ok(comments.into_iter().map(CommentData::from).collect())
            },
        )
        .await
    }

    /// 为指定 Issue 添加一个或多个标签。
    ///
    /// 调用 `gitcode issue label <number> --add <labels> -R <repo>` 添加标签
    ///(逗号分隔的 `--add` 是 gitcode v0.6.x 的专用标签子命令;`issue edit`
    /// 不支持 gh 风格的 `--add-label` flag)。`labels` 为空时不进行任何调用。
    ///
    /// # 自动创建缺失标签
    ///
    /// 当添加因标签不存在而失败时,本方法会自动调用 `gitcode label create`
    /// 创建缺失的标签(默认颜色 `ededed`),然后重试一次。
    ///
    /// # Errors
    ///
    /// 当 Issue 不存在、标签创建失败或 `gitcode` CLI 调用失败时返回错误。
    async fn add_labels(&self, number: u64, labels: &[String]) -> Result<()> {
        if labels.is_empty() {
            return Ok(());
        }

        let binary = crate::gitcode_binary();
        let number_str = number.to_string();
        let joined = labels.join(",");
        debug!(
            repo = %self.repo,
            number,
            label_count = labels.len(),
            "spawning `gitcode issue label --add`"
        );

        let cmd_args: Vec<&str> = vec![
            "issue",
            "label",
            &number_str,
            "--add",
            &joined,
            "-R",
            &self.repo,
        ];

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

        if output.status.success() {
            return Ok(());
        }

        // Auto-create missing labels and retry once.
        let missing = extract_missing_labels_from_error(&output.stderr);
        if missing.is_empty() {
            return Err(parse_gitcode_error(&output.stderr).into());
        }

        debug!(
            repo = %self.repo,
            missing_count = missing.len(),
            "auto-creating missing label(s) before retry"
        );

        for label in &missing {
            self.ensure_label_exists(label).await?;
        }

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

        if !retry_output.status.success() {
            return Err(parse_gitcode_error(&retry_output.stderr).into());
        }

        Ok(())
    }

    /// 从指定 Issue 移除一个标签。
    ///
    /// 调用 `gitcode issue label <number> --remove <label> -R <repo>` 移除标签。
    ///
    /// # Errors
    ///
    /// 当 Issue 不存在、标签未附加到该 Issue 或 `gitcode` CLI 调用失败时返回错误。
    async fn remove_label(&self, number: u64, label: &str) -> Result<()> {
        let binary = crate::gitcode_binary();
        let number_str = number.to_string();
        debug!(repo = %self.repo, number, label, "spawning `gitcode issue label --remove`");

        let output = self
            .runner
            .run(
                &binary,
                &[
                    "issue",
                    "label",
                    &number_str,
                    "--remove",
                    label,
                    "-R",
                    &self.repo,
                ],
            )
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gitcode: {e}")))?;

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

        Ok(())
    }
}

/// 从 `gc issue label --add` 的 stderr 中提取缺失的标签名。
///
/// GitCode CLI 是 `gh` 的分支,错误格式与 `gh` 一致:
/// `'<label>' not found`。
fn extract_missing_labels_from_error(stderr: &[u8]) -> Vec<String> {
    let text = String::from_utf8_lossy(stderr);
    let mut labels = Vec::new();
    let mut search_from: usize = 0;

    while let Some(rel_open) = text[search_from..].find('\'') {
        let open_pos = search_from + rel_open + 1;
        let Some(rel_close) = text[open_pos..].find('\'') else {
            break;
        };
        let close_pos = open_pos + rel_close;
        let after_close = &text[close_pos + 1..];

        if after_close.starts_with(" not found") {
            let label = text[open_pos..close_pos].to_string();
            if !label.is_empty() {
                labels.push(label);
            }
        }
        search_from = close_pos + 1;
    }

    labels
}

#[cfg(test)]
mod tests {
    use gitflow_core::types::UserSummary;

    use super::*;
    use crate::runner::{MockCommandRunner, RecordingMockRunner, SequencedMockCommandRunner};

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

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

    #[test]
    fn test_should_deserialize_issue_data_from_gc_output() {
        let gc_json = br#"{
            "number": 42,
            "title": "Fix login bug",
            "body": "Reproduced on v1.2.3",
            "state": "open",
            "labels": [
                {"name": "bug", "color": "d73a4a", "description": "Something isn't working"}
            ],
            "author": {"login": "octocat", "id": "1"},
            "assignees": [{"login": "alice", "id": "7"}],
            "createdAt": "2026-01-15T09:30:00Z",
            "updatedAt": "2026-01-16T11:00:00Z",
            "url": "https://gitcode.com/octocat/hello-world/issues/42"
        }"#;

        let issue: IssueData = serde_json::from_slice(gc_json).expect("valid IssueData JSON");
        assert_eq!(issue.number, 42);
        assert_eq!(issue.title, "Fix login bug");
        assert_eq!(issue.state, State::Open);
        assert_eq!(issue.labels.len(), 1);
        assert_eq!(issue.author.login, "octocat");
        assert_eq!(issue.assignees.len(), 1);
        assert_eq!(
            issue.url,
            "https://gitcode.com/octocat/hello-world/issues/42"
        );
    }

    #[test]
    fn test_should_keep_issue_created_at_none_when_gc_api_omits_it() {
        let gc_json = br#"{
            "number": "15",
            "title": "No timestamps",
            "body": null,
            "state": "open",
            "labels": [],
            "author": {"login": "dev", "id": "5"},
            "assignees": [],
            "html_url": "https://gitcode.com/octocat/hello-world/issues/15"
        }"#;

        let api: IssueApiResponse =
            serde_json::from_slice(gc_json).expect("valid IssueApiResponse");
        let issue: IssueData = api.into();
        assert!(
            issue.created_at.is_none(),
            "missing created_at must stay None, not fall back to Utc::now()"
        );
        assert!(
            issue.updated_at.is_none(),
            "missing updated_at must stay None, not fall back to Utc::now()"
        );
    }

    #[test]
    fn test_should_keep_comment_created_at_none_when_gc_api_omits_it() {
        let gc_json = br#"{
            "id": "1002",
            "body": "No timestamp provided.",
            "author": "maintainer"
        }"#;

        let api: CommentApiResponse =
            serde_json::from_slice(gc_json).expect("valid CommentApiResponse");
        let comment = CommentData::from(api);
        assert!(
            comment.created_at.is_none(),
            "missing created_at must stay None, not fall back to Utc::now()"
        );
    }

    #[test]
    fn test_should_deserialize_empty_issue_list_from_gc_output() {
        let gc_json = b"[]";
        let issues: Vec<IssueData> = serde_json::from_slice(gc_json).expect("valid IssueData list");
        assert!(issues.is_empty());
    }

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

    #[test]
    fn test_should_deserialize_closed_issue_from_gc_close_output() {
        let gc_json = br#"{
            "number": 10,
            "title": "Fixed typo",
            "body": null,
            "state": "closed",
            "labels": [],
            "author": {"login": "dev", "id": "5"},
            "assignees": [],
            "createdAt": "2026-06-01T08:00:00Z",
            "updatedAt": "2026-06-02T12:00:00Z",
            "url": "https://gitcode.com/octocat/hello-world/issues/10"
        }"#;

        let issue: IssueData = serde_json::from_slice(gc_json).expect("valid closed IssueData");
        assert_eq!(issue.number, 10);
        assert_eq!(issue.state, State::Closed);
    }

    #[test]
    fn test_should_deserialize_reopened_issue_from_gc_reopen_output() {
        let gc_json = br#"{
            "number": 10,
            "title": "Fixed typo",
            "body": null,
            "state": "open",
            "labels": [],
            "author": {"login": "dev", "id": "5"},
            "assignees": [],
            "createdAt": "2026-06-01T08:00:00Z",
            "updatedAt": "2026-06-03T09:00:00Z",
            "url": "https://gitcode.com/octocat/hello-world/issues/10"
        }"#;

        let issue: IssueData = serde_json::from_slice(gc_json).expect("valid reopened IssueData");
        assert_eq!(issue.number, 10);
        assert_eq!(issue.state, State::Open);
    }

    #[test]
    fn test_should_deserialize_comment_data_from_gc_comment_output() {
        let gc_json = br#"{
            "id": "1001",
            "body": "Thanks for reporting, looking into it.",
            "author": "maintainer",
            "created_at": "2026-06-15 14:00:00"
        }"#;

        let api: CommentApiResponse =
            serde_json::from_slice(gc_json).expect("valid CommentApiResponse");
        let comment = CommentData::from(api);
        assert_eq!(comment.id, 1001);
        assert_eq!(comment.body, "Thanks for reporting, looking into it.");
        assert_eq!(comment.author.login, "maintainer");
    }

    #[test]
    fn test_should_roundtrip_comment_data_via_serde() {
        let comment = CommentData {
            id: 77,
            body: "reviewed".into(),
            author: UserSummary {
                login: "alice".into(),
                id: "3".to_string(),
            },
            created_at: Some("2026-05-01T00:00:00Z".parse().expect("valid date")),
        };
        let json = serde_json::to_string(&comment).expect("serialize");
        let round_tripped: CommentData = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(round_tripped.id, comment.id);
        assert_eq!(round_tripped.body, comment.body);
        assert_eq!(round_tripped.author.login, comment.author.login);
    }

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

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

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

    fn sample_create_args() -> CreateIssueArgs {
        CreateIssueArgs {
            title: "Bug report".to_string(),
            body: Some("Steps to reproduce".to_string()),
            labels: vec!["bug".to_string()],
            assignees: vec!["alice".to_string()],
            milestone: None,
        }
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gc_fails_for_view() {
        let runner = MockCommandRunner::failure("issue not found", 256);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

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

        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("not valid json");
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

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

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

    #[tokio::test]
    async fn test_should_return_platform_error_when_gc_fails_for_list() {
        let runner = MockCommandRunner::failure("forbidden", 256);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

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

        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");
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

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

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

    #[tokio::test]
    async fn test_should_return_platform_error_when_gc_fails_for_create() {
        let runner = MockCommandRunner::failure("validation failed", 256);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

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

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

    #[tokio::test]
    async fn test_should_return_serialization_error_on_invalid_json_for_create() {
        let runner = MockCommandRunner::success("not valid json");
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

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

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

    #[tokio::test]
    async fn test_should_close_then_view_to_get_full_issue_with_milestone() {
        // 实测响应(2026-09-21,对 byx-darwin/NexaTrade 的真实 issue #3):
        // `gitcode issue close --json` 只返回精简确认对象(number/state/owner/repo/url),
        // 没有 title/created_at/updated_at/milestone。close() 必须用这个精简形状拿到
        // number,再调用 view() 取回完整、正确的 IssueData(而不是伪造 title=""、
        // created_at=now() 并丢弃 milestone)。
        let runner = SequencedMockCommandRunner::from_results(&[
            (
                true,
                r#"{"number":3,"state":"closed","owner":"byx-darwin","repo":"NexaTrade","url":"https://gitcode.com/byx-darwin/NexaTrade/issues/3"}"#,
            ),
            (
                true,
                r#"{"number":"3","title":"Real title","body":"real body","state":"closed","labels":[],"user":{"login":"octocat","id":"1"},"assignees":[],"created_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-02T00:00:00Z","html_url":"https://gitcode.com/owner/repo/issues/3","milestone":{"number":7,"title":"v3.0"}}"#,
            ),
        ]);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        let issue = provider.close(3).await.expect("close should succeed");

        // 标题/时间戳来自第二次(view)响应,证明确实走了 view 而非直接转换精简响应
        assert_eq!(issue.number, 3);
        assert_eq!(issue.title, "Real title");
        assert_eq!(issue.body.as_deref(), Some("real body"));
        assert_eq!(
            issue.created_at.map(|dt| dt.to_rfc3339()),
            Some("2026-01-01T00:00:00+00:00".to_string())
        );
        assert_eq!(
            issue.milestone,
            Some(gitflow_core::types::MilestoneRef {
                number: 7,
                title: "v3.0".into(),
            })
        );
        let calls = runner.recorded_calls();
        assert_eq!(calls.len(), 2, "close 必须先 close 再 view 两次调用");
        assert!(calls[0].1.contains(&"close".to_string()));
        assert!(calls[1].1.contains(&"view".to_string()));
    }

    #[tokio::test]
    async fn test_should_reopen_then_view_to_get_full_issue_with_milestone() {
        let runner = SequencedMockCommandRunner::from_results(&[
            (
                true,
                r#"{"number":3,"state":"open","owner":"byx-darwin","repo":"NexaTrade","url":"https://gitcode.com/byx-darwin/NexaTrade/issues/3"}"#,
            ),
            (
                true,
                r#"{"number":"3","title":"Real title","body":null,"state":"open","labels":[],"user":{"login":"octocat","id":"1"},"assignees":[],"created_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-02T00:00:00Z","html_url":"https://gitcode.com/owner/repo/issues/3","milestone":{"number":7,"title":"v3.0"}}"#,
            ),
        ]);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        let issue = provider.reopen(3).await.expect("reopen should succeed");

        assert_eq!(issue.number, 3);
        assert_eq!(issue.title, "Real title");
        assert_eq!(
            issue.milestone,
            Some(gitflow_core::types::MilestoneRef {
                number: 7,
                title: "v3.0".into(),
            })
        );
        let calls = runner.recorded_calls();
        assert_eq!(calls.len(), 2, "reopen 必须先 reopen 再 view 两次调用");
        assert!(calls[0].1.contains(&"reopen".to_string()));
        assert!(calls[1].1.contains(&"view".to_string()));
    }

    #[tokio::test]
    async fn test_should_propagate_view_error_after_issue_close_succeeds() {
        let runner = SequencedMockCommandRunner::from_results(&[
            (true, r#"{"number":3,"state":"closed"}"#),
            (false, "gitcode: issue not found"),
        ]);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

        let err = provider
            .close(3)
            .await
            .expect_err("view failure must propagate");

        assert!(matches!(err, gitflow_core::CoreError::Cli(_)));
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gc_fails_for_close() {
        let runner = MockCommandRunner::failure("not found", 256);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

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

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

    #[tokio::test]
    async fn test_should_return_platform_error_when_gc_fails_for_reopen() {
        let runner = MockCommandRunner::failure("not found", 256);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

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

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

    #[tokio::test]
    async fn test_should_return_platform_error_when_gc_fails_for_comment() {
        let runner = MockCommandRunner::failure("not found", 256);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

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

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

    #[tokio::test]
    async fn test_should_produce_complete_argv_for_list_comments_with_default_limit() {
        let runner = MockCommandRunner::success("[]");
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        let paged = provider
            .list_comments(359, None)
            .await
            .expect("list_comments should succeed");

        assert!(paged.items.is_empty());
        let calls = runner.recorded_calls();
        assert_eq!(calls[0].0, crate::gitcode_binary());
        assert_eq!(
            calls[0].1,
            vec![
                "api",
                "/repos/owner/repo/issues/359/comments?per_page=100&page=1"
            ]
        );
    }

    #[tokio::test]
    async fn test_should_build_well_formed_query_string_for_list_comments() {
        let runner = MockCommandRunner::success("[]");
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        provider
            .list_comments(359, None)
            .await
            .expect("list_comments should succeed");

        let calls = runner.recorded_calls();
        let api_path = &calls[0].1[1];
        assert_eq!(
            api_path.matches('?').count(),
            1,
            "must have exactly one '?', got: {api_path}"
        );
        assert_eq!(
            api_path.matches('&').count(),
            1,
            "must have exactly one '&', got: {api_path}"
        );
        // 整串相等,而非 `contains`:`"per_page=1001".contains("per_page=100")`
        // 与 `"per_page=100".contains("page=1")` 都为真,子串断言无法检测出
        // 页大小钳位失效或页号错误——正是本测试得名的那个缺陷。
        assert_eq!(
            api_path, "/repos/owner/repo/issues/359/comments?per_page=100&page=1",
            "got: {api_path}"
        );
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gc_fails_for_add_labels() {
        let runner = MockCommandRunner::failure("not found", 256);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

        let result = provider.add_labels(42, &["bug".to_string()]).await;

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

    #[tokio::test]
    async fn test_should_return_platform_error_when_gc_fails_for_remove_label() {
        let runner = MockCommandRunner::failure("not found", 256);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

        let result = provider.remove_label(42, "bug").await;

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

    #[tokio::test]
    async fn test_should_edit_issue_and_view_result() {
        // Sequence: 1. `gitcode issue edit` → succeeds
        //           2. `gitcode issue view --json` → returns updated issue JSON
        let runner = SequencedMockCommandRunner::from_results(&[
            (true, ""),
            (
                true,
                r#"{"number":"42","title":"New title","body":"orig","state":"open","labels":[],"user":{"login":"octocat","id":"1"},"assignees":[],"created_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-02T00:00:00Z","html_url":"https://gitcode.com/owner/repo/issues/42"}"#,
            ),
        ]);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

        let issue = provider
            .edit(
                42,
                gitflow_core::issue::EditIssueArgs {
                    title: Some("New title".to_string()),
                    body: None,
                    milestone: None,
                },
            )
            .await
            .expect("edit should succeed");

        assert_eq!(issue.number, 42);
        assert_eq!(issue.title, "New title");
    }

    #[tokio::test]
    async fn test_should_invoke_issue_edit_subcommand_with_provided_fields() {
        let runner = RecordingMockRunner::success("");
        let provider = GitCodeIssueProvider::with_runner("o/r", runner.clone());

        // view() call afterward will fail on empty stdout — irrelevant to this test,
        // which only asserts the first (edit) call's argv.
        let _ = provider
            .edit(
                54,
                gitflow_core::issue::EditIssueArgs {
                    title: Some("T".to_string()),
                    body: Some("B".to_string()),
                    milestone: None,
                },
            )
            .await;

        assert_eq!(
            runner.calls()[0],
            vec![
                "issue", "edit", "54", "-R", "o/r", "--title", "T", "--body", "B"
            ]
        );
    }

    #[tokio::test]
    async fn test_should_return_platform_error_when_gc_fails_for_edit() {
        let runner = MockCommandRunner::failure("not found", 256);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

        let result = provider
            .edit(42, gitflow_core::issue::EditIssueArgs::default())
            .await;

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

    // --- milestone wiring: create/edit/list ---

    #[test]
    fn test_should_deserialize_issue_with_milestone() {
        // Real shape captured from `gitcode issue view --json` on
        // byx-darwin/NexaTrade issue #1 (2026-09-21), trimmed to the fields
        // this module reads (`id`/`description`/`state`/`due_on` ignored).
        let json = br#"{
            "number": "1",
            "title": "gf-357 milestone test issue",
            "body": "temp test issue for milestone shape investigation",
            "state": "open",
            "html_url": "https://gitcode.com/byx-darwin/NexaTrade/issues/1",
            "user": {"login": "byx-darwin", "id": "66767cd4096c81780c61bf07"},
            "assignees": [],
            "labels": [],
            "milestone": {
                "id": null,
                "number": 866493,
                "title": "gf-357-test-milestone",
                "description": "",
                "state": "active",
                "due_on": "2026-10-21"
            },
            "created_at": "2026-09-21T10:18:16+08:00",
            "updated_at": "2026-09-21T10:18:16+08:00"
        }"#;
        let api: IssueApiResponse = serde_json::from_slice(json).expect("deserialize");
        let issue: IssueData = api.into();
        assert_eq!(
            issue.milestone,
            Some(gitflow_core::types::MilestoneRef {
                number: 866_493,
                title: "gf-357-test-milestone".into()
            })
        );
    }

    #[test]
    fn test_should_deserialize_issue_with_no_milestone() {
        let json = br#"{
            "number": "1",
            "title": "t",
            "body": null,
            "state": "open",
            "html_url": "https://gitcode.com/o/r/issues/1",
            "user": null,
            "assignees": [],
            "labels": [],
            "created_at": "2026-09-21T10:18:16+08:00",
            "updated_at": "2026-09-21T10:18:16+08:00"
        }"#;
        let api: IssueApiResponse = serde_json::from_slice(json).expect("deserialize");
        let issue: IssueData = api.into();
        assert!(issue.milestone.is_none());
    }

    /// A single-item `gitcode milestone list --json` response resolving to
    /// number 3 / title "v2.0", matching `resolve_milestone_identifier`'s contract.
    fn milestone_list_fixture() -> String {
        r#"[{"number": 3, "title": "v2.0", "description": null, "state": "open", "due_on": null, "closed_issues": 0, "open_issues": 0}]"#.to_string()
    }

    #[tokio::test]
    async fn test_should_wire_resolved_milestone_number_into_issue_create() {
        // Sequence: 1. milestone resolution: `gitcode milestone list -R owner/repo --json ...`
        //           2. `gitcode issue create ... --milestone 3`
        let runner = SequencedMockCommandRunner::from_results(&[
            (true, &milestone_list_fixture()),
            (
                true,
                r#"{"number":"42","title":"New feature","body":"Description","state":"open","labels":[],"user":{"login":"octocat","id":"1"},"assignees":[],"created_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-01T00:00:00Z","html_url":"https://gitcode.com/owner/repo/issues/42","milestone":{"number":3,"title":"v2.0"}}"#,
            ),
        ]);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        let mut args = sample_create_args();
        args.milestone = Some("3".to_string()); // resolve by number

        let issue = provider.create(args).await.expect("create should succeed");

        assert_eq!(
            issue.milestone,
            Some(gitflow_core::types::MilestoneRef {
                number: 3,
                title: "v2.0".into()
            })
        );

        let calls = runner.recorded_calls();
        assert_eq!(calls.len(), 2);
        assert_eq!(
            calls[0].1.first().map(String::as_str),
            Some("milestone"),
            "first call must resolve the milestone via `gitcode milestone list`, got: {:?}",
            calls[0].1
        );
        let create_call = &calls[1].1;
        assert!(
            create_call
                .windows(2)
                .any(|w| w[0] == "--milestone" && w[1] == "3"),
            "issue create argv must carry the resolved milestone NUMBER (not title), got: \
             {create_call:?}"
        );
    }

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

        let mut args = sample_create_args();
        args.milestone = Some("does-not-exist".to_string());

        let result = provider.create(args).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_should_wire_resolved_milestone_number_into_issue_edit() {
        // Sequence: 1. milestone resolution
        //           2. `gitcode issue edit <number> --milestone 3`
        //           3. `gitcode issue view --json` (edit() re-fetches via view)
        let runner = SequencedMockCommandRunner::from_results(&[
            (true, &milestone_list_fixture()),
            (true, ""),
            (
                true,
                r#"{"number":"42","title":"T","body":null,"state":"open","labels":[],"user":{"login":"octocat","id":"1"},"assignees":[],"created_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-01T00:00:00Z","html_url":"https://gitcode.com/owner/repo/issues/42","milestone":{"number":3,"title":"v2.0"}}"#,
            ),
        ]);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        let issue = provider
            .edit(
                42,
                gitflow_core::issue::EditIssueArgs {
                    title: None,
                    body: None,
                    milestone: Some(Some("3".to_string())),
                },
            )
            .await
            .expect("edit should succeed");

        assert_eq!(issue.milestone.map(|m| m.number), Some(3));

        let calls = runner.recorded_calls();
        let edit_call = &calls[1].1;
        assert!(
            edit_call
                .windows(2)
                .any(|w| w[0] == "--milestone" && w[1] == "3"),
            "issue edit argv must carry the resolved milestone NUMBER, got: {edit_call:?}"
        );
    }

    #[tokio::test]
    async fn test_should_error_on_unassign_milestone_without_spawning_cli() {
        // Confirmed 2026-09-21 against byx-darwin/NexaTrade issue #1: neither
        // `--milestone 0` (rejected by the CLI's own flag parsing as "no edit
        // option provided") nor `--milestone -1` (silently ignored by the
        // backend — `issue view` right after still shows the original
        // milestone attached) actually unassigns. GitCode's CLI has no
        // unassign capability, so `Some(None)` must error before spawning
        // anything, not fake success.
        let runner = RecordingMockRunner::success("should not be called");
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        let result = provider
            .edit(
                42,
                gitflow_core::issue::EditIssueArgs {
                    title: None,
                    body: None,
                    milestone: Some(None),
                },
            )
            .await;

        let err = result.expect_err("unassign must error, not silently succeed");
        assert!(
            matches!(err, gitflow_core::CoreError::Platform(_)),
            "expected CoreError::Platform, got {err:?}"
        );
        assert!(
            runner.calls().is_empty(),
            "must reject before spawning the CLI, got calls: {:?}",
            runner.calls()
        );
    }

    #[tokio::test]
    async fn test_should_wire_resolved_milestone_number_into_issue_list_filter() {
        // Sequence: 1. milestone resolution
        //           2. `gitcode issue list ... --milestone 3`
        let runner = SequencedMockCommandRunner::from_results(&[
            (true, &milestone_list_fixture()),
            (true, "[]"),
        ]);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        let _ = provider
            .list(ListIssueArgs {
                milestone: Some("v2.0".to_string()),
                ..ListIssueArgs::default()
            })
            .await;

        let calls = runner.recorded_calls();
        let list_call = &calls[1].1;
        assert!(
            list_call
                .windows(2)
                .any(|w| w[0] == "--milestone" && w[1] == "3"),
            "issue list argv must carry the resolved milestone NUMBER (not title), got: \
             {list_call:?}"
        );
    }

    // --- extract_missing_labels_from_error: pure-function tests ---

    #[test]
    fn test_should_extract_single_missing_label_from_gc_stderr() {
        let stderr =
            b"failed to update https://gitcode.com/owner/repo/issues/18: 'type:enhancement' not found";
        let missing = extract_missing_labels_from_error(stderr);
        assert_eq!(missing, vec!["type:enhancement".to_string()]);
    }

    #[test]
    fn test_should_return_empty_when_no_label_not_found_in_gc_stderr() {
        let stderr = b"gitcode: Not logged in";
        let missing = extract_missing_labels_from_error(stderr);
        assert!(missing.is_empty());
    }

    // --- add_labels: auto-create missing labels ---

    #[tokio::test]
    async fn test_should_auto_create_label_and_retry_on_add_labels() {
        // Sequence:
        // 1. `gc issue edit 18 --add-label type:enhancement` → fails
        // 2. `gc label create type:enhancement --color ededed -R owner/repo` → succeeds
        // 3. `gc issue edit 18 --add-label type:enhancement` → succeeds (retry)
        let runner = SequencedMockCommandRunner::from_results(&[
            (
                false,
                "failed to update https://gitcode.com/owner/repo/issues/18: 'type:enhancement' \
                 not found",
            ),
            (true, ""),
            (true, ""),
        ]);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

        let result = provider
            .add_labels(18, &["type:enhancement".to_string()])
            .await;

        assert!(result.is_ok(), "expected Ok, got {result:?}");
    }

    #[tokio::test]
    async fn test_should_propagate_error_when_gc_label_create_fails() {
        let runner = SequencedMockCommandRunner::from_results(&[
            (false, "failed to update ...: 'ghost' not found"),
            (false, "gitcode: 403 Forbidden"),
        ]);
        let provider = GitCodeIssueProvider::with_runner("o/r", runner);

        let result = provider.add_labels(1, &["ghost".to_string()]).await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_should_not_retry_on_non_label_error_gc() {
        let runner = SequencedMockCommandRunner::from_results(&[(false, "gitcode: Not logged in")]);
        let provider = GitCodeIssueProvider::with_runner("o/r", runner);

        let result = provider.add_labels(1, &["bug".to_string()]).await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_should_invoke_issue_label_subcommand_for_add_labels() {
        let runner = RecordingMockRunner::success("");
        let provider = GitCodeIssueProvider::with_runner("o/r", runner.clone());

        provider
            .add_labels(54, &["type:bug".to_string(), "priority:high".to_string()])
            .await
            .expect("add_labels should succeed");

        let calls = runner.calls();
        assert_eq!(calls.len(), 1);
        assert_eq!(
            calls[0],
            vec![
                "issue",
                "label",
                "54",
                "--add",
                "type:bug,priority:high",
                "-R",
                "o/r"
            ],
            r"gitcode v0.6.1 的 issue edit 没有 --add-label flag(Issue #90)"
        );
    }

    #[tokio::test]
    async fn test_should_return_ok_without_any_call_for_empty_labels() {
        let runner = RecordingMockRunner::success("");
        let provider = GitCodeIssueProvider::with_runner("o/r", runner.clone());

        provider
            .add_labels(1, &[])
            .await
            .expect("empty labels is a no-op");

        assert!(runner.calls().is_empty());
    }

    #[tokio::test]
    async fn test_should_invoke_issue_label_subcommand_for_remove_label() {
        let runner = RecordingMockRunner::success("");
        let provider = GitCodeIssueProvider::with_runner("o/r", runner.clone());

        provider
            .remove_label(54, "triage:done")
            .await
            .expect("remove should succeed");

        assert_eq!(
            runner.calls()[0],
            vec![
                "issue",
                "label",
                "54",
                "--remove",
                "triage:done",
                "-R",
                "o/r"
            ]
        );
    }

    #[tokio::test]
    async fn test_should_auto_create_missing_label_and_retry_via_issue_label() {
        // 1. issue label --add 失败,报告标签缺失
        // 2. label create 成功(自动创建)
        // 3. issue label --add 重试成功
        let runner = SequencedMockCommandRunner::from_results(&[
            (false, "HTTP 404: 'type:new' not found"),
            (true, r#"{"name": "type:new", "color": "ededed"}"#),
            (true, ""),
        ]);
        let provider = GitCodeIssueProvider::with_runner("o/r", runner);

        provider
            .add_labels(18, &["type:new".to_string()])
            .await
            .expect("should recover by auto-creating the label");
    }

    #[test]
    fn test_should_parse_issue_comment_with_user_object() {
        let json = r#"{"id": 12, "body": "hi", "user": {"login": "bob", "id": "u2"}, "created_at": "2026-07-30T12:00:00+08:00"}"#;
        let api: CommentApiResponse = serde_json::from_str(json).expect("user-object shape");
        let comment: CommentData = api.into();
        assert_eq!(comment.id, 12);
        assert_eq!(comment.author.login, "bob");
        assert_eq!(comment.author.id, "u2");
    }

    /// 构造 `count` 条合法 issue JSON,编号从 `start` 递增。
    fn issue_page_json(start: u64, count: u64) -> String {
        let items: Vec<String> = (start..start + count)
            .map(|n| {
                format!(
                    r#"{{"number":"{n}","title":"t{n}","state":"open","body":null,"labels":[],"created_at":"2026-07-30T12:00:00+08:00","updated_at":"2026-07-30T12:00:00+08:00","html_url":"https://gitcode.com/owner/repo/issues/{n}"}}"#
                )
            })
            .collect();
        format!("[{}]", items.join(","))
    }

    #[tokio::test]
    async fn test_should_page_through_gitcode_issue_list_with_incrementing_page_numbers() {
        // cap=100 → per_page=min(101,100)=100,want=cap+1=101。
        // 首页满 100 条:既非短页,又未达 want ⇒ `fetch_capped` 必然发出第二页。
        // 这是唯一能观测到页号递增的取值区间 —— cap < 100 时 per_page 恰为
        // cap+1,首页一次就满足 want,循环只会发出一次调用。
        let page1 = issue_page_json(1, 100);
        let page2 = issue_page_json(101, 100);
        let runner = SequencedMockCommandRunner::from_results(&[(true, &page1), (true, &page2)]);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        let result = provider
            .list(ListIssueArgs {
                limit: Some(100),
                ..ListIssueArgs::default()
            })
            .await
            .expect("list should succeed");

        assert_eq!(result.items.len(), 100, "返回条数必须被 cap 钳住");
        assert!(result.truncated, "超过 cap 必须诚实报告截断");

        let calls = runner.recorded_calls();
        assert_eq!(
            calls.len(),
            2,
            "必须真的翻到第二页,实际调用数: {}",
            calls.len()
        );
        let first = &calls[0].1;
        assert!(
            first
                .windows(2)
                .any(|w| w[0] == "--per-page" && w[1] == "100"),
            "首次调用必须显式传 --per-page,实际 argv: {first:?}"
        );
        assert!(
            first.windows(2).any(|w| w[0] == "--page" && w[1] == "1"),
            "首次调用必须显式传 --page=1,实际 argv: {first:?}"
        );
        assert!(
            !first.iter().any(|a| a == "--limit"),
            "--per-page 已决定单页大小,不得再传 --limit,实际 argv: {first:?}"
        );
        let second = &calls[1].1;
        assert!(
            second.windows(2).any(|w| w[0] == "--page" && w[1] == "2"),
            "页号必须在翻页中递增,实际 argv: {second:?}"
        );
    }

    #[tokio::test]
    async fn test_should_report_no_truncation_when_gitcode_issue_list_ends_on_short_page() {
        // cap=150 → per_page=min(151,100)=100,want=cap+1=151。
        // 首页满 100 条(非短页,且未达 want)⇒ 循环必须继续翻到第二页;
        // 第二页返回 20 条(短页)⇒ 循环必须就此停止,不再探第三页。
        // 总条目 120 < cap=150,因此必须诚实报告 truncated=false。
        let page1 = issue_page_json(1, 100);
        let page2 = issue_page_json(101, 20);
        let runner = SequencedMockCommandRunner::from_results(&[(true, &page1), (true, &page2)]);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        let result = provider
            .list(ListIssueArgs {
                limit: Some(150),
                ..ListIssueArgs::default()
            })
            .await
            .expect("list should succeed");

        assert_eq!(result.items.len(), 120, "两页条目必须全部保留,不得丢数据");
        assert!(!result.truncated, "总数未达 cap,必须诚实报告未截断");

        let calls = runner.recorded_calls();
        assert_eq!(
            calls.len(),
            2,
            "短页应在第二页停止,不得再探第三页,实际调用数: {}",
            calls.len()
        );
        let first = &calls[0].1;
        assert!(
            first.windows(2).any(|w| w[0] == "--page" && w[1] == "1"),
            "首次调用必须显式传 --page=1,实际 argv: {first:?}"
        );
        let second = &calls[1].1;
        assert!(
            second.windows(2).any(|w| w[0] == "--page" && w[1] == "2"),
            "第二次调用必须显式传 --page=2,实际 argv: {second:?}"
        );
    }

    #[tokio::test]
    async fn test_should_cap_gitcode_issue_per_page_at_api_maximum() {
        let runner = MockCommandRunner::success("[]");
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        provider
            .list(ListIssueArgs::default())
            .await
            .expect("list should succeed");

        let recorded = &runner.recorded_calls()[0].1;
        assert!(
            recorded
                .windows(2)
                .any(|w| w[0] == "--per-page" && w[1] == "100"),
            "默认 cap=1000 时 per_page 必须被 API 上限 100 钳住,实际 argv: {recorded:?}"
        );
    }

    #[tokio::test]
    async fn test_should_keep_forwarding_label_filter_on_gitcode() {
        let runner = MockCommandRunner::success("[]");
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());
        let args = ListIssueArgs {
            labels: vec!["bug".to_string()],
            ..ListIssueArgs::default()
        };
        provider.list(args).await.expect("list should succeed");
        let recorded = &runner.recorded_calls()[0].1;
        assert!(
            recorded
                .windows(2)
                .any(|w| w[0] == "--label" && w[1] == "bug")
        );
    }

    #[tokio::test]
    async fn test_should_list_issues_with_state_and_label_using_full_argv() {
        let runner = MockCommandRunner::success("[]");
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner.clone());

        let _ = provider
            .list(ListIssueArgs {
                state: Some(State::Open),
                labels: vec!["bug".to_string()],
                ..ListIssueArgs::default()
            })
            .await;

        assert_eq!(
            runner.recorded_calls()[0].1,
            vec![
                "issue",
                "list",
                "-R",
                "owner/repo",
                "--json",
                "--state",
                "open",
                "--label",
                "bug",
                "--per-page",
                "100",
                "--page",
                "1"
            ]
            .into_iter()
            .map(String::from)
            .collect::<Vec<_>>()
        );
    }
}

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

    /// 契约测试:验证 gitcode issue list JSON 输出与反序列化一致。
    ///
    /// 夹具来源:gitcode v0.6.x 真实 CLI 输出。
    #[tokio::test]
    async fn test_contract_issue_list_gitcode_v0_6() {
        let fixture = include_str!("../tests/fixtures/issue_list_gitcode_v0.6.json");
        let runner = MockCommandRunner::success(fixture);
        let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);

        let issues = provider
            .list(ListIssueArgs::default())
            .await
            .expect("contract fixture must parse");
        let issues = issues.items;

        assert_eq!(issues.len(), 1);
        let issue = &issues[0];
        assert_eq!(issue.number, 15);
        assert!(!issue.title.is_empty());
        assert_eq!(issue.state, gitflow_core::types::State::Open);
    }
}