tuitab 0.7.0

Terminal tabular data explorer — CSV/JSON/YAML/TOML/Parquet/Excel/SQLite viewer with filtering, sorting, pivot tables, and charts
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
//! Coverage for the MCP server.
//!
//! Most of these drive `handle_message` directly — it is the whole request path
//! minus the pipe, so a test gets the same answer a client would without paying
//! for a process.  One test at the end does spawn the binary, to check the
//! framing and that nothing leaks onto stdout.

use serde_json::{json, Value};
use std::path::PathBuf;
use tuitab::mcp::{handle_message, Server};

fn tmp(name: &str) -> PathBuf {
    // Not the system temp dir: test output belongs to the project, under the
    // gitignored tmp/ directory.
    let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tmp")
        .join("mcp-tests");
    std::fs::create_dir_all(&dir).unwrap();
    dir.join(name)
}

fn send(server: &mut Server, message: Value) -> Value {
    handle_message(server, &message.to_string()).expect("a request must get a response")
}

/// Call a tool and return its structured payload, failing on a tool error.
fn call(server: &mut Server, name: &str, arguments: Value) -> Value {
    let response = send(
        server,
        json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call",
               "params": {"name": name, "arguments": arguments}}),
    );
    let result = response
        .get("result")
        .unwrap_or_else(|| panic!("{} returned a protocol error: {}", name, response));
    assert_eq!(
        result.get("isError"),
        Some(&json!(false)),
        "{} failed: {}",
        name,
        result
    );
    result.get("structuredContent").cloned().unwrap()
}

/// Call a tool expecting it to run and fail, returning the message.
fn call_expecting_failure(server: &mut Server, name: &str, arguments: Value) -> String {
    let response = send(
        server,
        json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call",
               "params": {"name": name, "arguments": arguments}}),
    );
    let result = response
        .get("result")
        .expect("a tool error is still a result");
    assert_eq!(
        result.get("isError"),
        Some(&json!(true)),
        "expected {} to fail, got {}",
        name,
        result
    );
    result["content"][0]["text"].as_str().unwrap().to_string()
}

fn query(server: &mut Server, ops: Value) -> Value {
    call(
        server,
        "tuitab_query",
        json!({"source": {"path": "test_data/sample.csv"}, "ops": ops}),
    )
}

/// Cells of a result row, keyed by column name.
fn row(result: &Value, index: usize) -> Vec<(String, Value)> {
    let names: Vec<String> = result["columns"]
        .as_array()
        .unwrap()
        .iter()
        .map(|c| c["name"].as_str().unwrap().to_string())
        .collect();
    names
        .into_iter()
        .zip(result["rows"][index].as_array().unwrap().iter().cloned())
        .collect()
}

fn cell(result: &Value, index: usize, column: &str) -> Value {
    row(result, index)
        .into_iter()
        .find(|(name, _)| name == column)
        .unwrap_or_else(|| panic!("no column '{}' in {}", column, result["columns"]))
        .1
}

// ── protocol ────────────────────────────────────────────────────────────────

#[test]
fn initialize_answers_with_the_requested_version_and_the_instructions() {
    let mut server = Server::new();
    let response = send(
        &mut server,
        json!({"jsonrpc": "2.0", "id": 1, "method": "initialize",
               "params": {"protocolVersion": "2025-06-18", "capabilities": {},
                          "clientInfo": {"name": "test", "version": "0"}}}),
    );

    let result = &response["result"];
    assert_eq!(result["protocolVersion"], "2025-06-18");
    assert_eq!(result["serverInfo"]["name"], "tuitab");
    assert!(result["capabilities"]["tools"].is_object());

    let instructions = result["instructions"].as_str().unwrap();
    assert!(instructions.contains("tuitab_inspect"), "{}", instructions);
    assert!(instructions.contains("group_by"), "{}", instructions);
}

#[test]
fn an_unsupported_protocol_version_falls_back_to_one_we_speak() {
    let mut server = Server::new();
    let response = send(
        &mut server,
        json!({"jsonrpc": "2.0", "id": 1, "method": "initialize",
               "params": {"protocolVersion": "1.0.0"}}),
    );
    assert_eq!(response["result"]["protocolVersion"], "2025-06-18");
}

