codexctl 0.10.0

Codex Controller - Full control plane for Codex CLI
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
use crate::utils::auth::{
    UsageInfo, auth_mode_has_api_key, auth_mode_has_chatgpt, auth_mode_label, detect_auth_mode,
    extract_usage_info,
};
use crate::utils::config::Config;
use anyhow::{Context as _, Result};
use chrono::{DateTime, Utc};
use colored::Colorize as _;
use prettytable::{Cell, Row, Table, format};
use serde::Serialize;

pub async fn execute(
    config: Config,
    all: bool,
    realtime: bool,
    json: bool,
    quiet: bool,
) -> Result<()> {
    if all {
        return show_all_profiles_usage(config, json, quiet).await;
    }

    let codex_dir = config.codex_dir();
    let auth_path = codex_dir.join("auth.json");

    if !auth_path.exists() {
        anyhow::bail!(
            "No `Codex` authentication found. Run `codex` and sign in with ChatGPT or an API key."
        );
    }

    let Ok(content) = tokio::fs::read_to_string(&auth_path).await else {
        anyhow::bail!("Failed to read auth.json");
    };

    let Ok(auth_json) = serde_json::from_str::<serde_json::Value>(&content) else {
        anyhow::bail!("Failed to parse auth.json");
    };
    let auth_mode = detect_auth_mode(&auth_json);

    let usage_info = extract_usage_info(&auth_json).ok();
    let realtime_result = if realtime && auth_mode_has_api_key(&auth_mode) {
        Some(fetch_realtime_quota(&auth_json).await)
    } else {
        None
    };

    if json {
        let payload = UsageCommandJson {
            auth_mode: auth_mode.clone(),
            auth_mode_label: auth_mode_label(&auth_mode).to_string(),
            plan_claims_available: auth_mode_has_chatgpt(&auth_mode) && usage_info.is_some(),
            api_realtime_available: auth_mode_has_api_key(&auth_mode),
            realtime_requested: realtime,
            plan_claims: usage_info.clone(),
            realtime_quota: realtime_result
                .as_ref()
                .and_then(|result| result.as_ref().ok())
                .cloned(),
            realtime_error: realtime_result.and_then(|result| result.err().map(|e| e.to_string())),
        };
        println!("{}", serde_json::to_string_pretty(&payload)?);
        return Ok(());
    }

    // Extract usage info from ChatGPT/Codex JWT claims when present.
    if auth_mode_has_chatgpt(&auth_mode) {
        if let Some(info) = usage_info.as_ref() {
            display_usage_table(info);
        } else if !quiet {
            println!("\n{}", "`Codex` plan claims unavailable".yellow().bold());
            println!(
                "  Current auth mode reports ChatGPT/Codex tokens, but claims could not be parsed."
            );
        }
    } else if !quiet {
        println!("\n{}", "API-key auth mode detected".cyan().bold());
        println!("  ChatGPT/Codex plan claims are not available in API-key-only mode.");
    }

    // Fetch real-time quota if requested and API-key auth is available.
    if realtime {
        if !auth_mode_has_api_key(&auth_mode) {
            if !quiet {
                eprintln!(
                    "\n{} `--realtime` requires API key auth. Current mode: {}",
                    "".yellow(),
                    auth_mode
                );
            }
        } else {
            match realtime_result.expect("realtime result prepared when API key is available") {
                Ok(quota) => display_realtime_quota(&quota),
                Err(e) => {
                    if !quiet {
                        eprintln!("\n{} Could not fetch real-time quota: {}", "".yellow(), e);
                    }
                }
            }
        }
    }

    // Display subscription status when ChatGPT/Codex plan claims are available.
    if auth_mode_has_chatgpt(&auth_mode)
        && let Some(info) = usage_info.as_ref()
    {
        display_subscription_status(info);
    }

    // Display helpful info relevant to the current auth mode.
    display_limits_info(&auth_mode, usage_info.is_some());

    Ok(())
}

