use super::*;
use crate::acp_runtime::person::Attendance;
use agent_client_protocol::v1::schema::UsageUpdate;
use pretty_assertions::assert_eq;
use std::ffi::OsString;
fn shape(system_prompt: PromptPolicy, persistence: SessionPersistence) -> ClaudeSessionShape {
ClaudeSessionShape {
system_prompt,
model: Some("opus".into()),
reasoning: Some(crate::agent::ReasoningLevel::High),
tools: vec!["Read".into(), "Bash(git *)".into(), "Task".into()],
max_turns: 40,
persistence,
}
}
#[test]
fn delegated_session_carries_identity_and_fence_in_meta() {
let options = |nested: Value| {
let mut options = json!({
"tools": ["Read", "Bash"],
"allowedTools": ["Read", "Bash(git *)"],
"settingSources": ["project"],
"strictMcpConfig": true,
"maxTurns": 40,
"promptSuggestions": false,
"model": "opus",
"effort": "high",
});
let options_map = options.as_object_mut().unwrap();
for (key, value) in nested.as_object().unwrap() {
options_map.insert(key.clone(), value.clone());
}
options
};
let off = options(json!({"disallowedTools": ["Task", "Agent"]}));
let on = options(json!({"tools": ["Read", "Bash", "Task", "Agent"]}));
let cases = [
(
PermissionMode::Bypass,
NestedAgents::Off,
"bypassPermissions",
off.clone(),
),
(
PermissionMode::Bypass,
NestedAgents::Recorded,
"bypassPermissions",
on,
),
(PermissionMode::Plan, NestedAgents::Recorded, "plan", off),
];
let dir = tempfile::tempdir().unwrap();
for (mode, nested, claude_mode, options) in cases {
let policy = ClaudeAcpPolicy::delegated(
shape(
PromptPolicy::Extend("be brief".into()),
SessionPersistence::Keep,
),
mode,
false,
Attendance::Unattended,
dir.path().into(),
None,
);
assert_eq!(
(
policy.session_mode(),
Value::Object(policy.session_meta(nested).unwrap())
),
(
Some(SessionModeId::new(claude_mode)),
json!({
"systemPrompt": {"append": "be brief"},
"claudeCode": {"options": options},
})
),
"{mode:?} {nested:?}"
);
}
}
#[test]
fn headless_one_shot_replaces_the_prompt_and_discards_the_session() {
let dir = tempfile::tempdir().unwrap();
let mut shape = shape(
PromptPolicy::Replace("advise".into()),
SessionPersistence::Discard,
);
shape.tools = Vec::new();
shape.max_turns = 1;
let policy = ClaudeAcpPolicy::headless(shape, dir.path().into());
assert_eq!(
(
policy.session_mode(),
Value::Object(policy.session_meta(NestedAgents::Off).unwrap())
),
(
Some(SessionModeId::new("default")),
json!({
"systemPrompt": "advise",
"claudeCode": {"options": {
"tools": [],
"disallowedTools": ["Task", "Agent"],
"settingSources": [],
"strictMcpConfig": true,
"maxTurns": 1,
"promptSuggestions": false,
"model": "opus",
"effort": "high",
"persistSession": false,
}},
})
)
);
}
#[test]
fn usage_meta_yields_rate_limit_and_resolved_model() {
let dir = tempfile::tempdir().unwrap();
let policy = ClaudeAcpPolicy::headless(
shape(
PromptPolicy::Extend(String::new()),
SessionPersistence::Discard,
),
dir.path().into(),
);
let with_meta = |meta: Value| {
SessionUpdate::UsageUpdate(UsageUpdate::new(10, 100).meta(meta.as_object().cloned()))
};
let event = with_meta(json!({
"_claude/model": "claude-opus-5-5",
"_claude/rateLimit": {
"status": "allowed",
"resetsAt": 1_793_866_200,
"rateLimitType": "five_hour",
"isUsingOverage": false,
"unifiedWindows": {
"five_hour": {"utilization": 0.25, "resetsAt": 1_793_866_200},
"seven_day": {"utilization": 0.18, "resetsAt": 1_794_063_600},
},
},
}));
assert_eq!(
policy.status_effects(&event),
vec![
StreamEffect::RateLimit(RateLimitInfo {
status: Some("allowed".into()),
rate_limit_type: Some("five_hour".into()),
resets_at: Some(1_793_866_200),
utilization: Some(0.25),
overage_status: None,
overage_resets_at: None,
is_using_overage: Some(false),
}),
StreamEffect::Status(StatusPatch {
runtime_model: Some("claude-opus-5-5".into()),
..StatusPatch::default()
}),
]
);
assert_eq!(
policy.status_effects(&SessionUpdate::UsageUpdate(UsageUpdate::new(10, 100))),
vec![]
);
}
#[test]
fn model_pin_outranks_claude_settings() {
let unbounded_mcp: (OsString, OsString) = ("MCP_TOOL_TIMEOUT".into(), "2147483647".into());
let dir = tempfile::tempdir().unwrap();
let env_for = |model: Option<&str>| {
let mut shape = shape(
PromptPolicy::Extend(String::new()),
SessionPersistence::Keep,
);
shape.model = model.map(str::to_owned);
ClaudeAcpPolicy::headless(shape, dir.path().into())
.spawn_plan()
.unwrap()
.env
};
assert_eq!(
(env_for(Some("haiku")), env_for(None)),
(
vec![
("ANTHROPIC_MODEL".into(), "haiku".into()),
unbounded_mcp.clone()
],
vec![unbounded_mcp]
)
);
}
#[test]
fn asks_name_only_rho_served_tools() {
let dir = tempfile::tempdir().unwrap();
let policy = ClaudeAcpPolicy::headless(
shape(
PromptPolicy::Extend(String::new()),
SessionPersistence::Keep,
),
dir.path().into(),
);
let named = [
json!({"claudeCode": {"toolName": "mcp__rho__questionnaire"}}),
json!({"claudeCode": {"toolName": "Bash"}}),
json!({}),
]
.map(|meta| {
let call = AgentToolCallRef {
meta: meta.as_object(),
name: None,
kind: None,
};
policy.agent_tool(call).map(str::to_owned)
});
assert_eq!(named, [Some("questionnaire".to_owned()), None, None]);
}