shift-preflight-cli 0.10.1

CLI for SHIFT — transform images in AI model payloads to meet provider constraints
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
//! Interactive setup for multi-agent SHIFT integration.
//!
//! Detects which AI coding agents are installed and configures each one
//! to route API traffic through the SHIFT proxy. Also optionally installs
//! a macOS LaunchAgent for auto-start on login.
//!
//! Supported agents and their configuration mechanisms:
//!   - OpenCode:    writes opencode.json (plugin + provider.baseURL)
//!   - Claude Code: writes ~/.claude/settings.json (env.ANTHROPIC_BASE_URL)
//!   - Codex CLI:   writes ~/.codex/config.toml (openai_base_url)
//!   - Cursor:      prints manual instructions (UI-only setting)

use anyhow::{Context, Result};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

const DEFAULT_PORT: u16 = 8787;
#[cfg(target_os = "macos")]
const LAUNCH_AGENT_LABEL: &str = "com.shift-ai.proxy";

/// Detected agent information.
struct DetectedAgent {
    name: &'static str,
    key: &'static str,
    detected: bool,
    configured: bool,
}

/// Check if a command exists on PATH.
fn command_exists(cmd: &str) -> bool {
    Command::new("which")
        .arg(cmd)
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

/// Check if a directory exists.
fn dir_exists(path: &str) -> bool {
    let expanded = shellexpand(path);
    std::path::Path::new(&expanded).exists()
}

/// Simple ~ expansion.
fn shellexpand(path: &str) -> String {
    if let Some(rest) = path.strip_prefix("~/") {
        if let Ok(home) = std::env::var("HOME") {
            return format!("{}/{}", home, rest);
        }
    }
    path.to_string()
}

/// Detect installed agents.
fn detect_agents() -> Vec<DetectedAgent> {
    vec![
        DetectedAgent {
            name: "OpenCode",
            key: "opencode",
            detected: command_exists("opencode") || dir_exists("~/.config/opencode"),
            configured: false,
        },
        DetectedAgent {
            name: "Claude Code",
            key: "claude-code",
            detected: command_exists("claude") || dir_exists("~/.claude"),
            configured: false,
        },
        DetectedAgent {
            name: "Codex CLI",
            key: "codex",
            detected: command_exists("codex") || dir_exists("~/.codex"),
            configured: false,
        },
        DetectedAgent {
            name: "Cursor",
            key: "cursor",
            detected: command_exists("cursor") || dir_exists("~/.cursor"),
            configured: false,
        },
    ]
}

/// Check prerequisites.
fn check_prerequisites() -> Result<(bool, bool)> {
    let shift_ok = command_exists("shift-ai");
    let npx_ok = command_exists("npx");
    Ok((shift_ok, npx_ok))
}

// ── macOS LaunchAgent ────────────────────────────────────────────────

/// Generate the macOS LaunchAgent plist content.
#[cfg(target_os = "macos")]
fn launchagent_plist(shift_ai_path: &str, port: u16) -> String {
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>{label}</string>
    <key>ProgramArguments</key>
    <array>
        <string>{binary}</string>
        <string>proxy</string>
        <string>start</string>
        <string>--port</string>
        <string>{port}</string>
        <string>--foreground</string>
    </array>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardOutPath</key>
    <string>{home}/.shift/proxy.log</string>
    <key>StandardErrorPath</key>
    <string>{home}/.shift/proxy.log</string>
</dict>
</plist>"#,
        label = LAUNCH_AGENT_LABEL,
        binary = shift_ai_path,
        port = port,
        home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".into()),
    )
}

