use std::path::{Path, PathBuf};
use serde_json::{json, Value};
use supercode_harness::harness_service::HARNESS_SERVICE_METHODS;
use supercode_harness::jobs_control::{HERMES_BIN_ENV, OPENCLAW_BIN_ENV};
use supercode_harness::{HarnessSessionService, SdkOperation};
static BIN_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn bin_lock() -> std::sync::MutexGuard<'static, ()> {
BIN_LOCK.lock().unwrap_or_else(|error| error.into_inner())
}
fn scratch_dir(tag: &str) -> PathBuf {
let root = std::env::temp_dir().join(format!(
"supercode-orch21-{tag}-{}-{}",
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 request(method: &str, params: Value) -> Value {
json!({"jsonrpc": "2.0", "id": 1, "method": method, "params": params})
}
fn call(service: &mut HarnessSessionService, method: &str, params: Value) -> Value {
let response = service.handle(request(method, params));
assert!(response.get("error").is_none(), "{method}: {response}");
response["result"].clone()
}
fn make_executable(path: &Path) {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
}
#[cfg(not(unix))]
let _ = path;
}
fn write_fake_hermes(root: &Path) -> PathBuf {
let path = root.join("fake-hermes");
std::fs::write(
&path,
"#!/bin/sh\n\
dir=$(dirname \"$0\")\n\
for a in \"$@\"; do printf '%s\\n' \"$a\" >> \"$dir/hermes.argv\"; name=$a; done\n\
case \"$*\" in *missing*) printf \"Profile 'missing' does not exist.\\n\" >&2; exit 1 ;; esac\n\
case \"$2\" in\n\
create) mkdir -p \"$HERMES_HOME/profiles/$name\" ;;\n\
delete) rm -rf \"$HERMES_HOME/profiles/$name\" ;;\n\
esac\n\
printf 'ok\\n'\n",
)
.unwrap();
make_executable(&path);
path
}
fn write_fake_openclaw(root: &Path) -> PathBuf {
let path = root.join("fake-openclaw");
std::fs::write(
&path,
"#!/bin/sh\n\
dir=$(dirname \"$0\")\n\
for a in \"$@\"; do printf '%s\\n' \"$a\" >> \"$dir/openclaw.argv\"; done\n\
case \"$*\" in *missing*) printf 'Agent \"missing\" not found.\\n' >&2; exit 1 ;; esac\n\
verb=$2\n\
if [ -f \"$dir/openclaw.$verb.json\" ]; then\n\
cp \"$dir/openclaw.$verb.json\" \"$OPENCLAW_STATE_DIR/openclaw.json\"\n\
fi\n\
printf 'ok\\n'\n",
)
.unwrap();
make_executable(&path);
path
}
fn write_config(path: &Path, value: Value) {
std::fs::write(path, serde_json::to_string(&value).unwrap()).unwrap();
}
fn argv_log(root: &Path, harness: &str) -> String {
std::fs::read_to_string(root.join(format!("{harness}.argv"))).unwrap_or_default()
}
fn ran_arguments(result: &Value) -> String {
let ran = result["ran"]
.as_str()
.expect("every outcome narrates `ran`");
ran.split_once(' ')
.map(|(_, rest)| rest.to_string())
.unwrap_or_default()
}
fn profile_names(listing: &Value) -> Vec<String> {
listing["profiles"]
.as_array()
.expect("a profiles array")
.iter()
.map(|row| row["name"].as_str().unwrap_or_default().to_string())
.collect()
}
#[test]
fn hermes_profiles_are_made_and_removed_by_hermess_own_verb() {
let _guard = bin_lock();
let root = scratch_dir("hermes");
let home = root.join("hermes_home");
std::fs::create_dir_all(&home).unwrap();
let cli = write_fake_hermes(&root);
std::env::set_var(HERMES_BIN_ENV, &cli);
let homes = json!({"hermes": home.join("state.db")});
let mut service = HarnessSessionService::new();
let created = call(
&mut service,
"harness.v1.profiles.create",
json!({
"harness": "hermes",
"name": "coder",
"from": "default",
"homes": homes,
}),
);
assert_eq!(
ran_arguments(&created),
"profile create --clone-from default --no-alias coder"
);
assert_eq!(created["name"], "coder", "{created}");
assert_eq!(created["profile"]["kind"], "hermes_profile");
assert_eq!(created["profile"]["default"], false);
assert!(created["profile"]["home"]
.as_str()
.unwrap_or_default()
.ends_with("hermes_home/profiles/coder"));
assert!(home.join("profiles/coder").is_dir());
let log = argv_log(&root, "hermes");
assert!(
log.contains("\ncreate\n") && log.contains("\n--no-alias\n"),
"{log}"
);
let listing = call(
&mut service,
"harness.v1.profiles.list",
json!({"harness": "hermes", "homes": homes}),
);
assert!(
profile_names(&listing).contains(&"coder".to_string()),
"{listing}"
);
let deleted = call(
&mut service,
"harness.v1.profiles.delete",
json!({"harness": "hermes", "name": "coder", "homes": homes}),
);
assert_eq!(ran_arguments(&deleted), "profile delete --yes coder");
assert_eq!(deleted["deleted"], true, "{deleted}");
assert!(deleted.get("profile").is_none(), "{deleted}");
assert!(!home.join("profiles/coder").exists());
let listing = call(
&mut service,
"harness.v1.profiles.list",
json!({"harness": "hermes", "homes": homes}),
);
assert_eq!(
profile_names(&listing),
vec!["default".to_string()],
"{listing}"
);
std::env::remove_var(HERMES_BIN_ENV);
std::fs::remove_dir_all(&root).ok();
}
#[test]
fn openclaw_agents_are_added_and_deleted_by_openclaws_own_verb() {
let _guard = bin_lock();
let root = scratch_dir("openclaw");
let home = root.join("openclaw_home");
std::fs::create_dir_all(&home).unwrap();
let workspace = root.join("ws");
write_config(
&home.join("openclaw.json"),
json!({"agents": {"list": [{"id": "main"}]}}),
);
let cli = write_fake_openclaw(&root);
write_config(
&root.join("openclaw.add.json"),
json!({"agents": {"list": [
{"id": "main"},
{"id": "ops", "workspace": workspace, "model": "mock/mock-model"},
]}}),
);
write_config(
&root.join("openclaw.delete.json"),
json!({"agents": {"list": [{"id": "main"}]}}),
);
std::env::set_var(OPENCLAW_BIN_ENV, &cli);
let homes = json!({"openclaw": home});
let mut service = HarnessSessionService::new();
let created = call(
&mut service,
"harness.v1.profiles.create",
json!({
"harness": "openclaw",
"name": "ops",
"workspace": workspace,
"homes": homes,
}),
);
assert_eq!(
ran_arguments(&created),
format!(
"agents add ops --workspace {} --non-interactive --json",
workspace.display()
)
);
assert_eq!(created["name"], "ops", "{created}");
assert_eq!(created["profile"]["kind"], "openclaw_agent");
assert_eq!(created["profile"]["model"], "mock/mock-model");
let log = argv_log(&root, "openclaw");
assert!(
log.contains("\nadd\n") && log.contains("\n--non-interactive\n"),
"{log}"
);
let listing = call(
&mut service,
"harness.v1.profiles.list",
json!({"harness": "openclaw", "homes": homes}),
);
assert!(
profile_names(&listing).contains(&"ops".to_string()),
"{listing}"
);
let deleted = call(
&mut service,
"harness.v1.profiles.delete",
json!({"harness": "openclaw", "name": "ops", "homes": homes}),
);
assert_eq!(ran_arguments(&deleted), "agents delete ops --force --json");
assert_eq!(deleted["deleted"], true, "{deleted}");
let listing = call(
&mut service,
"harness.v1.profiles.list",
json!({"harness": "openclaw", "homes": homes}),
);
assert_eq!(
profile_names(&listing),
vec!["main".to_string()],
"{listing}"
);
std::env::remove_var(OPENCLAW_BIN_ENV);
std::fs::remove_dir_all(&root).ok();
}
#[test]
fn openclaw_create_needs_the_workspace_its_own_verb_demands() {
let _guard = bin_lock();
let root = scratch_dir("workspace");
let home = root.join("openclaw_home");
std::fs::create_dir_all(&home).unwrap();
write_config(
&home.join("openclaw.json"),
json!({"agents": {"list": [{"id": "main"}]}}),
);
let cli = write_fake_openclaw(&root);
std::env::set_var(OPENCLAW_BIN_ENV, &cli);
let mut service = HarnessSessionService::new();
let response = service.handle(request(
"harness.v1.profiles.create",
json!({"harness": "openclaw", "name": "ops", "homes": {"openclaw": home}}),
));
std::env::remove_var(OPENCLAW_BIN_ENV);
assert_eq!(response["error"]["code"], -32602, "{response}");
assert!(
response["error"]["message"]
.as_str()
.is_some_and(|message| message.contains("--workspace")),
"{response}"
);
assert!(argv_log(&root, "openclaw").is_empty());
std::fs::remove_dir_all(&root).ok();
}
#[test]
fn a_failing_harness_verb_surfaces_its_own_stderr() {
let _guard = bin_lock();
let root = scratch_dir("stderr");
let home = root.join("hermes_home");
std::fs::create_dir_all(&home).unwrap();
let cli = write_fake_hermes(&root);
std::env::set_var(HERMES_BIN_ENV, &cli);
let mut service = HarnessSessionService::new();
let response = service.handle(request(
"harness.v1.profiles.delete",
json!({
"harness": "hermes",
"name": "missing",
"homes": {"hermes": home.join("state.db")},
}),
));
std::env::remove_var(HERMES_BIN_ENV);
let message = response["error"]["message"].as_str().unwrap_or_default();
assert!(
message.contains("Profile 'missing' does not exist."),
"the harness's own stderr must survive: {response}"
);
assert!(response.get("result").is_none(), "{response}");
std::fs::remove_dir_all(&root).ok();
}
#[test]
fn codex_and_supercode_presets_refuse_both_verbs() {
let mut service = HarnessSessionService::new();
for (harness, needle) in [("codex", "[profiles.<name>]"), ("supercode", "CODE")] {
for method in ["harness.v1.profiles.create", "harness.v1.profiles.delete"] {
let response = service.handle(request(
method,
json!({"harness": harness, "name": "review", "workspace": "/tmp/ws"}),
));
assert_eq!(
response["error"]["code"], -32020,
"{harness} {method}: {response}"
);
let message = response["error"]["message"].as_str().unwrap_or_default();
assert!(message.contains(needle), "{harness} {method}: {response}");
assert!(
response.get("result").is_none(),
"{harness} {method}: {response}"
);
}
}
}
#[test]
fn a_harness_without_profiles_refuses_with_the_read_sides_sentence() {
let mut service = HarnessSessionService::new();
for harness in ["claude-code", "opencode", "pi"] {
let response = service.handle(request(
"harness.v1.profiles.create",
json!({"harness": harness, "name": "coder"}),
));
assert_eq!(response["error"]["code"], -32020, "{harness}: {response}");
assert!(
response["error"]["message"]
.as_str()
.is_some_and(|message| message.contains("has no profile concept")),
"{harness}: {response}"
);
}
}
#[test]
fn every_controlled_profile_method_is_declared_and_mirrored_by_the_sdk() {
for verb in ["create", "delete"] {
let method = format!("harness.v1.profiles.{verb}");
assert!(
HARNESS_SERVICE_METHODS.contains(&method.as_str()),
"{method} is not declared"
);
let operation = SdkOperation::from_method(&method)
.unwrap_or_else(|| panic!("{method} has no SDK operation"));
assert_eq!(operation.action_name(), format!("profiles_{verb}"));
}
}
#[test]
fn the_registry_reports_the_controlled_tier_only_where_a_verb_exists() {
let registry = supercode_harness::harness_support_registry();
for descriptor in ®istry.harnesses {
let Some(concept) = descriptor
.orchestration
.concepts
.iter()
.find(|concept| concept.concept == "profile")
else {
continue;
};
let controlled = matches!(
descriptor.id.as_str(),
"hermes" | "openclaw" | "orchestrator"
);
assert_eq!(
concept.controlled == supercode_harness::support::ImplementationKind::BuiltIn,
controlled,
"{}: {concept:?}",
descriptor.id.as_str()
);
if controlled {
assert!(
concept
.methods
.iter()
.any(|method| method == "harness.v1.profiles.create"),
"{}: {concept:?}",
descriptor.id.as_str()
);
}
}
}