hyperdb-mcp 0.6.0

MCP server for Hyper database — instant SQL analytics for LLM workflows
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
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Tests for inline data ingest (JSON and CSV): basic round-trips, append
//! mode, schema overrides, and error handling for empty input.

mod common;
use common::TestEngine;
use hyperdb_mcp::ingest::{
    detect_file_format, extract_json_path, ingest_csv, ingest_csv_file, ingest_json,
    ingest_json_file, InferredFileFormat, IngestOptions,
};
use std::io::Write;
use tempfile::TempPath;

/// Ingest a JSON array of two objects, then query back to verify rows were
/// inserted with correct column values and ordering.
#[test]
fn ingest_json_basic() {
    let te = TestEngine::new_ephemeral();
    let data = r#"[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]"#;
    let opts = IngestOptions {
        table: "users".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let result = ingest_json(&te.engine, data, &opts).unwrap();
    assert_eq!(result.rows, 2);

    let rows = te
        .engine
        .execute_query_to_json("SELECT * FROM users ORDER BY id")
        .unwrap();
    assert_eq!(rows.len(), 2);
    assert_eq!(rows[0]["name"], "Alice");
}

/// Verify that append mode adds rows to an existing table without dropping it.
/// First ingest creates the table with 1 row, second ingest appends 1 more.
#[test]
fn ingest_json_append_mode() {
    let te = TestEngine::new_ephemeral();
    let data1 = r#"[{"id": 1}]"#;
    let data2 = r#"[{"id": 2}]"#;
    let opts_replace = IngestOptions {
        table: "t".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let opts_append = IngestOptions {
        table: "t".into(),
        mode: "append".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    ingest_json(&te.engine, data1, &opts_replace).unwrap();
    ingest_json(&te.engine, data2, &opts_append).unwrap();

    let count: i64 = te
        .engine
        .connection()
        .execute_scalar_query("SELECT COUNT(*) FROM t")
        .unwrap()
        .unwrap();
    assert_eq!(count, 2);
}

/// Ingest inline CSV text with a header row and verify the COPY FROM path
/// correctly loads both data rows.
#[test]
fn ingest_csv_basic() {
    let te = TestEngine::new_ephemeral();
    let csv_text = "id,name,score\n1,Alice,95.5\n2,Bob,88.0\n";
    let opts = IngestOptions {
        table: "scores".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let result = ingest_csv(&te.engine, csv_text, &opts).unwrap();
    assert_eq!(result.rows, 2);

    let rows = te
        .engine
        .execute_query_to_json("SELECT * FROM scores ORDER BY id")
        .unwrap();
    assert_eq!(rows.len(), 2);
}

/// Verify that a schema override forces the column type, bypassing inference.
/// Here a string value "123.45" is inserted into a DOUBLE PRECISION column
/// declared by the override rather than being inferred as TEXT.
#[test]
fn ingest_json_with_schema_override() {
    let te = TestEngine::new_ephemeral();
    let data = r#"[{"amount": "123.45"}]"#;
    let mut schema = serde_json::Map::new();
    schema.insert(
        "amount".into(),
        serde_json::Value::String("DOUBLE PRECISION".into()),
    );
    let opts = IngestOptions {
        table: "orders".into(),
        mode: "replace".into(),
        schema_override: Some(schema),
        merge_key: None,
        target_db: None,
    };
    let result = ingest_json(&te.engine, data, &opts).unwrap();
    assert_eq!(result.rows, 1);
}

/// Verify that ingesting an empty JSON array returns an error rather than
/// silently creating a table with no columns.
#[test]
fn ingest_json_empty_returns_error() {
    let te = TestEngine::new_ephemeral();
    let data = "[]";
    let opts = IngestOptions {
        table: "empty".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let result = ingest_json(&te.engine, data, &opts);
    assert!(result.is_err());
}

/// Helper: write `content` to a temp file with the given extension,
/// keep the file alive for the caller's lifetime, and return its path
/// as a String.
///
/// Returns a [`TempPath`] (not a [`NamedTempFile`]) so the file handle
/// is closed after writing. On Windows, hyperd's `COPY FROM` runs in a
/// separate process and can't open a file that the test still holds
/// open for writing; the Unix sharing rules are more permissive so
/// keeping the handle would only break the file-based CSV test, but
/// we standardize on the closed-handle form to keep all tests
/// cross-platform.
fn tmp_with_ext(ext: &str, content: &[u8]) -> (String, TempPath) {
    let mut file = tempfile::Builder::new()
        .suffix(&format!(".{ext}"))
        .tempfile()
        .expect("create temp file");
    file.write_all(content).expect("write temp");
    file.flush().ok();
    let temp_path = file.into_temp_path();
    let path = temp_path.to_str().expect("utf-8 path").to_string();
    (path, temp_path)
}

/// `ingest_json_file` loads a `.json` file containing a top-level array
/// of objects — the format produced by typical REST API snapshots.
#[test]
fn ingest_json_file_loads_json_array() {
    let te = TestEngine::new_ephemeral();
    let (path, _keep) = tmp_with_ext(
        "json",
        b"[{\"id\":1,\"name\":\"Alice\"},{\"id\":2,\"name\":\"Bob\"},{\"id\":3,\"name\":\"Carol\"}]",
    );
    let opts = IngestOptions {
        table: "people".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let result = ingest_json_file(&te.engine, &path, &opts).unwrap();
    assert_eq!(result.rows, 3);
    assert_eq!(result.stats.file_format.as_deref(), Some("json"));
    assert_eq!(result.stats.operation, "load_file");

    let rows = te
        .engine
        .execute_query_to_json("SELECT id, name FROM people ORDER BY id")
        .unwrap();
    assert_eq!(rows.len(), 3);
    assert_eq!(rows[1]["name"], "Bob");
}

/// `ingest_json_file` auto-detects newline-delimited JSON (JSONL /
/// NDJSON) — the format hyperd itself uses for its logs. Blank lines
/// are tolerated so real-world log files load without preprocessing.
#[test]
fn ingest_json_file_loads_jsonl() {
    let te = TestEngine::new_ephemeral();
    let jsonl = b"{\"k\":\"start\",\"n\":1}\n\
                  \n\
                  {\"k\":\"progress\",\"n\":2}\n\
                  {\"k\":\"done\",\"n\":3}\n";
    let (path, _keep) = tmp_with_ext("jsonl", jsonl);
    let opts = IngestOptions {
        table: "events".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let result = ingest_json_file(&te.engine, &path, &opts).unwrap();
    assert_eq!(result.rows, 3, "blank lines are skipped, data rows count 3");
    assert_eq!(result.stats.file_format.as_deref(), Some("jsonl"));

    let rows = te
        .engine
        .execute_query_to_json("SELECT k, n FROM events ORDER BY n")
        .unwrap();
    assert_eq!(rows.len(), 3);
    assert_eq!(rows[0]["k"], "start");
    assert_eq!(rows[2]["k"], "done");
}

/// A malformed JSONL line surfaces a `SchemaMismatch` error that names
/// the offending line number, not a cryptic byte offset.
#[test]
fn ingest_json_file_reports_bad_jsonl_line() {
    let te = TestEngine::new_ephemeral();
    let bad = b"{\"id\":1}\n\
                {\"id\":2}\n\
                not-json\n";
    let (path, _keep) = tmp_with_ext("jsonl", bad);
    let opts = IngestOptions {
        table: "t".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let Err(err) = ingest_json_file(&te.engine, &path, &opts) else {
        panic!("expected malformed JSONL to error")
    };
    assert_eq!(err.code, hyperdb_mcp::error::ErrorCode::SchemaMismatch);
    assert!(
        err.message.contains("line 3"),
        "error should name the bad line: {}",
        err.message
    );
}

/// CSV ingest must load unquoted empty cells as SQL NULL, matching
/// `PostgreSQL`'s CSV default (and `inspect_file`'s `null_count`
/// accounting). Regression guard for the ",," = NULL contract that
/// users rely on when filtering with `WHERE col IS NULL`.
#[test]
fn ingest_csv_empty_cells_become_null() {
    let te = TestEngine::new_ephemeral();
    // Rows 1/3 have age set, row 2 leaves age empty. The empty cell
    // should land as SQL NULL, not an empty-string zero or a parse
    // error on the numeric column.
    let csv_text = "id,name,age\n1,Alice,30\n2,Bob,\n3,Carol,42\n";
    let opts = IngestOptions {
        table: "u".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let result = ingest_csv(&te.engine, csv_text, &opts).unwrap();
    assert_eq!(result.rows, 3);

    let nulls: i64 = te
        .engine
        .connection()
        .execute_scalar_query("SELECT COUNT(*) FROM u WHERE age IS NULL")
        .unwrap()
        .unwrap();
    assert_eq!(nulls, 1, "exactly one row should have a NULL age");

    let non_nulls: i64 = te
        .engine
        .connection()
        .execute_scalar_query("SELECT COUNT(*) FROM u WHERE age IS NOT NULL")
        .unwrap()
        .unwrap();
    assert_eq!(non_nulls, 2);
}

// --- Format detection ------------------------------------------------------

/// Known binary extensions win immediately without reading the file, so
/// the classifier is effectively free for Parquet / Arrow IPC paths.
#[test]
fn detect_file_format_matches_binary_extensions() {
    assert_eq!(
        detect_file_format(std::path::Path::new("/tmp/x.parquet")),
        InferredFileFormat::Parquet
    );
    assert_eq!(
        detect_file_format(std::path::Path::new("/tmp/x.pq")),
        InferredFileFormat::Parquet
    );
    assert_eq!(
        detect_file_format(std::path::Path::new("/tmp/x.arrow")),
        InferredFileFormat::ArrowIpc
    );
    assert_eq!(
        detect_file_format(std::path::Path::new("/tmp/x.ipc")),
        InferredFileFormat::ArrowIpc
    );
    assert_eq!(
        detect_file_format(std::path::Path::new("/tmp/x.feather")),
        InferredFileFormat::ArrowIpc
    );
}

/// JSON extensions (including `.ndjson`) map to Json without reading
/// the file. Covers the common case where a producer names the file
/// correctly.
#[test]
fn detect_file_format_matches_json_extensions() {
    for ext in ["json", "jsonl", "ndjson"] {
        let (path, _keep) = tmp_with_ext(ext, b"[{\"a\":1}]");
        assert_eq!(
            detect_file_format(std::path::Path::new(&path)),
            InferredFileFormat::Json,
            "`.{ext}` should map to Json without content sniff"
        );
    }
}

/// Content sniff fallback: a `.log` file whose first non-whitespace
/// byte is `{` dispatches to Json. This is the hyperd-log case — no
/// rename required.
#[test]
fn detect_file_format_sniffs_jsonl_from_unknown_extension() {
    let (path, _keep) = tmp_with_ext(
        "log",
        b"{\"ts\":\"2026-01-01T00:00:00\",\"k\":\"start\"}\n\
          {\"ts\":\"2026-01-01T00:00:01\",\"k\":\"done\"}\n",
    );
    assert_eq!(
        detect_file_format(std::path::Path::new(&path)),
        InferredFileFormat::Json
    );
}

/// A `.log` file whose first byte is `[` also sniffs as Json (JSON
/// array shape). Exercises the other JSON branch of the sniffer.
#[test]
fn detect_file_format_sniffs_json_array_from_unknown_extension() {
    let (path, _keep) = tmp_with_ext("log", b"  \n  [{\"k\":\"v\"}]\n");
    assert_eq!(
        detect_file_format(std::path::Path::new(&path)),
        InferredFileFormat::Json,
        "leading whitespace before `[` should still sniff as JSON"
    );
}

/// A file whose first non-whitespace byte is neither `[` nor `{` falls
/// back to CSV. Covers the common `.csv` / `.tsv` / unlabeled tabular
/// cases.
#[test]
fn detect_file_format_falls_back_to_csv() {
    let (path, _keep) = tmp_with_ext("txt", b"id,name,age\n1,Alice,30\n");
    assert_eq!(
        detect_file_format(std::path::Path::new(&path)),
        InferredFileFormat::Csv
    );
}

/// A nonexistent path is treated as CSV (the subsequent CSV ingest
/// will produce a cleaner "file not found" error than a classifier
/// failure would).
#[test]
fn detect_file_format_defaults_to_csv_when_unreadable() {
    assert_eq!(
        detect_file_format(std::path::Path::new("/tmp/definitely-not-a-real-file.xyz")),
        InferredFileFormat::Csv
    );
}

/// End-to-end: a JSONL file with a `.log` extension (hyperd's native
/// shape) loads cleanly through `ingest_json_file` via content sniff.
/// Regression guard for the "rename the log to `.jsonl` first" UX wart
/// this whole fix set was designed to remove.
#[test]
fn ingest_json_file_handles_log_extension_via_content_sniff() {
    let te = TestEngine::new_ephemeral();
    // Construct the payload through `detect_file_format` so the test
    // mirrors the production dispatch path rather than hard-coding a
    // specific ingest function.
    let (path, _keep) = tmp_with_ext(
        "log",
        b"{\"id\":1,\"msg\":\"hello\"}\n\
          {\"id\":2,\"msg\":\"world\"}\n",
    );
    let fmt = detect_file_format(std::path::Path::new(&path));
    assert_eq!(fmt, InferredFileFormat::Json);

    let opts = IngestOptions {
        table: "events".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let result = ingest_json_file(&te.engine, &path, &opts).unwrap();
    assert_eq!(result.rows, 2);
    assert_eq!(result.stats.file_format.as_deref(), Some("jsonl"));
}

/// Same contract for file-based CSV ingest: empty text cells become
/// NULL, so a downstream `WHERE col <> ''` does not need to carry a
/// defensive `OR col IS NULL` clause.
#[test]
fn ingest_csv_file_empty_cells_become_null() {
    let te = TestEngine::new_ephemeral();
    let (path, _keep) = tmp_with_ext("csv", b"code,label\nAAA,first\n,middle\nBBB,\n");
    let opts = IngestOptions {
        table: "lookup".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let result = ingest_csv_file(&te.engine, &path, &opts).unwrap();
    assert_eq!(result.rows, 3);

    let code_nulls: i64 = te
        .engine
        .connection()
        .execute_scalar_query("SELECT COUNT(*) FROM lookup WHERE code IS NULL")
        .unwrap()
        .unwrap();
    let label_nulls: i64 = te
        .engine
        .connection()
        .execute_scalar_query("SELECT COUNT(*) FROM lookup WHERE label IS NULL")
        .unwrap()
        .unwrap();
    assert_eq!(code_nulls, 1, "row 2's empty code should be NULL");
    assert_eq!(label_nulls, 1, "row 3's empty label should be NULL");
}

// ---------- extract_json_path tests ----------

/// Navigate a single key to reach an array.
#[test]
fn extract_json_path_simple_object() {
    let json = r#"{"results": [{"a": 1}, {"a": 2}]}"#;
    let out = extract_json_path(json, "results").unwrap();
    let arr: Vec<serde_json::Value> = serde_json::from_str(&out).unwrap();
    assert_eq!(arr.len(), 2);
}

/// Navigate into an array with a numeric index.
#[test]
fn extract_json_path_array_index() {
    let json = r#"{"content": [{"type": "text"}, {"type": "image"}]}"#;
    let out = extract_json_path(json, "content.0").unwrap();
    let obj: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(obj["type"], "text");
}

/// The key scenario: Splunk MCP wrapper with stringified JSON in the
/// `text` field. `content.0.text` is a string that must be parsed as
/// JSON before navigating further into `query_result.results`.
#[test]
fn extract_json_path_stringified_json() {
    let inner = serde_json::json!({
        "status": "success",
        "query_result": {
            "results": [
                {"_time": "2026-01-01T00:00:00Z", "_raw": "log line 1"},
                {"_time": "2026-01-01T00:01:00Z", "_raw": "log line 2"},
            ]
        }
    });
    let wrapper = serde_json::json!({
        "content": [{
            "type": "text",
            "text": inner.to_string()
        }]
    });

    let out =
        extract_json_path(&wrapper.to_string(), "content.0.text.query_result.results").unwrap();
    let arr: Vec<serde_json::Value> = serde_json::from_str(&out).unwrap();
    assert_eq!(arr.len(), 2);
    assert_eq!(arr[0]["_raw"], "log line 1");
}

/// Terminal string auto-parse: the value at the path is itself a
/// stringified JSON array.
#[test]
fn extract_json_path_terminal_string_parsed() {
    let json = r#"{"data": "[{\"a\":1},{\"a\":2}]"}"#;
    let out = extract_json_path(json, "data").unwrap();
    let arr: Vec<serde_json::Value> = serde_json::from_str(&out).unwrap();
    assert_eq!(arr.len(), 2);
    assert_eq!(arr[0]["a"], 1);
}

/// Missing key returns a descriptive error.
#[test]
fn extract_json_path_missing_key() {
    let json = r#"{"other": 1}"#;
    let err = extract_json_path(json, "missing").unwrap_err();
    assert!(
        err.message.contains("key not found"),
        "error should mention missing key: {}",
        err.message
    );
}

/// Array index out of bounds returns a descriptive error.
#[test]
fn extract_json_path_index_out_of_bounds() {
    let json = r#"{"arr": [1, 2]}"#;
    let err = extract_json_path(json, "arr.5").unwrap_err();
    assert!(
        err.message.contains("out of bounds"),
        "error should mention out of bounds: {}",
        err.message
    );
}

/// Navigating into a plain string (not valid JSON) returns an error.
#[test]
fn extract_json_path_string_not_json() {
    let json = r#"{"name": "plain text"}"#;
    let err = extract_json_path(json, "name.sub").unwrap_err();
    assert!(
        err.message.contains("not valid JSON"),
        "error should mention invalid JSON: {}",
        err.message
    );
}

/// Navigate into an object nested inside a string, then into an array.
#[test]
fn extract_json_path_multi_level() {
    let inner_inner = serde_json::json!({"rows": [{"x": 10}]});
    let inner = serde_json::json!({"payload": inner_inner.to_string()});
    let outer = serde_json::json!({"wrapper": inner.to_string()});

    let out = extract_json_path(&outer.to_string(), "wrapper.payload.rows").unwrap();
    let arr: Vec<serde_json::Value> = serde_json::from_str(&out).unwrap();
    assert_eq!(arr.len(), 1);
    assert_eq!(arr[0]["x"], 10);
}

/// End-to-end: extract from Splunk-shaped wrapper and ingest into Hyper.
#[test]
fn extract_json_path_then_ingest() {
    let te = TestEngine::new_ephemeral();
    let inner = serde_json::json!({
        "status": "success",
        "query_result": {
            "results": [
                {"id": 1, "msg": "hello"},
                {"id": 2, "msg": "world"},
            ]
        }
    });
    let wrapper = serde_json::json!({
        "content": [{"type": "text", "text": inner.to_string()}]
    });

    let extracted =
        extract_json_path(&wrapper.to_string(), "content.0.text.query_result.results").unwrap();

    let opts = IngestOptions {
        table: "splunk_data".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let result = ingest_json(&te.engine, &extracted, &opts).unwrap();
    assert_eq!(result.rows, 2);

    let rows = te
        .engine
        .execute_query_to_json("SELECT * FROM splunk_data ORDER BY id")
        .unwrap();
    assert_eq!(rows.len(), 2);
    assert_eq!(rows[0]["msg"], "hello");
    assert_eq!(rows[1]["msg"], "world");
}

// ──────────────────────────────────────────────────────────────────
// merge mode
// ──────────────────────────────────────────────────────────────────

/// Initial 3 rows, then merge 2 overlapping (update) + 1 new (insert).
/// Final shape: 4 rows; updated rows show the new values.
#[test]
fn ingest_json_merge_basic() {
    let te = TestEngine::new_ephemeral();
    let initial = r#"[
        {"id": 1, "name": "Alice"},
        {"id": 2, "name": "Bob"},
        {"id": 3, "name": "Carol"}
    ]"#;
    let updates = r#"[
        {"id": 2, "name": "Bob Jr."},
        {"id": 3, "name": "Carol Updated"},
        {"id": 4, "name": "Dave"}
    ]"#;

    let opts_replace = IngestOptions {
        table: "users".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    ingest_json(&te.engine, initial, &opts_replace).unwrap();

    let opts_merge = IngestOptions {
        table: "users".into(),
        mode: "merge".into(),
        schema_override: None,
        merge_key: Some(vec!["id".into()]),
        target_db: None,
    };
    let merge_result = ingest_json(&te.engine, updates, &opts_merge).unwrap();
    // No new columns in this merge → schema_changed must remain false so
    // the server handler skips the resource-list-changed broadcast.
    assert!(
        !merge_result.stats.schema_changed,
        "schema_changed must be false for a row-only merge"
    );

    let rows = te
        .engine
        .execute_query_to_json("SELECT * FROM users ORDER BY id")
        .unwrap();
    assert_eq!(
        rows.len(),
        4,
        "expected 3 originals minus 2 replaced + 3 merged = 4"
    );
    assert_eq!(rows[0]["name"], "Alice"); // unchanged
    assert_eq!(rows[1]["name"], "Bob Jr."); // updated
    assert_eq!(rows[2]["name"], "Carol Updated"); // updated
    assert_eq!(rows[3]["name"], "Dave"); // inserted
}

/// Initial table has columns (id, name); merge file adds `host`.
/// Verify ALTER TABLE auto-fires, post-merge schema includes `host`,
/// and old rows have `host = NULL`.
#[test]
fn ingest_json_merge_adds_new_column() {
    let te = TestEngine::new_ephemeral();
    let initial = r#"[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]"#;
    let with_host = r#"[
        {"id": 2, "name": "Bob", "host": "host-2"},
        {"id": 3, "name": "Carol", "host": "host-3"}
    ]"#;

    let opts_replace = IngestOptions {
        table: "t".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    ingest_json(&te.engine, initial, &opts_replace).unwrap();

    let opts_merge = IngestOptions {
        table: "t".into(),
        mode: "merge".into(),
        schema_override: None,
        merge_key: Some(vec!["id".into()]),
        target_db: None,
    };
    let merge_result = ingest_json(&te.engine, with_host, &opts_merge).unwrap();
    // ALTER TABLE fired → schema_changed must be true so the server
    // handler issues a resource-list-changed broadcast and clients
    // re-fetch their schema cache.
    assert!(
        merge_result.stats.schema_changed,
        "schema_changed must be true when merge ALTERs the target"
    );

    // Schema now includes host.
    let cols = te.engine.column_metadata("t").unwrap();
    assert!(
        cols.iter().any(|c| c.name == "host"),
        "expected `host` column added by ALTER TABLE; got {:?}",
        cols.iter().map(|c| &c.name).collect::<Vec<_>>()
    );

    // id=1 (untouched) should have host=NULL; id=2,3 should have values.
    let rows = te
        .engine
        .execute_query_to_json("SELECT * FROM t ORDER BY id")
        .unwrap();
    assert_eq!(rows.len(), 3);
    assert!(rows[0]["host"].is_null(), "old row's host must be NULL");
    assert_eq!(rows[1]["host"], "host-2");
    assert_eq!(rows[2]["host"], "host-3");
}

/// Merging into a non-existent table degenerates to a rename: the temp
/// table becomes the target and rows are loaded as-if by replace.
#[test]
fn ingest_json_merge_target_does_not_exist() {
    let te = TestEngine::new_ephemeral();
    let data = r#"[{"id": 1, "name": "Alice"}]"#;
    let opts_merge = IngestOptions {
        table: "fresh".into(),
        mode: "merge".into(),
        schema_override: None,
        merge_key: Some(vec!["id".into()]),
        target_db: None,
    };
    let result = ingest_json(&te.engine, data, &opts_merge).unwrap();
    assert_eq!(result.rows, 1);
    // Target was just created from scratch via the rename short-circuit;
    // by definition this is a "shape changed" event, so notify clients.
    assert!(
        result.stats.schema_changed,
        "schema_changed must be true when merge creates a fresh target"
    );

    let rows = te
        .engine
        .execute_query_to_json("SELECT * FROM fresh")
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0]["name"], "Alice");
}

/// `mode=merge` with no `merge_key` returns `InvalidArgument` from the
/// ingest layer (the server-handler validation is exercised separately
/// at the tool boundary).
#[test]
fn ingest_json_merge_missing_key_param() {
    let te = TestEngine::new_ephemeral();
    let data = r#"[{"id": 1}]"#;
    let opts = IngestOptions {
        table: "t".into(),
        mode: "merge".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    let err = ingest_json(&te.engine, data, &opts).unwrap_err();
    assert!(
        err.message.to_lowercase().contains("merge_key"),
        "error must mention merge_key; got: {}",
        err.message
    );
}

/// Merge with a key column that the *target* doesn't have. Caused by
/// a typo in the merge_key parameter or a target schema that diverges
/// from the file. Should raise a clear error and leave the target
/// untouched.
#[test]
fn ingest_json_merge_key_not_in_target() {
    let te = TestEngine::new_ephemeral();
    // Target has only `id`. Merge attempts to key on `not_a_col`.
    let opts_replace = IngestOptions {
        table: "t".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    ingest_json(&te.engine, r#"[{"id": 1}]"#, &opts_replace).unwrap();

    let opts_merge = IngestOptions {
        table: "t".into(),
        mode: "merge".into(),
        schema_override: None,
        merge_key: Some(vec!["not_a_col".into()]),
        target_db: None,
    };
    let err = ingest_json(&te.engine, r#"[{"id": 2, "not_a_col": "x"}]"#, &opts_merge).unwrap_err();
    assert!(
        err.message.contains("not_a_col"),
        "error must name the missing column; got: {}",
        err.message
    );
    assert!(err.message.to_lowercase().contains("not in target"));

    // Target unchanged.
    let count: i64 = te
        .engine
        .connection()
        .execute_scalar_query("SELECT COUNT(*) FROM t")
        .unwrap()
        .unwrap();
    assert_eq!(count, 1);
}

/// Merge where the target's `id` is BIGINT (forced) but the incoming
/// JSON file's id parses as TEXT — a real source of confusion in
/// practice. Reject with a clear error rather than silently coercing.
#[test]
fn ingest_json_merge_key_type_mismatch() {
    let te = TestEngine::new_ephemeral();
    // Force `id` to BIGINT explicitly.
    let mut so = serde_json::Map::new();
    so.insert("id".into(), serde_json::json!("BIGINT"));
    let opts_replace = IngestOptions {
        table: "t".into(),
        mode: "replace".into(),
        schema_override: Some(so),
        merge_key: None,
        target_db: None,
    };
    ingest_json(&te.engine, r#"[{"id": 1, "name": "a"}]"#, &opts_replace).unwrap();

    // Incoming has id as quoted string, no override → inferred TEXT.
    let opts_merge = IngestOptions {
        table: "t".into(),
        mode: "merge".into(),
        schema_override: None,
        merge_key: Some(vec!["id".into()]),
        target_db: None,
    };
    let err = ingest_json(&te.engine, r#"[{"id": "1", "name": "a"}]"#, &opts_merge).unwrap_err();
    assert!(
        err.message.to_lowercase().contains("type mismatch"),
        "error must mention type mismatch; got: {}",
        err.message
    );
}

/// Same shape as the key-type-mismatch test but for a non-key column.
/// We still reject because silently coercing risks data loss.
#[test]
fn ingest_json_merge_existing_column_type_mismatch() {
    let te = TestEngine::new_ephemeral();
    // Target has score as DOUBLE PRECISION.
    let mut so = serde_json::Map::new();
    so.insert("score".into(), serde_json::json!("DOUBLE PRECISION"));
    let opts_replace = IngestOptions {
        table: "t".into(),
        mode: "replace".into(),
        schema_override: Some(so),
        merge_key: None,
        target_db: None,
    };
    ingest_json(&te.engine, r#"[{"id": 1, "score": 99.5}]"#, &opts_replace).unwrap();

    // Incoming has score as quoted text.
    let opts_merge = IngestOptions {
        table: "t".into(),
        mode: "merge".into(),
        schema_override: None,
        merge_key: Some(vec!["id".into()]),
        target_db: None,
    };
    let err = ingest_json(
        &te.engine,
        r#"[{"id": 1, "score": "not a number"}]"#,
        &opts_merge,
    )
    .unwrap_err();
    assert!(
        err.message.to_lowercase().contains("type mismatch"),
        "error must mention type mismatch; got: {}",
        err.message
    );
}

/// On any merge failure, the temp `__hyperdb_merge_*` table must be
/// dropped — leaving stragglers in the workspace would degrade the
/// describe/list experience over time.
#[test]
fn ingest_json_merge_no_orphan_tmp_on_failure() {
    let te = TestEngine::new_ephemeral();
    // Force a key-not-in-target failure (cheap, deterministic).
    let opts_replace = IngestOptions {
        table: "t".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    ingest_json(&te.engine, r#"[{"id": 1}]"#, &opts_replace).unwrap();

    let opts_merge = IngestOptions {
        table: "t".into(),
        mode: "merge".into(),
        schema_override: None,
        merge_key: Some(vec!["bogus_key".into()]),
        target_db: None,
    };
    let _ = ingest_json(&te.engine, r#"[{"id": 2, "bogus_key": "x"}]"#, &opts_merge);

    // Look for any leftover `__hyperdb_merge_*` table.
    let table_rows = te
        .engine
        .execute_query_to_json("SELECT table_name FROM \"$catalog\".\"public\".\"$tables\"")
        .or_else(|_| {
            // Fallback: if `$catalog` isn't queryable, use describe_tables (which the
            // engine exposes for the same purpose).
            te.engine.describe_tables().map(|tables| {
                tables
                    .into_iter()
                    .map(|v| serde_json::json!({ "table_name": v["name"] }))
                    .collect::<Vec<_>>()
            })
        })
        .unwrap();
    let has_orphan = table_rows.iter().any(|v| {
        v["table_name"]
            .as_str()
            .is_some_and(|s| s.starts_with("__hyperdb_merge_"))
    });
    assert!(
        !has_orphan,
        "merge failure must clean up its temp table; found orphan in: {table_rows:?}"
    );
}

/// Composite merge_key — a row matches only when *every* key column
/// agrees. Tests that the SQL builder emits `t."a" = s."a" AND
/// t."b" = s."b"` rather than just one clause, and that
/// non-matching key tuples insert as new rows.
#[test]
fn ingest_json_merge_multi_key() {
    let te = TestEngine::new_ephemeral();
    // Initial: 4 rows keyed by (region, year).
    let initial = r#"[
        {"region": "us", "year": 2025, "amount": 100},
        {"region": "us", "year": 2026, "amount": 200},
        {"region": "eu", "year": 2025, "amount": 300},
        {"region": "eu", "year": 2026, "amount": 400}
    ]"#;
    let opts_replace = IngestOptions {
        table: "sales".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    ingest_json(&te.engine, initial, &opts_replace).unwrap();

    // Merge: us/2026 updates (amount→999); eu/2027 is new; us/2025 stays untouched.
    let updates = r#"[
        {"region": "us", "year": 2026, "amount": 999},
        {"region": "eu", "year": 2027, "amount": 500}
    ]"#;
    let opts_merge = IngestOptions {
        table: "sales".into(),
        mode: "merge".into(),
        schema_override: None,
        merge_key: Some(vec!["region".into(), "year".into()]),
        target_db: None,
    };
    ingest_json(&te.engine, updates, &opts_merge).unwrap();

    let rows = te
        .engine
        .execute_query_to_json("SELECT * FROM sales ORDER BY region, year")
        .unwrap();
    assert_eq!(rows.len(), 5, "4 initial - 1 replaced + 2 new = 5");
    // eu/2025 untouched
    assert_eq!(rows[0]["region"], "eu");
    assert_eq!(rows[0]["year"], 2025);
    assert_eq!(rows[0]["amount"], 300);
    // eu/2026 untouched (only the (us,2026) tuple matched, not (eu,2026))
    assert_eq!(rows[1]["amount"], 400);
    // eu/2027 inserted
    assert_eq!(rows[2]["year"], 2027);
    assert_eq!(rows[2]["amount"], 500);
    // us/2025 untouched
    assert_eq!(rows[3]["region"], "us");
    assert_eq!(rows[3]["year"], 2025);
    assert_eq!(rows[3]["amount"], 100);
    // us/2026 updated
    assert_eq!(rows[4]["amount"], 999);
}

/// Type canonicalization round-trip: `INT` and `INTEGER` are aliases
/// in `map_hyper_type`, and Hyper's catalog canonicalizes `INT` →
/// `INTEGER` on storage. Without `types_compatible`, the merge would
/// false-reject this round-trip with a spurious "type mismatch"
/// even though the types are semantically identical. This test pins
/// the alias behavior so future refactors don't regress it.
#[test]
fn ingest_json_merge_type_canonicalization_does_not_false_reject() {
    let te = TestEngine::new_ephemeral();

    // Force the target's `id` column to be created from the `"INT"` user
    // string. After `CREATE TABLE`, Hyper canonicalizes to `"INTEGER"`
    // in the catalog — that's the canonicalization we need to absorb.
    let mut so_int = serde_json::Map::new();
    so_int.insert("id".into(), serde_json::json!("INT"));
    let opts_replace = IngestOptions {
        table: "t".into(),
        mode: "replace".into(),
        schema_override: Some(so_int),
        merge_key: None,
        target_db: None,
    };
    ingest_json(&te.engine, r#"[{"id": 1, "name": "a"}]"#, &opts_replace).unwrap();

    // Sanity: confirm the catalog canonicalized to INTEGER. (If this
    // assertion ever fails, Hyper's behavior changed, and the test
    // becomes vacuous — but the round-trip below still has value.)
    let cols = te.engine.column_metadata("t").unwrap();
    let id_col = cols.iter().find(|c| c.name == "id").unwrap();
    assert!(
        id_col.hyper_type.eq_ignore_ascii_case("INTEGER"),
        "expected canonicalized 'INTEGER' (or 'integer'); got {:?}",
        id_col.hyper_type
    );

    // Now merge with the same logical type — but force the *override*
    // to use the `INTEGER` alias to drive the comparison. (Without an
    // override, JSON's number inferrer would emit BIGINT for this
    // value and we'd test something different.)
    let mut so_integer = serde_json::Map::new();
    so_integer.insert("id".into(), serde_json::json!("INTEGER"));
    let opts_merge = IngestOptions {
        table: "t".into(),
        mode: "merge".into(),
        schema_override: Some(so_integer),
        merge_key: Some(vec!["id".into()]),
        target_db: None,
    };
    let result = ingest_json(
        &te.engine,
        r#"[{"id": 1, "name": "updated"}, {"id": 2, "name": "new"}]"#,
        &opts_merge,
    );
    // Critical: the merge must succeed. A naive string comparison would
    // reject because target.hyper_type may be "INTEGER" while incoming
    // schema_override carries "INTEGER" verbatim — and Hyper might
    // store one as lower-cased. types_compatible normalizes both.
    assert!(
        result.is_ok(),
        "merge must not false-reject equivalent types; got: {:?}",
        result.err()
    );

    let rows = te
        .engine
        .execute_query_to_json("SELECT * FROM t ORDER BY id")
        .unwrap();
    assert_eq!(rows.len(), 2);
    assert_eq!(rows[0]["name"], "updated");
    assert_eq!(rows[1]["name"], "new");
}

/// Smoke test for merge mode against inline CSV — `merge_via_temp_table`
/// is format-agnostic, but `ingest_csv` carries its own merge branch and
/// the symmetry deserves at least one test. Initial 3 rows; merge 2
/// overlapping (update) + 1 new (insert) → final 4.
#[test]
fn ingest_csv_merge_basic() {
    let te = TestEngine::new_ephemeral();
    let initial = "id,name\n1,Alice\n2,Bob\n3,Carol\n";
    let updates = "id,name\n2,Bob Jr.\n3,Carol Updated\n4,Dave\n";

    let opts_replace = IngestOptions {
        table: "users_csv".into(),
        mode: "replace".into(),
        schema_override: None,
        merge_key: None,
        target_db: None,
    };
    ingest_csv(&te.engine, initial, &opts_replace).unwrap();

    let opts_merge = IngestOptions {
        table: "users_csv".into(),
        mode: "merge".into(),
        schema_override: None,
        merge_key: Some(vec!["id".into()]),
        target_db: None,
    };
    let merge_result = ingest_csv(&te.engine, updates, &opts_merge).unwrap();
    assert!(
        !merge_result.stats.schema_changed,
        "row-only merge must leave schema_changed false"
    );

    let rows = te
        .engine
        .execute_query_to_json("SELECT * FROM users_csv ORDER BY id")
        .unwrap();
    assert_eq!(rows.len(), 4);
    assert_eq!(rows[0]["name"], "Alice"); // unchanged
    assert_eq!(rows[1]["name"], "Bob Jr."); // updated
    assert_eq!(rows[2]["name"], "Carol Updated"); // updated
    assert_eq!(rows[3]["name"], "Dave"); // inserted
}