oxilite-cli 0.10.0

oxilite command line: load, query, and a SPARQL HTTP endpoint compatible with `oxigraph serve`
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
//! The server driven over an in-memory LSP connection against temporary workspaces.
use super::*;
use lsp_server::RequestId;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread::JoinHandle;

struct Client {
    connection: Connection,
    server: Option<JoinHandle<()>>,
    next_id: i32,
    dir: PathBuf,
    /// Notifications received while waiting for responses.
    inbox: Vec<Notification>,
    /// The server's answer to `initialize`.
    initialized: Value,
}

fn workspace(files: &[(&str, &str)]) -> PathBuf {
    static N: AtomicUsize = AtomicUsize::new(0);
    let dir = std::env::temp_dir().join(format!(
        "oxilite-studio-{}-{}",
        std::process::id(),
        N.fetch_add(1, Ordering::SeqCst)
    ));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    for (name, content) in files {
        write(&dir, name, content);
    }
    dir
}

fn write(dir: &std::path::Path, name: &str, content: &str) {
    let path = dir.join(name);
    std::fs::create_dir_all(path.parent().unwrap()).unwrap();
    std::fs::write(path, content).unwrap();
}

impl Client {
    fn start(dir: PathBuf) -> Self {
        let (server_side, connection) = Connection::memory();
        let server = std::thread::spawn(move || {
            serve(&server_side, Some(":memory:".into())).unwrap();
        });
        let mut client = Self {
            connection,
            server: Some(server),
            next_id: 0,
            dir,
            inbox: Vec::new(),
            initialized: Value::Null,
        };
        let root = url::Url::from_file_path(&client.dir).unwrap().to_string();
        client.initialized = client.ok(
            "initialize",
            json!({"capabilities": {}, "workspaceFolders": [{"uri": root, "name": "w"}]}),
        );
        client.send(Notification::new("initialized".into(), json!({})).into());
        client
    }

    fn send(&self, m: Message) {
        self.connection.sender.send(m).unwrap();
    }

    /// Sends a request and returns its response, keeping notifications in the inbox.
    fn call(&mut self, method: &str, params: Value) -> Response {
        self.next_id += 1;
        let id = RequestId::from(self.next_id);
        self.send(Request::new(id.clone(), method.into(), params).into());
        loop {
            match self.connection.receiver.recv().unwrap() {
                Message::Response(r) => {
                    assert_eq!(r.id, id);
                    return r;
                }
                Message::Notification(n) => self.inbox.push(n),
                Message::Request(_) => {}
            }
        }
    }

    fn ok(&mut self, method: &str, params: Value) -> Value {
        let r = self.call(method, params);
        assert!(r.error.is_none(), "{:?}", r.error);
        r.result.unwrap()
    }

    /// Takes the notifications received so far with this method.
    fn notifications(&mut self, method: &str) -> Vec<Value> {
        let (taken, kept) = std::mem::take(&mut self.inbox)
            .into_iter()
            .partition(|n| n.method == method);
        self.inbox = kept;
        taken.into_iter().map(|n: Notification| n.params).collect()
    }
}

impl Drop for Client {
    fn drop(&mut self) {
        let _ = self.call("shutdown", Value::Null);
        self.send(Notification::new("exit".into(), Value::Null).into());
        if let Some(s) = self.server.take() {
            let _ = s.join();
        }
        let _ = std::fs::remove_dir_all(&self.dir);
    }
}

const PEOPLE: &str =
    "@prefix ex: <http://ex.org/> .\nex:alice ex:name \"Alice\" .\nex:bob ex:name \"Bob\" .\n";
const KNOWS: &str = "@prefix ex: <http://ex.org/> .\nex:alice ex:knows ex:bob .\n";

// @tg: verifies:: [[tests#Studio server#Files load on initialize]] {since: "2026-09-24", level: unit}
#[test]
fn files_load_on_initialize() {
    let dir = workspace(&[
        ("people.ttl", PEOPLE),
        ("sub/knows.ttl", KNOWS),
        ("node_modules/skip.ttl", KNOWS),
        (".hidden/skip.ttl", KNOWS),
        ("notes.txt", "not rdf"),
    ]);
    let mut c = Client::start(dir);
    let status = c.ok("oxilite/status", Value::Null);
    assert_eq!(status["files"].as_array().unwrap().len(), 2);
    assert_eq!(status["triples"], 3);
}

// @tg: verifies:: [[tests#Studio server#Query joins across files]] {since: "2026-09-24", level: unit}
#[test]
fn query_joins_across_files() {
    let dir = workspace(&[("people.ttl", PEOPLE), ("knows.ttl", KNOWS)]);
    let mut c = Client::start(dir);
    let r = c.ok(
        "oxilite/query",
        json!({"query": "PREFIX ex: <http://ex.org/> SELECT ?n WHERE { ?a ex:knows ?b . ?b ex:name ?n }"}),
    );
    assert_eq!(r["kind"], "solutions");
    assert_eq!(r["variables"], json!(["n"]));
    assert_eq!(r["rows"][0][0]["value"], "Bob");
    assert_eq!(r["truncated"], false);
}

// @tg: verifies:: [[tests#Studio server#Row limit truncates]] {since: "2026-09-24", level: unit}
#[test]
fn row_limit_truncates() {
    let mut c = Client::start(workspace(&[("people.ttl", PEOPLE)]));
    let r = c.ok(
        "oxilite/query",
        json!({"query": "SELECT * WHERE { ?s ?p ?o }", "limit": 1}),
    );
    assert_eq!(r["rows"].as_array().unwrap().len(), 1);
    assert_eq!(r["truncated"], true);
}

// @tg: verifies:: [[tests#Studio server#Syntax error is a diagnostic]] {since: "2026-09-24", level: unit}
#[test]
fn syntax_error_is_a_diagnostic() {
    let broken = "@prefix ex: <http://ex.org/> .\nex:a ex:p ex:b .\nex:c ex:p \"unterminated .\n";
    let mut c = Client::start(workspace(&[("people.ttl", PEOPLE), ("broken.ttl", broken)]));
    let status = c.ok("oxilite/status", Value::Null);
    assert_eq!(status["triples"], 2, "the broken file loads nothing");
    let diagnostics = c.notifications(PublishDiagnostics::METHOD);
    assert_eq!(diagnostics.len(), 1);
    assert!(diagnostics[0]["uri"]
        .as_str()
        .unwrap()
        .ends_with("broken.ttl"));
    assert_eq!(
        diagnostics[0]["diagnostics"][0]["range"]["start"]["line"],
        2
    );
}

// @tg: verifies:: [[tests#Studio server#Reload picks up changes]] {since: "2026-09-24", level: unit}
#[test]
fn reload_picks_up_changes() {
    let broken = "@prefix ex: <http://ex.org/> .\nex:a ex:p .\n";
    let mut c = Client::start(workspace(&[("broken.ttl", broken)]));
    assert_eq!(c.ok("oxilite/status", Value::Null)["triples"], 0);
    c.notifications(PublishDiagnostics::METHOD);
    write(&c.dir.clone(), "broken.ttl", KNOWS);
    write(&c.dir.clone(), "people.ttl", PEOPLE);
    let status = c.ok("oxilite/reload", Value::Null);
    assert_eq!(status["triples"], 3);
    let cleared = c.notifications(PublishDiagnostics::METHOD);
    assert_eq!(cleared.len(), 1);
    assert_eq!(cleared[0]["diagnostics"], json!([]));
}

// @tg: verifies:: [[tests#Studio server#Query errors are request failures]] {since: "2026-09-24", level: unit}
#[test]
fn query_errors_are_request_failures() {
    let mut c = Client::start(workspace(&[]));
    let r = c.call("oxilite/query", json!({"query": "SELEC nothing"}));
    assert_eq!(r.error.unwrap().code, ErrorCode::RequestFailed as i32);
}

