use aion_core::AssistantSessionId;
use aion_integration_acp::catalogue::{self, CatalogueHarness};
use crate::config::ResolvedAssistantAccount;
use super::*;
fn harness() -> Result<&'static CatalogueHarness, String> {
catalogue::harness("claude-code")
.ok_or_else(|| "the catalogue must ship the claude-code entry".to_owned())
}
fn account() -> ResolvedAssistantAccount {
ResolvedAssistantAccount {
name: "work".to_owned(),
env: vec![("CLAUDE_CONFIG_DIR".to_owned(), "PATH".to_owned())],
}
}
fn account_with_absent_source() -> ResolvedAssistantAccount {
ResolvedAssistantAccount {
name: "work".to_owned(),
env: vec![(
"CLAUDE_CONFIG_DIR".to_owned(),
"AION_ASSISTANT_ABSENT_SOURCE_FOR_THIS_PIN".to_owned(),
)],
}
}
fn endpoint() -> AssistantEndpoints {
AssistantEndpoints {
base: "http://127.0.0.1:8080".to_owned(),
aion_mcp_enabled: true,
}
}
fn endpoint_with_general_mcp_dark() -> AssistantEndpoints {
AssistantEndpoints {
base: "http://127.0.0.1:8080".to_owned(),
aion_mcp_enabled: false,
}
}
type HttpServerParts<'specs> = (&'specs str, &'specs [(String, String)]);
fn http_server<'specs>(
specs: &'specs [McpServerSpec],
name: &str,
) -> Result<HttpServerParts<'specs>, String> {
specs
.iter()
.find_map(|spec| match spec {
McpServerSpec::Http {
name: served,
url,
headers,
} if served == name => Some((url.as_str(), headers.as_slice())),
_ => None,
})
.ok_or_else(|| format!("no MCP server named `{name}` was handed over, got {specs:?}"))
}
fn plan_if_available(
entry: &'static CatalogueHarness,
account: Option<&ResolvedAssistantAccount>,
endpoints: Option<&AssistantEndpoints>,
) -> Result<Option<HarnessPlan>, String> {
match plan(AssistantSessionId::new_v4(), entry, account, endpoints) {
Ok(plan) => Ok(Some(plan)),
Err(AssistantSessionError::HarnessUnavailable { .. }) if !entry.available() => Ok(None),
Err(error) => Err(format!("planning `{}` failed: {error}", entry.id)),
}
}
#[test]
fn a_harness_this_machine_cannot_run_is_refused_with_its_line_and_its_hint() -> Result<(), String> {
let mut planned = 0_usize;
let mut refused = 0_usize;
for entry in catalogue::CATALOGUE {
match plan(AssistantSessionId::new_v4(), entry, None, None) {
Ok(_plan) => {
assert!(
entry.available(),
"`{}` planned while its launch program does not resolve on this server's \
PATH; the refusal would then arrive at the first message instead",
entry.id
);
planned = planned.saturating_add(1);
}
Err(AssistantSessionError::HarnessUnavailable {
harness,
launch,
install_hint,
}) => {
assert!(
!entry.available(),
"`{}` was refused as unavailable while its program does resolve",
entry.id
);
assert_eq!(harness, entry.id);
assert_eq!(
launch,
entry.launch(),
"the refusal states the exact line this server would have run"
);
assert_eq!(
install_hint, entry.install_hint,
"the refusal carries the catalogue's own install sentence, so an operator is \
told what to install rather than that something went wrong"
);
refused = refused.saturating_add(1);
}
Err(other) => {
return Err(format!("`{}` failed for another reason: {other}", entry.id));
}
}
}
assert_eq!(
planned.saturating_add(refused),
catalogue::CATALOGUE.len(),
"every catalogue entry is decided one way or the other"
);
Ok(())
}
#[test]
fn the_unavailable_refusal_is_the_same_shape_selection_and_spawn_both_use() {
let error = AssistantSessionError::HarnessUnavailable {
harness: "opencode".to_owned(),
launch: "opencode acp".to_owned(),
install_hint: "install it".to_owned(),
};
assert_eq!(error.code(), "harness_unavailable");
let rendered = error.to_string();
for part in ["opencode", "opencode acp", "install it"] {
assert!(
rendered.contains(part),
"the refusal must carry `{part}`: {rendered}"
);
}
}
#[test]
fn an_account_whose_source_variable_is_absent_is_a_typed_absence_not_an_empty_value()
-> Result<(), String> {
let entry = harness()?;
let absent = account_with_absent_source();
match plan(AssistantSessionId::new_v4(), entry, Some(&absent), None) {
Err(AssistantSessionError::AccountEnvironmentAbsent {
harness,
account,
variables,
}) => {
assert_eq!(harness, entry.id);
assert_eq!(account, "work");
assert!(
variables.contains("AION_ASSISTANT_ABSENT_SOURCE_FOR_THIS_PIN"),
"the absence names the SERVER variable that is missing: {variables}"
);
assert!(
variables.contains("CLAUDE_CONFIG_DIR"),
"and the name the agent would have received, so the operator can see what the \
account was for: {variables}"
);
Ok(())
}
Err(AssistantSessionError::HarnessUnavailable { .. }) if !entry.available() => {
Ok(())
}
Err(other) => Err(format!("expected a typed absence, got: {other}")),
Ok(_plan) => Err(
"an account naming a variable this server does not carry must not spawn an agent with \
an empty one: the harness then looks logged out and nothing says why"
.to_owned(),
),
}
}
#[test]
fn an_account_whose_source_variable_is_present_is_carried_under_the_name_the_child_expects()
-> Result<(), String> {
let entry = harness()?;
let account = account();
let Some(plan) = plan_if_available(entry, Some(&account), None)? else {
return Ok(());
};
let rendered = format!("{:?}", plan.harness);
assert!(
rendered.contains("CLAUDE_CONFIG_DIR"),
"the account's variable must reach the spawn under the name the harness reads: {rendered}"
);
Ok(())
}
#[test]
fn a_server_that_can_state_no_address_hands_over_nothing_and_mints_nothing() -> Result<(), String> {
let entry = harness()?;
let Some(plan) = plan_if_available(entry, None, None)? else {
return Ok(());
};
assert!(
plan.token.is_none(),
"a session handed no endpoint of ours needs no identity of its own"
);
assert!(plan.harness.mcp_servers().is_empty());
Ok(())
}
#[test]
fn both_of_this_servers_endpoints_are_handed_over_with_one_session_scoped_bearer()
-> Result<(), String> {
let entry = harness()?;
let session_id = AssistantSessionId::new_v4();
let plan = match plan(session_id, entry, None, Some(&endpoint())) {
Ok(plan) => plan,
Err(AssistantSessionError::HarnessUnavailable { .. }) if !entry.available() => {
return Ok(());
}
Err(error) => return Err(error.to_string()),
};
let minted = plan
.token
.as_ref()
.ok_or_else(|| "a session handed an endpoint is given an identity".to_owned())?;
let specs = plan.harness.mcp_servers();
assert_eq!(specs.len(), 2, "two servers of ours, got {specs:?}");
let (aion_url, aion_headers) = http_server(specs, AION_MCP_SERVER_NAME)?;
assert_eq!(aion_url, "http://127.0.0.1:8080/mcp");
let (assistant_url, assistant_headers) = http_server(specs, ASSISTANT_MCP_SERVER_NAME)?;
assert_eq!(
assistant_url, "http://127.0.0.1:8080/assistant/mcp",
"the assistant tools are on their OWN route, never the general one"
);
for (surface, headers) in [("aion", aion_headers), ("assistant", assistant_headers)] {
let authorization = headers
.iter()
.find(|(key, _value)| key == "authorization")
.ok_or_else(|| format!("the {surface} endpoint carries a bearer"))?;
assert!(
authorization.1.ends_with(minted.secret()),
"the {surface} endpoint must carry the session's own minted bearer"
);
let session = headers
.iter()
.find(|(key, _value)| key == token::SESSION_ID_HEADER)
.ok_or_else(|| format!("the {surface} endpoint names the session it belongs to"))?;
assert_eq!(session.1, session_id.to_string());
}
Ok(())
}
#[test]
fn the_assistant_tool_server_is_handed_over_even_when_the_general_mcp_is_dark() -> Result<(), String>
{
let entry = harness()?;
let Some(plan) = plan_if_available(entry, None, Some(&endpoint_with_general_mcp_dark()))?
else {
return Ok(());
};
let specs = plan.harness.mcp_servers();
assert_eq!(specs.len(), 1, "only the assistant server, got {specs:?}");
let (url, _headers) = http_server(specs, ASSISTANT_MCP_SERVER_NAME)?;
assert_eq!(url, "http://127.0.0.1:8080/assistant/mcp");
assert!(
plan.token.is_some(),
"the assistant route is bearer-only, so a session reaching it must be given a bearer"
);
assert!(
http_server(specs, AION_MCP_SERVER_NAME).is_err(),
"the general workflow tools must NOT be handed over while `[mcp]` is dark"
);
Ok(())
}
#[test]
fn the_session_bearer_never_reaches_a_rendering_of_the_plan() -> Result<(), String> {
let entry = harness()?;
let Some(plan) = plan_if_available(entry, None, Some(&endpoint()))? else {
return Ok(());
};
let secret = plan
.token
.as_ref()
.ok_or_else(|| "the plan mints a bearer".to_owned())?
.secret()
.to_owned();
let specs = format!("{:?}", plan.harness.mcp_servers());
assert!(
specs.contains(&secret),
"the bearer must reach the child's MCP specification"
);
for rendered in [format!("{plan:?}"), format!("{:?}", plan.token)] {
assert!(
!rendered.contains(&secret),
"the session bearer must not reach a rendering: {rendered}"
);
}
Ok(())
}
#[test]
fn an_accounts_values_never_reach_a_rendering_of_the_plan() -> Result<(), String> {
let entry = harness()?;
let account = account();
let Some(plan) = plan_if_available(entry, Some(&account), None)? else {
return Ok(());
};
let value =
std::env::var("PATH").map_err(|error| format!("this venue has no PATH: {error}"))?;
let rendered = format!("{plan:?}");
assert!(
!rendered.contains(&value),
"an account's value must not reach a rendering of the plan: {rendered}"
);
Ok(())
}
#[test]
fn the_agent_environment_is_a_stated_allow_list_that_carries_what_a_launch_needs() {
for required in ["PATH", "HOME"] {
assert!(
AGENT_ENVIRONMENT.contains(&required),
"the assistant's agent environment must carry {required}"
);
}
assert!(
!AGENT_ENVIRONMENT.iter().any(|name| name.contains('=')),
"the set is NAMES, never NAME=VALUE pairs"
);
}
impl std::fmt::Debug for HarnessPlan {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("HarnessPlan")
.field("mcp_servers", &self.harness.mcp_servers().len())
.field("token", &self.token)
.finish()
}
}