skardi 0.5.0

High performance query engine for both offline compute and online serving
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
//! GitHub source pack: stable relational contracts over the Open Connector
//! `github.*` read actions (API-key auth, page-number pagination).
//!
//! Design decisions, per the integration design spec and the source-pack
//! admission gate:
//!
//! - **Page-number pagination everywhere** (`page`/`perPage`, 100 per page
//!   — GitHub's maximum; the camelCase keys are Open Connector's strict
//!   action-input contract, not GitHub's raw REST parameters). A short or
//!   empty page terminates the scan — except for `issues`, whose OC action
//!   filters pull requests out AFTER paginating, so a filtered page's
//!   length is not a termination signal: the table declares
//!   `raw_page_size_path: $.pageInfo.fetched` (upstream #228) and the scan
//!   continues while the RAW page was full, even when the filtered rows
//!   come back short or empty. Requires a gateway with
//!   oomol-lab/open-connector#228 (older gateways fail the fingerprint
//!   gate at registration, and their responses would fail the scan loudly
//!   with a missing `$.pageInfo.fetched` — never a silent truncation).
//! - **Filters are allowlisted only where faithful — and every string-enum
//!   push is Inexact.** `issues.state` / `pull_requests.state` narrow the
//!   fetch but DataFusion re-applies them: the translation is faithful only
//!   inside GitHub's enum domain (open/closed/all), and an Exact claim
//!   would lean on the provider rejecting out-of-domain literals rather
//!   than silently returning its default listing. `issues.updated_at >=`
//!   maps to `since` as [`Fidelity::Inexact`](crate::sources::providers::open_connector::filters::Fidelity::Inexact): GitHub documents issue `since` as
//!   "updated at *or after*" (a superset of the predicate under any
//!   timestamp-granularity fuzz), so DataFusion reapplies the predicate
//!   locally. The commits endpoint's `since` is documented as commits
//!   *after* the date — strictly-after cannot guarantee a superset of a
//!   `>=` predicate (the boundary row would be unrecoverable), so it is
//!   deliberately **not** mapped, exactly like the mock pack's `>=` note.
//! - **`issues` is pure issues.** GitHub's raw issues endpoint mixes pull
//!   requests in, but the Open Connector action filters them out before
//!   returning (`"Pull requests are filtered out from the response"`), so
//!   the table declares no `pull_request` marker column — it could never
//!   be non-NULL. Pull requests live in their own table.
//! - **Nullability is conservative**: only identity fields (`id`, `number`,
//!   `sha`, `tag_name`, …) are non-null. GitHub nulls out whole objects
//!   (`commit.author: null`, `issue.user: null`); nullable columns under
//!   them become SQL NULL per the converter's null-parent rule.
//! - **Fingerprints are pinned** from a live gateway: each table's
//!   `fingerprint` in `github.yaml` is the BLAKE3 hash of the canonicalized
//!   output schema captured into `fixtures/github/contracts/`, and a test
//!   keeps pin and captured contract locked together. Registration
//!   compares the pin against the discovered contract and fails with
//!   `ActionContractMismatch` on drift — including additive schema
//!   changes, which is the designed tradeoff (re-capture and re-pin on
//!   upstream upgrades). The action IDs, input keys, row paths, and
//!   endpoint contract are likewise reconciled against the live gateway
//!   and its provider source; the bundled fixtures (see tests) remain the
//!   build-time conversion contract.

use std::sync::OnceLock;

use crate::sources::providers::open_connector::error::OpenConnectorError;
use crate::sources::providers::open_connector::source_pack::SourcePack;

use super::loader;

static PACK: OnceLock<Result<SourcePack, String>> = OnceLock::new();

