postgres-mcp-readonly 0.1.7

Read-only MCP server for PostgreSQL and TimescaleDB
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
//! Integration tests for the schema toolset: catalog introspection against the seed databases, through the HTTP router.

mod common;

use axum::Router;
use common::{
    DB_NAME, PORT_15, PORT_18, PORT_19, PROTOCOL_2025, app, call_tool, config_with, one_db,
    post_mcp, result_of,
};
use postgres_mcp::config::Config;
use serde_json::{Value, json};

const STRICT_USER: &str = "mcp_strict";
const STRICT_PASSWORD: &str = "test-only-strict";
const SUPER_USER: &str = "postgres";
const SUPER_PASSWORD: &str = "test-only-superuser";
const SECRETS: [&str; 3] = ["test-only-superuser", "test-only-ro", "test-only-strict"];

/// One database entry for a given login, named appdb.
fn login(port: u16, user: &str, password: &str) -> Value {
    json!({
        "name": "appdb",
        "host": "127.0.0.1",
        "port": port,
        "database": DB_NAME,
        "user": user,
        "password": password,
        "sslmode": "disable",
    })
}

fn config_as(port: u16, user: &str, password: &str) -> Config {
    config_with(&[login(port, user, password)], &[])
}

/// The result object of one tool call through a router.
async fn call_on(router: &Router, name: &str, arguments: Value) -> Value {
    result_of(&call_tool(router, name, arguments).await).clone()
}

async fn call(config: Config, name: &str, arguments: Value) -> Value {
    call_on(&app(config), name, arguments).await
}

fn is_error(result: &Value) -> bool {
    result
        .get("isError")
        .and_then(Value::as_bool)
        .unwrap_or(false)
}

fn text_of(result: &Value) -> String {
    result["content"][0]["text"]
        .as_str()
        .expect("result has a text block")
        .to_owned()
}

/// The structured content of a successful call; a tool error fails the test with its text.
fn ok(result: &Value) -> &Value {
    assert!(!is_error(result), "tool error: {}", text_of(result));
    result
        .get("structuredContent")
        .unwrap_or_else(|| panic!("no structuredContent in {result}"))
}

/// The text of a tool error, which must be one.
fn error_text(result: &Value) -> String {
    assert!(is_error(result), "expected a tool error, got {result}");
    text_of(result)
}

fn names_of(items: &[Value]) -> Vec<String> {
    items
        .iter()
        .filter_map(|item| item["name"].as_str().map(str::to_owned))
        .collect()
}

/// The entry of `items` whose `key` is `value`.
fn find<'a>(items: &'a [Value], key: &str, value: &str) -> &'a Value {
    items
        .iter()
        .find(|item| item[key] == value)
        .unwrap_or_else(|| panic!("no entry with {key} = {value} in {items:?}"))
}

fn array<'a>(value: &'a Value, what: &str) -> &'a Vec<Value> {
    value
        .as_array()
        .unwrap_or_else(|| panic!("{what} is not an array: {value}"))
}

fn lists_objects(value: &Value) -> Vec<Value> {
    array(&value["objects"], "objects").clone()
}

/// Every object of a schema, read page by page with the given page size.
async fn walk_objects(router: &Router, arguments: Value, limit: u64) -> Vec<Value> {
    let mut collected = Vec::new();
    let mut cursor: Option<String> = None;
    loop {
        let mut args = arguments.clone();
        args["limit"] = json!(limit);
        if let Some(next) = &cursor {
            args["cursor"] = json!(next);
        }
        let page = ok(&call_on(router, "list_objects", args).await).clone();
        collected.extend(lists_objects(&page));
        match page.get("nextCursor").and_then(Value::as_str) {
            Some(next) => cursor = Some(next.to_owned()),
            None => break,
        }
    }
    collected
}

fn object_key(object: &Value) -> String {
    format!(
        "{}.{}.{}.{}",
        object["schema"].as_str().unwrap_or_default(),
        object["kind"].as_str().unwrap_or_default(),
        object["name"].as_str().unwrap_or_default(),
        object["arguments"].as_str().unwrap_or_default(),
    )
}

// ---------------------------------------------------------------- list_schemas

#[tokio::test]
async fn list_schemas_hides_system_schemas_by_default() {
    let result = call(one_db(PORT_18, &[]), "list_schemas", json!({})).await;
    let body = ok(&result).clone();
    assert_eq!(body["database"], "appdb");
    let schemas = array(&body["schemas"], "schemas").clone();
    let names = names_of(&schemas);
    for expected in ["app", "metrics", "pgmcp", "public", "shop", "types"] {
        assert!(names.contains(&expected.to_owned()), "{names:?}");
    }
    for hidden in [
        "pg_catalog",
        "information_schema",
        "_timescaledb_internal",
        "_timescaledb_catalog",
        "pg_toast",
    ] {
        assert!(!names.contains(&hidden.to_owned()), "{names:?}");
    }
    let mut sorted = names.clone();
    sorted.sort();
    assert_eq!(names, sorted, "ordered by name");
    let shop = find(&schemas, "name", "shop");
    // 11 original tables, shop.many_parts and shop."Odd.Name".
    assert_eq!(shop["tables"], 13, "{shop}");
    assert_eq!(shop["views"], 2, "{shop}");
    assert_eq!(shop["functions"], 7, "{shop}");
    assert_eq!(shop["owner"], "postgres", "{shop}");
}

#[tokio::test]
async fn list_schemas_include_system_adds_catalogs() {
    let result = call(
        one_db(PORT_18, &[]),
        "list_schemas",
        json!({"include_system": true}),
    )
    .await;
    let schemas = array(&ok(&result)["schemas"], "schemas").clone();
    let names = names_of(&schemas);
    assert!(names.contains(&"pg_catalog".to_owned()), "{names:?}");
    assert!(
        names.contains(&"information_schema".to_owned()),
        "{names:?}"
    );
}

#[tokio::test]
async fn list_schemas_as_strict_role_shows_only_usable_schemas() {
    let router = app(config_as(PORT_18, STRICT_USER, STRICT_PASSWORD));
    let result = call_on(&router, "list_schemas", json!({})).await;
    let schemas = array(&ok(&result)["schemas"], "schemas").clone();
    let names = names_of(&schemas);
    for expected in ["app", "shop", "types", "public", "pgmcp"] {
        assert!(names.contains(&expected.to_owned()), "{names:?}");
    }
    assert!(!names.contains(&"sess_test".to_owned()), "{names:?}");
    let shop = find(&schemas, "name", "shop");
    assert_eq!(shop["tables"], 13, "counts do not depend on USAGE: {shop}");
}

// ---------------------------------------------------------------- list_objects

#[tokio::test]
async fn list_objects_default_kinds_in_shop() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(&router, "list_objects", json!({"schema": "shop"})).await;
    let objects = lists_objects(ok(&result));
    let kind_of = |name: &str| -> String {
        find(&objects, "name", name)["kind"]
            .as_str()
            .unwrap_or_default()
            .to_owned()
    };
    assert_eq!(kind_of("customers"), "table");
    assert!(
        find(&objects, "name", "customers")
            .get("rowEstimate")
            .is_some(),
        "rowEstimate is always present for tables"
    );
    assert_eq!(kind_of("events"), "partitioned_table");
    assert_eq!(kind_of("open_orders"), "view");
    assert_eq!(kind_of("customer_totals"), "materialized_view");
    assert_eq!(kind_of("items_remote"), "foreign_table");
    assert_eq!(kind_of("invoice_seq"), "sequence");
    let partition = find(&objects, "name", "events_2024_q1");
    assert_eq!(partition["isPartition"], true);
    assert_eq!(partition["partitionOf"], "shop.events");
    assert!(
        find(&objects, "name", "events")
            .get("isPartition")
            .is_none(),
        "isPartition only on partitions"
    );
    for object in &objects {
        let kind = object["kind"].as_str().unwrap_or_default();
        assert!(
            !["function", "procedure", "type"].contains(&kind),
            "default kinds exclude {kind}: {object}"
        );
    }
    let remote = find(&objects, "name", "items_remote");
    assert!(
        remote.get("rowEstimate").is_none(),
        "no estimate for foreign tables: {remote}"
    );
}

