vibe_coding_tracker 2.2.0

Vibe Coding Tracker - AI coding assistant telemetry/usage parser, aggregate local session data into CodeAnalysis results
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
//! Binary entry point for the `vibe_coding_tracker` (`vct`) CLI.
//!
//! Parses the subcommand with clap and dispatches to the library crate:
//! batch `analysis` and `usage` views (TUI / table / text / JSON with a
//! time-range filter), complete single-file analysis, plus `version` and
//! `update`. The heavy lifting lives in [`vibe_coding_tracker`]; this file is
//! wiring.
//!
//! Two things run *before* clap on purpose, and the ordering is
//! load-bearing — see [`main`].

use anyhow::{Context, Result, bail};
use clap::Parser;
use comfy_table::{Cell, CellAlignment, Color, ContentArrangement, Table, presets::UTF8_FULL};
use owo_colors::OwoColorize;
use serde::Serialize;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::io::{self, Write};
use std::sync::Arc;
use vibe_coding_tracker::cli::{Cli, Commands, ConfigAction, resolve_time_range_with_default};

// mimalloc is opt-in behind the `mimalloc` cargo feature. The default build
// uses the system allocator because mimalloc's lazy purge retains freed
// pages — the RSS difference on the TUI loops (repeated parse of session
// directories) was roughly 10× in favour of the system allocator. Users who
// want mimalloc's speed for short one-shot runs can rebuild with
// `--features mimalloc`.
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
use vibe_coding_tracker::display::usage::{
    display_usage_interactive_with_pool, display_usage_table, display_usage_text,
};
use vibe_coding_tracker::get_version_info;
use vibe_coding_tracker::pricing::{ModelPricingMap, fetch_model_pricing};
use vibe_coding_tracker::session::{ParseMode, parse_session_file_typed_with_mode_and_diagnostics};
use vibe_coding_tracker::summary_cache::build_scan_pool;
use vibe_coding_tracker::usage::{
    CostSource, get_usage_from_directories_with_diagnostics, resolve_model_cost,
};
use vibe_coding_tracker::utils::extract_token_counts;

/// Parses the CLI and runs the selected subcommand.
///
/// Two steps run before `Cli::parse()` and must stay in this order:
/// `tune_system_allocator()` caps glibc arenas before the first allocation
/// (a Rayon worker), and the `--version` / `-V` branch is handled by hand so
/// the bare flag prints just [`vibe_coding_tracker::VERSION`] without going
/// through clap (the `version` *subcommand* still renders the full table).
///
/// # Errors
///
/// Propagates any failure from the dispatched subcommand: session parsing,
/// JSON (de)serialization, terminal/TUI errors, or the network and
/// binary-replacement errors raised by `update`. Pricing
/// fetch failure in `usage --json` is downgraded to a warning rather than an
/// error, so costs are reported as unavailable instead of aborting.
fn main() -> Result<()> {
    // Cap per-thread glibc arenas and pin the trim threshold before any
    // allocation happens under a Rayon worker. See `tune_system_allocator`
    // for why this matters on long TUI sessions.
    vibe_coding_tracker::utils::tune_system_allocator();

    // Install the file logger (default level `warn`) before anything can log or
    // panic. It writes only to `~/.vct/logs/`, never the terminal.
    vibe_coding_tracker::logging::init();

    if matches!(
        std::env::args_os().nth(1).and_then(|arg| arg.into_string().ok()),
        Some(arg) if arg == "--version" || arg == "-V"
    ) {
        println!("{}", vibe_coding_tracker::VERSION);
        return Ok(());
    }

    let result = run();
    if let Err(error) = &result {
        // Record the final error before anyhow prints it to stderr and the
        // process exits — the log file is the durable record of the failure.
        log::error!("command failed: {error:#}");
    }
    result
}