impl Client {
    fn open(&mut self, name: &str, language: &str, text: &str) -> String {
        let uri = url::Url::from_file_path(self.dir.join(name))
            .unwrap()
            .to_string();
        self.send(
            Notification::new(
                "textDocument/didOpen".into(),
                json!({"textDocument": {"uri": uri, "languageId": language, "version": 1, "text": text}}),
            )
            .into(),
        );
        uri
    }
}

fn at(uri: &str, line: u32, character: u32) -> Value {
    json!({"textDocument": {"uri": uri}, "position": {"line": line, "character": character}})
}

// @tg: verifies:: [[tests#Studio server#Hover and definition across files]] {since: "2026-09-24", level: unit}
#[test]
fn hover_and_definition_across_files() {
    let people = "@prefix ex: <http://ex.org/> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\nex:bob a ex:Person ; rdfs:label \"Bob\" .\n";
    let mut c = Client::start(workspace(&[("people.ttl", people), ("knows.ttl", KNOWS)]));
    let q = c.open(
        "q.rq",
        "sparql",
        "PREFIX ex: <http://ex.org/>\nSELECT * { ?x ex:knows ex:bob }",
    );
    let hover = c.ok("textDocument/hover", at(&q, 1, 27));
    let md = hover["contents"]["value"].as_str().unwrap();
    assert!(md.contains("**Bob**"), "{md}");
    assert!(md.contains("people.ttl:3"), "{md}");
    let defs = c.ok("textDocument/definition", at(&q, 1, 27));
    assert!(defs[0]["uri"].as_str().unwrap().ends_with("people.ttl"));
    assert_eq!(defs[0]["range"]["start"]["line"], 2);
    let mut params = at(&q, 1, 27);
    params["context"] = json!({"includeDeclaration": true});
    let refs = c.ok("textDocument/references", params);
    assert_eq!(refs.as_array().unwrap().len(), 2);
}

// @tg: verifies:: [[tests#Studio server#Initialize announces its capabilities]] {since: "2026-09-25", level: unit}
#[test]
fn initialize_announces_its_capabilities() {
    let c = Client::start(workspace(&[]));
    // Nested one level too deep, a client sees no capabilities and never syncs documents.
    let caps = &c.initialized["capabilities"];
    assert_eq!(caps["textDocumentSync"], 1);
    assert!(caps["completionProvider"]["triggerCharacters"].is_array());
    assert_eq!(caps["hoverProvider"], true);
    assert_eq!(c.initialized["serverInfo"]["name"], "oxilite studio-server");
}

// @tg: verifies:: [[tests#Studio server#Completion follows the document's connection]] {since: "2026-09-25", level: unit}
#[test]
fn completion_follows_the_documents_connection() {
    let mut c = Client::start(workspace(&[("people.ttl", PEOPLE)]));
    let db = c.dir.join("side.sqlite").display().to_string();
    c.ok(
        "oxilite/attach",
        json!({"path": db, "readOnly": false, "activate": false}),
    );
    let id = format!("attached:{db}");
    c.ok(
        "oxilite/query",
        json!({"query": "INSERT DATA { <http://ex.org/a> <http://ex.org/salary> 1 }", "connection": id, "confirmed": true}),
    );
    let q = c.open(
        "q.rq",
        "sparql",
        "PREFIX ex: <http://ex.org/>\nSELECT * { ?x ex:",
    );
    let labels = |c: &mut Client| -> Vec<String> {
        let items = c.ok("textDocument/completion", at(&q, 1, 17));
        items
            .as_array()
            .unwrap()
            .iter()
            .map(|i| i["label"].as_str().unwrap().to_string())
            .collect()
    };
    // Terms written in workspace files are offered on any connection; the store's own come first.
    assert_eq!(labels(&mut c), ["ex:name"], "the active Project store");
    let pin = |c: &Client, connection: Value| {
        c.send(
            Notification::new(
                "oxilite/documentConnection".into(),
                json!({"uri": q, "connection": connection}),
            )
            .into(),
        );
    };
    pin(&c, json!(id));
    assert_eq!(
        labels(&mut c),
        ["ex:salary", "ex:name"],
        "the pinned side database"
    );
    pin(&c, Value::Null);
    assert_eq!(labels(&mut c), ["ex:name"], "unpinned again");
}

// @tg: verifies:: [[tests#Studio server#Completion over LSP uses the store]] {since: "2026-09-24", level: unit}
#[test]
fn completion_over_lsp_uses_the_store() {
    let mut c = Client::start(workspace(&[("people.ttl", PEOPLE), ("knows.ttl", KNOWS)]));
    let q = c.open(
        "q.rq",
        "sparql",
        "PREFIX ex: <http://ex.org/>\nSELECT * { ?x ex:",
    );
    let items = c.ok("textDocument/completion", at(&q, 1, 17));
    let labels: Vec<&str> = items
        .as_array()
        .unwrap()
        .iter()
        .map(|i| i["label"].as_str().unwrap())
        .collect();
    assert_eq!(labels, ["ex:name", "ex:knows"]);
    let diagnostics = c.notifications(PublishDiagnostics::METHOD);
    assert!(diagnostics
        .iter()
        .any(|d| d["uri"] == q.as_str() && !d["diagnostics"].as_array().unwrap().is_empty()));
}

// @tg: verifies:: [[tests#Studio server#Attached stores and confirmed updates]] {since: "2026-09-24", level: unit}
#[test]
fn attached_store_needs_confirmation() {
    let mut c = Client::start(workspace(&[("people.ttl", PEOPLE)]));
    let db = c.dir.join("real.sqlite").display().to_string();
    let list = c.ok("oxilite/attach", json!({"path": db}));
    assert_eq!(list.as_array().unwrap().len(), 2);
    assert_eq!(list[1]["active"], true);
    let insert = json!({"query": "INSERT DATA { <http://ex.org/a> <http://ex.org/p> 1 }"});
    let r = c.call("oxilite/query", insert.clone());
    assert_eq!(r.error.unwrap().code, NEEDS_CONFIRMATION);
    let mut confirmed = insert;
    confirmed["confirmed"] = json!(true);
    let r = c.ok("oxilite/query", confirmed);
    assert_eq!(r["kind"], "update");
    assert_eq!(r["delta"], 1);
    let rows = c.ok("oxilite/query", json!({"query": "SELECT * { ?s ?p ?o }"}));
    assert_eq!(rows["rows"].as_array().unwrap().len(), 1);
    // The project store is separate and updates to it are marked as ephemeral.
    let r = c.ok("oxilite/query", json!({"query": "INSERT DATA { <http://ex.org/x> <http://ex.org/p> 2 }", "connection": "project"}));
    assert_eq!(r["ephemeral"], true);
    c.ok("oxilite/detach", json!({"id": format!("attached:{db}")}));
    let list = c.ok("oxilite/connections", Value::Null);
    assert_eq!(list[0]["active"], true);
}

// @tg: verifies:: [[tests#Studio server#Explain returns the SQL]] {since: "2026-09-24", level: unit}
#[test]
fn explain_returns_the_sql() {
    let mut c = Client::start(workspace(&[("people.ttl", PEOPLE)]));
    let plan = c.ok(
        "oxilite/explain",
        json!({"query": "PREFIX ex: <http://ex.org/> SELECT ?n WHERE { ?x ex:name ?n }"}),
    );
    assert!(plan["text"].as_str().unwrap().contains("SELECT"), "{plan}");
}

impl Client {
    /// Waits for the next notification with this method (skipping others), up to ten seconds.
    fn wait_for(&mut self, method: &str) -> Value {
        if let Some(i) = self.inbox.iter().position(|n| n.method == method) {
            return self.inbox.remove(i).params;
        }
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        loop {
            let left = deadline.saturating_duration_since(std::time::Instant::now());
            match self
                .connection
                .receiver
                .recv_timeout(left)
                .expect("notification in time")
            {
                Message::Notification(n) if n.method == method => return n.params,
                Message::Notification(n) => self.inbox.push(n),
                _ => {}
            }
        }
    }