#[tokio::test]
async fn list_objects_functions_and_procedures_in_shop() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "list_objects",
        json!({"schema": "shop", "kinds": ["function", "procedure"]}),
    )
    .await;
    let objects = lists_objects(ok(&result));
    assert_eq!(objects.len(), 7, "{objects:?}");
    let order_total = find(&objects, "name", "order_total");
    assert_eq!(order_total["kind"], "function");
    assert_eq!(order_total["arguments"], "bigint");
    assert_eq!(order_total["returns"], "numeric");
    assert_eq!(
        order_total["comment"],
        "Sum of order totals for one customer"
    );
    let fmts: Vec<&Value> = objects.iter().filter(|o| o["name"] == "fmt").collect();
    let mut arguments: Vec<&str> = fmts
        .iter()
        .filter_map(|o| o["arguments"].as_str())
        .collect();
    arguments.sort_unstable();
    assert_eq!(arguments, vec!["integer", "text"]);
    let noop = find(&objects, "name", "noop");
    assert_eq!(noop["kind"], "procedure");
    assert!(noop.get("returns").is_none(), "{noop}");
}

#[tokio::test]
async fn list_objects_types_in_types_schema() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "list_objects",
        json!({"schema": "types", "kinds": ["type"]}),
    )
    .await;
    let objects = lists_objects(ok(&result));
    let mut names = names_of(&objects);
    names.sort();
    assert_eq!(names, vec!["floatrange", "mood", "pair", "posint"]);
    let mood = find(&objects, "name", "mood");
    assert_eq!(mood["enumLabels"], json!(["sad", "ok", "happy"]));
    assert_eq!(mood["typeKind"], "enum");
    assert_eq!(find(&objects, "name", "posint")["typeKind"], "domain");
    assert_eq!(find(&objects, "name", "pair")["typeKind"], "composite");
    assert_eq!(find(&objects, "name", "floatrange")["typeKind"], "range");
    assert!(
        mood.get("enumLabels").is_some()
            && find(&objects, "name", "pair").get("enumLabels").is_none(),
        "enumLabels only on enums"
    );
}

#[tokio::test]
async fn list_objects_unknown_kind_is_an_error() {
    let result = call(
        one_db(PORT_18, &[]),
        "list_objects",
        json!({"kinds": ["bogus"]}),
    )
    .await;
    assert_eq!(
        error_text(&result),
        "Unknown kind \"bogus\". Valid kinds: table, partitioned_table, view, materialized_view, foreign_table, sequence, function, procedure, type."
    );
}

#[tokio::test]
async fn list_objects_empty_kinds_selects_nothing() {
    let result = call(
        one_db(PORT_18, &[]),
        "list_objects",
        json!({"schema": "shop", "kinds": []}),
    )
    .await;
    assert!(lists_objects(ok(&result)).is_empty());
}

#[tokio::test]
async fn list_objects_paging_has_no_gaps_or_duplicates() {
    let router = app(one_db(PORT_18, &[]));
    let all = walk_objects(&router, json!({"schema": "shop"}), 500).await;
    let paged = walk_objects(&router, json!({"schema": "shop"}), 2).await;
    let all_keys: Vec<String> = all.iter().map(object_key).collect();
    let paged_keys: Vec<String> = paged.iter().map(object_key).collect();
    assert_eq!(paged_keys, all_keys, "same objects, same order");
    let mut unique = paged_keys.clone();
    unique.sort();
    unique.dedup();
    assert_eq!(unique.len(), paged_keys.len(), "no duplicates");
    assert!(all.len() > 4, "shop has several objects: {}", all.len());
}

#[tokio::test]
async fn list_objects_next_cursor_absent_on_last_page() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "list_objects",
        json!({"schema": "types", "kinds": ["type"], "limit": 500}),
    )
    .await;
    assert!(ok(&result).get("nextCursor").is_none());
}

#[tokio::test]
async fn list_objects_name_like_is_case_insensitive_substring() {
    let result = call(
        one_db(PORT_18, &[]),
        "list_objects",
        json!({"schema": "shop", "name_like": "ORDER"}),
    )
    .await;
    let mut names = names_of(&lists_objects(ok(&result)));
    names.sort();
    // orders_id_seq is the bigserial sequence, listed by default.
    assert_eq!(
        names,
        vec!["open_orders", "order_items", "orders", "orders_id_seq"]
    );
}

#[tokio::test]
async fn list_objects_name_like_escapes_wildcards() {
    let router = app(one_db(PORT_18, &[]));
    let percent = call_on(
        &router,
        "list_objects",
        json!({"schema": "shop", "name_like": "%"}),
    )
    .await;
    assert!(lists_objects(ok(&percent)).is_empty());
    let underscore = call_on(
        &router,
        "list_objects",
        json!({"schema": "shop", "name_like": "_"}),
    )
    .await;
    let names = names_of(&lists_objects(ok(&underscore)));
    assert!(names.contains(&"order_items".to_owned()), "{names:?}");
    assert!(!names.contains(&"customers".to_owned()), "{names:?}");
}

#[tokio::test]
async fn list_objects_without_schema_hides_extension_functions() {
    let result = call(
        one_db(PORT_18, &[]),
        "list_objects",
        json!({"kinds": ["function"], "limit": 500}),
    )
    .await;
    let objects = lists_objects(ok(&result));
    assert!(!objects.is_empty());
    for object in &objects {
        let name = object["name"].as_str().unwrap_or_default();
        assert!(
            !name.starts_with("st_"),
            "PostGIS function listed: {object}"
        );
        assert_ne!(object["schema"], "pg_catalog", "{object}");
    }
    assert!(
        objects.iter().any(|o| o["name"] == "order_total"),
        "user functions are listed"
    );
}

#[tokio::test]
async fn list_objects_limit_and_cursor_are_validated() {
    let router = app(one_db(PORT_18, &[]));
    for limit in [0, 501] {
        let result = call_on(&router, "list_objects", json!({"limit": limit})).await;
        assert_eq!(
            error_text(&result),
            "limit must be between 1 and 500.",
            "limit {limit}"
        );
    }
    for cursor in ["x", "o:", "o:-1", "o:abc"] {
        let result = call_on(&router, "list_objects", json!({"cursor": cursor})).await;
        assert_eq!(error_text(&result), "Invalid cursor.", "cursor {cursor}");
    }
}

#[tokio::test]
async fn list_objects_as_strict_role_reads_shop() {
    let router = app(config_as(PORT_18, STRICT_USER, STRICT_PASSWORD));
    let result = call_on(&router, "list_objects", json!({"schema": "shop"})).await;
    let objects = lists_objects(ok(&result));
    assert!(objects.iter().any(|o| o["name"] == "customers"));
}

// ---------------------------------------------------------------- describe_table