/// Parses the CLI and dispatches the selected subcommand.
fn run() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Analysis {
            file,
            json,
            text,
            table,
            daily,
            weekly,
            monthly,
            all,
        } => {
            match file {
                Some(file_path) => {
                    let complete_json = json || (!text && !table);
                    let mode = if complete_json {
                        ParseMode::Full
                    } else {
                        ParseMode::UsageOnly
                    };
                    let (analysis, diagnostics) =
                        parse_session_file_typed_with_mode_and_diagnostics(&file_path, mode)?;
                    if diagnostics.skipped_records() > 0 {
                        eprintln!(
                            "Warning: Skipped {} malformed or unsupported analyzer records while parsing {}. Successful results are still shown.",
                            diagnostics.skipped_records(),
                            file_path.display()
                        );
                    }
                    if complete_json {
                        write_pretty_json(&analysis)?;
                    } else if text {
                        let projected =
                            vibe_coding_tracker::analysis::project_code_analysis(&analysis);
                        vibe_coding_tracker::display::analysis::display_analysis_text(&projected);
                    } else {
                        let projected =
                            vibe_coding_tracker::analysis::project_code_analysis(&analysis);
                        vibe_coding_tracker::display::analysis::display_analysis_table(&projected);
                    }
                }
                None => {
                    // Settings are only needed for the batch (all-sessions) path,
                    // so `analysis FILE`, `version`, `fetch`, etc. never read or
                    // create `~/.vct/config.toml`.
                    let config = vibe_coding_tracker::config::load();
                    vibe_coding_tracker::logging::apply(&config.logging);
                    let time_range = resolve_time_range_with_default(
                        daily,
                        weekly,
                        monthly,
                        all,
                        config.general.default_time_range,
                    );
                    let scan_pool =
                        Arc::new(build_scan_pool(config.performance.resolved_scan_threads())?);
                    if json {
                        let dataset = scan_pool.install(|| {
                            vibe_coding_tracker::analysis::collect_analysis_sessions_with(
                                time_range,
                                config.providers,
                                ParseMode::Full,
                            )
                        })?;
                        report_analysis_collection(&dataset.diagnostics)?;
                        write_pretty_json(&dataset)?;
                    } else if text || table {
                        let aggregation = scan_pool.install(|| {
                            vibe_coding_tracker::analysis::aggregate_sessions_by_model_with_diagnostics(
                                time_range,
                                config.providers,
                            )
                        })?;
                        report_analysis_collection(&aggregation.diagnostics)?;

                        if text {
                            vibe_coding_tracker::display::analysis::display_analysis_text(
                                &aggregation.data,
                            );
                        } else {
                            vibe_coding_tracker::display::analysis::display_analysis_table(
                                &aggregation.data,
                            );
                        }
                    } else {
                        vibe_coding_tracker::display::analysis::display_analysis_interactive_loading_with_pool(
                            time_range,
                            config.providers,
                            config.analysis.refresh_secs(),
                            scan_pool,
                        )?;
                    }
                }
            }
        }

        Commands::Usage {
            json,
            text,
            table,
            merge_providers,
            daily,
            weekly,
            monthly,
            all,
        } => {
            let config = vibe_coding_tracker::config::load();
            vibe_coding_tracker::logging::apply(&config.logging);
            let time_range = resolve_time_range_with_default(
                daily,
                weekly,
                monthly,
                all,
                config.general.default_time_range,
            );
            // A `--merge-providers` flag forces merging on; otherwise the saved
            // preference decides. The TUI's `m` toggle persists back to config.
            let merge = merge_providers || config.usage.merge_models;
            let scan_pool = Arc::new(build_scan_pool(config.performance.resolved_scan_threads())?);
            let usage_from_dirs = |tr| {
                scan_pool
                    .install(|| get_usage_from_directories_with_diagnostics(tr, config.providers))
            };

            if json {
                let usage = usage_from_dirs(time_range)?;
                report_usage_collection(&usage.diagnostics)?;
                let pricing_map = match fetch_model_pricing() {
                    Ok(map) => map,
                    Err(e) => {
                        log::warn!("failed to fetch pricing data: {e}; costs unavailable");
                        eprintln!(
                            "Warning: Failed to fetch pricing data: {}. Costs will be unavailable.",
                            e
                        );
                        ModelPricingMap::new(HashMap::new())
                    }
                };
                let enriched_data = build_enriched_json(&usage.data, &pricing_map)?;
                write_pretty_json(&enriched_data)?;
            } else if text {
                let usage = usage_from_dirs(time_range)?;
                report_usage_collection(&usage.diagnostics)?;
                display_usage_text(&usage.data, merge);
            } else if table {
                let usage = usage_from_dirs(time_range)?;
                report_usage_collection(&usage.diagnostics)?;
                display_usage_table(&usage.data, merge);
            } else {
                // `config` is not used after this, so hand the panel list off by
                // move; read both cadences first so the borrows end before the
                // partial move out of `config.usage`.
                let refresh = config.usage.refresh_secs();
                let quota_refresh = config.usage.quota_refresh_secs();
                display_usage_interactive_with_pool(
                    time_range,
                    merge,
                    config.usage.quota.panels,
                    config.providers,
                    refresh,
                    quota_refresh,
                    scan_pool,
                )?;
            }
        }

        Commands::Version { json, text } => {
            let version_info = get_version_info();

            if json {
                let json_output = serde_json::json!({
                    "Version": version_info.version,
                    "Rust Version": version_info.rust_version,
                    "Cargo Version": version_info.cargo_version
                });
                println!("{}", serde_json::to_string_pretty(&json_output)?);
            } else if text {
                println!("Version: {}", version_info.version);
                println!("Rust Version: {}", version_info.rust_version);
                println!("Cargo Version: {}", version_info.cargo_version);
            } else {
                println!("{}", "Vibe Coding Tracker".bright_cyan().bold());
                println!();

                let mut table = Table::new();
                table
                    .load_preset(UTF8_FULL)
                    .set_content_arrangement(ContentArrangement::Dynamic)
                    .add_row(vec![
                        Cell::new("Version")
                            .fg(Color::Green)
                            .set_alignment(CellAlignment::Left),
                        Cell::new(&version_info.version)
                            .fg(Color::White)
                            .set_alignment(CellAlignment::Left),
                    ])
                    .add_row(vec![
                        Cell::new("Rust Version")
                            .fg(Color::Green)
                            .set_alignment(CellAlignment::Left),
                        Cell::new(&version_info.rust_version)
                            .fg(Color::White)
                            .set_alignment(CellAlignment::Left),
                    ])
                    .add_row(vec![
                        Cell::new("Cargo Version")
                            .fg(Color::Green)
                            .set_alignment(CellAlignment::Left),
                        Cell::new(&version_info.cargo_version)
                            .fg(Color::White)
                            .set_alignment(CellAlignment::Left),
                    ]);

                println!("{table}");
            }
        }

        Commands::Update { check, force } => {
            if check {
                vibe_coding_tracker::update::check_update()?;
            } else {
                vibe_coding_tracker::update::update_interactive(force)?;
            }
        }

        Commands::Fetch {
            provider,
            text,
            table,
            ..
        } => {
            vibe_coding_tracker::fetch::run(provider, text, table)?;
        }

        Commands::Config { action } => {
            run_config(action.unwrap_or(ConfigAction::Show))?;
        }
    }

    Ok(())
}