#[test]
fn notifications_and_responses_get_no_reply() {
    let mut server = Server::new();
    assert!(handle_message(
        &mut server,
        r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#
    )
    .is_none());
    assert!(handle_message(&mut server, r#"{"jsonrpc":"2.0","id":7,"result":{}}"#).is_none());
}

#[test]
fn malformed_input_is_a_parse_error_rather_than_a_crash() {
    let mut server = Server::new();
    let response = handle_message(&mut server, "not json at all").unwrap();
    assert_eq!(response["error"]["code"], -32700);
}

#[test]
fn tools_list_offers_the_four_tools() {
    let mut server = Server::new();
    let response = send(
        &mut server,
        json!({"jsonrpc": "2.0", "id": 1, "method": "tools/list"}),
    );

    let names: Vec<&str> = response["result"]["tools"]
        .as_array()
        .unwrap()
        .iter()
        .map(|t| t["name"].as_str().unwrap())
        .collect();
    assert_eq!(
        names,
        vec![
            "tuitab_inspect",
            "tuitab_query",
            "tuitab_describe",
            "tuitab_jq"
        ]
    );

    for tool in response["result"]["tools"].as_array().unwrap() {
        assert!(
            tool["inputSchema"]["properties"].is_object(),
            "{}",
            tool["name"]
        );
        assert!(
            !tool["description"].as_str().unwrap().is_empty(),
            "{}",
            tool["name"]
        );
    }
}

#[test]
fn an_unknown_method_is_a_protocol_error() {
    let mut server = Server::new();
    let response = send(
        &mut server,
        json!({"jsonrpc": "2.0", "id": 1, "method": "nope"}),
    );
    assert_eq!(response["error"]["code"], -32601);
}

/// An unknown *tool* breaks the contract, so it is a protocol error — unlike a
/// tool that runs and fails, which comes back as a result the model can read.
#[test]
fn an_unknown_tool_is_a_protocol_error() {
    let mut server = Server::new();
    let response = send(
        &mut server,
        json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call",
               "params": {"name": "tuitab_nonesuch", "arguments": {}}}),
    );
    assert_eq!(response["error"]["code"], -32602);
    assert!(response["error"]["message"]
        .as_str()
        .unwrap()
        .contains("tuitab_nonesuch"));
}

// ── tuitab_inspect ──────────────────────────────────────────────────────────

#[test]
fn inspect_reports_columns_types_and_a_sample() {
    let mut server = Server::new();
    let result = call(
        &mut server,
        "tuitab_inspect",
        json!({"source": "test_data/sample.csv"}),
    );

    assert_eq!(result["row_count"], 20);
    let columns: Vec<&str> = result["columns"]
        .as_array()
        .unwrap()
        .iter()
        .map(|c| c["name"].as_str().unwrap())
        .collect();
    assert_eq!(columns, vec!["id", "name", "age", "salary", "department"]);

    let types: Vec<&str> = result["columns"]
        .as_array()
        .unwrap()
        .iter()
        .map(|c| c["type"].as_str().unwrap())
        .collect();
    assert_eq!(types[2], "integer", "age");
    assert_eq!(types[4], "string", "department");

    assert_eq!(result["sample_rows"].as_array().unwrap().len(), 5);
}

