Skip to main content

kjarni_cli/
lib.rs

1use clap::{Parser, Subcommand};
2
3/// Default model for `kjarni chat`.
4///
5/// Must be a CLI name that `ModelType::from_cli_name` resolves, or the bare
6/// `kjarni chat` command fails for everyone. `tests::default_models_exist` checks
7/// this and [`DEFAULT_GENERATE_MODEL`] against the registry.
8pub const DEFAULT_CHAT_MODEL: &str = "llama3.2-3b-instruct";
9
10/// Default model for `kjarni generate`.
11pub const DEFAULT_GENERATE_MODEL: &str = "llama3.2-1b-instruct";
12
13#[derive(Parser)]
14#[command(name = "kjarni")]
15#[command(
16    about = "Run transformer models on your own machine. Same output as PyTorch.",
17    long_about = None
18)]
19#[command(version)]
20pub struct Cli {
21    #[command(subcommand)]
22    pub command: Commands,
23
24    /// Verbosity level (-v, -vv, -vvv)
25    #[arg(short, long, action = clap::ArgAction::Count, global = true)]
26    pub verbose: u8,
27}
28
29#[derive(Subcommand, Debug, PartialEq)]
30pub enum Commands {
31    /// Manage models (list, download, info)
32    Model {
33        #[command(subcommand)]
34        action: ModelCommands,
35    },
36
37    /// Generate text from a prompt
38    Generate {
39        /// The prompt (or file path, or stdin if not provided)
40        prompt: Option<String>,
41
42        #[arg(short, long, default_value = DEFAULT_GENERATE_MODEL)]
43        model: String,
44
45        /// Load weights from a local file or directory instead of the registry
46        #[arg(long)]
47        model_path: Option<String>,
48
49        /// Maximum tokens to generate
50        #[arg(short = 'n', long, default_value_t = 100)]
51        max_tokens: usize,
52
53        /// Sampling temperature (0.0 = greedy, higher = more random)
54        #[arg(short, long, default_value_t = 0.7)]
55        temperature: f32,
56
57        /// Top-K sampling (limits to K most likely tokens)
58        #[arg(long)]
59        top_k: Option<usize>,
60
61        /// Top-P (nucleus) sampling threshold
62        #[arg(long)]
63        top_p: Option<f32>,
64
65        /// Min-P sampling threshold  
66        #[arg(long)]
67        min_p: Option<f32>,
68
69        /// Repetition penalty (1.0 = no penalty)
70        #[arg(long, default_value_t = 1.1)]
71        repetition_penalty: f32,
72
73        /// Use greedy decoding (ignores temperature)
74        #[arg(long)]
75        greedy: bool,
76
77        /// Use GPU
78        #[arg(long)]
79        gpu: bool,
80
81        /// Disable streaming output
82        #[arg(long)]
83        no_stream: bool,
84
85        /// Suppress status messages
86        #[arg(short, long)]
87        quiet: bool,
88
89        /// Prefer quantized GGUF weights when the model has them
90        ///
91        /// Q4_K_M is a fraction of the size and markedly faster to decode:
92        /// measured on an RTX A2000, llama3.2-3b runs 24.5 tok/s from GGUF
93        /// against 8.7 from full-precision safetensors. Falls back with a notice
94        /// when the model has no GGUF published.
95        #[arg(long)]
96        gguf: bool,
97
98        /// Draft model for speculative decoding. Must share the target's
99        /// vocabulary: a small model of the same family, such as
100        /// qwen2.5-0.5b-instruct drafting for qwen2.5-1.5b.
101        #[arg(long)]
102        draft: Option<String>,
103
104        /// Tokens the draft proposes per round. Higher trades wasted draft work
105        /// against fewer passes over the target's weights.
106        #[arg(long, default_value_t = 4)]
107        draft_tokens: usize,
108
109        /// Speculate from earlier in the text instead of with a draft model, matching
110        /// up to this many tokens. Helps where the output repeats the input.
111        #[arg(long, value_name = "NGRAM")]
112        prompt_lookup: Option<usize>,
113    },
114
115    /// Summarize text
116    Summarize {
117        /// Input text (or read from stdin)
118        #[arg(short, long)]
119        input: Option<String>,
120
121        /// Model to use
122        #[arg(short, long, default_value = "distilbart-cnn")]
123        model: String,
124
125        /// Load a BART or T5 model from a Hugging Face folder instead of the registry
126        #[arg(long, value_name = "DIR")]
127        model_path: Option<String>,
128
129        /// Minimum summary length
130        #[arg(long)]
131        min_length: Option<usize>,
132
133        /// Maximum summary length
134        #[arg(long)]
135        max_length: Option<usize>,
136
137        /// Number of beams for beam search
138        #[arg(long)]
139        num_beams: Option<usize>,
140
141        /// Length penalty for beam search (< 1 shorter, > 1 longer)
142        #[arg(long)]
143        length_penalty: Option<f32>,
144
145        /// Block repeated n-grams of this size
146        #[arg(long)]
147        no_repeat_ngram: Option<usize>,
148
149        /// Use greedy decoding (deterministic, fastest)
150        #[arg(long)]
151        greedy: bool,
152
153        /// Disable streaming output
154        #[arg(long)]
155        no_stream: bool,
156
157        /// Use GPU
158        #[arg(long)]
159        gpu: bool,
160
161        /// Suppress progress messages
162        #[arg(short, long)]
163        quiet: bool,
164    },
165
166    /// Translate text between languages
167    Translate {
168        /// Input text (or read from stdin)
169        #[arg(short, long)]
170        input: Option<String>,
171
172        /// Model to use. By default opus-mt-en-de or opus-mt-de-en for those two
173        /// directions, and flan-t5-base, which is much less reliable, for the rest
174        #[arg(short, long)]
175        model: Option<String>,
176
177        /// Load a Marian (opus-mt) or T5 model from a Hugging Face folder instead
178        /// of the registry
179        #[arg(long, value_name = "DIR")]
180        model_path: Option<String>,
181
182        /// Source language (e.g., en, de, fr)
183        #[arg(long)]
184        src: Option<String>,
185
186        /// Target language (e.g., en, de, fr)
187        #[arg(long)]
188        dst: Option<String>,
189
190        /// Maximum output length
191        #[arg(long)]
192        max_length: Option<usize>,
193
194        /// Number of beams for beam search
195        #[arg(long)]
196        num_beams: Option<usize>,
197
198        /// Length penalty for beam search (< 1 shorter, > 1 longer)
199        #[arg(long)]
200        length_penalty: Option<f32>,
201
202        /// Block repeated n-grams of this size
203        #[arg(long)]
204        no_repeat_ngram: Option<usize>,
205
206        /// Use greedy decoding (deterministic, fastest)
207        #[arg(long)]
208        greedy: bool,
209
210        /// Disable streaming output
211        #[arg(long)]
212        no_stream: bool,
213
214        /// Use GPU
215        #[arg(long)]
216        gpu: bool,
217
218        /// Suppress progress messages
219        #[arg(short, long)]
220        quiet: bool,
221    },
222
223    /// Show a model's metadata, config and tensor layout
224    Inspect {
225        /// Path to a .gguf, a .safetensors directory, or a name in ~/.cache/kjarni
226        path: String,
227    },
228
229    /// Generate embeddings for text
230    Embed {
231        /// Input text, file path, or stdin if not provided
232        input: Option<String>,
233
234        /// Embedding model
235        #[arg(short, long, default_value = "minilm-l6-v2")]
236        model: String,
237
238        /// Load weights from a local file or directory instead of the registry
239        #[arg(long)]
240        model_path: Option<String>,
241
242        /// Output format: raw, json
243        #[arg(long, default_value = "raw")]
244        format: String,
245
246        /// Keep the raw pooled vector instead of scaling it to unit length
247        #[arg(long)]
248        no_normalize: bool,
249
250        /// Scale each vector to unit length, which is the default
251        #[arg(long, hide = true, conflicts_with = "no_normalize")]
252        normalize: bool,
253
254        /// Pooling strategy: mean, cls, max
255        ///
256        /// Mean matches what sentence-transformers does for these models, and what the
257        /// library bindings and presets already default to. Pooling changes the vector
258        /// itself, so a different choice here does not produce a slightly different
259        /// answer, it produces one that cannot be compared against the others.
260        #[arg(long, default_value = "mean")]
261        pooling: String,
262
263        /// Run on the GPU
264        #[arg(long)]
265        gpu: bool,
266
267        /// Suppress status messages
268        #[arg(short, long)]
269        quiet: bool,
270    },
271
272    /// Serve Kjarni's tools to AI agents over MCP (stdio): folder search, photo
273    /// search, transcription, classification, reranking and similarity
274    Mcp,
275
276    /// Serve Kjarni over HTTP with an OpenAI-compatible API: chat, completions,
277    /// embeddings and speech-to-text, plus reranking, classification,
278    /// translation, summarization and entity recognition.
279    ///
280    /// Any OpenAI client works by pointing its base URL at
281    /// http://127.0.0.1:8080/v1. A request picks a model by its Kjarni name,
282    /// such as "qwen2.5-1.5b-instruct", or gets the default set by the flags
283    /// below. Each model loads on its first request and stays loaded.
284    Serve {
285        /// Address to listen on. Use 0.0.0.0 to accept other machines, and set
286        /// an API key when you do
287        #[arg(long, default_value = "127.0.0.1")]
288        host: String,
289
290        /// Port to listen on
291        #[arg(short, long, default_value_t = 8080)]
292        port: u16,
293
294        /// Default model for /v1/chat/completions and /v1/completions
295        #[arg(long, value_name = "MODEL")]
296        chat: Option<String>,
297
298        /// Default model for /v1/embeddings
299        #[arg(long, value_name = "MODEL")]
300        embed: Option<String>,
301
302        /// Default model for /v1/classify without labels
303        #[arg(long, value_name = "MODEL")]
304        classify: Option<String>,
305
306        /// Default model for /v1/classify with labels (zero-shot)
307        #[arg(long, value_name = "MODEL")]
308        zero_shot: Option<String>,
309
310        /// Default model for /v1/rerank
311        #[arg(long, value_name = "MODEL")]
312        rerank: Option<String>,
313
314        /// Default model for /v1/audio/transcriptions and /v1/audio/translations
315        #[arg(long, value_name = "MODEL")]
316        transcribe: Option<String>,
317
318        /// Default model for /v1/translate. Without one, a model is picked for
319        /// each language pair
320        #[arg(long, value_name = "MODEL")]
321        translate: Option<String>,
322
323        /// Default model for /v1/summarize
324        #[arg(long, value_name = "MODEL")]
325        summarize: Option<String>,
326
327        /// Default model for /v1/ner
328        #[arg(long, value_name = "MODEL")]
329        ner: Option<String>,
330
331        /// Load the models named by the flags above before taking requests,
332        /// instead of on their first request
333        #[arg(long)]
334        preload: bool,
335
336        /// Require 'Authorization: Bearer KEY' on every /v1 request. Also read
337        /// from the KJARNI_API_KEY environment variable
338        #[arg(long, value_name = "KEY")]
339        api_key: Option<String>,
340
341        /// Allow requests from web pages on other origins (CORS)
342        #[arg(long)]
343        cors: bool,
344
345        /// Reuse the work on a conversation's earlier turns between chat
346        /// requests. Faster multi-turn chat, more memory
347        #[arg(long)]
348        prefix_cache: bool,
349
350        /// Run models on the GPU
351        #[arg(long)]
352        gpu: bool,
353
354        /// Don't log requests or startup messages
355        #[arg(short, long)]
356        quiet: bool,
357    },
358
359    /// Transcribe audio to text
360    Transcribe {
361        /// Path to audio file (wav, mp3, flac, ogg)
362        file: String,
363
364        /// Model to use (whisper-small, whisper-large-v3)
365        #[arg(short, long, default_value = "whisper-small")]
366        model: String,
367
368        /// Load a Whisper model from a Hugging Face folder instead of the registry
369        #[arg(long, value_name = "DIR")]
370        model_path: Option<String>,
371
372        /// Language code (e.g., en, fr, de). Omit for auto-detect.
373        #[arg(short, long)]
374        language: Option<String>,
375
376        /// Translate to English instead of transcribing
377        #[arg(long)]
378        translate: bool,
379
380        /// Print each segment with its start and end time. Waits for the whole
381        /// transcript rather than streaming it.
382        #[arg(short, long)]
383        timestamps: bool,
384
385        /// Maximum tokens per 30-second chunk
386        #[arg(long)]
387        max_tokens: Option<usize>,
388
389        /// Disable streaming (wait for full result)
390        #[arg(long)]
391        no_stream: bool,
392
393        /// Use GPU acceleration
394        #[arg(long)]
395        gpu: bool,
396
397        /// Suppress progress output
398        #[arg(short, long)]
399        quiet: bool,
400    },
401
402    /// Classify text using a classification model
403    Classify {
404        /// Input text(s) to classify. Use - for stdin.
405        input: Vec<String>,
406
407        /// Model name from registry
408        #[arg(short, long, default_value = "distilbert-sentiment")]
409        model: String,
410
411        /// Load model from local path instead of registry
412        #[arg(long, value_name = "PATH")]
413        model_path: Option<String>,
414
415        /// Comma-separated labels. With a zero-shot model such as
416        /// minilm-zeroshot, the labels to choose from:
417        /// --labels "sizing,shipping,price". With other models, names for
418        /// the model's own labels, in the order of its output:
419        /// --labels "neikvætt,jákvætt"
420        #[arg(long, value_name = "LABELS")]
421        labels: Option<String>,
422
423        /// Zero-shot models only: the hypothesis each label is put into,
424        /// with {} where the label goes [default: "This example is {}."]
425        #[arg(long, value_name = "TEMPLATE")]
426        hypothesis: Option<String>,
427
428        /// Return top K predictions
429        #[arg(long, default_value = "5")]
430        top_k: usize,
431
432        /// Minimum confidence threshold (0.0-1.0)
433        #[arg(long)]
434        threshold: Option<f32>,
435
436        /// Maximum sequence length (truncates longer inputs)
437        #[arg(long)]
438        max_length: Option<usize>,
439
440        /// Batch size for inference
441        #[arg(long)]
442        batch_size: Option<usize>,
443
444        /// Use multi-label classification (sigmoid instead of softmax). With
445        /// a zero-shot model, judge each label on its own.
446        #[arg(long)]
447        multi_label: bool,
448
449        /// Output format: json, jsonl, text
450        #[arg(short, long, default_value = "text")]
451        format: String,
452
453        /// Run on GPU
454        #[arg(long)]
455        gpu: bool,
456
457        /// Model precision: f32, f16, bf16
458        #[arg(long)]
459        dtype: Option<String>,
460
461        /// Suppress progress output
462        #[arg(short, long)]
463        quiet: bool,
464    },
465
466    /// Find names of people, places, organizations and other entities
467    ///
468    /// Several texts go one per line on stdin. --model-path takes any
469    /// *ForTokenClassification model from the Hugging Face Hub, such as an
470    /// Icelandic or a medical one.
471    Ner {
472        /// Text to read. Words without quotes are one text. Reads stdin if empty.
473        input: Vec<String>,
474
475        /// Model name from registry
476        #[arg(short, long, default_value = "distilbert-ner")]
477        model: String,
478
479        /// Load model from local path instead of registry
480        #[arg(long, value_name = "PATH")]
481        model_path: Option<String>,
482
483        /// How tokens become entities, as in transformers: first (keeps words
484        /// whole), simple, max or average
485        #[arg(long, default_value = "first")]
486        aggregation: String,
487
488        /// Output format: text, json, jsonl
489        #[arg(short, long, default_value = "text")]
490        format: String,
491
492        /// Run on GPU
493        #[arg(long)]
494        gpu: bool,
495
496        /// Suppress progress output
497        #[arg(short, long)]
498        quiet: bool,
499    },
500
501    /// Rerank documents by relevance to a query
502    Rerank {
503        /// The query to rank against
504        query: String,
505
506        /// Documents to rerank (or read from stdin, one per line)
507        documents: Vec<String>,
508
509        #[arg(short, long, default_value = "minilm-l6-v2-cross-encoder")]
510        model: String,
511
512        /// Load a cross-encoder from a Hugging Face folder instead of the registry
513        #[arg(long, value_name = "DIR")]
514        model_path: Option<String>,
515
516        /// Return only top K results
517        #[arg(short = 'k', long)]
518        top_k: Option<usize>,
519
520        /// Output format: json, jsonl, text, docs
521        #[arg(short, long, default_value = "text")]
522        format: String,
523
524        /// Run on the GPU
525        #[arg(long)]
526        gpu: bool,
527
528        /// Suppress progress output
529        #[arg(short, long)]
530        quiet: bool,
531    },
532
533    /// Interactive chat mode
534    Chat {
535        #[arg(short, long, default_value = DEFAULT_CHAT_MODEL)]
536        model: String,
537
538        /// Load weights from a local file or directory instead of the registry
539        #[arg(long)]
540        model_path: Option<String>,
541
542        /// System prompt to set assistant behavior
543        #[arg(short, long)]
544        system: Option<String>,
545
546        /// Sampling temperature
547        #[arg(short, long, default_value_t = 0.7)]
548        temperature: f32,
549
550        /// Max tokens per response
551        #[arg(short = 'n', long, default_value_t = 512)]
552        max_tokens: usize,
553
554        /// Run on the GPU
555        #[arg(long)]
556        gpu: bool,
557
558        /// Suppress progress output
559        #[arg(short, long)]
560        quiet: bool,
561
562        /// Prefer quantized GGUF weights when the model has them
563        ///
564        /// Q4_K_M is a fraction of the size and markedly faster to decode:
565        /// measured on an RTX A2000, llama3.2-3b runs 24.5 tok/s from GGUF
566        /// against 8.7 from full-precision safetensors. Falls back with a notice
567        /// when the model has no GGUF published.
568        #[arg(long)]
569        gguf: bool,
570
571        /// Draft model for speculative decoding. Must share the target's
572        /// vocabulary, such as qwen2.5-0.5b-instruct drafting for qwen2.5-1.5b.
573        #[arg(long)]
574        draft: Option<String>,
575
576        /// Tokens the draft proposes per round.
577        #[arg(long, default_value_t = 4)]
578        draft_tokens: usize,
579    },
580
581    /// Create or manage search indexes
582    Index {
583        #[command(subcommand)]
584        action: IndexCommands,
585    },
586
587    /// Index and search images by description
588    #[cfg(feature = "image-io")]
589    Image {
590        #[command(subcommand)]
591        action: ImageCommands,
592    },
593
594    /// Search an index
595    Search {
596        /// Path to the index file
597        index_path: String,
598
599        /// Search query
600        query: String,
601
602        /// Number of results to return
603        #[arg(short = 'k', long, default_value_t = 10)]
604        top_k: usize,
605
606        /// Search mode: hybrid, semantic, keyword
607        #[arg(long, default_value = "hybrid")]
608        mode: String,
609
610        /// Encoder model for semantic search. Defaults to the model the index was
611        /// built with.
612        #[arg(short, long)]
613        model: Option<String>,
614
615        /// Reranking model (optional)
616        /// Use a cross-encoder model to rerank initial results
617        /// Example: --rerank-model "ms-marco-minilm"
618        #[arg(long, default_value = None)]
619        rerank_model: Option<String>,
620
621        /// Output format: json, jsonl, text
622        #[arg(short, long, default_value = "text")]
623        format: String,
624
625        /// Run on the GPU
626        #[arg(long)]
627        gpu: bool,
628
629        /// Suppress progress output
630        #[arg(short, long)]
631        quiet: bool,
632    },
633
634    /// Compute similarity between two texts
635    Similarity {
636        /// First text (or file path)
637        text1: String,
638
639        /// Second text (or file path)
640        text2: String,
641
642        /// Encoder model
643        #[arg(short, long, default_value = "minilm-l6-v2")]
644        model: String,
645
646        /// Run on the GPU
647        #[arg(long)]
648        gpu: bool,
649
650        /// Suppress progress output
651        #[arg(short, long)]
652        quiet: bool,
653    },
654}
655
656#[derive(Subcommand, Debug, PartialEq)]
657pub enum ModelCommands {
658    /// List all available models
659    List {
660        /// Filter by architecture (e.g., llama, bert, t5)
661        #[arg(short, long)]
662        arch: Option<String>,
663
664        /// Filter by task (e.g., chat, embedding, classification, summarization)
665        #[arg(short, long)]
666        task: Option<String>,
667
668        /// Show only downloaded models
669        #[arg(short, long)]
670        downloaded: bool,
671    },
672
673    /// Download a model
674    Download {
675        name: String,
676
677        #[arg(long)]
678        gguf: bool,
679
680        #[arg(short, long)]
681        quiet: bool,
682    },
683
684    /// Remove a downloaded model
685    Remove { name: String },
686
687    /// Show detailed info about a model
688    Info { name: String },
689
690    /// Search for models by name or description
691    Search { query: String },
692}
693
694#[derive(Subcommand, Debug, PartialEq)]
695pub enum InspectCommands {
696    /// Inspect a model file
697    Model {
698        /// Path to the model file
699        path: String,
700    },
701}
702
703/// Image search, backed by CLIP.
704///
705/// Separate from `index`/`search` because the vectors are not interchangeable:
706/// a CLIP image vector and a MiniLM text vector live in different spaces, and
707/// comparing them would return confident nonsense.
708#[cfg(feature = "image-io")]
709#[derive(Subcommand, Debug, PartialEq)]
710pub enum ImageCommands {
711    /// Embed images into a searchable index
712    Index {
713        /// Files or directories of images
714        #[arg(required = true)]
715        inputs: Vec<String>,
716
717        /// Where to write the index
718        #[arg(short, long, default_value = "images.json")]
719        output: String,
720
721        /// Suppress progress output
722        #[arg(short, long)]
723        quiet: bool,
724    },
725
726    /// Find images matching a description
727    Search {
728        /// Path to the image index
729        index_path: String,
730
731        /// What to look for, in words
732        query: String,
733
734        /// Number of results
735        #[arg(short = 'k', long, default_value_t = 5)]
736        top_k: usize,
737
738        /// Suppress progress output
739        #[arg(short, long)]
740        quiet: bool,
741    },
742}
743
744#[derive(Subcommand, Debug, PartialEq)]
745pub enum IndexCommands {
746    /// Create a new index from documents
747    Create {
748        /// Output index file path
749        output: String,
750
751        /// Input files/directories (uses built-in chunking)
752        #[arg(conflicts_with = "from_chunks")]
753        inputs: Vec<String>,
754
755        /// Pre-chunked JSONL file (bypass chunking)
756        #[arg(long, conflicts_with = "inputs")]
757        from_chunks: Option<String>,
758
759        /// Chunk size in characters
760        ///
761        /// The default fits inside the encoder's window. minilm-l6-v2 reads 256
762        /// tokens, roughly 900 characters, so at 1000 about nine chunks in ten
763        /// would be longer than the model reads, with the tail dropped silently.
764        /// The C# Indexer uses 512 too.
765        #[arg(long, default_value_t = 512)]
766        chunk_size: usize,
767
768        /// Chunk overlap in characters
769        #[arg(long, default_value_t = 100)]
770        chunk_overlap: usize,
771
772        /// Encoder model for embeddings
773        #[arg(short, long, default_value = "minilm-l6-v2")]
774        model: String,
775
776        /// Run on the GPU
777        #[arg(long)]
778        gpu: bool,
779
780        /// Suppress progress output
781        #[arg(short, long)]
782        quiet: bool,
783    },
784
785    /// Add files to an existing index, replacing any it already holds
786    Add {
787        /// Index file path
788        index_path: String,
789
790        /// Input files or directories to add
791        inputs: Vec<String>,
792
793        /// Chunk size in characters
794        ///
795        /// Matches `index create`; see the note there.
796        #[arg(long, default_value_t = 512)]
797        chunk_size: usize,
798
799        #[arg(long, default_value_t = 100)]
800        chunk_overlap: usize,
801
802        /// Encoder model. Defaults to the model the index was built with.
803        #[arg(short, long)]
804        model: Option<String>,
805
806        /// Run on the GPU
807        #[arg(long)]
808        gpu: bool,
809
810        /// Suppress progress output
811        #[arg(short, long)]
812        quiet: bool,
813    },
814
815    /// Remove files or folders from an index
816    Remove {
817        /// Index file path
818        index_path: String,
819
820        /// Files or folders to remove, which need not still exist
821        #[arg(required = true)]
822        paths: Vec<String>,
823
824        /// Suppress progress output
825        #[arg(short, long)]
826        quiet: bool,
827    },
828
829    /// Show index info
830    Info {
831        /// Index file path
832        index_path: String,
833    },
834}
835
836/// Convert verbosity count to log level string
837pub fn verbosity_to_log_level(verbose: u8) -> &'static str {
838    match verbose {
839        0 => "warn",
840        1 => "info",
841        2 => "debug",
842        _ => "trace",
843    }
844}
845
846#[cfg(test)]
847mod tests {
848    use super::*;
849    use clap::Parser;
850
851    /// Every model name hardcoded outside the registry must actually resolve.
852    ///
853    /// This exists because three separate lists had drifted from the registry at
854    /// once: both CLI defaults, and four of the seven entries in
855    /// `kjarni::chat::suggest_chat_models`. Each was presumably correct when
856    /// written and went stale when models gained an `-instruct` suffix, with
857    /// nothing to notice. A name that does not resolve is a user-facing failure
858    /// on the very first command someone types, so it should break the build
859    /// instead.
860    #[test]
861    fn hardcoded_model_names_resolve() {
862        use kjarni::ModelType;
863
864        let mut bad = Vec::new();
865
866        for name in [DEFAULT_CHAT_MODEL, DEFAULT_GENERATE_MODEL] {
867            if ModelType::from_cli_name(name).is_none() {
868                bad.push(format!("CLI default '{name}'"));
869            }
870        }
871
872        for name in kjarni::chat::suggested_models() {
873            if ModelType::from_cli_name(name).is_none() {
874                bad.push(format!("chat suggestion '{name}'"));
875            }
876        }
877
878        assert!(
879            bad.is_empty(),
880            "these names are not in the model registry, so anyone who uses them \
881             gets an error:\n  {}\n\
882             Run `kjarni model list` for the valid names.",
883            bad.join("\n  ")
884        );
885    }
886
887    /// The chat default must be usable for chat, not merely a real model.
888    ///
889    /// `kjarni chat` with no arguments should work; defaulting to an embedding
890    /// model would resolve fine here and still fail at runtime.
891    #[test]
892    fn default_chat_model_is_a_chat_model() {
893        assert!(
894            kjarni::chat::is_chat_model(DEFAULT_CHAT_MODEL).is_ok(),
895            "DEFAULT_CHAT_MODEL '{DEFAULT_CHAT_MODEL}' cannot be used for chat"
896        );
897    }
898    fn parse_args(args: &[&str]) -> Result<Cli, clap::Error> {
899        let mut full_args = vec!["kjarni"];
900        full_args.extend(args);
901        Cli::try_parse_from(full_args)
902    }
903
904    #[test]
905    fn test_verbosity_to_log_level_zero() {
906        assert_eq!(verbosity_to_log_level(0), "warn");
907    }
908
909    #[test]
910    fn test_verbosity_to_log_level_one() {
911        assert_eq!(verbosity_to_log_level(1), "info");
912    }
913
914    #[test]
915    fn test_verbosity_to_log_level_two() {
916        assert_eq!(verbosity_to_log_level(2), "debug");
917    }
918
919    #[test]
920    fn test_verbosity_to_log_level_three() {
921        assert_eq!(verbosity_to_log_level(3), "trace");
922    }
923
924    #[test]
925    fn test_verbosity_to_log_level_high() {
926        assert_eq!(verbosity_to_log_level(10), "trace");
927        assert_eq!(verbosity_to_log_level(255), "trace");
928    }
929
930    #[test]
931    fn test_generate_minimal() {
932        let cli = parse_args(&["generate"]).unwrap();
933
934        match cli.command {
935            Commands::Generate {
936                prompt,
937                model,
938                max_tokens,
939                temperature,
940                greedy,
941                gpu,
942                quiet,
943                ..
944            } => {
945                assert!(prompt.is_none());
946                assert_eq!(model, DEFAULT_GENERATE_MODEL);
947                assert_eq!(max_tokens, 100);
948                assert!((temperature - 0.7).abs() < 0.001);
949                assert!(!greedy);
950                assert!(!gpu);
951                assert!(!quiet);
952            }
953            _ => panic!("Expected Generate command"),
954        }
955    }
956
957    #[test]
958    fn test_generate_with_prompt() {
959        let cli = parse_args(&["generate", "Hello world"]).unwrap();
960
961        match cli.command {
962            Commands::Generate { prompt, .. } => {
963                assert_eq!(prompt, Some("Hello world".to_string()));
964            }
965            _ => panic!("Expected Generate command"),
966        }
967    }
968
969    #[test]
970    fn test_generate_with_model() {
971        let cli = parse_args(&["generate", "-m", "phi3.5-mini"]).unwrap();
972
973        match cli.command {
974            Commands::Generate { model, .. } => {
975                assert_eq!(model, "phi3.5-mini");
976            }
977            _ => panic!("Expected Generate command"),
978        }
979    }
980
981    #[test]
982    fn test_generate_with_max_tokens() {
983        let cli = parse_args(&["generate", "-n", "500"]).unwrap();
984
985        match cli.command {
986            Commands::Generate { max_tokens, .. } => {
987                assert_eq!(max_tokens, 500);
988            }
989            _ => panic!("Expected Generate command"),
990        }
991    }
992
993    #[test]
994    fn test_generate_with_temperature() {
995        let cli = parse_args(&["generate", "-t", "1.5"]).unwrap();
996
997        match cli.command {
998            Commands::Generate { temperature, .. } => {
999                assert!((temperature - 1.5).abs() < 0.001);
1000            }
1001            _ => panic!("Expected Generate command"),
1002        }
1003    }
1004
1005    #[test]
1006    fn test_generate_with_sampling_params() {
1007        let cli = parse_args(&[
1008            "generate", "--top-k", "50", "--top-p", "0.9", "--min-p", "0.05",
1009        ])
1010        .unwrap();
1011
1012        match cli.command {
1013            Commands::Generate {
1014                top_k,
1015                top_p,
1016                min_p,
1017                ..
1018            } => {
1019                assert_eq!(top_k, Some(50));
1020                assert_eq!(top_p, Some(0.9));
1021                assert_eq!(min_p, Some(0.05));
1022            }
1023            _ => panic!("Expected Generate command"),
1024        }
1025    }
1026
1027    #[test]
1028    fn test_generate_with_greedy() {
1029        let cli = parse_args(&["generate", "--greedy"]).unwrap();
1030
1031        match cli.command {
1032            Commands::Generate { greedy, .. } => {
1033                assert!(greedy);
1034            }
1035            _ => panic!("Expected Generate command"),
1036        }
1037    }
1038
1039    #[test]
1040    fn test_generate_with_gpu() {
1041        let cli = parse_args(&["generate", "--gpu"]).unwrap();
1042
1043        match cli.command {
1044            Commands::Generate { gpu, .. } => {
1045                assert!(gpu);
1046            }
1047            _ => panic!("Expected Generate command"),
1048        }
1049    }
1050
1051    #[test]
1052    fn test_generate_with_no_stream() {
1053        let cli = parse_args(&["generate", "--no-stream"]).unwrap();
1054
1055        match cli.command {
1056            Commands::Generate { no_stream, .. } => {
1057                assert!(no_stream);
1058            }
1059            _ => panic!("Expected Generate command"),
1060        }
1061    }
1062
1063    #[test]
1064    fn test_generate_with_quiet() {
1065        let cli = parse_args(&["generate", "-q"]).unwrap();
1066
1067        match cli.command {
1068            Commands::Generate { quiet, .. } => {
1069                assert!(quiet);
1070            }
1071            _ => panic!("Expected Generate command"),
1072        }
1073    }
1074
1075    #[test]
1076    fn test_generate_all_options() {
1077        let cli = parse_args(&[
1078            "generate",
1079            "test prompt",
1080            "-m",
1081            "llama3.2-3b-instruct",
1082            "-n",
1083            "256",
1084            "-t",
1085            "0.8",
1086            "--top-k",
1087            "40",
1088            "--top-p",
1089            "0.95",
1090            "--repetition-penalty",
1091            "1.2",
1092            "--greedy",
1093            "--gpu",
1094            "--no-stream",
1095            "-q",
1096        ])
1097        .unwrap();
1098
1099        match cli.command {
1100            Commands::Generate {
1101                prompt,
1102                model,
1103                max_tokens,
1104                temperature,
1105                top_k,
1106                top_p,
1107                repetition_penalty,
1108                greedy,
1109                gpu,
1110                no_stream,
1111                quiet,
1112                ..
1113            } => {
1114                assert_eq!(prompt, Some("test prompt".to_string()));
1115                assert_eq!(model, "llama3.2-3b-instruct");
1116                assert_eq!(max_tokens, 256);
1117                assert!((temperature - 0.8).abs() < 0.001);
1118                assert_eq!(top_k, Some(40));
1119                assert_eq!(top_p, Some(0.95));
1120                assert!((repetition_penalty - 1.2).abs() < 0.001);
1121                assert!(greedy);
1122                assert!(gpu);
1123                assert!(no_stream);
1124                assert!(quiet);
1125            }
1126            _ => panic!("Expected Generate command"),
1127        }
1128    }
1129
1130    #[test]
1131    fn test_chat_defaults() {
1132        let cli = parse_args(&["chat"]).unwrap();
1133
1134        match cli.command {
1135            Commands::Chat {
1136                model,
1137                system,
1138                temperature,
1139                max_tokens,
1140                gpu,
1141                quiet,
1142                ..
1143            } => {
1144                assert_eq!(model, DEFAULT_CHAT_MODEL);
1145                assert!(system.is_none());
1146                assert!((temperature - 0.7).abs() < 0.001);
1147                assert_eq!(max_tokens, 512);
1148                assert!(!gpu);
1149                assert!(!quiet);
1150            }
1151            _ => panic!("Expected Chat command"),
1152        }
1153    }
1154
1155    #[test]
1156    fn test_chat_with_system_prompt() {
1157        let cli = parse_args(&["chat", "-s", "You are a helpful assistant"]).unwrap();
1158
1159        match cli.command {
1160            Commands::Chat { system, .. } => {
1161                assert_eq!(system, Some("You are a helpful assistant".to_string()));
1162            }
1163            _ => panic!("Expected Chat command"),
1164        }
1165    }
1166
1167    #[test]
1168    fn test_chat_with_model() {
1169        let cli = parse_args(&["chat", "-m", "phi3.5-mini"]).unwrap();
1170
1171        match cli.command {
1172            Commands::Chat { model, .. } => {
1173                assert_eq!(model, "phi3.5-mini");
1174            }
1175            _ => panic!("Expected Chat command"),
1176        }
1177    }
1178
1179    #[test]
1180    fn test_classify_defaults() {
1181        let cli = parse_args(&["classify"]).unwrap();
1182
1183        match cli.command {
1184            Commands::Classify {
1185                input,
1186                model,
1187                top_k,
1188                format,
1189                multi_label,
1190                gpu,
1191                quiet,
1192                ..
1193            } => {
1194                assert!(input.is_empty());
1195                assert_eq!(model, "distilbert-sentiment");
1196                assert_eq!(top_k, 5);
1197                assert_eq!(format, "text");
1198                assert!(!multi_label);
1199                assert!(!gpu);
1200                assert!(!quiet);
1201            }
1202            _ => panic!("Expected Classify command"),
1203        }
1204    }
1205
1206    #[test]
1207    fn test_classify_with_input() {
1208        let cli = parse_args(&["classify", "This is great!"]).unwrap();
1209
1210        match cli.command {
1211            Commands::Classify { input, .. } => {
1212                assert_eq!(input, vec!["This is great!".to_string()]);
1213            }
1214            _ => panic!("Expected Classify command"),
1215        }
1216    }
1217
1218    #[test]
1219    fn test_classify_with_multiple_inputs() {
1220        let cli = parse_args(&["classify", "text one", "text two", "text three"]).unwrap();
1221
1222        match cli.command {
1223            Commands::Classify { input, .. } => {
1224                assert_eq!(input.len(), 3);
1225                assert_eq!(input[0], "text one");
1226                assert_eq!(input[1], "text two");
1227                assert_eq!(input[2], "text three");
1228            }
1229            _ => panic!("Expected Classify command"),
1230        }
1231    }
1232
1233    #[test]
1234    fn test_classify_with_labels() {
1235        let cli = parse_args(&["classify", "--labels", "bad,good"]).unwrap();
1236
1237        match cli.command {
1238            Commands::Classify { labels, .. } => {
1239                assert_eq!(labels, Some("bad,good".to_string()));
1240            }
1241            _ => panic!("Expected Classify command"),
1242        }
1243    }
1244
1245    #[test]
1246    fn test_classify_with_multi_label() {
1247        let cli = parse_args(&["classify", "--multi-label"]).unwrap();
1248
1249        match cli.command {
1250            Commands::Classify { multi_label, .. } => {
1251                assert!(multi_label);
1252            }
1253            _ => panic!("Expected Classify command"),
1254        }
1255    }
1256
1257    #[test]
1258    fn test_search_minimal() {
1259        let cli = parse_args(&["search", "./index", "my query"]).unwrap();
1260
1261        match cli.command {
1262            Commands::Search {
1263                index_path,
1264                query,
1265                top_k,
1266                mode,
1267                model,
1268                format,
1269                ..
1270            } => {
1271                assert_eq!(index_path, "./index");
1272                assert_eq!(query, "my query");
1273                assert_eq!(top_k, 10);
1274                assert_eq!(mode, "hybrid");
1275                assert_eq!(model, None);
1276                assert_eq!(format, "text");
1277            }
1278            _ => panic!("Expected Search command"),
1279        }
1280    }
1281
1282    #[test]
1283    fn test_search_with_top_k() {
1284        let cli = parse_args(&["search", "./index", "query", "-k", "20"]).unwrap();
1285
1286        match cli.command {
1287            Commands::Search { top_k, .. } => {
1288                assert_eq!(top_k, 20);
1289            }
1290            _ => panic!("Expected Search command"),
1291        }
1292    }
1293
1294    #[test]
1295    fn test_search_with_mode() {
1296        let cli = parse_args(&["search", "./index", "query", "--mode", "semantic"]).unwrap();
1297
1298        match cli.command {
1299            Commands::Search { mode, .. } => {
1300                assert_eq!(mode, "semantic");
1301            }
1302            _ => panic!("Expected Search command"),
1303        }
1304    }
1305
1306    #[test]
1307    fn test_search_with_rerank_model() {
1308        let cli = parse_args(&[
1309            "search",
1310            "./index",
1311            "query",
1312            "--rerank-model",
1313            "ms-marco-minilm",
1314        ])
1315        .unwrap();
1316
1317        match cli.command {
1318            Commands::Search { rerank_model, .. } => {
1319                assert_eq!(rerank_model, Some("ms-marco-minilm".to_string()));
1320            }
1321            _ => panic!("Expected Search command"),
1322        }
1323    }
1324
1325    #[test]
1326    fn test_similarity_minimal() {
1327        let cli = parse_args(&["similarity", "text one", "text two"]).unwrap();
1328
1329        match cli.command {
1330            Commands::Similarity {
1331                text1,
1332                text2,
1333                model,
1334                gpu,
1335                quiet,
1336            } => {
1337                assert_eq!(text1, "text one");
1338                assert_eq!(text2, "text two");
1339                assert_eq!(model, "minilm-l6-v2");
1340                assert!(!gpu);
1341                assert!(!quiet);
1342            }
1343            _ => panic!("Expected Similarity command"),
1344        }
1345    }
1346
1347    #[test]
1348    fn test_model_list_defaults() {
1349        let cli = parse_args(&["model", "list"]).unwrap();
1350
1351        match cli.command {
1352            Commands::Model {
1353                action:
1354                    ModelCommands::List {
1355                        arch,
1356                        task,
1357                        downloaded,
1358                    },
1359            } => {
1360                assert!(arch.is_none());
1361                assert!(task.is_none());
1362                assert!(!downloaded);
1363            }
1364            _ => panic!("Expected Model List command"),
1365        }
1366    }
1367
1368    #[test]
1369    fn test_model_list_with_filters() {
1370        let cli = parse_args(&[
1371            "model",
1372            "list",
1373            "--arch",
1374            "bert",
1375            "--task",
1376            "embedding",
1377            "--downloaded",
1378        ])
1379        .unwrap();
1380
1381        match cli.command {
1382            Commands::Model {
1383                action:
1384                    ModelCommands::List {
1385                        arch,
1386                        task,
1387                        downloaded,
1388                    },
1389            } => {
1390                assert_eq!(arch, Some("bert".to_string()));
1391                assert_eq!(task, Some("embedding".to_string()));
1392                assert!(downloaded);
1393            }
1394            _ => panic!("Expected Model List command"),
1395        }
1396    }
1397
1398    #[test]
1399    fn test_model_download() {
1400        let cli = parse_args(&["model", "download", "minilm-l6-v2"]).unwrap();
1401
1402        match cli.command {
1403            Commands::Model {
1404                action: ModelCommands::Download { name, gguf, quiet },
1405            } => {
1406                assert_eq!(name, "minilm-l6-v2");
1407                assert!(!gguf);
1408                assert!(!quiet);
1409            }
1410            _ => panic!("Expected Model Download command"),
1411        }
1412    }
1413
1414    #[test]
1415    fn test_model_download_gguf() {
1416        let cli = parse_args(&["model", "download", "llama3.2-1b", "--gguf"]).unwrap();
1417
1418        match cli.command {
1419            Commands::Model {
1420                action: ModelCommands::Download { name, gguf, .. },
1421            } => {
1422                assert_eq!(name, "llama3.2-1b");
1423                assert!(gguf);
1424            }
1425            _ => panic!("Expected Model Download command"),
1426        }
1427    }
1428
1429    #[test]
1430    fn test_model_info() {
1431        let cli = parse_args(&["model", "info", "phi3.5-mini"]).unwrap();
1432
1433        match cli.command {
1434            Commands::Model {
1435                action: ModelCommands::Info { name },
1436            } => {
1437                assert_eq!(name, "phi3.5-mini");
1438            }
1439            _ => panic!("Expected Model Info command"),
1440        }
1441    }
1442
1443    #[test]
1444    fn test_model_remove() {
1445        let cli = parse_args(&["model", "remove", "old-model"]).unwrap();
1446
1447        match cli.command {
1448            Commands::Model {
1449                action: ModelCommands::Remove { name },
1450            } => {
1451                assert_eq!(name, "old-model");
1452            }
1453            _ => panic!("Expected Model Remove command"),
1454        }
1455    }
1456
1457    #[test]
1458    fn test_model_search() {
1459        let cli = parse_args(&["model", "search", "llama"]).unwrap();
1460
1461        match cli.command {
1462            Commands::Model {
1463                action: ModelCommands::Search { query },
1464            } => {
1465                assert_eq!(query, "llama");
1466            }
1467            _ => panic!("Expected Model Search command"),
1468        }
1469    }
1470
1471    #[test]
1472    fn test_index_create_minimal() {
1473        let cli = parse_args(&["index", "create", "output.idx"]).unwrap();
1474
1475        match cli.command {
1476            Commands::Index {
1477                action:
1478                    IndexCommands::Create {
1479                        output,
1480                        inputs,
1481                        chunk_size,
1482                        chunk_overlap,
1483                        model,
1484                        ..
1485                    },
1486            } => {
1487                assert_eq!(output, "output.idx");
1488                assert!(inputs.is_empty());
1489                assert_eq!(chunk_size, 512);
1490                assert_eq!(chunk_overlap, 100);
1491                assert_eq!(model, "minilm-l6-v2");
1492            }
1493            _ => panic!("Expected Index Create command"),
1494        }
1495    }
1496
1497    #[test]
1498    fn test_index_create_with_inputs() {
1499        let cli = parse_args(&[
1500            "index",
1501            "create",
1502            "out.idx",
1503            "file1.txt",
1504            "file2.txt",
1505            "dir/",
1506        ])
1507        .unwrap();
1508
1509        match cli.command {
1510            Commands::Index {
1511                action: IndexCommands::Create { inputs, .. },
1512            } => {
1513                assert_eq!(inputs.len(), 3);
1514                assert_eq!(inputs[0], "file1.txt");
1515                assert_eq!(inputs[1], "file2.txt");
1516                assert_eq!(inputs[2], "dir/");
1517            }
1518            _ => panic!("Expected Index Create command"),
1519        }
1520    }
1521
1522    #[test]
1523    fn test_index_create_with_options() {
1524        let cli = parse_args(&[
1525            "index",
1526            "create",
1527            "out.idx",
1528            "--chunk-size",
1529            "500",
1530            "--chunk-overlap",
1531            "100",
1532            "-m",
1533            "nomic-embed-text",
1534            "--gpu",
1535            "-q",
1536        ])
1537        .unwrap();
1538
1539        match cli.command {
1540            Commands::Index {
1541                action:
1542                    IndexCommands::Create {
1543                        chunk_size,
1544                        chunk_overlap,
1545                        model,
1546                        gpu,
1547                        quiet,
1548                        ..
1549                    },
1550            } => {
1551                assert_eq!(chunk_size, 500);
1552                assert_eq!(chunk_overlap, 100);
1553                assert_eq!(model, "nomic-embed-text");
1554                assert!(gpu);
1555                assert!(quiet);
1556            }
1557            _ => panic!("Expected Index Create command"),
1558        }
1559    }
1560
1561    #[test]
1562    fn test_index_add() {
1563        let cli = parse_args(&["index", "add", "existing.idx", "newfile.txt"]).unwrap();
1564
1565        match cli.command {
1566            Commands::Index {
1567                action:
1568                    IndexCommands::Add {
1569                        index_path, inputs, ..
1570                    },
1571            } => {
1572                assert_eq!(index_path, "existing.idx");
1573                assert_eq!(inputs, vec!["newfile.txt".to_string()]);
1574            }
1575            _ => panic!("Expected Index Add command"),
1576        }
1577    }
1578
1579    #[test]
1580    fn test_index_info() {
1581        let cli = parse_args(&["index", "info", "my.idx"]).unwrap();
1582
1583        match cli.command {
1584            Commands::Index {
1585                action: IndexCommands::Info { index_path },
1586            } => {
1587                assert_eq!(index_path, "my.idx");
1588            }
1589            _ => panic!("Expected Index Info command"),
1590        }
1591    }
1592
1593    #[test]
1594    fn test_verbose_zero() {
1595        let cli = parse_args(&["generate"]).unwrap();
1596        assert_eq!(cli.verbose, 0);
1597    }
1598
1599    #[test]
1600    fn test_verbose_one() {
1601        let cli = parse_args(&["-v", "generate"]).unwrap();
1602        assert_eq!(cli.verbose, 1);
1603    }
1604
1605    #[test]
1606    fn test_verbose_two() {
1607        let cli = parse_args(&["-vv", "generate"]).unwrap();
1608        assert_eq!(cli.verbose, 2);
1609    }
1610
1611    #[test]
1612    fn test_verbose_three() {
1613        let cli = parse_args(&["-vvv", "generate"]).unwrap();
1614        assert_eq!(cli.verbose, 3);
1615    }
1616
1617    #[test]
1618    fn test_verbose_long_form() {
1619        let cli = parse_args(&["--verbose", "--verbose", "generate"]).unwrap();
1620        assert_eq!(cli.verbose, 2);
1621    }
1622
1623    #[test]
1624    fn test_verbose_after_command() {
1625        // Global flag can come after command
1626        let cli = parse_args(&["generate", "-v"]).unwrap();
1627        assert_eq!(cli.verbose, 1);
1628    }
1629
1630    #[test]
1631    fn test_missing_command() {
1632        let result = parse_args(&[]);
1633        assert!(result.is_err());
1634    }
1635
1636    #[test]
1637    fn test_unknown_command() {
1638        let result = parse_args(&["unknown"]);
1639        assert!(result.is_err());
1640    }
1641
1642    #[test]
1643    fn test_missing_required_arg() {
1644        // transcribe requires a file
1645        let result = parse_args(&["transcribe"]);
1646        assert!(result.is_err());
1647    }
1648
1649    #[test]
1650    fn test_invalid_number() {
1651        let result = parse_args(&["generate", "-n", "not_a_number"]);
1652        assert!(result.is_err());
1653    }
1654
1655    #[test]
1656    fn test_invalid_float() {
1657        let result = parse_args(&["generate", "-t", "not_a_float"]);
1658        assert!(result.is_err());
1659    }
1660
1661    #[test]
1662    fn test_rerank_minimal() {
1663        let cli = parse_args(&["rerank", "my query"]).unwrap();
1664
1665        match cli.command {
1666            Commands::Rerank {
1667                query,
1668                documents,
1669                model,
1670                top_k,
1671                format,
1672                ..
1673            } => {
1674                assert_eq!(query, "my query");
1675                assert!(documents.is_empty());
1676                assert_eq!(model, "minilm-l6-v2-cross-encoder");
1677                assert!(top_k.is_none());
1678                assert_eq!(format, "text");
1679            }
1680            _ => panic!("Expected Rerank command"),
1681        }
1682    }
1683
1684    #[test]
1685    fn test_rerank_with_documents() {
1686        let cli = parse_args(&["rerank", "query", "doc1", "doc2", "doc3"]).unwrap();
1687
1688        match cli.command {
1689            Commands::Rerank {
1690                query, documents, ..
1691            } => {
1692                assert_eq!(query, "query");
1693                assert_eq!(documents.len(), 3);
1694            }
1695            _ => panic!("Expected Rerank command"),
1696        }
1697    }
1698
1699    #[test]
1700    fn test_rerank_with_top_k() {
1701        let cli = parse_args(&["rerank", "query", "-k", "5"]).unwrap();
1702
1703        match cli.command {
1704            Commands::Rerank { top_k, .. } => {
1705                assert_eq!(top_k, Some(5));
1706            }
1707            _ => panic!("Expected Rerank command"),
1708        }
1709    }
1710
1711    #[test]
1712    fn test_summarize_defaults() {
1713        let cli = parse_args(&["summarize"]).unwrap();
1714
1715        match cli.command {
1716            Commands::Summarize {
1717                input,
1718                model,
1719                min_length,
1720                max_length,
1721                num_beams,
1722                ..
1723            } => {
1724                assert!(input.is_none());
1725                assert_eq!(model, "distilbart-cnn");
1726                assert!(min_length.is_none());
1727                assert!(max_length.is_none());
1728                assert!(num_beams.is_none());
1729            }
1730            _ => panic!("Expected Summarize command"),
1731        }
1732    }
1733
1734    #[test]
1735    fn test_summarize_with_options() {
1736        let cli = parse_args(&[
1737            "summarize",
1738            "--input",
1739            "input.txt",
1740            "--min-length",
1741            "50",
1742            "--max-length",
1743            "200",
1744            "--num-beams",
1745            "4",
1746            "--length-penalty",
1747            "1.5",
1748        ])
1749        .unwrap();
1750
1751        match cli.command {
1752            Commands::Summarize {
1753                input,
1754                min_length,
1755                max_length,
1756                num_beams,
1757                length_penalty,
1758                ..
1759            } => {
1760                assert_eq!(input, Some("input.txt".to_string()));
1761                assert_eq!(min_length, Some(50));
1762                assert_eq!(max_length, Some(200));
1763                assert_eq!(num_beams, Some(4));
1764                assert_eq!(length_penalty, Some(1.5));
1765            }
1766            _ => panic!("Expected Summarize command"),
1767        }
1768    }
1769
1770    #[test]
1771    fn test_translate_defaults() {
1772        let cli = parse_args(&["translate"]).unwrap();
1773
1774        match cli.command {
1775            Commands::Translate {
1776                input,
1777                model,
1778                src,
1779                dst,
1780                ..
1781            } => {
1782                assert!(input.is_none());
1783                assert_eq!(model, None);
1784                assert!(src.is_none());
1785                assert!(dst.is_none());
1786            }
1787            _ => panic!("Expected Translate command"),
1788        }
1789    }
1790
1791    #[test]
1792    fn embed_normalizes_unless_told_not_to() {
1793        let flag = |args: &[&str]| match parse_args(args).unwrap().command {
1794            Commands::Embed { no_normalize, .. } => no_normalize,
1795            _ => panic!("Expected Embed command"),
1796        };
1797        assert!(!flag(&["embed", "hello"]));
1798        assert!(!flag(&["embed", "--normalize", "hello"]));
1799        assert!(flag(&["embed", "--no-normalize", "hello"]));
1800        assert!(parse_args(&["embed", "--normalize", "--no-normalize", "hello"]).is_err());
1801    }
1802
1803    #[test]
1804    fn test_translate_with_languages() {
1805        let cli = parse_args(&["translate", "--src", "en", "--dst", "is"]).unwrap();
1806
1807        match cli.command {
1808            Commands::Translate { src, dst, .. } => {
1809                assert_eq!(src, Some("en".to_string()));
1810                assert_eq!(dst, Some("is".to_string()));
1811            }
1812            _ => panic!("Expected Translate command"),
1813        }
1814    }
1815    #[test]
1816    fn test_transcribe_minimal() {
1817        let cli = parse_args(&["transcribe", "audio.wav"]).unwrap();
1818
1819        match cli.command {
1820            Commands::Transcribe {
1821                file,
1822                model,
1823                language,
1824                ..
1825            } => {
1826                assert_eq!(file, "audio.wav");
1827                assert_eq!(model, "whisper-small");
1828                assert!(language.is_none());
1829            }
1830            _ => panic!("Expected Transcribe command"),
1831        }
1832    }
1833
1834    #[test]
1835    fn test_classify_with_model() {
1836        let cli = parse_args(&["classify", "i hate mondays", "--model", "toxic-bert"]).unwrap();
1837        match cli.command {
1838            Commands::Classify { input, model, .. } => {
1839                assert_eq!(input, vec!["i hate mondays".to_string()]);
1840                assert_eq!(model, "toxic-bert");
1841            }
1842            _ => panic!("Expected Classify command"),
1843        }
1844    }
1845
1846    #[test]
1847    fn test_transcribe_with_options() {
1848        let cli = parse_args(&[
1849            "transcribe",
1850            "audio.mp3",
1851            "-m",
1852            "whisper-large-v3",
1853            "--language",
1854            "is",
1855        ])
1856        .unwrap();
1857
1858        match cli.command {
1859            Commands::Transcribe {
1860                file,
1861                model,
1862                language,
1863                ..
1864            } => {
1865                assert_eq!(file, "audio.mp3");
1866                assert_eq!(model, "whisper-large-v3");
1867                assert_eq!(language, Some("is".to_string()));
1868            }
1869            _ => panic!("Expected Transcribe command"),
1870        }
1871    }
1872}
1873
1874mod send_sync_tests {
1875    use kjarni::{
1876        Classifier, Embedder, Indexer, Reranker, Searcher, chat::Chat, generator::Generator,
1877    };
1878    // Compile time verificatio
1879    const _: () = {
1880        const fn assert_send<T: Send>() {}
1881        const fn assert_sync<T: Sync>() {}
1882        assert_send::<Embedder>();
1883        assert_sync::<Embedder>();
1884
1885        assert_send::<Indexer>();
1886        assert_sync::<Indexer>();
1887
1888        assert_send::<Searcher>();
1889        assert_sync::<Searcher>();
1890
1891        assert_send::<Reranker>();
1892        assert_sync::<Reranker>();
1893
1894        assert_send::<Generator>();
1895        assert_sync::<Generator>();
1896
1897        assert_send::<Chat>();
1898        assert_sync::<Chat>();
1899
1900        assert_send::<Classifier>();
1901        assert_sync::<Classifier>();
1902    };
1903}