terraphim-cli 1.20.3

CLI tool for semantic knowledge graph search with JSON output for automation
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
//! Integration tests for terraphim-cli
//!
//! These tests verify end-to-end functionality of role switching,
//! KG search, and replace operations.

#[allow(deprecated)] // cargo_bin is deprecated but still works
use assert_cmd::Command;
#[allow(unused_imports)] // Used in test assertions
use predicates::prelude::*;
use serial_test::serial;

/// Get a command for the terraphim-cli binary
#[allow(deprecated)] // cargo_bin is deprecated but still functional
fn cli_command() -> Command {
    Command::cargo_bin("terraphim-cli").unwrap()
}

/// Helper to run CLI and get JSON output
fn run_cli_json(args: &[&str]) -> Result<serde_json::Value, String> {
    let output = Command::cargo_bin("terraphim-cli")
        .map_err(|e| format!("Failed to find binary: {}", e))?
        .args(args)
        .output()
        .map_err(|e| format!("Failed to execute: {}", e))?;

    let stdout = String::from_utf8_lossy(&output.stdout);

    if !output.status.success() {
        // Command failed - return error even if output is valid JSON (error object)
        return Err(format!(
            "Command failed with exit code {:?}: {}",
            output.status.code(),
            String::from_utf8_lossy(&output.stderr)
        ));
    }

    serde_json::from_str(&stdout)
        .map_err(|e| format!("Failed to parse JSON: {} - output: {}", e, stdout))
}

#[cfg(test)]
mod role_switching_tests {
    use super::*;

