maproom 0.1.0

Semantic code search powered by embeddings and SQLite
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
//! Status module for querying indexed repositories and worktrees.
//!
//! This module uses SqliteStore for database access.

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

use crate::db::traits::StoreCore;
use crate::db::SqliteStore;

#[derive(Debug, Serialize, Deserialize)]
pub struct StatusResponse {
    pub repos: Vec<RepoStatus>,
    pub index_size_bytes: Option<u64>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RepoStatus {
    pub name: String,
    pub worktrees: Vec<WorktreeStatus>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct WorktreeStatus {
    pub name: String,
    pub chunk_count: i64,
    pub last_updated: Option<String>,
    pub embedding_count: i64,
    pub embedding_percentage: f64,
    pub languages: HashMap<String, i64>,
}

/// Query database for worktree status information.
///
/// Uses SqliteStore for database access.
pub async fn get_status(
    store: Arc<SqliteStore>,
    repo_filter: Option<String>,
    worktree_filter: Option<String>,
    _verbose: bool,
) -> Result<StatusResponse> {
    // Get all repositories
    let repos = store.list_repos().await?;

    let mut repo_statuses = Vec::new();

    for repo in repos {
        // Apply repo filter if specified
        if let Some(ref filter) = repo_filter {
            if repo.name != *filter {
                continue;
            }
        }

        // Get worktrees for this repo
        let worktrees = store.list_worktrees(repo.id).await?;

        let mut worktree_statuses = Vec::new();

        for worktree in worktrees {
            // Apply worktree filter if specified
            if let Some(ref filter) = worktree_filter {
                if worktree.name != *filter {
                    continue;
                }
            }

            // Get chunk count for this worktree
            let chunk_count = store.get_worktree_chunk_count(worktree.id).await?;

            // Get embedding count for this worktree
            let embedding_count = store.get_worktree_embedding_count(worktree.id).await?;

            // Calculate embedding percentage (handle division by zero)
            let embedding_percentage = if chunk_count == 0 {
                0.0
            } else {
                (embedding_count as f64 / chunk_count as f64) * 100.0
            };

            // Get language breakdown for this worktree
            let language_breakdown = store.get_worktree_language_breakdown(worktree.id).await?;
            let languages: HashMap<String, i64> = language_breakdown.into_iter().collect();

            // Get last scan timestamp for this worktree
            let last_updated = store.get_worktree_last_scan(worktree.id).await?;

            worktree_statuses.push(WorktreeStatus {
                name: worktree.name,
                chunk_count,
                last_updated,
                embedding_count,
                embedding_percentage,
                languages,
            });
        }

        repo_statuses.push(RepoStatus {
            name: repo.name,
            worktrees: worktree_statuses,
        });
    }

    // Sort repos by name for consistent output
    repo_statuses.sort_by(|a, b| a.name.cmp(&b.name));

    // Get database file size
    let index_size_bytes = get_database_size().await;

    Ok(StatusResponse {
        repos: repo_statuses,
        index_size_bytes,
    })
}

/// Get the size of the database file in bytes.
///
/// Returns `None` for in-memory databases or if the file cannot be read.
async fn get_database_size() -> Option<u64> {
    use crate::db::get_database_url;

    let db_url = match get_database_url() {
        Ok(url) => url,
        Err(_) => return None,
    };

    if db_url.starts_with("sqlite://") {
        let path = db_url.strip_prefix("sqlite://").unwrap();
        match std::fs::metadata(path) {
            Ok(metadata) => Some(metadata.len()),
            Err(_) => None,
        }
    } else {
        None
    }
}

/// Format status as human-readable text
pub fn format_text(status: &StatusResponse, verbose: bool) -> String {
    if status.repos.is_empty() {
        return "No repositories indexed yet.\n\nRun 'maproom scan' to index a repository."
            .to_string();
    }

    let mut output = String::new();

    for repo in &status.repos {
        output.push_str(&format!("Repository: {}\n", repo.name));

        if repo.worktrees.is_empty() {
            output.push_str("  No worktrees indexed\n");
        } else {
            for worktree in &repo.worktrees {
                output.push_str(&format!("  Worktree: {}\n", worktree.name));
                output.push_str(&format!(
                    "    Chunks: {}\n",
                    format_number(worktree.chunk_count)
                ));

                // Embeddings line with count and percentage
                output.push_str(&format!(
                    "    Embeddings: {} ({:.1}%)\n",
                    format_number(worktree.embedding_count),
                    worktree.embedding_percentage
                ));

                // Languages line with sorted list
                output.push_str("    Languages: ");
                if worktree.languages.is_empty() {
                    output.push_str("(none)\n");
                } else {
                    // Sort languages by count descending, then alphabetically
                    let mut lang_pairs: Vec<_> = worktree.languages.iter().collect();
                    lang_pairs.sort_by(|a, b| b.1.cmp(a.1).then_with(|| a.0.cmp(b.0)));

                    // Determine how many to show
                    let show_count = if verbose {
                        lang_pairs.len()
                    } else {
                        lang_pairs.len().min(5)
                    };
                    let truncated = !verbose && lang_pairs.len() > 5;

                    // Format the languages
                    let lang_strs: Vec<String> = lang_pairs
                        .iter()
                        .take(show_count)
                        .map(|(lang, count)| format!("{} ({})", lang, count))
                        .collect();
                    output.push_str(&lang_strs.join(", "));

                    // Add truncation indicator if needed
                    if truncated {
                        let remaining = lang_pairs.len() - show_count;
                        output.push_str(&format!(" ...and {} more (use --verbose)", remaining));
                    }
                    output.push('\n');
                }

                // Last scan line
                output.push_str("    Last scan: ");
                if let Some(ref last_updated) = worktree.last_updated {
                    output.push_str(last_updated);
                } else {
                    output.push_str("never");
                }
                output.push('\n');
            }
        }

        output.push('\n');
    }

    // Add total index size at the end (once, not per-worktree)
    if let Some(bytes) = status.index_size_bytes {
        output.push_str(&format!("Total index size: {}\n", format_size_mb(bytes)));
    }

    output
}

/// Format number with thousands separator
fn format_number(n: i64) -> String {
    let s = n.to_string();
    let mut result = String::new();

    for (count, c) in s.chars().rev().enumerate() {
        if count > 0 && count % 3 == 0 {
            result.insert(0, ',');
        }
        result.insert(0, c);
    }

    result
}

/// Format bytes as megabytes with 2 decimal places
fn format_size_mb(bytes: u64) -> String {
    format!("{:.2} MB", bytes as f64 / 1_048_576.0)
}

/// Format status as JSON
pub fn format_json(status: &StatusResponse) -> Result<String> {
    serde_json::to_string_pretty(status)
        .map_err(|e| anyhow::anyhow!("Failed to serialize status to JSON: {}", e))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::sqlite::SqliteStore;
    use crate::db::traits::StoreChunks;
    use crate::db::traits::StoreMigration;
    use crate::db::{ChunkRecord, FileRecord};
    use rusqlite::params;

    #[tokio::test]
    async fn test_worktree_status_with_populated_data() {
        let store = Arc::new(SqliteStore::connect(":memory:").await.unwrap());
        store.migrate().await.unwrap();

        // Create test data using the available methods
        let repo_id = store
            .get_or_create_repo("test-repo", "/test/path")
            .await
            .unwrap();
        let worktree_id = store
            .get_or_create_worktree(repo_id, "main", "/test/path")
            .await
            .unwrap();
        let commit_id = store
            .get_or_create_commit(repo_id, "abc123", None)
            .await
            .unwrap();

        // Create files with different languages
        let file1 = FileRecord {
            repo_id,
            worktree_id,
            commit_id,
            relpath: "test.rs".to_string(),
            language: Some("rust".to_string()),
            content_hash: "hash1".to_string(),
            size_bytes: 100,
            last_modified: None,
        };
        let file1_id = store.upsert_file(&file1).await.unwrap();

        let file2 = FileRecord {
            repo_id,
            worktree_id,
            commit_id,
            relpath: "test.py".to_string(),
            language: Some("python".to_string()),
            content_hash: "hash2".to_string(),
            size_bytes: 100,
            last_modified: None,
        };
        let file2_id = store.upsert_file(&file2).await.unwrap();

        // Create chunks
        let chunk1 = ChunkRecord {
            file_id: file1_id,
            worktree_id,
            blob_sha: "blob1".to_string(),
            symbol_name: Some("fn1".to_string()),
            kind: "function".to_string(),
            signature: None,
            docstring: None,
            start_line: 1,
            end_line: 10,
            preview: "fn fn1() {}".to_string(),
            ts_doc_text: String::new(),
            recency_score: 1.0,
            churn_score: 0.5,
            metadata: None,
        };
        store.insert_chunk(&chunk1).await.unwrap();

        let chunk2 = ChunkRecord {
            file_id: file2_id,
            worktree_id,
            blob_sha: "blob2".to_string(),
            symbol_name: Some("fn2".to_string()),
            kind: "function".to_string(),
            signature: None,
            docstring: None,
            start_line: 1,
            end_line: 10,
            preview: "def fn2(): pass".to_string(),
            ts_doc_text: String::new(),
            recency_score: 1.0,
            churn_score: 0.5,
            metadata: None,
        };
        store.insert_chunk(&chunk2).await.unwrap();

        // Create embeddings for some chunks
        store
            .run(move |conn| {
                conn.execute(
                    "INSERT INTO code_embeddings (blob_sha, embedding, embedding_dim, model_version)
                     VALUES (?1, ?2, ?3, ?4)",
                    params!["blob1", vec![0u8; 4096], 1024, "test-model"],
                )?;
                Ok(())
            })
            .await
            .unwrap();

        // Create index_state entry
        store
            .run(move |conn| {
                conn.execute(
                    "INSERT INTO index_state (worktree_id, tree_sha, chunks_processed, embeddings_generated, last_indexed)
                     VALUES (?1, ?2, ?3, ?4, ?5)",
                    params![worktree_id, "tree123", 0, 0, "2024-01-01T12:00:00Z"],
                )?;
                Ok(())
            })
            .await
            .unwrap();

        // Get status
        let status = get_status(store.clone(), None, None, false).await.unwrap();

        // Verify response structure
        assert_eq!(status.repos.len(), 1);
        assert_eq!(status.repos[0].name, "test-repo");
        assert_eq!(status.repos[0].worktrees.len(), 1);

        // Verify worktree status
        let worktree_status = &status.repos[0].worktrees[0];
        assert_eq!(worktree_status.name, "main");
        assert_eq!(worktree_status.chunk_count, 2);
        assert_eq!(worktree_status.embedding_count, 1);
        assert_eq!(worktree_status.embedding_percentage, 50.0);
        assert_eq!(
            worktree_status.last_updated,
            Some("2024-01-01T12:00:00Z".to_string())
        );

        // Verify language breakdown
        assert_eq!(worktree_status.languages.len(), 2);
        assert_eq!(worktree_status.languages.get("rust"), Some(&1));
        assert_eq!(worktree_status.languages.get("python"), Some(&1));
    }

    #[tokio::test]
    async fn test_worktree_status_with_zero_chunks() {
        let store = Arc::new(SqliteStore::connect(":memory:").await.unwrap());
        store.migrate().await.unwrap();

        // Create test data with no chunks
        let repo_id = store
            .get_or_create_repo("test-repo", "/test/path")
            .await
            .unwrap();
        let _worktree_id = store
            .get_or_create_worktree(repo_id, "main", "/test/path")
            .await
            .unwrap();

        // Get status
        let status = get_status(store.clone(), None, None, false).await.unwrap();

        // Verify worktree status
        let worktree_status = &status.repos[0].worktrees[0];
        assert_eq!(worktree_status.chunk_count, 0);
        assert_eq!(worktree_status.embedding_count, 0);
        assert_eq!(worktree_status.embedding_percentage, 0.0); // Division by zero handled
        assert_eq!(worktree_status.last_updated, None); // No index_state entry
        assert_eq!(worktree_status.languages.len(), 0);
    }

    #[tokio::test]
    async fn test_index_size_bytes_for_in_memory_database() {
        // Set environment variable to use :memory: database
        std::env::set_var("MAPROOM_DATABASE_URL", ":memory:");

        let store = Arc::new(SqliteStore::connect(":memory:").await.unwrap());
        store.migrate().await.unwrap();

        // Create minimal test data
        let repo_id = store
            .get_or_create_repo("test-repo", "/test/path")
            .await
            .unwrap();
        store
            .get_or_create_worktree(repo_id, "main", "/test/path")
            .await
            .unwrap();

        // Get status
        let status = get_status(store.clone(), None, None, false).await.unwrap();

        // For in-memory database, index_size_bytes should be None
        assert_eq!(status.index_size_bytes, None);

        // Cleanup
        std::env::remove_var("MAPROOM_DATABASE_URL");
    }

    #[tokio::test]
    async fn test_embedding_percentage_calculation() {
        let store = Arc::new(SqliteStore::connect(":memory:").await.unwrap());
        store.migrate().await.unwrap();

        // Create test data
        let repo_id = store
            .get_or_create_repo("test-repo", "/test/path")
            .await
            .unwrap();
        let worktree_id = store
            .get_or_create_worktree(repo_id, "main", "/test/path")
            .await
            .unwrap();
        let commit_id = store
            .get_or_create_commit(repo_id, "abc123", None)
            .await
            .unwrap();

        // Create file
        let file = FileRecord {
            repo_id,
            worktree_id,
            commit_id,
            relpath: "test.rs".to_string(),
            language: Some("rust".to_string()),
            content_hash: "hash1".to_string(),
            size_bytes: 100,
            last_modified: None,
        };
        let file_id = store.upsert_file(&file).await.unwrap();

        // Create 4 chunks
        for i in 0..4 {
            let chunk = ChunkRecord {
                file_id,
                worktree_id,
                blob_sha: format!("blob{}", i),
                symbol_name: Some(format!("fn{}", i)),
                kind: "function".to_string(),
                signature: None,
                docstring: None,
                start_line: (i * 10 + 1) as i32,
                end_line: (i * 10 + 10) as i32,
                preview: format!("fn fn{}() {{}}", i),
                ts_doc_text: String::new(),
                recency_score: 1.0,
                churn_score: 0.5,
                metadata: None,
            };
            store.insert_chunk(&chunk).await.unwrap();
        }

        // Create embeddings for 3 of 4 chunks
        for i in 0..3 {
            let blob_sha = format!("blob{}", i);
            store
                .run(move |conn| {
                    conn.execute(
                        "INSERT INTO code_embeddings (blob_sha, embedding, embedding_dim, model_version)
                         VALUES (?1, ?2, ?3, ?4)",
                        params![blob_sha, vec![0u8; 4096], 1024, "test-model"],
                    )?;
                    Ok(())
                })
                .await
                .unwrap();
        }

        // Get status
        let status = get_status(store.clone(), None, None, false).await.unwrap();

        // Verify embedding percentage (3/4 = 75%)
        let worktree_status = &status.repos[0].worktrees[0];
        assert_eq!(worktree_status.chunk_count, 4);
        assert_eq!(worktree_status.embedding_count, 3);
        assert_eq!(worktree_status.embedding_percentage, 75.0);
    }

    #[tokio::test]
    async fn test_language_breakdown_conversion() {
        let store = Arc::new(SqliteStore::connect(":memory:").await.unwrap());
        store.migrate().await.unwrap();

        // Create test data
        let repo_id = store
            .get_or_create_repo("test-repo", "/test/path")
            .await
            .unwrap();
        let worktree_id = store
            .get_or_create_worktree(repo_id, "main", "/test/path")
            .await
            .unwrap();
        let commit_id = store
            .get_or_create_commit(repo_id, "abc123", None)
            .await
            .unwrap();

        // Create files with various languages
        let files = vec![
            ("test1.rs", "rust"),
            ("test2.rs", "rust"),
            ("test.py", "python"),
            ("test.go", "go"),
        ];

        for (path, lang) in files {
            let file = FileRecord {
                repo_id,
                worktree_id,
                commit_id,
                relpath: path.to_string(),
                language: Some(lang.to_string()),
                content_hash: format!("hash_{}", path),
                size_bytes: 100,
                last_modified: None,
            };
            store.upsert_file(&file).await.unwrap();
        }

        // Get status
        let status = get_status(store.clone(), None, None, false).await.unwrap();

        // Verify language breakdown
        let worktree_status = &status.repos[0].worktrees[0];
        assert_eq!(worktree_status.languages.len(), 3);
        assert_eq!(worktree_status.languages.get("rust"), Some(&2));
        assert_eq!(worktree_status.languages.get("python"), Some(&1));
        assert_eq!(worktree_status.languages.get("go"), Some(&1));
    }

    #[test]
    fn test_format_text_full_output() {
        let mut languages = HashMap::new();
        languages.insert("md".to_string(), 122);
        languages.insert("json".to_string(), 36);
        languages.insert("py".to_string(), 12);

        let status = StatusResponse {
            repos: vec![RepoStatus {
                name: "manifoldlogic/claude-code-plugins".to_string(),
                worktrees: vec![WorktreeStatus {
                    name: "main".to_string(),
                    chunk_count: 6276,
                    embedding_count: 6226,
                    embedding_percentage: 99.2,
                    languages,
                    last_updated: Some("2026-02-04 00:59:41".to_string()),
                }],
            }],
            index_size_bytes: Some(1_405_456), // ~1.34 MB
        };

        let output = format_text(&status, false);
        assert!(output.contains("Repository: manifoldlogic/claude-code-plugins"));
        assert!(output.contains("Worktree: main"));
        assert!(output.contains("Chunks: 6,276"));
        assert!(output.contains("Embeddings: 6,226 (99.2%)"));
        assert!(output.contains("Languages: md (122), json (36), py (12)"));
        assert!(output.contains("Last scan: 2026-02-04 00:59:41"));
        assert!(output.contains("Total index size: 1.34 MB"));
    }

    #[test]
    fn test_format_text_zero_chunks() {
        let status = StatusResponse {
            repos: vec![RepoStatus {
                name: "test-repo".to_string(),
                worktrees: vec![WorktreeStatus {
                    name: "main".to_string(),
                    chunk_count: 0,
                    embedding_count: 0,
                    embedding_percentage: 0.0,
                    languages: HashMap::new(),
                    last_updated: None,
                }],
            }],
            index_size_bytes: None,
        };

        let output = format_text(&status, false);
        assert!(output.contains("Chunks: 0"));
        assert!(output.contains("Embeddings: 0 (0.0%)"));
        assert!(output.contains("Languages: (none)"));
        assert!(output.contains("Last scan: never"));
        assert!(!output.contains("Total index size"));
    }

    #[test]
    fn test_format_text_verbose_shows_all_languages() {
        let mut languages = HashMap::new();
        for i in 0..10 {
            languages.insert(format!("lang{}", i), i as i64);
        }

        let status = StatusResponse {
            repos: vec![RepoStatus {
                name: "test-repo".to_string(),
                worktrees: vec![WorktreeStatus {
                    name: "main".to_string(),
                    chunk_count: 100,
                    embedding_count: 100,
                    embedding_percentage: 100.0,
                    languages,
                    last_updated: Some("2026-02-04 00:00:00".to_string()),
                }],
            }],
            index_size_bytes: None,
        };

        let output = format_text(&status, true);
        // Should show all 10 languages in verbose mode
        assert!(output.contains("lang0"));
        assert!(output.contains("lang9"));
        assert!(!output.contains("...and"));
    }

    #[test]
    fn test_format_text_truncates_languages_non_verbose() {
        let mut languages = HashMap::new();
        for i in 0..10 {
            languages.insert(format!("lang{}", i), (10 - i) as i64); // Descending counts
        }

        let status = StatusResponse {
            repos: vec![RepoStatus {
                name: "test-repo".to_string(),
                worktrees: vec![WorktreeStatus {
                    name: "main".to_string(),
                    chunk_count: 100,
                    embedding_count: 100,
                    embedding_percentage: 100.0,
                    languages,
                    last_updated: Some("2026-02-04 00:00:00".to_string()),
                }],
            }],
            index_size_bytes: None,
        };

        let output = format_text(&status, false);
        // Should show only top 5 and truncation indicator
        assert!(output.contains("...and 5 more (use --verbose)"));
    }

    #[test]
    fn test_format_text_exactly_five_languages_no_truncation() {
        let mut languages = HashMap::new();
        languages.insert("lang1".to_string(), 5);
        languages.insert("lang2".to_string(), 4);
        languages.insert("lang3".to_string(), 3);
        languages.insert("lang4".to_string(), 2);
        languages.insert("lang5".to_string(), 1);

        let status = StatusResponse {
            repos: vec![RepoStatus {
                name: "test-repo".to_string(),
                worktrees: vec![WorktreeStatus {
                    name: "main".to_string(),
                    chunk_count: 100,
                    embedding_count: 100,
                    embedding_percentage: 100.0,
                    languages,
                    last_updated: Some("2026-02-04 00:00:00".to_string()),
                }],
            }],
            index_size_bytes: None,
        };

        let output = format_text(&status, false);
        // Exactly 5 languages should show no truncation indicator
        assert!(!output.contains("...and"));
        assert!(output.contains("lang1 (5)"));
        assert!(output.contains("lang5 (1)"));
    }

    #[test]
    fn test_format_text_six_languages_shows_truncation() {
        let mut languages = HashMap::new();
        for i in 0..6 {
            languages.insert(format!("lang{}", i), (6 - i) as i64);
        }

        let status = StatusResponse {
            repos: vec![RepoStatus {
                name: "test-repo".to_string(),
                worktrees: vec![WorktreeStatus {
                    name: "main".to_string(),
                    chunk_count: 100,
                    embedding_count: 100,
                    embedding_percentage: 100.0,
                    languages,
                    last_updated: Some("2026-02-04 00:00:00".to_string()),
                }],
            }],
            index_size_bytes: None,
        };

        let output = format_text(&status, false);
        // 6 languages should show truncation with "...and 1 more"
        assert!(output.contains("...and 1 more (use --verbose)"));
    }

    #[test]
    fn test_format_text_multi_repo_shows_index_size_once() {
        let status = StatusResponse {
            repos: vec![
                RepoStatus {
                    name: "repo1".to_string(),
                    worktrees: vec![WorktreeStatus {
                        name: "main".to_string(),
                        chunk_count: 100,
                        embedding_count: 100,
                        embedding_percentage: 100.0,
                        languages: HashMap::new(),
                        last_updated: Some("2026-02-04 00:00:00".to_string()),
                    }],
                },
                RepoStatus {
                    name: "repo2".to_string(),
                    worktrees: vec![WorktreeStatus {
                        name: "main".to_string(),
                        chunk_count: 200,
                        embedding_count: 200,
                        embedding_percentage: 100.0,
                        languages: HashMap::new(),
                        last_updated: Some("2026-02-04 00:00:00".to_string()),
                    }],
                },
            ],
            index_size_bytes: Some(2_097_152), // 2.00 MB
        };

        let output = format_text(&status, false);
        // Should show index size exactly once at the end
        let size_occurrences = output.matches("Total index size:").count();
        assert_eq!(size_occurrences, 1);
        assert!(output.contains("Total index size: 2.00 MB"));
    }

    #[test]
    fn test_format_size_mb() {
        assert_eq!(format_size_mb(0), "0.00 MB");
        assert_eq!(format_size_mb(1_048_576), "1.00 MB");
        assert_eq!(format_size_mb(1_405_456), "1.34 MB");
        assert_eq!(format_size_mb(2_097_152), "2.00 MB");
        assert_eq!(format_size_mb(10_485_760), "10.00 MB");
        assert_eq!(format_size_mb(1_073_741_824), "1024.00 MB");
    }

    #[test]
    fn test_language_sorting_by_count_then_alphabetically() {
        let mut languages = HashMap::new();
        languages.insert("python".to_string(), 10);
        languages.insert("rust".to_string(), 10); // Same count as python
        languages.insert("javascript".to_string(), 5);
        languages.insert("go".to_string(), 5); // Same count as javascript

        let status = StatusResponse {
            repos: vec![RepoStatus {
                name: "test-repo".to_string(),
                worktrees: vec![WorktreeStatus {
                    name: "main".to_string(),
                    chunk_count: 100,
                    embedding_count: 100,
                    embedding_percentage: 100.0,
                    languages,
                    last_updated: Some("2026-02-04 00:00:00".to_string()),
                }],
            }],
            index_size_bytes: None,
        };

        let output = format_text(&status, false);
        // Should be sorted: python (10), rust (10), go (5), javascript (5)
        // (descending by count, then alphabetically for ties)
        let lang_line = output.lines().find(|l| l.contains("Languages:")).unwrap();
        let python_pos = lang_line.find("python").unwrap();
        let rust_pos = lang_line.find("rust").unwrap();
        let go_pos = lang_line.find("go").unwrap();
        let javascript_pos = lang_line.find("javascript").unwrap();

        // Higher counts come first
        assert!(python_pos < go_pos);
        assert!(rust_pos < javascript_pos);

        // Within same count, alphabetical order
        assert!(python_pos < rust_pos); // 'p' < 'r'
        assert!(go_pos < javascript_pos); // 'g' < 'j'
    }
}