    /// Waits for diagnostics published for `uri`.
    fn diagnostics_for(&mut self, uri: &str) -> Value {
        loop {
            let d = self.wait_for(PublishDiagnostics::METHOD);
            if d["uri"] == uri {
                return d;
            }
        }
    }

    fn changed(&mut self, names: &[&str]) {
        let changes: Vec<Value> = names
            .iter()
            .map(|n| json!({"uri": url::Url::from_file_path(self.dir.join(n)).unwrap().to_string(), "type": 2}))
            .collect();
        self.send(
            Notification::new(
                "workspace/didChangeWatchedFiles".into(),
                json!({"changes": changes}),
            )
            .into(),
        );
    }
}

const ONTOLOGY: &str = "@prefix ex: <http://ex.org/> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n@prefix owl: <http://www.w3.org/2002/07/owl#> .\n<http://ex.org/onto> a owl:Ontology .\nex:Employee rdfs:subClassOf ex:Person .\n";
const STAFF: &str = "@prefix ex: <http://ex.org/> .\nex:carol a ex:Employee ; ex:name \"Carol\" .\nex:dan a ex:Employee .\n";
const SHAPES: &str = "@prefix ex: <http://ex.org/> .\n@prefix sh: <http://www.w3.org/ns/shacl#> .\nex:PersonShape a sh:NodeShape ;\n  sh:targetClass ex:Person ;\n  sh:property [ sh:path ex:name ; sh:minCount 1 ] .\n";

// @tg: verifies:: [[tests#Studio server#SHACL results land on the data line]] {since: "2026-09-24", level: unit}
#[test]
fn shacl_results_land_on_the_data_line() {
    let mut c = Client::start(workspace(&[
        ("onto.ttl", ONTOLOGY),
        ("staff.ttl", STAFF),
        ("shapes.ttl", SHAPES),
    ]));
    // Without reasoning, nobody is a Person: the data conforms.
    let report = c.wait_for("oxilite/validationChanged");
    assert_eq!(report["conforms"], true, "{report}");
    // With RDFS, employees are persons, and dan has no name.
    c.inbox.clear();
    c.ok("oxilite/setReasoning", json!({"profile": "rdfs"}));
    let report = c.wait_for("oxilite/validationChanged");
    assert_eq!(report["conforms"], false, "{report}");
    let results = report["results"].as_array().unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0]["focus"].as_str().unwrap(), "http://ex.org/dan");
    let diagnostics: Vec<Value> = c
        .notifications(PublishDiagnostics::METHOD)
        .into_iter()
        .filter(|d| {
            d["uri"].as_str().unwrap().ends_with("staff.ttl")
                && !d["diagnostics"].as_array().unwrap().is_empty()
        })
        .collect();
    let d = &diagnostics.last().unwrap()["diagnostics"][0];
    assert_eq!(d["source"], "shacl");
    assert_eq!(d["range"]["start"]["line"], 2, "dan's line");
    assert!(d["relatedInformation"][0]["location"]["uri"]
        .as_str()
        .unwrap()
        .ends_with("shapes.ttl"));
    // Shapes are not data.
    let rows = c.ok(
        "oxilite/query",
        json!({"query": "ASK { ?s a <http://www.w3.org/ns/shacl#NodeShape> }"}),
    );
    assert_eq!(rows["value"], false);
}

// @tg: verifies:: [[tests#Studio server#Manifest graphs and per-graph reload]] {since: "2026-09-24", level: unit}
#[test]
fn manifest_graphs_and_per_graph_reload() {
    let manifest = "reasoning = \"owl2rl\"\n[[graph]]\niri = \"https://ex.org/g/staff\"\nfiles = [\"data/*.ttl\"]\n[[graph]]\niri = \"https://ex.org/g/onto\"\nfiles = [\"onto.ttl\"]\nrole = \"ontology\"\n[rules]\nfiles = [\"rules/*.dl\"]\n";
    let rules = "@prefix ex: <http://ex.org/> .\nex:colleague(?x, ?y) :- ex:Employee(?x), ex:Employee(?y), ?x != ?y.\n";
    let mut c = Client::start(workspace(&[
        ("oxilite.toml", manifest),
        ("onto.ttl", ONTOLOGY),
        ("data/staff.ttl", STAFF),
        (
            "data/more.ttl",
            "@prefix ex: <http://ex.org/> .\nex:erin a ex:Employee .\n",
        ),
        ("rules/colleagues.dl", rules),
        ("ignored.ttl", KNOWS),
    ]));
    let status = c.ok("oxilite/status", Value::Null);
    assert_eq!(status["profile"], "owl2rl");
    assert_eq!(status["files"].as_array().unwrap().len(), 4, "{status}");
    let count = |c: &mut Client, q: &str| {
        c.ok("oxilite/query", json!({"query": q}))["rows"]
            .as_array()
            .unwrap()
            .len()
    };
    assert_eq!(
        count(
            &mut c,
            "SELECT ?x WHERE { GRAPH <https://ex.org/g/staff> { ?x a ?t } }"
        ),
        3
    );
    // OWL 2 RL makes employees persons; the rule makes them colleagues.
    assert_eq!(
        count(&mut c, "SELECT ?x WHERE { ?x a <http://ex.org/Person> }"),
        3
    );
    assert_eq!(
        count(&mut c, "SELECT * WHERE { ?x <http://ex.org/colleague> ?y }"),
        6
    );
    // Changing one file of the staff graph reloads that graph only, and reasoning follows.
    write(
        &c.dir.clone(),
        "data/more.ttl",
        "@prefix ex: <http://ex.org/> .\n",
    );
    c.changed(&["data/more.ttl"]);
    c.wait_for("oxilite/storeChanged");
    assert_eq!(
        count(&mut c, "SELECT ?x WHERE { ?x a <http://ex.org/Person> }"),
        2
    );
    assert_eq!(
        count(&mut c, "SELECT * WHERE { ?x <http://ex.org/colleague> ?y }"),
        2
    );
    // The resource view knows what is inferred, and by which producer.
    let d = c.ok("oxilite/describe", json!({"iri": "http://ex.org/carol"}));
    let colleague = d["outgoing"]
        .as_array()
        .unwrap()
        .iter()
        .find(|r| r["p"]["value"] == "http://ex.org/colleague")
        .unwrap();
    assert_eq!(colleague["inferred"], true);
    assert_eq!(colleague["producers"], json!(["rules/colleagues.dl"]));
    let person = d["outgoing"]
        .as_array()
        .unwrap()
        .iter()
        .find(|r| r["o"]["value"] == "http://ex.org/Person")
        .unwrap();
    assert_eq!(person["producers"], json!(["owl2rl"]));
    let name = d["outgoing"]
        .as_array()
        .unwrap()
        .iter()
        .find(|r| r["p"]["value"] == "http://ex.org/name")
        .unwrap();
    assert_eq!(name["inferred"], false);
}

// @tg: verifies:: [[tests#Studio server#Ontology graphs apply where the manifest maps them]] {since: "2026-09-26", level: unit}
#[test]
fn ontology_graphs_apply_where_mapped() {
    let manifest = "reasoning = \"rdfs\"\n[[graph]]\niri = \"https://ex.org/g/staff\"\nfiles = [\"staff.ttl\"]\n[[graph]]\niri = \"https://ex.org/g/guests\"\nfiles = [\"guests.ttl\"]\n[[graph]]\niri = \"https://ex.org/g/onto\"\nfiles = [\"onto.ttl\"]\nrole = \"ontology\"\napplies_to = [\"https://ex.org/g/staff\"]\n";
    let mut c = Client::start(workspace(&[
        ("oxilite.toml", manifest),
        ("onto.ttl", ONTOLOGY),
        ("staff.ttl", STAFF),
        (
            "guests.ttl",
            "@prefix ex: <http://ex.org/> .\nex:gus a ex:Employee .\n",
        ),
    ]));
    let rows = c.ok(
        "oxilite/query",
        json!({"query": "SELECT ?x WHERE { ?x a <http://ex.org/Person> } ORDER BY ?x"}),
    );
    let people: Vec<String> = rows["rows"]
        .as_array()
        .unwrap()
        .iter()
        .map(|r| r[0]["value"].as_str().unwrap_or_default().to_owned())
        .collect();
    assert_eq!(
        people,
        ["http://ex.org/carol", "http://ex.org/dan"],
        "{rows}"
    );
    let registry = c.ok(
        "oxilite/query",
        json!({"query": "ASK { GRAPH <oxilite:schema> { <https://ex.org/g/onto> <https://oxilite.dev/ns#appliesTo> <https://ex.org/g/staff> } }"}),
    );
    assert_eq!(registry["value"], true, "{registry}");
}