    #[test]
    #[serial]
    fn test_list_roles() {
        let result = run_cli_json(&["roles"]);

        match result {
            Ok(json) => {
                assert!(json.is_array(), "Roles should be an array");
                let roles = json.as_array().unwrap();
                // Should have at least one role (Default)
                assert!(!roles.is_empty(), "Should have at least one role");
            }
            Err(e) => {
                // May fail if service can't initialize - acceptable in CI
                eprintln!("Roles test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_config_shows_selected_role() {
        let result = run_cli_json(&["config"]);

        match result {
            Ok(json) => {
                assert!(
                    json.get("selected_role").is_some(),
                    "Config should have selected_role"
                );
                let selected = json["selected_role"].as_str().unwrap();
                assert!(!selected.is_empty(), "Selected role should not be empty");
            }
            Err(e) => {
                eprintln!("Config test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_search_with_default_role() {
        let result = run_cli_json(&["search", "test query"]);

        match result {
            Ok(json) => {
                assert!(json.get("role").is_some(), "Search result should have role");
                // Role should be the default selected role
                let role = json["role"].as_str().unwrap();
                assert!(!role.is_empty(), "Role should not be empty");
            }
            Err(e) => {
                eprintln!("Search test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_search_with_explicit_role() {
        let result = run_cli_json(&["search", "test", "--role", "Default"]);

        match result {
            Ok(json) => {
                assert_eq!(
                    json["role"].as_str(),
                    Some("Default"),
                    "Should use specified role"
                );
            }
            Err(e) => {
                eprintln!("Search with role test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_graph_with_explicit_role() {
        let result = run_cli_json(&["graph", "--role", "Default"]);

        match result {
            Ok(json) => {
                assert_eq!(
                    json["role"].as_str(),
                    Some("Default"),
                    "Should use specified role"
                );
            }
            Err(e) => {
                eprintln!("Graph with role test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_find_with_explicit_role() {
        let result = run_cli_json(&["find", "test text", "--role", "Default"]);

        match result {
            Ok(json) => {
                // Check if this is an error response or success response
                if json.get("error").is_some() {
                    eprintln!("Find with role returned error: {:?}", json);
                    return;
                }
                // Should succeed with the specified role
                assert!(
                    json.get("text").is_some() || json.get("matches").is_some(),
                    "Find should have text or matches field"
                );
            }
            Err(e) => {
                eprintln!("Find with role test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_replace_with_explicit_role() {
        let result = run_cli_json(&["replace", "test text", "--role", "Default"]);

        match result {
            Ok(json) => {
                // Check if this is an error response
                if json.get("error").is_some() {
                    eprintln!("Replace with role returned error: {:?}", json);
                    return;
                }
                // May have original field or be an error
                assert!(
                    json.get("original").is_some() || json.get("replaced").is_some(),
                    "Replace should have original or replaced field: {:?}",
                    json
                );
            }
            Err(e) => {
                eprintln!("Replace with role test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_thesaurus_with_explicit_role() {
        let result = run_cli_json(&["thesaurus", "--role", "Default"]);

        match result {
            Ok(json) => {
                // Check if this is an error response
                if json.get("error").is_some() {
                    eprintln!("Thesaurus with role returned error: {:?}", json);
                    return;
                }
                // Should have either role or terms field
                assert!(
                    json.get("role").is_some()
                        || json.get("terms").is_some()
                        || json.get("name").is_some(),
                    "Thesaurus should have role, terms, or name field: {:?}",
                    json
                );
            }
            Err(e) => {
                eprintln!("Thesaurus with role test skipped: {}", e);
            }
        }
    }
}

#[cfg(test)]
mod kg_search_tests {
    use super::*;

    #[test]
    #[serial]
    fn test_basic_search() {
        let result = run_cli_json(&["search", "rust"]);

        match result {
            Ok(json) => {
                assert_eq!(json["query"].as_str(), Some("rust"));
                assert!(json.get("results").is_some());
                assert!(json.get("count").is_some());
            }
            Err(e) => {
                eprintln!("Basic search test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_search_with_limit() {
        let result = run_cli_json(&["search", "test", "--limit", "3"]);

        match result {
            Ok(json) => {
                let count = json["count"].as_u64().unwrap_or(0);
                assert!(count <= 3, "Results should respect limit");
            }
            Err(e) => {
                eprintln!("Search with limit test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_search_with_multiple_words() {
        let result = run_cli_json(&["search", "rust async programming"]);

        match result {
            Ok(json) => {
                assert_eq!(json["query"].as_str(), Some("rust async programming"));
            }
            Err(e) => {
                eprintln!("Multi-word search test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_search_returns_array_of_results() {
        let result = run_cli_json(&["search", "tokio"]);

        match result {
            Ok(json) => {
                assert!(json["results"].is_array(), "Results should be an array");
            }
            Err(e) => {
                eprintln!("Search results array test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_search_results_have_required_fields() {
        let result = run_cli_json(&["search", "api"]);

        match result {
            Ok(json) => {
                if let Some(results) = json["results"].as_array() {
                    for doc in results {
                        assert!(doc.get("id").is_some(), "Document should have id");
                        assert!(doc.get("title").is_some(), "Document should have title");
                        assert!(doc.get("url").is_some(), "Document should have url");
                    }
                }
            }
            Err(e) => {
                eprintln!("Search results fields test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_graph_returns_concepts() {
        let result = run_cli_json(&["graph"]);

        match result {
            Ok(json) => {
                assert!(json.get("concepts").is_some(), "Graph should have concepts");
                assert!(json["concepts"].is_array(), "Concepts should be an array");
            }
            Err(e) => {
                eprintln!("Graph concepts test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_graph_with_custom_top_k() {
        let result = run_cli_json(&["graph", "--top-k", "5"]);

        match result {
            Ok(json) => {
                assert_eq!(json["top_k"].as_u64(), Some(5));
                let concepts = json["concepts"].as_array().unwrap();
                assert!(concepts.len() <= 5, "Should return at most 5 concepts");
            }
            Err(e) => {
                eprintln!("Graph top-k test skipped: {}", e);
            }
        }
    }
}

#[cfg(test)]
mod replace_tests {
    use super::*;

    #[test]
    #[serial]
    fn test_replace_markdown_format() {
        let result = run_cli_json(&["replace", "rust programming", "--link-format", "markdown"]);

        match result {
            Ok(json) => {
                // Skip test if KG not configured (returns error JSON)
                if json.get("error").is_some() {
                    eprintln!("Replace markdown test skipped: Knowledge graph not configured");
                    return;
                }
                assert_eq!(json["format"].as_str(), Some("markdown"));
                assert_eq!(json["original"].as_str(), Some("rust programming"));
                assert!(json.get("replaced").is_some());
            }
            Err(e) => {
                eprintln!("Replace markdown test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_replace_html_format() {
        let result = run_cli_json(&["replace", "async tokio", "--link-format", "html"]);

        match result {
            Ok(json) => {
                // Skip test if KG not configured (returns error JSON)
                if json.get("error").is_some() {
                    eprintln!("Replace html test skipped: Knowledge graph not configured");
                    return;
                }
                assert_eq!(json["format"].as_str(), Some("html"));
            }
            Err(e) => {
                eprintln!("Replace html test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_replace_wiki_format() {
        let result = run_cli_json(&["replace", "docker kubernetes", "--link-format", "wiki"]);

        match result {
            Ok(json) => {
                // Skip test if KG not configured (returns error JSON)
                if json.get("error").is_some() {
                    eprintln!("Replace wiki test skipped: Knowledge graph not configured");
                    return;
                }
                assert_eq!(json["format"].as_str(), Some("wiki"));
            }
            Err(e) => {
                eprintln!("Replace wiki test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_replace_plain_format() {
        let result = run_cli_json(&["replace", "git github", "--link-format", "plain"]);

        match result {
            Ok(json) => {
                // Skip test if KG not configured (returns error JSON)
                if json.get("error").is_some() {
                    eprintln!("Replace plain test skipped: Knowledge graph not configured");
                    return;
                }
                assert_eq!(json["format"].as_str(), Some("plain"));
                // Plain format should not modify text
                assert_eq!(
                    json["original"].as_str(),
                    json["replaced"].as_str(),
                    "Plain format should not modify text"
                );
            }
            Err(e) => {
                eprintln!("Replace plain test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_replace_default_format_is_markdown() {
        let result = run_cli_json(&["replace", "test text"]);

        match result {
            Ok(json) => {
                // Skip test if KG not configured (returns error JSON)
                if json.get("error").is_some() {
                    eprintln!(
                        "Replace default format test skipped: Knowledge graph not configured"
                    );
                    return;
                }
                assert_eq!(
                    json["format"].as_str(),
                    Some("markdown"),
                    "Default format should be markdown"
                );
            }
            Err(e) => {
                eprintln!("Replace default format test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_replace_preserves_unmatched_text() {
        let result = run_cli_json(&[
            "replace",
            "some random text without matches xyz123",
            "--format",
            "markdown",
        ]);

        match result {
            Ok(json) => {
                let _original = json["original"].as_str().unwrap();
                let replaced = json["replaced"].as_str().unwrap();
                // Text without matches should be preserved
                assert!(replaced.contains("xyz123") || replaced.contains("random"));
            }
            Err(e) => {
                eprintln!("Replace preserves text test skipped: {}", e);
            }
        }
    }
}

#[cfg(test)]
mod find_tests {
    use super::*;

    #[test]
    #[serial]
    fn test_find_basic() {
        let result = run_cli_json(&["find", "rust async tokio"]);

        match result {
            Ok(json) => {
                // Skip test if KG not configured (returns error JSON)
                if json.get("error").is_some() {
                    eprintln!("Find basic test skipped: Knowledge graph not configured");
                    return;
                }
                assert_eq!(json["text"].as_str(), Some("rust async tokio"));
                assert!(json.get("matches").is_some());
                assert!(json.get("count").is_some());
            }
            Err(e) => {
                eprintln!("Find basic test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_find_returns_array_of_matches() {
        let result = run_cli_json(&["find", "api server client"]);

        match result {
            Ok(json) => {
                // Skip test if KG not configured (returns error JSON)
                if json.get("error").is_some() {
                    eprintln!("Find matches array test skipped: Knowledge graph not configured");
                    return;
                }
                assert!(json["matches"].is_array(), "Matches should be an array");
            }
            Err(e) => {
                eprintln!("Find matches array test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_find_matches_have_required_fields() {
        let result = run_cli_json(&["find", "database json config"]);

        match result {
            Ok(json) => {
                if let Some(matches) = json["matches"].as_array() {
                    for m in matches {
                        assert!(m.get("term").is_some(), "Match should have term");
                        assert!(
                            m.get("normalized").is_some(),
                            "Match should have normalized"
                        );
                    }
                }
            }
            Err(e) => {
                eprintln!("Find matches fields test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_find_count_matches_array_length() {
        let result = run_cli_json(&["find", "linux docker kubernetes"]);

        match result {
            Ok(json) => {
                let count = json["count"].as_u64().unwrap_or(0) as usize;
                let matches_len = json["matches"].as_array().map(|a| a.len()).unwrap_or(0);
                assert_eq!(count, matches_len, "Count should match array length");
            }
            Err(e) => {
                eprintln!("Find count test skipped: {}", e);
            }
        }
    }
}

#[cfg(test)]
mod thesaurus_tests {
    use super::*;

    #[test]
    #[serial]
    fn test_thesaurus_basic() {
        let result = run_cli_json(&["thesaurus"]);

        match result {
            Ok(json) => {
                // Skip test if KG not configured (returns error JSON)
                if json.get("error").is_some() {
                    eprintln!("Thesaurus basic test skipped: Knowledge graph not configured");
                    return;
                }
                assert!(json.get("role").is_some());
                assert!(json.get("name").is_some());
                assert!(json.get("terms").is_some());
                assert!(json.get("total_count").is_some());
                assert!(json.get("shown_count").is_some());
            }
            Err(e) => {
                eprintln!("Thesaurus basic test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_thesaurus_with_limit() {
        let result = run_cli_json(&["thesaurus", "--limit", "5"]);

        match result {
            Ok(json) => {
                // Skip test if KG not configured (returns error JSON)
                if json.get("error").is_some() {
                    eprintln!("Thesaurus limit test skipped: Knowledge graph not configured");
                    return;
                }
                let shown = json["shown_count"].as_u64().unwrap_or(0);
                assert!(shown <= 5, "Should respect limit");

                let terms = json["terms"].as_array().unwrap();
                assert!(terms.len() <= 5, "Terms array should respect limit");
            }
            Err(e) => {
                eprintln!("Thesaurus limit test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_thesaurus_terms_have_required_fields() {
        let result = run_cli_json(&["thesaurus", "--limit", "10"]);

        match result {
            Ok(json) => {
                if let Some(terms) = json["terms"].as_array() {
                    for term in terms {
                        assert!(term.get("id").is_some(), "Term should have id");
                        assert!(term.get("term").is_some(), "Term should have term");
                        assert!(
                            term.get("normalized").is_some(),
                            "Term should have normalized"
                        );
                    }
                }
            }
            Err(e) => {
                eprintln!("Thesaurus terms fields test skipped: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_thesaurus_total_count_greater_or_equal_shown() {
        let result = run_cli_json(&["thesaurus", "--limit", "5"]);

        match result {
            Ok(json) => {
                let total = json["total_count"].as_u64().unwrap_or(0);
                let shown = json["shown_count"].as_u64().unwrap_or(0);
                assert!(total >= shown, "Total count should be >= shown count");
            }
            Err(e) => {
                eprintln!("Thesaurus count test skipped: {}", e);
            }
        }
    }
}

#[cfg(test)]
mod output_format_tests {
    use super::*;

    #[test]
    #[serial]
    fn test_json_output() {
        let output = cli_command()
            .args(["--format", "json", "roles"])
            .output()
            .expect("Failed to execute");

        let stdout = String::from_utf8_lossy(&output.stdout);
        let trimmed = stdout.trim();

        // Output should either be valid JSON or contain an error
        if !trimmed.is_empty() {
            let is_json = (trimmed.starts_with('[') && trimmed.ends_with(']'))
                || (trimmed.starts_with('{') && trimmed.ends_with('}'));
            let has_error = trimmed.contains("error") || trimmed.contains("Error");

            assert!(
                is_json || has_error || output.status.success(),
                "Output should be JSON or contain error: {}",
                trimmed
            );
        }
    }

    #[test]
    #[serial]
    fn test_json_pretty_output() {
        let output = cli_command()
            .args(["--format", "json-pretty", "config"])
            .output()
            .expect("Failed to execute");

        if output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            // Pretty JSON has multiple lines
            let lines: Vec<&str> = stdout.lines().collect();
            assert!(lines.len() > 1, "Pretty JSON should have multiple lines");
        }
    }

    #[test]
    #[serial]
    fn test_text_output() {
        let output = cli_command()
            .args(["--format", "text", "config"])
            .output()
            .expect("Failed to execute");

        let stdout = String::from_utf8_lossy(&output.stdout);
        // Text output should not be empty
        assert!(!stdout.trim().is_empty() || !output.status.success());
    }
}

#[cfg(test)]
mod evaluate_tests {
    use super::*;
    use tempfile::TempDir;

    fn create_temp_ground_truth() -> TempDir {
        let dir = TempDir::new().expect("Failed to create temp dir");
        let ground_truth_path = dir.path().join("ground_truth.json");
        let json = serde_json::json!([
            {
                "id": "doc1",
                "text": "I love rust and async programming",
                "expected_terms": [
                    {"term": "rust", "category": null},
                    {"term": "async", "category": null}
                ]
            },
            {
                "id": "doc2",
                "text": "Python is great for data science",
                "expected_terms": [
                    {"term": "python", "category": null},
                    {"term": "data science", "category": null}
                ]
            }
        ]);
        std::fs::write(
            &ground_truth_path,
            serde_json::to_string_pretty(&json).unwrap(),
        )
        .unwrap();
        dir
    }

    fn create_temp_thesaurus() -> TempDir {
        let dir = TempDir::new().expect("Failed to create temp dir");
        let thesaurus_path = dir.path().join("thesaurus.json");
        let json = serde_json::json!({
            "name": "test",
            "data": {
                "rust": {"id": 1, "nterm": "rust"},
                "async": {"id": 2, "nterm": "async"},
                "python": {"id": 3, "nterm": "python"}
            }
        });
        std::fs::write(
            &thesaurus_path,
            serde_json::to_string_pretty(&json).unwrap(),
        )
        .unwrap();
        dir
    }

    #[test]
    #[serial]
    fn test_evaluate_command_success() {
        let gt_dir = create_temp_ground_truth();
        let th_dir = create_temp_thesaurus();

        let gt_path = gt_dir.path().join("ground_truth.json");
        let th_path = th_dir.path().join("thesaurus.json");

        let result = run_cli_json(&[
            "evaluate",
            "--ground-truth",
            gt_path.to_str().unwrap(),
            "--thesaurus",
            th_path.to_str().unwrap(),
        ]);

        match result {
            Ok(json) => {
                // Verify expected structure
                assert!(
                    json.get("total_documents").is_some(),
                    "Should have total_documents"
                );
                assert!(json.get("overall").is_some(), "Should have overall metrics");
                assert!(json.get("per_term").is_some(), "Should have per_term array");
                // Overall should have precision, recall, f1
                let overall = &json["overall"];
                assert!(
                    overall.get("precision").is_some(),
                    "Overall should have precision"
                );
                assert!(
                    overall.get("recall").is_some(),
                    "Overall should have recall"
                );
                assert!(overall.get("f1").is_some(), "Overall should have f1");
            }
            Err(e) => {
                panic!("Evaluate command failed: {}", e);
            }
        }
    }

    #[test]
    #[serial]
    fn test_evaluate_command_missing_ground_truth() {
        let th_dir = create_temp_thesaurus();
        let th_path = th_dir.path().join("thesaurus.json");

        let output = cli_command()
            .args([
                "evaluate",
                "--ground-truth",
                "/nonexistent/path.json",
                "--thesaurus",
                th_path.to_str().unwrap(),
            ])
            .output()
            .expect("Failed to execute");

        // Should fail with non-zero exit code
        assert!(
            !output.status.success(),
            "Should fail with missing ground truth file"
        );
    }

    #[test]
    #[serial]
    fn test_evaluate_command_missing_thesaurus() {
        let gt_dir = create_temp_ground_truth();
        let gt_path = gt_dir.path().join("ground_truth.json");

        let output = cli_command()
            .args([
                "evaluate",
                "--ground-truth",
                gt_path.to_str().unwrap(),
                "--thesaurus",
                "/nonexistent/path.json",
            ])
            .output()
            .expect("Failed to execute");

        // Should fail with non-zero exit code
        assert!(
            !output.status.success(),
            "Should fail with missing thesaurus file"
        );
    }

    #[test]
    #[serial]
    fn test_evaluate_output_contains_expected_fields() {
        let gt_dir = create_temp_ground_truth();
        let th_dir = create_temp_thesaurus();

        let gt_path = gt_dir.path().join("ground_truth.json");
        let th_path = th_dir.path().join("thesaurus.json");

        let result = run_cli_json(&[
            "evaluate",
            "--ground-truth",
            gt_path.to_str().unwrap(),
            "--thesaurus",
            th_path.to_str().unwrap(),
        ]);

        match result {
            Ok(json) => {
                // Check overall metrics structure
                let overall = &json["overall"];
                assert!(overall.get("true_positives").is_some());
                assert!(overall.get("false_positives").is_some());
                assert!(overall.get("false_negatives").is_some());

                // Check systematic_errors field exists (may be empty)
                assert!(json.get("systematic_errors").is_some());
            }
            Err(e) => {
                panic!("Evaluate command failed: {}", e);
            }
        }
    }
}