#[tokio::test]
async fn describe_table_orders_columns_and_keys() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(&router, "describe_table", json!({"table": "shop.orders"})).await;
    let body = ok(&result).clone();
    assert_eq!(body["schema"], "shop");
    assert_eq!(body["name"], "orders");
    assert_eq!(body["kind"], "table");
    assert_eq!(body["persistence"], "permanent");
    assert_eq!(body["comment"], "Orders placed by customers");
    assert_eq!(
        body["rowSecurity"],
        json!({"enabled": false, "forced": false})
    );
    assert!(
        body["sizes"]["totalBytes"].as_i64().unwrap_or_default() > 0,
        "{body}"
    );

    let columns = array(&body["columns"], "columns").clone();
    let expected = [
        ("id", "bigint"),
        ("customer_id", "bigint"),
        ("status", "text"),
        ("total", "numeric(12,2)"),
        ("placed_at", "timestamp with time zone"),
        ("notes", "text"),
    ];
    let names: Vec<(&str, &str)> = columns
        .iter()
        .map(|c| {
            (
                c["name"].as_str().unwrap_or_default(),
                c["type"].as_str().unwrap_or_default(),
            )
        })
        .collect();
    assert_eq!(names, expected.to_vec());
    let id = find(&columns, "name", "id");
    assert!(
        id["default"]
            .as_str()
            .unwrap_or_default()
            .contains("nextval"),
        "{id}"
    );
    assert_eq!(id["nullable"], false);
    let notes = find(&columns, "name", "notes");
    assert_eq!(notes["collation"], "C", "{notes}");
    assert_eq!(notes["nullable"], true);
    assert!(find(&columns, "name", "total").get("comment").is_some());
    assert_eq!(
        find(&columns, "name", "total")["comment"],
        "Order total, two decimals"
    );

    assert_eq!(body["primaryKey"]["name"], "orders_pkey");
    assert_eq!(body["primaryKey"]["columns"], json!(["id"]));

    let indexes = array(&body["indexes"], "indexes").clone();
    let mut index_names = names_of(&indexes);
    index_names.sort();
    assert_eq!(
        index_names,
        vec![
            "orders_customer_placed_idx",
            "orders_lower_status_idx",
            "orders_open_placed_idx",
            "orders_pkey",
        ]
    );
    let partial = find(&indexes, "name", "orders_open_placed_idx");
    assert!(
        partial["definition"]
            .as_str()
            .unwrap_or_default()
            .contains("WHERE")
    );
    let expression = find(&indexes, "name", "orders_lower_status_idx");
    assert!(
        expression["definition"]
            .as_str()
            .unwrap_or_default()
            .contains("lower(status)")
    );
    assert_eq!(find(&indexes, "name", "orders_pkey")["primary"], true);
    assert_eq!(find(&indexes, "name", "orders_pkey")["method"], "btree");

    let foreign_keys = array(&body["foreignKeys"], "foreignKeys").clone();
    assert_eq!(foreign_keys.len(), 1, "{foreign_keys:?}");
    let fk = &foreign_keys[0];
    assert_eq!(fk["columns"], json!(["customer_id"]));
    assert_eq!(fk["referencesTable"], "shop.customers");
    assert_eq!(fk["referencesColumns"], json!(["id"]));
    assert_eq!(fk["onDelete"], "CASCADE");
    assert_eq!(fk["onUpdate"], "NO ACTION");
    assert_eq!(fk["deferrable"], false);

    let referenced = array(&body["referencedBy"], "referencedBy").clone();
    let by_items = find(&referenced, "table", "shop.order_items");
    assert_eq!(by_items["columns"], json!(["order_id"]));
    assert_eq!(by_items["referencesColumns"], json!(["id"]));

    let triggers = array(&body["triggers"], "triggers").clone();
    let touch = find(&triggers, "name", "orders_touch");
    assert_eq!(touch["timing"], "BEFORE");
    assert_eq!(touch["events"], json!(["UPDATE"]));
    assert_eq!(touch["level"], "ROW");
    assert_eq!(touch["enabled"], true);
    assert_eq!(touch["function"], "shop.touch_order");
    assert!(
        body.as_object()
            .is_some_and(|m| m.keys().all(|k| !k.ends_with("Error"))),
        "no section failed: {body}"
    );
}

#[tokio::test]
async fn describe_table_customers_identity_generated_check_unique() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.customers"}),
    )
    .await;
    let body = ok(&result).clone();
    let columns = array(&body["columns"], "columns").clone();
    assert_eq!(find(&columns, "name", "id")["identity"], "always");
    let email = find(&columns, "name", "email");
    assert_eq!(email["type"], "citext");
    assert_eq!(email["comment"], "Case-insensitive login email");
    let generated = find(&columns, "name", "name_upper");
    assert!(
        generated["generated"]
            .as_str()
            .unwrap_or_default()
            .contains("upper"),
        "{generated}"
    );
    assert_eq!(generated["default"], Value::Null);
    let checks = array(&body["checkConstraints"], "checkConstraints").clone();
    assert!(
        checks.iter().any(|c| c["definition"]
            .as_str()
            .unwrap_or_default()
            .contains("tier")),
        "{checks:?}"
    );
    let uniques = array(&body["uniqueConstraints"], "uniqueConstraints").clone();
    assert!(
        uniques.iter().any(|u| u["columns"] == json!(["email"])),
        "{uniques:?}"
    );
    assert_eq!(body["comment"], "Customer accounts");
}

#[tokio::test]
async fn describe_table_partitioned_events_and_partition() {
    let router = app(one_db(PORT_18, &[]));
    let parent = call_on(&router, "describe_table", json!({"table": "shop.events"})).await;
    let body = ok(&parent).clone();
    assert_eq!(body["kind"], "partitioned_table");
    let partitioning = &body["partitioning"];
    assert_eq!(partitioning["strategy"], "range");
    assert!(
        partitioning["key"]
            .as_str()
            .unwrap_or_default()
            .contains("happened_at")
    );
    assert_eq!(partitioning["parent"], Value::Null);
    assert_eq!(partitioning["partitionsTruncated"], false);
    let partitions = array(&partitioning["partitions"], "partitions").clone();
    assert_eq!(partitions.len(), 3, "{partitions:?}");
    let default_partition = find(&partitions, "name", "shop.events_default");
    assert!(
        default_partition["bound"]
            .as_str()
            .unwrap_or_default()
            .contains("DEFAULT")
    );

    let child = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.events_2024_q1"}),
    )
    .await;
    let child_body = ok(&child).clone();
    assert_eq!(child_body["partitioning"]["parent"], "shop.events");
    assert!(
        child_body["partitioning"]["bound"]
            .as_str()
            .unwrap_or_default()
            .contains("FOR VALUES")
    );
    assert_eq!(child_body["comment"], "Events in 2024 Q1");
}

#[tokio::test]
async fn describe_table_row_security_policy() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.tenant_data"}),
    )
    .await;
    let body = ok(&result).clone();
    assert_eq!(body["rowSecurity"]["enabled"], true);
    assert_eq!(body["rowSecurity"]["forced"], false);
    let policies = array(&body["policies"], "policies").clone();
    let policy = find(&policies, "name", "tenant_isolation");
    assert!(
        policy["using"]
            .as_str()
            .unwrap_or_default()
            .contains("tenant"),
        "{policy}"
    );
    assert_eq!(policy["command"], "ALL");
    assert_eq!(policy["permissive"], true);
    assert_eq!(policy["roles"], json!(["public"]));
}

#[tokio::test]
async fn describe_table_view_and_materialized_view() {
    let router = app(one_db(PORT_18, &[]));
    let view = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.open_orders"}),
    )
    .await;
    let view_body = ok(&view).clone();
    assert_eq!(view_body["kind"], "view");
    assert!(
        view_body["viewDefinition"]
            .as_str()
            .unwrap_or_default()
            .contains("open")
    );
    assert_eq!(view_body["sizes"], Value::Null);
    assert_eq!(view_body["comment"], "Orders with status open");

    let matview = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.customer_totals"}),
    )
    .await;
    let matview_body = ok(&matview).clone();
    assert_eq!(matview_body["kind"], "materialized_view");
    let indexes = array(&matview_body["indexes"], "indexes").clone();
    let unique = find(&indexes, "name", "customer_totals_customer_idx");
    assert_eq!(unique["unique"], true);
    assert!(matview_body["viewDefinition"].is_string());
}