// @tg: verifies:: [[tests#Studio server#Explorer shows the class hierarchy]] {since: "2026-09-24", level: unit}
#[test]
fn explorer_shows_the_class_hierarchy() {
    let mut c = Client::start(workspace(&[("onto.ttl", ONTOLOGY), ("staff.ttl", STAFF)]));
    c.ok("oxilite/setReasoning", json!({"profile": "rdfs"}));
    let root = c.ok("oxilite/explorer", json!({"node": "root"}));
    let ids: Vec<&str> = root
        .as_array()
        .unwrap()
        .iter()
        .map(|n| n["id"].as_str().unwrap())
        .collect();
    assert_eq!(
        ids,
        ["graphs", "classes", "properties", "files", "prefixes"]
    );
    let classes = c.ok("oxilite/explorer", json!({"node": "classes"}));
    let person = classes
        .as_array()
        .unwrap()
        .iter()
        .find(|n| n["iri"] == "http://ex.org/Person")
        .unwrap();
    assert_eq!(person["collapsible"], true);
    assert_eq!(person["description"], "0 (+2 inferred)");
    let subs = c.ok(
        "oxilite/explorer",
        json!({"node": "class:http://ex.org/Person"}),
    );
    assert_eq!(subs[0]["iri"], "http://ex.org/Employee");
    assert_eq!(subs[0]["description"], "2");
    let files = c.ok("oxilite/explorer", json!({"node": "files"}));
    assert!(files[0]["description"]
        .as_str()
        .unwrap()
        .starts_with("ontology"));
}

// @tg: verifies:: [[tests#Studio server#Datalog runs and reports errors]] {since: "2026-09-24", level: unit}
#[test]
fn datalog_runs_and_reports_errors() {
    let family =
        "@prefix ex: <http://ex.org/> .\nex:ada ex:parent ex:bob .\nex:bob ex:parent ex:cy .\n";
    let mut c = Client::start(workspace(&[("family.ttl", family)]));
    let program = "@prefix ex: <http://ex.org/> .\nex:ancestor(?x, ?y) :- ex:parent(?x, ?y).\nex:ancestor(?x, ?z) :- ex:parent(?x, ?y), ex:ancestor(?y, ?z).\n?- ex:ancestor(ex:ada, ?who).\n";
    let r = c.ok("oxilite/datalog", json!({"query": program}));
    assert_eq!(r["kind"], "solutions");
    assert_eq!(r["rows"].as_array().unwrap().len(), 2);
    let plan = c.ok(
        "oxilite/explain",
        json!({"query": program, "language": "datalog"}),
    );
    assert_eq!(plan["kind"], "datalog");
    // An unsafe rule is reported on its head.
    let bad = "@prefix ex: <http://ex.org/> .\n\nex:orphan(?x, ?y) :- ex:parent(?x, ?z).\n";
    let uri = c.open("bad.dl", "datalog", bad);
    let d = c.diagnostics_for(&uri);
    assert!(
        d["diagnostics"][0]["message"]
            .as_str()
            .unwrap()
            .contains("unsafe"),
        "{d}"
    );
    assert_eq!(d["diagnostics"][0]["range"]["start"]["line"], 2);
    // Completion offers the store's predicates in rule bodies.
    let uri = c.open(
        "new.dl",
        "datalog",
        "@prefix ex: <http://ex.org/> .\nex:x(?a, ?b) :- ex:",
    );
    let items = c.ok("textDocument/completion", at(&uri, 1, 19));
    assert!(
        items
            .as_array()
            .unwrap()
            .iter()
            .any(|i| i["label"] == "ex:parent"),
        "{items}"
    );
}

// @tg: verifies:: [[tests#Studio server#Cypher runs over the data's own names]] {since: "2026-09-24", level: unit}
#[test]
fn cypher_runs_over_the_datas_own_names() {
    let people = "@prefix ex: <http://ex.org/> .\nex:alice a ex:Person ; ex:name \"Alice\" ; ex:knows ex:bob .\nex:bob a ex:Person ; ex:name \"Bob\" .\n";
    let mut c = Client::start(workspace(&[("people.ttl", people)]));
    // The first request is Cypher: the vocabulary (and its base namespace) is computed for it.
    let r = c.ok(
        "oxilite/cypher",
        json!({"query": "MATCH (a:Person)-[:knows]->(b:Person) RETURN a.name AS a, b.name AS b"}),
    );
    assert_eq!(r["kind"], "cypher");
    assert_eq!(r["rows"], json!([["Alice", "Bob"]]));
    let paths = c.ok(
        "oxilite/cypher",
        json!({"query": "MATCH p = (:Person)-[:knows]->(:Person) RETURN p"}),
    );
    assert_eq!(paths["rows"][0][0]["type"], "path");
    let uri = c.open("q.cypher", "cypher", "MATCH (p:Pe");
    let items = c.ok("textDocument/completion", at(&uri, 0, 11));
    assert_eq!(items[0]["label"], "Person");
    let uri = c.open("r.cypher", "cypher", "MATCH (a)-[:kn");
    let items = c.ok("textDocument/completion", at(&uri, 0, 14));
    assert!(
        items
            .as_array()
            .unwrap()
            .iter()
            .any(|i| i["label"] == "knows"),
        "{items}"
    );
    let uri = c.open("bad.cypher", "cypher", "MATCH (n RETURN n");
    let d = c.diagnostics_for(&uri);
    assert!(!d["diagnostics"].as_array().unwrap().is_empty());
}

// @tg: verifies:: [[tests#Studio server#Import and export round-trip]] {since: "2026-09-24", level: unit}
#[test]
fn import_and_export_round_trip() {
    let mut c = Client::start(workspace(&[]));
    let dir = c.dir.clone();
    write(
        &dir,
        "extra/people.nt",
        "<http://ex.org/a> <http://ex.org/p> \"x\" .\n",
    );
    let db = dir.join("real.sqlite").display().to_string();
    c.ok("oxilite/attach", json!({"path": db}));
    let path = dir.join("extra/people.nt").display().to_string();
    let r = c.call("oxilite/import", json!({"path": path}));
    assert_eq!(r.error.unwrap().code, NEEDS_CONFIRMATION);
    let r = c.ok(
        "oxilite/import",
        json!({"path": path, "graph": "http://ex.org/g", "confirmed": true}),
    );
    assert_eq!(r["added"], 1);
    let out = dir.join("out.nq").display().to_string();
    c.ok("oxilite/export", json!({"path": out}));
    let text = std::fs::read_to_string(&out).unwrap();
    assert!(text.contains("<http://ex.org/g>"), "{text}");
    let r = c.call(
        "oxilite/export",
        json!({"path": dir.join("out.ttl").display().to_string()}),
    );
    assert!(r.error.is_some(), "a dataset does not fit in Turtle");
}

fn iri(v: &str) -> Value {
    json!({"termType": "NamedNode", "value": v})
}

