tokmd 1.11.0

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

use anyhow::{Context, Result};
use console::style;
use dialoguer::{Confirm, Input, Select, theme::ColorfulTheme};
use std::path::Path;

/// Result of the init wizard.
#[derive(Debug, Clone)]
pub struct WizardResult {
    /// Project type selected.
    pub project_type: ProjectType,

    /// Module roots (comma-separated directories).
    pub module_roots: Vec<String>,

    /// Module depth.
    pub module_depth: usize,

    /// Context budget (token count).
    pub context_budget: String,

    /// Whether to write a tokmd.toml file.
    pub write_config: bool,

    /// Whether to write a .tokeignore file.
    pub write_tokeignore: bool,
}

/// Supported project types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProjectType {
    Rust,
    Node,
    Python,
    Go,
    Cpp,
    Mono,
    Other,
}

impl ProjectType {
    /// Get the string representation of the project type.
    pub fn as_str(&self) -> &'static str {
        match self {
            ProjectType::Rust => "rust",
            ProjectType::Node => "node",
            ProjectType::Python => "python",
            ProjectType::Go => "go",
            ProjectType::Cpp => "cpp",
            ProjectType::Mono => "mono",
            ProjectType::Other => "default",
        }
    }

    /// Get the default module roots for this project type.
    pub fn default_module_roots(&self) -> Vec<String> {
        match self {
            ProjectType::Rust => vec!["crates".to_string(), "src".to_string()],
            ProjectType::Node => vec![
                "packages".to_string(),
                "apps".to_string(),
                "src".to_string(),
            ],
            ProjectType::Python => vec!["src".to_string(), "lib".to_string()],
            ProjectType::Go => vec!["cmd".to_string(), "pkg".to_string(), "internal".to_string()],
            ProjectType::Cpp => vec!["src".to_string(), "include".to_string(), "lib".to_string()],
            ProjectType::Mono => vec![
                "packages".to_string(),
                "apps".to_string(),
                "libs".to_string(),
            ],
            ProjectType::Other => vec!["src".to_string()],
        }
    }
}

/// Map a selection index to a project type.
///
/// This is the pure logic extracted from `run_init_wizard` to enable deterministic testing.
/// Returns `None` if index is `None` (user cancelled).
pub fn index_to_project_type(index: Option<usize>) -> Option<ProjectType> {
    match index {
        Some(0) => Some(ProjectType::Rust),
        Some(1) => Some(ProjectType::Node),
        Some(2) => Some(ProjectType::Python),
        Some(3) => Some(ProjectType::Go),
        Some(4) => Some(ProjectType::Cpp),
        Some(5) => Some(ProjectType::Mono),
        Some(6) => Some(ProjectType::Other),
        None => None,                        // User cancelled
        Some(_) => Some(ProjectType::Other), // Fallback for out-of-bounds
    }
}

/// Pure logic for determining wizard result from collected answers.
///
/// This is extracted from `run_init_wizard` to enable deterministic testing.
/// Returns `None` if:
/// - `project_type` is `None` (user cancelled selection)
/// - Both `write_config` and `write_tokeignore` are `false` (nothing to write)
pub fn wizard_result_from_answers(
    project_type: Option<ProjectType>,
    module_roots: Vec<String>,
    module_depth: usize,
    context_budget: String,
    write_config: bool,
    write_tokeignore: bool,
) -> Option<WizardResult> {
    let project_type = project_type?;

    if !write_config && !write_tokeignore {
        return None;
    }

    Some(WizardResult {
        project_type,
        module_roots,
        module_depth,
        context_budget,
        write_config,
        write_tokeignore,
    })
}