/// Multi-table formats are the reason `container` exists, and the repository
/// ships no SQLite fixture — so this makes one, which also exercises the saver.
#[test]
fn inspect_lists_tables_before_showing_columns() {
    let mut server = Server::new();
    let db = tmp("sample.sqlite");
    let _ = std::fs::remove_file(&db);

    call(
        &mut server,
        "tuitab_query",
        json!({"source": "test_data/sample.csv", "ops": [],
               "output": {"path": db.to_string_lossy()}}),
    );

    // Without a container, the answer is the list of tables, not rows.
    let listing = call(
        &mut server,
        "tuitab_inspect",
        json!({"source": {"path": db.to_string_lossy()}}),
    );
    let tables: Vec<&str> = listing["containers"]
        .as_array()
        .unwrap()
        .iter()
        .map(|t| t.as_str().unwrap())
        .collect();
    assert_eq!(
        tables,
        vec!["data"],
        "save_sqlite writes one table named 'data'"
    );
    assert!(
        listing.get("columns").is_none(),
        "no columns without a container"
    );
    assert!(listing["note"].as_str().unwrap().contains("container"));

    // With one, it is an ordinary table.
    let table = call(
        &mut server,
        "tuitab_inspect",
        json!({"source": {"path": db.to_string_lossy(), "container": "data"}}),
    );
    assert_eq!(table["row_count"], 20);

    // And it queries like any other source.
    let result = call(
        &mut server,
        "tuitab_query",
        json!({"source": {"path": db.to_string_lossy(), "container": "data"},
               "ops": [{"frequency": {"by": ["department"]}}]}),
    );
    assert_eq!(result["row_count"], 4);

    std::fs::remove_file(&db).unwrap();
}

#[test]
fn inspect_reports_a_missing_file_as_a_tool_error() {
    let mut server = Server::new();
    let message = call_expecting_failure(
        &mut server,
        "tuitab_inspect",
        json!({"source": "test_data/does_not_exist.csv"}),
    );
    assert!(message.contains("No such file"), "{}", message);
}

// ── filtering and sorting ───────────────────────────────────────────────────

#[test]
fn filter_keeps_only_matching_rows() {
    let mut server = Server::new();
    // Seven people are over 40: ages 45, 42, 52, 47, 44, 50 and 41.
    let result = query(
        &mut server,
        json!([{"filter": [{"col": "age", "op": "gt", "value": 40}]}]),
    );
    assert_eq!(result["row_count"], 7);
}

#[test]
fn filter_predicates_combine_with_and() {
    let mut server = Server::new();
    // Engineering ages are 30, 28, 42, 52, 33, 37 and 34 — two are over 40.
    // Seven people company-wide are, so this only passes if both predicates
    // apply rather than the last one winning.
    let result = query(
        &mut server,
        json!([{"filter": [
            {"col": "department", "op": "eq", "value": "Engineering"},
            {"col": "age", "op": "gt", "value": 40}
        ]}]),
    );
    assert_eq!(result["row_count"], 2);
}

/// An aggregate in `compute` must see only the rows that survived `filter`.
///
/// `add_computed_column` evaluates over the whole physical frame
/// (`dataframe.rs:1101`), and `sum(x)` lowers to a broadcast over the entire
/// column (`expression.rs:213`).  Unless a row-dropping operation materialises
/// its result, a share-of-total lands on the wrong denominator — and looks
/// perfectly reasonable while doing it.
#[test]
fn an_aggregate_after_filter_uses_only_the_surviving_rows() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([
            {"filter": [{"col": "department", "op": "eq", "value": "Engineering"}]},
            {"compute": {"name": "share", "expr": "salary / sum(salary)"}}
        ]),
    );

    assert_eq!(result["row_count"], 7, "Engineering has seven people");

    let shares: f64 = (0..7)
        .map(|i| cell(&result, i, "share").as_f64().unwrap())
        .sum();

    // The seven Engineering salaries total 563001.50; the whole file totals
    // 1624003.25.  Dividing by the latter would give 0.3467 — a number that
    // reads like a plausible answer to a different question.
    assert!(
        (shares - 1.0).abs() < 1e-9,
        "shares must total 1.0 within the filtered set, got {}",
        shares
    );
}

#[test]
fn filter_supports_in_between_and_contains() {
    let mut server = Server::new();

    let in_list = query(
        &mut server,
        json!([{"filter": [{"col": "department", "op": "in", "value": ["HR", "Marketing"]}]}]),
    );
    assert_eq!(in_list["row_count"], 8, "4 in HR and 4 in Marketing");

    let between = query(
        &mut server,
        json!([{"filter": [{"col": "age", "op": "between", "value": [30, 35]}]}]),
    );
    assert_eq!(
        between["row_count"], 5,
        "ages 30, 35, 31, 33 and 34, bounds included"
    );

    let contains = query(
        &mut server,
        json!([{"filter": [{"col": "name", "op": "contains", "value": "^A"}]}]),
    );
    assert_eq!(contains["row_count"], 1, "only Alice Johnson starts with A");
}