/// Install the macOS LaunchAgent.
#[cfg(target_os = "macos")]
fn install_launchagent(port: u16) -> Result<()> {
    let home = std::env::var("HOME").context("HOME not set")?;
    let agents_dir = PathBuf::from(&home).join("Library/LaunchAgents");
    fs::create_dir_all(&agents_dir).context("failed to create LaunchAgents directory")?;

    let plist_path = agents_dir.join(format!("{}.plist", LAUNCH_AGENT_LABEL));

    // Find shift-ai binary path
    let shift_ai_path = Command::new("which")
        .arg("shift-ai")
        .output()
        .context("failed to find shift-ai")?;
    let binary = String::from_utf8_lossy(&shift_ai_path.stdout)
        .trim()
        .to_string();
    if binary.is_empty() {
        anyhow::bail!("shift-ai not found on PATH");
    }

    let content = launchagent_plist(&binary, port);
    fs::write(&plist_path, &content).context("failed to write LaunchAgent plist")?;

    // Unload if already loaded (ignore errors)
    let _ = Command::new("launchctl")
        .args(["unload", &plist_path.to_string_lossy()])
        .output();

    // Load
    Command::new("launchctl")
        .args(["load", &plist_path.to_string_lossy()])
        .status()
        .context("failed to load LaunchAgent")?;

    Ok(())
}

// ── Per-agent configuration ──────────────────────────────────────────

/// Configure Claude Code by writing/updating ~/.claude/settings.json.
fn configure_claude_code(port: u16) -> Result<bool> {
    let home = std::env::var("HOME").context("HOME not set")?;
    let settings_dir = PathBuf::from(&home).join(".claude");
    fs::create_dir_all(&settings_dir)?;

    let settings_path = settings_dir.join("settings.json");

    let mut settings: serde_json::Value = if settings_path.exists() {
        let content = fs::read_to_string(&settings_path)?;
        serde_json::from_str(&content).unwrap_or_else(|_| serde_json::json!({}))
    } else {
        serde_json::json!({})
    };

    // Add env.ANTHROPIC_BASE_URL
    let env = settings
        .as_object_mut()
        .context("settings is not an object")?
        .entry("env")
        .or_insert_with(|| serde_json::json!({}));

    if let Some(env_obj) = env.as_object_mut() {
        let url = format!("http://localhost:{}", port);
        env_obj.insert(
            "ANTHROPIC_BASE_URL".to_string(),
            serde_json::Value::String(url),
        );
    }

    let content = serde_json::to_string_pretty(&settings)?;
    fs::write(&settings_path, content)?;
    Ok(true)
}

/// Configure OpenCode by updating opencode.json at a specific path.
/// Returns `Ok(true)` if configuration was written, `Ok(false)` if the
/// config file does not exist.
fn configure_opencode_at(config_path: &Path, port: u16) -> Result<bool> {
    if !config_path.exists() {
        return Ok(false);
    }

    let content = fs::read_to_string(config_path)?;
    let mut config: serde_json::Value =
        serde_json::from_str(&content).unwrap_or_else(|_| serde_json::json!({}));

    // Set provider.anthropic.options.baseURL
    let provider = config
        .as_object_mut()
        .context("config is not an object")?
        .entry("provider")
        .or_insert_with(|| serde_json::json!({}));
    let anthropic = provider
        .as_object_mut()
        .context("provider is not an object")?
        .entry("anthropic")
        .or_insert_with(|| serde_json::json!({}));
    let options = anthropic
        .as_object_mut()
        .context("anthropic is not an object")?
        .entry("options")
        .or_insert_with(|| serde_json::json!({}));

    if let Some(opts) = options.as_object_mut() {
        // OpenCode's Anthropic client uses baseURL as the full prefix and
        // appends only `/messages` (NOT `/v1/messages`).  Therefore we must
        // include `/v1` here so the final URL becomes `…/v1/messages`.
        let url = format!("http://localhost:{}/v1", port);
        opts.insert("baseURL".to_string(), serde_json::Value::String(url));
    }

    // Add plugin if not present
    let plugins = config
        .as_object_mut()
        .unwrap()
        .entry("plugin")
        .or_insert_with(|| serde_json::json!([]));
    if let Some(arr) = plugins.as_array_mut() {
        let plugin_name = "@shift-preflight/opencode-plugin";
        if !arr.iter().any(|v| v.as_str() == Some(plugin_name)) {
            arr.push(serde_json::Value::String(plugin_name.to_string()));
        }
    }

    let output = serde_json::to_string_pretty(&config)?;
    fs::write(config_path, output)?;
    Ok(true)
}