/// Fetch real-time quota from `OpenAI` API
async fn fetch_realtime_quota(
    auth_json: &serde_json::Value,
) -> anyhow::Result<crate::utils::api::RealTimeQuota> {
    use crate::utils::api::{extract_api_key, fetch_quota};

    let api_key = extract_api_key(auth_json).context("No API key found in auth.json")?;

    fetch_quota(&api_key).await
}

/// Display real-time quota information
#[allow(clippy::cast_precision_loss)]
fn display_realtime_quota(quota: &crate::utils::api::RealTimeQuota) {
    println!(
        "\n{}",
        "📈 Real-Time API Quota (separate from ChatGPT/Codex plans)"
            .bold()
            .cyan()
    );
    println!();

    let mut table = Table::new();
    table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);

    table.add_row(Row::new(vec![
        Cell::new("Account ID"),
        Cell::new(&quota.account_id),
    ]));

    table.add_row(Row::new(vec![
        Cell::new("Plan"),
        Cell::new(&quota.plan.to_uppercase()),
    ]));

    // Format as dollars
    let limit_dollars = format!("${:.2}", quota.quota_limit as f64 / 100.0);
    let usage_dollars = format!("${:.2}", quota.usage_this_month as f64 / 100.0);
    let remaining_dollars = format!("${:.2}", quota.remaining_quota as f64 / 100.0);

    table.add_row(Row::new(vec![
        Cell::new("Monthly Limit"),
        Cell::new(&limit_dollars),
    ]));

    table.add_row(Row::new(vec![
        Cell::new("Used This Month"),
        Cell::new(&usage_dollars),
    ]));

    let remaining_style = if quota.is_critical() {
        "Fr"
    } else if quota.is_low() {
        "Fy"
    } else {
        "Fg"
    };
    table.add_row(Row::new(vec![
        Cell::new("Remaining"),
        Cell::new(&remaining_dollars).style_spec(remaining_style),
    ]));

    let percent_style = if quota.is_critical() {
        "Fr"
    } else if quota.is_low() {
        "Fy"
    } else {
        "Fg"
    };
    table.add_row(Row::new(vec![
        Cell::new("Percent Used"),
        Cell::new(&format!("{:.1}%", quota.percent_used)).style_spec(percent_style),
    ]));

    if let Some(days) = quota.days_until_reset() {
        let days_text = if days > 0 {
            format!("{days} days until reset")
        } else {
            "Resets today".to_string()
        };
        table.add_row(Row::new(vec![Cell::new("Reset"), Cell::new(&days_text)]));
    }

    table.printstd();

    // Warning messages
    if quota.is_critical() {
        println!("\n{}", "⚠️  WARNING: Quota critically low!".red().bold());
        println!(
            "   Only {:.1}% remaining. Consider switching profiles.",
            100.0 - quota.percent_used
        );
    } else if quota.is_low() {
        println!("\n{}", "⚠️  Quota running low".yellow());
        println!("   {:.1}% used. Monitor usage closely.", quota.percent_used);
    }
}