#[test]
fn any_of_gives_the_filter_an_or() {
    let mut server = Server::new();

    let either = query(
        &mut server,
        json!([{"filter": [{"any_of": [
            {"col": "department", "op": "eq", "value": "HR"},
            {"col": "department", "op": "eq", "value": "Marketing"}
        ]}]}]),
    );
    assert_eq!(either["row_count"], 8, "4 in HR and 4 in Marketing");

    // A group beside a plain predicate reads as (A OR B) AND C.
    let narrowed = query(
        &mut server,
        json!([{"filter": [
            {"any_of": [{"col": "department", "op": "eq", "value": "HR"},
                        {"col": "department", "op": "eq", "value": "Marketing"}]},
            {"col": "age", "op": "gt", "value": 40}
        ]}]),
    );
    assert_eq!(narrowed["row_count"], 1, "only Noah Martin, 44");
}

#[test]
fn a_predicate_can_name_another_column_as_its_value() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([{"filter": [{"col": "salary", "op": "gt", "value": {"col": "age"}}]}]),
    );
    assert_eq!(result["row_count"], 20, "every salary exceeds its age");
}

/// A filter that legitimately matches nothing must return nothing, not fall
/// through to a second evaluator with different semantics.
#[test]
fn a_filter_matching_nothing_returns_nothing() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([{"filter": [{"col": "department", "op": "eq", "value": "Legal"}]}]),
    );
    assert_eq!(result["row_count"], 0);
    assert_eq!(result["returned"], 0);
}

#[test]
fn a_filter_on_an_unknown_column_names_the_ones_that_exist() {
    let mut server = Server::new();
    let message = call_expecting_failure(
        &mut server,
        "tuitab_query",
        json!({"source": "test_data/sample.csv",
               "ops": [{"filter": [{"col": "salary_usd", "op": "gt", "value": 1}]}]}),
    );
    assert!(message.contains("salary_usd"), "{}", message);
    assert!(
        message.contains("department"),
        "must list what is available: {}",
        message
    );
}

#[test]
fn sort_orders_rows_and_composes_with_filter() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([
            {"filter": [{"col": "department", "op": "eq", "value": "HR"}]},
            {"sort": {"col": "age", "desc": true}}
        ]),
    );
    // HR ages are 25, 29, 26 and 27.
    assert_eq!(result["row_count"], 4);
    assert_eq!(cell(&result, 0, "age"), json!(29));
    assert_eq!(cell(&result, 3, "age"), json!(25));
}

#[test]
fn sort_takes_several_keys_at_once() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([{"sort": {"by": [{"col": "department"}, {"col": "age", "desc": true}]}}]),
    );

    // Engineering sorts first alphabetically, and holds ages 52, 42, 37, 34,
    // 33, 30, 28 once ordered downwards.
    for (i, age) in [52, 42, 37, 34, 33, 30, 28].iter().enumerate() {
        assert_eq!(cell(&result, i, "department"), json!("Engineering"));
        assert_eq!(cell(&result, i, "age"), json!(age));
    }
}

/// A bare column name and the single-key object form both still work — a model
/// asking one simple question should not have to write a list.
#[test]
fn sort_still_accepts_the_short_forms() {
    let mut server = Server::new();

    let object_form = query(&mut server, json!([{"sort": {"col": "age", "desc": true}}]));
    assert_eq!(cell(&object_form, 0, "age"), json!(52));

    let bare_name = query(&mut server, json!([{"sort": {"by": "age"}}]));
    assert_eq!(cell(&bare_name, 0, "age"), json!(25));
}

// ── aggregation ─────────────────────────────────────────────────────────────

#[test]
fn aggregate_answers_a_grand_total() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([{"aggregate": [{"col": "salary", "fn": "sum"},
                              {"col": "*", "fn": "count"}]}]),
    );

    assert_eq!(result["row_count"], 1, "a total is a single row");
    assert_eq!(cell(&result, 0, "salary:sum"), json!(1_624_003.25));
    assert_eq!(cell(&result, 0, "count"), json!(20));
}