/// Configure OpenCode by updating ~/.config/opencode/opencode.json.
fn configure_opencode(port: u16) -> Result<bool> {
    let home = std::env::var("HOME").context("HOME not set")?;
    let config_path = PathBuf::from(&home).join(".config/opencode/opencode.json");
    configure_opencode_at(&config_path, port)
}

/// Configure Codex CLI by writing/updating ~/.codex/config.toml.
fn configure_codex(port: u16) -> Result<bool> {
    let home = std::env::var("HOME").context("HOME not set")?;
    let config_dir = PathBuf::from(&home).join(".codex");
    fs::create_dir_all(&config_dir)?;

    let config_path = config_dir.join("config.toml");
    let key = "openai_base_url";
    let value = format!("http://localhost:{}", port);

    if config_path.exists() {
        let content = fs::read_to_string(&config_path)?;
        // Check if already set
        if content.contains(key) {
            // Replace existing line
            let mut new_lines = Vec::new();
            for line in content.lines() {
                if line.trim_start().starts_with(key) {
                    new_lines.push(format!("{} = \"{}\"", key, value));
                } else {
                    new_lines.push(line.to_string());
                }
            }
            fs::write(&config_path, new_lines.join("\n") + "\n")?;
        } else {
            // Append
            let mut content = content;
            if !content.ends_with('\n') {
                content.push('\n');
            }
            content.push_str(&format!("{} = \"{}\"\n", key, value));
            fs::write(&config_path, content)?;
        }
    } else {
        // Create new config
        fs::write(&config_path, format!("{} = \"{}\"\n", key, value))?;
    }

    Ok(true)
}

// ── Stale OpenCode plugin cache detection ────────────────────────────

/// Compare two semver strings. Returns true if `cached` is older than `current`.
fn is_older_semver(cached: &str, current: &str) -> bool {
    let parse = |s: &str| -> Option<(u64, u64, u64)> {
        let parts: Vec<&str> = s.trim().trim_start_matches('v').split('.').collect();
        if parts.len() != 3 {
            return None;
        }
        Some((
            parts[0].parse().ok()?,
            parts[1].parse().ok()?,
            parts[2].parse().ok()?,
        ))
    };

    match (parse(cached), parse(current)) {
        (Some(c), Some(cur)) => c < cur,
        _ => false,
    }
}

/// Check for a stale OpenCode plugin cache at a specific directory.
///
/// If a cached `package.json` exists under `cache_dir` with a version older
/// than the CLI's own version, the cache directory is deleted.
///
/// Returns `Some(old_version)` if a stale cache was cleared, `None` otherwise.
pub(crate) fn check_and_clear_stale_opencode_cache_at(cache_dir: &Path) -> Result<Option<String>> {
    let pkg_json = cache_dir.join("node_modules/@shift-preflight/opencode-plugin/package.json");

    if !pkg_json.exists() {
        return Ok(None);
    }

    let content =
        fs::read_to_string(&pkg_json).context("failed to read cached plugin package.json")?;
    let parsed: serde_json::Value =
        serde_json::from_str(&content).context("failed to parse cached plugin package.json")?;

    let cached_version = match parsed.get("version").and_then(|v| v.as_str()) {
        Some(v) => v.to_string(),
        None => return Ok(None),
    };

    let current_version = env!("CARGO_PKG_VERSION");

    if is_older_semver(&cached_version, current_version) {
        // Don't follow symlinks — only delete real directories
        let meta = fs::symlink_metadata(cache_dir)?;
        if !meta.is_dir() {
            return Ok(None);
        }
        fs::remove_dir_all(cache_dir).context("failed to remove stale OpenCode plugin cache")?;
        Ok(Some(cached_version))
    } else {
        Ok(None)
    }
}