/// Show usage information for all profiles
#[allow(clippy::too_many_lines)]
async fn show_all_profiles_usage(config: Config, json: bool, quiet: bool) -> Result<()> {
    let profiles_dir = config.profiles_dir();
    if !profiles_dir.exists() {
        anyhow::bail!("No profiles directory found");
    }

    let mut entries = tokio::fs::read_dir(profiles_dir).await?;
    let mut profiles = Vec::new();

    // Scan all profiles
    while let Some(entry) = entries.next_entry().await? {
        let path = entry.path();
        if !path.is_dir() {
            continue;
        }

        let name = path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string();

        if name == "backups" || name.starts_with('.') {
            continue;
        }

        // Read auth.json from profile
        let meta = read_profile_meta(&path).await;
        profiles.push(build_profile_usage_row(&path, name, meta).await);
    }

    if profiles.is_empty() {
        anyhow::bail!("No profiles found");
    }

    profiles.sort_by(|a, b| {
        b.sort_score
            .cmp(&a.sort_score)
            .then_with(|| a.name.cmp(&b.name))
    });

    if json {
        println!("{}", serde_json::to_string_pretty(&profiles)?);
    } else if !quiet {
        println!("\n{}", "📊 Usage Across All Profiles".bold().cyan());
        println!();

        let mut table = Table::new();
        table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);

        // Header
        table.add_row(Row::new(vec![
            Cell::new("Profile").style_spec("Fb"),
            Cell::new("Auth Mode").style_spec("Fb"),
            Cell::new("Identity").style_spec("Fb"),
            Cell::new("Access").style_spec("Fb"),
            Cell::new("Status").style_spec("Fb"),
        ]));

        for profile in profiles {
            table.add_row(Row::new(vec![
                Cell::new(&profile.name).style_spec("Fg"),
                Cell::new(auth_mode_label(&profile.auth_mode)),
                Cell::new(&profile.identity),
                Cell::new(&profile.access),
                Cell::new(&profile.status).style_spec(&profile.status_style),
            ]));
        }

        table.printstd();
        println!();
        println!(
            "{}",
            "💡 Tip: Use 'codexctl load auto' to switch toward the strongest available profile"
                .dimmed()
        );
    }

    Ok(())
}

fn display_usage_table(info: &UsageInfo) {
    println!("\n{}", "`Codex` Usage & Subscription Info".bold().cyan());
    println!();

    let mut table = Table::new();
    table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);

    // Header
    table.add_row(Row::new(vec![
        Cell::new("Property").style_spec("Fb"),
        Cell::new("Value").style_spec("Fb"),
    ]));

    // Email
    table.add_row(Row::new(vec![
        Cell::new("Email"),
        Cell::new(&info.email).style_spec("Fg"),
    ]));

    // Plan Type with color coding
    let plan_style = match info.plan_type.as_str() {
        "team" => "Fb",
        "enterprise" => "Fm",
        "personal" => "Fy",
        _ => "",
    };
    table.add_row(Row::new(vec![
        Cell::new("Plan Type"),
        Cell::new(&info.plan_type.to_uppercase()).style_spec(plan_style),
    ]));

    // Account ID (truncated)
    let short_account_id = if info.account_id.len() > 12 {
        format!("{}...", &info.account_id[..12])
    } else {
        info.account_id.clone()
    };
    table.add_row(Row::new(vec![
        Cell::new("Account ID"),
        Cell::new(&short_account_id),
    ]));

    // Organizations
    if !info.organizations.is_empty() {
        let orgs = info.organizations.join(", ");
        table.add_row(Row::new(vec![Cell::new("Organizations"), Cell::new(&orgs)]));
    }

    // Subscription period
    if let (Some(start), Some(end)) = (&info.subscription_start, &info.subscription_end) {
        let start_formatted = format_date(start);
        let end_formatted = format_date(end);

        table.add_row(Row::new(vec![
            Cell::new("Subscription"),
            Cell::new(&format!("{start_formatted} to {end_formatted}")),
        ]));

        // Calculate days remaining
        if let Ok(days) = calculate_days_remaining(end) {
            let (days_text, color) = if days > 0 {
                (format!("{days} days remaining"), "Fg")
            } else {
                (format!("Expired {} days ago", days.abs()), "Fr")
            };
            table.add_row(Row::new(vec![
                Cell::new("Status"),
                Cell::new(&days_text).style_spec(color),
            ]));
        }
    }

    table.printstd();
}