/// The GitHub pack, parsed once from the embedded YAML asset.
pub fn pack() -> Result<&'static SourcePack, OpenConnectorError> {
    loader::builtin("github.yaml", include_str!("github.yaml"), &PACK)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sources::providers::open_connector::error::OpenConnectorError;
    use crate::sources::providers::open_connector::json_to_arrow::RowConverter;
    use crate::sources::providers::open_connector::pagination::PaginationStrategy;
    use crate::sources::providers::open_connector::row_path::RowPath;
    use crate::sources::providers::open_connector::source_pack::SourcePackTable;
    use arrow::array::{
        Array, BooleanArray, ListArray, StringArray, TimestampMillisecondArray, UInt64Array,
    };
    use arrow::record_batch::RecordBatch;

    /// Discovery responses serve the CAPTURED live contracts, so every e2e
    /// registration exercises the fingerprint gate's pass side.
    fn github_discovery(path: &str) -> MockResponse {
        let contracts: &[(&str, &str)] = &[
            (
                "github.list_my_repositories",
                include_str!("fixtures/github/contracts/list_my_repositories.json"),
            ),
            (
                "github.list_repository_issues",
                include_str!("fixtures/github/contracts/list_repository_issues.json"),
            ),
            (
                "github.list_issue_comments",
                include_str!("fixtures/github/contracts/list_issue_comments.json"),
            ),
            (
                "github.list_pull_requests",
                include_str!("fixtures/github/contracts/list_pull_requests.json"),
            ),
            (
                "github.list_pull_request_reviews",
                include_str!("fixtures/github/contracts/list_pull_request_reviews.json"),
            ),
            (
                "github.list_commits",
                include_str!("fixtures/github/contracts/list_commits.json"),
            ),
            (
                "github.list_workflow_runs",
                include_str!("fixtures/github/contracts/list_workflow_runs.json"),
            ),
            (
                "github.list_releases",
                include_str!("fixtures/github/contracts/list_releases.json"),
            ),
        ];
        let output_schema = contracts
            .iter()
            .find(|(action, _)| path.ends_with(action))
            .map(|(_, schema)| *schema)
            .unwrap_or(r#"{"type": "object"}"#);
        MockResponse::ok(&discovery_ok("{}", output_schema, true, None))
    }

    #[tokio::test]
    async fn filtered_issue_pages_do_not_truncate_the_scan() {
        // The OC action filters pull requests out AFTER paginating, so a
        // filtered page can be short — or entirely empty — while more pages
        // exist. The raw signal (pageInfo.fetched, upstream #228) must
        // drive continuation: page 1 returns 2 issues of a full raw page,
        // page 2 is ALL pull requests (0 issues, raw full), page 3 is the
        // genuine final page. Short-page termination would stop after page
        // 1 and lose everything after it.
        let gateway = MockGateway::start(|req| {
            if req.method == "GET" && req.path == "/v1/health" {
                return MockResponse::ok("{}");
            }
            if req.method == "GET" && req.path.starts_with("/v1/actions/") {
                return github_discovery(&req.path);
            }
            if req.method == "POST" && req.path == "/v1/actions/github.list_repository_issues" {
                let body: Value = serde_json::from_str(&req.body).unwrap_or_default();
                let page = body["input"]["page"].as_u64().unwrap_or(1);
                let response = match page {
                    1 => json!({"issues": [issue(1, "open", "2026-01-01T00:00:00Z"),
                                             issue(2, "open", "2026-01-01T00:00:00Z")],
                                 "pageInfo": {"fetched": 100}}),
                    2 => json!({"issues": [], "pageInfo": {"fetched": 100}}),
                    3 => json!({"issues": [issue(3, "open", "2026-01-02T00:00:00Z")],
                                 "pageInfo": {"fetched": 1}}),
                    other => panic!("unexpected page {other}"),
                };
                return MockResponse::ok(&envelope_ok(&response.to_string()));
            }
            MockResponse::new(404, "{}")
        })
        .await;
        let (_gw, ctx) = setup_with_gateway(gateway, "SKARDI_TEST_OC_GITHUB_RAW_PAGE").await;

        let batches = collect(&ctx, "SELECT number FROM saas.gh.issues ORDER BY number").await;
        assert_eq!(
            rows_of(&batches),
            3,
            "all three pages were scanned: the short and the all-PR page did not terminate"
        );
    }

    #[test]
    fn fingerprint_coverage_gap_is_pinned() {
        // The gate protects only what upstream DECLARES; these mapped
        // columns ride additionalProperties passthrough, so their drift is
        // invisible to the fingerprint and surfaces at scan time per
        // conversion rules (a shape change fails loudly; a removed nullable
        // field reads as NULL). Pinning the set makes any change — upstream
        // declaring more, or a mapping change — a conscious decision.
        use crate::sources::providers::open_connector::testutil::fingerprint_uncovered_columns;
        for (short, contract, expected) in [
            (
                "repositories",
                include_str!("fixtures/github/contracts/list_my_repositories.json"),
                &[
                    "language",
                    "stargazers_count",
                    "forks_count",
                    "open_issues_count",
                    "archived",
                    "created_at",
                    "updated_at",
                    "pushed_at",
                ] as &[&str],
            ),
            (
                "issues",
                include_str!("fixtures/github/contracts/list_repository_issues.json"),
                &["created_at", "updated_at", "closed_at"],
            ),
            (
                "issue_comments",
                include_str!("fixtures/github/contracts/list_issue_comments.json"),
                &["author_login"],
            ),
            (
                "pull_requests",
                include_str!("fixtures/github/contracts/list_pull_requests.json"),
                &[
                    "head_ref",
                    "base_ref",
                    "created_at",
                    "updated_at",
                    "closed_at",
                    "merged_at",
                ],
            ),
            (
                "reviews",
                include_str!("fixtures/github/contracts/list_pull_request_reviews.json"),
                &["author_login"],
            ),
            (
                "commits",
                include_str!("fixtures/github/contracts/list_commits.json"),
                &["message", "author_name", "authored_at", "committed_at"],
            ),
            (
                "workflow_runs",
                include_str!("fixtures/github/contracts/list_workflow_runs.json"),
                &["created_at", "updated_at"],
            ),
            (
                "releases",
                include_str!("fixtures/github/contracts/list_releases.json"),
                &[],
            ),
        ] {
            let t = table(short);
            assert_eq!(
                fingerprint_uncovered_columns(contract, t.row_path, t.fields),
                expected,
                "fingerprint coverage changed for {short}"
            );
        }
    }

    #[test]
    fn pinned_fingerprints_match_the_reconciled_contracts() {
        // Pin <-> captured-contract lock, through the SAME function
        // registration uses. On mismatch this prints the actual hashes —
        // which is also how the pins are (re)taken after an upstream
        // upgrade.
        use crate::sources::providers::open_connector::action_registry::fingerprint_schema;
        let contracts = [
            (
                "repositories",
                include_str!("fixtures/github/contracts/list_my_repositories.json"),
            ),
            (
                "issues",
                include_str!("fixtures/github/contracts/list_repository_issues.json"),
            ),
            (
                "issue_comments",
                include_str!("fixtures/github/contracts/list_issue_comments.json"),
            ),
            (
                "pull_requests",
                include_str!("fixtures/github/contracts/list_pull_requests.json"),
            ),
            (
                "reviews",
                include_str!("fixtures/github/contracts/list_pull_request_reviews.json"),
            ),
            (
                "commits",
                include_str!("fixtures/github/contracts/list_commits.json"),
            ),
            (
                "workflow_runs",
                include_str!("fixtures/github/contracts/list_workflow_runs.json"),
            ),
            (
                "releases",
                include_str!("fixtures/github/contracts/list_releases.json"),
            ),
        ];
        let mut mismatches = Vec::new();
        for (short, contract) in contracts {
            let schema: Value = serde_json::from_str(contract).expect("contract fixture parses");
            let actual = fingerprint_schema(Some(&schema));
            let t = table(short);
            if t.expected_fingerprint != Some(actual.as_str()) {
                mismatches.push(format!(
                    "{}: pinned {:?}, contract fixture hashes to {actual}",
                    t.id, t.expected_fingerprint
                ));
            }
        }
        assert!(mismatches.is_empty(), "{}", mismatches.join("\n"));
    }

    #[tokio::test]
    async fn drifted_contract_fails_registration_not_the_scan() {
        // The pin's refusal side: a gateway whose discovered output schema
        // differs from the captured contract must be refused at
        // REGISTRATION, table and action named. (Every other e2e proves
        // the pass side via github_discovery's captured contracts.)
        let gateway = MockGateway::start(|req| {
            if req.method == "GET" && req.path == "/v1/health" {
                return MockResponse::ok("{}");
            }
            if req.method == "GET" && req.path.starts_with("/v1/actions/") {
                return MockResponse::ok(&discovery_ok("{}", r#"{"type": "object"}"#, true, None));
            }
            MockResponse::new(404, "{}")
        })
        .await;
        let _token = testutil::EnvVarGuard::set("SKARDI_TEST_OC_GITHUB_DRIFT", "test-token");
        let config: OpenConnectorConfig = serde_yaml::from_str(
            r#"
runtime_token_env: SKARDI_TEST_OC_GITHUB_DRIFT
bindings:
  - name: gh
    source_pack: github
    resource: { owner: acme, repo: widgets }
    tables: [issues]
"#,
        )
        .expect("config parses");
        let mut ctx = SessionContext::new();
        let gateways = OpenConnectorGateways::default();
        let err = register_open_connector_tables(
            &mut ctx,
            "saas",
            &gateway.url,
            Some(&config),
            false,
            HierarchyLevel::Catalog,
            Some(&gateways),
        )
        .await
        .expect_err("a drifted contract must fail registration");
        let message = err.to_string();
        assert!(
            message.contains("github.issues")
                && message.contains("github.list_repository_issues")
                && message.contains("fingerprint mismatch"),
            "table, action, and cause are named: {message}"
        );
    }

    /// Look up a table by short name; the assets are test-pinned to parse.
    fn table(
        short: &str,
    ) -> &'static crate::sources::providers::open_connector::source_pack::SourcePackTable {
        pack()
            .expect("embedded asset is test-pinned to parse")
            .tables
            .iter()
            .find(|t| t.id.rsplit('.').next() == Some(short))
            .unwrap_or_else(|| panic!("table {short}"))
    }

    // ── Contract tests: bundled redacted fixtures are the build-time
    // conversion contract (null-bearing, nested, empty, extra upstream
    // fields, and a schema mismatch per the source-pack admission gate). ─

    /// Convert one bundled fixture page through a table's declared contract.
    fn convert_fixture(table: &SourcePackTable, fixture: &str) -> RecordBatch {
        let page: serde_json::Value = serde_json::from_str(fixture).expect("fixture parses");
        let rows = RowPath::parse(table.row_path)
            .expect("row path")
            .rows(&page, 1)
            .expect("row array");
        RowConverter::new(table.fields)
            .expect("converter")
            .convert(rows, 1)
            .expect("fixture converts")
    }

    fn strings<'a>(batch: &'a RecordBatch, column: &str) -> &'a StringArray {
        batch
            .column_by_name(column)
            .unwrap_or_else(|| panic!("column {column}"))
            .as_any()
            .downcast_ref()
            .expect("Utf8 column")
    }

    fn u64s<'a>(batch: &'a RecordBatch, column: &str) -> &'a UInt64Array {
        batch
            .column_by_name(column)
            .unwrap_or_else(|| panic!("column {column}"))
            .as_any()
            .downcast_ref()
            .expect("UInt64 column")
    }

    fn bools<'a>(batch: &'a RecordBatch, column: &str) -> &'a BooleanArray {
        batch
            .column_by_name(column)
            .unwrap_or_else(|| panic!("column {column}"))
            .as_any()
            .downcast_ref()
            .expect("Boolean column")
    }

    fn timestamps<'a>(batch: &'a RecordBatch, column: &str) -> &'a TimestampMillisecondArray {
        batch
            .column_by_name(column)
            .unwrap_or_else(|| panic!("column {column}"))
            .as_any()
            .downcast_ref()
            .expect("Timestamp column")
    }

    fn string_list(batch: &RecordBatch, column: &str, row: usize) -> Vec<String> {
        let lists: &ListArray = batch
            .column_by_name(column)
            .unwrap_or_else(|| panic!("column {column}"))
            .as_any()
            .downcast_ref()
            .expect("List column");
        let values = lists.value(row);
        let values = values
            .as_any()
            .downcast_ref::<StringArray>()
            .expect("Utf8 items");
        (0..values.len())
            .map(|i| values.value(i).to_string())
            .collect()
    }

    #[test]
    fn issues_fixture_converts_with_nulls_and_lists() {
        let batch = convert_fixture(table("issues"), include_str!("fixtures/github/issues.json"));
        assert_eq!(batch.num_rows(), 3);

        assert_eq!(u64s(&batch, "id").value(0), 101);
        assert_eq!(u64s(&batch, "number").value(2), 3);
        assert_eq!(
            strings(&batch, "title").value(0),
            "Scan panics on empty page"
        );
        assert_eq!(strings(&batch, "state").value(1), "closed");

        // JSON null body and null `user` parent become SQL NULL.
        assert!(strings(&batch, "body").is_null(1));
        assert_eq!(strings(&batch, "author_login").value(0), "octocat");
        assert!(strings(&batch, "author_login").is_null(1));

        // Object lists pluck the declared key; empty arrays stay empty lists.
        assert_eq!(
            string_list(&batch, "assignees", 0),
            vec!["octocat", "hubot"]
        );
        assert!(string_list(&batch, "assignees", 1).is_empty());
        assert_eq!(string_list(&batch, "labels", 0), vec!["bug", "p1"]);

        // Timestamps parse; closed_at is NULL while open.
        assert_eq!(
            timestamps(&batch, "created_at").value(0),
            1_767_225_600_000,
            "2026-01-01T00:00:00Z"
        );
        assert!(timestamps(&batch, "closed_at").is_null(0));
        assert!(!timestamps(&batch, "closed_at").is_null(1));
    }

    #[test]
    fn issues_mismatch_fixture_fails_with_the_targeted_error() {
        // Admission-gate schema-mismatch fixture: upstream turning a declared
        // integer into a string is incompatible drift and must fail the scan
        // with the full (column, page, row, expected, found-kind) identity —
        // never a quiet null, and never the offending value itself.
        let page: serde_json::Value =
            serde_json::from_str(include_str!("fixtures/github/issues_type_mismatch.json"))
                .expect("fixture parses");
        let rows = RowPath::parse(table("issues").row_path)
            .expect("row path")
            .rows(&page, 1)
            .expect("row array");
        let err = RowConverter::new(table("issues").fields)
            .expect("converter")
            .convert(rows, 1)
            .expect_err("a string where UInt64 is declared must fail conversion");
        match err {
            OpenConnectorError::ConversionFailed {
                column,
                path,
                page,
                row,
                expected,
                found,
            } => {
                assert_eq!(column, "number");
                assert_eq!(path, "$.number");
                assert_eq!(page, 1);
                assert_eq!(
                    row, 1,
                    "the valid first row converts; the error names the bad row"
                );
                assert_eq!(expected, "non-negative integer");
                assert_eq!(found, "string");
            }
            other => panic!("expected ConversionFailed, got {other}"),
        }
    }

    #[test]
    fn repositories_fixture_converts_with_nullable_metadata() {
        let batch = convert_fixture(
            table("repositories"),
            include_str!("fixtures/github/repositories.json"),
        );
        assert_eq!(batch.num_rows(), 2);
        assert_eq!(strings(&batch, "full_name").value(0), "acme/widgets");
        assert!(bools(&batch, "private").value(1));
        assert!(bools(&batch, "archived").value(1));
        assert_eq!(strings(&batch, "language").value(0), "Rust");
        assert!(strings(&batch, "language").is_null(1));
        assert!(strings(&batch, "description").is_null(1));
        assert!(timestamps(&batch, "pushed_at").is_null(1));
        assert_eq!(u64s(&batch, "stargazers_count").value(0), 42);
    }

    #[test]
    fn issue_comments_fixture_converts_with_null_author() {
        let batch = convert_fixture(
            table("issue_comments"),
            include_str!("fixtures/github/issue_comments.json"),
        );
        assert_eq!(batch.num_rows(), 2);
        assert_eq!(strings(&batch, "body").value(0), "Reproduced on main.");
        assert!(strings(&batch, "body").is_null(1));
        assert!(strings(&batch, "author_login").is_null(1));
    }

    #[test]
    fn pull_requests_fixture_converts_with_nested_refs_and_merge_state() {
        let batch = convert_fixture(
            table("pull_requests"),
            include_str!("fixtures/github/pull_requests.json"),
        );
        assert_eq!(batch.num_rows(), 2);
        assert_eq!(strings(&batch, "head_ref").value(0), "feature/dark-mode");
        assert_eq!(strings(&batch, "base_ref").value(0), "main");
        assert!(bools(&batch, "draft").value(0));
        assert!(timestamps(&batch, "merged_at").is_null(0), "open PR");
        assert!(!timestamps(&batch, "merged_at").is_null(1), "merged PR");
    }

    #[test]
    fn reviews_fixture_converts_with_null_bearing_row() {
        let batch = convert_fixture(
            table("reviews"),
            include_str!("fixtures/github/reviews.json"),
        );
        assert_eq!(batch.num_rows(), 2);
        assert_eq!(strings(&batch, "state").value(0), "APPROVED");
        assert!(strings(&batch, "author_login").is_null(1));
        assert!(strings(&batch, "commit_id").is_null(1));
        assert!(timestamps(&batch, "submitted_at").is_null(1));
    }

    #[test]
    fn commits_fixture_converts_with_null_github_account() {
        // The classic GitHub shape: `author` (the account) is JSON null for
        // unlinked commit emails, while git-level identity under `commit.*`
        // stays available.
        let batch = convert_fixture(
            table("commits"),
            include_str!("fixtures/github/commits.json"),
        );
        assert_eq!(batch.num_rows(), 2);
        assert_eq!(strings(&batch, "author_login").value(0), "octocat");
        assert!(strings(&batch, "author_login").is_null(1));
        assert_eq!(strings(&batch, "author_name").value(1), "Legacy Importer");
        assert_eq!(strings(&batch, "message").value(0), "feat: add dark mode");
        assert!(!timestamps(&batch, "committed_at").is_null(0));
    }

    #[test]
    fn workflow_runs_fixture_converts_with_in_progress_run() {
        let batch = convert_fixture(
            table("workflow_runs"),
            include_str!("fixtures/github/workflow_runs.json"),
        );
        assert_eq!(batch.num_rows(), 2);
        assert_eq!(strings(&batch, "conclusion").value(0), "success");
        assert!(
            strings(&batch, "conclusion").is_null(1),
            "conclusion is NULL while a run is in progress"
        );
        assert!(strings(&batch, "name").is_null(1));
        assert_eq!(strings(&batch, "status").value(1), "in_progress");
        assert_eq!(u64s(&batch, "run_number").value(0), 128);
    }

    #[test]
    fn releases_fixture_converts_with_unpublished_draft() {
        let batch = convert_fixture(
            table("releases"),
            include_str!("fixtures/github/releases.json"),
        );
        assert_eq!(batch.num_rows(), 2);
        assert_eq!(strings(&batch, "tag_name").value(0), "v1.2.0");
        assert!(bools(&batch, "draft").value(1));
        assert!(timestamps(&batch, "published_at").is_null(1), "draft");
        assert!(strings(&batch, "name").is_null(1));
        assert!(strings(&batch, "author_login").is_null(1));
    }

    #[test]
    fn every_table_converts_an_empty_page_and_keeps_its_schema() {
        for table in pack().expect("embedded asset parses").tables {
            let converter = RowConverter::new(table.fields).expect("converter");
            let batch = converter.convert(&[], 1).expect("empty page");
            assert_eq!(batch.num_rows(), 0, "{}", table.id);
            assert_eq!(
                batch.schema().fields().len(),
                table.fields.len(),
                "{} keeps its stable schema on empty results",
                table.id
            );
        }
    }

    // ── Integration tests: the issues table end to end through a mock
    // gateway — pagination, the state=all pin, Exact and Inexact pushdown,
    // the PR marker, LIMIT, and UDTF parity. ─────────────────────────────

    use crate::sources::hierarchy::HierarchyLevel;
    use crate::sources::providers::open_connector::testutil;
    use crate::sources::providers::open_connector::testutil::{
        MockGateway, MockResponse, RecordedRequest, discovery_ok, envelope_ok,
    };
    use crate::sources::providers::open_connector::{
        OpenConnectorConfig, OpenConnectorGateways, register_open_connector_tables,
        register_open_connector_udtfs,
    };
    use datafusion::prelude::SessionContext;
    use serde_json::{Value, json};

    /// One minimal issue row: only the non-null contract fields plus
    /// whatever the test cares about (missing nullable keys become NULL).
    fn issue(n: u64, state: &str, updated_at: &str) -> Value {
        json!({
            "id": n,
            "number": n,
            "title": format!("issue-{n}"),
            "state": state,
            "updated_at": updated_at
        })
    }

    /// Mock gateway serving `github.list_repository_issues` over `rows`:
    /// honors `state` exactly, pages at `perPage`, and deliberately
    /// IGNORES `since` — returning a superset is exactly what an Inexact
    /// mapping permits, and DataFusion must trim it.
    fn issues_handler(req: &RecordedRequest, rows: &[Value]) -> MockResponse {
        if req.method == "GET" && req.path == "/v1/health" {
            return MockResponse::ok("{}");
        }
        if req.method == "GET" && req.path == "/v1/actions/github.list_repository_issues" {
            return github_discovery(&req.path);
        }
        if req.method == "POST" && req.path == "/v1/actions/github.list_repository_issues" {
            let body: Value = serde_json::from_str(&req.body).unwrap_or_default();
            let input = body.get("input").cloned().unwrap_or_default();
            let page = input.get("page").and_then(Value::as_u64).unwrap_or(1) as usize;
            let per_page = input.get("perPage").and_then(Value::as_u64).unwrap_or(30) as usize;
            let state = input
                .get("state")
                .and_then(Value::as_str)
                .unwrap_or("open")
                .to_string();
            let matching: Vec<_> = rows
                .iter()
                .filter(|row| {
                    state == "all" || row.get("state").and_then(Value::as_str) == Some(&state)
                })
                .collect();
            let slice: Vec<_> = matching
                .iter()
                .skip((page - 1) * per_page)
                .take(per_page)
                .map(|row| (*row).clone())
                .collect();
            // The raw page length before any post-pagination filtering; the
            // stub does none, so it equals the slice length.
            let fetched = slice.len();
            return MockResponse::ok(&envelope_ok(
                &json!({"issues": slice, "pageInfo": {"fetched": fetched}}).to_string(),
            ));
        }
        MockResponse::new(404, "{}")
    }

    fn issues_config(token_env: &str) -> OpenConnectorConfig {
        serde_yaml::from_str(&format!(
            r#"
runtime_token_env: {token_env}
bindings:
  - name: gh
    source_pack: github
    resource: {{ owner: acme, repo: widgets }}
    tables: [issues]
"#
        ))
        .expect("parse config")
    }

    /// Register the gateway (catalog + UDTFs) against `rows`.
    async fn setup(rows: Vec<Value>, token_env: &str) -> (MockGateway, SessionContext) {
        let served = std::sync::Arc::new(rows);
        let gateway = {
            let served = std::sync::Arc::clone(&served);
            MockGateway::start(move |req| issues_handler(req, &served)).await
        };
        setup_with_gateway(gateway, token_env).await
    }

    /// Register the issues binding against an arbitrary gateway stub.
    async fn setup_with_gateway(
        gateway: MockGateway,
        token_env: &str,
    ) -> (MockGateway, SessionContext) {
        unsafe {
            std::env::set_var(token_env, "test-token");
        }
        let gateways = OpenConnectorGateways::default();
        let mut ctx = SessionContext::new();
        register_open_connector_tables(
            &mut ctx,
            "saas",
            &gateway.url,
            Some(&issues_config(token_env)),
            false,
            HierarchyLevel::Catalog,
            Some(&gateways),
        )
        .await
        .expect("gateway registration succeeds");
        unsafe {
            std::env::remove_var(token_env);
        }
        register_open_connector_udtfs(&ctx, gateways).expect("UDTF registration succeeds");
        (gateway, ctx)
    }

    async fn collect(ctx: &SessionContext, sql: &str) -> Vec<RecordBatch> {
        ctx.sql(sql)
            .await
            .expect("plan")
            .collect()
            .await
            .expect("collect")
    }

    fn rows_of(batches: &[RecordBatch]) -> usize {
        batches.iter().map(|b| b.num_rows()).sum()
    }

    fn execute_bodies(gateway: &MockGateway) -> Vec<String> {
        gateway
            .requests()
            .into_iter()
            .filter(|r| r.method == "POST")
            .map(|r| r.body)
            .collect()
    }

    /// 150 issues: odd numbers open, even numbers closed.
    fn many_issues() -> Vec<Value> {
        (1..=150)
            .map(|n| {
                issue(
                    n,
                    if n % 2 == 1 { "open" } else { "closed" },
                    "2026-01-01T00:00:00Z",
                )
            })
            .collect()
    }

    #[tokio::test]
    async fn full_scan_paginates_the_complete_collection() {
        let (gateway, ctx) = setup(many_issues(), "SKARDI_TEST_OC_GITHUB_SCAN").await;

        let batches = collect(&ctx, "SELECT count(*) AS n FROM saas.gh.issues").await;
        let count = u64s_i64(&batches[0], "n");
        assert_eq!(
            count, 150,
            "closed issues included, not GitHub's open-only default"
        );

        let bodies = execute_bodies(&gateway);
        assert_eq!(bodies.len(), 2, "150 rows at perPage=100 → 2 pages");
        assert!(bodies[0].contains(r#""page":1"#) && bodies[0].contains(r#""perPage":100"#));
        assert!(bodies[1].contains(r#""page":2"#));
        assert!(
            bodies.iter().all(|body| body.contains(r#""state":"all""#)),
            "the state=all pin makes the table the complete collection"
        );
        assert!(
            bodies
                .iter()
                .all(|body| body.contains(r#""owner":"acme""#)
                    && body.contains(r#""repo":"widgets""#)),
            "resource inputs ride on every request"
        );
    }

    /// Extract the single Int64 count value (count(*) output).
    fn u64s_i64(batch: &RecordBatch, column: &str) -> i64 {
        batch
            .column_by_name(column)
            .expect("count column")
            .as_any()
            .downcast_ref::<arrow::array::Int64Array>()
            .expect("Int64")
            .value(0)
    }

    #[tokio::test]
    async fn state_predicate_overrides_the_fixed_input_exactly() {
        let (gateway, ctx) = setup(many_issues(), "SKARDI_TEST_OC_GITHUB_STATE").await;

        let batches = collect(&ctx, "SELECT id FROM saas.gh.issues WHERE state = 'open'").await;
        assert_eq!(rows_of(&batches), 75, "odd-numbered issues are open");
        assert!(
            execute_bodies(&gateway).iter().all(
                |body| body.contains(r#""state":"open""#) && !body.contains(r#""state":"all""#)
            ),
            "the pushed state predicate replaces the state=all pin"
        );
    }

    #[tokio::test]
    async fn pull_requests_state_pin_and_override_run_end_to_end() {
        // pull_requests is the second (and only other) table declaring the
        // state=all pin plus a pushed state filter; its pin/override path
        // must run through the real scan engine, not ride on the issues
        // coverage alone.
        let rows: Vec<Value> = (1..=4)
            .map(|number| {
                json!({
                    "id": number * 10,
                    "number": number,
                    "title": format!("pr-{number}"),
                    "state": if number % 2 == 1 { "open" } else { "closed" }
                })
            })
            .collect();
        let served = std::sync::Arc::new(rows);
        let gateway = {
            let served = std::sync::Arc::clone(&served);
            MockGateway::start(move |req| {
                if req.method == "GET" && req.path == "/v1/health" {
                    return MockResponse::ok("{}");
                }
                if req.method == "GET" && req.path == "/v1/actions/github.list_pull_requests" {
                    return github_discovery(&req.path);
                }
                if req.method == "POST" && req.path == "/v1/actions/github.list_pull_requests" {
                    let body: Value = serde_json::from_str(&req.body).unwrap_or_default();
                    let input = body.get("input").cloned().unwrap_or_default();
                    let state = input
                        .get("state")
                        .and_then(Value::as_str)
                        .unwrap_or("open")
                        .to_string();
                    let slice: Vec<_> = served
                        .iter()
                        .filter(|row| {
                            state == "all"
                                || row.get("state").and_then(Value::as_str) == Some(&state)
                        })
                        .cloned()
                        .collect();
                    return MockResponse::ok(&envelope_ok(
                        &json!({"pull_requests": slice}).to_string(),
                    ));
                }
                MockResponse::new(404, "{}")
            })
            .await
        };

        let token_env = "SKARDI_TEST_OC_GITHUB_PR_STATE";
        unsafe {
            std::env::set_var(token_env, "test-token");
        }
        let config: OpenConnectorConfig = serde_yaml::from_str(&format!(
            r#"
runtime_token_env: {token_env}
bindings:
  - name: gh
    source_pack: github
    resource: {{ owner: acme, repo: widgets }}
    tables: [pull_requests]
"#
        ))
        .expect("parse config");
        let mut ctx = SessionContext::new();
        register_open_connector_tables(
            &mut ctx,
            "saas",
            &gateway.url,
            Some(&config),
            false,
            HierarchyLevel::Catalog,
            None,
        )
        .await
        .expect("gateway registration succeeds");
        unsafe {
            std::env::remove_var(token_env);
        }

        // Without a predicate, the state=all pin reads the complete
        // collection (GitHub's endpoint defaults to open PRs only).
        let batches = collect(&ctx, "SELECT id FROM saas.gh.pull_requests").await;
        assert_eq!(rows_of(&batches), 4, "the pin exposes closed PRs too");
        assert!(
            execute_bodies(&gateway)
                .iter()
                .all(|body| body.contains(r#""state":"all""#)),
            "the fixed input rides every request"
        );

        // A pushed state predicate replaces the pin in the action input.
        let batches = collect(
            &ctx,
            "SELECT id FROM saas.gh.pull_requests WHERE state = 'open'",
        )
        .await;
        assert_eq!(rows_of(&batches), 2, "PRs 1 and 3 are open");
        let bodies = execute_bodies(&gateway);
        let last = bodies.last().expect("an execute call");
        assert!(
            last.contains(r#""state":"open""#) && !last.contains(r#""state":"all""#),
            "the pushed state predicate replaces the state=all pin: {last}"
        );
    }

    #[tokio::test]
    async fn since_pushdown_is_inexact_and_reapplied_locally() {
        // The gateway ignores `since` entirely — the harshest legal Inexact
        // provider (a full superset). DataFusion must trim it back to the
        // predicate, so the boundary row stays and older rows never leak.
        let rows = vec![
            issue(1, "open", "2026-01-01T00:00:00Z"),
            issue(2, "open", "2026-01-02T00:00:00Z"),
            issue(3, "open", "2026-01-03T00:00:00Z"),
        ];
        let (gateway, ctx) = setup(rows, "SKARDI_TEST_OC_GITHUB_SINCE").await;

        let batches = collect(
            &ctx,
            "SELECT id FROM saas.gh.issues \
             WHERE updated_at >= TIMESTAMP '2026-01-02T00:00:00Z' ORDER BY id",
        )
        .await;
        assert_eq!(
            rows_of(&batches),
            2,
            "rows 2 and 3: the superset row 1 is re-filtered, the boundary row 2 kept"
        );
        assert!(
            execute_bodies(&gateway)
                .iter()
                .all(|body| body.contains(r#""since":"2026-01-02T00:00:00Z""#)),
            "the predicate still narrows the fetch as GitHub's since"
        );
    }

    /// Register one non-issues table against a stub gateway that serves the
    /// given rows for `action_id` under `row_key`, recording every request.
    async fn setup_table(
        table: &'static str,
        action_id: &'static str,
        row_key: &'static str,
        rows: Vec<Value>,
        token_env: &'static str,
    ) -> (MockGateway, SessionContext) {
        let served = std::sync::Arc::new(rows);
        let gateway = {
            let served = std::sync::Arc::clone(&served);
            MockGateway::start(move |req| {
                if req.method == "GET" && req.path == "/v1/health" {
                    return MockResponse::ok("{}");
                }
                if req.method == "GET" && req.path == format!("/v1/actions/{action_id}") {
                    return github_discovery(&req.path);
                }
                if req.method == "POST" && req.path == format!("/v1/actions/{action_id}") {
                    return MockResponse::ok(&envelope_ok(
                        &json!({row_key: served.as_slice()}).to_string(),
                    ));
                }
                MockResponse::new(404, "{}")
            })
            .await
        };

        unsafe {
            std::env::set_var(token_env, "test-token");
        }
        let config: OpenConnectorConfig = serde_yaml::from_str(&format!(
            r#"
runtime_token_env: {token_env}
bindings:
  - name: gh
    source_pack: github
    resource: {{ owner: acme, repo: widgets }}
    tables: [{table}]
"#
        ))
        .expect("parse config");
        let mut ctx = SessionContext::new();
        register_open_connector_tables(
            &mut ctx,
            "saas",
            &gateway.url,
            Some(&config),
            false,
            HierarchyLevel::Catalog,
            None,
        )
        .await
        .expect("gateway registration succeeds");
        unsafe {
            std::env::remove_var(token_env);
        }
        (gateway, ctx)
    }

    #[tokio::test]
    async fn commits_time_predicate_is_never_pushed_as_since() {
        // The pack deliberately does NOT map committed_at to the commits
        // endpoint's `since`: GitHub documents it as commits strictly
        // *after* the date, so pushing a `>=` predicate could drop the
        // boundary commit unrecoverably. This guards the decision — if a
        // future mapping is added by accident, the body assertion fails.
        let commit =
            |sha: &str, date: &str| json!({"sha": sha, "commit": {"committer": {"date": date}}});
        let rows = vec![
            commit("aaa", "2026-01-01T00:00:00Z"),
            commit("bbb", "2026-01-02T00:00:00Z"),
            commit("ccc", "2026-01-03T00:00:00Z"),
        ];
        let (gateway, ctx) = setup_table(
            "commits",
            "github.list_commits",
            "commits",
            rows,
            "SKARDI_TEST_OC_GITHUB_COMMITS_SINCE",
        )
        .await;

        let batches = collect(
            &ctx,
            "SELECT sha FROM saas.gh.commits \
             WHERE committed_at >= TIMESTAMP '2026-01-02T00:00:00Z'",
        )
        .await;
        assert_eq!(
            rows_of(&batches),
            2,
            "DataFusion filters locally: boundary commit bbb stays"
        );
        let bodies = execute_bodies(&gateway);
        assert!(!bodies.is_empty());
        assert!(
            bodies.iter().all(|body| {
                serde_json::from_str::<Value>(body).expect("request body is JSON")["input"]
                    .get("since")
                    .is_none()
            }),
            "committed_at must never reach the strictly-after `since`: {bodies:?}"
        );
    }

    #[tokio::test]
    async fn workflow_runs_status_predicate_is_never_pushed() {
        // `status` is deliberately unmapped: GitHub's status parameter also
        // matches conclusion values, so an Exact claim would be unfaithful.
        // Guard the decision the same way as commits/since.
        let run = |id: u64, status: &str| json!({"id": id, "status": status});
        let rows = vec![
            run(1, "completed"),
            run(2, "in_progress"),
            run(3, "completed"),
        ];
        let (gateway, ctx) = setup_table(
            "workflow_runs",
            "github.list_workflow_runs",
            "workflow_runs",
            rows,
            "SKARDI_TEST_OC_GITHUB_RUNS_STATUS",
        )
        .await;

        let batches = collect(
            &ctx,
            "SELECT id FROM saas.gh.workflow_runs WHERE status = 'completed'",
        )
        .await;
        assert_eq!(rows_of(&batches), 2, "DataFusion filters locally");
        let bodies = execute_bodies(&gateway);
        assert!(!bodies.is_empty());
        assert!(
            bodies.iter().all(|body| {
                serde_json::from_str::<Value>(body).expect("request body is JSON")["input"]
                    .get("status")
                    .is_none()
            }),
            "the status predicate must never reach the provider: {bodies:?}"
        );
    }

    #[tokio::test]
    async fn binding_missing_a_required_resource_fails_before_discovery() {
        // owner/repo are the pack's declared resource contract; a binding
        // without `repo` must fail registration with the targeted error —
        // and before any action-discovery request leaves the process. This
        // is also the registration-time MissingResourceInput path's only
        // direct pin (the UDTF path asserts it separately).
        let gateway = MockGateway::start(|req| {
            if req.method == "GET" && req.path == "/v1/health" {
                return MockResponse::ok("{}");
            }
            MockResponse::new(404, "{}")
        })
        .await;

        let token_env = "SKARDI_TEST_OC_GITHUB_MISSING_RESOURCE";
        unsafe {
            std::env::set_var(token_env, "test-token");
        }
        let config: OpenConnectorConfig = serde_yaml::from_str(&format!(
            r#"
runtime_token_env: {token_env}
bindings:
  - name: gh
    source_pack: github
    resource: {{ owner: acme }}
    tables: [issues]
"#
        ))
        .expect("parse config");
        let mut ctx = SessionContext::new();
        let result = register_open_connector_tables(
            &mut ctx,
            "saas",
            &gateway.url,
            Some(&config),
            false,
            HierarchyLevel::Catalog,
            None,
        )
        .await;
        unsafe {
            std::env::remove_var(token_env);
        }

        let err = result
            .unwrap_err()
            .downcast::<crate::sources::providers::open_connector::OpenConnectorError>()
            .unwrap();
        assert!(matches!(
            err,
            crate::sources::providers::open_connector::OpenConnectorError::MissingResourceInput {
                ref binding,
                ref key,
            } if binding == "gh" && key == "repo"
        ));
        assert!(
            gateway
                .requests()
                .iter()
                .all(|request| request.path == "/v1/health"),
            "resource enforcement precedes every discovery call"
        );
    }

    /// Register `workflow_runs` against a stub serving `total` runs through
    /// GitHub's wrapped envelope (`{total_count, workflow_runs}`), sliced by
    /// page/perPage.
    async fn setup_workflow_runs(
        total: usize,
        token_env: &'static str,
    ) -> (MockGateway, SessionContext) {
        let gateway = MockGateway::start(move |req| {
            if req.method == "GET" && req.path == "/v1/health" {
                return MockResponse::ok("{}");
            }
            if req.method == "GET" && req.path == "/v1/actions/github.list_workflow_runs" {
                return github_discovery(&req.path);
            }
            if req.method == "POST" && req.path == "/v1/actions/github.list_workflow_runs" {
                let body: Value = serde_json::from_str(&req.body).unwrap_or_default();
                let input = body.get("input").cloned().unwrap_or_default();
                let page = input.get("page").and_then(Value::as_u64).unwrap_or(1) as usize;
                let per_page = input.get("perPage").and_then(Value::as_u64).unwrap_or(30) as usize;
                let slice: Vec<Value> = (1..=total)
                    .map(|id| json!({"id": id, "status": "completed"}))
                    .skip((page - 1) * per_page)
                    .take(per_page)
                    .collect();
                return MockResponse::ok(&envelope_ok(
                    &json!({"total_count": total, "workflow_runs": slice}).to_string(),
                ));
            }
            MockResponse::new(404, "{}")
        })
        .await;

        unsafe {
            std::env::set_var(token_env, "test-token");
        }
        let config: OpenConnectorConfig = serde_yaml::from_str(&format!(
            r#"
runtime_token_env: {token_env}
bindings:
  - name: gh
    source_pack: github
    resource: {{ owner: acme, repo: widgets }}
    tables: [workflow_runs]
"#
        ))
        .expect("parse config");
        let mut ctx = SessionContext::new();
        register_open_connector_tables(
            &mut ctx,
            "saas",
            &gateway.url,
            Some(&config),
            false,
            HierarchyLevel::Catalog,
            None,
        )
        .await
        .expect("gateway registration succeeds");
        unsafe {
            std::env::remove_var(token_env);
        }
        (gateway, ctx)
    }

    #[tokio::test]
    async fn wrapped_envelope_with_total_count_paginates_and_terminates() {
        // workflow_runs is the pack's one structurally different response
        // shape: the row array sits beside a sibling `total_count`
        // (GitHub's actual envelope). Pagination and short-page termination
        // must run end to end against that shape — the sibling key is
        // ignored by the row path and must never confuse the scan.
        let (gateway, ctx) = setup_workflow_runs(150, "SKARDI_TEST_OC_GITHUB_RUNS_ENVELOPE").await;

        let batches = collect(&ctx, "SELECT id FROM saas.gh.workflow_runs").await;
        assert_eq!(rows_of(&batches), 150, "the sibling total_count is inert");

        let bodies = execute_bodies(&gateway);
        assert_eq!(
            bodies.len(),
            2,
            "150 runs at perPage=100: a full page, then a short page that terminates"
        );
        assert!(bodies[0].contains(r#""page":1"#), "{}", bodies[0]);
        assert!(bodies[1].contains(r#""page":2"#), "{}", bodies[1]);
    }

    #[tokio::test]
    async fn exact_page_boundary_terminates_on_the_empty_page() {
        // 100 rows == per_page: GitHub cannot signal completion on the full
        // page, so the engine must spend one more request and terminate on
        // the EMPTY page — the realistic large-repo path, previously covered
        // only by the pagination unit tests, never through a pack scan.
        let (gateway, ctx) = setup_workflow_runs(100, "SKARDI_TEST_OC_GITHUB_RUNS_BOUNDARY").await;

        let batches = collect(&ctx, "SELECT id FROM saas.gh.workflow_runs").await;
        assert_eq!(rows_of(&batches), 100, "every boundary row is emitted");

        let bodies = execute_bodies(&gateway);
        assert_eq!(
            bodies.len(),
            2,
            "a full page cannot terminate: the empty page 2 is fetched and ends the scan"
        );
        assert!(bodies[0].contains(r#""page":1"#), "{}", bodies[0]);
        assert!(bodies[1].contains(r#""page":2"#), "{}", bodies[1]);
    }

    #[tokio::test]
    async fn out_of_domain_state_yields_empty_not_the_provider_default() {
        // The motivating case for classifying `state` as Inexact: a provider
        // that silently ignores an out-of-domain enum ('merged') and returns
        // its default listing. Under Exact those rows would leak back as the
        // "exact" answer; Inexact re-applies the predicate locally, so the
        // query is correctly empty. setup_table's stub is exactly such a
        // provider — it serves its rows regardless of the state input.
        let rows = vec![
            json!({"id": 1, "number": 1, "title": "a", "state": "open"}),
            json!({"id": 2, "number": 2, "title": "b", "state": "open"}),
        ];
        let (gateway, ctx) = setup_table(
            "pull_requests",
            "github.list_pull_requests",
            "pull_requests",
            rows,
            "SKARDI_TEST_OC_GITHUB_PR_OOD_STATE",
        )
        .await;

        let batches = collect(
            &ctx,
            "SELECT id FROM saas.gh.pull_requests WHERE state = 'merged'",
        )
        .await;
        assert_eq!(
            rows_of(&batches),
            0,
            "the provider's default listing must not leak into an empty query"
        );
        // The predicate still narrows the fetch on providers that honor it.
        let bodies = execute_bodies(&gateway);
        assert!(!bodies.is_empty());
        assert!(
            bodies
                .iter()
                .all(|body| body.contains(r#""state":"merged""#)),
            "the push still happens: {bodies:?}"
        );
    }

    #[test]
    fn issues_declares_no_pull_request_marker() {
        // Negative-space guard: the Open Connector action already filters
        // pull requests out of `list_repository_issues`, so a `pull_request`
        // marker column could never be non-NULL — the table must not
        // declare one, and PRs are reached through their own table.
        assert!(
            table("issues")
                .fields
                .iter()
                .all(|f| f.name != "pull_request"),
            "issues is pure issues under the OC contract; a marker column \
             would be permanently NULL"
        );
    }

    #[tokio::test]
    async fn limit_stops_github_pagination_after_the_first_page() {
        let (gateway, ctx) = setup(many_issues(), "SKARDI_TEST_OC_GITHUB_LIMIT").await;

        let batches = collect(&ctx, "SELECT id FROM saas.gh.issues LIMIT 5").await;
        assert_eq!(rows_of(&batches), 5);
        assert_eq!(
            execute_bodies(&gateway).len(),
            1,
            "LIMIT 5 must stop after the first page"
        );
    }

    #[tokio::test]
    async fn query_udtf_matches_the_yaml_bound_issues_table() {
        let (_gateway, ctx) = setup(many_issues(), "SKARDI_TEST_OC_GITHUB_UDTF").await;

        let from_table = collect(
            &ctx,
            "SELECT id, title, state FROM saas.gh.issues ORDER BY id",
        )
        .await;
        let from_udtf = collect(
            &ctx,
            r#"SELECT id, title, state
               FROM open_connector_query('saas', 'github.issues',
                                         '{"owner":"acme","repo":"widgets"}')
               ORDER BY id"#,
        )
        .await;
        assert_eq!(from_table[0].schema(), from_udtf[0].schema());
        assert_eq!(
            arrow::util::pretty::pretty_format_batches(&from_table)
                .unwrap()
                .to_string(),
            arrow::util::pretty::pretty_format_batches(&from_udtf)
                .unwrap()
                .to_string()
        );
    }

    #[tokio::test]
    async fn shared_binding_sends_each_table_only_its_declared_resources() {
        // Live-gateway-confirmed failure mode: a binding's resource map must
        // NOT be forwarded wholesale. `github.list_my_repositories` declares
        // no resource inputs and its strict schema rejects `owner`/`repo`
        // (`additionalProperties: false`) — yet repositories must be able to
        // share one binding with the repo-scoped tables. Each table's
        // requests carry exactly the keys it declares.
        let gateway = MockGateway::start(|req| {
            if req.method == "GET" && req.path == "/v1/health" {
                return MockResponse::ok("{}");
            }
            if req.method == "GET" && req.path.starts_with("/v1/actions/") {
                return github_discovery(&req.path);
            }
            if req.method == "POST" && req.path == "/v1/actions/github.list_my_repositories" {
                let body: Value = serde_json::from_str(&req.body).unwrap_or_default();
                let input = body.get("input").cloned().unwrap_or_default();
                // Mirror the live gateway's strictness so a regression fails
                // this test the way it fails production: HTTP 400.
                if input.get("owner").is_some() || input.get("repo").is_some() {
                    return MockResponse::new(
                        400,
                        &crate::sources::providers::open_connector::testutil::envelope_err(
                            "invalid_input",
                            "Action input does not match the action schema.",
                        ),
                    );
                }
                return MockResponse::ok(&envelope_ok(r#"{"repositories": []}"#));
            }
            if req.method == "POST" && req.path == "/v1/actions/github.list_repository_issues" {
                return MockResponse::ok(&envelope_ok(
                    r#"{"issues": [], "pageInfo": {"fetched": 0}}"#,
                ));
            }
            MockResponse::new(404, "{}")
        })
        .await;

        let token_env = "SKARDI_TEST_OC_GITHUB_SHARED_BINDING";
        unsafe {
            std::env::set_var(token_env, "test-token");
        }
        let config: OpenConnectorConfig = serde_yaml::from_str(&format!(
            r#"
runtime_token_env: {token_env}
bindings:
  - name: gh
    source_pack: github
    resource: {{ owner: acme, repo: widgets }}
    tables: [repositories, issues]
"#
        ))
        .expect("parse config");
        let mut ctx = SessionContext::new();
        register_open_connector_tables(
            &mut ctx,
            "saas",
            &gateway.url,
            Some(&config),
            false,
            HierarchyLevel::Catalog,
            None,
        )
        .await
        .expect("owner/repo are consumed by issues, so the binding is valid");
        unsafe {
            std::env::remove_var(token_env);
        }

        let batches = collect(&ctx, "SELECT name FROM saas.gh.repositories").await;
        assert_eq!(rows_of(&batches), 0, "strict stub accepted the request");
        let batches = collect(&ctx, "SELECT id FROM saas.gh.issues").await;
        assert_eq!(rows_of(&batches), 0);

        let input_for = |action: &str| -> Value {
            let body = gateway
                .requests()
                .into_iter()
                .find(|r| r.method == "POST" && r.path.contains(action))
                .unwrap_or_else(|| panic!("{action} was executed"))
                .body;
            serde_json::from_str::<Value>(&body).expect("JSON body")["input"].clone()
        };
        let repos_input = input_for("github.list_my_repositories");
        assert!(
            repos_input.get("owner").is_none() && repos_input.get("repo").is_none(),
            "repositories declares no resources, so none are sent: {repos_input}"
        );
        let issues_input = input_for("github.list_repository_issues");
        assert_eq!(issues_input.get("owner"), Some(&Value::from("acme")));
        assert_eq!(issues_input.get("repo"), Some(&Value::from("widgets")));
    }

    #[tokio::test]
    async fn unconsumed_resource_key_fails_registration() {
        // A key no bound table declares is dead configuration (requests
        // never carry it) — almost certainly a typo, so registration fails
        // loudly instead of silently dropping it.
        let gateway = MockGateway::start(|req| {
            if req.method == "GET" && req.path == "/v1/health" {
                return MockResponse::ok("{}");
            }
            MockResponse::new(404, "{}")
        })
        .await;

        let token_env = "SKARDI_TEST_OC_GITHUB_UNKNOWN_KEY";
        unsafe {
            std::env::set_var(token_env, "test-token");
        }
        let config: OpenConnectorConfig = serde_yaml::from_str(&format!(
            r#"
runtime_token_env: {token_env}
bindings:
  - name: gh
    source_pack: github
    resource: {{ owner: acme, repo: widgets, ownr: typo }}
    tables: [issues]
"#
        ))
        .expect("parse config");
        let mut ctx = SessionContext::new();
        let err = register_open_connector_tables(
            &mut ctx,
            "saas",
            &gateway.url,
            Some(&config),
            false,
            HierarchyLevel::Catalog,
            None,
        )
        .await
        .expect_err("unconsumed key must fail registration");
        unsafe {
            std::env::remove_var(token_env);
        }
        let message = format!("{err:#}");
        assert!(
            message.contains("resource key 'ownr'") && message.contains("'gh'"),
            "error names the binding and the dead key: {message}"
        );
    }

    #[tokio::test]
    async fn numeric_yaml_resource_values_reach_the_gateway_as_numbers() {
        // A YAML binding's numeric resource must arrive as a JSON number —
        // the same value an equivalent UDTF resource JSON sends — never a
        // string. Both numeric-resource consumers are covered: issue_comments
        // (issueNumber) and reviews (pullNumber).
        let gateway = MockGateway::start(|req| {
            if req.method == "GET" && req.path == "/v1/health" {
                return MockResponse::ok("{}");
            }
            if req.method == "GET" && req.path.starts_with("/v1/actions/") {
                return github_discovery(&req.path);
            }
            if req.method == "POST" && req.path == "/v1/actions/github.list_issue_comments" {
                return MockResponse::ok(&envelope_ok(
                    &serde_json::json!({"comments": []}).to_string(),
                ));
            }
            if req.method == "POST" && req.path == "/v1/actions/github.list_pull_request_reviews" {
                return MockResponse::ok(&envelope_ok(
                    &serde_json::json!({"reviews": []}).to_string(),
                ));
            }
            MockResponse::new(404, "{}")
        })
        .await;

        let token_env = "SKARDI_TEST_OC_GITHUB_NUMERIC_RESOURCE";
        unsafe {
            std::env::set_var(token_env, "test-token");
        }
        let config: OpenConnectorConfig = serde_yaml::from_str(&format!(
            r#"
runtime_token_env: {token_env}
bindings:
  - name: gh
    source_pack: github
    resource: {{ owner: acme, repo: widgets, issueNumber: 42 }}
    tables: [issue_comments]
  - name: ghr
    source_pack: github
    resource: {{ owner: acme, repo: widgets, pullNumber: 7 }}
    tables: [reviews]
"#
        ))
        .expect("parse config");
        let mut ctx = SessionContext::new();
        register_open_connector_tables(
            &mut ctx,
            "saas",
            &gateway.url,
            Some(&config),
            false,
            HierarchyLevel::Catalog,
            None,
        )
        .await
        .expect("gateway registration succeeds");
        unsafe {
            std::env::remove_var(token_env);
        }

        let batches = collect(&ctx, "SELECT id FROM saas.gh.issue_comments").await;
        assert_eq!(rows_of(&batches), 0, "stub serves an empty collection");
        let batches = collect(&ctx, "SELECT id FROM saas.ghr.reviews").await;
        assert_eq!(rows_of(&batches), 0, "stub serves an empty collection");

        let bodies_for = |action: &str| -> Vec<String> {
            gateway
                .requests()
                .into_iter()
                .filter(|r| r.method == "POST" && r.path.contains(action))
                .map(|r| r.body)
                .collect()
        };
        for (action, number_json, stringified) in [
            (
                "github.list_issue_comments",
                r#""issueNumber":42"#,
                r#""issueNumber":"42""#,
            ),
            (
                "github.list_pull_request_reviews",
                r#""pullNumber":7"#,
                r#""pullNumber":"7""#,
            ),
        ] {
            let bodies = bodies_for(action);
            assert!(!bodies.is_empty(), "{action} was executed");
            assert!(
                bodies.iter().all(|body| body.contains(number_json)),
                "{action}: numeric resource must stay a JSON number: {bodies:?}"
            );
            assert!(
                bodies.iter().all(|body| !body.contains(stringified)),
                "{action}: never stringified: {bodies:?}"
            );
        }
    }

    #[test]
    fn every_table_binds_and_declares_a_complete_contract() {
        // Bind-time validation (row paths, field paths, pagination) plus the
        // admission-gate basics: page-number pagination that terminates, and
        // owner/repo-style resources spelled out.
        for table in pack().expect("embedded asset parses").tables {
            RowPath::parse(table.row_path).unwrap_or_else(|e| panic!("{}: {e}", table.id));
            RowConverter::new(table.fields).unwrap_or_else(|e| panic!("{}: {e}", table.id));
            table
                .pagination
                .validate()
                .unwrap_or_else(|e| panic!("{}: {e}", table.id));
            assert!(
                matches!(
                    table.pagination,
                    PaginationStrategy::PageNumber { per_page: 100, .. }
                ),
                "{} uses GitHub's page-number pagination at the 100 maximum",
                table.id
            );
            assert!(
                table.id.starts_with("github."),
                "{} carries the pack namespace",
                table.id
            );
            assert!(
                table.action_id.starts_with("github."),
                "{} executes a github action",
                table.id
            );
        }
    }
}