/// Handles the `config` subcommand: print the path, show current settings, open
/// the file in the user's editor, or print the JSON schema.
fn run_config(action: ConfigAction) -> Result<()> {
    match action {
        // Pure stdout: schema generation needs no config file, so it must not
        // resolve (and thereby create) ~/.vct — keep it usable on a read-only home.
        ConfigAction::Schema => print!("{}", vibe_coding_tracker::config::schema_json()),
        ConfigAction::Path => {
            println!(
                "{}",
                vibe_coding_tracker::utils::get_config_path()?.display()
            );
        }
        ConfigAction::Show => {
            let path = vibe_coding_tracker::utils::get_config_path()?;
            // Ensure the file exists (first-run creation) before reading it back.
            let _ = vibe_coding_tracker::config::load();
            let contents = std::fs::read_to_string(&path).unwrap_or_default();
            println!("{}", "Vibe Coding Tracker settings".bright_cyan().bold());
            println!("{}", path.display().dimmed());
            println!();
            print!("{}", contents);
        }
        ConfigAction::Edit => {
            let path = vibe_coding_tracker::utils::get_config_path()?;
            // Ensure the file exists before handing it to the editor.
            let _ = vibe_coding_tracker::config::load();
            let editor = std::env::var("VISUAL")
                .or_else(|_| std::env::var("EDITOR"))
                .unwrap_or_else(|_| default_editor().to_string());
            // `$EDITOR` / `$VISUAL` often carry arguments (`code --wait`, `vim -f`),
            // so split into program + args rather than treating the whole string as
            // one executable name.
            let mut parts = editor.split_whitespace();
            let program = parts
                .next()
                .ok_or_else(|| anyhow::anyhow!("empty editor command"))?;
            let status = std::process::Command::new(program)
                .args(parts)
                .arg(&path)
                .status()
                .with_context(|| format!("Failed to launch editor '{}'", editor))?;
            // Surface an aborted / failed editor as a non-zero CLI exit so scripts
            // (and users) can tell the edit did not complete cleanly.
            if !status.success() {
                anyhow::bail!("Editor '{}' exited with {}", editor, status);
            }
        }
        ConfigAction::Migrate => {
            use vibe_coding_tracker::config::MigrationStatus;
            let path = vibe_coding_tracker::utils::get_config_path()?;
            match vibe_coding_tracker::config::migrate_config_file(&path)? {
                MigrationStatus::Created => {
                    println!("Created a new config at {}", path.display());
                }
                MigrationStatus::Migrated => {
                    println!("Migrated config to the latest format: {}", path.display());
                }
                MigrationStatus::AlreadyCurrent => {
                    println!("Config is already up to date: {}", path.display());
                }
            }
        }
    }
    Ok(())
}

