use std::path::{Path, PathBuf};
use std::process::{Command, Output};
fn bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_supercode"))
}
fn scratch() -> PathBuf {
let root = std::env::temp_dir().join(format!(
"supercode-orch19-cli-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&root).unwrap();
root
}
fn write_rollout(codex_home: &Path, id: &str, cwd: &Path) {
let dir = codex_home.join("sessions/2026/09/03");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join(format!("rollout-2026-09-03T00-00-00-{id}.jsonl")),
format!(
"{{\"timestamp\":\"2026-09-03T00:00:00.000Z\",\"type\":\"session_meta\",\
\"payload\":{{\"id\":\"{id}\",\"cwd\":{}}}}}\n",
serde_json::to_string(&cwd.to_string_lossy()).unwrap()
),
)
.unwrap();
}
fn fake_codex(root: &Path) -> PathBuf {
let path = root.join("fake-codex");
std::fs::write(
&path,
"#!/bin/sh\n\
dir=$(dirname \"$0\")\n\
for a in \"$@\"; do printf '%s\\n' \"$a\" >> \"$dir/codex.argv\"; done\n\
found=$(find \"$CODEX_HOME/sessions\" -name \"*$2*\" 2>/dev/null | head -1)\n\
[ -z \"$found\" ] && { printf 'Error: no saved session with id %s\\n' \"$2\" >&2; exit 1; }\n\
case \"$1\" in\n\
archive) mkdir -p \"$CODEX_HOME/archived_sessions\"; mv \"$found\" \"$CODEX_HOME/archived_sessions/\" ;;\n\
delete) rm -f \"$found\" ;;\n\
esac\n\
printf 'ok\\n'\n",
)
.unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
}
path
}
fn run(root: &Path, args: &[&str]) -> Output {
Command::new(bin())
.env("CODEX_HOME", root.join("codex_home"))
.env("SUPERCODE_CODEX_BIN", root.join("fake-codex"))
.env("SUPERCODE_HOME", root.join("supercode_home"))
.args(args)
.output()
.expect("supercode binary runs")
}
fn stdout_of(output: &Output) -> String {
assert!(
output.status.success(),
"command failed: {}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8_lossy(&output.stdout).into_owned()
}
#[test]
fn archive_through_a_harnesss_own_door_narrates_the_verb_and_the_store() {
let root = scratch();
let home = root.join("codex_home");
let workspace = root.join("ws");
std::fs::create_dir_all(&workspace).unwrap();
write_rollout(&home, "cx-cli-1", &workspace);
fake_codex(&root);
let stdout = stdout_of(&run(
&root,
&["sessions", "archive", "--harness", "codex", "cx-cli-1"],
));
assert!(
stdout.contains("ran: ") && stdout.contains("archive cx-cli-1"),
"the harness's own verb must be narrated: {stdout}"
);
assert!(
stdout.contains("archived: codex conversation cx-cli-1"),
"{stdout}"
);
assert!(home
.join("archived_sessions")
.read_dir()
.unwrap()
.next()
.is_some());
let argv = std::fs::read_to_string(root.join("codex.argv")).unwrap();
assert_eq!(argv.trim(), "archive\ncx-cli-1");
}
#[test]
fn delete_through_a_harnesss_own_door_emits_the_rpc_row_with_json() {
let root = scratch();
let home = root.join("codex_home");
let workspace = root.join("ws");
std::fs::create_dir_all(&workspace).unwrap();
write_rollout(&home, "cx-cli-2", &workspace);
fake_codex(&root);
let stdout = stdout_of(&run(
&root,
&[
"sessions",
"delete",
"--harness",
"codex",
"cx-cli-2",
"--json",
],
));
let row: serde_json::Value = serde_json::from_str(&stdout).expect("--json is the RPC row");
assert_eq!(row["harness"], serde_json::json!("codex"));
assert_eq!(row["verb"], serde_json::json!("delete"));
assert_eq!(row["deleted"], serde_json::json!(true));
assert!(row["ran"].as_str().unwrap().contains("delete cx-cli-2"));
}
#[test]
fn a_harness_without_the_door_refuses_and_says_why() {
let root = scratch();
fake_codex(&root);
for (harness, verb, needle) in [
("hermes", "archive", "BULK filter verb"),
("openclaw", "delete", "v2026.7.1-2"),
("claude-code", "delete", "RETENTION WINDOW"),
] {
let output = run(&root, &["sessions", verb, "--harness", harness, "sess-1"]);
assert!(
!output.status.success(),
"{harness} {verb} must fail, not no-op"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains(needle),
"{harness} {verb} must explain itself: {stderr}"
);
}
}
#[test]
fn new_and_reset_refuse_a_harness_whose_conversation_is_the_runtime() {
let root = scratch();
let output = run(
&root,
&["sessions", "new", "--harness", "codex", "--session", "cx-1"],
);
assert!(!output.status.success());
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("harness.v1.runtimes.start"),
"the refusal must name the door that DOES exist: {stderr}"
);
}
#[test]
fn without_a_harness_the_verbs_still_act_on_supercodes_own_store() {
let root = scratch();
fake_codex(&root);
let sessions = root.join("supercode_home/sessions");
std::fs::create_dir_all(&sessions).unwrap();
let store = supercode::SessionStore::open(&sessions).unwrap();
store.save("orch19-cli", "own store", "").unwrap();
let output = run(&root, &["sessions", "archive", "orch19-cli"]);
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(String::from_utf8_lossy(&output.stderr).contains("archived"));
assert!(store
.list()
.iter()
.any(|info| info.name == "orch19-cli" && info.archived));
assert!(!root.join("codex.argv").exists());
}