/// Run the interactive init wizard.
///
/// Returns `Some(WizardResult)` if the user completes the wizard,
/// or `None` if they cancel.
pub fn run_init_wizard(_dir: &Path) -> Result<Option<WizardResult>> {
    let theme = ColorfulTheme::default();

    // Welcome message
    eprintln!();
    eprintln!("{}", style("Welcome to tokmd init wizard!").bold().cyan());
    eprintln!("This wizard will help you configure tokmd for your project.");
    eprintln!();

    // Project type selection
    let project_types = &[
        "Rust (crates/, src/)",
        "Node.js (packages/, apps/, src/)",
        "Python (src/, lib/)",
        "Go (cmd/, pkg/, internal/)",
        "C/C++ (src/, include/, lib/)",
        "Monorepo (packages/, apps/, libs/)",
        "Other",
    ];

    let selection = Select::with_theme(&theme)
        .with_prompt("What type of project is this?")
        .items(project_types)
        .default(0)
        .interact_opt()
        .context("Failed to get project type selection")?;

    let project_type = match index_to_project_type(selection) {
        Some(pt) => pt,
        None => return Ok(None), // User cancelled
    };

    // Module roots
    let default_roots = project_type.default_module_roots().join(", ");
    let roots_input: String = Input::with_theme(&theme)
        .with_prompt("Module roots (comma-separated directories)")
        .default(default_roots)
        .interact_text()
        .context("Failed to get module roots")?;

    let module_roots: Vec<String> = roots_input
        .split(',')
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();

    // Module depth
    let module_depth: usize = Input::with_theme(&theme)
        .with_prompt("Module depth")
        .default(2)
        .interact_text()
        .context("Failed to get module depth")?;

    // Context budget
    let context_budget: String = Input::with_theme(&theme)
        .with_prompt("Context budget (tokens)")
        .default("128k".to_string())
        .interact_text()
        .context("Failed to get context budget")?;

    // Confirmation
    eprintln!();
    eprintln!("{}", style("Configuration summary:").bold());
    eprintln!("  Project type: {}", project_type.as_str());
    eprintln!("  Module roots: {}", module_roots.join(", "));
    eprintln!("  Module depth: {}", module_depth);
    eprintln!("  Context budget: {}", context_budget);
    eprintln!();

    let write_config = Confirm::with_theme(&theme)
        .with_prompt("Write tokmd.toml configuration file?")
        .default(true)
        .interact()
        .context("Failed to get config confirmation")?;

    let write_tokeignore = Confirm::with_theme(&theme)
        .with_prompt("Write .tokeignore file?")
        .default(true)
        .interact()
        .context("Failed to get tokeignore confirmation")?;

    let result = wizard_result_from_answers(
        Some(project_type),
        module_roots,
        module_depth,
        context_budget,
        write_config,
        write_tokeignore,
    );

    if result.is_none() {
        eprintln!("No files to write. Init cancelled.");
    }

    Ok(result)
}

/// Generate tokmd.toml content from wizard result.
///
/// Uses the `TomlConfig` struct to ensure output matches the schema exactly.
pub fn generate_toml_config(result: &WizardResult) -> Result<String> {
    use crate::cli::{AnalyzeConfig, ContextConfig, ExportConfig, ModuleConfig, TomlConfig};

    let config = TomlConfig {
        module: ModuleConfig {
            roots: Some(result.module_roots.clone()),
            depth: Some(result.module_depth),
            ..Default::default()
        },
        export: ExportConfig {
            format: Some("jsonl".to_string()),
            min_code: Some(10),
            ..Default::default()
        },
        context: ContextConfig {
            budget: Some(result.context_budget.clone()),
            strategy: Some("greedy".to_string()),
            ..Default::default()
        },
        analyze: AnalyzeConfig {
            preset: Some("receipt".to_string()),
            ..Default::default()
        },
        ..Default::default()
    };

    let toml_content =
        toml::to_string_pretty(&config).context("Failed to serialize configuration to TOML")?;

    Ok(format!(
        "# tokmd configuration\n\
         # Generated by tokmd init\n\n\
         {toml_content}"
    ))
}