#[tokio::test]
async fn describe_table_foreign_table_hides_passwords() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.items_remote"}),
    )
    .await;
    let body = ok(&result).clone();
    assert_eq!(body["kind"], "foreign_table");
    assert_eq!(body["foreign"]["server"], "loopback");
    let options = body["foreign"]["options"]
        .as_object()
        .cloned()
        .unwrap_or_default();
    assert_eq!(options.get("schema_name"), Some(&json!("app")));
    assert_eq!(options.get("table_name"), Some(&json!("items")));
    let serialized = body.to_string();
    for secret in SECRETS {
        assert!(!serialized.contains(secret), "{secret} in {serialized}");
    }
    assert!(!serialized.contains("password"), "{serialized}");
}

#[tokio::test]
async fn describe_table_inheritance() {
    let router = app(one_db(PORT_18, &[]));
    let child = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.audit_child"}),
    )
    .await;
    assert_eq!(ok(&child)["inherits"], json!(["shop.audit_base"]));
    let parent = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.audit_base"}),
    )
    .await;
    assert_eq!(ok(&parent)["inheritedBy"], json!(["shop.audit_child"]));
    assert_eq!(ok(&parent)["inherits"], json!([]));
}

#[tokio::test]
async fn describe_table_all_types_sample() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "describe_table",
        json!({"table": "types.all_types"}),
    )
    .await;
    let columns = array(&ok(&result)["columns"], "columns").clone();
    let expected = [
        ("c_varchar", "character varying(20)"),
        ("c_bpchar", "character(5)"),
        ("c_bit", "bit(4)"),
        ("c_int4_arr", "integer[]"),
        ("c_mood", "types.mood"),
        ("c_mood_arr", "types.mood[]"),
        ("c_int4multirange", "int4multirange"),
        ("c_timestamptz_arr", "timestamp with time zone[]"),
        ("c_numeric", "numeric"),
        ("c_money", "money"),
        ("c_varbit", "bit varying"),
        ("c_citext", "citext"),
        ("c_pair", "types.pair"),
        ("c_posint", "types.posint"),
        ("c_floatrange", "types.floatrange"),
    ];
    for (name, ty) in expected {
        assert_eq!(find(&columns, "name", name)["type"], ty, "{name}");
    }
    assert_eq!(
        find(&columns, "name", "c_numeric")["comment"],
        "Arbitrary-precision numeric, including NaN in row 2"
    );
}

#[tokio::test]
async fn describe_table_hypertable_is_a_table_on_15432() {
    let result = call(
        one_db(PORT_18, &[]),
        "describe_table",
        json!({"table": "metrics.readings"}),
    )
    .await;
    let body = ok(&result).clone();
    assert_eq!(body["kind"], "table");
    assert_eq!(body["schema"], "metrics");
    assert_eq!(array(&body["columns"], "columns").len(), 4, "{body}");
}

#[tokio::test]
async fn describe_table_name_resolution() {
    let router = app(one_db(PORT_18, &[]));
    // app.orders exists too, so a bare name is ambiguous.
    let bare = call_on(&router, "describe_table", json!({"table": "orders"})).await;
    assert_eq!(
        error_text(&bare),
        "Table \"orders\" exists in several schemas: app, shop. Pass schema."
    );
    let qualified = call_on(&router, "describe_table", json!({"table": "shop.orders"})).await;
    assert_eq!(ok(&qualified)["name"], "orders");
    let quoted = call_on(&router, "describe_table", json!({"table": "\"orders\""})).await;
    assert_eq!(
        error_text(&quoted),
        "Table \"\"orders\"\" exists in several schemas: app, shop. Pass schema."
    );
    let with_schema = call_on(
        &router,
        "describe_table",
        json!({"table": "orders", "schema": "shop"}),
    )
    .await;
    assert_eq!(ok(&with_schema)["schema"], "shop");
    let unique = call_on(&router, "describe_table", json!({"table": "items"})).await;
    assert_eq!(ok(&unique)["schema"], "app");
}

#[tokio::test]
async fn describe_table_unknown_name_suggests_similar_tables() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(&router, "describe_table", json!({"table": "order"})).await;
    assert_eq!(
        error_text(&result),
        "Table \"order\" not found. Did you mean: app.orders, shop.open_orders, shop.order_items, shop.orders?"
    );
    let wrong_schema = call_on(
        &router,
        "describe_table",
        json!({"table": "open_orders", "schema": "app"}),
    )
    .await;
    let text = error_text(&wrong_schema);
    assert!(
        text.starts_with("Table \"open_orders\" not found."),
        "{text}"
    );
    let sequence = call_on(&router, "describe_table", json!({"table": "invoice_seq"})).await;
    assert!(error_text(&sequence).starts_with("Table \"invoice_seq\" not found."));
}

#[tokio::test]
async fn describe_table_warns_when_role_cannot_select() {
    let router = app(config_as(PORT_18, STRICT_USER, STRICT_PASSWORD));
    let result = call_on(
        &router,
        "describe_table",
        json!({"table": "public.pgmcp_seed_done"}),
    )
    .await;
    let body = ok(&result).clone();
    assert_eq!(
        body["warnings"],
        json!(["The connected role cannot SELECT from this table."])
    );
    assert_eq!(array(&body["columns"], "columns").len(), 1, "{body}");
}

#[tokio::test]
async fn describe_table_as_strict_role_on_shop_has_no_warning() {
    let router = app(config_as(PORT_18, STRICT_USER, STRICT_PASSWORD));
    let result = call_on(&router, "describe_table", json!({"table": "shop.orders"})).await;
    assert_eq!(ok(&result)["warnings"], json!([]));
    assert_eq!(ok(&result)["primaryKey"]["name"], "orders_pkey");
}

// ---------------------------------------------------------------- describe_function

#[tokio::test]
async fn describe_function_overloads_and_attributes() {
    let router = app(one_db(PORT_18, &[]));
    let fmt = call_on(
        &router,
        "describe_function",
        json!({"function": "shop.fmt"}),
    )
    .await;
    let fmt_body = ok(&fmt).clone();
    let overloads = array(&fmt_body["functions"], "functions").clone();
    assert_eq!(overloads.len(), 2, "{overloads:?}");
    assert_eq!(overloads[0]["arguments"], "integer");
    assert_eq!(overloads[1]["arguments"], "text");
    assert_eq!(overloads[0]["returns"], "text");
    assert_eq!(overloads[0]["volatility"], "immutable");
    assert_eq!(overloads[0]["language"], "sql");

    let total = call_on(
        &router,
        "describe_function",
        json!({"function": "order_total"}),
    )
    .await;
    let total_body = ok(&total).clone();
    let function = &array(&total_body["functions"], "functions")[0];
    assert_eq!(function["schema"], "shop");
    assert_eq!(function["kind"], "function");
    assert_eq!(function["volatility"], "stable");
    assert_eq!(function["language"], "sql");
    assert_eq!(function["arguments"], "bigint");
    assert_eq!(function["returns"], "numeric");
    assert_eq!(function["comment"], "Sum of order totals for one customer");
    assert!(
        function["source"]
            .as_str()
            .unwrap_or_default()
            .contains("SELECT")
    );
    assert!(function.get("sourceNote").is_none());
    assert_eq!(function["config"], json!([]));
    assert_eq!(function["parallel"], "unsafe");
    assert_eq!(function["strict"], false);
}