// @tg: verifies:: [[tests#Studio server#Why explains inferences down to asserted lines]] {since: "2026-09-24", level: unit}
#[test]
fn why_explains_inferences_down_to_asserted_lines() {
    let manifest = "reasoning = \"owl2rl\"\n[[graph]]\niri = \"https://ex.org/g/all\"\nfiles = [\"*.ttl\"]\n[rules]\nfiles = [\"rules/*.dl\"]\n";
    let onto = "@prefix ex: <http://ex.org/> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\nex:Manager rdfs:subClassOf ex:Employee .\nex:Employee rdfs:subClassOf ex:Person .\n";
    let data = "@prefix ex: <http://ex.org/> .\nex:ann a ex:Manager .\nex:ann ex:manages ex:bo .\n";
    let rules =
        "@prefix ex: <http://ex.org/> .\nex:boss(?y, ?x) :- ex:manages(?x, ?y), ex:Person(?x).\n";
    let mut c = Client::start(workspace(&[
        ("oxilite.toml", manifest),
        ("onto.ttl", onto),
        ("data.ttl", data),
        ("rules/boss.dl", rules),
    ]));
    // ann is a Person through two subclass steps.
    let tree = c.ok("oxilite/why", json!({"s": iri("http://ex.org/ann"), "p": iri(scanner::RDF_TYPE), "o": iri("http://ex.org/Person")}));
    assert_eq!(tree["status"], "inferred", "{tree}");
    assert_eq!(tree["producer"], "owl2rl");
    assert!(
        tree["rule"].as_str().unwrap().starts_with("cax-sco"),
        "{tree}"
    );
    let leaves: Vec<&Value> = tree["premises"].as_array().unwrap().iter().collect();
    assert!(
        leaves.iter().any(|p| p["status"] == "inferred"),
        "one premise is itself inferred: {tree}"
    );
    // The rule conclusion names its rule and bottoms out in asserted lines.
    let tree = c.ok("oxilite/why", json!({"s": iri("http://ex.org/bo"), "p": iri("http://ex.org/boss"), "o": iri("http://ex.org/ann")}));
    assert_eq!(tree["producer"], "rules/boss.dl", "{tree}");
    assert!(tree["rule"].as_str().unwrap().contains(":-"));
    let premises = tree["premises"].as_array().unwrap();
    assert_eq!(premises.len(), 2);
    assert_eq!(premises[0]["status"], "asserted");
    assert!(premises[0]["location"]["uri"]
        .as_str()
        .unwrap()
        .ends_with("data.ttl"));
    assert_eq!(premises[0]["location"]["range"]["start"]["line"], 2);
    assert_eq!(
        premises[1]["status"], "inferred",
        "ann is a Person by the ontology"
    );
    // An asserted triple is a leaf; a triple that does not hold says so.
    let leaf = c.ok("oxilite/why", json!({"s": iri("http://ex.org/ann"), "p": iri(scanner::RDF_TYPE), "o": iri("http://ex.org/Manager")}));
    assert_eq!(leaf["status"], "asserted");
    let absent = c.ok("oxilite/why", json!({"s": iri("http://ex.org/bo"), "p": iri(scanner::RDF_TYPE), "o": iri("http://ex.org/Person")}));
    assert_eq!(absent["status"], "absent");
}

const TEST_MANIFEST: &str = r#"
[[graph]]
iri = "https://ex.org/g/people"
files = ["data/*.ttl"]
[shapes]
files = ["shapes/*.ttl"]
[rules]
files = ["rules/*.dl"]

[[test]]
name = "people have names"
query = "tests/names.rq"
expect = "tests/names.srj"

[[test]]
name = "stale expectation"
query = "tests/names.rq"
expect = "tests/wrong.srj"

[[test]]
name = "bad person rejected"
data = "tests/bad.ttl"
expect_violations = ["ex:PersonShape"]

[[test]]
name = "project conforms"

[[test]]
name = "ancestry is transitive"
entails = "tests/yes.ttl"
not_entails = "tests/no.ttl"
"#;