/// Check for a stale OpenCode plugin cache at the default location.
///
/// Returns `Some(old_version)` if a stale cache was cleared, `None` otherwise.
pub(crate) fn check_and_clear_stale_opencode_cache() -> Result<Option<String>> {
    let home = match std::env::var("HOME") {
        Ok(h) => h,
        Err(_) => return Ok(None),
    };
    let cache_dir = PathBuf::from(&home)
        .join(".cache/opencode/packages/@shift-preflight/opencode-plugin@latest");
    check_and_clear_stale_opencode_cache_at(&cache_dir)
}

// ── Interactive setup ────────────────────────────────────────────────

/// Run the interactive setup.
pub fn run_setup() -> Result<()> {
    use colored::Colorize;
    use std::io::{self, IsTerminal, Write};

    let use_color = io::stdout().is_terminal();
    let port = DEFAULT_PORT;

    // Header
    if use_color {
        println!("{}", "SHIFT Setup".bold().green());
        println!("{}", "".repeat(50).green());
    } else {
        println!("=== SHIFT Setup ===");
    }
    println!();

    // Prerequisites
    println!("Checking prerequisites...");
    let (shift_ok, npx_ok) = check_prerequisites()?;
    if shift_ok {
        println!(
            "  {} shift-ai v{} installed",
            "".green(),
            env!("CARGO_PKG_VERSION")
        );
    } else {
        println!("  {} shift-ai not found", "".red());
        println!();
        println!("Install shift-ai first:");
        println!("  brew install alohaninja/shift/shift-ai");
        println!("  # or: cargo install shift-preflight-cli");
        return Ok(());
    }
    if npx_ok {
        println!("  {} npx available", "".green());
    } else {
        println!("  {} npx not found (required for proxy)", "".red());
        println!();
        println!("Install Node.js: brew install node");
        return Ok(());
    }
    println!();

    // Detect agents
    println!("Detecting AI agents...");
    let mut agents = detect_agents();
    let any_detected = agents.iter().any(|a| a.detected);

    for agent in &agents {
        if agent.detected {
            println!("  {} {} — found", "".green(), agent.name);
        } else {
            println!("  {} {} — not found", "".red().dimmed(), agent.name);
        }
    }
    println!();

    if !any_detected {
        println!("No AI agents detected. You can still start the proxy manually:");
        println!("  shift-ai proxy start");
        println!();
        println!("Then configure your agent:");
        println!("  shift-ai env --list");
        return Ok(());
    }

    // Ask for confirmation
    print!("Configure detected agents and install LaunchAgent? [Y/n] ");
    io::stdout().flush()?;
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    let input = input.trim().to_lowercase();
    if input == "n" || input == "no" {
        println!("Setup cancelled.");
        return Ok(());
    }
    println!();

    // Configure each detected agent
    println!("Installing...");

    for agent in agents.iter_mut() {
        if !agent.detected {
            continue;
        }

        let result = match agent.key {
            "opencode" => configure_opencode(port),
            "claude-code" => configure_claude_code(port),
            "codex" => configure_codex(port),
            "cursor" => Ok(false), // UI-only, handled below
            _ => Ok(false),
        };

        match result {
            Ok(true) => {
                agent.configured = true;
                println!("  {} {} configured", "".green(), agent.name);
            }
            Ok(false) if agent.key == "cursor" => {
                println!(
                    "  {} {} — open Settings > Models > Override OpenAI Base URL:",
                    "".yellow(),
                    agent.name,
                );
                println!("         http://localhost:{}/v1", port);
            }
            Ok(false) => {
                println!(
                    "  {} {} — run: shift-ai env {}",
                    "".yellow(),
                    agent.name,
                    agent.key
                );
            }
            Err(e) => {
                println!("  {} {} — failed: {}", "".red(), agent.name, e);
            }
        }

        // Always check for stale OpenCode plugin cache, regardless of config result
        if agent.key == "opencode" {
            match check_and_clear_stale_opencode_cache() {
                Ok(Some(old_ver)) => {
                    println!(
                        "  {} Cleared stale OpenCode plugin cache (was v{}, latest v{})",
                        "".green(),
                        old_ver,
                        env!("CARGO_PKG_VERSION"),
                    );
                }
                Ok(None) => {} // cache current or absent, nothing to do
                Err(e) => {
                    println!(
                        "  {} OpenCode plugin cache check failed: {}",
                        "".yellow(),
                        e,
                    );
                }
            }
        }
    }

    // Install LaunchAgent (macOS only)
    #[cfg(target_os = "macos")]
    {
        match install_launchagent(port) {
            Ok(()) => {
                println!(
                    "  {} LaunchAgent installed (auto-start on login)",
                    "".green()
                );
            }
            Err(e) => {
                println!("  {} LaunchAgent — failed: {}", "".red(), e);
            }
        }
    }

    // Start proxy now
    match crate::proxy::ensure(Some(port), None, false) {
        Ok(()) => {
            println!("  {} Proxy healthy on port {}", "".green(), port);
        }
        Err(e) => {
            println!("  {} Proxy — failed to start: {}", "".red(), e);
        }
    }

    println!();
    if use_color {
        println!("{}", "Done!".bold().green());
    } else {
        println!("Done!");
    }
    println!("All API traffic now flows through SHIFT.");
    println!("Run `shift-ai gain` to see cumulative token savings.");

    Ok(())
}

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

    /// Create a fake OpenCode plugin cache directory with a package.json
    /// containing the given version string.
    fn make_fake_cache(base: &Path, version: &str) {
        let pkg_dir = base.join("node_modules/@shift-preflight/opencode-plugin");
        fs::create_dir_all(&pkg_dir).unwrap();
        let pkg_json = pkg_dir.join("package.json");
        let content = serde_json::json!({
            "name": "@shift-preflight/opencode-plugin",
            "version": version
        });
        fs::write(&pkg_json, serde_json::to_string_pretty(&content).unwrap()).unwrap();
    }

    #[test]
    fn test_check_stale_cache_detects_old_version() {
        let tmp = tempfile::tempdir().unwrap();
        // Take ownership so TempDir::drop won't try to delete after remove_dir_all
        let tmp_path = tmp.keep();
        let cache_dir = tmp_path.clone();

        // The CLI version from Cargo.toml (e.g. "0.9.4").
        // Create a cache with a version guaranteed to be older.
        make_fake_cache(&cache_dir, "0.0.1");

        let result = check_and_clear_stale_opencode_cache_at(&cache_dir).unwrap();
        assert_eq!(result, Some("0.0.1".to_string()));
        // Directory should have been deleted
        assert!(!cache_dir.exists());
        // Clean up the parent if it still exists (it shouldn't, since cache_dir == tmp_path)
        let _ = fs::remove_dir_all(&tmp_path);
    }

    #[test]
    fn test_check_stale_cache_skips_current_version() {
        let tmp = tempfile::tempdir().unwrap();
        let cache_dir = tmp.path().to_path_buf();

        let current = env!("CARGO_PKG_VERSION");
        make_fake_cache(&cache_dir, current);

        let result = check_and_clear_stale_opencode_cache_at(&cache_dir).unwrap();
        assert_eq!(result, None);
        // Directory should still exist
        assert!(cache_dir.exists());
    }

    #[test]
    fn test_check_stale_cache_skips_missing_cache() {
        let tmp = tempfile::tempdir().unwrap();
        let cache_dir = tmp.path().join("nonexistent");
        // Don't create anything — cache_dir does not exist

        let result = check_and_clear_stale_opencode_cache_at(&cache_dir).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    fn test_configure_opencode_sets_correct_baseurl() {
        let tmp = tempfile::tempdir().unwrap();
        let config_path = tmp.path().join("opencode.json");

        // Write a minimal config
        let initial = serde_json::json!({
            "$schema": "https://opencode.ai/config.json"
        });
        fs::write(
            &config_path,
            serde_json::to_string_pretty(&initial).unwrap(),
        )
        .unwrap();

        let result = configure_opencode_at(&config_path, 8787).unwrap();
        assert!(result);

        // Read back and verify
        let content = fs::read_to_string(&config_path).unwrap();
        let config: serde_json::Value = serde_json::from_str(&content).unwrap();

        let base_url = config["provider"]["anthropic"]["options"]["baseURL"]
            .as_str()
            .unwrap();
        assert_eq!(base_url, "http://localhost:8787/v1");
        // OpenCode appends only `/messages`, so baseURL MUST include `/v1`.
        assert!(
            base_url.ends_with("/v1"),
            "OpenCode baseURL must include /v1, got: {}",
            base_url
        );
        assert!(
            !base_url.ends_with("/v1/v1"),
            "must not have double /v1, got: {}",
            base_url
        );

        // Verify plugin was added
        let plugins = config["plugin"].as_array().unwrap();
        assert!(plugins
            .iter()
            .any(|v| v.as_str() == Some("@shift-preflight/opencode-plugin")));
    }

    #[test]
    fn test_is_older_semver() {
        assert!(is_older_semver("0.0.1", "0.9.4"));
        assert!(is_older_semver("0.9.3", "0.9.4"));
        assert!(is_older_semver("0.8.0", "1.0.0"));
        assert!(!is_older_semver("0.9.4", "0.9.4")); // same
        assert!(!is_older_semver("1.0.0", "0.9.4")); // newer
        assert!(!is_older_semver("0.9.5", "0.9.4")); // newer patch
        assert!(!is_older_semver("invalid", "0.9.4")); // invalid
    }

    /// Pre-release versions (e.g. "0.9.4-beta.1") contain a hyphen in the
    /// patch component, so `parse()` returns `None` and `is_older_semver`
    /// safely returns `false` — meaning we do NOT delete the cache.  This
    /// is the conservative choice: don't delete what we can't understand.
    #[test]
    fn test_is_older_semver_prerelease() {
        assert!(!is_older_semver("0.9.4-beta.1", "0.9.4"));
        assert!(!is_older_semver("0.9.4", "0.9.5-rc.1"));
        assert!(!is_older_semver("1.0.0-alpha", "1.0.0"));
    }

    /// Validates the core migration scenario: an existing opencode.json with
    /// the OLD baseURL (missing `/v1`) gets corrected to include `/v1`.
    #[test]
    fn test_configure_opencode_overwrites_old_baseurl() {
        let tmp = tempfile::tempdir().unwrap();
        let config_path = tmp.path().join("opencode.json");

        // Write a config with the OLD wrong baseURL (no /v1 suffix)
        let initial = serde_json::json!({
            "$schema": "https://opencode.ai/config.json",
            "provider": {
                "anthropic": {
                    "options": {
                        "baseURL": "http://localhost:8787"
                    }
                }
            }
        });
        fs::write(
            &config_path,
            serde_json::to_string_pretty(&initial).unwrap(),
        )
        .unwrap();

        let result = configure_opencode_at(&config_path, 8787).unwrap();
        assert!(result);

        // Read back and verify the baseURL was corrected
        let content = fs::read_to_string(&config_path).unwrap();
        let config: serde_json::Value = serde_json::from_str(&content).unwrap();

        let base_url = config["provider"]["anthropic"]["options"]["baseURL"]
            .as_str()
            .unwrap();
        assert_eq!(
            base_url, "http://localhost:8787/v1",
            "Old baseURL without /v1 must be overwritten to include /v1"
        );
    }
}