#[tokio::test]
async fn describe_function_security_definer_and_procedure() {
    let router = app(one_db(PORT_18, &[]));
    let secret = call_on(
        &router,
        "describe_function",
        json!({"function": "shop.secret_fn"}),
    )
    .await;
    let secret_fn = &array(&ok(&secret)["functions"], "functions")[0].clone();
    assert_eq!(secret_fn["securityDefiner"], true);
    assert_eq!(secret_fn["language"], "plpgsql");
    assert!(
        secret_fn["source"]
            .as_str()
            .unwrap_or_default()
            .contains("pgmcp secret")
    );

    let noop = call_on(
        &router,
        "describe_function",
        json!({"function": "shop.noop"}),
    )
    .await;
    let noop_fn = &array(&ok(&noop)["functions"], "functions")[0].clone();
    assert_eq!(noop_fn["kind"], "procedure");
    assert_eq!(noop_fn["returns"], Value::Null);
}

#[tokio::test]
async fn describe_function_compiled_has_no_source() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "describe_function",
        json!({"function": "pg_catalog.version"}),
    )
    .await;
    let function = &array(&ok(&result)["functions"], "functions")[0].clone();
    assert_eq!(function["schema"], "pg_catalog");
    assert_eq!(function["language"], "internal");
    assert_eq!(function["source"], Value::Null);
    assert_eq!(function["sourceNote"], "compiled");
}

#[tokio::test]
async fn describe_function_not_found_and_system_schemas() {
    let router = app(one_db(PORT_18, &[]));
    let missing = call_on(&router, "describe_function", json!({"function": "nope"})).await;
    assert_eq!(error_text(&missing), "Function \"nope\" not found.");
    let system = call_on(&router, "describe_function", json!({"function": "version"})).await;
    assert_eq!(error_text(&system), "Function \"version\" not found.");
    let with_schema = call_on(
        &router,
        "describe_function",
        json!({"function": "fmt", "schema": "shop"}),
    )
    .await;
    assert_eq!(array(&ok(&with_schema)["functions"], "functions").len(), 2);
}

#[tokio::test]
async fn describe_function_as_strict_role_reads_definer_source() {
    let router = app(config_as(PORT_18, STRICT_USER, STRICT_PASSWORD));
    let result = call_on(
        &router,
        "describe_function",
        json!({"function": "shop.secret_fn"}),
    )
    .await;
    assert_eq!(array(&ok(&result)["functions"], "functions").len(), 1);
}

// ---------------------------------------------------------------- search_schema

#[tokio::test]
async fn search_finds_column_by_name() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(&router, "search_schema", json!({"query": "email"})).await;
    let matches = array(&ok(&result)["matches"], "matches").clone();
    let column = matches
        .iter()
        .find(|m| m["kind"] == "column" && m["schema"] == "shop" && m["table"] == "customers")
        .unwrap_or_else(|| panic!("{matches:?}"));
    assert_eq!(column["name"], "email");
    assert_eq!(column["type"], "citext");
    assert_eq!(column["matched"], "name");
    assert_eq!(column["comment"], "Case-insensitive login email");
}

#[tokio::test]
async fn search_ranks_exact_then_prefix_then_substring_then_comment() {
    let router = app(one_db(PORT_18, &[]));
    let exact = call_on(&router, "search_schema", json!({"query": "orders"})).await;
    let first = &array(&ok(&exact)["matches"], "matches")[0].clone();
    assert_eq!(first["kind"], "table");
    assert_eq!(first["name"], "orders");
    // app.orders and shop.orders are both exact matches; ties sort by schema name.
    assert_eq!(first["schema"], "app");

    let prefix = call_on(&router, "search_schema", json!({"query": "order"})).await;
    let matches = array(&ok(&prefix)["matches"], "matches").clone();
    let names: Vec<&str> = matches.iter().filter_map(|m| m["name"].as_str()).collect();
    assert!(
        names.contains(&"orders") && names.contains(&"order_items"),
        "{names:?}"
    );
    let last_name_match = matches
        .iter()
        .rposition(|m| m["matched"] == "name")
        .unwrap_or_default();
    let first_comment_match = matches
        .iter()
        .position(|m| m["matched"] == "comment")
        .unwrap_or(usize::MAX);
    assert!(
        first_comment_match > last_name_match,
        "comment matches come after every name match"
    );
    assert!(
        matches[0]["name"]
            .as_str()
            .unwrap_or_default()
            .starts_with("order")
    );
}

#[tokio::test]
async fn search_matches_comment_only_text() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(&router, "search_schema", json!({"query": "Quantity"})).await;
    let matches = array(&ok(&result)["matches"], "matches").clone();
    let hit = matches
        .iter()
        .find(|m| m["name"] == "qty")
        .unwrap_or_else(|| panic!("{matches:?}"));
    assert_eq!(hit["kind"], "column");
    assert_eq!(hit["table"], "order_items");
    assert_eq!(hit["matched"], "comment");
    assert_eq!(hit["comment"], "Quantity ordered");
}

#[tokio::test]
async fn search_query_length_and_wildcards() {
    let router = app(one_db(PORT_18, &[]));
    for query in ["%", "a", ""] {
        let result = call_on(&router, "search_schema", json!({"query": query})).await;
        assert_eq!(
            error_text(&result),
            "query must be at least 2 characters.",
            "query {query:?}"
        );
    }
    let literal = call_on(&router, "search_schema", json!({"query": "a%"})).await;
    for m in array(&ok(&literal)["matches"], "matches") {
        let name = m["name"].as_str().unwrap_or_default();
        let comment = m["comment"].as_str().unwrap_or_default();
        assert!(name.contains("a%") || comment.contains("a%"), "{m}");
    }
}

#[tokio::test]
async fn search_paging_is_stable() {
    let router = app(one_db(PORT_18, &[]));
    let all = call_on(
        &router,
        "search_schema",
        json!({"query": "order", "limit": 500}),
    )
    .await;
    let all_matches = array(&ok(&all)["matches"], "matches").clone();
    let mut paged = Vec::new();
    let mut cursor: Option<String> = None;
    loop {
        let mut args = json!({"query": "order", "limit": 2});
        if let Some(next) = &cursor {
            args["cursor"] = json!(next);
        }
        let page = call_on(&router, "search_schema", args).await;
        let body = ok(&page).clone();
        paged.extend(array(&body["matches"], "matches").clone());
        match body.get("nextCursor").and_then(Value::as_str) {
            Some(next) => cursor = Some(next.to_owned()),
            None => break,
        }
    }
    assert_eq!(paged, all_matches);
}

#[tokio::test]
async fn search_kinds_limit_to_functions() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "search_schema",
        json!({"query": "order", "kinds": ["function"]}),
    )
    .await;
    let matches = array(&ok(&result)["matches"], "matches").clone();
    assert!(!matches.is_empty());
    assert!(
        matches.iter().all(|m| m["kind"] == "function"),
        "{matches:?}"
    );
    assert!(matches.iter().any(|m| m["name"] == "order_total"));
}

#[tokio::test]
async fn search_excludes_extension_functions() {
    let router = app(one_db(PORT_18, &[]));
    // PostGIS functions live in public and are extension-owned, so none may be listed.
    let postgis = call_on(
        &router,
        "search_schema",
        json!({"query": "makepoint", "kinds": ["function"]}),
    )
    .await;
    assert_eq!(array(&ok(&postgis)["matches"], "matches").len(), 0);
    let prefix = call_on(
        &router,
        "search_schema",
        json!({"query": "st_", "kinds": ["function"], "limit": 500}),
    )
    .await;
    let matches = array(&ok(&prefix)["matches"], "matches").clone();
    assert!(
        matches.iter().all(|m| m["schema"] != "public"),
        "{matches:?}"
    );
    assert!(
        matches.iter().any(|m| m["name"] == "list_log_files"),
        "{matches:?}"
    );
}