fn test_workspace() -> PathBuf {
    let shapes = "@prefix ex: <http://ex.org/> .\n@prefix sh: <http://www.w3.org/ns/shacl#> .\nex:PersonShape a sh:NodeShape ; sh:targetClass ex:Person ; sh:property [ sh:path ex:name ; sh:minCount 1 ] .\n";
    workspace(&[
        ("oxilite.toml", TEST_MANIFEST),
        ("data/people.ttl", "@prefix ex: <http://ex.org/> .\nex:ada a ex:Person ; ex:name \"Ada\" ; ex:parent ex:bob .\nex:bob a ex:Person ; ex:name \"Bob\" ; ex:parent ex:cy .\n"),
        ("shapes/person.ttl", shapes),
        ("rules/family.dl", "@prefix ex: <http://ex.org/> .\nex:ancestor(?x, ?y) :- ex:parent(?x, ?y).\nex:ancestor(?x, ?z) :- ex:parent(?x, ?y), ex:ancestor(?y, ?z).\n"),
        ("tests/names.rq", "PREFIX ex: <http://ex.org/> SELECT ?n WHERE { ?p ex:name ?n }"),
        ("tests/wrong.srj", r#"{"head": {"vars": ["n"]}, "results": {"bindings": [{"n": {"type": "literal", "value": "Zed"}}]}}"#),
        ("tests/bad.ttl", "@prefix ex: <http://ex.org/> .\nex:nobody a ex:Person .\n"),
        ("tests/yes.ttl", "@prefix ex: <http://ex.org/> .\nex:ada ex:ancestor ex:cy .\n"),
        ("tests/no.ttl", "@prefix ex: <http://ex.org/> .\nex:cy ex:ancestor ex:ada .\n"),
    ])
}

// @tg: verifies:: [[tests#Studio server#Manifest tests run and snapshot]] {since: "2026-09-24", level: unit}
#[test]
fn manifest_tests_run_and_snapshot() {
    let mut c = Client::start(test_workspace());
    let tests = c.ok("oxilite/tests", Value::Null);
    assert_eq!(tests.as_array().unwrap().len(), 5);
    assert!(tests[0]["line"].as_u64().unwrap() > 0);
    let run = |c: &mut Client, name: &str| c.ok("oxilite/runTest", json!({"name": name}));
    // No expected file yet: the test fails until a snapshot is written.
    assert_eq!(run(&mut c, "people have names")["passed"], false);
    c.ok(
        "oxilite/updateSnapshot",
        json!({"name": "people have names"}),
    );
    assert_eq!(run(&mut c, "people have names")["passed"], true);
    let stale = run(&mut c, "stale expectation");
    assert_eq!(stale["passed"], false);
    assert!(
        stale["message"].as_str().unwrap().contains("missing"),
        "{stale}"
    );
    let bad = run(&mut c, "bad person rejected");
    assert_eq!(bad["passed"], true, "{bad}");
    assert_eq!(run(&mut c, "project conforms")["passed"], true);
    let entail = run(&mut c, "ancestry is transitive");
    assert_eq!(entail["passed"], true, "{entail}");
}

// @tg: verifies:: [[tests#Studio server#Check reports everything and fails]] {since: "2026-09-24", level: unit}
#[test]
fn check_reports_everything_and_fails() {
    let dir = test_workspace();
    let (passed, report) = check::check(dir.clone()).unwrap();
    assert!(!passed, "the stale expectation fails");
    let failing: Vec<&str> = report["tests"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|t| t["passed"] == false)
        .map(|t| t["name"].as_str().unwrap())
        .collect();
    assert_eq!(failing, ["people have names", "stale expectation"]);
    assert_eq!(report["conforms"], true);
    let text = check::render(&report);
    assert!(text.contains("FAIL    stale expectation"), "{text}");
    write(
        &dir,
        "data/people.ttl",
        "@prefix ex: <http://ex.org/> .\nex:ada a ex:Person .\n",
    );
    let (_, report) = check::check(dir.clone()).unwrap();
    assert_eq!(report["conforms"], false);
    assert_eq!(report["violations"][0]["file"], "data/people.ttl");
    assert_eq!(report["violations"][0]["line"], 2);
    let _ = std::fs::remove_dir_all(dir);
}

/// A stand-in for D1's `/raw` endpoint: runs each statement on a SQLite database and answers
/// in D1's JSON shape, with `meta` counts.
fn fake_d1() -> (String, std::thread::JoinHandle<()>, Arc<tiny_http::Server>) {
    use oxilite::rusqlite::RusqliteBackend;
    use oxilite_core::{Request as SqlRequest, SqlValue, Statement, SyncBackend};
    let server = Arc::new(tiny_http::Server::http("127.0.0.1:0").unwrap());
    let url = format!("http://{}", server.server_addr().to_ip().unwrap());
    let db = RusqliteBackend::open(":memory:").unwrap();
    let s = Arc::clone(&server);
    let handle = std::thread::spawn(move || {
        for mut request in s.incoming_requests() {
            assert_eq!(
                request
                    .headers()
                    .iter()
                    .find(|h| h.field.equiv("Authorization"))
                    .unwrap()
                    .value
                    .as_str(),
                "Bearer secret"
            );
            let mut body = String::new();
            request.as_reader().read_to_string(&mut body).unwrap();
            let v: Value = serde_json::from_str(&body).unwrap();
            let mut results = Vec::new();
            let mut ok = true;
            let mut message = String::new();
            for sql in v["sql"].as_str().unwrap().split(";\n") {
                match db.execute(&SqlRequest::atomic(vec![Statement::new(sql)])) {
                    Ok(r) => {
                        let rs = r.into_iter().next().unwrap_or_default();
                        let rows: Vec<Value> = rs
                            .rows
                            .iter()
                            .map(|row| {
                                json!(row
                                    .iter()
                                    .map(|c| match c {
                                        SqlValue::Null => Value::Null,
                                        SqlValue::Integer(i) => json!(i),
                                        SqlValue::Real(f) => json!(f),
                                        SqlValue::Text(t) => json!(t),
                                    })
                                    .collect::<Vec<_>>())
                            })
                            .collect();
                        results.push(json!({
                            "results": {"columns": [], "rows": rows},
                            "meta": {"changes": rs.changes, "rows_read": rows.len(), "rows_written": rs.changes},
                            "success": true,
                        }));
                    }
                    Err(e) => {
                        ok = false;
                        message = e.to_string();
                        break;
                    }
                }
            }
            let answer = if ok {
                json!({"success": true, "errors": [], "result": results})
            } else {
                json!({"success": false, "errors": [{"message": message}], "result": []})
            };
            let _ = request.respond(tiny_http::Response::from_string(answer.to_string()));
        }
    });
    (url, handle, server)
}

// @tg: verifies:: [[tests#Studio server#D1 over HTTP with billing]] {since: "2026-09-24", level: unit}
#[test]
fn d1_over_http_with_billing() {
    let (url, handle, server) = fake_d1();
    let mut c = Client::start(workspace(&[]));
    let list = c.ok(
        "oxilite/attachD1",
        json!({"account": "acc", "database": "db0123456789", "token": "secret", "readOnly": false, "endpoint": url}),
    );
    let d1 = list
        .as_array()
        .unwrap()
        .iter()
        .find(|x| x["kind"] == "d1")
        .unwrap()
        .clone();
    assert_eq!(d1["active"], true);
    assert_eq!(d1["label"], "D1 db012345");
    // An update asks first, with the rows it will write.
    let insert = json!({"query": "INSERT DATA { <http://ex.org/a> <http://ex.org/p> 1 . <http://ex.org/b> <http://ex.org/p> 2 }"});
    let r = c.call("oxilite/query", insert.clone());
    let e = r.error.unwrap();
    assert_eq!(e.code, NEEDS_CONFIRMATION);
    assert_eq!(
        e.data.unwrap()["estimate"],
        "about 10 billed rows written (2 quads)"
    );
    let mut confirmed = insert;
    confirmed["confirmed"] = json!(true);
    let r = c.ok("oxilite/query", confirmed);
    assert_eq!(r["delta"], 2);
    assert!(r["d1"]["rowsWritten"].as_u64().unwrap() > 0, "{r}");
    // 64-bit ids survive the JSON round trip, so reads decode.
    let rows = c.ok(
        "oxilite/query",
        json!({"query": "SELECT ?s ?o WHERE { ?s <http://ex.org/p> ?o } ORDER BY ?o"}),
    );
    assert_eq!(rows["rows"][1][0]["value"], "http://ex.org/b");
    assert_eq!(rows["rows"][1][1]["value"], "2");
    assert!(rows["d1"]["requests"].as_u64().unwrap() >= 1);
    let list = c.ok("oxilite/connections", Value::Null);
    let d1 = list
        .as_array()
        .unwrap()
        .iter()
        .find(|x| x["kind"] == "d1")
        .unwrap();
    assert!(d1["billing"]["requests"].as_u64().unwrap() > 2);
    drop(c);
    server.unblock();
    let _ = handle.join();
}

// @tg: verifies:: [[tests#Studio server#MCP tools answer agents]] {since: "2026-09-24", level: unit}
#[test]
fn mcp_tools_answer_agents() {
    let dir = test_workspace();
    let mut m = mcp::Mcp::open(Some(dir.clone()), None).unwrap();
    let init = m.handle(&json!({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18"}})).unwrap();
    assert_eq!(init["result"]["capabilities"]["tools"], json!({}));
    assert!(m
        .handle(&json!({"jsonrpc": "2.0", "method": "notifications/initialized"}))
        .is_none());
    let list = m
        .handle(&json!({"jsonrpc": "2.0", "id": 2, "method": "tools/list"}))
        .unwrap();
    let names: Vec<&str> = list["result"]["tools"]
        .as_array()
        .unwrap()
        .iter()
        .map(|t| t["name"].as_str().unwrap())
        .collect();
    assert_eq!(
        names,
        [
            "sparql_query",
            "datalog_query",
            "schema",
            "validate",
            "why",
            "reload"
        ]
    );
    let call = |m: &mut mcp::Mcp, name: &str, args: Value| {
        let r = m.handle(&json!({"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": name, "arguments": args}})).unwrap();
        (
            r["result"]["isError"] == true,
            r["result"]["content"][0]["text"]
                .as_str()
                .unwrap()
                .to_string(),
        )
    };
    let (err, text) = call(
        &mut m,
        "sparql_query",
        json!({"query": "SELECT ?n WHERE { ?p <http://ex.org/name> ?n } ORDER BY ?n"}),
    );
    assert!(!err);
    assert_eq!(text, "?n\n\"Ada\"\n\"Bob\"");
    let (_, schema) = call(&mut m, "schema", json!({}));
    assert!(schema.contains("<http://ex.org/name> 2"), "{schema}");
    assert!(schema.contains("ex: <http://ex.org/>"), "{schema}");
    let (_, v) = call(&mut m, "validate", json!({}));
    assert_eq!(v, "The data conforms to the shapes.");
    let (_, why) = call(
        &mut m,
        "why",
        json!({"subject": "http://ex.org/ada", "predicate": "http://ex.org/ancestor", "object": "http://ex.org/cy"}),
    );
    assert!(why.contains("rules/family.dl"), "{why}");
    let (err, _) = call(&mut m, "sparql_query", json!({"query": "SELEC"}));
    assert!(err);
    let _ = std::fs::remove_dir_all(dir);
}

// @tg: verifies:: [[tests#Studio server#ShEx results are diagnostics]] {since: "2026-09-24", level: unit}
#[test]
fn shex_results_are_diagnostics() {
    let data = "@prefix ex: <http://ex.org/> .\nex:ada ex:name \"Ada\" .\nex:nobody ex:age 3 .\n";
    let schema = "PREFIX ex: <http://ex.org/>\nPREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n\nex:PersonShape { ex:name xsd:string }\n";
    let map = "<http://ex.org/ada>@<http://ex.org/PersonShape>,<http://ex.org/nobody>@<http://ex.org/PersonShape>";
    let mut c = Client::start(workspace(&[
        ("data.ttl", data),
        ("person.shex", schema),
        ("person.sm", map),
    ]));
    let report = c.wait_for("oxilite/validationChanged");
    assert_eq!(report["conforms"], false, "{report}");
    let results = report["results"].as_array().unwrap();
    assert_eq!(results.len(), 1, "{report}");
    assert_eq!(results[0]["focus"], "http://ex.org/nobody");
    assert_eq!(results[0]["component"], "shex");
    assert!(results[0]["location"]["uri"]
        .as_str()
        .unwrap()
        .ends_with("data.ttl"));
    assert_eq!(results[0]["location"]["range"]["start"]["line"], 2);
    assert!(results[0]["shapeLocation"]["uri"]
        .as_str()
        .unwrap()
        .ends_with("person.shex"));
}

// @tg: verifies:: [[tests#Studio server#Full-text search with the manifest index]] {since: "2026-09-24", level: unit}
#[test]
fn full_text_search_with_the_manifest_index() {
    let manifest =
        "text_index = true\n[[graph]]\niri = \"https://ex.org/g\"\nfiles = [\"*.ttl\"]\n";
    let data = "@prefix ex: <http://ex.org/> .\nex:a ex:note \"graph databases on SQLite\" .\nex:b ex:note \"a note about cats\" .\n";
    let mut c = Client::start(workspace(&[
        ("oxilite.toml", manifest),
        ("notes.ttl", data),
    ]));
    let r = c.ok(
        "oxilite/query",
        json!({"query": "PREFIX oxl: <https://oxilite.dev/ns#> SELECT ?s WHERE { ?s ?p ?o FILTER(oxl:textMatch(?o, \"sqlite\")) }"}),
    );
    assert_eq!(r["rows"].as_array().unwrap().len(), 1, "{r}");
    assert_eq!(r["rows"][0][0]["value"], "http://ex.org/a");
    let plan = c.ok("oxilite/explain", json!({"query": "PREFIX oxl: <https://oxilite.dev/ns#> SELECT ?s WHERE { ?s ?p ?o FILTER(oxl:textMatch(?o, \"sqlite\")) }"}));
    assert!(
        plan["text"].as_str().unwrap().contains("terms_fts"),
        "the index is used: {plan}"
    );
}

// @tg: verifies:: [[tests#Studio server#Ontology diagram data]] {since: "2026-09-24", level: unit}
#[test]
fn ontology_diagram_data() {
    let onto = "@prefix ex: <http://ex.org/> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n@prefix owl: <http://www.w3.org/2002/07/owl#> .\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\nex:Person a owl:Class .\nex:Employee rdfs:subClassOf ex:Person .\nex:Company a owl:Class .\nex:worksFor rdfs:domain ex:Employee ; rdfs:range ex:Company .\nex:name rdfs:domain ex:Person ; rdfs:range xsd:string .\n";
    let mut c = Client::start(workspace(&[("onto.ttl", onto), ("staff.ttl", STAFF)]));
    let o = c.ok("oxilite/ontology", Value::Null);
    let classes: Vec<&str> = o["classes"]
        .as_array()
        .unwrap()
        .iter()
        .map(|c| c["iri"].as_str().unwrap())
        .collect();
    assert!(
        classes.contains(&"http://ex.org/Company") && classes.contains(&"http://ex.org/Employee"),
        "{o}"
    );
    assert!(
        !classes
            .iter()
            .any(|c| c.starts_with("http://www.w3.org/2002/07/owl#")),
        "no OWL vocabulary: {o}"
    );
    assert_eq!(
        o["subclass"],
        json!([["http://ex.org/Employee", "http://ex.org/Person"]])
    );
    let works = o["properties"]
        .as_array()
        .unwrap()
        .iter()
        .find(|p| p["iri"] == "http://ex.org/worksFor")
        .unwrap();
    assert_eq!(works["datatype"], false);
    assert_eq!(works["range"], "http://ex.org/Company");
    let name = o["properties"]
        .as_array()
        .unwrap()
        .iter()
        .find(|p| p["iri"] == "http://ex.org/name")
        .unwrap();
    assert_eq!(name["datatype"], true);
}

// @tg: verifies:: [[tests#Studio server#Datalog debugger counts per rule]] {since: "2026-09-24", level: unit}
#[test]
fn datalog_debugger_counts_per_rule() {
    let family =
        "@prefix ex: <http://ex.org/> .\nex:ada ex:parent ex:bob .\nex:bob ex:parent ex:cy .\n";
    let mut c = Client::start(workspace(&[("family.ttl", family)]));
    let program = "@prefix ex: <http://ex.org/> .\nex:ancestor(?x, ?y) :- ex:parent(?x, ?y).\nex:ancestor(?x, ?z) :- ex:parent(?x, ?y), ex:ancestor(?y, ?z).\nex:orphan(?x, ?y) :- ex:adopted(?x, ?y).\n";
    let d = c.ok("oxilite/datalogDebug", json!({"program": program}));
    let rules = d["rules"].as_array().unwrap();
    assert_eq!(rules.len(), 3);
    assert_eq!(rules[0]["bodyMatches"], 2);
    assert_eq!(rules[1]["bodyMatches"], 1, "ada -> bob -> cy");
    assert_eq!(rules[0]["facts"], 3);
    assert_eq!(rules[2]["bodyMatches"], 0, "an empty rule stands out");
    assert_eq!(rules[2]["line"], 3);
    assert!(!d["plan"].as_str().unwrap().is_empty());
}

// @tg: verifies:: [[tests#Studio server#Why picks the rule whose premises hold]] {since: "2026-09-24", level: unit}
#[test]
fn why_picks_the_rule_whose_premises_hold() {
    let data =
        "@prefix ex: <http://ex.org/> .\nex:ada ex:manages ex:bob .\nex:bob ex:manages ex:eve .\n";
    let rules = "@prefix ex: <http://ex.org/> .\nex:reportsTo(?x, ?m) :- ex:manages(?m, ?x).\nex:reportsTo(?x, ?top) :- ex:manages(?m, ?x), ex:reportsTo(?m, ?top).\n";
    let mut c = Client::start(workspace(&[("data.ttl", data), ("rules/org.dl", rules)]));
    // eve reports to ada only through bob: the direct rule must not be chosen.
    let tree = c.ok(
        "oxilite/why",
        json!({"s": iri("http://ex.org/eve"), "p": iri("http://ex.org/reportsTo"), "o": iri("http://ex.org/ada")}),
    );
    let premises = tree["premises"].as_array().unwrap();
    assert_eq!(premises.len(), 2, "{tree}");
    assert!(premises.iter().all(|p| p["status"] != "absent"), "{tree}");
    assert_eq!(premises[0]["status"], "asserted");
    assert_eq!(premises[1]["status"], "inferred");
    assert_eq!(premises[1]["premises"][0]["status"], "asserted");
}

// @tg: verifies:: [[tests#Studio server#Attaching without activating]] {since: "2026-09-25", level: unit}
#[test]
fn attaching_without_activating() {
    let mut c = Client::start(workspace(&[("people.ttl", PEOPLE)]));
    let db = c.dir.join("side.sqlite").display().to_string();
    let list = c.ok(
        "oxilite/attach",
        json!({"path": db, "readOnly": false, "activate": false}),
    );
    assert_eq!(list[0]["active"], true, "the Project store stays active");
    assert_eq!(list[1]["active"], false);
    // A request naming the attached connection still reaches it.
    let id = format!("attached:{db}");
    let r = c.ok(
        "oxilite/query",
        json!({"query": "SELECT * { ?s ?p ?o }", "connection": id}),
    );
    assert_eq!(r["rows"].as_array().unwrap().len(), 0);
    let r = c.ok("oxilite/query", json!({"query": "SELECT * { ?s ?p ?o }"}));
    assert_eq!(
        r["rows"].as_array().unwrap().len(),
        2,
        "unnamed requests use the active one"
    );
}

// @tg: verifies:: [[tests#Studio server#Registry lists the manifest mapping]] {since: "2026-09-27", level: unit}
#[test]
fn registry_lists_the_manifest_mapping() {
    let manifest = "reasoning = \"rdfs\"\n[[graph]]\niri = \"https://ex.org/g/staff\"\nfiles = [\"staff.ttl\"]\n[[graph]]\niri = \"https://ex.org/g/onto\"\nfiles = [\"onto.ttl\"]\nrole = \"ontology\"\napplies_to = [\"https://ex.org/g/staff\"]\n";
    let mut c = Client::start(workspace(&[
        ("oxilite.toml", manifest),
        ("onto.ttl", ONTOLOGY),
        ("staff.ttl", STAFF),
    ]));
    let r = c.ok("oxilite/registry", json!({}));
    let entries = r["entries"].as_array().unwrap();
    assert_eq!(entries.len(), 1, "{r}");
    assert_eq!(entries[0]["graph"], "https://ex.org/g/onto");
    assert_eq!(entries[0]["role"], "ontology");
    assert_eq!(entries[0]["active"], true);
    assert_eq!(entries[0]["appliesTo"], json!(["https://ex.org/g/staff"]));
    let staff = r["graphs"]
        .as_array()
        .unwrap()
        .iter()
        .find(|g| g["graph"] == "https://ex.org/g/staff")
        .unwrap();
    assert_eq!(staff["triples"], 3);
    assert_eq!(r["ephemeral"], true);
    assert_eq!(r["problems"], json!([]));
    // The explorer shows the role and targets next to the size.
    let graphs = c.ok("oxilite/explorer", json!({"node": "graphs"}));
    let onto = graphs
        .as_array()
        .unwrap()
        .iter()
        .find(|n| n["iri"] == "https://ex.org/g/onto")
        .unwrap();
    assert_eq!(onto["role"], "ontology");
    assert!(
        onto["description"]
            .as_str()
            .unwrap()
            .ends_with("ontology → https://ex.org/g/staff"),
        "{onto}"
    );
}

// @tg: verifies:: [[tests#Studio server#Registry edits follow the update rules]] {since: "2026-09-27", level: unit}
#[test]
fn registry_edits_follow_the_update_rules() {
    let mut c = Client::start(workspace(&[("people.ttl", PEOPLE)]));
    let db = c.dir.join("reg.sqlite").display().to_string();
    c.ok("oxilite/attach", json!({"path": db}));
    let onto = "https://ex.org/onto/zoo";
    let load = json!({"query": format!("INSERT DATA {{ GRAPH <{onto}> {{ <https://ex.org/zoo#> <http://www.w3.org/2002/07/owl#imports> <https://ex.org/core#> }} GRAPH <https://ex.org/data/zoo> {{ <http://ex.org/rex> a <http://ex.org/Dog> }} }}"), "confirmed": true});
    c.ok("oxilite/query", load);
    let register = json!({"op": "register", "graph": onto, "role": "ontology", "appliesTo": ["https://ex.org/data/zoo"]});
    let r = c.call("oxilite/registryEdit", register.clone());
    assert_eq!(r.error.unwrap().code, NEEDS_CONFIRMATION);
    let mut confirmed = register;
    confirmed["confirmed"] = json!(true);
    let r = c.ok("oxilite/registryEdit", confirmed);
    assert_eq!(r["changed"], true);
    assert_eq!(r["ephemeral"], false);
    // A second role keeps the first and shares the targets.
    c.ok(
        "oxilite/registryEdit",
        json!({"op": "addRole", "graph": onto, "role": "shacl", "confirmed": true}),
    );
    let reg = c.ok("oxilite/registry", json!({}));
    let roles: Vec<&str> = reg["entries"]
        .as_array()
        .unwrap()
        .iter()
        .map(|e| e["role"].as_str().unwrap())
        .collect();
    assert_eq!(roles, ["ontology", "shacl"], "{reg}");
    assert!(reg["entries"]
        .as_array()
        .unwrap()
        .iter()
        .all(|e| e["appliesTo"] == json!(["https://ex.org/data/zoo"])));
    assert_eq!(
        reg["ownImports"],
        json!([{"graph": onto, "imports": ["https://ex.org/core#"]}])
    );
    // Remap to every graph, deactivate, and install the system graphs.
    for edit in [
        json!({"op": "map", "graph": onto, "appliesTo": ["https://oxilite.dev/ns#AllGraphs"], "confirmed": true}),
        json!({"op": "deactivate", "graph": onto, "confirmed": true}),
        json!({"op": "installSystemGraphs", "confirmed": true}),
    ] {
        assert_eq!(c.ok("oxilite/registryEdit", edit)["changed"], true);
    }
    let reg = c.ok("oxilite/registry", json!({}));
    let e = &reg["entries"][0];
    assert_eq!(e["appliesTo"], json!([]), "{reg}");
    assert_eq!(e["active"], false);
    assert_eq!(reg["systemGraphs"]["current"], true);
    assert_eq!(
        reg["systemGraphs"]["present"],
        json!(["oxilite:schema", "oxilite:vocabulary"])
    );
    // Dropping removes the registration and the graph's triples.
    let r = c.ok(
        "oxilite/registryEdit",
        json!({"op": "drop", "graph": onto, "confirmed": true}),
    );
    assert_eq!(r["dropped"], 1);
    let reg = c.ok("oxilite/registry", json!({}));
    assert_eq!(reg["entries"], json!([]));
    assert!(!reg["graphs"]
        .as_array()
        .unwrap()
        .iter()
        .any(|g| g["graph"] == onto));
    // A read-only connection refuses edits.
    c.ok("oxilite/attach", json!({"path": db, "readOnly": true}));
    let r = c.call(
        "oxilite/registryEdit",
        json!({"op": "unregister", "graph": onto, "confirmed": true}),
    );
    assert!(r.error.unwrap().message.contains("read-only"));
}

// @tg: verifies:: [[tests#Studio server#Vector indexes on a Turso connection]] {since: "2026-09-28", level: unit}
#[test]
fn vector_indexes_on_a_turso_connection() {
    let mut c = Client::start(workspace(&[("people.ttl", PEOPLE)]));
    // The project store is bundled SQLite: no vector indexes there.
    let r = c.ok("oxilite/vectorIndexes", json!({"connection": "project"}));
    assert_eq!(r["supported"], false);
    assert_eq!(r["indexes"], json!([]));
    let db = c.dir.join("kg.db").display().to_string();
    let list = c.ok("oxilite/attach", json!({"path": db, "engine": "turso"}));
    assert_eq!(list[1]["kind"], "turso");
    c.ok(
        "oxilite/query",
        json!({
            "query": "INSERT DATA { <http://ex.org/a> <http://ex.org/e> \"[1, 0]\" . <http://ex.org/b> <http://ex.org/e> \"[0, 1]\" }",
            "confirmed": true
        }),
    );
    let created = c.ok(
        "oxilite/vectorIndexCreate",
        json!({"name": "docs", "property": "http://ex.org/e", "dimensions": 2, "metric": "cosine"}),
    );
    assert_eq!(created["rows"], 2);
    assert_eq!(created["built"], true);
    let r = c.ok("oxilite/vectorIndexes", json!({}));
    assert_eq!(r["supported"], true);
    assert_eq!(r["indexes"][0]["name"], "docs");
    assert_eq!(r["indexes"][0]["iri"], "oxilite:vector/docs");
    let hits = c.ok(
        "oxilite/vectorSearch",
        json!({"index": "docs", "vector": [0.9, 0.1], "k": 1}),
    );
    assert_eq!(hits["hits"][0]["node"]["value"], "http://ex.org/a");
    assert!(hits["hits"][0]["score"].as_f64().unwrap() > 0.9);
    let hits = c.ok(
        "oxilite/vectorSearch",
        json!({"index": "docs", "node": "http://ex.org/b", "k": 2}),
    );
    assert_eq!(hits["hits"][0]["node"]["value"], "http://ex.org/b");
    assert_eq!(
        c.ok("oxilite/functions", json!({})),
        json!({"functions": []})
    );
    assert_eq!(
        c.ok("oxilite/vectorIndexDrop", json!({"name": "docs"})),
        json!({"dropped": true})
    );
    let r = c.ok("oxilite/vectorIndexes", json!({}));
    assert_eq!(r["indexes"], json!([]));
}