#[test]
fn aggregate_totals_only_what_survived_the_filter() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([
            {"filter": [{"col": "department", "op": "eq", "value": "Engineering"}]},
            {"aggregate": [{"col": "salary", "fn": "sum"}]}
        ]),
    );
    assert_eq!(cell(&result, 0, "salary:sum"), json!(563_001.5));
}

#[test]
fn group_by_returns_exactly_the_requested_aggregates() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([{"group_by": {"by": ["department"],
                             "agg": [{"col": "salary", "fn": "sum"},
                                     {"col": "*", "fn": "count"}]}}]),
    );

    let columns: Vec<&str> = result["columns"]
        .as_array()
        .unwrap()
        .iter()
        .map(|c| c["name"].as_str().unwrap())
        .collect();
    assert_eq!(
        columns,
        vec!["department", "salary:sum", "count"],
        "no Count, Pct or Bar columns anyone did not ask for"
    );

    // group_by_stable keeps first-appearance order: Engineering, Management,
    // Marketing, HR.
    assert_eq!(cell(&result, 0, "department"), json!("Engineering"));
    assert_eq!(cell(&result, 0, "salary:sum"), json!(563001.5));
    assert_eq!(cell(&result, 0, "count"), json!(7));
    assert_eq!(cell(&result, 1, "department"), json!("Management"));
    assert_eq!(cell(&result, 1, "salary:sum"), json!(480000.5));
}

/// `preserves_col_type` hands an average the source column's type, which is
/// right for TUI formatting and wrong as a label on a fractional number.
#[test]
fn an_average_over_integers_is_reported_as_a_float() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([{"group_by": {"by": ["department"], "agg": [{"col": "age", "fn": "avg"}]}}]),
    );

    let age_avg = result["columns"]
        .as_array()
        .unwrap()
        .iter()
        .find(|c| c["name"] == "age:avg")
        .unwrap();
    assert_eq!(age_avg["type"], "float");

    // Engineering ages 30, 28, 42, 52, 33, 37, 34 sum to 256 over 7 people.
    assert_eq!(cell(&result, 0, "age:avg"), json!(256.0 / 7.0));
}

#[test]
fn group_by_after_filter_aggregates_only_the_surviving_rows() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([
            {"filter": [{"col": "age", "op": "gt", "value": 40}]},
            {"group_by": {"by": ["department"], "agg": [{"col": "*", "fn": "count"}]}}
        ]),
    );
    // Of the seven over-40s: Management 4 (45, 47, 50, 41), Engineering 2
    // (42, 52), Marketing 1 (44).
    let total: i64 = result["rows"]
        .as_array()
        .unwrap()
        .iter()
        .map(|r| r[1].as_i64().unwrap())
        .sum();
    assert_eq!(total, 7);
}

/// build_frequency_table silently drops an incompatible aggregator
/// (dataframe.rs:1445).  That would hand the model a short answer with no way
/// to notice, so the MCP layer refuses instead.
#[test]
fn an_aggregate_that_cannot_apply_is_an_error_not_a_silent_omission() {
    let mut server = Server::new();
    let message = call_expecting_failure(
        &mut server,
        "tuitab_query",
        json!({"source": "test_data/sample.csv",
               "ops": [{"group_by": {"by": ["department"],
                                     "agg": [{"col": "name", "fn": "sum"}]}}]}),
    );
    assert!(message.contains("name"), "{}", message);
    assert!(message.contains("sum"), "{}", message);
    assert!(message.contains("string"), "must say why: {}", message);
}

#[test]
fn frequency_ranks_by_count_and_carries_a_share_column() {
    let mut server = Server::new();
    let result = query(&mut server, json!([{"frequency": {"by": ["department"]}}]));

    let columns: Vec<&str> = result["columns"]
        .as_array()
        .unwrap()
        .iter()
        .map(|c| c["name"].as_str().unwrap())
        .collect();
    assert_eq!(
        columns,
        vec!["department", "Count", "Pct"],
        "the ASCII Bar column is dropped"
    );

    assert_eq!(result["row_count"], 4);
    assert_eq!(cell(&result, 0, "department"), json!("Engineering"));
    assert_eq!(cell(&result, 0, "Count"), json!(7));
    // Percentages are fractions, not "35%".
    assert_eq!(cell(&result, 0, "Pct"), json!(0.35));
}