#[tokio::test]
async fn search_unknown_kind_is_an_error() {
    let result = call(
        one_db(PORT_18, &[]),
        "search_schema",
        json!({"query": "order", "kinds": ["index"]}),
    )
    .await;
    assert_eq!(
        error_text(&result),
        "Unknown kind \"index\". Valid kinds: table, view, column, function."
    );
}

#[tokio::test]
async fn search_as_strict_role_finds_shop_objects() {
    let router = app(config_as(PORT_18, STRICT_USER, STRICT_PASSWORD));
    let result = call_on(&router, "search_schema", json!({"query": "customer"})).await;
    let matches = array(&ok(&result)["matches"], "matches").clone();
    assert!(
        matches
            .iter()
            .any(|m| m["name"] == "customers" && m["schema"] == "shop")
    );
}

// ---------------------------------------------------------------- get_privileges

#[tokio::test]
async fn privileges_read_only_role_on_15432() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(&router, "get_privileges", json!({})).await;
    let body = ok(&result).clone();
    assert_eq!(body["database"], "appdb");
    assert_eq!(body["role"]["name"], "mcp_ro");
    assert_eq!(body["role"]["superuser"], false);
    let member_of = array(&body["role"]["memberOf"], "memberOf").clone();
    for role in ["pg_read_all_data", "pg_monitor", "pg_read_all_stats"] {
        assert!(member_of.contains(&json!(role)), "{member_of:?}");
    }
    let predefined = &body["predefinedRoles"];
    assert_eq!(predefined["pgReadAllData"], true);
    assert_eq!(predefined["pgMonitor"], true);
    assert_eq!(predefined["pgReadAllStats"], true);
    assert_eq!(predefined["pgWriteAllData"], false);
    assert_eq!(body["databasePrivileges"]["connect"], true);
    let features = &body["features"];
    assert_eq!(features["serverLog"]["available"], true);
    assert_eq!(features["pgStatStatements"]["available"], true);
    assert_eq!(features["timescaledb"]["installed"], true);
    assert_eq!(features["otherSessionsQueryText"]["available"], true);
    assert_eq!(body["beyondReadOnly"], json!([]));
    let schemas = array(&body["schemas"], "schemas").clone();
    let shop = find(&schemas, "name", "shop");
    assert_eq!(shop["selectableTables"], 13);
    assert_eq!(shop["unselectableTables"], 0);
}

#[tokio::test]
async fn privileges_without_pg_stat_statements_say_how_to_enable_it() {
    let router = app(one_db(PORT_15, &[]));
    let result = call_on(&router, "get_privileges", json!({})).await;
    let features = ok(&result)["features"].clone();
    assert_eq!(features["pgStatStatements"]["available"], false);
    assert_eq!(
        features["pgStatStatements"]["missing"],
        "CREATE EXTENSION pg_stat_statements; (and add it to shared_preload_libraries)"
    );
    assert_eq!(features["timescaledb"]["installed"], false);
    assert_eq!(features["timescaledb"]["version"], Value::Null);
}

/// The decoy log reader on database postgres (PostgreSQL 19) is owned by mcp_strict and is not SECURITY DEFINER.
#[tokio::test]
async fn privileges_decoy_log_reader_is_not_available() {
    let mut entry = login(PORT_19, "mcp_ro", "test-only-ro");
    entry["database"] = json!("postgres");
    let router = app(config_with(&[entry], &[]));
    let result = call_on(&router, "get_privileges", json!({})).await;
    assert_eq!(
        ok(&result)["features"]["serverLog"],
        json!({
            "available": false,
            "missing": "Run sql/log_reader.sql as a superuser, then GRANT USAGE ON SCHEMA pgmcp TO mcp_ro; GRANT EXECUTE ON FUNCTION pgmcp.read_log_file(text, bigint, integer), pgmcp.list_log_files() TO mcp_ro;"
        }),
    );
}

#[tokio::test]
async fn privileges_strict_role_is_limited() {
    let router = app(config_as(PORT_18, STRICT_USER, STRICT_PASSWORD));
    let result = call_on(&router, "get_privileges", json!({})).await;
    let body = ok(&result).clone();
    assert_eq!(body["role"]["name"], "mcp_strict");
    assert_eq!(body["predefinedRoles"]["pgReadAllData"], false);
    assert_eq!(body["predefinedRoles"]["pgReadAllStats"], true);
    assert_eq!(
        body["features"]["otherSessionsQueryText"]["available"],
        true
    );
    assert_eq!(body["features"]["serverLog"]["available"], true);
    let schemas = array(&body["schemas"], "schemas").clone();
    let sess = find(&schemas, "name", "sess_test");
    assert_eq!(sess["usage"], false);
    let shop = find(&schemas, "name", "shop");
    assert_eq!(shop["usage"], true);
    assert_eq!(shop["unselectableTables"], 0);
    let public = find(&schemas, "name", "public");
    assert!(
        public["unselectableTables"].as_i64().unwrap_or_default() >= 1,
        "{public}"
    );
}

#[tokio::test]
async fn privileges_superuser_is_flagged() {
    let router = app(config_as(PORT_18, SUPER_USER, SUPER_PASSWORD));
    let result = call_on(&router, "get_privileges", json!({})).await;
    let beyond = array(&ok(&result)["beyondReadOnly"], "beyondReadOnly").clone();
    assert!(beyond.contains(&json!("superuser")), "{beyond:?}");
}

// ---------------------------------------------------------------- toolsets and secrets

#[tokio::test]
async fn schema_tools_are_absent_when_toolset_is_off() {
    let router = app(one_db(PORT_18, &[("TOOLSETS", "core")]));
    let body = json!({"jsonrpc": "2.0", "id": 2, "method": "tools/list"});
    let reply = post_mcp(&router, &body, &[("mcp-protocol-version", PROTOCOL_2025)]).await;
    let tools = array(&result_of(&reply.json())["tools"], "tools").clone();
    let names: Vec<&str> = tools.iter().filter_map(|t| t["name"].as_str()).collect();
    for name in [
        "list_schemas",
        "list_objects",
        "describe_table",
        "describe_function",
        "search_schema",
        "get_privileges",
    ] {
        assert!(!names.contains(&name), "{names:?}");
    }
    assert!(names.contains(&"list_databases"), "{names:?}");
    let call_body = json!({
        "jsonrpc": "2.0",
        "id": 3,
        "method": "tools/call",
        "params": {"name": "list_schemas", "arguments": {}},
    });
    let call_reply = post_mcp(
        &router,
        &call_body,
        &[("mcp-protocol-version", PROTOCOL_2025)],
    )
    .await;
    let response = call_reply.json();
    assert!(response.get("error").is_some(), "{response}");
    assert!(response.get("result").is_none(), "{response}");
}

#[tokio::test]
async fn schema_tools_are_listed_by_default() {
    let router = app(one_db(PORT_18, &[]));
    let body = json!({"jsonrpc": "2.0", "id": 2, "method": "tools/list"});
    let reply = post_mcp(&router, &body, &[("mcp-protocol-version", PROTOCOL_2025)]).await;
    let tools = array(&result_of(&reply.json())["tools"], "tools").clone();
    for tool in &tools {
        if [
            "list_schemas",
            "list_objects",
            "describe_table",
            "describe_function",
            "search_schema",
            "get_privileges",
        ]
        .contains(&tool["name"].as_str().unwrap_or_default())
        {
            assert_eq!(tool["annotations"]["readOnlyHint"], true, "{tool}");
            assert_eq!(tool["annotations"]["destructiveHint"], false, "{tool}");
            assert!(tool["title"].is_string(), "{tool}");
        }
    }
    let names: Vec<&str> = tools.iter().filter_map(|t| t["name"].as_str()).collect();
    assert!(names.contains(&"describe_table") && names.contains(&"get_privileges"));
}