fn display_subscription_status(info: &UsageInfo) {
    println!();

    match info.plan_type.to_lowercase().as_str() {
        "team" | "business" => {
            println!("{}", "✓ ChatGPT Team/Business Plan Detected".green().bold());
        }
        "enterprise" => {
            println!("{}", "✓ ChatGPT Enterprise Plan Detected".magenta().bold());
        }
        "personal" | "plus" | "pro" | "free" => {
            println!("{}", "✓ Personal ChatGPT Plan Detected".yellow().bold());
        }
        _ => {
            println!("{}", "⚠ Unknown Plan Type".yellow().bold());
        }
    }
    println!("  • Plan claims come from local `auth.json` session tokens");
    println!("  • ChatGPT/Codex plan access and API billing are separate");
}

fn display_limits_info(auth_mode: &str, has_chatgpt_claims: bool) {
    println!();
    println!("{}", "📋 Usage Limits".bold());
    match auth_mode {
        "chatgpt" => {
            if has_chatgpt_claims {
                println!(
                    "  `codexctl usage` shows ChatGPT/Codex plan claims from local auth tokens."
                );
            }
            println!("  `codexctl usage --realtime` requires API key auth.");
        }
        "api_key" => {
            println!("  API-key mode: plan claims are unavailable in `codexctl usage`.");
            println!("  `codexctl usage --realtime` queries OpenAI API billing/quota.");
        }
        "chatgpt+api_key" => {
            if has_chatgpt_claims {
                println!(
                    "  `codexctl usage` shows ChatGPT/Codex plan claims from local auth tokens."
                );
            }
            println!("  `codexctl usage --realtime` queries OpenAI API billing/quota.");
        }
        _ => {
            println!("  Unknown auth mode. Sign in with ChatGPT or API key by running `codex`.");
        }
    }
    if auth_mode_has_api_key(auth_mode) {
        println!(
            "  API limits page: {}",
            "https://platform.openai.com/settings/organization/limits"
                .cyan()
                .underline()
        );
    }
    println!();
    println!("{}", "💡 Tips:".dimmed());
    println!(
        "  {}",
        "• ChatGPT subscriptions and OpenAI API usage are billed separately".dimmed()
    );
    println!(
        "  {}",
        "• `Codex` CLI shows usage warnings when approaching limits".dimmed()
    );
    println!(
        "  {}",
        "• Run 'codexctl status' to check current profile".dimmed()
    );
    println!(
        "  {}",
        "• Use 'codexctl backup' before switching profiles".dimmed()
    );
}

fn format_date(iso_date: &str) -> String {
    // Parse ISO 8601 date and format nicely
    let parts: Vec<&str> = iso_date.split('T').collect();
    if let Some(date_part) = parts.first() {
        let date_parts: Vec<&str> = date_part.split('-').collect();
        if date_parts.len() == 3 {
            let year = date_parts[0];
            let month = match date_parts[1] {
                "01" => "Jan",
                "02" => "Feb",
                "03" => "Mar",
                "04" => "Apr",
                "05" => "May",
                "06" => "Jun",
                "07" => "Jul",
                "08" => "Aug",
                "09" => "Sep",
                "10" => "Oct",
                "11" => "Nov",
                "12" => "Dec",
                _ => date_parts[1],
            };
            let day = date_parts[2];
            return format!("{month} {day}, {year}");
        }
    }

    iso_date.to_string()
}

fn calculate_days_remaining(iso_date: &str) -> Result<i64> {
    use chrono::{DateTime, Utc};

    let end_date = DateTime::parse_from_rfc3339(iso_date)
        .map_err(|e| anyhow::anyhow!("Failed to parse date: {e}"))?;

    let now = Utc::now();
    let duration = end_date.with_timezone(&Utc) - now;

    Ok(duration.num_days())
}

#[derive(Debug, Serialize)]
struct ProfileUsageRow {
    name: String,
    auth_mode: String,
    identity: String,
    access: String,
    status: String,
    #[serde(skip_serializing)]
    status_style: String,
    #[serde(skip_serializing)]
    sort_score: i32,
}