#[test]
fn compute_adds_a_derived_column() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([
            {"compute": {"name": "decade", "expr": "age / 10"}},
            {"filter": [{"col": "department", "op": "eq", "value": "HR"}]},
            {"sort": {"col": "age", "desc": false}}
        ]),
    );
    // The youngest HR person is 25.
    assert_eq!(cell(&result, 0, "decade"), json!(2.5));
}

// ── join ────────────────────────────────────────────────────────────────────

#[test]
fn join_brings_in_columns_from_a_second_file() {
    let mut server = Server::new();
    let result = query(
        &mut server,
        json!([
            {"join": {"source": {"path": "test_data/prices.csv"},
                      "left_on": ["id"], "how": "inner"}},
            {"sort": {"col": "id", "desc": false}}
        ]),
    );
    // prices.csv holds ids 1 and 2 only.
    assert_eq!(result["row_count"], 2);
    assert_eq!(cell(&result, 0, "value"), json!(100));
    assert_eq!(cell(&result, 1, "value"), json!(200));
}

// ── output shaping ──────────────────────────────────────────────────────────

#[test]
fn a_truncated_result_says_so_and_still_reports_the_real_count() {
    let mut server = Server::new();
    let result = call(
        &mut server,
        "tuitab_query",
        json!({"source": "test_data/sample.csv", "ops": [], "output": {"limit": 2}}),
    );

    assert_eq!(result["returned"], 2);
    assert_eq!(result["row_count"], 20);
    assert_eq!(result["truncated"], true);
    assert!(result["note"].as_str().unwrap().contains("output.path"));
}

#[test]
fn several_pipelines_run_against_one_load() {
    let mut server = Server::new();
    let result = call(
        &mut server,
        "tuitab_query",
        json!({"source": "test_data/sample.csv", "pipelines": [
            {"name": "headcount", "ops": [{"frequency": {"by": ["department"]}}]},
            {"name": "seniors", "ops": [{"filter": [{"col": "age", "op": "ge", "value": 45}]}]}
        ]}),
    );

    let results = result["results"].as_array().unwrap();
    assert_eq!(results.len(), 2);
    assert_eq!(results[0]["name"], "headcount");
    assert_eq!(results[0]["row_count"], 4);
    assert_eq!(results[1]["name"], "seniors");
    assert_eq!(results[1]["row_count"], 4, "ages 45, 52, 47 and 50");
}

#[test]
fn output_path_writes_a_file_and_refuses_to_clobber_one() {
    let mut server = Server::new();
    let path = tmp("headcount.csv");
    let _ = std::fs::remove_file(&path);

    let ops = json!([{"frequency": {"by": ["department"]}}]);
    let result = call(
        &mut server,
        "tuitab_query",
        json!({"source": "test_data/sample.csv", "ops": ops,
               "output": {"path": path.to_string_lossy()}}),
    );

    assert_eq!(result["row_count"], 4);
    let written = std::fs::read_to_string(&path).unwrap();
    assert!(written.contains("Engineering"), "{}", written);

    // A second run must not quietly replace it.
    let message = call_expecting_failure(
        &mut server,
        "tuitab_query",
        json!({"source": "test_data/sample.csv", "ops": ops,
               "output": {"path": path.to_string_lossy()}}),
    );
    assert!(message.contains("already exists"), "{}", message);

    // Unless asked to.
    call(
        &mut server,
        "tuitab_query",
        json!({"source": "test_data/sample.csv", "ops": ops,
               "output": {"path": path.to_string_lossy(), "overwrite": true}}),
    );

    std::fs::remove_file(&path).unwrap();
}

// ── describe and jq ─────────────────────────────────────────────────────────