/// Writes one pretty-printed JSON value to stdout followed by a newline.
fn write_pretty_json(value: &impl Serialize) -> Result<()> {
    let stdout = io::stdout();
    let mut writer = stdout.lock();
    serde_json::to_writer_pretty(&mut writer, value)?;
    writeln!(writer)?;
    Ok(())
}

/// Rejects a completely failed noninteractive scan and reports partial data.
fn report_analysis_collection(
    diagnostics: &vibe_coding_tracker::analysis::AnalysisCollectionDiagnostics,
) -> Result<()> {
    let Some(first) = diagnostics.failures.first() else {
        return Ok(());
    };
    if diagnostics.all_failed() {
        bail!(
            "failed to parse all {} analysis sources; first failure: {} {}: {}",
            diagnostics.candidates,
            first.provider,
            first.source.display(),
            first.error
        );
    }
    if diagnostics.partially_failed() {
        eprintln!(
            "Warning: Encountered {} analysis source failures while scanning {} candidates. Successful results are still shown. First failure: {} {}: {}",
            diagnostics.failures.len(),
            diagnostics.candidates,
            first.provider,
            first.source.display(),
            first.error
        );
    }
    Ok(())
}

/// Rejects a completely failed noninteractive usage scan and reports partial data.
fn report_usage_collection(
    diagnostics: &vibe_coding_tracker::usage::UsageCollectionDiagnostics,
) -> Result<()> {
    let Some(first) = diagnostics.failures.first() else {
        return Ok(());
    };
    if diagnostics.all_failed() {
        bail!(
            "failed to read all {} usage sources; first failure: {} {}: {}",
            diagnostics.candidates,
            first.provider,
            first.source.display(),
            first.error
        );
    }
    if diagnostics.partially_failed() {
        eprintln!(
            "Warning: Encountered {} usage source failures while scanning {} candidates. Successful results are still shown. First failure: {} {}: {}",
            diagnostics.failures.len(),
            diagnostics.candidates,
            first.provider,
            first.source.display(),
            first.error
        );
    }
    Ok(())
}

/// The fallback editor when neither `$VISUAL` nor `$EDITOR` is set.
fn default_editor() -> &'static str {
    if cfg!(windows) { "notepad" } else { "vi" }
}

/// Builds the `usage --json` payload, joining each model's token counts with
/// its priced cost.
///
/// For every model it resolves the USD cost via
/// [`resolve_model_cost`](vibe_coding_tracker::usage::resolve_model_cost) and
/// emits a JSON object with `model`, `usage`, `cost_usd`, and (when a non-exact
/// LiteLLM key was used) `matched_model`. OpenCode models without an exact
/// price report OpenCode's own stored cost only for the OpenCode portion of a
/// merged row rather than applying it to other providers with the same model.
///
/// # Errors
///
/// Returns an error only if a usage value cannot be serialized into the
/// resulting JSON object.
fn build_enriched_json(
    usage_data: &vibe_coding_tracker::UsageData,
    pricing_map: &ModelPricingMap,
) -> Result<Vec<Value>> {
    let mut enriched_data = Vec::with_capacity(usage_data.models.len());

    for (model, usage) in usage_data.models.iter() {
        let (cost, matched_model) = resolve_enriched_model_cost(model, usage_data, pricing_map)
            .unwrap_or_else(|| price_usage_value(model, usage, pricing_map, CostSource::Litellm));

        let mut entry = json!({
            "model": model,
            "usage": usage,
            "cost_usd": cost
        });

        if let Some(matched) = &matched_model {
            entry["matched_model"] = json!(matched);
        }

        enriched_data.push(entry);
    }

    Ok(enriched_data)
}