#[tokio::test]
async fn no_secret_in_any_output() {
    let router = app(one_db(PORT_18, &[]));
    let calls = [
        ("list_schemas", json!({})),
        ("list_objects", json!({})),
        ("describe_table", json!({"table": "shop.items_remote"})),
        ("describe_function", json!({"function": "shop.fmt"})),
        ("search_schema", json!({"query": "remote"})),
        ("get_privileges", json!({})),
    ];
    for (name, arguments) in calls {
        let result = call_on(&router, name, arguments).await;
        let serialized = result.to_string();
        for secret in SECRETS {
            assert!(!serialized.contains(secret), "{name} leaks {secret}");
        }
    }
}

#[tokio::test]
async fn list_schemas_across_all_three_servers() {
    for port in [PORT_18, PORT_19, PORT_15] {
        let result = call(one_db(port, &[]), "list_schemas", json!({})).await;
        let schemas = array(&ok(&result)["schemas"], "schemas").clone();
        let shop = find(&schemas, "name", "shop");
        assert_eq!(shop["tables"], 13, "port {port}");
        assert_eq!(shop["views"], 2, "port {port}");
    }
}

#[tokio::test]
async fn describe_table_orders_on_all_three_servers() {
    for port in [PORT_18, PORT_19, PORT_15] {
        let result = call(
            one_db(port, &[]),
            "describe_table",
            json!({"table": "shop.orders"}),
        )
        .await;
        let body = ok(&result).clone();
        assert_eq!(body["name"], "orders", "port {port}");
        let names = names_of(array(&body["columns"], "columns"));
        assert_eq!(
            names,
            vec!["id", "customer_id", "status", "total", "placed_at", "notes"],
            "port {port}"
        );
        assert_eq!(body["primaryKey"]["name"], "orders_pkey", "port {port}");
        assert_eq!(array(&body["indexes"], "indexes").len(), 4, "port {port}");
        assert_eq!(
            array(&body["foreignKeys"], "foreignKeys").len(),
            1,
            "port {port}"
        );
        assert_eq!(body["triggers"][0]["name"], "orders_touch", "port {port}");
    }
}

#[tokio::test]
async fn describe_table_internal_triggers_are_not_listed() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.customers"}),
    )
    .await;
    let body = ok(&result).clone();
    assert_eq!(
        body["triggers"],
        json!([]),
        "foreign key triggers are internal"
    );
    assert!(body["sizes"]["totalBytes"].as_i64().unwrap_or_default() > 0);
    assert!(body["stats"].is_object(), "{body}");
    assert!(body["stats"]["liveRows"].is_number(), "{body}");
    assert!(body["stats"].get("deadRows").is_some(), "{body}");
    let indexes = array(&body["indexes"], "indexes").clone();
    let email_key = find(&indexes, "name", "customers_email_key");
    assert_eq!(email_key["unique"], true);
    assert_eq!(email_key["method"], "btree");
    assert!(
        email_key["sizeBytes"].as_i64().unwrap_or_default() > 0,
        "{email_key}"
    );
    assert!(email_key["scans"].is_number(), "{email_key}");
}

#[tokio::test]
async fn describe_table_partitioned_row_estimate_is_leaf_sum_or_unknown() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(&router, "describe_table", json!({"table": "shop.events"})).await;
    let estimate = ok(&result)["rowEstimate"].as_i64();
    assert!(
        estimate == Some(300) || estimate == Some(-1),
        "sum of analyzed leaves (300) or -1: {estimate:?}"
    );
    let objects = call_on(
        &router,
        "list_objects",
        json!({"schema": "shop", "kinds": ["partitioned_table"]}),
    )
    .await;
    let events = find(&lists_objects(ok(&objects)).clone(), "name", "events").clone();
    assert_eq!(events["rowEstimate"].as_i64(), estimate, "{events}");
    assert!(
        events["totalBytes"].as_i64().unwrap_or_default() > 0,
        "{events}"
    );
}

/// The value of column `v` in the single row of an execute_query result on the same router.
async fn scalar_through_query(router: &Router, sql: &str) -> Value {
    let result = call_on(router, "execute_query", json!({"sql": sql})).await;
    let rows = array(&ok(&result)["rows"], "rows").clone();
    assert_eq!(rows.len(), 1, "{sql}");
    rows[0]["v"].clone()
}

#[tokio::test]
async fn table_bytes_agree_across_describe_table_table_stats_and_pg_table_size() {
    let router = app(one_db(PORT_18, &[]));
    let described = call_on(&router, "describe_table", json!({"table": "shop.orders"})).await;
    let described_table = ok(&described)["sizes"]["tableBytes"].clone();
    let stats = call_on(
        &router,
        "table_stats",
        json!({"schema": "shop", "table": "orders", "include_indexes": false}),
    )
    .await;
    let rows = array(&ok(&stats)["tables"], "tables").clone();
    assert_eq!(rows.len(), 1, "{stats}");
    let stats_table = rows[0]["tableBytes"].clone();
    let pg_table = scalar_through_query(
        &router,
        "SELECT pg_table_size('shop.orders'::regclass) AS v",
    )
    .await;
    assert_eq!(described_table, pg_table, "{described}");
    assert_eq!(stats_table, pg_table, "{stats}");
    assert!(pg_table.as_i64().unwrap_or_default() > 0, "{pg_table}");
}

#[tokio::test]
async fn describe_table_sizes_match_postgres_for_leaf_parent_and_plain_table() {
    let router = app(one_db(PORT_18, &[]));
    let leaf = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.events_2024_q1"}),
    )
    .await;
    let leaf_sizes = ok(&leaf)["sizes"].clone();
    let leaf_total = scalar_through_query(
        &router,
        "SELECT pg_total_relation_size('shop.events_2024_q1'::regclass) AS v",
    )
    .await;
    assert_eq!(leaf_sizes["totalBytes"], leaf_total, "{leaf_sizes}");
    let leaf_table = scalar_through_query(
        &router,
        "SELECT pg_table_size('shop.events_2024_q1'::regclass) AS v",
    )
    .await;
    assert_eq!(leaf_sizes["tableBytes"], leaf_table, "{leaf_sizes}");

    let parent = call_on(&router, "describe_table", json!({"table": "shop.events"})).await;
    let parent_total = ok(&parent)["sizes"]["totalBytes"].clone();
    let leaves_total = scalar_through_query(
        &router,
        "SELECT sum(pg_total_relation_size(t.relid))::bigint AS v \
         FROM pg_catalog.pg_partition_tree('shop.events'::regclass) t WHERE t.isleaf",
    )
    .await;
    assert_eq!(parent_total, leaves_total, "{parent}");
    assert!(parent_total.as_i64().unwrap_or_default() > 0, "{parent}");

    let plain = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.customers"}),
    )
    .await;
    let plain_total = scalar_through_query(
        &router,
        "SELECT pg_total_relation_size('shop.customers'::regclass) AS v",
    )
    .await;
    assert_eq!(ok(&plain)["sizes"]["totalBytes"], plain_total, "{plain}");
}

#[tokio::test]
async fn describe_function_aggregate_and_window_kinds() {
    let router = app(one_db(PORT_18, &[]));
    let aggregate = call_on(
        &router,
        "describe_function",
        json!({"function": "pg_catalog.count"}),
    )
    .await;
    let aggregates = array(&ok(&aggregate)["functions"], "functions").clone();
    assert!(!aggregates.is_empty());
    assert!(
        aggregates.iter().all(|f| f["kind"] == "aggregate"),
        "{aggregates:?}"
    );
    let window = call_on(
        &router,
        "describe_function",
        json!({"function": "pg_catalog.row_number"}),
    )
    .await;
    let windows = array(&ok(&window)["functions"], "functions").clone();
    assert!(!windows.is_empty());
    assert!(windows.iter().all(|f| f["kind"] == "window"), "{windows:?}");
}