#[test]
fn describe_labels_the_population_standard_deviation_unambiguously() {
    let mut server = Server::new();
    let result = call(
        &mut server,
        "tuitab_describe",
        json!({"source": "test_data/sample.csv", "columns": ["age"]}),
    );

    let metrics: Vec<&str> = result["rows"]
        .as_array()
        .unwrap()
        .iter()
        .map(|r| r[0].as_str().unwrap())
        .collect();
    assert!(metrics.contains(&"stdev_pop"), "{:?}", metrics);
    assert!(
        !metrics.contains(&"stdev"),
        "the ambiguous label is gone: {:?}",
        metrics
    );

    let mean = result["rows"]
        .as_array()
        .unwrap()
        .iter()
        .find(|r| r[0] == "mean")
        .unwrap();
    assert_eq!(mean[1], "36.65");
}

#[test]
fn jq_walks_a_nested_document() {
    let mut server = Server::new();
    let result = call(
        &mut server,
        "tuitab_jq",
        json!({"source": "test_data/nested.json", "program": "map(.name)"}),
    );
    assert_eq!(result["result"], json!(["alpha", "beta"]));
}

#[test]
fn jq_on_a_csv_points_at_the_right_tool() {
    let mut server = Server::new();
    let message = call_expecting_failure(
        &mut server,
        "tuitab_jq",
        json!({"source": "test_data/sample.csv", "program": "."}),
    );
    assert!(message.contains("tuitab_query"), "{}", message);
}

// ── the wire ────────────────────────────────────────────────────────────────

/// The one test that pays for a process: it checks the newline framing and that
/// nothing but protocol messages reaches stdout.
#[test]
fn the_binary_speaks_the_protocol_over_stdio() {
    use std::io::Write;
    use std::process::{Command, Stdio};

    let mut child = Command::new(env!("CARGO_BIN_EXE_tuitab"))
        .arg("--mcp")
        .current_dir(env!("CARGO_MANIFEST_DIR"))
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .unwrap();

    let script = [
        r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"t","version":"0"}}}"#,
        r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#,
        r#"{"jsonrpc":"2.0","id":2,"method":"tools/list"}"#,
        r#"{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"tuitab_inspect","arguments":{"source":"test_data/sample.csv"}}}"#,
    ]
    .join("\n");

    child
        .stdin
        .take()
        .unwrap()
        .write_all(format!("{}\n", script).as_bytes())
        .unwrap();

    let output = child.wait_with_output().unwrap();
    assert!(
        output.status.success(),
        "server exited with {}",
        output.status
    );

    let stdout = String::from_utf8(output.stdout).unwrap();
    let lines: Vec<&str> = stdout.lines().filter(|l| !l.is_empty()).collect();

    // Three requests, three replies — the notification gets none.
    assert_eq!(lines.len(), 3, "unexpected stdout:\n{}", stdout);

    for line in &lines {
        let value: Value = serde_json::from_str(line)
            .unwrap_or_else(|e| panic!("stdout line is not a message: {} ({})", line, e));
        assert_eq!(value["jsonrpc"], "2.0");
    }

    let ids: Vec<u64> = lines
        .iter()
        .map(|l| {
            serde_json::from_str::<Value>(l).unwrap()["id"]
                .as_u64()
                .unwrap()
        })
        .collect();
    assert_eq!(ids, vec![1, 2, 3]);
}

/// An unseeded random operation must report the seed it drew, or its result is
/// one nobody — including the model quoting it — can reproduce.
#[test]
fn an_unseeded_sample_reports_the_seed_it_used() {
    let mut server = Server::new();
    let first = query(&mut server, json!([{"sample": {"n": 5}}]));

    let seeds = first["seeds"]
        .as_array()
        .expect("the drawn seed must come back");
    assert_eq!(seeds.len(), 1);
    let seed = seeds[0]["seed"].as_u64().unwrap();

    // Feeding it back reproduces the same rows.
    let repeated = query(&mut server, json!([{"sample": {"n": 5, "seed": seed}}]));
    assert_eq!(first["rows"], repeated["rows"]);
}

#[test]
fn a_seeded_operation_reports_no_seed_because_none_was_drawn() {
    let mut server = Server::new();
    let result = query(&mut server, json!([{"sample": {"n": 5, "seed": 3}}]));
    assert!(
        result.get("seeds").is_none(),
        "nothing was drawn for the caller"
    );
}