/// Resolves cost for one merged JSON row from provider-scoped usage pieces.
fn resolve_enriched_model_cost(
    model: &str,
    usage_data: &vibe_coding_tracker::UsageData,
    pricing_map: &ModelPricingMap,
) -> Option<(f64, Option<String>)> {
    let mut total_cost = 0.0;
    let mut matched_model = None;
    let mut found = false;

    for usage in [
        &usage_data.per_provider.claude,
        &usage_data.per_provider.codex,
        &usage_data.per_provider.copilot,
        &usage_data.per_provider.gemini,
        &usage_data.per_provider.grok,
    ] {
        if let Some(raw_usage) = usage.get(model) {
            found = true;
            let (cost, matched) =
                price_usage_value(model, raw_usage, pricing_map, CostSource::Litellm);
            total_cost += cost;
            if matched_model.is_none() {
                matched_model = matched;
            }
        }
    }

    // OpenCode and Hermes prefer an exact LiteLLM match before their stored
    // costs. Cursor is a local token estimate, so it uses an exact LiteLLM
    // price when available and otherwise remains unpriced.
    let stored = |m: &vibe_coding_tracker::constants::FastHashMap<String, f64>| {
        m.get(model).copied().unwrap_or(0.0)
    };
    for (usage, source) in [
        (
            &usage_data.per_provider.opencode,
            CostSource::OpenCodeStored(stored(&usage_data.stored_costs.opencode)),
        ),
        (
            &usage_data.per_provider.cursor,
            CostSource::OpenCodeStored(0.0),
        ),
        (
            &usage_data.per_provider.hermes,
            CostSource::HermesStored(stored(&usage_data.stored_costs.hermes)),
        ),
    ] {
        if let Some(raw_usage) = usage.get(model) {
            found = true;
            let (cost, matched) = price_usage_value(model, raw_usage, pricing_map, source);
            total_cost += cost;
            if matched_model.is_none() {
                matched_model = matched;
            }
        }
    }

    found.then_some((total_cost, matched_model))
}

/// Prices one raw usage value under `source`.
fn price_usage_value(
    model: &str,
    usage: &Value,
    pricing_map: &ModelPricingMap,
    source: CostSource,
) -> (f64, Option<String>) {
    let counts = extract_token_counts(usage);
    resolve_model_cost(model, &counts, pricing_map, source)
}

#[cfg(test)]
mod tests {
    use super::*;
    use vibe_coding_tracker::models::{PerProviderUsage, ProviderActiveDays, UsageResult};
    use vibe_coding_tracker::pricing::{ModelPricing, clear_pricing_cache};

    #[test]
    fn json_rows_include_grok_source_cost() {
        clear_pricing_cache();
        let mut raw_pricing = HashMap::new();
        raw_pricing.insert(
            "shared-model".to_string(),
            ModelPricing {
                input_cost_per_token: 0.01,
                ..Default::default()
            },
        );
        let pricing_map = ModelPricingMap::new(raw_pricing);
        let mut models = UsageResult::default();
        models.insert("shared-model".to_string(), json!({"input_tokens": 200}));
        let mut per_provider = PerProviderUsage::default();
        per_provider
            .claude
            .insert("shared-model".to_string(), json!({"input_tokens": 100}));
        per_provider
            .grok
            .insert("shared-model".to_string(), json!({"input_tokens": 100}));
        let usage_data = vibe_coding_tracker::UsageData {
            models,
            per_provider,
            provider_days: ProviderActiveDays::default(),
            stored_costs: vibe_coding_tracker::usage::StoredCosts::default(),
        };

        let rows = build_enriched_json(&usage_data, &pricing_map).unwrap();

        assert!((rows[0]["cost_usd"].as_f64().unwrap() - 2.0).abs() < 1e-9);
    }

    #[test]
    fn json_rows_price_opencode_fallback_only_for_opencode_tokens() {
        clear_pricing_cache();

        let mut raw_pricing = HashMap::new();
        raw_pricing.insert(
            "shared".to_string(),
            ModelPricing {
                input_cost_per_token: 0.01,
                ..Default::default()
            },
        );
        let pricing_map = ModelPricingMap::new(raw_pricing);

        let mut models = UsageResult::default();
        models.insert("shared-pro".to_string(), json!({"input_tokens": 200}));

        let mut per_provider = PerProviderUsage::default();
        per_provider
            .claude
            .insert("shared-pro".to_string(), json!({"input_tokens": 100}));
        per_provider
            .opencode
            .insert("shared-pro".to_string(), json!({"input_tokens": 100}));

        let mut stored_costs = vibe_coding_tracker::usage::StoredCosts::default();
        stored_costs.opencode.insert("shared-pro".to_string(), 7.0);

        let usage_data = vibe_coding_tracker::UsageData {
            models,
            per_provider,
            provider_days: ProviderActiveDays::default(),
            stored_costs,
        };

        let rows = build_enriched_json(&usage_data, &pricing_map).unwrap();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0]["cost_usd"].as_f64().unwrap(), 8.0);
        assert_eq!(rows[0]["matched_model"].as_str().unwrap(), "shared");
    }
}