#[tokio::test]
async fn search_kinds_table_includes_partitioned_and_views_by_kind() {
    let router = app(one_db(PORT_18, &[]));
    let tables = call_on(
        &router,
        "search_schema",
        json!({"query": "events", "schema": "shop", "kinds": ["table"]}),
    )
    .await;
    let matches = array(&ok(&tables)["matches"], "matches").clone();
    let events = find(&matches, "name", "events");
    assert_eq!(events["kind"], "table");
    assert!(matches.iter().all(|m| m["kind"] != "view"), "{matches:?}");
    let views = call_on(
        &router,
        "search_schema",
        json!({"query": "orders", "schema": "shop", "kinds": ["view"]}),
    )
    .await;
    let view_matches = array(&ok(&views)["matches"], "matches").clone();
    assert!(!view_matches.is_empty());
    assert!(
        view_matches
            .iter()
            .all(|m| m["kind"] == "view" || m["kind"] == "materialized_view"),
        "{view_matches:?}"
    );
    assert!(view_matches.iter().any(|m| m["name"] == "open_orders"));
}

#[tokio::test]
async fn privileges_report_session_user() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(&router, "get_privileges", json!({})).await;
    assert_eq!(ok(&result)["role"]["sessionUser"], "mcp_ro");
    assert_eq!(ok(&result)["role"]["inherit"], true);
    assert_eq!(ok(&result)["role"]["connectionLimit"], 20);
}

/// The role with CONNECT, USAGE on app and SELECT on app.items only (docker/initdb/56_test_objects.sql).
fn min_config(port: u16) -> Config {
    config_as(port, "mcp_min", "test-only-min")
}

#[tokio::test]
async fn privileges_min_role_reports_missing_grants() {
    let router = app(min_config(PORT_18));
    let result = call_on(&router, "get_privileges", json!({})).await;
    let body = ok(&result).clone();
    assert_eq!(body["role"]["name"], "mcp_min");
    assert_eq!(body["predefinedRoles"]["pgReadAllStats"], false);
    let features = &body["features"];
    assert_eq!(
        features["otherSessionsQueryText"],
        json!({"available": false, "missing": "GRANT pg_read_all_stats TO mcp_min;"})
    );
    assert_eq!(
        features["serverLog"],
        json!({
            "available": false,
            "missing": "Run sql/log_reader.sql as a superuser, then GRANT USAGE ON SCHEMA pgmcp TO mcp_min; GRANT EXECUTE ON FUNCTION pgmcp.read_log_file(text, bigint, integer), pgmcp.list_log_files() TO mcp_min;"
        })
    );
    // pg_stat_statements grants SELECT to PUBLIC, so the role can read the view.
    assert_eq!(features["pgStatStatements"], json!({"available": true}));
    assert_eq!(features["timescaledb"]["installed"], true);
}

#[tokio::test]
async fn list_schemas_min_role_shows_only_usable_schemas() {
    let router = app(min_config(PORT_18));
    let result = call_on(&router, "list_schemas", json!({})).await;
    let schemas = array(&ok(&result)["schemas"], "schemas").clone();
    assert_eq!(names_of(&schemas), vec!["app", "public"], "{schemas:?}");
}

#[tokio::test]
async fn describe_table_min_role_reads_its_own_table() {
    let router = app(min_config(PORT_18));
    let result = call_on(&router, "describe_table", json!({"table": "app.items"})).await;
    let body = ok(&result).clone();
    assert_eq!(body["schema"], "app");
    assert_eq!(body["name"], "items");
    assert_eq!(body["warnings"], json!([]), "{body}");
}

#[tokio::test]
async fn describe_table_min_role_warns_it_cannot_select_other_schemas() {
    let router = app(min_config(PORT_18));
    let result = call_on(&router, "describe_table", json!({"table": "shop.orders"})).await;
    let body = ok(&result).clone();
    assert_eq!(body["name"], "orders");
    assert_eq!(
        body["warnings"],
        json!(["The connected role cannot SELECT from this table."]),
        "{body}"
    );
}

#[tokio::test]
async fn describe_table_lists_at_most_200_partitions() {
    let router = app(one_db(PORT_18, &[]));
    let result = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.many_parts"}),
    )
    .await;
    let partitioning = &ok(&result)["partitioning"];
    assert_eq!(
        array(&partitioning["partitions"], "partitions").len(),
        200,
        "{partitioning}"
    );
    assert_eq!(partitioning["partitionsTruncated"], true, "{partitioning}");
}

#[tokio::test]
async fn describe_table_quoted_name_with_dot_and_uppercase() {
    let router = app(one_db(PORT_18, &[]));
    let bare = call_on(
        &router,
        "describe_table",
        json!({"table": "Odd.Name", "schema": "shop"}),
    )
    .await;
    assert_eq!(ok(&bare)["name"], "Odd.Name");
    assert_eq!(ok(&bare)["schema"], "shop");
    let qualified = call_on(
        &router,
        "describe_table",
        json!({"table": "shop.\"Odd.Name\""}),
    )
    .await;
    assert_eq!(ok(&qualified)["name"], "Odd.Name");
    let rows = call_on(
        &router,
        "execute_query",
        json!({"sql": "SELECT \"Select\" FROM shop.\"Odd.Name\""}),
    )
    .await;
    assert_eq!(ok(&rows)["columns"][0]["name"], "Select", "{rows}");
    assert_eq!(ok(&rows)["rows"], json!([{"Select": 1}]), "{rows}");
}

/// Every `required` array in a schema has unique names, and each one is a property of the same object.
fn check_required_names(node: &Value, path: &str) {
    let mut pending = vec![(node, path.to_owned())];
    while let Some((value, at)) = pending.pop() {
        match value {
            Value::Object(object) => {
                if let Some(required) = object.get("required") {
                    let names = required.as_array().expect("required is an array");
                    let mut seen = std::collections::HashSet::new();
                    for name in names {
                        let name = name.as_str().expect("required names are strings");
                        assert!(seen.insert(name), "{at}: {name} is required twice");
                        let known = object
                            .get("properties")
                            .and_then(Value::as_object)
                            .is_some_and(|props| props.contains_key(name));
                        assert!(known, "{at}: required {name} is not a property");
                    }
                }
                pending.extend(
                    object
                        .iter()
                        .map(|(key, child)| (child, format!("{at}.{key}"))),
                );
            }
            Value::Array(items) => {
                pending.extend(items.iter().map(|child| (child, format!("{at}[]"))));
            }
            _ => {}
        }
    }
}

/// Every tool's input and output schema lists each required name once, and only names it declares.
#[tokio::test]
async fn every_tool_schema_lists_each_required_name_once() {
    let router = app(one_db(PORT_18, &[]));
    let body = json!({"jsonrpc": "2.0", "id": 2, "method": "tools/list"});
    let reply = post_mcp(&router, &body, &[("mcp-protocol-version", PROTOCOL_2025)]).await;
    let tools = array(&result_of(&reply.json())["tools"], "tools").clone();
    assert!(tools.len() >= 20, "{} tools", tools.len());
    for tool in &tools {
        let name = tool["name"].as_str().expect("tool name");
        for key in ["inputSchema", "outputSchema"] {
            let schema = tool
                .get(key)
                .unwrap_or_else(|| panic!("{name} has no {key}"));
            check_required_names(schema, &format!("{name}.{key}"));
        }
    }
}