#[derive(Debug, Serialize)]
struct UsageCommandJson {
    auth_mode: String,
    auth_mode_label: String,
    plan_claims_available: bool,
    api_realtime_available: bool,
    realtime_requested: bool,
    plan_claims: Option<UsageInfo>,
    realtime_quota: Option<crate::utils::api::RealTimeQuota>,
    realtime_error: Option<String>,
}

async fn read_profile_meta(
    profile_dir: &std::path::Path,
) -> Option<crate::utils::profile::ProfileMeta> {
    let meta_path = profile_dir.join("profile.json");
    let content = tokio::fs::read_to_string(meta_path).await.ok()?;
    serde_json::from_str(&content).ok()
}

async fn build_profile_usage_row(
    profile_dir: &std::path::Path,
    name: String,
    meta: Option<crate::utils::profile::ProfileMeta>,
) -> ProfileUsageRow {
    let auth_path = profile_dir.join("auth.json");
    let fallback_mode = meta
        .as_ref()
        .map(|m| m.auth_mode.clone())
        .unwrap_or_else(|| "unknown".to_string());
    let fallback_identity = meta
        .as_ref()
        .and_then(|m| m.email.clone())
        .unwrap_or_else(|| "-".to_string());

    if !auth_path.exists() {
        return ProfileUsageRow {
            name,
            auth_mode: fallback_mode,
            identity: fallback_identity,
            access: "No auth data".to_string(),
            status: "Missing auth.json".to_string(),
            status_style: "Fr".to_string(),
            sort_score: 0,
        };
    }

    let auth_content = match tokio::fs::read(&auth_path).await {
        Ok(content) => content,
        Err(_) => {
            return ProfileUsageRow {
                name,
                auth_mode: fallback_mode,
                identity: fallback_identity,
                access: "Unreadable auth".to_string(),
                status: "Cannot read auth.json".to_string(),
                status_style: "Fr".to_string(),
                sort_score: 0,
            };
        }
    };

    if crate::utils::crypto::is_encrypted(&auth_content) {
        return ProfileUsageRow {
            name,
            auth_mode: fallback_mode,
            identity: fallback_identity,
            access: "Decrypt on load".to_string(),
            status: "Locked (encrypted)".to_string(),
            status_style: "Fy".to_string(),
            sort_score: 1,
        };
    }

    let auth_json: serde_json::Value = match serde_json::from_slice(&auth_content) {
        Ok(json) => json,
        Err(_) => {
            return ProfileUsageRow {
                name,
                auth_mode: fallback_mode,
                identity: fallback_identity,
                access: "Invalid auth".to_string(),
                status: "Invalid auth.json".to_string(),
                status_style: "Fr".to_string(),
                sort_score: 0,
            };
        }
    };

    let auth_mode = detect_auth_mode(&auth_json);
    let usage_info = extract_usage_info(&auth_json).ok();

    if auth_mode_has_chatgpt(&auth_mode)
        && let Some(usage) = usage_info
    {
        let days_left = usage
            .subscription_end
            .as_ref()
            .and_then(|end| {
                DateTime::parse_from_rfc3339(end)
                    .ok()
                    .map(|d| (d.with_timezone(&Utc) - Utc::now()).num_days())
            })
            .unwrap_or(0);
        let (status, status_style, sort_score) = if days_left > 7 {
            (format!("Active ({days_left}d)"), "Fg", 4)
        } else if days_left > 0 {
            (format!("Expiring soon ({days_left}d)"), "Fy", 3)
        } else {
            ("Expired".to_string(), "Fr", 2)
        };

        let access = if auth_mode_has_api_key(&auth_mode) {
            format!("{} + API key", usage.plan_type.to_uppercase())
        } else {
            usage.plan_type.to_uppercase()
        };

        return ProfileUsageRow {
            name,
            auth_mode,
            identity: usage.email,
            access,
            status,
            status_style: status_style.to_string(),
            sort_score,
        };
    }

    if auth_mode_has_api_key(&auth_mode) {
        let status = if auth_mode_has_chatgpt(&auth_mode) {
            "API ready; ChatGPT claims unavailable".to_string()
        } else {
            "API ready".to_string()
        };
        return ProfileUsageRow {
            name,
            auth_mode,
            identity: fallback_identity,
            access: "OpenAI API billing/quota".to_string(),
            status,
            status_style: "Fg".to_string(),
            sort_score: 2,
        };
    }

    ProfileUsageRow {
        name,
        auth_mode,
        identity: fallback_identity,
        access: "Unknown".to_string(),
        status: "Unknown auth mode".to_string(),
        status_style: "Fr".to_string(),
        sort_score: 0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_build_profile_usage_row_api_key_profile() {
        let temp_dir = TempDir::new().unwrap();
        tokio::fs::write(
            temp_dir.path().join("auth.json"),
            r#"{"api_key":"sk-test"}"#,
        )
        .await
        .unwrap();

        let row = build_profile_usage_row(temp_dir.path(), "api".to_string(), None).await;
        assert_eq!(row.auth_mode, "api_key");
        assert_eq!(row.access, "OpenAI API billing/quota");
        assert_eq!(row.status, "API ready");
    }

    #[tokio::test]
    async fn test_build_profile_usage_row_encrypted_profile_uses_metadata() {
        let temp_dir = TempDir::new().unwrap();
        let mut profile = crate::utils::profile::Profile::new(
            "encrypted".to_string(),
            Some("hidden@example.com".to_string()),
            None,
        );
        profile.meta.auth_mode = "chatgpt".to_string();
        profile.add_file(
            "auth.json",
            br#"{"tokens":{"id_token":"header.payload.signature"}}"#.to_vec(),
        );
        profile
            .save_to_disk_encrypted(temp_dir.path(), Some(&"secret".to_string()))
            .unwrap();
        let meta = read_profile_meta(temp_dir.path()).await;

        let row = build_profile_usage_row(temp_dir.path(), "encrypted".to_string(), meta).await;
        assert_eq!(row.status, "Locked (encrypted)");
        assert_eq!(row.identity, "hidden@example.com");
    }

    #[tokio::test]
    async fn test_build_profile_usage_row_hybrid_profile() {
        let temp_dir = TempDir::new().unwrap();
        let auth = make_chatgpt_auth("team", 30, true);
        tokio::fs::write(temp_dir.path().join("auth.json"), auth.to_string())
            .await
            .unwrap();

        let row = build_profile_usage_row(temp_dir.path(), "hybrid".to_string(), None).await;
        assert_eq!(row.auth_mode, "chatgpt+api_key");
        assert_eq!(row.identity, "user@example.com");
        assert_eq!(row.access, "TEAM + API key");
    }

    fn make_chatgpt_auth(
        plan: &str,
        days_until_expiry: i64,
        with_api_key: bool,
    ) -> serde_json::Value {
        use chrono::Duration;

        let now = Utc::now();
        let payload = serde_json::json!({
            "email": "user@example.com",
            "https://api.openai.com/auth": {
                "chatgpt_plan_type": plan,
                "chatgpt_subscription_active_start": now.to_rfc3339(),
                "chatgpt_subscription_active_until": (now + Duration::days(days_until_expiry)).to_rfc3339(),
                "chatgpt_account_id": "acct_test",
                "organizations": []
            }
        });
        let header = URL_SAFE_NO_PAD.encode(br#"{"alg":"none"}"#);
        let payload = URL_SAFE_NO_PAD.encode(payload.to_string().as_bytes());
        let token = format!("{header}.{payload}.sig");

        if with_api_key {
            serde_json::json!({
                "tokens": { "id_token": token },
                "api_key": "sk-test"
            })
        } else {
            serde_json::json!({
                "tokens": { "id_token": token }
            })
        }
    }
}