supercode-cli 0.4.12

supercode — a lightweight, fully-customizable AI coding agent CLI in Rust. Any model via OpenRouter; natively continues Claude Code and Codex sessions.
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
//! CLI-level acceptance tests for UX-24 ("--json breadth + machine-discovery
//! subcommands + WRAPPERS doc"):
//!
//! - dev/01: `supercode doctor --json`, `inspect --json`, and
//!   `sessions list --json` emit valid JSON.
//! - dev/02: `supercode model list --json` and `provider list --json` emit
//!   machine-readable inventories.
//!
//! Spawns the real, built `supercode` binary (mirroring `login_cli.rs`'s /
//! `quiet_cli.rs`'s idiom) — the CLI, doctor/session/discovery code paths
//! are the genuine, unmodified binary, not a reimplementation. `inspect
//! --json` is exercised here too as a REGRESSION check (it predates UX-24)
//! to prove its shape is untouched.

use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};

use supercode::store::SessionStore;

fn bin() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_supercode"))
}

fn fresh_home(tag: &str) -> PathBuf {
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    let dir = std::env::temp_dir().join(format!(
        "supercode-ux24-json-{tag}-{}-{nanos}",
        std::process::id()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

fn run(home: &Path, args: &[&str]) -> Output {
    Command::new(bin())
        .env("SUPERCODE_HOME", home)
        .env_remove("OPENROUTER_API_KEY")
        .env_remove("OPENAI_API_KEY")
        .env_remove("ANTHROPIC_API_KEY")
        .env_remove("NO_COLOR")
        .args(args)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .expect("failed to spawn the supercode binary")
}

/// Parse stdout as JSON, asserting stderr carries no chrome (spinner,
/// banners, log lines) — the "byte-clean" bar from UX-24's proof mode.
fn json_stdout(out: &Output) -> serde_json::Value {
    assert!(
        out.status.success(),
        "expected success, got {:?}\nstdout: {}\nstderr: {}",
        out.status,
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.trim().is_empty(),
        "expected clean stderr, got: {stderr}"
    );
    serde_json::from_slice(&out.stdout).unwrap_or_else(|e| {
        panic!(
            "stdout was not valid JSON: {e}\nstdout: {}",
            String::from_utf8_lossy(&out.stdout)
        )
    })
}

// ---- dev/01: doctor --json --------------------------------------------

#[test]
fn doctor_json_is_valid_and_documents_the_expected_keys() {
    let home = fresh_home("doctor");
    let out = run(&home, &["doctor", "--json"]);
    let v = json_stdout(&out);
    let obj = v.as_object().expect("doctor --json is an object");
    for key in [
        "version",
        "config_home",
        "config_file_found",
        "model",
        "base_url",
        "api_key_source",
        "provider",
        "aliases",
        // UX-31: additive keys — every key above is unchanged from UX-24.
        "tiers",
        "first_failing_tier",
        "deep_probe_requested",
    ] {
        assert!(obj.contains_key(key), "missing key `{key}` in {v}");
    }
    // No key configured in this fresh, env-scrubbed home → no source, no probe.
    assert_eq!(obj["api_key_source"], serde_json::Value::Null);
    assert_eq!(obj["provider"], serde_json::Value::Null);
    assert!(obj["aliases"]
        .as_array()
        .unwrap()
        .contains(&serde_json::json!({
            "alias": "opus",
            "slug": "anthropic/claude-opus-4-8",
        })));

    // UX-31: with no key configured, tier 1 (config) is the first failure,
    // and the deep probe was never requested by this call.
    assert_eq!(v["first_failing_tier"], "config");
    assert_eq!(v["deep_probe_requested"], false);
    let tiers = v["tiers"].as_array().expect("tiers is an array");
    assert_eq!(tiers.len(), 3, "expected exactly 3 tiers, got {tiers:?}");
    for name in ["config", "catalog", "deep"] {
        assert!(
            tiers.iter().any(|t| t["name"] == name),
            "missing tier `{name}` in {tiers:?}"
        );
    }
}

#[test]
fn doctor_human_output_is_unchanged_without_the_flag() {
    let home = fresh_home("doctor-human");
    let out = run(&home, &["doctor"]);
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("system health"));
    assert!(stdout.contains("model"));
    // The styled panel is never valid JSON.
    assert!(serde_json::from_str::<serde_json::Value>(&stdout).is_err());
}

// ---- dev/01: sessions list --json ---------------------------------------

#[test]
fn sessions_list_json_reports_a_real_saved_session() {
    let home = fresh_home("sessions-list");
    let store = SessionStore::open(home.join("sessions")).unwrap();
    store
        .save("demo-00000000000000000001", "demo session", "{}\n")
        .unwrap();

    let out = run(&home, &["sessions", "list", "--json"]);
    let v = json_stdout(&out);
    let arr = v.as_array().expect("sessions list --json is an array");
    assert_eq!(arr.len(), 1);
    let s = &arr[0];
    for key in [
        "name",
        "short_id",
        "title",
        "archived",
        "reduced",
        "tier",
        "full_bytes",
        "view_bytes",
        "stub_count",
        "escalations",
        "age",
    ] {
        assert!(s.as_object().unwrap().contains_key(key), "missing `{key}`");
    }
    assert_eq!(s["name"], "demo-00000000000000000001");
    assert_eq!(s["short_id"], "00000000000000000001");
    assert_eq!(s["title"], "demo session");
    assert_eq!(s["archived"], false);
}

#[test]
fn sessions_list_json_is_an_empty_array_with_no_sessions() {
    let home = fresh_home("sessions-list-empty");
    let out = run(&home, &["sessions", "list", "--json"]);
    let v = json_stdout(&out);
    assert_eq!(v.as_array().unwrap().len(), 0);
}

#[test]
fn sessions_list_human_output_is_unchanged_without_the_flag() {
    let home = fresh_home("sessions-list-human");
    let store = SessionStore::open(home.join("sessions")).unwrap();
    store
        .save("demo-00000000000000000002", "demo session", "{}\n")
        .unwrap();
    let out = run(&home, &["sessions", "list"]);
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("sessions (1)"));
    assert!(stdout.contains("demo session"));
    assert!(serde_json::from_str::<serde_json::Value>(&stdout).is_err());
}

// ---- dev/01 (regression): inspect --json has one envelope ----------------

#[test]
fn inspect_json_regression_still_emits_a_session_key() {
    let home = fresh_home("inspect");
    let session_dir = home.join("src");
    std::fs::create_dir_all(&session_dir).unwrap();
    let file = session_dir.join("sample.jsonl");
    std::fs::write(
        &file,
        r#"{"type":"user","message":{"role":"user","content":[{"type":"text","text":"hi"}]},"sessionId":"s1","uuid":"u1","timestamp":"2024-01-01T00:00:00Z"}
"#,
    )
    .unwrap();
    let out = run(&home, &["inspect", file.to_str().unwrap(), "--json"]);
    let v = json_stdout(&out);
    assert!(
        v.get("session").is_some(),
        "inspect --json shape changed: {v}"
    );
}

#[test]
fn inspect_json_saved_session_uses_the_same_session_envelope_as_a_file() {
    let home = fresh_home("inspect-saved-envelope");
    let store = SessionStore::open(home.join("sessions")).unwrap();
    store
        .save(
            "saved-envelope-00000000000000000003",
            "saved envelope",
            "{\"role\":\"user\",\"content\":\"saved hello\"}\n",
        )
        .unwrap();

    let out = run(
        &home,
        &["inspect", "saved-envelope-00000000000000000003", "--json"],
    );
    let v = json_stdout(&out);
    let session = v
        .get("session")
        .expect("saved-session inspect must retain the documented envelope");
    assert_eq!(session["message_count"], 1);
    assert_eq!(session["messages"][0]["content_preview"], "saved hello");
    assert!(
        v.get("message_count").is_none(),
        "saved sessions must not use a second incompatible root schema: {v}"
    );
}

#[test]
fn inspect_json_reads_supercode_native_store_messages_instead_of_reporting_empty() {
    let home = fresh_home("inspect-native-store");
    let store = SessionStore::open(home.join("sessions")).unwrap();
    store
        .save(
            "native-store-00000000000000000004",
            "native store",
            concat!(
                "{\"role\":\"user\",\"content\":\"native prompt\"}\n",
                "{\"role\":\"assistant\",\"content\":\"native reply\"}\n"
            ),
        )
        .unwrap();

    let out = run(
        &home,
        &["inspect", "native-store-00000000000000000004", "--json"],
    );
    let v = json_stdout(&out);
    assert_eq!(v["session"]["source"], "native");
    assert_eq!(
        v["session"]["session_id"],
        "native-store-00000000000000000004"
    );
    assert_eq!(v["session"]["message_count"], 2);
    assert_eq!(
        v["session"]["messages"][0]["content_preview"],
        "native prompt"
    );
    assert_eq!(
        v["session"]["messages"][1]["content_preview"],
        "native reply"
    );
}

#[test]
fn convert_reads_supercode_native_store_messages_instead_of_writing_empty_output() {
    let home = fresh_home("convert-native-store");
    let store = SessionStore::open(home.join("sessions")).unwrap();
    let name = "native-convert-00000000000000000006";
    store
        .save(
            name,
            "native convert",
            concat!(
                "{\"role\":\"user\",\"content\":\"convert prompt\"}\n",
                "{\"role\":\"assistant\",\"content\":\"convert reply\"}\n"
            ),
        )
        .unwrap();
    let converted = home.join("native.codex.jsonl");

    let out = run(
        &home,
        &[
            "convert",
            name,
            "--to",
            "codex",
            "--out",
            converted.to_str().unwrap(),
        ],
    );
    assert!(
        out.status.success(),
        "convert failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(converted.exists());

    let inspected = json_stdout(&run(
        &home,
        &["inspect", converted.to_str().unwrap(), "--json"],
    ));
    assert_eq!(inspected["session"]["source"], "codex");
    assert_eq!(inspected["session"]["message_count"], 2);
    assert_eq!(
        inspected["session"]["messages"][0]["content_preview"],
        "convert prompt"
    );
    assert_eq!(
        inspected["session"]["messages"][1]["content_preview"],
        "convert reply"
    );
}

#[test]
fn inspect_json_rejects_a_malformed_native_store_line_without_partial_output() {
    let home = fresh_home("inspect-native-store-malformed");
    let store = SessionStore::open(home.join("sessions")).unwrap();
    store
        .save(
            "native-malformed-00000000000000000005",
            "native malformed",
            "{\"role\":\"user\",\"content\":\"good prefix\"}\nnot json\n",
        )
        .unwrap();

    let out = run(
        &home,
        &["inspect", "native-malformed-00000000000000000005", "--json"],
    );
    assert!(!out.status.success());
    assert!(
        out.stdout.is_empty(),
        "must not bless a partial store transcript"
    );
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("parsing stored session"),
        "error must identify the corrupt saved transcript: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let converted = home.join("must-not-exist.codex.jsonl");
    let convert = run(
        &home,
        &[
            "convert",
            "native-malformed-00000000000000000005",
            "--to",
            "codex",
            "--out",
            converted.to_str().unwrap(),
        ],
    );
    assert!(!convert.status.success());
    assert!(convert.stdout.is_empty());
    assert!(
        !converted.exists(),
        "convert must not write a partial export for corrupt native data"
    );
    assert!(
        String::from_utf8_lossy(&convert.stderr).contains("parsing stored session"),
        "convert error must identify the corrupt saved transcript: {}",
        String::from_utf8_lossy(&convert.stderr)
    );
}

// ---- dev/02: model list / provider list -----------------------------------

#[test]
fn model_list_json_is_a_stable_alias_inventory() {
    let home = fresh_home("model-list");
    let out = run(&home, &["model", "list", "--json"]);
    let v = json_stdout(&out);
    let arr = v.as_array().expect("model list --json is an array");
    assert!(arr.len() >= 8, "expected the full alias table, got {arr:?}");
    assert!(arr.contains(&serde_json::json!({
        "alias": "sonnet",
        "slug": "anthropic/claude-sonnet-4-6",
    })));
    for entry in arr {
        assert!(entry.get("alias").and_then(|a| a.as_str()).is_some());
        assert!(entry.get("slug").and_then(|s| s.as_str()).is_some());
    }
}

#[test]
fn model_list_human_output_is_unchanged_without_the_flag() {
    let home = fresh_home("model-list-human");
    let out = run(&home, &["model", "list"]);
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("models ("));
    assert!(stdout.contains("opus"));
    assert!(serde_json::from_str::<serde_json::Value>(&stdout).is_err());
}

#[test]
fn provider_list_json_is_derived_from_the_model_alias_table() {
    let home = fresh_home("provider-list");
    let model_out = run(&home, &["model", "list", "--json"]);
    let models = json_stdout(&model_out);
    let expected_providers: std::collections::BTreeSet<String> = models
        .as_array()
        .unwrap()
        .iter()
        .map(|m| {
            m["slug"]
                .as_str()
                .unwrap()
                .split('/')
                .next()
                .unwrap()
                .to_string()
        })
        .collect();

    let out = run(&home, &["provider", "list", "--json"]);
    let v = json_stdout(&out);
    let arr = v.as_array().expect("provider list --json is an array");
    let got_providers: std::collections::BTreeSet<String> = arr
        .iter()
        .map(|p| p["name"].as_str().unwrap().to_string())
        .collect();

    // Single-source-of-truth: every vendor prefix in the alias table's
    // slugs appears as a provider, and vice versa — no hand-kept list that
    // can drift from `model list`.
    assert_eq!(
        expected_providers, got_providers,
        "provider list must never drift from the model alias table"
    );
    assert!(got_providers.contains("anthropic"));

    for p in arr {
        let name = p["name"].as_str().unwrap();
        let model_count = p["model_count"].as_u64().unwrap();
        let aliases = p["aliases"].as_array().unwrap();
        assert_eq!(model_count as usize, aliases.len());
        for a in aliases {
            let alias = a.as_str().unwrap();
            let found = models
                .as_array()
                .unwrap()
                .iter()
                .find(|m| m["alias"] == alias)
                .unwrap_or_else(|| {
                    panic!("alias `{alias}` from provider `{name}` not in model list")
                });
            assert!(found["slug"]
                .as_str()
                .unwrap()
                .starts_with(&format!("{name}/")));
        }
    }
}

#[test]
fn provider_list_human_output_is_readable() {
    let home = fresh_home("provider-list-human");
    let out = run(&home, &["provider", "list"]);
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("providers ("));
    assert!(stdout.contains("anthropic"));
    assert!(serde_json::from_str::<serde_json::Value>(&stdout).is_err());
}