/// Map project type to InitProfile.
pub fn project_type_to_profile(project_type: ProjectType) -> crate::cli::InitProfile {
    match project_type {
        ProjectType::Rust => crate::cli::InitProfile::Rust,
        ProjectType::Node => crate::cli::InitProfile::Node,
        ProjectType::Python => crate::cli::InitProfile::Python,
        ProjectType::Go => crate::cli::InitProfile::Go,
        ProjectType::Cpp => crate::cli::InitProfile::Cpp,
        ProjectType::Mono => crate::cli::InitProfile::Mono,
        ProjectType::Other => crate::cli::InitProfile::Default,
    }
}

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

    // ==================== ProjectType::as_str() tests ====================

    #[test]
    fn test_as_str_rust() {
        assert_eq!(ProjectType::Rust.as_str(), "rust");
    }

    #[test]
    fn test_as_str_node() {
        assert_eq!(ProjectType::Node.as_str(), "node");
    }

    #[test]
    fn test_as_str_python() {
        assert_eq!(ProjectType::Python.as_str(), "python");
    }

    #[test]
    fn test_as_str_go() {
        assert_eq!(ProjectType::Go.as_str(), "go");
    }

    #[test]
    fn test_as_str_cpp() {
        assert_eq!(ProjectType::Cpp.as_str(), "cpp");
    }

    #[test]
    fn test_as_str_mono() {
        assert_eq!(ProjectType::Mono.as_str(), "mono");
    }

    #[test]
    fn test_as_str_other() {
        assert_eq!(ProjectType::Other.as_str(), "default");
    }

    // ==================== ProjectType::default_module_roots() tests ====================

    #[test]
    fn test_default_roots_rust() {
        assert_eq!(
            ProjectType::Rust.default_module_roots(),
            vec!["crates".to_string(), "src".to_string()]
        );
    }

    #[test]
    fn test_default_roots_node() {
        assert_eq!(
            ProjectType::Node.default_module_roots(),
            vec![
                "packages".to_string(),
                "apps".to_string(),
                "src".to_string()
            ]
        );
    }

    #[test]
    fn test_default_roots_python() {
        assert_eq!(
            ProjectType::Python.default_module_roots(),
            vec!["src".to_string(), "lib".to_string()]
        );
    }

    #[test]
    fn test_default_roots_go() {
        assert_eq!(
            ProjectType::Go.default_module_roots(),
            vec!["cmd".to_string(), "pkg".to_string(), "internal".to_string()]
        );
    }

    #[test]
    fn test_default_roots_cpp() {
        assert_eq!(
            ProjectType::Cpp.default_module_roots(),
            vec!["src".to_string(), "include".to_string(), "lib".to_string()]
        );
    }

    #[test]
    fn test_default_roots_mono() {
        assert_eq!(
            ProjectType::Mono.default_module_roots(),
            vec![
                "packages".to_string(),
                "apps".to_string(),
                "libs".to_string()
            ]
        );
    }

    #[test]
    fn test_default_roots_other() {
        assert_eq!(
            ProjectType::Other.default_module_roots(),
            vec!["src".to_string()]
        );
    }

    // ==================== project_type_to_profile() tests ====================

    #[test]
    fn test_profile_rust() {
        assert_eq!(
            project_type_to_profile(ProjectType::Rust),
            crate::cli::InitProfile::Rust
        );
    }

    #[test]
    fn test_profile_node() {
        assert_eq!(
            project_type_to_profile(ProjectType::Node),
            crate::cli::InitProfile::Node
        );
    }

    #[test]
    fn test_profile_python() {
        assert_eq!(
            project_type_to_profile(ProjectType::Python),
            crate::cli::InitProfile::Python
        );
    }

    #[test]
    fn test_profile_go() {
        assert_eq!(
            project_type_to_profile(ProjectType::Go),
            crate::cli::InitProfile::Go
        );
    }

    #[test]
    fn test_profile_cpp() {
        assert_eq!(
            project_type_to_profile(ProjectType::Cpp),
            crate::cli::InitProfile::Cpp
        );
    }

    #[test]
    fn test_profile_mono() {
        assert_eq!(
            project_type_to_profile(ProjectType::Mono),
            crate::cli::InitProfile::Mono
        );
    }

    #[test]
    fn test_profile_other_maps_to_default() {
        assert_eq!(
            project_type_to_profile(ProjectType::Other),
            crate::cli::InitProfile::Default
        );
    }

    // ==================== Legacy tests ====================

    #[test]
    fn test_project_type_defaults() {
        assert!(!ProjectType::Rust.default_module_roots().is_empty());
        assert!(!ProjectType::Node.default_module_roots().is_empty());
    }

    #[test]
    fn test_generate_config() {
        let result = WizardResult {
            project_type: ProjectType::Rust,
            module_roots: vec!["crates".to_string(), "src".to_string()],
            module_depth: 2,
            context_budget: "128k".to_string(),
            write_config: true,
            write_tokeignore: true,
        };

        let config = generate_toml_config(&result).expect("should generate config");

        // Check header comment
        assert!(config.contains("# tokmd configuration"));
        assert!(config.contains("# Generated by tokmd init"));

        // Check module section
        assert!(config.contains("[module]"));
        assert!(config.contains("\"crates\""));
        assert!(config.contains("\"src\""));
        assert!(config.contains("depth = 2"));

        // Check export section
        assert!(config.contains("[export]"));
        assert!(config.contains("format = \"jsonl\""));
        assert!(config.contains("min_code = 10"));

        // Check context section
        assert!(config.contains("[context]"));
        assert!(config.contains("budget = \"128k\""));
        assert!(config.contains("strategy = \"greedy\""));

        // Check analyze section
        assert!(config.contains("[analyze]"));
        assert!(config.contains("preset = \"receipt\""));

        // Verify it's valid TOML by parsing
        let parsed: crate::cli::TomlConfig =
            toml::from_str(&config).expect("generated config should be valid TOML");
        assert_eq!(parsed.module.depth, Some(2));
        assert_eq!(parsed.context.budget, Some("128k".to_string()));
    }

    // ==================== index_to_project_type() tests ====================

    #[test]
    fn test_index_to_project_type_rust() {
        assert_eq!(index_to_project_type(Some(0)), Some(ProjectType::Rust));
    }

    #[test]
    fn test_index_to_project_type_node() {
        assert_eq!(index_to_project_type(Some(1)), Some(ProjectType::Node));
    }

    #[test]
    fn test_index_to_project_type_python() {
        assert_eq!(index_to_project_type(Some(2)), Some(ProjectType::Python));
    }

    #[test]
    fn test_index_to_project_type_go() {
        assert_eq!(index_to_project_type(Some(3)), Some(ProjectType::Go));
    }

    #[test]
    fn test_index_to_project_type_cpp() {
        assert_eq!(index_to_project_type(Some(4)), Some(ProjectType::Cpp));
    }

    #[test]
    fn test_index_to_project_type_mono() {
        assert_eq!(index_to_project_type(Some(5)), Some(ProjectType::Mono));
    }

    #[test]
    fn test_index_to_project_type_other() {
        assert_eq!(index_to_project_type(Some(6)), Some(ProjectType::Other));
    }

    #[test]
    fn test_index_to_project_type_none_returns_none() {
        // Kills "delete None arm" mutant - user cancellation
        assert_eq!(index_to_project_type(None), None);
    }

    #[test]
    fn test_index_to_project_type_out_of_bounds_returns_other() {
        // Kills "delete fallback arm" mutant
        assert_eq!(index_to_project_type(Some(100)), Some(ProjectType::Other));
        assert_eq!(index_to_project_type(Some(7)), Some(ProjectType::Other));
    }

    // ==================== wizard_result_from_answers() tests ====================

    #[test]
    fn test_wizard_result_happy_path() -> anyhow::Result<()> {
        // Kills "wizard_result_from_answers -> None" mutant
        let result = wizard_result_from_answers(
            Some(ProjectType::Rust),
            vec!["crates".to_string()],
            2,
            "128k".to_string(),
            true,
            true,
        );
        assert!(result.is_some());
        let r = result.ok_or_else(|| anyhow::anyhow!("Expected Some"))?;
        assert_eq!(r.project_type, ProjectType::Rust);
        assert_eq!(r.module_roots, vec!["crates"]);
        assert_eq!(r.module_depth, 2);
        assert_eq!(r.context_budget, "128k");
        assert!(r.write_config);
        assert!(r.write_tokeignore);
        Ok(())
    }

    #[test]
    fn test_wizard_result_none_project_type_returns_none() {
        // User cancelled project type selection
        let result = wizard_result_from_answers(
            None,
            vec!["src".to_string()],
            2,
            "128k".to_string(),
            true,
            true,
        );
        assert!(result.is_none());
    }

    #[test]
    fn test_wizard_result_no_files_to_write_returns_none() {
        // Kills "delete !write_config && !write_tokeignore check" mutant
        let result = wizard_result_from_answers(
            Some(ProjectType::Rust),
            vec!["src".to_string()],
            2,
            "128k".to_string(),
            false,
            false,
        );
        assert!(result.is_none());
    }

    #[test]
    fn test_wizard_result_only_config_returns_some() -> anyhow::Result<()> {
        let result = wizard_result_from_answers(
            Some(ProjectType::Python),
            vec!["src".to_string()],
            1,
            "64k".to_string(),
            true,
            false,
        );
        assert!(result.is_some());
        let r = result.ok_or_else(|| anyhow::anyhow!("Expected Some"))?;
        assert!(r.write_config);
        assert!(!r.write_tokeignore);
        Ok(())
    }

    #[test]
    fn test_wizard_result_only_tokeignore_returns_some() -> anyhow::Result<()> {
        let result = wizard_result_from_answers(
            Some(ProjectType::Node),
            vec!["packages".to_string()],
            3,
            "256k".to_string(),
            false,
            true,
        );
        assert!(result.is_some());
        let r = result.ok_or_else(|| anyhow::anyhow!("Expected Some"))?;
        assert!(!r.write_config);
        assert!(r.write_tokeignore);
        Ok(())
    }

    #[test]
    fn test_wizard_result_preserves_all_fields() -> anyhow::Result<()> {
        let result = wizard_result_from_answers(
            Some(ProjectType::Go),
            vec!["cmd".to_string(), "pkg".to_string()],
            5,
            "1m".to_string(),
            true,
            false,
        );
        let r = result.ok_or_else(|| anyhow::anyhow!("Expected Some"))?;
        assert_eq!(r.project_type, ProjectType::Go);
        assert_eq!(r.module_roots, vec!["cmd", "pkg"]);
        assert_eq!(r.module_depth, 5);
        assert_eq!(r.context_budget, "1m");
        Ok(())
    }

    // ==================== Mutant killer: as_str not empty/xyzzy ====================

    #[test]
    fn test_as_str_not_empty() {
        // Kills "as_str -> empty string" mutants
        assert!(!ProjectType::Rust.as_str().is_empty());
        assert!(!ProjectType::Node.as_str().is_empty());
        assert!(!ProjectType::Python.as_str().is_empty());
        assert!(!ProjectType::Go.as_str().is_empty());
        assert!(!ProjectType::Cpp.as_str().is_empty());
        assert!(!ProjectType::Mono.as_str().is_empty());
        assert!(!ProjectType::Other.as_str().is_empty());
    }

    #[test]
    fn test_as_str_not_xyzzy() {
        // Kills "as_str -> xyzzy" mutants
        assert_ne!(ProjectType::Rust.as_str(), "xyzzy");
        assert_ne!(ProjectType::Node.as_str(), "xyzzy");
        assert_ne!(ProjectType::Python.as_str(), "xyzzy");
        assert_ne!(ProjectType::Go.as_str(), "xyzzy");
        assert_ne!(ProjectType::Cpp.as_str(), "xyzzy");
        assert_ne!(ProjectType::Mono.as_str(), "xyzzy");
        assert_ne!(ProjectType::Other.as_str(), "xyzzy");
    }

    // ==================== Mutant killer: default_module_roots not empty/xyzzy ====================

    #[test]
    fn test_default_roots_not_empty() {
        // Kills "default_module_roots -> vec![]" mutants
        assert!(!ProjectType::Rust.default_module_roots().is_empty());
        assert!(!ProjectType::Node.default_module_roots().is_empty());
        assert!(!ProjectType::Python.default_module_roots().is_empty());
        assert!(!ProjectType::Go.default_module_roots().is_empty());
        assert!(!ProjectType::Cpp.default_module_roots().is_empty());
        assert!(!ProjectType::Mono.default_module_roots().is_empty());
        assert!(!ProjectType::Other.default_module_roots().is_empty());
    }

    #[test]
    fn test_default_roots_not_xyzzy() {
        // Kills "default_module_roots -> vec![xyzzy]" mutants
        for pt in [
            ProjectType::Rust,
            ProjectType::Node,
            ProjectType::Python,
            ProjectType::Go,
            ProjectType::Cpp,
            ProjectType::Mono,
            ProjectType::Other,
        ] {
            let roots = pt.default_module_roots();
            assert!(
                !roots.contains(&"xyzzy".to_string()),
                "{pt:?} should not contain 'xyzzy'"
            );
        }
    }
}